Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 (ivana@ipno.in2p3.fr)
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"
37
38using namespace G4Analysis;
39using std::to_string;
40
41//_____________________________________________________________________________
43 : fManager(manager)
44{
45 fNtupleDir = std::make_unique<G4UIdirectory>("/analysis/ntuple/");
46 fNtupleDir->SetGuidance("ntuple control");
47
48 SetActivationCmd();
49 SetActivationToAllCmd();
50 SetFileNameCmd();
51 SetFileNameToAllCmd();
52 ListCmd();
53}
54
55//_____________________________________________________________________________
57
58//
59// private functions
60//
61
62//_____________________________________________________________________________
63void G4NtupleMessenger::AddIdParameter(G4UIcommand& command)
64{
65 auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
66 ntupleId->SetGuidance("Ntuple id");
67 ntupleId->SetParameterRange("NtupleId>=0");
68
69 command.SetParameter(ntupleId);
70}
71
72//_____________________________________________________________________________
73void G4NtupleMessenger::SetActivationCmd()
74{
75 fSetActivationCmd = CreateCommand<G4UIcommand>(
76 "setActivation", "Set activation for the ntuple");
77
78 AddIdParameter(*fSetActivationCmd);
79
80 auto ntupleActivation = new G4UIparameter("NtupleActivation", 'b', true);
81 ntupleActivation->SetGuidance("Ntuple activation");
82 ntupleActivation->SetDefaultValue(true);
83 fSetActivationCmd->SetParameter(ntupleActivation);
84}
85
86//_____________________________________________________________________________
87void G4NtupleMessenger::SetActivationToAllCmd()
88{
89 fSetActivationAllCmd = CreateCommand<G4UIcmdWithABool>(
90 "setActivationToAll", "Set activation to all ntuples");
91 fSetActivationAllCmd->SetParameterName("AllNtupleActivation",false);
92}
93
94//_____________________________________________________________________________
95void G4NtupleMessenger::SetFileNameCmd()
96{
97 fSetFileNameCmd = CreateCommand<G4UIcommand>(
98 "setFileName", "Set file name for the ntuple");
99
100 AddIdParameter(*fSetFileNameCmd);
101
102 auto ntupleFileName = new G4UIparameter("NtupleFileName", 's', false);
103 ntupleFileName->SetGuidance("Ntuple file name");
104 fSetFileNameCmd->SetParameter(ntupleFileName);
105}
106
107//_____________________________________________________________________________
108void G4NtupleMessenger::SetFileNameToAllCmd()
109{
110 fSetFileNameAllCmd = CreateCommand<G4UIcmdWithAString>(
111 "setFileNameToAll", "Set file name to all ntuples");
112 fSetFileNameAllCmd->SetParameterName("AllNtupleFileName",false);
113}
114
115//_____________________________________________________________________________
116void G4NtupleMessenger::ListCmd()
117{
118 fListCmd = CreateCommand<G4UIcommand>("list", "List all/active ntuples");
119 fListCmd->AvailableForStates(G4State_Idle, G4State_GeomClosed, G4State_EventProc);
120
121 auto parOnlyIfActive = new G4UIparameter("onlyIfActive", 'b', true);
122 parOnlyIfActive->SetGuidance("Option whether to list only active objects");
123 parOnlyIfActive->SetDefaultValue("true");
124 fListCmd->SetParameter(parOnlyIfActive);
125}
126
127//
128// public methods
129//
130
131//_____________________________________________________________________________
133{
134 // process "All" commands first
135
136 if ( command == fSetActivationAllCmd.get() ) {
137 fManager->SetActivation(fSetActivationAllCmd->GetNewBoolValue(newValues));
138 return;
139 }
140
141 if ( command == fSetFileNameAllCmd.get() ) {
142 fManager->SetFileName(newValues);
143 return;
144 }
145
146 // Tokenize parameters in a vector
147 std::vector<G4String> parameters;
148 G4Analysis::Tokenize(newValues, parameters);
149 // check consistency
150 if ( parameters.size() != command->GetParameterEntries() ) {
151 // Should never happen but let's check anyway for consistency
153 "Got wrong number of \"" + command->GetCommandName() +
154 "\" parameters: " + std::to_string(parameters.size()) +
155 " instead of " + std::to_string(command->GetParameterEntries()) + " expected",
156 fkClass, "WarnAboutParameters");
157 return;
158 }
159
160 auto counter = 0;
161 auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
162
163 if ( command == fSetActivationCmd.get() ) {
164 fManager->SetNtupleActivation(id, G4UIcommand::ConvertToBool(parameters[counter++]));
165 return;
166 }
167
168 if ( command == fSetFileNameCmd.get() ) {
169 fManager->SetNtupleFileName(id, parameters[counter++]);
170 return;
171 }
172
173 if ( command == fListCmd.get() ) {
174 auto onlyIfActive = G4UIcommand::ConvertToBool(parameters[0]);
175 fManager->ListNtuple(onlyIfActive);
176 return;
177 }
178}
@ G4State_EventProc
@ G4State_Idle
@ G4State_GeomClosed
void SetNewValue(G4UIcommand *command, G4String value) final
~G4NtupleMessenger() override
G4NtupleMessenger()=delete
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:139
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:147
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:561
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:549
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:138
G4bool ListNtuple(G4bool onlyIfActive=true) const
void SetNtupleFileName(const G4String &fileName)
void SetActivation(G4bool activation)
G4bool SetFileName(const G4String &fileName)
void SetNtupleActivation(G4bool activation)
void Tokenize(const G4String &line, std::vector< G4String > &tokens)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)