Geant4  10.02.p02
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, "CreateNtupleIColumn");
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 
418  FinishTNtuple(ntupleDescription);
419 
420 #ifdef G4VERBOSE
421  if ( fState.GetVerboseL2() ) {
422  G4ExceptionDescription description;
423  description << ntupleDescription->fNtupleBooking.name() << " ntupleId " << ntupleId;
424  fState.GetVerboseL2()->Message("finish", "ntuple", description);
425  }
426 #endif
427 }
428 
429 //_____________________________________________________________________________
430 template <typename TNTUPLE>
431 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
432  G4int columnId, G4int value)
433 {
434  return FillNtupleTColumn<int>(columnId, value);
435 }
436 
437 //_____________________________________________________________________________
438 template <typename TNTUPLE>
439 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
440  G4int columnId, G4float value)
441 {
442  return FillNtupleTColumn<float>(columnId, value);
443 }
444 
445 //_____________________________________________________________________________
446 template <typename TNTUPLE>
447 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
448  G4int columnId, G4double value)
449 {
450  return FillNtupleTColumn<double>(columnId, value);
451 }
452 
453 //_____________________________________________________________________________
454 template <typename TNTUPLE>
455 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
456  G4int columnId, const G4String& value)
457 {
458  return FillNtupleTColumn<std::string>(columnId, value);
459 }
460 
461 //_____________________________________________________________________________
462 template <typename TNTUPLE>
463 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow()
464 {
465  return AddNtupleRow(fFirstId);
466 }
467 
468 //_____________________________________________________________________________
469 template <typename TNTUPLE>
470 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleIColumn(
471  G4int ntupleId, G4int columnId, G4int value)
472 {
473  return FillNtupleTColumn<int>(ntupleId, columnId, value);
474 }
475 //_____________________________________________________________________________
476 template <typename TNTUPLE>
477 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleFColumn(
478  G4int ntupleId, G4int columnId, G4float value)
479 {
480  return FillNtupleTColumn<float>(ntupleId, columnId, value);
481 }
482 
483 //_____________________________________________________________________________
484 template <typename TNTUPLE>
485 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleDColumn(
486  G4int ntupleId, G4int columnId, G4double value)
487 {
488  return FillNtupleTColumn<double>(ntupleId, columnId, value);
489 }
490 
491 //_____________________________________________________________________________
492 template <typename TNTUPLE>
493 G4bool G4TNtupleManager<TNTUPLE>::FillNtupleSColumn(
494  G4int ntupleId, G4int columnId, const G4String& value)
495 {
496  return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
497 }
498 
499 //_____________________________________________________________________________
500 template <typename TNTUPLE>
501 G4bool G4TNtupleManager<TNTUPLE>::AddNtupleRow(
502  G4int ntupleId)
503 {
504  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
505  //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
506  return false;
507  }
508 
509 #ifdef G4VERBOSE
510  if ( fState.GetVerboseL4() ) {
511  G4ExceptionDescription description;
512  description << " ntupleId " << ntupleId;
513  fState.GetVerboseL4()->Message("add", "ntuple row", description);
514  }
515 #endif
516 
517  auto ntuple = GetNtupleInFunction(ntupleId, "AddNtupleRow");
518  if ( ! ntuple ) return false;
519 
520  auto result = ntuple->add_row();
521  if ( ! result ) {
522  G4ExceptionDescription description;
523  description << " " << " ntupleId " << ntupleId
524  << "adding row has failed.";
525  G4Exception("G4TNtupleManager::AddTNtupleRow()",
526  "Analysis_W002", JustWarning, description);
527  }
528 
529 #ifdef G4VERBOSE
530  if ( fState.GetVerboseL4() ) {
531  G4ExceptionDescription description;
532  description << " ntupleId " << ntupleId;
533  fState.GetVerboseL4()->Message("add", "ntuple row", description);
534  }
535 #endif
536 
537  return true;
538 }
539 
540 //_____________________________________________________________________________
541 template <typename TNTUPLE>
542 void G4TNtupleManager<TNTUPLE>::SetActivation(
543  G4bool activation)
544 {
545  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
546  ntupleDescription->fActivation = activation;
547  }
548 }
549 
550 //_____________________________________________________________________________
551 template <typename TNTUPLE>
552 void G4TNtupleManager<TNTUPLE>::SetActivation(
553  G4int ntupleId, G4bool activation)
554 {
555  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
556  if ( ! ntupleDescription ) return;
557 
558  ntupleDescription->fActivation = activation;
559 }
560 
561 //_____________________________________________________________________________
562 template <typename TNTUPLE>
563 G4bool G4TNtupleManager<TNTUPLE>::GetActivation(
564  G4int ntupleId) const
565 {
566  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
567  if ( ! ntupleDescription ) return false;
568 
569  return ntupleDescription->fActivation;
570 }
571 
572 //_____________________________________________________________________________
573 template <typename TNTUPLE>
574 TNTUPLE*
575 G4TNtupleManager<TNTUPLE>::GetNtuple() const
576 {
577  return GetNtuple(fFirstId);
578 }
579 
580 //_____________________________________________________________________________
581 template <typename TNTUPLE>
582 TNTUPLE*
583 G4TNtupleManager<TNTUPLE>::GetNtuple(G4int ntupleId) const
584 {
585  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
586  if ( ! ntupleDescription ) return nullptr;
587 
588  return ntupleDescription->fNtuple;
589 }
590 
591 //_____________________________________________________________________________
592 template <typename TNTUPLE>
593 typename std::vector<TNTUPLE*>::iterator
594 G4TNtupleManager<TNTUPLE>::BeginNtuple()
595 {
596  return fNtupleVector.begin();
597 }
598 
599 //_____________________________________________________________________________
600 template <typename TNTUPLE>
601 typename std::vector<TNTUPLE*>::iterator
602 G4TNtupleManager<TNTUPLE>::EndNtuple()
603 {
604  return fNtupleVector.end();
605 }
606 
607 //_____________________________________________________________________________
608 template <typename TNTUPLE>
609 typename std::vector<TNTUPLE*>::const_iterator
610 G4TNtupleManager<TNTUPLE>::BeginConstNtuple() const
611 {
612  return fNtupleVector.begin();
613 }
614 
615 //_____________________________________________________________________________
616 template <typename TNTUPLE>
617 typename std::vector<TNTUPLE*>::const_iterator
618 G4TNtupleManager<TNTUPLE>::EndConstNtuple() const
619 {
620  return fNtupleVector.end();
621 }
622 
623 //_____________________________________________________________________________
624 template <typename TNTUPLE>
625 G4int G4TNtupleManager<TNTUPLE>::GetNofNtuples() const
626 {
627  return fNtupleVector.size();
628 }