Geant4-11
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
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"
36
37#include "G4UIdirectory.hh"
38#include "G4UIcommand.hh"
39#include "G4UIparameter.hh"
40#include "G4Tokenizer.hh"
41
42#include <vector>
43#include <algorithm>
44
45using namespace G4Analysis;
46using std::to_string;
47
48namespace {
49
50//_____________________________________________________________________________
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//_____________________________________________________________________________
65void 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//
83// private functions
84//
85
86//_____________________________________________________________________________
88{
89 G4String newStr(str);
90
91 // Hn, Pn
92 G4String upperHnType = G4StrUtil::to_upper_copy(str);
93 Replace(newStr, "UHNTYPE_", upperHnType);
94
95 // hn, pn
96 Replace(newStr, "HNTYPE_", fHnType);
97
98 // n = 1,2,3
99 G4String second = fHnType.substr(1,1);
100 Replace(newStr, "NDIM_", second);
101
102 // histogram, profile
104 Replace(newStr, "LOBJECT", lowerObjectType);
105
106 // Histogram, Profile
107 Replace(newStr, "OBJECT", ObjectType(fHnType));
108
109 // X, Y, Z
110 G4String upperAxis = G4StrUtil::to_upper_copy(axis);
111 Replace(newStr, "UAXIS", upperAxis);
112
113 // x, y, z
114 Replace(newStr, "AXIS", axis);
115
116 // return result
117 return newStr;
118}
119
120//
121// public functions
122//
123
124//_____________________________________________________________________________
125std::unique_ptr<G4UIdirectory>
127{
128 std::unique_ptr<G4UIdirectory> directory(new G4UIdirectory(Update("/analysis/HNTYPE_/")));
129 directory->SetGuidance(Update("NDIM_D LOBJECT control"));
130 return directory;
131}
132
133//_____________________________________________________________________________
134std::unique_ptr<G4UIcommand>
136{
137 auto parId = new G4UIparameter("id", 'i', false);
138 parId->SetGuidance(Update("OBJECT id"));
139 parId->SetParameterRange("id>=0");
140
141 std::unique_ptr<G4UIcommand> command(
142 new G4UIcommand(Update("/analysis/HNTYPE_/get"), messenger));
143 command->SetGuidance(Update("Get the address of the NDIM_D LOBJECT of given id"));
144 command->SetParameter(parId);
145 command->AvailableForStates(G4State_Idle, G4State_GeomClosed, G4State_EventProc);
146
147 return command;
148}
149
150//_____________________________________________________________________________
151std::unique_ptr<G4UIcommand>
153{
154 auto parId = new G4UIparameter("id", 'i', false);
155 parId->SetGuidance(Update("OBJECT id"));
156 parId->SetParameterRange("id>=0");
157
158 auto parTitle = new G4UIparameter("title", 's', true);
159 parTitle->SetGuidance(Update("OBJECT title"));
160 parTitle->SetDefaultValue("none");
161
162 std::unique_ptr<G4UIcommand> command(
163 new G4UIcommand(Update("/analysis/HNTYPE_/setTitle"), messenger));
164 command->SetGuidance(Update("Set title for the NDIM_D LOBJECT of given id"));
165 command->SetParameter(parId);
166 command->SetParameter(parTitle);
167 command->AvailableForStates(G4State_PreInit, G4State_Idle);
168
169 return command;
170}
171
172
173//_____________________________________________________________________________
174std::unique_ptr<G4UIcommand>
176 G4UImessenger* messenger) const
177{
178 auto parId = new G4UIparameter("id", 'i', false);
179 parId->SetGuidance(Update( "OBJECT id"));
180 parId->SetParameterRange("id>=0");
181
182 auto parNbins = new G4UIparameter("nbins", 'i', false);
183 parNbins->SetGuidance("Number of bins");
184
185 auto parValMin = new G4UIparameter("valMin", 'd', false);
186 parValMin->SetGuidance("Minimum value, expressed in unit");
187
188 auto parValMax = new G4UIparameter("valMax", 'd', false);
189 parValMax->SetGuidance("Maximum value, expressed in unit");
190
191 auto parValUnit = new G4UIparameter("valUnit", 's', true);
192 parValUnit->SetGuidance("The unit applied to filled values and valMin, valMax");
193 parValUnit->SetDefaultValue("none");
194
195 auto parValFcn = new G4UIparameter("valFcn", 's', true);
196 parValFcn->SetParameterCandidates("log log10 exp none");
197 G4String fcnGuidance = "The function applied to filled values (log, log10, exp, none).\n";
198 fcnGuidance += "Note that the unit parameter cannot be omitted in this case,\n";
199 fcnGuidance += "but none value should be used instead.";
200 parValFcn->SetGuidance(fcnGuidance);
201 parValFcn->SetDefaultValue("none");
202
203 auto parValBinScheme = new G4UIparameter("valBinScheme", 's', true);
204 parValBinScheme->SetParameterCandidates("linear log");
205 G4String binSchemeGuidance = "The binning scheme (linear, log).\n";
206 binSchemeGuidance
207 += "Note that the unit and fcn parameters cannot be omitted in this case,\n";
208 binSchemeGuidance += "but none value should be used instead.";
209 parValBinScheme->SetGuidance(binSchemeGuidance);
210 parValBinScheme->SetDefaultValue("linear");
211
212 auto commandName = Update("/analysis/HNTYPE_/setUAXIS", axis);
213 std::unique_ptr<G4UIcommand> command(
214 new G4UIcommand(Update("/analysis/HNTYPE_/setUAXIS", axis), messenger));
215 command->SetGuidance(Update("Set parameters for the NDIM_D LOBJECT of given id:"));
216 command->SetGuidance(
217 Update(" nAXISbins; AXISvalMin; AXISvalMax; AXISunit; AXISfunction; AXISbinScheme", axis));
218 command->SetParameter(parId);
219 command->SetParameter(parNbins);
220 command->SetParameter(parValMin);
221 command->SetParameter(parValMax);
222 command->SetParameter(parValUnit);
223 command->SetParameter(parValFcn);
224 command->SetParameter(parValBinScheme);
225 command->AvailableForStates(G4State_PreInit, G4State_Idle);
226
227 return command;
228}
229
230//_____________________________________________________________________________
231 std::unique_ptr<G4UIcommand>
233 G4UImessenger* messenger) const
234{
235 auto parId = new G4UIparameter("id", 'i', false);
236 parId->SetGuidance(Update("OBJECT id"));
237 parId->SetParameterRange("id>=0");
238
239 auto parValMin = new G4UIparameter("valMin", 'd', false);
240 parValMin->SetGuidance(Update("Minimum AXIS-value expressed in unit", axis));
241
242 auto parValMax = new G4UIparameter("valMax", 'd', false);
243 parValMax->SetGuidance(Update("Maximum AXIS-value expressed in unit", axis));
244
245 auto parValUnit = new G4UIparameter("valUnit", 's', true);
246 parValUnit->SetGuidance("The unit applied to filled values and valMin, valMax");
247 parValUnit->SetDefaultValue("none");
248
249 auto parValFcn = new G4UIparameter("valFcn", 's', true);
250 parValFcn->SetParameterCandidates("log log10 exp none");
251 G4String fcnGuidance = "The function applied to filled values (log, log10, exp, none).\n";
252 fcnGuidance += "Note that the unit parameter cannot be omitted in this case,\n";
253 fcnGuidance += "but none value should be used instead.";
254 parValFcn->SetGuidance(fcnGuidance);
255 parValFcn->SetDefaultValue("none");
256
257 std::unique_ptr<G4UIcommand> command(
258 new G4UIcommand(Update("/analysis/HNTYPE_/setUAXIS", axis), messenger));
259 command->SetGuidance(Update("Set parameters for the NDIM_D LOBJECT of #id:"));
260 command->SetGuidance(
261 Update(" AXISvalMin; AXISvalMax; AXISunit; AXISfunction", axis));
262 command->SetParameter(parId);
263 command->SetParameter(parValMin);
264 command->SetParameter(parValMax);
265 command->SetParameter(parValUnit);
266 command->SetParameter(parValFcn);
267 command->AvailableForStates(G4State_PreInit, G4State_Idle);
268
269 return command;
270}
271
272//_____________________________________________________________________________
273std::unique_ptr<G4UIcommand>
275 G4UImessenger* messenger) const
276{
277 auto parId = new G4UIparameter("id", 'i', false);
278 parId->SetGuidance(Update("OBJECT id"));
279 parId->SetParameterRange("id>=0");
280
281 auto parAxis = new G4UIparameter("axis", 's', false);
282 parAxis->SetGuidance(Update("Histogram AXIS-axis title", axis));
283
284 std::unique_ptr<G4UIcommand> command(
285 new G4UIcommand(Update("/analysis/HNTYPE_/setUAXISaxis", axis), messenger));
286 command->SetGuidance(Update("Set AXIS-axis title for the NDIM_D LOBJECT of given id", axis));
287 command->SetParameter(parId);
288 command->SetParameter(parAxis);
289 command->AvailableForStates(G4State_PreInit, G4State_Idle);
290
291 return command;
292}
293
294//_____________________________________________________________________________
295std::unique_ptr<G4UIcommand>
297 G4UImessenger* messenger) const
298{
299 auto parId = new G4UIparameter("id", 'i', false);
300 parId->SetGuidance(Update("OBJECT id"));
301 parId->SetParameterRange("id>=0");
302
303 auto parAxisLog = new G4UIparameter("axis", 'b', false);
304 parAxisLog->SetGuidance(Update("Histogram AXIS-axis log scale", axis));
305
306 std::unique_ptr<G4UIcommand> command(
307 new G4UIcommand(Update("/analysis/HNTYPE_/setUAXISaxisLog", axis), messenger));
308 command->SetGuidance(
309 Update("Activate AXIS-axis log scale for plotting of the NDIM_D LOBJECT of given id", axis));
310 command->SetParameter(parId);
311 command->SetParameter(parAxisLog);
312 command->AvailableForStates(G4State_PreInit, G4State_Idle);
313
314 return command;
315}
316
317//_____________________________________________________________________________
319 std::vector<G4String>& parameters,
320 G4int& counter) const
321{
322 data.fNbins = G4UIcommand::ConvertToInt(parameters[counter++]);
323 data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
324 data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
325 data.fSunit = parameters[counter++];
326 data.fSfcn = parameters[counter++];
327 data.fSbinScheme = parameters[counter++];
328}
329
330//_____________________________________________________________________________
332 std::vector<G4String>& parameters,
333 G4int& counter) const
334{
335 data.fVmin = G4UIcommand::ConvertToDouble(parameters[counter++]);
336 data.fVmax = G4UIcommand::ConvertToDouble(parameters[counter++]); ;
337 data.fSunit = parameters[counter++];
338 data.fSfcn = parameters[counter++];
339}
340
341//_____________________________________________________________________________
343 G4int nofParameters) const
344{
345 Warn(
346 "Got wrong number of \"" + command->GetCommandName() +
347 "\" parameters: " + to_string(nofParameters) +
348 " instead of " + to_string(command->GetParameterEntries()) + " expected",
349 fkClass, "WarnAboutParameters");
350}
351
352//_____________________________________________________________________________
354{
355 Warn(
356 "Command setX, setY, setZ must be called successively in this order.\n"
357 "Command was ignored.",
358 fkClass, "WarnAboutSetCommands");
359}
@ G4State_EventProc
@ G4State_Idle
@ G4State_GeomClosed
@ G4State_PreInit
static constexpr double second
Definition: G4SIunits.hh:137
int G4int
Definition: G4Types.hh:85
G4String Update(const G4String &str, const G4String &axis="") const
std::unique_ptr< G4UIdirectory > CreateHnDirectory() const
static constexpr std::string_view fkClass
std::unique_ptr< G4UIcommand > CreateSetBinsCommand(const G4String &axis, G4UImessenger *messenger) const
void GetBinData(BinData &data, std::vector< G4String > &parameters, G4int &counter) const
void GetValueData(ValueData &data, std::vector< G4String > &parameters, G4int &counter) const
std::unique_ptr< G4UIcommand > CreateSetAxisLogCommand(const G4String &axis, G4UImessenger *messenger) const
std::unique_ptr< G4UIcommand > CreateGetCommand(G4UImessenger *messenger) const
void WarnAboutParameters(G4UIcommand *command, G4int nofParameters) const
std::unique_ptr< G4UIcommand > CreateSetAxisCommand(const G4String &axis, G4UImessenger *messenger) const
std::unique_ptr< G4UIcommand > CreateSetValuesCommand(const G4String &axis, G4UImessenger *messenger) const
std::unique_ptr< G4UIcommand > CreateSetTitleCommand(G4UImessenger *messenger) const
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:138
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:557
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:575
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:137
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4String to_lower_copy(G4String str)
Return lowercased copy of string.
G4String to_upper_copy(G4String str)
Return uppercase copy of string.
void Replace(std::string &str, const std::string &from, const std::string &to)