Geant4  10.00.p02
G4RootNtupleManager.cc
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: G4RootNtupleManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootNtupleManager.hh"
33 
34 #include "tools/wroot/file"
35 
36 //_____________________________________________________________________________
38  : G4VNtupleManager(state),
39  fNtupleDirectory(0),
40  fNtupleVector()
41 {
42 }
43 
44 //_____________________________________________________________________________
46 {
47  std::vector<G4RootNtupleDescription*>::iterator it;
48  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
49  delete (*it);
50  }
51 }
52 
53 //
54 // private methods
55 //
56 
57 //_____________________________________________________________________________
58 tools::wroot::ntuple::column<int>*
60 {
61  G4RootNtupleDescription* ntupleDecription
62  = GetNtupleInFunction(ntupleId, "GetNtupleIColumn");
63  if ( ! ntupleDecription ) return 0;
64 
65  std::map<G4int, tools::wroot::ntuple::column<int>* >& ntupleIColumnMap
66  = ntupleDecription->fNtupleIColumnMap;
67  std::map<G4int, tools::wroot::ntuple::column<int>* >::const_iterator it
68  = ntupleIColumnMap.find(columnId);
69 
70  if ( it == ntupleIColumnMap.end() ) {
71  G4ExceptionDescription description;
72  description << " " << "ntupleId " << ntupleId
73  << " columnId " << columnId << " does not exist.";
74  G4Exception("G4RootNtupleManager::GetNtupleIColumn()",
75  "Analysis_W009", JustWarning, description);
76  return 0;
77  }
78 
79  return it->second;
80 }
81 
82 //_____________________________________________________________________________
83 tools::wroot::ntuple::column<float>*
85 {
86  G4RootNtupleDescription* ntupleDecription
87  = GetNtupleInFunction(ntupleId, "GetNtupleIColumn");
88  if ( ! ntupleDecription ) return 0;
89 
90  std::map<G4int, tools::wroot::ntuple::column<float>* >& ntupleFColumnMap
91  = ntupleDecription->fNtupleFColumnMap;
92  std::map<G4int, tools::wroot::ntuple::column<float>* >::const_iterator it
93  = ntupleFColumnMap.find(columnId);
94 
95  if ( it == ntupleFColumnMap.end() ) {
96  G4ExceptionDescription description;
97  description << " " << "ntupleId " << ntupleId
98  << " columnId " << columnId << " does not exist.";
99  G4Exception("G4RootNtupleManager::GetNtupleFColumn()",
100  "Analysis_W009", JustWarning, description);
101  return 0;
102  }
103 
104  return it->second;
105 }
106 
107 
108 //_____________________________________________________________________________
109 tools::wroot::ntuple::column<double>*
111 {
112  G4RootNtupleDescription* ntupleDecription
113  = GetNtupleInFunction(ntupleId, "GetNtupleIColumn");
114  if ( ! ntupleDecription ) return 0;
115 
116  std::map<G4int, tools::wroot::ntuple::column<double>* >& ntupleDColumnMap
117  = ntupleDecription->fNtupleDColumnMap;
118  std::map<G4int, tools::wroot::ntuple::column<double>* >::const_iterator it
119  = ntupleDColumnMap.find(columnId);
120 
121  if ( it == ntupleDColumnMap.end() ) {
122  G4ExceptionDescription description;
123  description << " " << "ntupleId " << ntupleId
124  << " columnId " << columnId << " does not exist.";
125  G4Exception("G4RootNtupleManager::GetNtupleDColumn()",
126  "Analysis_W009", JustWarning, description);
127  return 0;
128  }
129 
130  return it->second;
131 }
132 
133 //_____________________________________________________________________________
135  G4String functionName, G4bool warn,
136  G4bool /*onlyIfActive*/) const
137 {
138  G4int index = id - fFirstId;
139  if ( index < 0 || index >= G4int(fNtupleVector.size()) ) {
140  if ( warn) {
141  G4String inFunction = "G4RootNtupleManager::";
142  inFunction += functionName;
143  G4ExceptionDescription description;
144  description << " " << "ntuple " << id << " does not exist.";
145  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
146  }
147  return 0;
148  }
149 
150  return fNtupleVector[index];
151 }
152 
153 //
154 // protected methods
155 //
156 
157 //_____________________________________________________________________________
159 {
160 // Create ntuple from ntuple_booking.
161 
162  if ( ! fNtupleVector.size() ) return;
163 
164  std::vector<G4RootNtupleDescription*>::iterator itn;
165  for (itn = fNtupleVector.begin(); itn != fNtupleVector.end(); itn++ ) {
166 
167  tools::ntuple_booking* ntupleBooking = (*itn)->fNtupleBooking;
168  if ( ! ntupleBooking ) continue;
169 
170 #ifdef G4VERBOSE
171  if ( fState.GetVerboseL4() )
173  ->Message("create from booking", "ntuple", ntupleBooking->m_name);
174 #endif
175 
176  (*itn)->fNtuple
177  = new tools::wroot::ntuple(*fNtupleDirectory, *ntupleBooking);
178 
179  if ( ntupleBooking->m_columns.size() ) {
180  // store ntuple columns in local maps
181  const std::vector<tools::ntuple_booking::col_t>& columns
182  = ntupleBooking->m_columns;
183  std::vector<tools::ntuple_booking::col_t>::const_iterator it;
184  G4int index = 0;
185  for ( it = columns.begin(); it!=columns.end(); ++it) {
186  if ( (*it).second == tools::_cid(int(0) ) ) {
187  (*itn)->fNtupleIColumnMap[index++]
188  = (*itn)->fNtuple->find_column<int>((*it).first);
189  }
190  else if ( (*it).second == tools::_cid(float(0) ) ) {
191  (*itn)->fNtupleFColumnMap[index++]
192  = (*itn)->fNtuple->find_column<float>((*it).first);
193  }
194  else if ( (*it).second== tools::_cid(double(0))) {
195  (*itn)->fNtupleDColumnMap[index++]
196  = (*itn)->fNtuple->find_column<double>((*it).first);
197  }
198  else {
199  G4ExceptionDescription description;
200  description << " "
201  << "Unsupported column type " << (*it).first;
202  G4Exception("G4RootNtupleManager::CreateNtupleFromBooking()",
203  "Analysis_W004", JustWarning, description);
204  }
205  }
206  }
207 #ifdef G4VERBOSE
208  if ( fState.GetVerboseL3() )
210  ->Message("create from booking", "ntuple", ntupleBooking->m_name);
211 #endif
212  }
213 }
214 
215 //_____________________________________________________________________________
217 {
218  return ! fNtupleVector.size();
219 }
220 
221 //_____________________________________________________________________________
223 {
224 // Reset ntuples
225 
226  std::vector<G4RootNtupleDescription*>::iterator it;
227  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
228  // ntuple is deleted automatically when file is closed
229  // delete (*it)->fNtuple;
230  (*it)->fNtuple=0;
231  }
232 
233  return true;
234 }
235 
236 //_____________________________________________________________________________
237 tools::wroot::ntuple* G4RootNtupleManager::GetNtuple() const
238 {
239  return GetNtuple(fFirstId);
240 }
241 
242 //_____________________________________________________________________________
243 tools::wroot::ntuple* G4RootNtupleManager::GetNtuple(G4int ntupleId) const
244 {
245  G4RootNtupleDescription* ntupleDescription
246  = GetNtupleInFunction(ntupleId, "GetNtuple");
247 
248  return ntupleDescription->fNtuple;
249 }
250 
251 //_____________________________________________________________________________
253  const G4String& title)
254 {
255 #ifdef G4VERBOSE
256  if ( fState.GetVerboseL4() )
257  fState.GetVerboseL4()->Message("create", "ntuple", name);
258 #endif
259 
260  // Create ntuple description
261  G4int index = fNtupleVector.size();
262  G4RootNtupleDescription* ntupleDescription
263  = new G4RootNtupleDescription();
264  fNtupleVector.push_back(ntupleDescription);
265 
266  // Create ntuple booking
267  ntupleDescription->fNtupleBooking = new tools::ntuple_booking();
268  ntupleDescription->fNtupleBooking->m_name = name;
269  ntupleDescription->fNtupleBooking->m_title = title;
270  // ntuple booking object is deleted in destructor
271 
272  // Create ntuple if the file is open
273  if ( fNtupleDirectory ) {
274  ntupleDescription->fNtuple
275  = new tools::wroot::ntuple(*fNtupleDirectory, name, title);
276  // ntuple object is deleted automatically when closing a file
277  }
278 
279  fLockFirstId = true;
280 
281 #ifdef G4VERBOSE
282  if ( fState.GetVerboseL2() ) {
283  G4ExceptionDescription description;
284  description << name << " ntupleId " << index + fFirstId;
285  fState.GetVerboseL2()->Message("create", "ntuple", description);
286  }
287 #endif
288 
289  return index + fFirstId;
290 }
291 
292 //_____________________________________________________________________________
294 {
295  G4int ntupleId = fNtupleVector.size() + fFirstId - 1;
296  return CreateNtupleIColumn(ntupleId, name);
297 }
298 
299 //_____________________________________________________________________________
301 {
302  G4int ntupleId = fNtupleVector.size() + fFirstId - 1;
303  return CreateNtupleFColumn(ntupleId, name);
304 }
305 
306 //_____________________________________________________________________________
308 {
309  G4int ntupleId = fNtupleVector.size() + fFirstId - 1;
310  return CreateNtupleDColumn(ntupleId, name);
311 }
312 
313 //_____________________________________________________________________________
315 {
316  // nothing to be done here
317 }
318 
319 //_____________________________________________________________________________
321  const G4String& name)
322 {
323 #ifdef G4VERBOSE
324  if ( fState.GetVerboseL4() ) {
325  G4ExceptionDescription description;
326  description << name << " ntupleId " << ntupleId;
327  fState.GetVerboseL4()->Message("create", "ntuple I column", description);
328  }
329 #endif
330 
331  G4RootNtupleDescription* ntupleDescription
332  = GetNtupleInFunction(ntupleId, "CreateNtupleIColumn");
333  if ( ! ntupleDescription ) return -1;
334 
335  tools::ntuple_booking* ntupleBooking
336  = ntupleDescription->fNtupleBooking;
337  if ( ! ntupleBooking ) {
338  G4ExceptionDescription description;
339  description << " "
340  << "Ntuple " << ntupleId << " has to be created first. ";
341  G4Exception("G4RootNtupleManager::CreateNtupleIColumn()",
342  "Analysis_W005", JustWarning, description);
343  return -1;
344  }
345 
346  // Save column info in booking
347  G4int index = ntupleBooking->m_columns.size();
348  ntupleBooking->add_column<int>(name);
349 
350  // Create column if ntuple already exists
351  if ( ntupleDescription->fNtuple ) {
352  tools::wroot::ntuple::column<int>* column
353  = ntupleDescription->fNtuple->create_column<int>(name);
354  ntupleDescription->fNtupleIColumnMap[index] = column;
355  }
356 
358 
359 #ifdef G4VERBOSE
360  if ( fState.GetVerboseL2() ) {
361  G4ExceptionDescription description;
362  description << name << " ntupleId " << ntupleId;
363  fState.GetVerboseL2()->Message("create", "ntuple I column", description);
364  }
365 #endif
366 
367  return index + fFirstNtupleColumnId;
368 }
369 
370 //_____________________________________________________________________________
372 {
373 #ifdef G4VERBOSE
374  if ( fState.GetVerboseL4() ) {
375  G4ExceptionDescription description;
376  description << name << " ntupleId " << ntupleId;
377  fState.GetVerboseL4()->Message("create", "ntuple F column", description);
378  }
379 #endif
380 
381  G4RootNtupleDescription* ntupleDescription
382  = GetNtupleInFunction(ntupleId, "CreateNtupleFColumn");
383  if ( ! ntupleDescription ) return -1;
384 
385  tools::ntuple_booking* ntupleBooking
386  = ntupleDescription->fNtupleBooking;
387 
388  if ( ! ntupleBooking ) {
389  G4ExceptionDescription description;
390  description << " "
391  << "Ntuple " << ntupleId << " has to be created first. ";
392  G4Exception("G4RootNtupleManager::CreateNtupleFColumn()",
393  "Analysis_W005", JustWarning, description);
394  return -1;
395  }
396 
397  // Save column info in booking
398  G4int index = ntupleBooking->m_columns.size();
399  ntupleBooking->add_column<float>(name);
400 
401  // Create column if ntuple already exists
402  if ( ntupleDescription->fNtuple ) {
403  tools::wroot::ntuple::column<float>* column
404  = ntupleDescription->fNtuple->create_column<float>(name);
405  ntupleDescription->fNtupleFColumnMap[index] = column;
406  }
407 
409 
410 #ifdef G4VERBOSE
411  if ( fState.GetVerboseL2() ) {
412  G4ExceptionDescription description;
413  description << name << " ntupleId " << ntupleId;
414  fState.GetVerboseL2()->Message("create", "ntuple F column", description);
415  }
416 #endif
417 
418  return index + fFirstNtupleColumnId;
419 }
420 
421 
422 //_____________________________________________________________________________
424 {
425 #ifdef G4VERBOSE
426  if ( fState.GetVerboseL4() ) {
427  G4ExceptionDescription description;
428  description << name << " ntupleId " << ntupleId;
429  fState.GetVerboseL4()->Message("create", "ntuple D column", description);
430  }
431 #endif
432 
433  G4RootNtupleDescription* ntupleDescription
434  = GetNtupleInFunction(ntupleId, "CreateNtupleDColumn");
435  if ( ! ntupleDescription ) return -1;
436 
437  tools::ntuple_booking* ntupleBooking
438  = ntupleDescription->fNtupleBooking;
439 
440  if ( ! ntupleBooking ) {
441  G4ExceptionDescription description;
442  description << " "
443  << "Ntuple " << ntupleId << " has to be created first. ";
444  G4Exception("G4RootNtupleManager::CreateNtupleDColumn()",
445  "Analysis_W005", JustWarning, description);
446  return -1;
447  }
448 
449  // Save column info in booking
450  G4int index = ntupleBooking->m_columns.size();
451  ntupleBooking->add_column<double>(name);
452 
453  // Create column if ntuple already exists
454  if ( ntupleDescription->fNtuple ) {
455  tools::wroot::ntuple::column<double>* column
456  = ntupleDescription->fNtuple->create_column<double>(name);
457  ntupleDescription->fNtupleDColumnMap[index] = column;
458  }
459 
461 
462 #ifdef G4VERBOSE
463  if ( fState.GetVerboseL2() ) {
464  G4ExceptionDescription description;
465  description << name << " ntupleId " << ntupleId;
466  fState.GetVerboseL2()->Message("create", "ntuple D column", description);
467  }
468 #endif
469 
470  return index + fFirstNtupleColumnId;
471 }
472 
473 //_____________________________________________________________________________
475 {
476  // nothing to be done here
477 }
478 
479 //_____________________________________________________________________________
481 {
482  return FillNtupleIColumn(fFirstId, columnId, value);
483 }
484 
485 //_____________________________________________________________________________
487 {
488  return FillNtupleFColumn(fFirstId, columnId, value);
489 }
490 
491 //_____________________________________________________________________________
493 {
494  return FillNtupleDColumn(fFirstId, columnId, value);
495 }
496 
497 //_____________________________________________________________________________
499 {
500  return AddNtupleRow(fFirstId);
501 }
502 
503 //_____________________________________________________________________________
505  G4int value)
506 {
507  tools::wroot::ntuple::column<int>* column
508  = GetNtupleIColumn(ntupleId, columnId);
509  if ( ! column ) {
510  G4ExceptionDescription description;
511  description << " " << "ntupleId " << ntupleId
512  << " columnId " << columnId << " does not exist.";
513  G4Exception("G4RootNtupleManager::FillNtupleIColumn()",
514  "Analysis_W009", JustWarning, description);
515  return false;
516  }
517 
518  column->fill(value);
519 #ifdef G4VERBOSE
520  if ( fState.GetVerboseL4() ) {
521  G4ExceptionDescription description;
522  description << " ntupleId " << ntupleId
523  << " columnId " << columnId << " value " << value;
524  fState.GetVerboseL4()->Message("fill", "ntuple I column", description);
525  }
526 #endif
527  return true;
528 }
529 //_____________________________________________________________________________
531  G4float value)
532 {
533  tools::wroot::ntuple::column<float>* column
534  = GetNtupleFColumn(ntupleId, columnId);
535  if ( ! column ) {
536  G4ExceptionDescription description;
537  description << " " << "ntupleId " << ntupleId
538  << " columnId " << columnId << " does not exist.";
539  G4Exception("G4RootNtupleManager::FillNtupleFColumn()",
540  "Analysis_W009", JustWarning, description);
541  return false;
542  }
543 
544  column->fill(value);
545 #ifdef G4VERBOSE
546  if ( fState.GetVerboseL4() ) {
547  G4ExceptionDescription description;
548  description << " ntupleId " << ntupleId
549  << " columnId " << columnId << " value " << value;
550  fState.GetVerboseL4()->Message("fill", "ntuple F column", description);
551  }
552 #endif
553  return true;
554 }
555 //_____________________________________________________________________________
557  G4double value)
558 {
559  tools::wroot::ntuple::column<double>* column
560  = GetNtupleDColumn(ntupleId, columnId);
561  if ( ! column ) {
562  G4ExceptionDescription description;
563  description << " " << "ntupleId " << ntupleId
564  << " columnId " << columnId << " does not exist.";
565  G4Exception("G4RootNtupleManager::FillNtupleDColumn()",
566  "Analysis_W009", JustWarning, description);
567  return false;
568  }
569 
570  column->fill(value);
571 #ifdef G4VERBOSE
572  if ( fState.GetVerboseL4() ) {
573  G4ExceptionDescription description;
574  description << " ntupleId " << ntupleId
575  << " columnId " << columnId << " value " << value;
576  fState.GetVerboseL4()->Message("fill", "ntuple D column", description);
577  }
578 #endif
579  return true;
580 }
581 
582 //_____________________________________________________________________________
584 {
585 #ifdef G4VERBOSE
586  if ( fState.GetVerboseL4() ) {
587  G4ExceptionDescription description;
588  description << " ntupleId " << ntupleId;
589  fState.GetVerboseL4()->Message("add", "ntuple row", description);
590  }
591 #endif
592 
593  G4RootNtupleDescription* ntupleDescription
594  = GetNtupleInFunction(ntupleId, "AddNtupleRow");
595  if ( ! ntupleDescription ) return false;
596 
597  if ( ! ntupleDescription->fNtuple ) {
598  G4ExceptionDescription description;
599  description << " " << " ntupleId " << ntupleId
600  << " does not exist. ";
601  G4Exception("G4RootNtupleManager::AddNtupleRow()",
602  "Analysis_W008", JustWarning, description);
603  return false;
604  }
605 
606  G4bool result = ntupleDescription->fNtuple->add_row();
607  if ( ! result ) {
608  G4ExceptionDescription description;
609  description << " " << " ntupleId " << ntupleId
610  << "adding row has failed.";
611  G4Exception("G4RootNtupleManager::AddNtupleRow()",
612  "Analysis_W004", JustWarning, description);
613  }
614 #ifdef G4VERBOSE
615  if ( fState.GetVerboseL4() ) {
616  G4ExceptionDescription description;
617  description << " ntupleId " << ntupleId;
618  fState.GetVerboseL4()->Message("add", "ntuple row", description, result);
619  }
620 #endif
621 
622  return result;
623 }
tools::wroot::ntuple::column< int > * GetNtupleIColumn(G4int ntupleId, G4int columnId) const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool FillNtupleFColumn(G4int columnId, G4float value)
virtual G4int CreateNtupleFColumn(const G4String &name)
std::map< G4int, tools::wroot::ntuple::column< float > * > fNtupleFColumnMap
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
float G4float
Definition: G4Types.hh:77
tools::wroot::directory * fNtupleDirectory
std::map< G4int, tools::wroot::ntuple::column< int > * > fNtupleIColumnMap
virtual G4int CreateNtupleDColumn(const G4String &name)
tools::wroot::ntuple::column< float > * GetNtupleFColumn(G4int ntupleId, G4int columnId) const
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL2() const
virtual G4bool FillNtupleIColumn(G4int columnId, G4int value)
const G4AnalysisVerbose * GetVerboseL3() const
const G4AnalysisVerbose * GetVerboseL4() const
std::map< G4int, tools::wroot::ntuple::column< double > * > fNtupleDColumnMap
bool G4bool
Definition: G4Types.hh:79
std::vector< G4RootNtupleDescription * > fNtupleVector
virtual G4RootNtupleDescription * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true, G4bool onlyIfActive=true) const
tools::wroot::ntuple * GetNtuple() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual G4int CreateNtupleIColumn(const G4String &name)
virtual G4bool AddNtupleRow()
tools::wroot::ntuple * fNtuple
tools::ntuple_booking * fNtupleBooking
tools::wroot::ntuple::column< double > * GetNtupleDColumn(G4int ntupleId, G4int columnId) const
virtual G4int CreateNtuple(const G4String &name, const G4String &title)
G4bool fLockFirstNtupleColumnId
double G4double
Definition: G4Types.hh:76
G4RootNtupleManager(const G4AnalysisManagerState &state)
virtual G4bool FillNtupleDColumn(G4int columnId, G4double value)
const G4AnalysisManagerState & fState