Geant4  10.03
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 // $Id$
27 
28 #include "G4AnalysisManagerState.hh"
29 #include "G4AnalysisUtilities.hh"
30 
31 //
32 // private template functions
33 //
34 
35 //_____________________________________________________________________________
36 template <typename TNTUPLE>
37 G4TNtupleManager<TNTUPLE>::G4TNtupleManager(
38  const G4AnalysisManagerState& state)
39  : G4VNtupleManager(state),
40  fNtupleDescriptionVector(),
41  fNtupleVector()
42 {}
43 
44 //_____________________________________________________________________________
45 template <typename TNTUPLE>
46 G4TNtupleManager<TNTUPLE>::~G4TNtupleManager()
47 {
48  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
49  delete ntupleDescription;
50  }
51 }
52 
53 //_____________________________________________________________________________
54 template <typename TNTUPLE>
55 G4TNtupleDescription<TNTUPLE>*
56 G4TNtupleManager<TNTUPLE>::GetNtupleDescriptionInFunction(
57  G4int id, G4String functionName, G4bool warn) const
58 {
59  auto index = id - fFirstId;
60  if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
61  if ( warn) {
62  G4String inFunction = "G4TNtupleManager::";
63  inFunction += functionName;
64  G4ExceptionDescription description;
65  description << " " << "ntuple " << id << " does not exist.";
66  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
67  }
68  return nullptr;
69  }
70 
71  return fNtupleDescriptionVector[index];
72 }
73 
74 //_____________________________________________________________________________
75 template <typename TNTUPLE>
76 TNTUPLE*
77 G4TNtupleManager<TNTUPLE>::GetNtupleInFunction(
78  G4int id, G4String functionName, G4bool warn) const
79 {
80  auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
81  if ( ! ntupleDescription ) return nullptr;
82 
83  if ( ! ntupleDescription->fNtuple ) {
84  if ( warn ) {
85  G4String inFunction = "G4TNtupleManager::";
86  inFunction += functionName;
87  G4ExceptionDescription description;
88  description << " " << "ntupleId " << id << " does not exist.";
89  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
90  }
91  return nullptr;
92  }
93 
94  return ntupleDescription->fNtuple;
95 }
96 
97 //_____________________________________________________________________________
98 template <typename TNTUPLE>
99 template <typename T>
100 void G4TNtupleManager<TNTUPLE>::CreateTColumnInNtuple(
101  G4TNtupleDescription<TNTUPLE>* ntupleDescription,
102  const G4String& name, std::vector<T>* vector)
103 {
104  if ( ntupleDescription->fNtuple ) {
105  if ( vector == nullptr ) {
106  ntupleDescription->fNtuple->template create_column<T>(name);
107  }
108  else {
109  ntupleDescription->fNtuple->template create_column<T>(name, *vector);
110  }
111  }
112 }
113 
114 //_____________________________________________________________________________
115 template <typename TNTUPLE>
116 template <typename T>
117 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleTColumn(
118  G4int ntupleId, const G4String& name, std::vector<T>* vector)
119 {
120 #ifdef G4VERBOSE
121  if ( fState.GetVerboseL4() ) {
122  G4ExceptionDescription description;
123  description << name << " ntupleId " << ntupleId;
124  fState.GetVerboseL4()->Message("create", "ntuple T column", description);
125  }
126 #endif
127 
128  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "CreateNtupleTColumn");
129  if ( ! ntupleDescription ) return G4Analysis::kInvalidId;
130 
131  // Save column info in booking
132  auto& ntupleBooking = ntupleDescription->fNtupleBooking;
133  auto index = ntupleBooking.columns().size();
134  if ( ! vector )
135  ntupleBooking.template add_column<T>(name);
136  else
137  ntupleBooking.template add_column<T>(name, *vector);
138 
139  // Create column if ntuple already exists
140  CreateTColumnInNtuple<T>(ntupleDescription, name, vector);
141  fLockFirstNtupleColumnId = true;
142 
143 #ifdef G4VERBOSE
144  if ( fState.GetVerboseL2() ) {
145  G4ExceptionDescription description;
146  description << name << " ntupleId " << ntupleId;
147  fState.GetVerboseL2()->Message("create", "ntuple T column", description);
148  }
149 #endif
150 
151  return index + fFirstNtupleColumnId;
152 
153 }
154 
155 //_____________________________________________________________________________
156 template <typename TNTUPLE>
157 template <typename T>
158 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleTColumn(
159  const G4String& name, std::vector<T>* vector)
160 {
161  auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
162  return CreateNtupleTColumn<T>(ntupleId, name, vector);
163 }
164 
165 //_____________________________________________________________________________
166 template <typename TNTUPLE>
167 template <typename T>
168 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleTColumn(
169  G4int ntupleId, G4int columnId, const T& value)
170 {
171  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
172  //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
173  return false;
174  }
175 
176  // get ntuple
177  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
178  if ( ! ntuple ) return false;
179 
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);
188  return false;
189  }
190  auto icolumn = ntuple->columns()[index];
191 
192  // get column and check its type
193  auto column = dynamic_cast<typename TNTUPLE::template column<T>* >(icolumn);
194  if ( ! column ) {
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);
201  return false;
202  }
203 
204  column->fill(value);
205 
206 #ifdef G4VERBOSE
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);
212  }
213 #endif
214  return true;
215 }
216 
217 //_____________________________________________________________________________
218 template <typename TNTUPLE>
219 template <typename T>
220 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleTColumn(
221  G4int columnId, const T& value)
222 {
223  return FillNtupleTColumn<T>(fFirstId, columnId, value);
224 }
225 
226 //
227 // protected functions
228 //
229 
230 //_____________________________________________________________________________
231 template <typename TNTUPLE>
232 void G4TNtupleManager<TNTUPLE>::CreateNtuplesFromBooking()
233 {
234 // Create ntuple from ntuple_booking.
235 
236  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
237 
238  // Do not create ntuple if it is inactivated
239  if ( fState.GetIsActivation() && ( ! ntupleDescription->fActivation ) ) continue;
240 
241  // Do not create ntuple if it already exists
242  if ( ntupleDescription->fNtuple ) continue;
243 
244 #ifdef G4VERBOSE
245  if ( fState.GetVerboseL4() )
246  fState.GetVerboseL4()
247  ->Message("create from booking", "ntuple",
248  ntupleDescription->fNtupleBooking.name());
249 #endif
250 
251  // create ntuple
252  CreateTNtupleFromBooking(ntupleDescription);
253 
254  // finish created ntuple
255  FinishTNtuple(ntupleDescription);
256 
257 #ifdef G4VERBOSE
258  if ( fState.GetVerboseL3() )
259  fState.GetVerboseL3()
260  ->Message("create from booking", "ntuple",
261  ntupleDescription->fNtupleBooking.name());
262 #endif
263  }
264 }
265 
266 //_____________________________________________________________________________
267 template <typename TNTUPLE>
268 G4bool
269 G4TNtupleManager<TNTUPLE>::IsEmpty() const
270 {
271  return ! fNtupleDescriptionVector.size();
272 }
273 
274 //_____________________________________________________________________________
275 template <typename TNTUPLE>
276 G4bool
277 G4TNtupleManager<TNTUPLE>::Reset(G4bool deleteNtuple)
278 {
279  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
280  if ( deleteNtuple ) {
281  delete ntupleDescription->fNtuple;
282  }
283  ntupleDescription->fNtuple = 0;
284  }
285 
286  fNtupleVector.clear();
287 
288  return true;
289 }
290 
291 //_____________________________________________________________________________
292 template <typename TNTUPLE>
293 G4int G4TNtupleManager<TNTUPLE>::CreateNtuple(
294  const G4String& name, const G4String& title)
295 {
296 #ifdef G4VERBOSE
297  if ( fState.GetVerboseL4() )
298  fState.GetVerboseL4()->Message("create", "ntuple", name);
299 #endif
300 
301  // Create ntuple description
302  auto index = fNtupleDescriptionVector.size();
303  auto ntupleDescription = new G4TNtupleDescription<TNTUPLE>();
304  fNtupleDescriptionVector.push_back(ntupleDescription);
305 
306  // Save name & title in ntuple booking
307  ntupleDescription->fNtupleBooking.set_name(name);
308  ntupleDescription->fNtupleBooking.set_title(title);
309 
310  // Create ntuple
311  CreateTNtuple(ntupleDescription, name, title);
312  fLockFirstId = true;
313 
314 #ifdef G4VERBOSE
315  if ( fState.GetVerboseL2() ) {
316  G4ExceptionDescription description;
317  description << name << " ntupleId " << index + fFirstId;
318  fState.GetVerboseL2()->Message("create", "ntuple", description);
319  }
320 #endif
321 
322  return index + fFirstId;
323 }
324 
325 //_____________________________________________________________________________
326 template <typename TNTUPLE>
327 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleIColumn(
328  const G4String& name, std::vector<int>* vector)
329 {
330  return CreateNtupleTColumn<int>(name, vector);
331 }
332 
333 //_____________________________________________________________________________
334 template <typename TNTUPLE>
335 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleFColumn(
336  const G4String& name, std::vector<float>* vector)
337 {
338  return CreateNtupleTColumn<float>(name, vector);
339 }
340 
341 //_____________________________________________________________________________
342 template <typename TNTUPLE>
343 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleDColumn(
344  const G4String& name, std::vector<double>* vector)
345 {
346  return CreateNtupleTColumn<double>(name, vector);
347 }
348 
349 //_____________________________________________________________________________
350 template <typename TNTUPLE>
351 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleSColumn(
352  const G4String& name)
353 {
354  return CreateNtupleTColumn<std::string>(name, nullptr);
355 }
356 
357 //_____________________________________________________________________________
358 template <typename TNTUPLE>
359 void G4TNtupleManager<TNTUPLE>::FinishNtuple()
360 {
361  auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
362  FinishNtuple(ntupleId);
363 }
364 
365 //_____________________________________________________________________________
366 template <typename TNTUPLE>
367 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleIColumn(
368  G4int ntupleId, const G4String& name, std::vector<int>* vector)
369 {
370  return CreateNtupleTColumn<int>(ntupleId, name, vector);
371 }
372 
373 //_____________________________________________________________________________
374 template <typename TNTUPLE>
375 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleFColumn(
376  G4int ntupleId, const G4String& name, std::vector<float>* vector)
377 {
378  return CreateNtupleTColumn<float>(ntupleId, name, vector);
379 }
380 
381 
382 //_____________________________________________________________________________
383 template <typename TNTUPLE>
384 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleDColumn(
385  G4int ntupleId, const G4String& name, std::vector<double>* vector)
386 {
387  return CreateNtupleTColumn<double>(ntupleId, name, vector);
388 }
389 
390 //_____________________________________________________________________________
391 template <typename TNTUPLE>
392 G4int G4TNtupleManager<TNTUPLE>::CreateNtupleSColumn(
393  G4int ntupleId, const G4String& name)
394 {
395  return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
396 }
397 
398 //_____________________________________________________________________________
399 template <typename TNTUPLE>
400 void G4TNtupleManager<TNTUPLE>::FinishNtuple(
401  G4int ntupleId)
402 {
403  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple");
404  if ( ! ntupleDescription ) return;
405 
406 #ifdef G4VERBOSE
407  if ( fState.GetVerboseL4() ) {
408  G4ExceptionDescription description;
409  description << ntupleDescription->fNtupleBooking.name() << " ntupleId " << ntupleId;
410  fState.GetVerboseL4()->Message("finish", "ntuple", description);
411  }
412 #endif
413 
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
418 
419  FinishTNtuple(ntupleDescription);
420 
421 #ifdef G4VERBOSE
422  if ( fState.GetVerboseL2() ) {
423  G4ExceptionDescription description;
424  description << ntupleDescription->fNtupleBooking.name() << " ntupleId " << ntupleId;
425  fState.GetVerboseL2()->Message("finish", "ntuple", description);
426  }
427 #endif
428 }
429 
430 //_____________________________________________________________________________
431 template <typename TNTUPLE>
432 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
433  G4int columnId, G4int value)
434 {
435  return FillNtupleTColumn<int>(columnId, value);
436 }
437 
438 //_____________________________________________________________________________
439 template <typename TNTUPLE>
440 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
441  G4int columnId, G4float value)
442 {
443  return FillNtupleTColumn<float>(columnId, value);
444 }
445 
446 //_____________________________________________________________________________
447 template <typename TNTUPLE>
448 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
449  G4int columnId, G4double value)
450 {
451  return FillNtupleTColumn<double>(columnId, value);
452 }
453 
454 //_____________________________________________________________________________
455 template <typename TNTUPLE>
456 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
457  G4int columnId, const G4String& value)
458 {
459  return FillNtupleTColumn<std::string>(columnId, value);
460 }
461 
462 //_____________________________________________________________________________
463 template <typename TNTUPLE>
464 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow()
465 {
466  return AddNtupleRow(fFirstId);
467 }
468 
469 //_____________________________________________________________________________
470 template <typename TNTUPLE>
471 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
472  G4int ntupleId, G4int columnId, G4int value)
473 {
474  return FillNtupleTColumn<int>(ntupleId, columnId, value);
475 }
476 //_____________________________________________________________________________
477 template <typename TNTUPLE>
478 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
479  G4int ntupleId, G4int columnId, G4float value)
480 {
481  return FillNtupleTColumn<float>(ntupleId, columnId, value);
482 }
483 
484 //_____________________________________________________________________________
485 template <typename TNTUPLE>
486 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
487  G4int ntupleId, G4int columnId, G4double value)
488 {
489  return FillNtupleTColumn<double>(ntupleId, columnId, value);
490 }
491 
492 //_____________________________________________________________________________
493 template <typename TNTUPLE>
494 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
495  G4int ntupleId, G4int columnId, const G4String& value)
496 {
497  return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
498 }
499 
500 //_____________________________________________________________________________
501 template <typename TNTUPLE>
502 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow(
503  G4int ntupleId)
504 {
505  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
506  //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
507  return false;
508  }
509 
510 #ifdef G4VERBOSE
511  if ( fState.GetVerboseL4() ) {
512  G4ExceptionDescription description;
513  description << " ntupleId " << ntupleId;
514  fState.GetVerboseL4()->Message("add", "ntuple row", description);
515  }
516 #endif
517 
518  auto ntuple = GetNtupleInFunction(ntupleId, "AddNtupleRow");
519  if ( ! ntuple ) return false;
520 
521  auto result = ntuple->add_row();
522  if ( ! result ) {
523  G4ExceptionDescription description;
524  description << " " << " ntupleId " << ntupleId
525  << "adding row has failed.";
526  G4Exception("G4TNtupleManager::AddTNtupleRow()",
527  "Analysis_W002", JustWarning, description);
528  }
529 
530 #ifdef G4VERBOSE
531  if ( fState.GetVerboseL4() ) {
532  G4ExceptionDescription description;
533  description << " ntupleId " << ntupleId;
534  fState.GetVerboseL4()->Message("add", "ntuple row", description);
535  }
536 #endif
537 
538  return true;
539 }
540 
541 //_____________________________________________________________________________
542 template <typename TNTUPLE>
543 void G4TNtupleManager<TNTUPLE>::SetActivation(
544  G4bool activation)
545 {
546  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
547  ntupleDescription->fActivation = activation;
548  }
549 }
550 
551 //_____________________________________________________________________________
552 template <typename TNTUPLE>
553 void G4TNtupleManager<TNTUPLE>::SetActivation(
554  G4int ntupleId, G4bool activation)
555 {
556  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
557  if ( ! ntupleDescription ) return;
558 
559  ntupleDescription->fActivation = activation;
560 }
561 
562 //_____________________________________________________________________________
563 template <typename TNTUPLE>
564 G4bool G4TNtupleManager<TNTUPLE>::GetActivation(
565  G4int ntupleId) const
566 {
567  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
568  if ( ! ntupleDescription ) return false;
569 
570  return ntupleDescription->fActivation;
571 }
572 
573 //_____________________________________________________________________________
574 template <typename TNTUPLE>
575 TNTUPLE*
576 G4TNtupleManager<TNTUPLE>::GetNtuple() const
577 {
578  return GetNtuple(fFirstId);
579 }
580 
581 //_____________________________________________________________________________
582 template <typename TNTUPLE>
583 TNTUPLE*
584 G4TNtupleManager<TNTUPLE>::GetNtuple(G4int ntupleId) const
585 {
586  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
587  if ( ! ntupleDescription ) return nullptr;
588 
589  return ntupleDescription->fNtuple;
590 }
591 
592 //_____________________________________________________________________________
593 template <typename TNTUPLE>
594 typename std::vector<TNTUPLE*>::iterator
595 G4TNtupleManager<TNTUPLE>::BeginNtuple()
596 {
597  return fNtupleVector.begin();
598 }
599 
600 //_____________________________________________________________________________
601 template <typename TNTUPLE>
602 typename std::vector<TNTUPLE*>::iterator
603 G4TNtupleManager<TNTUPLE>::EndNtuple()
604 {
605  return fNtupleVector.end();
606 }
607 
608 //_____________________________________________________________________________
609 template <typename TNTUPLE>
610 typename std::vector<TNTUPLE*>::const_iterator
611 G4TNtupleManager<TNTUPLE>::BeginConstNtuple() const
612 {
613  return fNtupleVector.begin();
614 }
615 
616 //_____________________________________________________________________________
617 template <typename TNTUPLE>
618 typename std::vector<TNTUPLE*>::const_iterator
619 G4TNtupleManager<TNTUPLE>::EndConstNtuple() const
620 {
621  return fNtupleVector.end();
622 }
623 
624 //_____________________________________________________________________________
625 template <typename TNTUPLE>
626 G4int G4TNtupleManager<TNTUPLE>::GetNofNtuples() const
627 {
628  return fNtupleVector.size();
629 }