Geant4  10.01.p02
G4XmlRNtupleManager.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$
27 
28 // Author: Ivana Hrivnacova, 25/07/2014 (ivana@ipno.in2p3.fr)
29 
30 #include "G4XmlRNtupleManager.hh"
33 
34 //
35 // utility function (to be provided in tools)
36 //
37 
38 namespace tools {
39 namespace aida {
40 template <class T>
41 bool to_vector(base_ntu& a_ntu,std::vector<T>& a_vec) {
42  a_vec.clear();
43  const std::vector<base_col*>& cols = a_ntu.cols();
44  if(cols.empty()) return false;
45  base_col* _base_col = cols.front();
46  aida_col<T>* _col = safe_cast<base_col, aida_col<T> >(*_base_col);
47  if(!_col) return false;
48  a_ntu.start();
49  uint64 _rows = a_ntu.rows();
50  a_vec.resize(_rows);
51  T v;
52  {for(uint64 row=0;row<_rows;row++) {
53  if(!a_ntu.next()) {a_vec.clear();return false;}
54  if(!_col->get_entry(v)) {a_vec.clear();return false;}
55  a_vec[row] = v;
56  }}
57  return true;
58 }
59 }}
60 
61 //_____________________________________________________________________________
63  : G4VRNtupleManager(state),
64  fNtupleVector()
65 {
66 }
67 
68 //_____________________________________________________________________________
70 {
71  std::vector<G4XmlRNtupleDescription*>::iterator it;
72  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
73  delete (*it);
74  }
75 }
76 
77 //
78 // private methods
79 //
80 
81 //_____________________________________________________________________________
83  G4String functionName, G4bool warn) const
84 {
85  G4int index = id - fFirstId;
86  if ( index < 0 || index >= G4int(fNtupleVector.size()) ) {
87  if ( warn) {
88  G4String inFunction = "G4XmlRNtupleManager::";
89  inFunction += functionName;
90  G4ExceptionDescription description;
91  description << " " << "ntuple " << id << " does not exist.";
92  G4Exception(inFunction, "Analysis_WR011", JustWarning, description);
93  }
94  return 0;
95  }
96 
97  return fNtupleVector[index];
98 }
99 
100 //
101 // protected methods
102 //
103 
104 //_____________________________________________________________________________
106 {
107  return ! fNtupleVector.size();
108 }
109 
110 //_____________________________________________________________________________
112 {
113 // Reset ntuples
114 
115  std::vector<G4XmlRNtupleDescription*>::iterator it;
116  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
117  // ntuple is deleted automatically when file is closed
118  // delete (*it)->fNtuple;
119  (*it)->fNtuple=0;
120  }
121 
122  return true;
123 }
124 
125 //_____________________________________________________________________________
126 tools::aida::ntuple* G4XmlRNtupleManager::GetNtuple() const
127 {
128  return GetNtuple(fFirstId);
129 }
130 
131 //_____________________________________________________________________________
132 tools::aida::ntuple* G4XmlRNtupleManager::GetNtuple(G4int ntupleId) const
133 {
134  G4XmlRNtupleDescription* rntupleDescription
135  = GetNtupleInFunction(ntupleId, "GetRNtuple");
136 
137  if ( ! rntupleDescription ) return 0;
138 
139  return rntupleDescription->fNtuple;
140 }
141 
142 //_____________________________________________________________________________
144 {
145  G4int id = fNtupleVector.size() + fFirstId;
146 
147  fNtupleVector.push_back(rntupleDescription);
148 
149  return id;
150 }
151 
152 //_____________________________________________________________________________
154  G4int& value)
155 {
156  return SetNtupleIColumn(fFirstId, columnName, value);
157 }
158 
159 //_____________________________________________________________________________
161  G4float& value)
162 {
163  return SetNtupleFColumn(fFirstId, columnName, value);
164 }
165 
166 //_____________________________________________________________________________
168  G4double& value)
169 {
170  return SetNtupleDColumn(fFirstId, columnName, value);
171 }
172 
173 //_____________________________________________________________________________
175  G4String& value)
176 {
177  return SetNtupleSColumn(fFirstId, columnName, value);
178 }
179 
180 //_____________________________________________________________________________
182  std::vector<G4int>& vector)
183 {
184  return SetNtupleIColumn(fFirstId, columnName, vector);
185 }
186 
187 //_____________________________________________________________________________
189  std::vector<G4float>& vector)
190 {
191  return SetNtupleFColumn(fFirstId, columnName, vector);
192 }
193 
194 //_____________________________________________________________________________
196  std::vector<G4double>& vector)
197 {
198  return SetNtupleDColumn(fFirstId, columnName, vector);
199 }
200 
201 //_____________________________________________________________________________
203  const G4String& columnName,
204  G4int& value)
205 {
206 #ifdef G4VERBOSE
207  if ( fState.GetVerboseL4() ) {
208  G4ExceptionDescription description;
209  description << " ntupleId " << ntupleId << " " << columnName;
210  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
211  }
212 #endif
213 
214  G4XmlRNtupleDescription* ntupleDescription
215  = GetNtupleInFunction(ntupleId, "SetNtupleIColumn");
216  if ( ! ntupleDescription ) return false;
217 
218  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
219  ntupleBinding->add_column(columnName, value);
220 
221 #ifdef G4VERBOSE
222  if ( fState.GetVerboseL2() ) {
223  G4ExceptionDescription description;
224  description << " ntupleId " << ntupleId << " " << columnName;
225  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
226  }
227 #endif
228 
229  return true;
230 }
231 
232 //_____________________________________________________________________________
234  const G4String& columnName,
235  G4float& value)
236 {
237 #ifdef G4VERBOSE
238  if ( fState.GetVerboseL4() ) {
239  G4ExceptionDescription description;
240  description << " ntupleId " << ntupleId << " " << columnName;
241  fState.GetVerboseL4()->Message("set", "ntuple F column", description);
242  }
243 #endif
244 
245  G4XmlRNtupleDescription* ntupleDescription
246  = GetNtupleInFunction(ntupleId, "SetNtupleFColumn");
247  if ( ! ntupleDescription ) return false;
248 
249  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
250  ntupleBinding->add_column(columnName, value);
251 
252 #ifdef G4VERBOSE
253  if ( fState.GetVerboseL2() ) {
254  G4ExceptionDescription description;
255  description << " ntupleId " << ntupleId << " " << columnName;
256  fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
257  }
258 #endif
259 
260  return true;
261 }
262 
263 //_____________________________________________________________________________
265  const G4String& columnName,
266  G4double& value)
267 {
268 // Add protection if ntuple is initialized
269 
270 #ifdef G4VERBOSE
271  if ( fState.GetVerboseL4() ) {
272  G4ExceptionDescription description;
273  description << " ntupleId " << ntupleId << " " << columnName;
274  fState.GetVerboseL4()->Message("set", "ntuple D column", description);
275  }
276 #endif
277 
278  G4XmlRNtupleDescription* ntupleDescription
279  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
280  if ( ! ntupleDescription ) return false;
281 
282  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
283  ntupleBinding->add_column(columnName, value);
284 
285 #ifdef G4VERBOSE
286  if ( fState.GetVerboseL2() ) {
287  G4ExceptionDescription description;
288  description << " ntupleId " << ntupleId << " " << columnName;
289  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
290  }
291 #endif
292 
293  return true;
294 }
295 
296 //_____________________________________________________________________________
298  const G4String& columnName,
299  G4String& value)
300 {
301 // Add protection if ntuple is initialized
302 
303 #ifdef G4VERBOSE
304  if ( fState.GetVerboseL4() ) {
305  G4ExceptionDescription description;
306  description << " ntupleId " << ntupleId << " " << columnName;
307  fState.GetVerboseL4()->Message("set", "ntuple S column", description);
308  }
309 #endif
310 
311  G4XmlRNtupleDescription* ntupleDescription
312  = GetNtupleInFunction(ntupleId, "SetNtupleSColumn");
313  if ( ! ntupleDescription ) return false;
314 
315  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
316  ntupleBinding->add_column(columnName, value);
317 
318 #ifdef G4VERBOSE
319  if ( fState.GetVerboseL2() ) {
320  G4ExceptionDescription description;
321  description << " ntupleId " << ntupleId << " " << columnName;
322  fState.GetVerboseL2()->Message("set", "ntuple S colum", description, true);
323  }
324 #endif
325 
326  return true;
327 }
328 
329 //_____________________________________________________________________________
331  const G4String& columnName,
332  std::vector<G4int>& vector)
333 {
334 // Add protection if ntuple is initialized
335 
336 #ifdef G4VERBOSE
337  if ( fState.GetVerboseL4() ) {
338  G4ExceptionDescription description;
339  description << " ntupleId " << ntupleId << " " << columnName;
340  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
341  }
342 #endif
343 
344  G4XmlRNtupleDescription* ntupleDescription
345  = GetNtupleInFunction(ntupleId, "SetNtupleIColumn");
346  if ( ! ntupleDescription ) return false;
347 
348  // not supported
349  //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
350  //ntupleBinding->add_column(columnName, vector);
351 
352  tools::aida::ntuple* subNtuple = new tools::aida::ntuple(G4cout, columnName);
353  ntupleDescription->fIVectorBindingMap[subNtuple] = &vector;
354  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
355  ntupleBinding->add_column(columnName, *subNtuple);
356 
357 #ifdef G4VERBOSE
358  if ( fState.GetVerboseL2() ) {
359  G4ExceptionDescription description;
360  description << " ntupleId " << ntupleId << " " << columnName;
361  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
362  }
363 #endif
364 
365  return true;
366 }
367 
368 //_____________________________________________________________________________
370  const G4String& columnName,
371  std::vector<G4float>& vector)
372 {
373 // Add protection if ntuple is initialized
374 
375 #ifdef G4VERBOSE
376  if ( fState.GetVerboseL4() ) {
377  G4ExceptionDescription description;
378  description << " ntupleId " << ntupleId << " " << columnName;
379  fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description);
380  }
381 #endif
382 
383  G4XmlRNtupleDescription* ntupleDescription
384  = GetNtupleInFunction(ntupleId, "SetNtupleFColumn");
385  if ( ! ntupleDescription ) return false;
386 
387  // not supported
388  //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
389  //ntupleBinding->add_column(columnName, vector);
390 
391  tools::aida::ntuple* subNtuple = new tools::aida::ntuple(G4cout, columnName);
392  ntupleDescription->fFVectorBindingMap[subNtuple] = &vector;
393  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
394  ntupleBinding->add_column(columnName, *subNtuple);
395 
396 #ifdef G4VERBOSE
397  if ( fState.GetVerboseL2() ) {
398  G4ExceptionDescription description;
399  description << " ntupleId " << ntupleId << " " << columnName;
400  fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
401  }
402 #endif
403 
404  return true;
405 }
406 
407 //_____________________________________________________________________________
409  const G4String& columnName,
410  std::vector<G4double>& vector)
411 {
412 // Add protection if ntuple is initialized
413 
414 #ifdef G4VERBOSE
415  if ( fState.GetVerboseL4() ) {
416  G4ExceptionDescription description;
417  description << " ntupleId " << ntupleId << " " << columnName;
418  fState.GetVerboseL4()->Message("set", "ntuple D column of vector", description);
419  }
420 #endif
421 
422  G4XmlRNtupleDescription* ntupleDescription
423  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
424  if ( ! ntupleDescription ) return false;
425 
426  // not supported
427  //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
428  //ntupleBinding->add_column(columnName, vector);
429 
430  tools::aida::ntuple* subNtuple = new tools::aida::ntuple(G4cout, columnName);
431  ntupleDescription->fDVectorBindingMap[subNtuple] = &vector;
432  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
433  ntupleBinding->add_column(columnName, *subNtuple);
434 
435 #ifdef G4VERBOSE
436  if ( fState.GetVerboseL2() ) {
437  G4ExceptionDescription description;
438  description << " ntupleId " << ntupleId << " " << columnName;
439  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
440  }
441 #endif
442 
443  return true;
444 }
445 
446 //_____________________________________________________________________________
448 {
449  return GetNtupleRow(fFirstId);
450 }
451 
452 //_____________________________________________________________________________
454 {
455 #ifdef G4VERBOSE
456  if ( fState.GetVerboseL4() ) {
457  G4ExceptionDescription description;
458  description << " ntupleId " << ntupleId;
459  fState.GetVerboseL4()->Message("get", "ntuple row", description);
460  }
461 #endif
462 
463  G4XmlRNtupleDescription* ntupleDescription
464  = GetNtupleInFunction(ntupleId, "GetNtupleRow");
465  if ( ! ntupleDescription ) return false;
466 
467  tools::aida::ntuple* ntuple = ntupleDescription->fNtuple;
468 
469  G4bool isInitialized = ntupleDescription->fIsInitialized;
470  if ( ! isInitialized ) {
471  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
472  if ( ! ntuple->set_binding(std::cout, *ntupleBinding) ) {
473  G4ExceptionDescription description;
474  description
475  << " "
476  << "Ntuple initialization failed !!";
477  G4Exception("G4XmlRNtuple::GetNtupleRow()",
478  "Analysis_WR021", JustWarning, description);
479  return false;
480  }
481  ntupleDescription->fIsInitialized = true;
482  ntuple->start();
483  }
484 
485  G4bool next = ntuple->next();
486  if ( next ) {
487  if ( ! ntuple->get_row() ) {
488  G4ExceptionDescription description;
489  description
490  << " "
491  << "Ntuple get_row() failed !!";
492  G4Exception("G4XmlRNtuple::GetNtupleRow()",
493  "Analysis_WR021", JustWarning, description);
494  return false;
495  }
496 
497  // fill vector from sub ntuples
498 
499  {std::map<tools::aida::ntuple*, std::vector<int>* >::iterator it;
500  for ( it = ntupleDescription->fIVectorBindingMap.begin();
501  it != ntupleDescription->fIVectorBindingMap.end(); it++) {
502  tools::aida::to_vector<int>(*(it->first), *(it->second));
503  }}
504  {std::map<tools::aida::ntuple*, std::vector<float>* >::iterator it;
505  for ( it = ntupleDescription->fFVectorBindingMap.begin();
506  it != ntupleDescription->fFVectorBindingMap.end(); it++) {
507  tools::aida::to_vector<float>(*(it->first), *(it->second));
508  }}
509  {std::map<tools::aida::ntuple*, std::vector<double>* >::iterator it;
510  for ( it = ntupleDescription->fDVectorBindingMap.begin();
511  it != ntupleDescription->fDVectorBindingMap.end(); it++) {
512  tools::aida::to_vector<double>(*(it->first), *(it->second));
513  }}
514  }
515 
516 #ifdef G4VERBOSE
517  if ( fState.GetVerboseL2() ) {
518  G4ExceptionDescription description;
519  description << " ntupleId " << ntupleId;
520  fState.GetVerboseL2()->Message("get", "ntuple row", description, true);
521  }
522 #endif
523 
524  return next;
525 }
std::map< tools::aida::ntuple *, std::vector< double > * > fDVectorBindingMap
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
bool to_vector(base_ntu &a_ntu, std::vector< T > &a_vec)
tools::ntuple_binding * fNtupleBinding
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
virtual G4bool SetNtupleDColumn(const G4String &columnName, G4double &value)
float G4float
Definition: G4Types.hh:77
int G4int
Definition: G4Types.hh:78
std::map< tools::aida::ntuple *, std::vector< int > * > fIVectorBindingMap
const G4AnalysisVerbose * GetVerboseL2() const
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL4() const
virtual G4bool SetNtupleIColumn(const G4String &columnName, G4int &value)
bool G4bool
Definition: G4Types.hh:79
virtual G4bool SetNtupleSColumn(const G4String &columnName, G4String &value)
tools::aida::ntuple * GetNtuple() const
virtual G4bool SetNtupleFColumn(const G4String &columnName, G4float &value)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4XmlRNtupleManager(const G4AnalysisManagerState &state)
tools::aida::ntuple * fNtuple
std::vector< G4XmlRNtupleDescription * > fNtupleVector
G4XmlRNtupleDescription * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
std::map< tools::aida::ntuple *, std::vector< float > * > fFVectorBindingMap
double G4double
Definition: G4Types.hh:76
G4int SetNtuple(G4XmlRNtupleDescription *rntupleDescription)
virtual G4bool GetNtupleRow()
G4bool isInitialized()
Check if the generator is initialized.
const G4AnalysisManagerState & fState