Geant4-11
G4TNtupleManager.icc
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#include "G4AnalysisManagerState.hh"
28#include "G4AnalysisUtilities.hh"
29
30using std::to_string;
31
32//
33// private template functions
34//
35
36//_____________________________________________________________________________
37template <typename NT, typename FT>
38G4TNtupleManager<NT, FT>::G4TNtupleManager(
39 const G4AnalysisManagerState& state)
40 : G4BaseNtupleManager(state)
41{}
42
43//_____________________________________________________________________________
44template <typename NT, typename FT>
45G4TNtupleManager<NT, FT>::~G4TNtupleManager()
46{
47 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
48 delete ntupleDescription;
49 }
50}
51
52//_____________________________________________________________________________
53template <typename NT, typename FT>
54G4TNtupleDescription<NT, FT>*
55G4TNtupleManager<NT, FT>::GetNtupleDescriptionInFunction(
56 G4int id, std::string_view functionName, G4bool warn) const
57{
58 auto index = id - fFirstId;
59 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
60 if ( warn) {
61 G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
62 fkClass, functionName);
63 }
64 return nullptr;
65 }
66
67 return fNtupleDescriptionVector[index];
68}
69
70//_____________________________________________________________________________
71template <typename NT, typename FT>
72NT* G4TNtupleManager<NT, FT>::GetNtupleInFunction(
73 G4int id, std::string_view functionName, G4bool warn) const
74{
75 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
76 if ( ! ntupleDescription ) return nullptr;
77
78 if ( ! ntupleDescription->fNtuple ) {
79 if ( warn ) {
80 G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
81 fkClass, functionName);
82 }
83 return nullptr;
84 }
85
86 return ntupleDescription->fNtuple;
87}
88
89//_____________________________________________________________________________
90template <typename NT, typename FT>
91template <typename T>
92G4bool G4TNtupleManager<NT, FT>::FillNtupleTColumn(
93 G4int ntupleId, G4int columnId, const T& value)
94{
95 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
96 //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
97 return false;
98 }
99
100 // get ntuple
101 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
102 if ( ! ntuple ) return false;
103
104 // get generic column
105 auto index = columnId - fFirstNtupleColumnId;
106 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
107 G4Analysis::Warn(
108 "Ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
109 " does not exist.",
110 fkClass, "FillNtupleTColumn");
111 return false;
112 }
113 auto icolumn = ntuple->columns()[index];
114
115 // get column and check its type
116 auto column = dynamic_cast<typename NT::template column<T>* >(icolumn);
117 if ( ! column ) {
118 G4Analysis::Warn(
119 "Column type does not match: "
120 " ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
121 " value " + G4Analysis::ToString(value),
122 fkClass, "FillNtupleTColumn");
123 return false;
124 }
125
126 column->fill(value);
127
128 if ( IsVerbose(G4Analysis::kVL4) ) {
129 Message(G4Analysis::kVL4, "fill", "ntuple T column",
130 " ntupleId " + to_string(ntupleId) +
131 " column " + to_string(columnId) +
132 " value " + G4Analysis::ToString<T>(value));
133 }
134
135 return true;
136}
137
138//
139// protected functions
140//
141
142//_____________________________________________________________________________
143template <typename NT, typename FT>
144G4int G4TNtupleManager<NT, FT>::CreateNtuple(G4NtupleBooking* ntupleBooking)
145{
146 Message(G4Analysis::kVL4, "create from booking", "ntuple",
147 ntupleBooking->fNtupleBooking.name());
148
149 // The ntuple index
150 auto index = ntupleBooking->fNtupleId - fFirstId;
151
152 // Check if the ntuple description of this id already exists
153 // (what should normally never happen)
154 // delete it and print a warning
155 if ( index < G4int(fNtupleDescriptionVector.size()) &&
156 fNtupleDescriptionVector[index] ) {
157 delete fNtupleDescriptionVector[index];
158 G4Analysis::Warn(
159 "Ntuple description " + to_string(ntupleBooking->fNtupleId) +
160 " already exists.", fkClass, "CreateNtuple");
161 }
162
163 // Allocate the vector element(s)
164 while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
165 fNtupleDescriptionVector.push_back(nullptr);
166 }
167
168 // Create ntuple description from ntuple booking.
169 auto ntupleDescription = new G4TNtupleDescription<NT, FT>(ntupleBooking);
170 fNtupleDescriptionVector[index] = ntupleDescription;
171
172 // Do not create ntuple if it is inactivated
173 if ( fState.GetIsActivation() &&
174 ( ! ntupleDescription->fActivation ) ) return G4Analysis::kInvalidId;
175
176 // Do not create ntuple if it already exists
177 if ( ntupleDescription->fNtuple ) {
178 G4Analysis::Warn(
179 "Ntuple " + to_string(ntupleBooking->fNtupleId) +
180 " already exists.", fkClass, "CreateNtuple");
181 return ntupleBooking->fNtupleId;
182 }
183
184 // create ntuple
185 CreateTNtupleFromBooking(ntupleDescription);
186
187 // finish created ntuple
188 auto fromBooking = true;
189 FinishTNtuple(ntupleDescription, fromBooking);
190
191 Message(G4Analysis::kVL3, "create from booking", "ntuple",
192 ntupleBooking->fNtupleBooking.name());
193
194 return ntupleBooking->fNtupleId;
195}
196
197//_____________________________________________________________________________
198template <typename NT, typename FT>
199void G4TNtupleManager<NT, FT>::CreateNtuplesFromBooking(
200 const std::vector<G4NtupleBooking*>& ntupleBookings)
201{
202// Create ntuple from ntuple bookings.
203
204 // Create ntuple descriptions from ntuple booking.
205 for ( auto ntupleBooking : ntupleBookings ) {
206 CreateNtuple(ntupleBooking);
207 }
208}
209
210//_____________________________________________________________________________
211template <typename NT, typename FT>
212G4bool
213G4TNtupleManager<NT, FT>::Reset()
214{
215 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
216 delete ntupleDescription;
217 }
218
219 fNtupleDescriptionVector.clear();
220 fNtupleVector.clear();
221
222 return true;
223}
224
225//_____________________________________________________________________________
226template <typename NT, typename FT>
227void
228G4TNtupleManager<NT, FT>::Clear()
229{
230 // Reset function clears all data
231 Reset();
232
233 Message(G4Analysis::kVL2, "clear", "ntuples");
234}
235
236//_____________________________________________________________________________
237template <typename NT, typename FT>
238G4bool G4TNtupleManager<NT, FT>::FillNtupleIColumn(
239 G4int ntupleId, G4int columnId, G4int value)
240{
241 return FillNtupleTColumn<int>(ntupleId, columnId, value);
242}
243//_____________________________________________________________________________
244template <typename NT, typename FT>
245G4bool G4TNtupleManager<NT, FT>::FillNtupleFColumn(
246 G4int ntupleId, G4int columnId, G4float value)
247{
248 return FillNtupleTColumn<float>(ntupleId, columnId, value);
249}
250
251//_____________________________________________________________________________
252template <typename NT, typename FT>
253G4bool G4TNtupleManager<NT, FT>::FillNtupleDColumn(
254 G4int ntupleId, G4int columnId, G4double value)
255{
256 return FillNtupleTColumn<double>(ntupleId, columnId, value);
257}
258
259//_____________________________________________________________________________
260template <typename NT, typename FT>
261G4bool G4TNtupleManager<NT, FT>::FillNtupleSColumn(
262 G4int ntupleId, G4int columnId, const G4String& value)
263{
264 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
265}
266
267//_____________________________________________________________________________
268template <typename NT, typename FT>
269G4bool G4TNtupleManager<NT, FT>::AddNtupleRow(
270 G4int ntupleId)
271{
272 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
273 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
274 return false;
275 }
276
277 if ( IsVerbose(G4Analysis::kVL4) ) {
278 Message(G4Analysis::kVL4, "add", "ntuple row",
279 " ntupleId " + to_string(ntupleId));
280 }
281
282 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
283 if ( ! ntupleDescription ) return false;
284
285 auto ntuple = ntupleDescription->fNtuple;
286 if ( ! ntuple ) return false;
287
288 auto result = ntuple->add_row();
289 if ( ! result ) {
290 G4Analysis::Warn(
291 "Ntuple " + to_string(ntupleId) + " adding row has failed.",
292 fkClass, "AddTNtupleRow");
293 }
294
295 ntupleDescription->fHasFill = true;
296
297 if ( IsVerbose(G4Analysis::kVL4) ) {
298 Message(G4Analysis::kVL4, "add", "ntuple row",
299 " ntupleId " + to_string(ntupleId));
300 }
301
302 return true;
303}
304
305//_____________________________________________________________________________
306template <typename NT, typename FT>
307void G4TNtupleManager<NT, FT>::SetActivation(
308 G4bool activation)
309{
310 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
311 ntupleDescription->fActivation = activation;
312 }
313}
314
315//_____________________________________________________________________________
316template <typename NT, typename FT>
317void G4TNtupleManager<NT, FT>::SetActivation(
318 G4int ntupleId, G4bool activation)
319{
320 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
321 if ( ! ntupleDescription ) return;
322
323 ntupleDescription->fActivation = activation;
324}
325
326//_____________________________________________________________________________
327template <typename NT, typename FT>
328G4bool G4TNtupleManager<NT, FT>::GetActivation(
329 G4int ntupleId) const
330{
331 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
332 if ( ! ntupleDescription ) return false;
333
334 return ntupleDescription->fActivation;
335}
336
337//_____________________________________________________________________________
338template <typename NT, typename FT>
339NT*
340G4TNtupleManager<NT, FT>::GetNtuple() const
341{
342 return GetNtuple(fFirstId);
343}
344
345//_____________________________________________________________________________
346template <typename NT, typename FT>
347NT*
348G4TNtupleManager<NT, FT>::GetNtuple(G4int ntupleId) const
349{
350 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
351 if ( ! ntupleDescription ) return nullptr;
352
353 return ntupleDescription->fNtuple;
354}
355
356//_____________________________________________________________________________
357template <typename NT, typename FT>
358typename std::vector<NT*>::iterator
359G4TNtupleManager<NT, FT>::BeginNtuple()
360{
361 return fNtupleVector.begin();
362}
363
364//_____________________________________________________________________________
365template <typename NT, typename FT>
366typename std::vector<NT*>::iterator
367G4TNtupleManager<NT, FT>::EndNtuple()
368{
369 return fNtupleVector.end();
370}
371
372//_____________________________________________________________________________
373template <typename NT, typename FT>
374typename std::vector<NT*>::const_iterator
375G4TNtupleManager<NT, FT>::BeginConstNtuple() const
376{
377 return fNtupleVector.begin();
378}
379
380//_____________________________________________________________________________
381template <typename NT, typename FT>
382typename std::vector<NT*>::const_iterator
383G4TNtupleManager<NT, FT>::EndConstNtuple() const
384{
385 return fNtupleVector.end();
386}
387
388//_____________________________________________________________________________
389template <typename NT, typename FT>
390G4int G4TNtupleManager<NT, FT>::GetNofNtuples() const
391{
392 return G4int(fNtupleVector.size());
393}