Geant4-11
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
41namespace {
42
43//_____________________________________________________________________________
45 const G4String& commandName, std::size_t got, std::size_t expected,
46 std::string_view className)
47{
48 Warn(
49 "Got wrong number of \"" + commandName + "\" parameters: " +
50 to_string(got) + " instead of " + to_string(expected) + " expected",
51 className, "SetNewValue");
52}
53
54}
55
56//_____________________________________________________________________________
58 : G4UImessenger(),
59 fManager(manager)
60{
61 fNtupleDir = std::make_unique<G4UIdirectory>("/analysis/ntuple/");
62 fNtupleDir->SetGuidance("ntuple control");
63
68}
69
70//_____________________________________________________________________________
72
73//
74// public functions
75//
76
77//_____________________________________________________________________________
79{
80 auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
81 ntupleId->SetGuidance("Ntuple id");
82 ntupleId->SetParameterRange("NtupleId>=0");
83
84 auto ntupleActivation = new G4UIparameter("NtupleActivation", 's', true);
85 ntupleActivation->SetGuidance("Ntuple activation");
86 ntupleActivation->SetDefaultValue("none");
87
88 fSetActivationCmd = std::make_unique<G4UIcommand>("/analysis/ntuple/setActivation", this);
89 G4String guidance("Set activation for the ntuple of given id");
90
91 fSetActivationCmd->SetGuidance(guidance);
92 fSetActivationCmd->SetParameter(ntupleId);
93 fSetActivationCmd->SetParameter(ntupleActivation);
95}
96
97//_____________________________________________________________________________
99{
101 = std::make_unique<G4UIcmdWithABool>("/analysis/ntuple/setActivationToAll", this);
102 G4String guidance("Set activation to all ntuples");
103 fSetActivationAllCmd->SetGuidance(guidance);
104 fSetActivationAllCmd->SetParameterName("AllNtupleActivation",false);
105}
106
107//_____________________________________________________________________________
109{
110 auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
111 ntupleId->SetGuidance("Ntuple id");
112 ntupleId->SetParameterRange("NtupleId>=0");
113
114 auto ntupleFileName = new G4UIparameter("NtupleFileName", 's', true);
115 ntupleFileName->SetGuidance("Ntuple file name");
116 ntupleFileName->SetDefaultValue("none");
117
118 fSetFileNameCmd = std::make_unique<G4UIcommand>("/analysis/ntuple/setFileName", this);
119 G4String guidance("Set file name for the ntuple of given id");
120
121 fSetFileNameCmd->SetGuidance(guidance);
122 fSetFileNameCmd->SetParameter(ntupleId);
123 fSetFileNameCmd->SetParameter(ntupleFileName);
124 fSetFileNameCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
125}
126
127//_____________________________________________________________________________
129{
131 = std::make_unique<G4UIcmdWithAString>("/analysis/ntuple/setFileNameToAll", this);
132 G4String guidance("Set file name to all ntuples");
133 fSetFileNameAllCmd->SetGuidance(guidance);
134 fSetFileNameAllCmd->SetParameterName("AllNtupleFileName",false);
135}
136
137//
138// public methods
139//
140
141//_____________________________________________________________________________
143{
144 if ( command == fSetActivationCmd.get() ) {
145 // tokenize parameters in a vector
146 std::vector<G4String> parameters;
147 G4Analysis::Tokenize(newValues, parameters);
148 // check consistency
149 if ( parameters.size() == command->GetParameterEntries() ) {
150 auto counter = 0;
151 auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
152 auto activation = G4UIcommand::ConvertToBool(parameters[counter++]);
153 fManager->SetNtupleActivation(id, activation);
154 }
155 else {
156 // Should never happen but let's check anyway for consistency
158 parameters.size(),command->GetParameterEntries(), fkClass);
159 }
160 }
161 else if ( command == fSetActivationAllCmd.get() ) {
162 auto activation = fSetActivationAllCmd->GetNewBoolValue(newValues);
163 fManager->SetNtupleActivation(activation);
164 }
165 else if ( command == fSetFileNameCmd.get() ) {
166 // tokenize parameters in a vector
167 std::vector<G4String> parameters;
168 G4Analysis::Tokenize(newValues, parameters);
169 // check consistency
170 if ( parameters.size() == command->GetParameterEntries() ) {
171 auto counter = 0;
172 auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
173 auto fileName = parameters[counter++];
174 fManager->SetNtupleFileName(id, fileName);
175 }
176 else {
177 // Should never happen but let's check anyway for consistency
179 parameters.size(),command->GetParameterEntries(), fkClass);
180 }
181 }
182 else if ( command == fSetFileNameAllCmd.get() ) {
183 auto fileName = newValues;
184 fManager->SetNtupleFileName(fileName);
185 }
186}
@ G4State_Idle
@ G4State_PreInit
static char className[]
Definition: G4Win32.cc:36
std::unique_ptr< G4UIcommand > fSetFileNameCmd
virtual void SetNewValue(G4UIcommand *command, G4String value) final
std::unique_ptr< G4UIcmdWithABool > fSetActivationAllCmd
std::unique_ptr< G4UIcommand > fSetActivationCmd
static constexpr std::string_view fkClass
G4VAnalysisManager * fManager
Associated class.
G4NtupleMessenger()=delete
std::unique_ptr< G4UIdirectory > fNtupleDir
virtual ~G4NtupleMessenger()
std::unique_ptr< G4UIcmdWithAString > fSetFileNameAllCmd
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:138
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:557
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:545
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:137
void SetNtupleFileName(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)
void WrongParametersWarning(const G4String &commandName, std::size_t got, std::size_t expected, std::string_view className)