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, "CreateNtupleIColumn");
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;
418 FinishTNtuple(ntupleDescription);
421 if ( fState.GetVerboseL2() ) {
422 G4ExceptionDescription description;
423 description << ntupleDescription->fNtupleBooking.name() << " ntupleId " << ntupleId;
424 fState.GetVerboseL2()->Message("finish", "ntuple", description);
429 //_____________________________________________________________________________
430 template <typename TNTUPLE>
431 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
432 G4int columnId, G4int value)
434 return FillNtupleTColumn<int>(columnId, value);
437 //_____________________________________________________________________________
438 template <typename TNTUPLE>
439 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
440 G4int columnId, G4float value)
442 return FillNtupleTColumn<float>(columnId, value);
445 //_____________________________________________________________________________
446 template <typename TNTUPLE>
447 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
448 G4int columnId, G4double value)
450 return FillNtupleTColumn<double>(columnId, value);
453 //_____________________________________________________________________________
454 template <typename TNTUPLE>
455 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
456 G4int columnId, const G4String& value)
458 return FillNtupleTColumn<std::string>(columnId, value);
461 //_____________________________________________________________________________
462 template <typename TNTUPLE>
463 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow()
465 return AddNtupleRow(fFirstId);
468 //_____________________________________________________________________________
469 template <typename TNTUPLE>
470 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
471 G4int ntupleId, G4int columnId, G4int value)
473 return FillNtupleTColumn<int>(ntupleId, columnId, value);
475 //_____________________________________________________________________________
476 template <typename TNTUPLE>
477 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
478 G4int ntupleId, G4int columnId, G4float value)
480 return FillNtupleTColumn<float>(ntupleId, columnId, value);
483 //_____________________________________________________________________________
484 template <typename TNTUPLE>
485 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
486 G4int ntupleId, G4int columnId, G4double value)
488 return FillNtupleTColumn<double>(ntupleId, columnId, value);
491 //_____________________________________________________________________________
492 template <typename TNTUPLE>
493 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
494 G4int ntupleId, G4int columnId, const G4String& value)
496 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
499 //_____________________________________________________________________________
500 template <typename TNTUPLE>
501 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow(
504 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
505 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
510 if ( fState.GetVerboseL4() ) {
511 G4ExceptionDescription description;
512 description << " ntupleId " << ntupleId;
513 fState.GetVerboseL4()->Message("add", "ntuple row", description);
517 auto ntuple = GetNtupleInFunction(ntupleId, "AddNtupleRow");
518 if ( ! ntuple ) return false;
520 auto result = ntuple->add_row();
522 G4ExceptionDescription description;
523 description << " " << " ntupleId " << ntupleId
524 << "adding row has failed.";
525 G4Exception("G4TNtupleManager::AddTNtupleRow()",
526 "Analysis_W002", JustWarning, description);
530 if ( fState.GetVerboseL4() ) {
531 G4ExceptionDescription description;
532 description << " ntupleId " << ntupleId;
533 fState.GetVerboseL4()->Message("add", "ntuple row", description);
540 //_____________________________________________________________________________
541 template <typename TNTUPLE>
542 void G4TNtupleManager<TNTUPLE>::SetActivation(
545 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
546 ntupleDescription->fActivation = activation;
550 //_____________________________________________________________________________
551 template <typename TNTUPLE>
552 void G4TNtupleManager<TNTUPLE>::SetActivation(
553 G4int ntupleId, G4bool activation)
555 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
556 if ( ! ntupleDescription ) return;
558 ntupleDescription->fActivation = activation;
561 //_____________________________________________________________________________
562 template <typename TNTUPLE>
563 G4bool G4TNtupleManager<TNTUPLE>::GetActivation(
564 G4int ntupleId) const
566 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
567 if ( ! ntupleDescription ) return false;
569 return ntupleDescription->fActivation;
572 //_____________________________________________________________________________
573 template <typename TNTUPLE>
575 G4TNtupleManager<TNTUPLE>::GetNtuple() const
577 return GetNtuple(fFirstId);
580 //_____________________________________________________________________________
581 template <typename TNTUPLE>
583 G4TNtupleManager<TNTUPLE>::GetNtuple(G4int ntupleId) const
585 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
586 if ( ! ntupleDescription ) return nullptr;
588 return ntupleDescription->fNtuple;
591 //_____________________________________________________________________________
592 template <typename TNTUPLE>
593 typename std::vector<TNTUPLE*>::iterator
594 G4TNtupleManager<TNTUPLE>::BeginNtuple()
596 return fNtupleVector.begin();
599 //_____________________________________________________________________________
600 template <typename TNTUPLE>
601 typename std::vector<TNTUPLE*>::iterator
602 G4TNtupleManager<TNTUPLE>::EndNtuple()
604 return fNtupleVector.end();
607 //_____________________________________________________________________________
608 template <typename TNTUPLE>
609 typename std::vector<TNTUPLE*>::const_iterator
610 G4TNtupleManager<TNTUPLE>::BeginConstNtuple() const
612 return fNtupleVector.begin();
615 //_____________________________________________________________________________
616 template <typename TNTUPLE>
617 typename std::vector<TNTUPLE*>::const_iterator
618 G4TNtupleManager<TNTUPLE>::EndConstNtuple() const
620 return fNtupleVector.end();
623 //_____________________________________________________________________________
624 template <typename TNTUPLE>
625 G4int G4TNtupleManager<TNTUPLE>::GetNofNtuples() const
627 return fNtupleVector.size();