Geant4-11
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 (ivana@ipno.in2p3.fr)
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//_____________________________________________________________________________
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//
72// protected functions
73//
74
75//_____________________________________________________________________________
77{
78 return ! fNtupleBookingVector.size();
79}
80
81//_____________________________________________________________________________
83 const G4String& name, const G4String& title)
84{
85 Message(kVL4, "create", "ntuple booking", name);
86
87 // Create ntuple description
88 auto index = fNtupleBookingVector.size();
89 auto ntupleBooking = new G4NtupleBooking();
90 fNtupleBookingVector.push_back(ntupleBooking);
91
92 // Save name & title in ntuple booking
93 ntupleBooking->fNtupleBooking.set_name(name);
94 ntupleBooking->fNtupleBooking.set_title(title);
95 ntupleBooking->fNtupleId = G4int(index + fFirstId);
96
97 // Create ntuple
98 fLockFirstId = true;
99
100 Message(kVL2, "create", "ntuple booking",
101 name + " ntupleId " + to_string(ntupleBooking->fNtupleId));
102
103 return ntupleBooking->fNtupleId;
104}
105
106//_____________________________________________________________________________
108 std::vector<int>* vector)
109{
111}
112
113//_____________________________________________________________________________
115 std::vector<float>* vector)
116{
118}
119
120//_____________________________________________________________________________
122 std::vector<double>* vector)
123{
125}
126
127//_____________________________________________________________________________
129 std::vector<std::string>* vector)
130{
132}
133
134//_____________________________________________________________________________
136{
138}
139
140//_____________________________________________________________________________
142 G4int ntupleId, const G4String& name, std::vector<int>* vector)
143{
144 return CreateNtupleTColumn<int>(ntupleId, name, vector);
145}
146
147//_____________________________________________________________________________
149 G4int ntupleId, const G4String& name, std::vector<float>* vector)
150{
151 return CreateNtupleTColumn<float>(ntupleId, name, vector);
152}
153
154//_____________________________________________________________________________
156 G4int ntupleId, const G4String& name, std::vector<double>* vector)
157{
158 return CreateNtupleTColumn<double>(ntupleId, name, vector);
159}
160
161//_____________________________________________________________________________
163 G4int ntupleId, const G4String& name, std::vector<std::string>* vector)
164{
165 return CreateNtupleTColumn<std::string>(ntupleId, name, vector);
166}
167
168//_____________________________________________________________________________
170 G4int ntupleId)
171{
172 // Nothing to be done for booking,
173 return GetNtupleBookingInFunction(ntupleId, "FinishNtuple");
174}
175
176//_____________________________________________________________________________
178{
180 Warn("Cannot set FirstNtupleColumnId as its value was already used.",
181 fkClass, "SetFirstNtupleColumnId");
182 return false;
183 }
184
185 fFirstNtupleColumnId = firstId;
186 return true;
187}
188
189//_____________________________________________________________________________
191 G4bool activation)
192{
193 for ( auto ntupleBooking : fNtupleBookingVector ) {
194 ntupleBooking->fActivation = activation;
195 }
196}
197
198//_____________________________________________________________________________
200 G4int ntupleId, G4bool activation)
201{
202 auto ntupleBooking
203 = GetNtupleBookingInFunction(ntupleId, "SetActivation");
204 if ( ! ntupleBooking ) return;
205
206 ntupleBooking->fActivation = activation;
207}
208
209//_____________________________________________________________________________
211 G4int ntupleId) const
212{
213 auto ntupleBooking
214 = GetNtupleBookingInFunction(ntupleId, "GetActivation");
215 if ( ! ntupleBooking ) return false;
216
217 return ntupleBooking->fActivation;
218}
219
220//_____________________________________________________________________________
222 const G4String& fileName)
223{
224 for ( auto ntupleBooking : fNtupleBookingVector ) {
225 ntupleBooking->fFileName = fileName;
226 }
227}
228
229//_____________________________________________________________________________
231 G4int ntupleId, const G4String& fileName)
232{
233 auto ntupleBooking
234 = GetNtupleBookingInFunction(ntupleId, "SetFileName");
235 if ( ! ntupleBooking ) return;
236
237 // Do nothing if file name does not change
238 if ( ntupleBooking->fFileName == fileName ) return;
239
240 auto ntupleFileName = fileName;
241 auto extension = GetExtension(fileName);
242 if ( extension.size() ) {
243 // Check if valid extension (if present)
244 auto output = G4Analysis::GetOutput(extension);
245 if ( output == G4AnalysisOutput::kNone ) {
246 Warn("The file extension " + extension + " is not supported.",
247 fkClass, "SetFileName");
248 return;
249 }
250 }
251 else {
252 if ( fFileType.size() ) {
253 //add extension if missing and file type is defined
254 ntupleFileName = fileName + "." + fFileType;
255 }
256 }
257
258 // Save the fileName in booking
259 // If extension is still missing (possible with generic manager),
260 // it will be completed with the default one at OpenFile
261 ntupleBooking->fFileName = ntupleFileName;
262}
263
264//_____________________________________________________________________________
266 G4int ntupleId) const
267{
268 auto ntupleBooking
269 = GetNtupleBookingInFunction(ntupleId, "GetFileName");
270 if ( ! ntupleBooking ) return "";
271
272 return ntupleBooking->fFileName;
273}
274
275//_____________________________________________________________________________
277{
278 for ( auto ntupleBooking : fNtupleBookingVector ) {
279 delete ntupleBooking;
280 }
281 fNtupleBookingVector.clear();
283
284 Message(G4Analysis::kVL2, "clear", "ntupleBookings");
285}
286
287//
288// public methods
289//
290
291//_____________________________________________________________________________
293{
294 // do nothing if file type is defined and is same
295 if ( fFileType == fileType ) return;
296
297 // save the type
298 fFileType = fileType;
299
300 // Give warning and redefine file extension in bookings
301 // with file name of different fileTypes
302 for ( auto ntupleBooking : fNtupleBookingVector ) {
303
304 if ( ! (ntupleBooking->fFileName).size() ) continue;
305
306 auto extension = GetExtension(ntupleBooking->fFileName);
307 if ( fFileType == extension ) continue;
308
309 // multiple file types are not suported
310 auto baseFileName = GetBaseName(ntupleBooking->fFileName);
311 auto ntupleFileName = baseFileName + "." + fFileType;
312 if ( extension.size() ) {
313 Warn("Writing ntuples in files of different output types " +
314 fFileType + ", " + extension + " is not supported.",
315 fkClass, "SetFileType");
316 }
317
318 // Save the info in ntuple description
319 ntupleBooking->fFileName = ntupleFileName;
320 }
321}
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
static constexpr std::string_view fkClass
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)
G4NtupleBooking * GetNtupleBookingInFunction(G4int id, std::string_view function, G4bool warn=true) const
G4int GetCurrentNtupleId() const
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
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)