Geant4 10.7.0
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;
34
35//
36// private template functions
37//
38
39//_____________________________________________________________________________
41 const G4AnalysisManagerState& state)
42 : G4BaseAnalysisManager(state),
43 fNtupleBookingVector(),
44 fFileType(),
45 fFirstNtupleColumnId(0),
46 fLockFirstNtupleColumnId(false)
47{}
48
49//_____________________________________________________________________________
51{
52 for ( auto ntupleBooking : fNtupleBookingVector ) {
53 delete ntupleBooking;
54 }
55}
56
57//_____________________________________________________________________________
59G4NtupleBookingManager::GetNtupleBookingInFunction(
60 G4int id, G4String functionName, G4bool warn) const
61{
62 auto index = id - fFirstId;
63 if ( index < 0 || index >= G4int(fNtupleBookingVector.size()) ) {
64 if ( warn) {
65 G4String inFunction = "G4NtupleBookingManager::";
66 inFunction += functionName;
67 G4ExceptionDescription description;
68 description << " " << "ntuple booking " << id << " does not exist.";
69 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
70 }
71 return nullptr;
72 }
73
74 return fNtupleBookingVector[index];
75}
76
77//
78// protected functions
79//
80
81//_____________________________________________________________________________
83{
84 return ! fNtupleBookingVector.size();
85}
86
87//_____________________________________________________________________________
89 const G4String& name, const G4String& title)
90{
91#ifdef G4VERBOSE
92 if ( fState.GetVerboseL4() )
93 fState.GetVerboseL4()->Message("create", "ntuple booking", name);
94#endif
95
96 // Create ntuple description
97 auto index = fNtupleBookingVector.size();
98 auto ntupleBooking = new G4NtupleBooking();
99 fNtupleBookingVector.push_back(ntupleBooking);
100
101 // Save name & title in ntuple booking
102 ntupleBooking->fNtupleBooking.set_name(name);
103 ntupleBooking->fNtupleBooking.set_title(title);
104
105 // Create ntuple
106 fLockFirstId = true;
107
108#ifdef G4VERBOSE
109 if ( fState.GetVerboseL2() ) {
110 G4ExceptionDescription description;
111 description << name << " ntupleId " << index + fFirstId;
112 fState.GetVerboseL2()->Message("create", "ntuple booking", description);
113 }
114#endif
115
116 return G4int(index + fFirstId);
117}
118
119//_____________________________________________________________________________
121 std::vector<int>* vector)
122{
123 return CreateNtupleIColumn(GetCurrentNtupleId(), name, vector);
124}
125
126//_____________________________________________________________________________
128 std::vector<float>* vector)
129{
130 return CreateNtupleFColumn(GetCurrentNtupleId(), name, vector);
131}
132
133//_____________________________________________________________________________
135 std::vector<double>* vector)
136{
137 return CreateNtupleDColumn(GetCurrentNtupleId(), name, vector);
138}
139
140//_____________________________________________________________________________
142{
143 return CreateNtupleSColumn(GetCurrentNtupleId(), name);
144}
145
146//_____________________________________________________________________________
148{
149 return FinishNtuple(GetCurrentNtupleId());
150}
151
152//_____________________________________________________________________________
154 G4int ntupleId, const G4String& name, std::vector<int>* vector)
155{
156 return CreateNtupleTColumn<int>(ntupleId, name, vector);
157}
158
159//_____________________________________________________________________________
161 G4int ntupleId, const G4String& name, std::vector<float>* vector)
162{
163 return CreateNtupleTColumn<float>(ntupleId, name, vector);
164}
165
166//_____________________________________________________________________________
168 G4int ntupleId, const G4String& name, std::vector<double>* vector)
169{
170 return CreateNtupleTColumn<double>(ntupleId, name, vector);
171}
172
173//_____________________________________________________________________________
175 G4int ntupleId, const G4String& name)
176{
177 return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
178}
179
180//_____________________________________________________________________________
182 G4int ntupleId)
183{
184 // Nothing to be done for booking,
185 return GetNtupleBookingInFunction(ntupleId, "FinishNtuple");
186}
187
188//_____________________________________________________________________________
190{
191 if ( fLockFirstNtupleColumnId ) {
192 G4ExceptionDescription description;
193 description
194 << "Cannot set FirstNtupleColumnId as its value was already used.";
195 G4Exception("G4BaseNtupleManager::SetFirstNtupleColumnId()",
196 "Analysis_W013", JustWarning, description);
197 return false;
198 }
199
200 fFirstNtupleColumnId = firstId;
201 return true;
202}
203
204//_____________________________________________________________________________
206 G4bool activation)
207{
208 for ( auto ntupleBooking : fNtupleBookingVector ) {
209 ntupleBooking->fActivation = activation;
210 }
211}
212
213//_____________________________________________________________________________
215 G4int ntupleId, G4bool activation)
216{
217 auto ntupleBooking
218 = GetNtupleBookingInFunction(ntupleId, "SetActivation");
219 if ( ! ntupleBooking ) return;
220
221 ntupleBooking->fActivation = activation;
222}
223
224//_____________________________________________________________________________
226 G4int ntupleId) const
227{
228 auto ntupleBooking
229 = GetNtupleBookingInFunction(ntupleId, "GetActivation");
230 if ( ! ntupleBooking ) return false;
231
232 return ntupleBooking->fActivation;
233}
234
235//_____________________________________________________________________________
237 const G4String& fileName)
238{
239 for ( auto ntupleBooking : fNtupleBookingVector ) {
240 ntupleBooking->fFileName = fileName;
241 }
242}
243
244//_____________________________________________________________________________
246 G4int ntupleId, const G4String& fileName)
247{
248 auto ntupleBooking
249 = GetNtupleBookingInFunction(ntupleId, "SetFileName");
250 if ( ! ntupleBooking ) return;
251
252 // Do nothing if file name does not change
253 if ( ntupleBooking->fFileName == fileName ) return;
254
255 auto ntupleFileName = fileName;
256 auto extension = GetExtension(fileName);
257 if ( extension.size() ) {
258 // Check if valid extension (if present)
259 auto output = G4Analysis::GetOutput(extension);
260 if ( output == G4AnalysisOutput::kNone ) {
261 G4ExceptionDescription description;
262 description
263 << "The file extension " << extension << "is not supported.";
264 G4Exception("G4NtupleBookingManager::SetFileName",
265 "Analysis_W051", JustWarning, description);
266 return;
267 }
268 }
269 else {
270 if ( fFileType.size() ) {
271 //add extension if missing and file type is defined
272 ntupleFileName = fileName + "." + fFileType;
273 }
274 }
275
276 // Save the fileName in booking
277 // If extension is still missing (possible with generic manager),
278 // it will be completed with the default one at OpenFile
279 ntupleBooking->fFileName = ntupleFileName;
280}
281
282//_____________________________________________________________________________
284 G4int ntupleId) const
285{
286 auto ntupleBooking
287 = GetNtupleBookingInFunction(ntupleId, "GetFileName");
288 if ( ! ntupleBooking ) return "";
289
290 return ntupleBooking->fFileName;
291}
292
293//
294// public methods
295//
296
297//_____________________________________________________________________________
299{
300 // do nothing if file type is defined and is same
301 if ( fFileType == fileType ) return;
302
303 // save the type
304 fFileType = fileType;
305
306 // Give warning and redefine file extension in bookings
307 // with file name of different fileTypes
308 for ( auto ntupleBooking : fNtupleBookingVector ) {
309
310 if ( ! (ntupleBooking->fFileName).size() ) continue;
311
312 auto extension = GetExtension(ntupleBooking->fFileName);
313 if ( fFileType == extension ) continue;
314
315 // multiple file types are not suported
316 auto baseFileName = GetBaseName(ntupleBooking->fFileName);
317 auto ntupleFileName = baseFileName + "." + fFileType;
318 if ( extension.size()) {
319 G4ExceptionDescription description;
320 description
321 << "Writing ntuples in files of different output types "
322 << fFileType << ", " << extension << " is not supported." << G4endl
323 << "Ntuple " << ntupleBooking->fNtupleBooking.name()
324 << " will be written in " << ntupleFileName;
325 G4Exception("G4NtupleBookingManager::SetFileType",
326 "Analysis_W051", JustWarning, description);
327 }
328
329 // Save the info in ntuple description
330 ntupleBooking->fFileName = ntupleFileName;
331 }
332}
@ 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
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool GetActivation(G4int ntupleId) const
G4bool SetFirstNtupleColumnId(G4int firstId)
std::vector< G4NtupleBooking * > fNtupleBookingVector
G4int CreateNtupleSColumn(const G4String &name)
G4String GetFileName(G4int id) const
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(const G4AnalysisManagerState &state)
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="")
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
G4String GetBaseName(const G4String &fileName)