ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AnalysisMessengerHelper.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AnalysisMessengerHelper.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 
27 // Author: Ivana Hrivnacova, 05/05/2015 (ivana@ipno.in2p3.fr)
28 //
29 // This messenger class is a generalization of the HistoMessenger class,
30 // originally developed for the extended/electromagnetic examples
31 // by Michel Maire (michel.maire@lapp.in2p3.fr)
32 
34 #include "G4VAnalysisManager.hh"
35 #include "G4AnalysisUtilities.hh"
36 
37 #include "G4UIdirectory.hh"
38 #include "G4UIcommand.hh"
39 #include "G4UIparameter.hh"
40 #include "G4Tokenizer.hh"
41 
42 #include <iostream>
43 #include <vector>
44 #include <algorithm>
45 
46 using namespace G4Analysis;
47 
48 namespace {
49 
50 //_____________________________________________________________________________
51 G4String ObjectType(const G4String& hnType)
52 {
53  G4String first = hnType.substr(0,1);
54  if (first == "h") {
55  return "Histogram";
56  } else if (first == "p") {
57  return "Profile";
58  } else {
59  // other possibilitied not handled
60  return "";
61  }
62 }
63 
64 //_____________________________________________________________________________
65 void Replace(std::string& str, const std::string& from, const std::string& to) {
66  // Replace all occurrences of from string
67  if (from.empty()) return;
68  size_t start_pos = 0;
69  while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
70  str.replace(start_pos, from.length(), to);
71  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
72  }
73 }
74 
75 }
76 
77 //_____________________________________________________________________________
79  : fHnType(hnType)
80 {}
81 
82 //_____________________________________________________________________________
84 {}
85 
86 //
87 // private functions
88 //
89 
90 //_____________________________________________________________________________
92 {
93  G4String newStr(str);
94 
95  // Hn, Pn
96  G4String upperHnType(str);
97  upperHnType.toUpper();
98  Replace(newStr, "UHNTYPE_", upperHnType);
99 
100  // hn, pn
101  Replace(newStr, "HNTYPE_", fHnType);
102 
103  // n = 1,2,3
104  G4String second = fHnType.substr(1,1);
105  Replace(newStr, "NDIM_", second);
106 
107  // histogram, profile
108  G4String lowerObjectType(ObjectType(fHnType));
109  lowerObjectType.toLower();
110  Replace(newStr, "LOBJECT", lowerObjectType);
111 
112  // Histogram, Profile
113  Replace(newStr, "OBJECT", ObjectType(fHnType));
114 
115  // X, Y, Z
116  G4String upperAxis(axis);
117  upperAxis.toUpper();
118  Replace(newStr, "UAXIS", upperAxis);
119 
120  // x, y, z
121  Replace(newStr, "AXIS", axis);
122 
123  // return result
124  return newStr;
125 }
126 
127 //
128 // public functions
129 //
130 
131 //_____________________________________________________________________________
132 std::unique_ptr<G4UIdirectory>
134 {
135  std::unique_ptr<G4UIdirectory> directory(new G4UIdirectory(Update("/analysis/HNTYPE_/")));
136  directory->SetGuidance(Update("NDIM_D LOBJECT control"));
137  return directory;
138 }
139 
140 //_____________________________________________________________________________
141 std::unique_ptr<G4UIcommand>
143 {
144  auto parId = new G4UIparameter("id", 'i', false);
145  parId->SetGuidance(Update("OBJECT id"));
146  parId->SetParameterRange("id>=0");
147 
148  auto parTitle = new G4UIparameter("title", 's', true);
149  parTitle->SetGuidance(Update("OBJECT title"));
150  parTitle->SetDefaultValue("none");
151 
152  std::unique_ptr<G4UIcommand> command(
153  new G4UIcommand(Update("/analysis/HNTYPE_/setTitle"), messenger));
154  command->SetGuidance(Update("Set title for the NDIM_D LOBJECT of given id"));
155  command->SetParameter(parId);
156  command->SetParameter(parTitle);
158 
159  return command;
160 }
161 
162 
163 //_____________________________________________________________________________
164 std::unique_ptr<G4UIcommand>
166  G4UImessenger* messenger) const
167 {
168  auto parId = new G4UIparameter("id", 'i', false);
169  parId->SetGuidance(Update( "OBJECT id"));
170  parId->SetParameterRange("id>=0");
171 
172  auto parNbins = new G4UIparameter("nbins", 'i', false);
173  parNbins->SetGuidance("Number of bins");
174 
175  auto parValMin = new G4UIparameter("valMin", 'd', false);
176  parValMin->SetGuidance("Minimum value, expressed in unit");
177 
178  auto parValMax = new G4UIparameter("valMax", 'd', false);
179  parValMax->SetGuidance("Maximum value, expressed in unit");
180 
181  auto parValUnit = new G4UIparameter("valUnit", 's', true);
182  parValUnit->SetGuidance("The unit applied to filled values and valMin, valMax");
183  parValUnit->SetDefaultValue("none");
184 
185  auto parValFcn = new G4UIparameter("valFcn", 's', true);
186  parValFcn->SetParameterCandidates("log log10 exp none");
187  G4String fcnGuidance = "The function applied to filled values (log, log10, exp, none).\n";
188  fcnGuidance += "Note that the unit parameter cannot be omitted in this case,\n";
189  fcnGuidance += "but none value should be used instead.";
190  parValFcn->SetGuidance(fcnGuidance);
191  parValFcn->SetDefaultValue("none");
192 
193  auto parValBinScheme = new G4UIparameter("valBinScheme", 's', true);
194  parValBinScheme->SetParameterCandidates("linear log");
195  G4String binSchemeGuidance = "The binning scheme (linear, log).\n";
196  binSchemeGuidance
197  += "Note that the unit and fcn parameters cannot be omitted in this case,\n";
198  binSchemeGuidance += "but none value should be used instead.";
199  parValBinScheme->SetGuidance(binSchemeGuidance);
200  parValBinScheme->SetDefaultValue("linear");
201 
202  auto commandName = Update("/analysis/HNTYPE_/setUAXIS", axis);
203  std::unique_ptr<G4UIcommand> command(
204  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXIS", axis), messenger));
205  command->SetGuidance(Update("Set parameters for the NDIM_D LOBJECT of given id:"));
206  command->SetGuidance(
207  Update(" nAXISbins; AXISvalMin; AXISvalMax; AXISunit; AXISfunction; AXISbinScheme", axis));
208  command->SetParameter(parId);
209  command->SetParameter(parNbins);
210  command->SetParameter(parValMin);
211  command->SetParameter(parValMax);
212  command->SetParameter(parValUnit);
213  command->SetParameter(parValFcn);
214  command->SetParameter(parValBinScheme);
216 
217  return command;
218 }
219 
220 //_____________________________________________________________________________
221  std::unique_ptr<G4UIcommand>
223  G4UImessenger* messenger) const
224 {
225  auto parId = new G4UIparameter("id", 'i', false);
226  parId->SetGuidance(Update("OBJECT id"));
227  parId->SetParameterRange("id>=0");
228 
229  auto parValMin = new G4UIparameter("valMin", 'd', false);
230  parValMin->SetGuidance(Update("Minimum AXIS-value expressed in unit", axis));
231 
232  auto parValMax = new G4UIparameter("valMax", 'd', false);
233  parValMax->SetGuidance(Update("Maximum AXIS-value expressed in unit", axis));
234 
235  auto parValUnit = new G4UIparameter("valUnit", 's', true);
236  parValUnit->SetGuidance("The unit applied to filled values and valMin, valMax");
237  parValUnit->SetDefaultValue("none");
238 
239  auto parValFcn = new G4UIparameter("valFcn", 's', true);
240  parValFcn->SetParameterCandidates("log log10 exp none");
241  G4String fcnGuidance = "The function applied to filled values (log, log10, exp, none).\n";
242  fcnGuidance += "Note that the unit parameter cannot be omitted in this case,\n";
243  fcnGuidance += "but none value should be used instead.";
244  parValFcn->SetGuidance(fcnGuidance);
245  parValFcn->SetDefaultValue("none");
246 
247  std::unique_ptr<G4UIcommand> command(
248  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXIS", axis), messenger));
249  command->SetGuidance(Update("Set parameters for the NDIM_D LOBJECT of #id:"));
250  command->SetGuidance(
251  Update(" AXISvalMin; AXISvalMax; AXISunit; AXISfunction", axis));
252  command->SetParameter(parId);
253  command->SetParameter(parValMin);
254  command->SetParameter(parValMax);
255  command->SetParameter(parValUnit);
256  command->SetParameter(parValFcn);
258 
259  return command;
260 }
261 
262 //_____________________________________________________________________________
263 std::unique_ptr<G4UIcommand>
265  G4UImessenger* messenger) const
266 {
267  auto parId = new G4UIparameter("id", 'i', false);
268  parId->SetGuidance(Update("OBJECT id"));
269  parId->SetParameterRange("id>=0");
270 
271  auto parAxis = new G4UIparameter("axis", 's', false);
272  parAxis->SetGuidance(Update("Histogram AXIS-axis title", axis));
273 
274  std::unique_ptr<G4UIcommand> command(
275  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXISaxis", axis), messenger));
276  command->SetGuidance(Update("Set AXIS-axis title for the NDIM_D LOBJECT of given id", axis));
277  command->SetParameter(parId);
278  command->SetParameter(parAxis);
280 
281  return command;
282 }
283 
284 //_____________________________________________________________________________
285 std::unique_ptr<G4UIcommand>
287  G4UImessenger* messenger) const
288 {
289  auto parId = new G4UIparameter("id", 'i', false);
290  parId->SetGuidance(Update("OBJECT id"));
291  parId->SetParameterRange("id>=0");
292 
293  auto parAxisLog = new G4UIparameter("axis", 'b', false);
294  parAxisLog->SetGuidance(Update("Histogram AXIS-axis log scale", axis));
295 
296  std::unique_ptr<G4UIcommand> command(
297  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXISaxisLog", axis), messenger));
298  command->SetGuidance(
299  Update("Activate AXIS-axis log scale for plotting of the NDIM_D LOBJECT of given id", axis));
300  command->SetParameter(parId);
301  command->SetParameter(parAxisLog);
303 
304  return command;
305 }
306 
307 //_____________________________________________________________________________
309  std::vector<G4String>& parameters,
310  G4int& counter) const
311 {
312  data.fNbins = G4UIcommand::ConvertToInt(parameters[counter++]);
313  data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
314  data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
315  data.fSunit = parameters[counter++];
316  data.fSfcn = parameters[counter++];
317  data.fSbinScheme = parameters[counter++];
318 }
319 
320 //_____________________________________________________________________________
322  std::vector<G4String>& parameters,
323  G4int& counter) const
324 {
325  data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
326  data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
327  data.fSunit = parameters[counter++];
328  data.fSfcn = parameters[counter++];
329 }
330 
331 //_____________________________________________________________________________
333  G4int nofParameters) const
334 {
335  G4ExceptionDescription description;
336  description
337  << "Got wrong number of \"" << command->GetCommandName()
338  << "\" parameters: " << nofParameters
339  << " instead of " << command->GetParameterEntries()
340  << " expected" << G4endl;
341  G4String methodName(Update("G4UHNTYPE_Messenger::SetNewValue"));
342  G4Exception(methodName,
343  "Analysis_W013", JustWarning, description);
344 }
345 
346 //_____________________________________________________________________________
348 {
349  G4ExceptionDescription description;
350  description
351  << "Command setX, setY, setZ must be called sucessively in this order. " << G4endl
352  << "Command was ignored." << G4endl;
353  G4String methodName(Update("G4UHNTYPE_Messenger::SetNewValue"));
354  G4Exception(methodName,
355  "Analysis_W013", JustWarning, description);
356 }
357 
358