Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4NtupleBookingManager.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, 01/09/2020 ([email protected])
28
32
33using namespace G4Analysis;
34using std::to_string;
35
36//
37// private template functions
38//
39
40//_____________________________________________________________________________
42 const G4AnalysisManagerState& state)
44{}
45
46//_____________________________________________________________________________
48{
49 for ( auto ntupleBooking : fNtupleBookingVector ) {
50 delete ntupleBooking;
51 }
52}
53
54//_____________________________________________________________________________
56G4NtupleBookingManager::GetNtupleBookingInFunction(
57 G4int id, std::string_view functionName, G4bool warn) const
58{
59 auto index = id - fFirstId;
60 if ( index < 0 || index >= G4int(fNtupleBookingVector.size()) ) {
61 if ( warn) {
62 Warn("Ntuple booking " + to_string(id) + " does not exist.",
63 fkClass, functionName);
64 }
65 return nullptr;
66 }
67
68 return fNtupleBookingVector[index];
69}
70
71//_____________________________________________________________________________
72G4bool G4NtupleBookingManager::CheckName(
73 const G4String& name, const G4String& objectType) const
74{
75 if (name.size() == 0u) {
76 Warn("Empty " + objectType + " name is not allowed.\n" +
77 objectType + " was not created.", fkClass, "CheckName");
78 return false;
79 }
80 return true;
81}
82
83//
84// protected functions
85//
86
87//_____________________________________________________________________________
89{
90 return fNtupleBookingVector.size() == 0u;
91}
92
93//_____________________________________________________________________________
95 const G4String& name, const G4String& title)
96{
97 if ( ! CheckName(name, "Ntuple") ) return kInvalidId;
98
99 Message(kVL4, "create", "ntuple booking", name);
100
101 // Create ntuple description
102 auto index = fNtupleBookingVector.size();
103 auto ntupleBooking = new G4NtupleBooking();
104 fNtupleBookingVector.push_back(ntupleBooking);
105
106 // Save name & title in ntuple booking
107 ntupleBooking->fNtupleBooking.set_name(name);
108 ntupleBooking->fNtupleBooking.set_title(title);
109 ntupleBooking->fNtupleId = G4int(index + fFirstId);
110
111 // Create ntuple
112 fLockFirstId = true;
113
114 Message(kVL2, "create", "ntuple booking",
115 name + " ntupleId " + to_string(ntupleBooking->fNtupleId));
116
117 return ntupleBooking->fNtupleId;
118}
119
120//_____________________________________________________________________________
122 std::vector<int>* vector)
123{
124 return CreateNtupleIColumn(GetCurrentNtupleId(), name, vector);
125}
126
127//_____________________________________________________________________________
129 std::vector<float>* vector)
130{
131 return CreateNtupleFColumn(GetCurrentNtupleId(), name, vector);
132}
133
134//_____________________________________________________________________________
136 std::vector<double>* vector)
137{
138 return CreateNtupleDColumn(GetCurrentNtupleId(), name, vector);
139}
140
141//_____________________________________________________________________________
143 std::vector<std::string>* vector)
144{
145 return CreateNtupleSColumn(GetCurrentNtupleId(), name, vector);
146}
147
148//_____________________________________________________________________________
150{
151 return FinishNtuple(GetCurrentNtupleId());
152}
153
154//_____________________________________________________________________________
156 G4int ntupleId, const G4String& name, std::vector<int>* vector)
157{
158 return CreateNtupleTColumn<int>(ntupleId, name, vector);
159}
160
161//_____________________________________________________________________________
163 G4int ntupleId, const G4String& name, std::vector<float>* vector)
164{
165 return CreateNtupleTColumn<float>(ntupleId, name, vector);
166}
167
168//_____________________________________________________________________________
170 G4int ntupleId, const G4String& name, std::vector<double>* vector)
171{
172 return CreateNtupleTColumn<double>(ntupleId, name, vector);
173}
174
175//_____________________________________________________________________________
177 G4int ntupleId, const G4String& name, std::vector<std::string>* vector)
178{
179 return CreateNtupleTColumn<std::string>(ntupleId, name, vector);
180}
181
182//_____________________________________________________________________________
184 G4int ntupleId)
185{
186 // Nothing to be done for booking,
187 return GetNtupleBookingInFunction(ntupleId, "FinishNtuple");
188}
189
190//_____________________________________________________________________________
192{
193 if ( fLockFirstNtupleColumnId ) {
194 Warn("Cannot set FirstNtupleColumnId as its value was already used.",
195 fkClass, "SetFirstNtupleColumnId");
196 return false;
197 }
198
199 fFirstNtupleColumnId = firstId;
200 return true;
201}
202
203//_____________________________________________________________________________
205 G4bool activation)
206{
207 for ( auto ntupleBooking : fNtupleBookingVector ) {
208 ntupleBooking->fActivation = activation;
209 }
210}
211
212//_____________________________________________________________________________
214 G4int ntupleId, G4bool activation)
215{
216 auto ntupleBooking
217 = GetNtupleBookingInFunction(ntupleId, "SetActivation");
218 if (ntupleBooking == nullptr) return;
219
220 ntupleBooking->fActivation = activation;
221}
222
223//_____________________________________________________________________________
225 G4int ntupleId) const
226{
227 auto ntupleBooking
228 = GetNtupleBookingInFunction(ntupleId, "GetActivation");
229 if (ntupleBooking == nullptr) return false;
230
231 return ntupleBooking->fActivation;
232}
233
234//_____________________________________________________________________________
236 const G4String& fileName)
237{
238 for ( auto ntupleBooking : fNtupleBookingVector ) {
239 ntupleBooking->fFileName = fileName;
240 }
241}
242
243//_____________________________________________________________________________
245 G4int ntupleId, const G4String& fileName)
246{
247 auto ntupleBooking
248 = GetNtupleBookingInFunction(ntupleId, "SetFileName");
249 if (ntupleBooking == nullptr) return;
250
251 // Do nothing if file name does not change
252 if ( ntupleBooking->fFileName == fileName ) return;
253
254 auto ntupleFileName = fileName;
255 auto extension = GetExtension(fileName);
256 if (extension.size() != 0u) {
257 // Check if valid extension (if present)
258 auto output = G4Analysis::GetOutput(extension);
259 if ( output == G4AnalysisOutput::kNone ) {
260 Warn("The file extension " + extension + " is not supported.",
261 fkClass, "SetFileName");
262 return;
263 }
264 }
265 else {
266 if (fFileType.size() != 0u) {
267 //add extension if missing and file type is defined
268 ntupleFileName = fileName + "." + fFileType;
269 }
270 }
271
272 // Save the fileName in booking
273 // If extension is still missing (possible with generic manager),
274 // it will be completed with the default one at OpenFile
275 ntupleBooking->fFileName = ntupleFileName;
276}
277
278//_____________________________________________________________________________
280 G4int ntupleId) const
281{
282 auto ntupleBooking
283 = GetNtupleBookingInFunction(ntupleId, "GetFileName");
284 if (ntupleBooking == nullptr) return "";
285
286 return ntupleBooking->fFileName;
287}
288
289//_____________________________________________________________________________
291{
292 for ( auto ntupleBooking : fNtupleBookingVector ) {
293 delete ntupleBooking;
294 }
295 fNtupleBookingVector.clear();
296 fLockFirstNtupleColumnId = false;
297
298 Message(G4Analysis::kVL2, "clear", "ntupleBookings");
299}
300
301//
302// public methods
303//
304
305//_____________________________________________________________________________
307{
308 // do nothing if file type is defined and is same
309 if ( fFileType == fileType ) return;
310
311 // save the type
312 fFileType = fileType;
313
314 // Give warning and redefine file extension in bookings
315 // with file name of different fileTypes
316 for ( auto ntupleBooking : fNtupleBookingVector ) {
317 if ((ntupleBooking->fFileName).size() == 0u) continue;
318
319 auto extension = GetExtension(ntupleBooking->fFileName);
320 if ( fFileType == extension ) continue;
321
322 // multiple file types are not suported
323 auto baseFileName = GetBaseName(ntupleBooking->fFileName);
324 auto ntupleFileName = baseFileName + "." + fFileType;
325 if (extension.size() != 0u) {
326 Warn("Writing ntuples in files of different output types " +
327 fFileType + ", " + extension + " is not supported.",
328 fkClass, "SetFileType");
329 }
330
331 // Save the info in ntuple description
332 ntupleBooking->fFileName = ntupleFileName;
333 }
334}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4bool GetActivation(G4int ntupleId) const
G4bool SetFirstNtupleColumnId(G4int firstId)
std::vector< G4NtupleBooking * > fNtupleBookingVector
G4String GetFileName(G4int id) const
G4int CreateNtupleSColumn(const G4String &name, std::vector< std::string > *vector)
G4NtupleBooking * FinishNtuple()
G4int CreateNtupleDColumn(const G4String &name, std::vector< double > *vector)
G4int CreateNtuple(const G4String &name, const G4String &title)
G4int CreateNtupleFColumn(const G4String &name, std::vector< float > *vector)
G4NtupleBookingManager()=delete
void SetFileName(const G4String &fileName)
G4int CreateNtupleIColumn(const G4String &name, std::vector< int > *vector)
void SetFileType(const G4String &fileType)
void SetActivation(G4bool activation)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
constexpr G4int kVL2
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
constexpr G4int kVL4
constexpr G4int kInvalidId
G4String GetBaseName(const G4String &fileName)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
const char * name(G4int ptype)