Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 //_____________________________________________________________________________
92 G4String G4AnalysisMessengerHelper::Update(const G4String& str, const G4String& axis) const
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  auto commandName = Update("/analysis/HNTYPE_/setUAXIS", axis);
204  std::unique_ptr<G4UIcommand> command(
205  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXIS", axis), messenger));
206  command->SetGuidance(Update("Set parameters for the NDIM_D LOBJECT of given id:"));
207  command->SetGuidance(
208  Update(" nAXISbins; AXISvalMin; AXISvalMax; AXISunit; AXISfunction; AXISbinScheme", axis));
209  command->SetParameter(parId);
210  command->SetParameter(parNbins);
211  command->SetParameter(parValMin);
212  command->SetParameter(parValMax);
213  command->SetParameter(parValUnit);
214  command->SetParameter(parValFcn);
215  command->SetParameter(parValBinScheme);
216  command->AvailableForStates(G4State_PreInit, G4State_Idle);
217 
218  return command;
219 }
220 
221 //_____________________________________________________________________________
222  std::unique_ptr<G4UIcommand>
224  G4UImessenger* messenger) const
225 {
226  auto parId = new G4UIparameter("id", 'i', false);
227  parId->SetGuidance(Update("OBJECT id"));
228  parId->SetParameterRange("id>=0");
229 
230  auto parValMin = new G4UIparameter("valMin", 'd', false);
231  parValMin->SetGuidance(Update("Minimum AXIS-value expressed in unit", axis));
232 
233  auto parValMax = new G4UIparameter("valMax", 'd', false);
234  parValMax->SetGuidance(Update("Maximum AXIS-value expressed in unit", axis));
235 
236  auto parValUnit = new G4UIparameter("valUnit", 's', true);
237  parValUnit->SetGuidance("The unit applied to filled values and valMin, valMax");
238  parValUnit->SetDefaultValue("none");
239 
240  auto parValFcn = new G4UIparameter("valFcn", 's', true);
241  parValFcn->SetParameterCandidates("log log10 exp none");
242  G4String fcnGuidance = "The function applied to filled values (log, log10, exp, none).\n";
243  fcnGuidance += "Note that the unit parameter cannot be omitted in this case,\n";
244  fcnGuidance += "but none value should be used instead.";
245  parValFcn->SetGuidance(fcnGuidance);
246  parValFcn->SetDefaultValue("none");
247 
248  std::unique_ptr<G4UIcommand> command(
249  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXIS", axis), messenger));
250  command->SetGuidance(Update("Set parameters for the NDIM_D LOBJECT of #id:"));
251  command->SetGuidance(
252  Update(" AXISvalMin; AXISvalMax; AXISunit; AXISfunction", axis));
253  command->SetParameter(parId);
254  command->SetParameter(parValMin);
255  command->SetParameter(parValMax);
256  command->SetParameter(parValUnit);
257  command->SetParameter(parValFcn);
258  command->AvailableForStates(G4State_PreInit, G4State_Idle);
259 
260  return command;
261 }
262 
263 //_____________________________________________________________________________
264 std::unique_ptr<G4UIcommand>
266  G4UImessenger* messenger) const
267 {
268  auto parId = new G4UIparameter("id", 'i', false);
269  parId->SetGuidance(Update("OBJECT id"));
270  parId->SetParameterRange("id>=0");
271 
272  auto parAxis = new G4UIparameter("axis", 's', true);
273  parAxis->SetGuidance(Update("Histogram AXIS-axis title", axis));
274  parAxis->SetDefaultValue("none");
275 
276  std::unique_ptr<G4UIcommand> command(
277  new G4UIcommand(Update("/analysis/HNTYPE_/setUAXISaxis", axis), messenger));
278  command->SetGuidance(Update("Set AXIS-axis title for the NDIM_D LOBJECT of given id", axis));
279  command->SetParameter(parId);
280  command->SetParameter(parAxis);
281  command->AvailableForStates(G4State_PreInit, G4State_Idle);
282 
283  return command;
284 }
285 
286 //_____________________________________________________________________________
288  std::vector<G4String>& parameters,
289  G4int& counter) const
290 {
291  data.fNbins = G4UIcommand::ConvertToInt(parameters[counter++]);
292  data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
293  data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
294  data.fSunit = parameters[counter++];
295  data.fSfcn = parameters[counter++];
296  data.fSbinScheme = parameters[counter++];
297 }
298 
299 //_____________________________________________________________________________
301  std::vector<G4String>& parameters,
302  G4int& counter) const
303 {
304  data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
305  data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
306  data.fSunit = parameters[counter++];
307  data.fSfcn = parameters[counter++];
308 }
309 
310 //_____________________________________________________________________________
312  G4int nofParameters) const
313 {
314  G4ExceptionDescription description;
315  description
316  << "Got wrong number of \"" << command->GetCommandName()
317  << "\" parameters: " << nofParameters
318  << " instead of " << command->GetParameterEntries()
319  << " expected" << G4endl;
320  G4String methodName(Update("G4UHNTYPE_Messenger::SetNewValue"));
321  G4Exception(methodName,
322  "Analysis_W013", JustWarning, description);
323 }
324 
325 //_____________________________________________________________________________
327 {
328  G4ExceptionDescription description;
329  description
330  << "Command setX, setY, setZ must be called sucessively in this order. " << G4endl
331  << "Command was ignored." << G4endl;
332  G4String methodName(Update("G4UHNTYPE_Messenger::SetNewValue"));
333  G4Exception(methodName,
334  "Analysis_W013", JustWarning, description);
335 }
336 
337 
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
static constexpr double second
Definition: G4SIunits.hh:157
std::unique_ptr< G4UIcommand > CreateSetBinsCommand(const G4String &axis, G4UImessenger *messenger) const
int G4int
Definition: G4Types.hh:78
const XML_Char const XML_Char * data
Definition: expat.h:268
G4String & replace(unsigned int, unsigned int, const char *, unsigned int)
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:455
std::unique_ptr< G4UIcommand > CreateSetTitleCommand(G4UImessenger *messenger) const
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:447
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