Geant4 10.7.0
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"
37
38#include <iostream>
39
40using namespace G4Analysis;
41
42namespace {
43
44void WrongParametersException(
45 const G4String& commandName, std::size_t got, std::size_t expected)
46{
47 G4ExceptionDescription description;
48 description
49 << "Got wrong number of \"" << commandName
50 << "\" parameters: " << got << " instead of " << expected
51 << " expected" << G4endl;
52 G4Exception("G4NtupleMessenger::SetNewValue",
53 "Analysis_W013", JustWarning, description);
54}
55
56}
57
58//_____________________________________________________________________________
60 : G4UImessenger(),
61 fManager(manager),
62 fSetActivationCmd(nullptr),
63 fSetActivationAllCmd(nullptr),
64 fSetFileNameCmd(nullptr),
65 fSetFileNameAllCmd(nullptr)
66{
67 fNtupleDir = G4Analysis::make_unique<G4UIdirectory>("/analysis/ntuple/");
68 fNtupleDir->SetGuidance("ntuple control");
69
70 SetActivationCmd();
71 SetActivationToAllCmd();
72 SetFileNameCmd();
73 SetFileNameToAllCmd();
74}
75
76//_____________________________________________________________________________
78{
79}
80
81//
82// public functions
83//
84
85//_____________________________________________________________________________
86void G4NtupleMessenger::SetActivationCmd()
87{
88 auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
89 ntupleId->SetGuidance("Ntuple id");
90 ntupleId->SetParameterRange("NtupleId>=0");
91
92 auto ntupleActivation = new G4UIparameter("NtupleActivation", 's', true);
93 ntupleActivation->SetGuidance("Ntuple activation");
94 ntupleActivation->SetDefaultValue("none");
95
96 fSetActivationCmd = G4Analysis::make_unique<G4UIcommand>("/analysis/ntuple/setActivation", this);
97 G4String guidance("Set activation for the ntuple of given id");
98
99 fSetActivationCmd->SetGuidance(guidance);
100 fSetActivationCmd->SetParameter(ntupleId);
101 fSetActivationCmd->SetParameter(ntupleActivation);
102 fSetActivationCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
103}
104
105//_____________________________________________________________________________
106void G4NtupleMessenger::SetActivationToAllCmd()
107{
108 fSetActivationAllCmd
109 = G4Analysis::make_unique<G4UIcmdWithABool>("/analysis/ntuple/setActivationToAll", this);
110 G4String guidance("Set activation to all ntuples");
111 fSetActivationAllCmd->SetGuidance(guidance);
112 fSetActivationAllCmd->SetParameterName("AllNtupleActivation",false);
113}
114
115//_____________________________________________________________________________
116void G4NtupleMessenger::SetFileNameCmd()
117{
118 auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
119 ntupleId->SetGuidance("Ntuple id");
120 ntupleId->SetParameterRange("NtupleId>=0");
121
122 auto ntupleFileName = new G4UIparameter("NtupleFileName", 's', true);
123 ntupleFileName->SetGuidance("Ntuple file name");
124 ntupleFileName->SetDefaultValue("none");
125
126 fSetFileNameCmd = G4Analysis::make_unique<G4UIcommand>("/analysis/ntuple/setFileName", this);
127 G4String guidance("Set file name for the ntuple of given id");
128
129 fSetFileNameCmd->SetGuidance(guidance);
130 fSetFileNameCmd->SetParameter(ntupleId);
131 fSetFileNameCmd->SetParameter(ntupleFileName);
132 fSetFileNameCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
133}
134
135//_____________________________________________________________________________
136void G4NtupleMessenger::SetFileNameToAllCmd()
137{
138 fSetFileNameAllCmd
139 = G4Analysis::make_unique<G4UIcmdWithAString>("/analysis/ntuple/setFileNameToAll", this);
140 G4String guidance("Set file name to all ntuples");
141 fSetFileNameAllCmd->SetGuidance(guidance);
142 fSetFileNameAllCmd->SetParameterName("AllNtupleFileName",false);
143}
144
145//
146// public methods
147//
148
149//_____________________________________________________________________________
151{
152 if ( command == fSetActivationCmd.get() ) {
153 // tokenize parameters in a vector
154 std::vector<G4String> parameters;
155 G4Analysis::Tokenize(newValues, parameters);
156 // check consistency
157 if ( parameters.size() == command->GetParameterEntries() ) {
158 auto counter = 0;
159 auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
160 auto activation = G4UIcommand::ConvertToBool(parameters[counter++]);
161 fManager->SetNtupleActivation(id, activation);
162 }
163 else {
164 // Should never happen but let's check anyway for consistency
165 WrongParametersException(command->GetCommandName(),
166 parameters.size(),command->GetParameterEntries());
167 }
168 }
169 else if ( command == fSetActivationAllCmd.get() ) {
170 auto activation = fSetActivationAllCmd->GetNewBoolValue(newValues);
171 fManager->SetNtupleActivation(activation);
172 }
173 else if ( command == fSetFileNameCmd.get() ) {
174 // tokenize parameters in a vector
175 std::vector<G4String> parameters;
176 G4Analysis::Tokenize(newValues, parameters);
177 // check consistency
178 if ( parameters.size() == command->GetParameterEntries() ) {
179 auto counter = 0;
180 auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
181 auto fileName = parameters[counter++];
182 fManager->SetNtupleFileName(id, fileName);
183 }
184 else {
185 // Should never happen but let's check anyway for consistency
186 WrongParametersException(command->GetCommandName(),
187 parameters.size(),command->GetParameterEntries());
188 }
189 }
190 else if ( command == fSetFileNameAllCmd.get() ) {
191 auto fileName = newValues;
192 fManager->SetNtupleFileName(fileName);
193 }
194}
@ G4State_Idle
@ G4State_PreInit
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
#define G4endl
Definition: G4ios.hh:57
virtual void SetNewValue(G4UIcommand *command, G4String value) final
G4NtupleMessenger(G4VAnalysisManager *manager)
virtual ~G4NtupleMessenger()
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:138
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:543
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:530
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)