EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eASTRunActionMessenger.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file eASTRunActionMessenger.cc
1 // ********************************************************************
2 //
3 // eASTRunActionMessenger.cc
4 // A messenger class that defines histograms and n-tuple in eASTRunAction.
5 //
6 // History
7 // September 8th, 2020 : first implementation
8 //
9 // ********************************************************************
10 
12 
13 #include "eASTRunAction.hh"
14 #include "G4UIcommand.hh"
15 #include "G4UIparameter.hh"
16 #include "G4UIdirectory.hh"
17 #include "G4UIcmdWithAString.hh"
18 #include "G4UIcmdWithABool.hh"
19 #include "G4UIcmdWithAnInteger.hh"
20 #include "G4UIcmdWithoutParameter.hh"
21 #include "G4UnitsTable.hh"
22 
24 : pRA(dc), currentID(-1)
25 {
26  G4UIparameter* para = nullptr;
27 
28  anaDir = new G4UIdirectory("/eAST/analysis/");
29  anaDir->SetGuidance("GORAD analysis commands");
30 
31  verboseCmd = new G4UIcmdWithAnInteger("/eAST/analysis/verbose",this);
32  verboseCmd->SetGuidance("Set verbose level");
33  verboseCmd->SetParameterName("level",true);
34  verboseCmd->SetDefaultValue(0);
35  verboseCmd->SetRange("level>=0");
36  verboseCmd->AvailableForStates(G4State_Idle);
37 
38  fileCmd = new G4UIcmdWithAString("/eAST/analysis/file",this);
39  fileCmd->SetGuidance("Define the output file name.");
40  fileCmd->SetParameterName("file",false);
41  fileCmd->AvailableForStates(G4State_Idle);
42 
43  listCmd = new G4UIcmdWithoutParameter("/eAST/analysis/list",this);
44  listCmd->SetGuidance("List defined histograms.");
45  listCmd->AvailableForStates(G4State_Idle);
46 
47  openCmd = new G4UIcmdWithAnInteger("/eAST/analysis/open",this);
48  openCmd->SetGuidance("Open a histogram that has already been created and closed.");
49  openCmd->SetGuidance("\"create\" command opens a new histogram so you don't need to open it.");
50  openCmd->SetGuidance("This \"open\" command is required only for reopening a closed histogram.");
51  openCmd->SetParameterName("id",false);
52  openCmd->AvailableForStates(G4State_Idle);
53 
54  plotCmd = new G4UIcmdWithAnInteger("/eAST/analysis/plot",this);
55  plotCmd->SetGuidance("Create an additional postscript plot for specified histogram/profile.");
56  plotCmd->SetGuidance("Regardless of this command, histogram is dumped to the output file.");
57  plotCmd->SetGuidance("If id is not specified, currently open histogram/profile is plotted.");
58  plotCmd->SetGuidance("If id = -1, all currently defined histograms/profiles are plotted.");
59  plotCmd->SetParameterName("id",true,true);
60  plotCmd->AvailableForStates(G4State_Idle);
61 
62  carryCmd = new G4UIcmdWithABool("/eAST/analysis/carry",this);
63  carryCmd->SetGuidance("Carry histograms over more than one runs.");
64  carryCmd->SetGuidance("Once this is set, histograms won't be output until /eAST/analysis/flush is explicitly issued.");
65  carryCmd->SetGuidance("This command has to be issued before starting the run to be carried over.");
66  carryCmd->SetParameterName("carry",true);
67  carryCmd->SetDefaultValue(true);
68  carryCmd->AvailableForStates(G4State_Idle);
69 
70  flushCmd = new G4UIcmdWithoutParameter("/eAST/analysis/flush",this);
71  flushCmd->SetGuidance("Make output. This command is necessary if /eAST/analysis/carry is set.");
72  flushCmd->AvailableForStates(G4State_Idle);
73 
74  resetCmd = new G4UIcmdWithoutParameter("/eAST/analysis/reset",this);
75  resetCmd->SetGuidance("Reset histograms without making output.");
76  resetCmd->AvailableForStates(G4State_Idle);
77 
78  oneDDir = new G4UIdirectory("/eAST/analysis/1D/");
79  oneDDir->SetGuidance("1-dimentional histogram");
80 
81  create1DCmd = new G4UIcommand("/eAST/analysis/1D/create",this);
82  create1DCmd->SetGuidance("Create a 1D histogram and fill it with event-by-event score.");
83  create1DCmd->SetGuidance("Scoring mesh (logical volume for real-world volume scoring) and");
84  create1DCmd->SetGuidance("primitive scorers must be defined prior to this command.");
85  para = new G4UIparameter("meshName",'s',false);
86  para->SetGuidance("Scoring mesh name. Logical volume name for real-world volume scoring.");
87  create1DCmd->SetParameter(para);
88  para = new G4UIparameter("primName",'s',false);
89  create1DCmd->SetParameter(para);
90  para = new G4UIparameter("idx",'i',true);
91  para->SetGuidance("Index (i.e. copy number) of the cell to be scored. \"-1\" (defult) to score all cells.");
92  para->SetDefaultValue(-1);
93  para->SetParameterRange("idx>=-1");
94  create1DCmd->SetParameter(para);
95  create1DCmd->AvailableForStates(G4State_Idle);
96 
97  create1DPlotPCmd = new G4UIcommand("/eAST/analysis/1D/spectrum",this);
98  create1DPlotPCmd->SetGuidance("Create a 1D energy spectrum histogram and fill it with each individual track that gets into the volume.");
99  create1DPlotPCmd->SetGuidance("Histogram is created for each physical volume separately.");
100  create1DPlotPCmd->SetGuidance("So, this command should not be used for Box or Cylinder mesh type due to memory consumption concern.");
101  create1DPlotPCmd->SetGuidance("Currently, this is supported only for some scorers.");
102  para = new G4UIparameter("meshName",'s',false);
103  para->SetGuidance("Scoring mesh name. Logical volume name for real-world volume scoring.");
104  create1DPlotPCmd->SetParameter(para);
105  para = new G4UIparameter("primName",'s',false);
106  para->SetGuidance("Scoring primitive name.");
107  create1DPlotPCmd->SetParameter(para);
108  create1DPlotPCmd->AvailableForStates(G4State_Idle);
109 
110  config1DCmd = new G4UIcommand("/eAST/analysis/1D/config",this);
111  config1DCmd->SetGuidance("Set binning parameters of the current 1D histogram.");
112  config1DCmd->SetGuidance("<unit> is applied to <minVal> and <maxVal> as well as filled value.");
113  para = new G4UIparameter("nBin",'i',false);
114  para->SetParameterRange("nBin>0");
115  config1DCmd->SetParameter(para);
116  para = new G4UIparameter("minVal",'d',false);
117  config1DCmd->SetParameter(para);
118  para = new G4UIparameter("maxVal",'d',false);
119  config1DCmd->SetParameter(para);
120  para = new G4UIparameter("unit",'s',true);
121  para->SetDefaultValue("none");
122  config1DCmd->SetParameter(para);
123  para = new G4UIparameter("scale",'s',true);
124  para->SetGuidance("Define the binning scale. (default: linear)");
125  para->SetParameterCandidates("linear log");
126  para->SetDefaultValue("linear");
127  config1DCmd->SetParameter(para);
128  para = new G4UIparameter("logVal",'b',true);
129  para->SetDefaultValue(false);
130  config1DCmd->SetParameter(para);
131  config1DCmd->AvailableForStates(G4State_Idle);
132 
133  title1DCmd = new G4UIcommand("/eAST/analysis/1D/title",this);
134  title1DCmd->SetGuidance("Define histogram title");
135  para = new G4UIparameter("title",'s',false);
136  title1DCmd->SetParameter(para);
137  para = new G4UIparameter("x_axis",'s',false);
138  title1DCmd->SetParameter(para);
139  para = new G4UIparameter("y_axis",'s',false);
140  title1DCmd->SetParameter(para);
141  title1DCmd->AvailableForStates(G4State_Idle);
142 
143  set1DYaxisLogCmd = new G4UIcmdWithABool("/eAST/analysis/1D/yaxisLog",this);
144  set1DYaxisLogCmd->SetGuidance("Set y-axis in log scale.");
145  set1DYaxisLogCmd->SetParameterName("flag",true);
146  set1DYaxisLogCmd->SetDefaultValue(true);
147  set1DYaxisLogCmd->AvailableForStates(G4State_Idle);
148 
149  onePDir = new G4UIdirectory("/eAST/analysis/1P/");
150  onePDir->SetGuidance("1-dimentional profile plot");
151 
152  create1PCmd = new G4UIcommand("/eAST/analysis/1P/create",this);
153  create1PCmd->SetGuidance("Create a 1D profile plot and fill it with event-by-event score.");
154  create1PCmd->SetGuidance("Scoring mesh (logical volume for real-world volume scoring) and");
155  create1PCmd->SetGuidance("primitive scorers must be defined prior to this command.");
156  create1PCmd->SetGuidance("Copy number of the scoring cell is used as the x-axis value.");
157  para = new G4UIparameter("meshName",'s',false);
158  para->SetGuidance("Scoring mesh name. Logical volume name for real-world volume scoring.");
159  create1PCmd->SetParameter(para);
160  para = new G4UIparameter("primName",'s',false);
161  create1PCmd->SetParameter(para);
162  para = new G4UIparameter("idx",'i',false);
163  para->SetGuidance("Maximum index (i.e. copy number) of the cell to be scored.");
164  para->SetParameterRange("idx>0");
165  create1PCmd->SetParameter(para);
166  create1PCmd->AvailableForStates(G4State_Idle);
167 
168  set1PCmd = new G4UIcommand("/eAST/analysis/1P/set",this);
169  set1PCmd->SetGuidance("Set binning parameters of the current 1D profile plot.");
170  set1PCmd->SetGuidance("<unit> is applied to <minYVal> and <maxYVal> as well as filled value.");
171  para = new G4UIparameter("minYVal",'d',false);
172  set1PCmd->SetParameter(para);
173  para = new G4UIparameter("maxYVal",'d',false);
174  set1PCmd->SetParameter(para);
175  para = new G4UIparameter("unit",'s',true);
176  para->SetDefaultValue("none");
177  set1PCmd->SetParameter(para);
178  para = new G4UIparameter("func-x",'s',true);
179  para->SetGuidance("The function applied to the filled x-value (default: none).");
180  para->SetParameterCandidates("log log10 exp none");
181  para->SetDefaultValue("none");
182  set1PCmd->SetParameter(para);
183  para = new G4UIparameter("func-y",'s',true);
184  para->SetGuidance("The function applied to the filled y-value (default: none).");
185  para->SetParameterCandidates("log log10 exp none");
186  para->SetDefaultValue("none");
187  set1PCmd->SetParameter(para);
188  para = new G4UIparameter("scale",'s',true);
189  para->SetGuidance("Define the binning scale. (default: linear)");
190  para->SetParameterCandidates("linear log");
191  para->SetDefaultValue("linear");
192  set1PCmd->SetParameter(para);
193  set1PCmd->AvailableForStates(G4State_Idle);
194 
195  title1PCmd = new G4UIcommand("/eAST/analysis/1P/title",this);
196  title1PCmd->SetGuidance("Define histogram title");
197  para = new G4UIparameter("title",'s',false);
198  title1PCmd->SetParameter(para);
199  para = new G4UIparameter("x_axis",'s',false);
200  title1PCmd->SetParameter(para);
201  para = new G4UIparameter("y_axis",'s',false);
202  title1PCmd->SetParameter(para);
203  title1PCmd->AvailableForStates(G4State_Idle);
204 
205  ntupleDir = new G4UIdirectory("/eAST/analysis/ntuple/");
206  onePDir->SetGuidance("Define an ntuple");
207 
208  addColumnCmd = new G4UIcommand("/eAST/analysis/ntuple/addColumn",this);
209  addColumnCmd->SetGuidance("Define a column and fill it with event-by-event score.");
210  addColumnCmd->SetGuidance("Scoring mesh (logical volume for real-world volume scoring) and");
211  addColumnCmd->SetGuidance("primitive scorers must be defined prior to this command.");
212  para = new G4UIparameter("meshName",'s',false);
213  para->SetGuidance("Scoring mesh name. Logical volume name for real-world volume scoring.");
214  addColumnCmd->SetParameter(para);
215  para = new G4UIparameter("primName",'s',false);
216  addColumnCmd->SetParameter(para);
217  para = new G4UIparameter("unit",'s',true);
218  para->SetDefaultValue("none");
219  addColumnCmd->SetParameter(para);
220  para = new G4UIparameter("idx",'i',true);
221  para->SetGuidance("Index (i.e. copy number) of the cell to be scored. \"-1\" (defult) to score all cells.");
222  para->SetDefaultValue(-1);
223  para->SetParameterRange("idx>=-1");
224  addColumnCmd->SetParameter(para);
225  addColumnCmd->AvailableForStates(G4State_Idle);
226 }
227 
229 {
230  delete addColumnCmd;
231  delete ntupleDir;
232 
233  delete create1PCmd;
234  delete set1PCmd;
235  delete title1PCmd;
236  delete onePDir;
237 
238  delete create1DCmd;
239  delete create1DPlotPCmd;
240  delete config1DCmd;
241  delete title1DCmd;
242  delete set1DYaxisLogCmd;
243  delete oneDDir;
244 
245  delete fileCmd;
246  delete verboseCmd;
247  delete listCmd;
248  delete openCmd;
249  delete plotCmd;
250  delete carryCmd;
251  delete flushCmd;
252  delete resetCmd;
253  delete anaDir;
254 }
255 
256 #include "G4Tokenizer.hh"
257 
258 void eASTRunActionMessenger::SetNewValue(G4UIcommand* cmd, G4String val)
259 {
260  if(cmd==fileCmd)
261  { pRA->SetFileName(val); }
262  else if(cmd==verboseCmd)
263  { pRA->SetVerbose(verboseCmd->GetNewIntValue(val)); }
264  else if(cmd==listCmd)
265  { pRA->ListHistograms(); }
266  else if(cmd==openCmd)
267  {
268  auto id = openCmd->GetNewIntValue(val);
269  if(currentID!=id)
270  {
271  if(!CheckOpenID(cmd)) return;
272  auto valid = pRA->Open(id);
273  if(!valid)
274  {
275  G4ExceptionDescription ed;
276  ed << "<" << id << "> is not a valid histogram ID.";
277  cmd->CommandFailed(ed);
278  }
279  else
280  { currentID = id; }
281  }
282  }
283  else if(cmd==plotCmd)
284  {
285  auto id = plotCmd->GetNewIntValue(val);
286  G4bool valid = true;
287  if(id==-1)
288  { valid = pRA->SetAllPlotting(true); }
289  else
290  { valid = pRA->SetPlotting(id,true); }
291  if(!valid)
292  {
293  G4ExceptionDescription ed;
294  ed << "Histogram/profile id <" << id << "> is not valid.";
295  cmd->CommandFailed(ed);
296  }
297  }
298  else if(cmd==carryCmd)
299  { pRA->SetCarry(carryCmd->GetNewBoolValue(val)); }
300  else if(cmd==flushCmd)
301  { pRA->Flush(); }
302  else if(cmd==resetCmd)
303  { /*pRA->ResetHistograms();*/
304  G4cout << "Not yet implemented. Sorry." << G4endl;
305  }
306 
307  // 1D histogram commands
308 
309  else if(cmd==create1DCmd)
310  {
311  if(!CheckOpenID(cmd)) return;
312  G4Tokenizer next(val);
313  G4String meshName = next();
314  G4String primName = next();
315  G4int idx = StoI(next());
316  auto id = pRA->Create1D(meshName,primName,idx);
317  if(id<0)
318  {
319  G4ExceptionDescription ed;
320  ed << "1D histogram <" << val << "> cannot be created.";
321  cmd->CommandFailed(ed);
322  }
323  else
324  { currentID = id; }
325  }
326  else if(cmd==create1DPlotPCmd)
327  {
328  if(!CheckOpenID(cmd)) return;
329  G4Tokenizer next(val);
330  G4String meshName = next();
331  G4String primName = next();
332  G4bool wgt = true;
333  auto id = pRA->Create1DForPlotter(meshName,primName,wgt);
334  if(id<0)
335  {
336  G4ExceptionDescription ed;
337  ed << "1D histogram <" << val << "> cannot be created.";
338  cmd->CommandFailed(ed);
339  }
340  else
341  { currentID = id; }
342  }
343  else if(cmd==config1DCmd)
344  {
345  if(!CheckID(cmd)) return;
346  G4Tokenizer next(val);
347  G4int nBin = StoI(next());
348  G4double minVal = StoD(next());
349  G4double maxVal = StoD(next());
350  G4String unit = next();
351  G4String schem = next();
352  G4bool logVal = StoB(next());
353  if(unit!="none" && !(G4UnitDefinition::IsUnitDefined(unit)))
354  {
355  G4ExceptionDescription ed;
356  ed << "Unknown unit <" << unit << ">. Command failed.";
357  cmd->CommandFailed(ed);
358  }
359  else
360  { pRA->Set1D(currentID,nBin,minVal,maxVal,unit,schem,logVal); }
361  }
362  else if(cmd==title1DCmd)
363  {
364  if(!CheckID(cmd)) return;
365  G4Tokenizer next(val);
366  G4String title = next();
367  G4String x_axis = next();
368  G4String y_axis = next();
369  pRA->Set1DTitle(currentID,title,x_axis,y_axis);
370  }
371  else if(cmd==set1DYaxisLogCmd)
372  {
373  if(!CheckID(cmd)) return;
374  auto succ = pRA->Set1DYAxisLog(currentID,set1DYaxisLogCmd->GetNewBoolValue(val));
375  if(!succ)
376  {
377  G4ExceptionDescription ed;
378  ed << "This command is not available for this histogram.";
379  cmd->CommandFailed(ed);
380  }
381  }
382 
383  // 1D profile commands
384 
385  else if(cmd==create1PCmd)
386  {
387  if(!CheckOpenID(cmd)) return;
388  G4Tokenizer next(val);
389  G4String meshName = next();
390  G4String primName = next();
391  G4int cn = StoI(next());
392  auto id = pRA->Create1P(meshName,primName,cn);
393  if(id<0)
394  {
395  G4ExceptionDescription ed;
396  ed << "1D histogram <" << val << "> cannot be created.";
397  cmd->CommandFailed(ed);
398  }
399  else
400  { currentID = id; }
401  }
402  else if(cmd==set1PCmd)
403  {
404  if(!CheckID(cmd)) return;
405  G4Tokenizer next(val);
406  G4double minYVal = StoD(next());
407  G4double maxYVal = StoD(next());
408  G4String unit = next();
409  G4String funcX = next();
410  G4String funcY = next();
411  G4String schem = next();
412  if(unit!="none" && !(G4UnitDefinition::IsUnitDefined(unit)))
413  {
414  G4ExceptionDescription ed;
415  ed << "Unknown unit <" << unit << ">. Command failed.";
416  cmd->CommandFailed(ed);
417  }
418  else
419  { pRA->Set1P(currentID,minYVal,maxYVal,unit,funcX,funcY,schem); }
420  }
421  else if(cmd==title1PCmd)
422  {
423  if(!CheckID(cmd)) return;
424  G4Tokenizer next(val);
425  G4String title = next();
426  G4String x_axis = next();
427  G4String y_axis = next();
428  pRA->Set1PTitle(currentID,title,x_axis,y_axis);
429  }
430 
431  // ntuple commands
432 
433  else if(cmd==addColumnCmd)
434  {
435  G4Tokenizer next(val);
436  G4String meshName = next();
437  G4String primName = next();
438  G4String unit = next();
439  G4int idx = StoI(next());
440  if(unit!="none" && !(G4UnitDefinition::IsUnitDefined(unit)))
441  {
442  G4ExceptionDescription ed;
443  ed << "Unknown unit <" << unit << ">. Command failed.";
444  cmd->CommandFailed(ed);
445  }
446  else
447  {
448  auto id = pRA->NtupleColumn(meshName,primName,unit,idx);
449  if(id<0)
450  {
451  G4ExceptionDescription ed;
452  ed << "Ntuple column <" << val << "> cannot be created.";
453  cmd->CommandFailed(ed);
454  }
455  }
456  }
457 }
458 
459 G4String eASTRunActionMessenger::GetCurrentValue(G4UIcommand* cmd)
460 {
461  G4String val("");
462  if(cmd==openCmd)
463  { val = openCmd->ConvertToString(currentID); }
464  if(cmd==fileCmd)
465  { val = pRA->GetFileName(); }
466  else if(cmd==verboseCmd)
467  { val = verboseCmd->ConvertToString(pRA->GetVerbose()); }
468  else if(cmd==plotCmd)
469  { val = plotCmd->ConvertToString(currentID); }
470  else if(cmd==carryCmd)
471  { val = carryCmd->ConvertToString(pRA->GetCarry()); }
472 
473  return val;
474 }
475 
476