2 // ********************************************************************
3 // * License and Disclaimer *
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. *
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. *
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 // ********************************************************************
28 #include "G4AnalysisManagerState.hh"
29 #include "G4AnalysisUtilities.hh"
32 // private template functions
35 //_____________________________________________________________________________
36 template <typename TNTUPLE>
37 G4TNtupleManager<TNTUPLE>::G4TNtupleManager(
38 const G4AnalysisManagerState& state)
39 : G4VNtupleManager(state),
40 fNtupleDescriptionVector(),
44 //_____________________________________________________________________________
45 template <typename TNTUPLE>
46 G4TNtupleManager<TNTUPLE>::~G4TNtupleManager()
48 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
49 delete ntupleDescription;
53 //_____________________________________________________________________________
54 template <typename TNTUPLE>
55 G4TNtupleDescription<TNTUPLE>*
56 G4TNtupleManager<TNTUPLE>::GetNtupleDescriptionInFunction(
57 G4int id, G4String functionName, G4bool warn) const
59 auto index = id - fFirstId;
60 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
62 G4String inFunction = "G4TNtupleManager::";
63 inFunction += functionName;
64 G4ExceptionDescription description;
65 description << " " << "ntuple " << id << " does not exist.";
66 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
71 return fNtupleDescriptionVector[index];
74 //_____________________________________________________________________________
75 template <typename TNTUPLE>
77 G4TNtupleManager<TNTUPLE>::GetNtupleInFunction(
78 G4int id, G4String functionName, G4bool warn) const
80 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
81 if ( ! ntupleDescription ) return nullptr;
83 if ( ! ntupleDescription->fNtuple ) {
85 G4String inFunction = "G4TNtupleManager::";
86 inFunction += functionName;
87 G4ExceptionDescription description;
88 description << " " << "ntupleId " << id << " does not exist.";
89 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
94 return ntupleDescription->fNtuple;
97 //_____________________________________________________________________________
98 template <typename TNTUPLE>
100 void G4TNtupleManager<TNTUPLE>::CreateTColumnInNtuple(
101 G4TNtupleDescription<TNTUPLE>* ntupleDescription,
102 const G4String& name, std::vector<T>* vector)
104 if ( ntupleDescription->fNtuple ) {
105 if ( vector == nullptr ) {
106 ntupleDescription->fNtuple->template create_column<T>(name);
109 ntupleDescription->fNtuple->template create_column<T>(name, *vector);
114 //_____________________________________________________________________________
115 template <typename TNTUPLE>
116 template <typename T>
117 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleTColumn(
118 G4int ntupleId, const G4String& name, std::vector<T>* vector)
121 if ( fState.GetVerboseL4() ) {
122 G4ExceptionDescription description;
123 description << name << " ntupleId " << ntupleId;
124 fState.GetVerboseL4()->Message("create", "ntuple T column", description);
128 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "CreateNtupleTColumn");
129 if ( ! ntupleDescription ) return G4Analysis::kInvalidId;
131 // Save column info in booking
132 auto& ntupleBooking = ntupleDescription->fNtupleBooking;
133 auto index = ntupleBooking.columns().size();
135 ntupleBooking.template add_column<T>(name);
137 ntupleBooking.template add_column<T>(name, *vector);
139 // Create column if ntuple already exists
140 CreateTColumnInNtuple<T>(ntupleDescription, name, vector);
141 fLockFirstNtupleColumnId = true;
144 if ( fState.GetVerboseL2() ) {
145 G4ExceptionDescription description;
146 description << name << " ntupleId " << ntupleId;
147 fState.GetVerboseL2()->Message("create", "ntuple T column", description);
151 return index + fFirstNtupleColumnId;
155 //_____________________________________________________________________________
156 template <typename TNTUPLE>
157 template <typename T>
158 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleTColumn(
159 const G4String& name, std::vector<T>* vector)
161 auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
162 return CreateNtupleTColumn<T>(ntupleId, name, vector);
165 //_____________________________________________________________________________
166 template <typename TNTUPLE>
167 template <typename T>
168 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleTColumn(
169 G4int ntupleId, G4int columnId, const T& value)
171 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
172 //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
177 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
178 if ( ! ntuple ) return false;
180 // get generic column
181 auto index = columnId - fFirstNtupleColumnId;
182 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
183 G4ExceptionDescription description;
184 description << " " << "ntupleId " << ntupleId
185 << " columnId " << columnId << " does not exist.";
186 G4Exception("G4TNtupleManager::FillNtupleTColumn()",
187 "Analysis_W011", JustWarning, description);
190 auto icolumn = ntuple->columns()[index];
192 // get column and check its type
193 auto column = dynamic_cast<typename TNTUPLE::template column<T>* >(icolumn);
195 G4ExceptionDescription description;
196 description << " Column type does not match: "
197 << " ntupleId " << ntupleId
198 << " columnId " << columnId << " value " << value;
199 G4Exception("G4TNtupleManager:FillNtupleTColumn",
200 "Analysis_W011", JustWarning, description);
207 if ( fState.GetVerboseL4() ) {
208 G4ExceptionDescription description;
209 description << " ntupleId " << ntupleId
210 << " columnId " << columnId << " value " << value;
211 fState.GetVerboseL4()->Message("fill", "ntuple T column", description);
217 //_____________________________________________________________________________
218 template <typename TNTUPLE>
219 template <typename T>
220 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleTColumn(
221 G4int columnId, const T& value)
223 return FillNtupleTColumn<T>(fFirstId, columnId, value);
227 // protected functions
230 //_____________________________________________________________________________
231 template <typename TNTUPLE>
232 void G4TNtupleManager<TNTUPLE>::CreateNtuplesFromBooking()
234 // Create ntuple from ntuple_booking.
236 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
238 // Do not create ntuple if it is inactivated
239 if ( fState.GetIsActivation() && ( ! ntupleDescription->fActivation ) ) continue;
241 // Do not create ntuple if it already exists
242 if ( ntupleDescription->fNtuple ) continue;
245 if ( fState.GetVerboseL4() )
246 fState.GetVerboseL4()
247 ->Message("create from booking", "ntuple",
248 ntupleDescription->fNtupleBooking.name());
252 CreateTNtupleFromBooking(ntupleDescription);
254 // finish created ntuple
255 FinishTNtuple(ntupleDescription);
258 if ( fState.GetVerboseL3() )
259 fState.GetVerboseL3()
260 ->Message("create from booking", "ntuple",
261 ntupleDescription->fNtupleBooking.name());
266 //_____________________________________________________________________________
267 template <typename TNTUPLE>
269 G4TNtupleManager<TNTUPLE>::IsEmpty() const
271 return ! fNtupleDescriptionVector.size();
274 //_____________________________________________________________________________
275 template <typename TNTUPLE>
277 G4TNtupleManager<TNTUPLE>::Reset(G4bool deleteNtuple)
279 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
280 if ( deleteNtuple ) {
281 delete ntupleDescription->fNtuple;
283 ntupleDescription->fNtuple = 0;
286 fNtupleVector.clear();
291 //_____________________________________________________________________________
292 template <typename TNTUPLE>
293 G4int G4TNtupleManager<TNTUPLE>::CreateNtuple(
294 const G4String& name, const G4String& title)
297 if ( fState.GetVerboseL4() )
298 fState.GetVerboseL4()->Message("create", "ntuple", name);
301 // Create ntuple description
302 auto index = fNtupleDescriptionVector.size();
303 auto ntupleDescription = new G4TNtupleDescription<TNTUPLE>();
304 fNtupleDescriptionVector.push_back(ntupleDescription);
306 // Save name & title in ntuple booking
307 ntupleDescription->fNtupleBooking.set_name(name);
308 ntupleDescription->fNtupleBooking.set_title(title);
311 CreateTNtuple(ntupleDescription, name, title);
315 if ( fState.GetVerboseL2() ) {
316 G4ExceptionDescription description;
317 description << name << " ntupleId " << index + fFirstId;
318 fState.GetVerboseL2()->Message("create", "ntuple", description);
322 return index + fFirstId;
325 //_____________________________________________________________________________
326 template <typename TNTUPLE>
327 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleIColumn(
328 const G4String& name, std::vector<int>* vector)
330 return CreateNtupleTColumn<int>(name, vector);
333 //_____________________________________________________________________________
334 template <typename TNTUPLE>
335 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleFColumn(
336 const G4String& name, std::vector<float>* vector)
338 return CreateNtupleTColumn<float>(name, vector);
341 //_____________________________________________________________________________
342 template <typename TNTUPLE>
343 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleDColumn(
344 const G4String& name, std::vector<double>* vector)
346 return CreateNtupleTColumn<double>(name, vector);
349 //_____________________________________________________________________________
350 template <typename TNTUPLE>
351 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleSColumn(
352 const G4String& name)
354 return CreateNtupleTColumn<std::string>(name, nullptr);
357 //_____________________________________________________________________________
358 template <typename TNTUPLE>
359 void G4TNtupleManager<TNTUPLE>::FinishNtuple()
361 auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
362 FinishNtuple(ntupleId);
365 //_____________________________________________________________________________
366 template <typename TNTUPLE>
367 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleIColumn(
368 G4int ntupleId, const G4String& name, std::vector<int>* vector)
370 return CreateNtupleTColumn<int>(ntupleId, name, vector);
373 //_____________________________________________________________________________
374 template <typename TNTUPLE>
375 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleFColumn(
376 G4int ntupleId, const G4String& name, std::vector<float>* vector)
378 return CreateNtupleTColumn<float>(ntupleId, name, vector);
382 //_____________________________________________________________________________
383 template <typename TNTUPLE>
384 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleDColumn(
385 G4int ntupleId, const G4String& name, std::vector<double>* vector)
387 return CreateNtupleTColumn<double>(ntupleId, name, vector);
390 //_____________________________________________________________________________
391 template <typename TNTUPLE>
392 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleSColumn(
393 G4int ntupleId, const G4String& name)
395 return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
398 //_____________________________________________________________________________
399 template <typename TNTUPLE>
400 void G4TNtupleManager<TNTUPLE>::FinishNtuple(
403 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple");
404 if ( ! ntupleDescription ) return;
407 if ( fState.GetVerboseL4() ) {
408 G4ExceptionDescription description;
409 description << ntupleDescription->fNtupleBooking.name() << " ntupleId " << ntupleId;
410 fState.GetVerboseL4()->Message("finish", "ntuple", description);
414 // check that ntuple was really created
415 // auto ntuple = GetNtupleInFunction(ntupleId, "FinishNtuple", false);
416 // if ( ! ntuple ) return;
417 // The test has to be now done in the FinishTNtuple implementations
419 FinishTNtuple(ntupleDescription);
422 if ( fState.GetVerboseL2() ) {
423 G4ExceptionDescription description;
424 description << ntupleDescription->fNtupleBooking.name() << " ntupleId " << ntupleId;
425 fState.GetVerboseL2()->Message("finish", "ntuple", description);
430 //_____________________________________________________________________________
431 template <typename TNTUPLE>
432 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
433 G4int columnId, G4int value)
435 return FillNtupleTColumn<int>(columnId, value);
438 //_____________________________________________________________________________
439 template <typename TNTUPLE>
440 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
441 G4int columnId, G4float value)
443 return FillNtupleTColumn<float>(columnId, value);
446 //_____________________________________________________________________________
447 template <typename TNTUPLE>
448 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
449 G4int columnId, G4double value)
451 return FillNtupleTColumn<double>(columnId, value);
454 //_____________________________________________________________________________
455 template <typename TNTUPLE>
456 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
457 G4int columnId, const G4String& value)
459 return FillNtupleTColumn<std::string>(columnId, value);
462 //_____________________________________________________________________________
463 template <typename TNTUPLE>
464 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow()
466 return AddNtupleRow(fFirstId);
469 //_____________________________________________________________________________
470 template <typename TNTUPLE>
471 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
472 G4int ntupleId, G4int columnId, G4int value)
474 return FillNtupleTColumn<int>(ntupleId, columnId, value);
476 //_____________________________________________________________________________
477 template <typename TNTUPLE>
478 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
479 G4int ntupleId, G4int columnId, G4float value)
481 return FillNtupleTColumn<float>(ntupleId, columnId, value);
484 //_____________________________________________________________________________
485 template <typename TNTUPLE>
486 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
487 G4int ntupleId, G4int columnId, G4double value)
489 return FillNtupleTColumn<double>(ntupleId, columnId, value);
492 //_____________________________________________________________________________
493 template <typename TNTUPLE>
494 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
495 G4int ntupleId, G4int columnId, const G4String& value)
497 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
500 //_____________________________________________________________________________
501 template <typename TNTUPLE>
502 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow(
505 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
506 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
511 if ( fState.GetVerboseL4() ) {
512 G4ExceptionDescription description;
513 description << " ntupleId " << ntupleId;
514 fState.GetVerboseL4()->Message("add", "ntuple row", description);
518 auto ntuple = GetNtupleInFunction(ntupleId, "AddNtupleRow");
519 if ( ! ntuple ) return false;
521 auto result = ntuple->add_row();
523 G4ExceptionDescription description;
524 description << " " << " ntupleId " << ntupleId
525 << "adding row has failed.";
526 G4Exception("G4TNtupleManager::AddTNtupleRow()",
527 "Analysis_W002", JustWarning, description);
531 if ( fState.GetVerboseL4() ) {
532 G4ExceptionDescription description;
533 description << " ntupleId " << ntupleId;
534 fState.GetVerboseL4()->Message("add", "ntuple row", description);
541 //_____________________________________________________________________________
542 template <typename TNTUPLE>
543 void G4TNtupleManager<TNTUPLE>::SetActivation(
546 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
547 ntupleDescription->fActivation = activation;
551 //_____________________________________________________________________________
552 template <typename TNTUPLE>
553 void G4TNtupleManager<TNTUPLE>::SetActivation(
554 G4int ntupleId, G4bool activation)
556 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
557 if ( ! ntupleDescription ) return;
559 ntupleDescription->fActivation = activation;
562 //_____________________________________________________________________________
563 template <typename TNTUPLE>
564 G4bool G4TNtupleManager<TNTUPLE>::GetActivation(
565 G4int ntupleId) const
567 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
568 if ( ! ntupleDescription ) return false;
570 return ntupleDescription->fActivation;
573 //_____________________________________________________________________________
574 template <typename TNTUPLE>
576 G4TNtupleManager<TNTUPLE>::GetNtuple() const
578 return GetNtuple(fFirstId);
581 //_____________________________________________________________________________
582 template <typename TNTUPLE>
584 G4TNtupleManager<TNTUPLE>::GetNtuple(G4int ntupleId) const
586 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
587 if ( ! ntupleDescription ) return nullptr;
589 return ntupleDescription->fNtuple;
592 //_____________________________________________________________________________
593 template <typename TNTUPLE>
594 typename std::vector<TNTUPLE*>::iterator
595 G4TNtupleManager<TNTUPLE>::BeginNtuple()
597 return fNtupleVector.begin();
600 //_____________________________________________________________________________
601 template <typename TNTUPLE>
602 typename std::vector<TNTUPLE*>::iterator
603 G4TNtupleManager<TNTUPLE>::EndNtuple()
605 return fNtupleVector.end();
608 //_____________________________________________________________________________
609 template <typename TNTUPLE>
610 typename std::vector<TNTUPLE*>::const_iterator
611 G4TNtupleManager<TNTUPLE>::BeginConstNtuple() const
613 return fNtupleVector.begin();
616 //_____________________________________________________________________________
617 template <typename TNTUPLE>
618 typename std::vector<TNTUPLE*>::const_iterator
619 G4TNtupleManager<TNTUPLE>::EndConstNtuple() const
621 return fNtupleVector.end();
624 //_____________________________________________________________________________
625 template <typename TNTUPLE>
626 G4int G4TNtupleManager<TNTUPLE>::GetNofNtuples() const
628 return fNtupleVector.size();