Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4NtupleMessenger.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 ([email protected])
28
29#include "G4NtupleMessenger.hh"
30#include "G4VAnalysisManager.hh"
32
33#include "G4UIcommand.hh"
34#include "G4UIparameter.hh"
35#include "G4UIcmdWithABool.hh"
36#include "G4UIcmdWithAString.hh"
38
39using namespace G4Analysis;
40using std::to_string;
41
42//_____________________________________________________________________________
44 : fManager(manager)
45{
46 fNtupleDir = std::make_unique<G4UIdirectory>("/analysis/ntuple/");
47 fNtupleDir->SetGuidance("ntuple control");
48
49 CreateCmd();
50 CreateColumnCmds();
51 FinishCmd();
52 DeleteCmd();
53 SetActivationCmd();
54 SetActivationToAllCmd();
55 SetFileNameCmd();
56 SetFileNameToAllCmd();
57 ListCmd();
58}
59
60//_____________________________________________________________________________
62
63//
64// private functions
65//
66
67//_____________________________________________________________________________
68void G4NtupleMessenger::AddIdParameter(G4UIcommand& command)
69{
70 auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
71 ntupleId->SetGuidance("Ntuple id");
72 ntupleId->SetParameterRange("NtupleId>=0");
73
74 command.SetParameter(ntupleId);
75}
76
77//_____________________________________________________________________________
78void G4NtupleMessenger::CreateCmd()
79{
80 fCreateCmd = CreateCommand<G4UIcommand>("create", "Create ntuple");
81
82 auto ntName = new G4UIparameter("name", 's', false);
83 ntName->SetGuidance("Ntuple name");
84 fCreateCmd->SetParameter(ntName);
85
86 auto ntTitle = new G4UIparameter("title", 's', false);
87 ntTitle->SetGuidance("Ntuple title");
88 fCreateCmd->SetParameter(ntTitle);
89}
90
91//_____________________________________________________________________________
92void G4NtupleMessenger::CreateColumnCmds()
93{
94 std::vector<char> colTypes = {'I', 'F', 'D', 'S'};
95
96 for (auto colType : colTypes ) {
97 G4String name = "createColumn";
98 G4String guidance = "Create ntuple column";
99 name.insert(6, 1, colType);
100 guidance.insert(7, 1, colType);
101 auto cmd = CreateCommand<G4UIcmdWithAString>(name, guidance);
102 fCreateColumnCmds[colType] = std::move(cmd);
103 }
104}
105
106//_____________________________________________________________________________
107void G4NtupleMessenger::FinishCmd()
108{
109 fFinishCmd = CreateCommand<G4UIcmdWithoutParameter>(
110 "finish", "Finish creating ntuple");
111}
112
113//_____________________________________________________________________________
114void G4NtupleMessenger::DeleteCmd()
115{
116 fDeleteCmd = CreateCommand<G4UIcommand>(
117 "delete", "Delete ntuple with given id");
118
119 // Add Id parameter
120 AddIdParameter(*fDeleteCmd);
121
122 auto parKeepSetting = new G4UIparameter("keepSetting", 'b', true);
123 G4String guidance =
124 "If set true, activation, file name, etc. options will be kept\n"
125 "and applied when a new object with the same id is created.";
126 parKeepSetting->SetGuidance(guidance.c_str());
127 parKeepSetting->SetDefaultValue("false");
128 fDeleteCmd->SetParameter(parKeepSetting);
129}
130
131//_____________________________________________________________________________
132void G4NtupleMessenger::SetActivationCmd()
133{
134 fSetActivationCmd = CreateCommand<G4UIcommand>(
135 "setActivation", "Set activation for the ntuple with given id");
136
137 AddIdParameter(*fSetActivationCmd);
138
139 auto ntupleActivation = new G4UIparameter("NtupleActivation", 'b', true);
140 ntupleActivation->SetGuidance("Ntuple activation");
141 ntupleActivation->SetDefaultValue(true);
142 fSetActivationCmd->SetParameter(ntupleActivation);
143}
144
145//_____________________________________________________________________________
146void G4NtupleMessenger::SetActivationToAllCmd()
147{
148 fSetActivationAllCmd = CreateCommand<G4UIcmdWithABool>(
149 "setActivationToAll", "Set activation to all ntuples");
150 fSetActivationAllCmd->SetParameterName("AllNtupleActivation",false);
151}
152
153//_____________________________________________________________________________
154void G4NtupleMessenger::SetFileNameCmd()
155{
156 fSetFileNameCmd = CreateCommand<G4UIcommand>(
157 "setFileName", "Set file name for the ntuple with given id");
158
159 AddIdParameter(*fSetFileNameCmd);
160
161 auto ntupleFileName = new G4UIparameter("NtupleFileName", 's', false);
162 ntupleFileName->SetGuidance("Ntuple file name");
163 fSetFileNameCmd->SetParameter(ntupleFileName);
164}
165
166//_____________________________________________________________________________
167void G4NtupleMessenger::SetFileNameToAllCmd()
168{
169 fSetFileNameAllCmd = CreateCommand<G4UIcmdWithAString>(
170 "setFileNameToAll", "Set file name to all ntuples");
171 fSetFileNameAllCmd->SetParameterName("AllNtupleFileName",false);
172}
173
174//_____________________________________________________________________________
175void G4NtupleMessenger::ListCmd()
176{
177 fListCmd = CreateCommand<G4UIcommand>("list", "List all/active ntuples");
178 fListCmd->AvailableForStates(G4State_Idle, G4State_GeomClosed, G4State_EventProc);
179
180 auto parOnlyIfActive = new G4UIparameter("onlyIfActive", 'b', true);
181 parOnlyIfActive->SetGuidance("Option whether to list only active objects");
182 parOnlyIfActive->SetDefaultValue("true");
183 fListCmd->SetParameter(parOnlyIfActive);
184}
185
186//
187// public methods
188//
189
190//_____________________________________________________________________________
192{
193 // process "All" commands first
194
195 if ( command == fSetActivationAllCmd.get() ) {
196 fManager->SetActivation(fSetActivationAllCmd->GetNewBoolValue(newValues));
197 return;
198 }
199
200 if ( command == fSetFileNameAllCmd.get() ) {
201 fManager->SetFileName(newValues);
202 return;
203 }
204
205 // Tokenize parameters in a vector
206 std::vector<G4String> parameters;
207 G4Analysis::Tokenize(newValues, parameters);
208 // check consistency
209 if ( parameters.size() != command->GetParameterEntries() ) {
210 // Should never happen but let's check anyway for consistency
212 "Got wrong number of \"" + command->GetCommandName() +
213 "\" parameters: " + std::to_string(parameters.size()) +
214 " instead of " + std::to_string(command->GetParameterEntries()) + " expected",
215 fkClass, "WarnAboutParameters");
216 return;
217 }
218
219 auto counter = 0;
220
221 // commands without Id parameter
222
223 if ( command == fCreateCmd.get() ) {
224 auto name = parameters[counter++];
225 auto title = parameters[counter++];
226 fTmpNtupleId = fManager->CreateNtuple(name, title);
227 return;
228 }
229
230 for (const auto& [colType, checkCommand] : fCreateColumnCmds) {
231 if ( command == checkCommand.get() ) {
232 auto name = parameters[counter++];
233 switch (colType) {
234 case 'I':
235 fManager->CreateNtupleIColumn(fTmpNtupleId, name);
236 return;
237 case 'F':
238 fManager->CreateNtupleFColumn(fTmpNtupleId, name);
239 return;
240 case 'D':
241 fManager->CreateNtupleDColumn(fTmpNtupleId, name);
242 return;
243 case 'S':
244 fManager->CreateNtupleSColumn(fTmpNtupleId, name);
245 return;
246 default:
247 return;
248 }
249 }
250 }
251
252 if ( command == fFinishCmd.get() ) {
253 fManager->FinishNtuple(fTmpNtupleId);
254 fTmpNtupleId = G4Analysis::kInvalidId;
255 return;
256 }
257
258 // commands with Id parameter
259
260 auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
261
262 if ( command == fDeleteCmd.get() ) {
263 auto keepSetting = G4UIcommand::ConvertToBool(parameters[counter++]);
264 fManager->DeleteNtuple(id, keepSetting);
265 return;
266 }
267
268 if ( command == fSetActivationCmd.get() ) {
269 fManager->SetNtupleActivation(id, G4UIcommand::ConvertToBool(parameters[counter++]));
270 return;
271 }
272
273 if ( command == fSetFileNameCmd.get() ) {
274 fManager->SetNtupleFileName(id, parameters[counter++]);
275 return;
276 }
277
278 if ( command == fListCmd.get() ) {
279 auto onlyIfActive = G4UIcommand::ConvertToBool(parameters[0]);
280 fManager->ListNtuple(onlyIfActive);
281 return;
282 }
283}
@ G4State_EventProc
@ G4State_Idle
@ G4State_GeomClosed
void SetNewValue(G4UIcommand *command, G4String value) final
~G4NtupleMessenger() override
G4NtupleMessenger()=delete
std::size_t GetParameterEntries() const
void SetParameter(G4UIparameter *const newParameter)
static G4int ConvertToInt(const char *st)
static G4bool ConvertToBool(const char *st)
const G4String & GetCommandName() const
G4bool ListNtuple(G4bool onlyIfActive=true) const
G4bool DeleteNtuple(G4int id, G4bool clear=false)
G4int CreateNtupleIColumn(const G4String &name)
G4int CreateNtupleDColumn(const G4String &name)
void SetNtupleFileName(const G4String &fileName)
G4int CreateNtupleFColumn(const G4String &name)
void SetActivation(G4bool activation)
G4int CreateNtupleSColumn(const G4String &name)
G4int CreateNtuple(const G4String &name, const G4String &title)
G4bool SetFileName(const G4String &fileName)
void SetNtupleActivation(G4bool activation)
void Tokenize(const G4String &line, std::vector< G4String > &tokens)
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
const char * name(G4int ptype)