Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>*
59 G4RootNtupleManager::GetNtupleIColumn(G4int ntupleId, G4int columnId) const
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  << "column " << 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>*
84 G4RootNtupleManager::GetNtupleFColumn(G4int ntupleId, G4int columnId) const
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  << "column " << 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>*
110 G4RootNtupleManager::GetNtupleDColumn(G4int ntupleId, G4int columnId) const
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  << "column " << 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 //_____________________________________________________________________________
134 G4RootNtupleDescription* G4RootNtupleManager::GetNtupleInFunction(G4int id,
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  tools::ntuple_booking* ntupleBooking
334  = ntupleDescription->fNtupleBooking;
335 
336  if ( ! ntupleBooking ) {
337  G4ExceptionDescription description;
338  description << " "
339  << "Ntuple " << ntupleId << " has to be created first. ";
340  G4Exception("G4RootNtupleManager::CreateNtupleIColumn()",
341  "Analysis_W005", JustWarning, description);
342  return -1;
343  }
344 
345  // Save column info in booking
346  G4int index = ntupleBooking->m_columns.size();
347  ntupleBooking->add_column<int>(name);
348 
349  // Create column if ntuple already exists
350  if ( ntupleDescription->fNtuple ) {
351  tools::wroot::ntuple::column<int>* column
352  = ntupleDescription->fNtuple->create_column<int>(name);
353  ntupleDescription->fNtupleIColumnMap[index] = column;
354  }
355 
357 
358 #ifdef G4VERBOSE
359  if ( fState.GetVerboseL2() ) {
360  G4ExceptionDescription description;
361  description << name << " ntupleId " << ntupleId;
362  fState.GetVerboseL2()->Message("create", "ntuple I column", description);
363  }
364 #endif
365 
366  return index + fFirstNtupleColumnId;
367 }
368 
369 //_____________________________________________________________________________
371 {
372 #ifdef G4VERBOSE
373  if ( fState.GetVerboseL4() ) {
374  G4ExceptionDescription description;
375  description << name << " ntupleId " << ntupleId;
376  fState.GetVerboseL4()->Message("create", "ntuple F column", description);
377  }
378 #endif
379 
380  G4RootNtupleDescription* ntupleDescription
381  = GetNtupleInFunction(ntupleId, "CreateNtupleFColumn");
382  tools::ntuple_booking* ntupleBooking
383  = ntupleDescription->fNtupleBooking;
384 
385  if ( ! ntupleBooking ) {
386  G4ExceptionDescription description;
387  description << " "
388  << "Ntuple " << ntupleId << " has to be created first. ";
389  G4Exception("G4RootNtupleManager::CreateNtupleFColumn()",
390  "Analysis_W005", JustWarning, description);
391  return -1;
392  }
393 
394  // Save column info in booking
395  G4int index = ntupleBooking->m_columns.size();
396  ntupleBooking->add_column<float>(name);
397 
398  // Create column if ntuple already exists
399  if ( ntupleDescription->fNtuple ) {
400  tools::wroot::ntuple::column<float>* column
401  = ntupleDescription->fNtuple->create_column<float>(name);
402  ntupleDescription->fNtupleFColumnMap[index] = column;
403  }
404 
406 
407 #ifdef G4VERBOSE
408  if ( fState.GetVerboseL2() ) {
409  G4ExceptionDescription description;
410  description << name << " ntupleId " << ntupleId;
411  fState.GetVerboseL2()->Message("create", "ntuple F column", description);
412  }
413 #endif
414 
415  return index + fFirstNtupleColumnId;
416 }
417 
418 
419 //_____________________________________________________________________________
421 {
422 #ifdef G4VERBOSE
423  if ( fState.GetVerboseL4() ) {
424  G4ExceptionDescription description;
425  description << name << " ntupleId " << ntupleId;
426  fState.GetVerboseL4()->Message("create", "ntuple D column", description);
427  }
428 #endif
429 
430  G4RootNtupleDescription* ntupleDescription
431  = GetNtupleInFunction(ntupleId, "CreateNtupleDColumn");
432  tools::ntuple_booking* ntupleBooking
433  = ntupleDescription->fNtupleBooking;
434 
435  if ( ! ntupleBooking ) {
436  G4ExceptionDescription description;
437  description << " "
438  << "Ntuple " << ntupleId << " has to be created first. ";
439  G4Exception("G4RootNtupleManager::CreateNtupleDColumn()",
440  "Analysis_W005", JustWarning, description);
441  return -1;
442  }
443 
444  // Save column info in booking
445  G4int index = ntupleBooking->m_columns.size();
446  ntupleBooking->add_column<double>(name);
447 
448  // Create column if ntuple already exists
449  if ( ntupleDescription->fNtuple ) {
450  tools::wroot::ntuple::column<double>* column
451  = ntupleDescription->fNtuple->create_column<double>(name);
452  ntupleDescription->fNtupleDColumnMap[index] = column;
453  }
454 
456 
457 #ifdef G4VERBOSE
458  if ( fState.GetVerboseL2() ) {
459  G4ExceptionDescription description;
460  description << name << " ntupleId " << ntupleId;
461  fState.GetVerboseL2()->Message("create", "ntuple D column", description);
462  }
463 #endif
464 
465  return index + fFirstNtupleColumnId;
466 }
467 
468 //_____________________________________________________________________________
470 {
471  // nothing to be done here
472 }
473 
474 //_____________________________________________________________________________
476 {
477  return FillNtupleIColumn(fFirstId, columnId, value);
478 }
479 
480 //_____________________________________________________________________________
482 {
483  return FillNtupleFColumn(fFirstId, columnId, value);
484 }
485 
486 //_____________________________________________________________________________
488 {
489  return FillNtupleDColumn(fFirstId, columnId, value);
490 }
491 
492 //_____________________________________________________________________________
494 {
495  return AddNtupleRow(fFirstId);
496 }
497 
498 //_____________________________________________________________________________
500  G4int value)
501 {
502  tools::wroot::ntuple::column<int>* column
503  = GetNtupleIColumn(ntupleId, columnId);
504  if ( ! column ) {
505  G4ExceptionDescription description;
506  description << " " << "ntupleId " << ntupleId
507  << "column " << columnId << " does not exist.";
508  G4Exception("G4RootNtupleManager::FillNtupleIColumn()",
509  "Analysis_W009", JustWarning, description);
510  return false;
511  }
512 
513  column->fill(value);
514 #ifdef G4VERBOSE
515  if ( fState.GetVerboseL4() ) {
516  G4ExceptionDescription description;
517  description << " ntupleId " << ntupleId
518  << " columnId " << columnId << " value " << value;
519  fState.GetVerboseL4()->Message("fill", "ntuple I column", description);
520  }
521 #endif
522  return true;
523 }
524 //_____________________________________________________________________________
526  G4float value)
527 {
528  tools::wroot::ntuple::column<float>* column
529  = GetNtupleFColumn(ntupleId, columnId);
530  if ( ! column ) {
531  G4ExceptionDescription description;
532  description << " " << "ntupleId " << ntupleId
533  << "column " << columnId << " does not exist.";
534  G4Exception("G4RootNtupleManager::FillNtupleFColumn()",
535  "Analysis_W009", JustWarning, description);
536  return false;
537  }
538 
539  column->fill(value);
540 #ifdef G4VERBOSE
541  if ( fState.GetVerboseL4() ) {
542  G4ExceptionDescription description;
543  description << " ntupleId " << ntupleId
544  << " columnId " << columnId << " value " << value;
545  fState.GetVerboseL4()->Message("fill", "ntuple F column", description);
546  }
547 #endif
548  return true;
549 }
550 //_____________________________________________________________________________
552  G4double value)
553 {
554  tools::wroot::ntuple::column<double>* column
555  = GetNtupleDColumn(ntupleId, columnId);
556  if ( ! column ) {
557  G4ExceptionDescription description;
558  description << " " << "ntupleId " << ntupleId
559  << "column " << columnId << " does not exist.";
560  G4Exception("G4RootNtupleManager::FillNtupleDColumn()",
561  "Analysis_W009", JustWarning, description);
562  return false;
563  }
564 
565  column->fill(value);
566 #ifdef G4VERBOSE
567  if ( fState.GetVerboseL4() ) {
568  G4ExceptionDescription description;
569  description << " ntupleId " << ntupleId
570  << " columnId " << columnId << " value " << value;
571  fState.GetVerboseL4()->Message("fill", "ntuple D column", description);
572  }
573 #endif
574  return true;
575 }
576 
577 //_____________________________________________________________________________
579 {
580 #ifdef G4VERBOSE
581  if ( fState.GetVerboseL4() ) {
582  G4ExceptionDescription description;
583  description << " ntupleId " << ntupleId;
584  fState.GetVerboseL4()->Message("add", "ntuple row", description);
585  }
586 #endif
587 
588  G4RootNtupleDescription* ntupleDescription
589  = GetNtupleInFunction(ntupleId, "AddNtupleRow");
590 
591  if ( ! ntupleDescription || ! ntupleDescription->fNtuple ) {
592  G4ExceptionDescription description;
593  description << " " << " ntupleId " << ntupleId
594  << " does not exist. ";
595  G4Exception("G4RootNtupleManager::AddNtupleRow()",
596  "Analysis_W008", JustWarning, description);
597  return false;
598  }
599 
600  G4bool result = ntupleDescription->fNtuple->add_row();
601  if ( ! result ) {
602  G4ExceptionDescription description;
603  description << " " << " ntupleId " << ntupleId
604  << "adding row has failed.";
605  G4Exception("G4RootNtupleManager::AddNtupleRow()",
606  "Analysis_W004", JustWarning, description);
607  }
608 #ifdef G4VERBOSE
609  if ( fState.GetVerboseL4() ) {
610  G4ExceptionDescription description;
611  description << " ntupleId " << ntupleId;
612  fState.GetVerboseL4()->Message("add", "ntuple row", description, result);
613  }
614 #endif
615 
616  return result;
617 }
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
float G4float
Definition: G4Types.hh:77
std::map< G4int, tools::wroot::ntuple::column< int > * > fNtupleIColumnMap
const XML_Char * name
virtual G4int CreateNtupleDColumn(const G4String &name)
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
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)
subroutine title(NA, NB, NCA, NCB)
Definition: dpm25nuc7.f:1744
virtual G4bool AddNtupleRow()
tools::wroot::ntuple * fNtuple
tools::ntuple_booking * fNtupleBooking
const XML_Char int const XML_Char * value
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