Geant4  10.02.p01
G4AnalysisMessengerHelper.cc
Go to the documentation of this file.
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 // $Id$
27 
28 // Author: Ivana Hrivnacova, 05/05/2015 (ivana@ipno.in2p3.fr)
29 //
30 // This messenger class is a generalization of the HistoMessenger class,
31 // originally developed for the extended/electromagnetic examples
32 // by Michel Maire (michel.maire@lapp.in2p3.fr)
33 
35 #include "G4VAnalysisManager.hh"
36 #include "G4AnalysisUtilities.hh"
37 
38 #include "G4UIdirectory.hh"
39 #include "G4UIcommand.hh"
40 #include "G4UIparameter.hh"
41 #include "G4Tokenizer.hh"
42 
43 #include <iostream>
44 #include <vector>
45 #include <algorithm>
46 
47 using namespace G4Analysis;
48 
49 namespace {
50 
51 //_____________________________________________________________________________
52 G4String ObjectType(const G4String& hnType)
53 {
54  G4String first = hnType.substr(0,1);
55  if (first == "h") {
56  return "Histogram";
57  } else if (first == "p") {
58  return "Profile";
59  } else {
60  // other possibilitied not handled
61  return "";
62  }
63 }
64 
65 //_____________________________________________________________________________
66 void Replace(std::string& str, const std::string& from, const std::string& to) {
67  // Replace all occurences of from string
68  if (from.empty()) return;
69  size_t start_pos = 0;
70  while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
71  str.replace(start_pos, from.length(), to);
72  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
73  }
74 }
75 
76 }
77 
78 //_____________________________________________________________________________
80  : fHnType(hnType)
81 {}
82 
83 //_____________________________________________________________________________
85 {}
86 
87 //
88 // private functions
89 //
90 
91 //_____________________________________________________________________________
93 {
94  G4String newStr(str);
95 
96  // Hn, Pn
97  G4String upperHnType(str);
98  upperHnType.toUpper();
99  Replace(newStr, "UHNTYPE_", upperHnType);
100 
101  // hn, pn
102  Replace(newStr, "HNTYPE_", fHnType);
103 
104  // n = 1,2,3
105  G4String second = fHnType.substr(1,1);
106  Replace(newStr, "NDIM_", second);
107 
108  // histogram, profile
109  G4String lowerObjectType(ObjectType(fHnType));
110  lowerObjectType.toLower();
111  Replace(newStr, "LOBJECT", lowerObjectType);
112 
113  // Histogram, Profile
114  Replace(newStr, "OBJECT", ObjectType(fHnType));
115 
116  // X, Y, Z
117  G4String upperAxis(axis);
118  upperAxis.toUpper();
119  Replace(newStr, "UAXIS", upperAxis);
120 
121  // x, y, z
122  Replace(newStr, "AXIS", axis);
123 
124  // return result
125  return newStr;
126 }
127 
128 //
129 // public functions
130 //
131 
132 //_____________________________________________________________________________
133 std::unique_ptr<G4UIdirectory>
135 {
136  std::unique_ptr<G4UIdirectory> directory(new G4UIdirectory(Update("/analysis/HNTYPE_/")));
137  directory->SetGuidance(Update("NDIM_D LOBJECT control"));
138  return directory;
139 }
140 
141 //_____________________________________________________________________________
142 std::unique_ptr<G4UIcommand>
144 {
145  auto parId = new G4UIparameter("id", 'i', false);
146  parId->SetGuidance(Update("OBJECT id"));
147  parId->SetParameterRange("id>=0");
148 
149  auto parTitle = new G4UIparameter("title", 's', true);
150  parTitle->SetGuidance(Update("OBJECT title"));
151  parTitle->SetDefaultValue("none");
152 
153  std::unique_ptr<G4UIcommand> command(
154  new G4UIcommand(Update("/analysis/HNTYPE_/setTitle"), messenger));
155  command->SetGuidance(Update("Set title for the NDIM_D LOBJECT of given id"));
156  command->SetParameter(parId);
157  command->SetParameter(parTitle);
158  command->AvailableForStates(G4State_PreInit, G4State_Idle);
159 
160  return command;
161 }
162 
163 
164 //_____________________________________________________________________________
165 std::unique_ptr<G4UIcommand>
167  G4UImessenger* messenger) const
168 {
169  auto parId = new G4UIparameter("id", 'i', false);
170  parId->SetGuidance(Update( "OBJECT id"));
171  parId->SetParameterRange("id>=0");
172 
173  auto parNbins = new G4UIparameter("nbins", 'i', false);
174  parNbins->SetGuidance("Number of bins");
175 
176  auto parValMin = new G4UIparameter("valMin", 'd', false);
177  parValMin->SetGuidance("Minimum value, expressed in unit");
178 
179  auto parValMax = new G4UIparameter("valMax", 'd', false);
180  parValMax->SetGuidance("Maximum value, expressed in unit");
181 
182  auto parValUnit = new G4UIparameter("valUnit", 's', true);
183  parValUnit->SetGuidance("The unit applied to filled values and valMin, valMax");
184  parValUnit->SetDefaultValue("none");
185 
186  auto parValFcn = new G4UIparameter("valFcn", 's', true);
187  parValFcn->SetParameterCandidates("log log10 exp none");
188  G4String fcnGuidance = "The function applied to filled values (log, log10, exp, none).\n";
189  fcnGuidance += "Note that the unit parameter cannot be omitted in this case,\n";
190  fcnGuidance += "but none value should be used instead.";
191  parValFcn->SetGuidance(fcnGuidance);
192  parValFcn->SetDefaultValue("none");
193 
194  auto parValBinScheme = new G4UIparameter("valBinScheme", 's', true);
195  parValBinScheme->SetParameterCandidates("linear log");
196  G4String binSchemeGuidance = "The binning scheme (linear, log).\n";
197  binSchemeGuidance
198  += "Note that the unit and fcn parameters cannot be omitted in this case,\n";
199  binSchemeGuidance += "but none value should be used instead.";
200  parValBinScheme->SetGuidance(binSchemeGuidance);
201  parValBinScheme->SetDefaultValue("linear");
202 
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);
215  command->AvailableForStates(G4State_PreInit, G4State_Idle);
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->AvailableForStates(G4State_PreInit, G4State_Idle);
253 
254  return command;
255 }
256 
257 //_____________________________________________________________________________
258 std::unique_ptr<G4UIcommand>
260  G4UImessenger* messenger) const
261 {
262  auto parId = new G4UIparameter("id", 'i', false);
263  parId->SetGuidance(Update("OBJECT id"));
264  parId->SetParameterRange("id>=0");
265 
266  auto parAxis = new G4UIparameter("axis", 's', true);
267  parAxis->SetGuidance(Update("Histogram AXIS-axis title", axis));
268  parAxis->SetDefaultValue("none");
269 
270  std::unique_ptr<G4UIcommand> command(
271  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXISaxis", axis), messenger));
272  command->SetGuidance(Update("Set AXIS-axis title for the NDIM_D LOBJECT of given id", axis));
273  command->SetParameter(parId);
274  command->SetParameter(parAxis);
275  command->AvailableForStates(G4State_PreInit, G4State_Idle);
276 
277  return command;
278 }
279 
280 //_____________________________________________________________________________
282  std::vector<G4String>& parameters,
283  G4int& counter) const
284 {
285  data.fNbins = G4UIcommand::ConvertToInt(parameters[counter++]);
286  data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
287  data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
288  data.fSunit = parameters[counter++];
289  data.fSfcn = parameters[counter++];
290  data.fSbinScheme = parameters[counter++];
291 }
292 
293 //_____________________________________________________________________________
295  std::vector<G4String>& parameters,
296  G4int& counter) const
297 {
298  data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
299  data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
300  data.fSunit = parameters[counter++];
301  data.fSfcn = parameters[counter++];
302 }
303 
304 //_____________________________________________________________________________
306  G4int nofParameters) const
307 {
308  G4ExceptionDescription description;
309  description
310  << "Got wrong number of \"" << command->GetCommandName()
311  << "\" parameters: " << nofParameters
312  << " instead of " << command->GetParameterEntries()
313  << " expected" << G4endl;
314  G4String methodName(Update("G4UHNTYPE_Messenger::SetNewValue"));
315  G4Exception(methodName,
316  "Analysis_W013", JustWarning, description);
317 }
318 
319 //_____________________________________________________________________________
321 {
322  G4ExceptionDescription description;
323  description
324  << "Command setX, setY, setZ must be called sucessively in this order. " << G4endl
325  << "Command was ignored." << G4endl;
326  G4String methodName(Update("G4UHNTYPE_Messenger::SetNewValue"));
327  G4Exception(methodName,
328  "Analysis_W013", JustWarning, description);
329 }
330 
331 
G4String Update(const G4String &str, const G4String &axis="") const
void WarnAboutParameters(G4UIcommand *command, G4int nofParameters) const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::unique_ptr< G4UIcommand > CreateSetValuesCommand(const G4String &axis, G4UImessenger *messenger) const
std::unique_ptr< G4UIcommand > CreateSetAxisCommand(const G4String &axis, G4UImessenger *messenger) const
std::unique_ptr< G4UIcommand > CreateSetBinsCommand(const G4String &axis, G4UImessenger *messenger) const
int G4int
Definition: G4Types.hh:78
G4String & replace(unsigned int, unsigned int, const char *, unsigned int)
void toUpper()
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:443
static const double second
Definition: G4SIunits.hh:156
std::unique_ptr< G4UIcommand > CreateSetTitleCommand(G4UImessenger *messenger) const
void toLower()
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:435
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
G4AnalysisMessengerHelper(const G4String &hnType)
#define G4endl
Definition: G4ios.hh:61
void GetBinData(BinData &data, std::vector< G4String > &parameters, G4int &counter) const
G4int GetParameterEntries() const
Definition: G4UIcommand.hh:143
void GetValueData(ValueData &data, std::vector< G4String > &parameters, G4int &counter) const
std::unique_ptr< G4UIdirectory > CreateHnDirectory() const