Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootRNtupleManager.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: G4RootRNtupleManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 09/04/2014 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootRNtupleManager.hh"
33 
34 //_____________________________________________________________________________
36  : G4VRNtupleManager(state),
37  fNtupleVector()
38 {
39 }
40 
41 //_____________________________________________________________________________
43 {
44  std::vector<G4RootRNtupleDescription*>::iterator it;
45  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
46  delete (*it);
47  }
48 }
49 
50 //
51 // private methods
52 //
53 
54 //_____________________________________________________________________________
55 G4RootRNtupleDescription* G4RootRNtupleManager::GetNtupleInFunction(G4int id,
56  G4String functionName, G4bool warn) const
57 {
58  G4int index = id - fFirstId;
59  if ( index < 0 || index >= G4int(fNtupleVector.size()) ) {
60  if ( warn) {
61  G4String inFunction = "G4RootRNtupleManager::";
62  inFunction += functionName;
63  G4ExceptionDescription description;
64  description << " " << "ntuple " << id << " does not exist.";
65  G4Exception(inFunction, "Analysis_WR011", JustWarning, description);
66  }
67  return nullptr;
68  }
69 
70  return fNtupleVector[index];
71 }
72 
73 //
74 // protected methods
75 //
76 
77 //_____________________________________________________________________________
79 {
80  return ! fNtupleVector.size();
81 }
82 
83 //_____________________________________________________________________________
85 {
86 // Reset ntuples
87 
88  std::vector<G4RootRNtupleDescription*>::iterator it;
89  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
90  // ntuple is deleted automatically when file is closed
91  // delete (*it)->fNtuple;
92  (*it)->fNtuple=0;
93  }
94 
95  return true;
96 }
97 
98 //_____________________________________________________________________________
99 tools::rroot::ntuple* G4RootRNtupleManager::GetNtuple() const
100 {
101  return GetNtuple(fFirstId);
102 }
103 
104 //_____________________________________________________________________________
105 tools::rroot::ntuple* G4RootRNtupleManager::GetNtuple(G4int ntupleId) const
106 {
107  G4RootRNtupleDescription* rntupleDescription
108  = GetNtupleInFunction(ntupleId, "GetRNtuple");
109 
110  if ( ! rntupleDescription ) return nullptr;
111 
112  return rntupleDescription->fNtuple;
113 }
114 
115 //_____________________________________________________________________________
117 {
118  G4int id = fNtupleVector.size() + fFirstId;
119 
120  fNtupleVector.push_back(rntupleDescription);
121 
122  return id;
123 }
124 
125 //_____________________________________________________________________________
127  G4int& value)
128 {
129  return SetNtupleIColumn(fFirstId, columnName, value);
130 }
131 
132 //_____________________________________________________________________________
134  G4float& value)
135 {
136  return SetNtupleFColumn(fFirstId, columnName, value);
137 }
138 
139 //_____________________________________________________________________________
141  G4double& value)
142 {
143  return SetNtupleDColumn(fFirstId, columnName, value);
144 }
145 
146 //_____________________________________________________________________________
148  G4String& value)
149 {
150  return SetNtupleSColumn(fFirstId, columnName, value);
151 }
152 
153 //_____________________________________________________________________________
155  std::vector<G4int>& vector)
156 {
157  return SetNtupleIColumn(fFirstId, columnName, vector);
158 }
159 
160 //_____________________________________________________________________________
162  std::vector<G4float>& vector)
163 {
164  return SetNtupleFColumn(fFirstId, columnName, vector);
165 }
166 
167 //_____________________________________________________________________________
169  std::vector<G4double>& vector)
170 {
171  return SetNtupleDColumn(fFirstId, columnName, vector);
172 }
173 
174 //_____________________________________________________________________________
176  const G4String& columnName,
177  G4int& value)
178 {
179 #ifdef G4VERBOSE
180  if ( fState.GetVerboseL4() ) {
181  G4ExceptionDescription description;
182  description << " ntupleId " << ntupleId << " " << columnName;
183  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
184  }
185 #endif
186 
187  G4RootRNtupleDescription* ntupleDescription
188  = GetNtupleInFunction(ntupleId, "SetNtupleIColumn");
189  if ( ! ntupleDescription ) return false;
190 
191  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
192  ntupleBinding->add_column(columnName, value);
193 
194 #ifdef G4VERBOSE
195  if ( fState.GetVerboseL2() ) {
196  G4ExceptionDescription description;
197  description << " ntupleId " << ntupleId << " " << columnName;
198  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
199  }
200 #endif
201 
202  return true;
203 }
204 
205 //_____________________________________________________________________________
207  const G4String& columnName,
208  G4float& value)
209 {
210 #ifdef G4VERBOSE
211  if ( fState.GetVerboseL4() ) {
212  G4ExceptionDescription description;
213  description << " ntupleId " << ntupleId << " " << columnName;
214  fState.GetVerboseL4()->Message("set", "ntuple F column", description);
215  }
216 #endif
217 
218  G4RootRNtupleDescription* ntupleDescription
219  = GetNtupleInFunction(ntupleId, "SetNtupleFColumn");
220  if ( ! ntupleDescription ) return false;
221 
222  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
223  ntupleBinding->add_column(columnName, value);
224 
225 #ifdef G4VERBOSE
226  if ( fState.GetVerboseL2() ) {
227  G4ExceptionDescription description;
228  description << " ntupleId " << ntupleId << " " << columnName;
229  fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
230  }
231 #endif
232 
233  return true;
234 }
235 
236 //_____________________________________________________________________________
238  const G4String& columnName,
239  G4double& value)
240 {
241 #ifdef G4VERBOSE
242  if ( fState.GetVerboseL4() ) {
243  G4ExceptionDescription description;
244  description << " ntupleId " << ntupleId << " " << columnName;
245  fState.GetVerboseL4()->Message("set", "ntuple D column", description);
246  }
247 #endif
248 
249  G4RootRNtupleDescription* ntupleDescription
250  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
251  if ( ! ntupleDescription ) return false;
252 
253  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
254  ntupleBinding->add_column(columnName, value);
255 
256 #ifdef G4VERBOSE
257  if ( fState.GetVerboseL2() ) {
258  G4ExceptionDescription description;
259  description << " ntupleId " << ntupleId << " " << columnName;
260  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
261  }
262 #endif
263 
264  return true;
265 }
266 
267 //_____________________________________________________________________________
269  const G4String& columnName,
270  G4String& value)
271 {
272 #ifdef G4VERBOSE
273  if ( fState.GetVerboseL4() ) {
274  G4ExceptionDescription description;
275  description << " ntupleId " << ntupleId << " " << columnName;
276  fState.GetVerboseL4()->Message("set", "ntuple S column", description);
277  }
278 #endif
279 
280  G4RootRNtupleDescription* ntupleDescription
281  = GetNtupleInFunction(ntupleId, "SetNtupleSColumn");
282  if ( ! ntupleDescription ) return false;
283 
284  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
285  ntupleBinding->add_column(columnName, value);
286 
287 #ifdef G4VERBOSE
288  if ( fState.GetVerboseL2() ) {
289  G4ExceptionDescription description;
290  description << " ntupleId " << ntupleId << " " << columnName;
291  fState.GetVerboseL2()->Message("set", "ntuple S colum", description, true);
292  }
293 #endif
294 
295  return true;
296 }
297 
298 //_____________________________________________________________________________
300  const G4String& columnName,
301  std::vector<G4int>& vector)
302 {
303 #ifdef G4VERBOSE
304  if ( fState.GetVerboseL4() ) {
305  G4ExceptionDescription description;
306  description << " ntupleId " << ntupleId << " " << columnName;
307  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
308  }
309 #endif
310 
311  G4RootRNtupleDescription* ntupleDescription
312  = GetNtupleInFunction(ntupleId, "SetNtupleIColumn");
313  if ( ! ntupleDescription ) return false;
314 
315  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
316  ntupleBinding->add_column(columnName, vector);
317 
318 #ifdef G4VERBOSE
319  if ( fState.GetVerboseL2() ) {
320  G4ExceptionDescription description;
321  description << " ntupleId " << ntupleId << " " << columnName;
322  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
323  }
324 #endif
325 
326  return true;
327 }
328 
329 //_____________________________________________________________________________
331  const G4String& columnName,
332  std::vector<G4float>& vector)
333 {
334 #ifdef G4VERBOSE
335  if ( fState.GetVerboseL4() ) {
336  G4ExceptionDescription description;
337  description << " ntupleId " << ntupleId << " " << columnName;
338  fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description);
339  }
340 #endif
341 
342  G4RootRNtupleDescription* ntupleDescription
343  = GetNtupleInFunction(ntupleId, "SetNtupleFColumn");
344  if ( ! ntupleDescription ) return false;
345 
346  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
347  ntupleBinding->add_column(columnName, vector);
348 
349 #ifdef G4VERBOSE
350  if ( fState.GetVerboseL2() ) {
351  G4ExceptionDescription description;
352  description << " ntupleId " << ntupleId << " " << columnName;
353  fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
354  }
355 #endif
356 
357  return true;
358 }
359 
360 //_____________________________________________________________________________
362  const G4String& columnName,
363  std::vector<G4double>& vector)
364 {
365 #ifdef G4VERBOSE
366  if ( fState.GetVerboseL4() ) {
367  G4ExceptionDescription description;
368  description << " ntupleId " << ntupleId << " " << columnName;
369  fState.GetVerboseL4()->Message("set", "ntuple D column of vector", description);
370  }
371 #endif
372 
373  G4RootRNtupleDescription* ntupleDescription
374  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
375  if ( ! ntupleDescription ) return false;
376 
377  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
378  ntupleBinding->add_column(columnName, vector);
379 
380 #ifdef G4VERBOSE
381  if ( fState.GetVerboseL2() ) {
382  G4ExceptionDescription description;
383  description << " ntupleId " << ntupleId << " " << columnName;
384  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
385  }
386 #endif
387 
388  return true;
389 }
390 
391 //_____________________________________________________________________________
393 {
394  return GetNtupleRow(fFirstId);
395 }
396 
397 //_____________________________________________________________________________
399 {
400 #ifdef G4VERBOSE
401  if ( fState.GetVerboseL4() ) {
402  G4ExceptionDescription description;
403  description << " ntupleId " << ntupleId;
404  fState.GetVerboseL4()->Message("get", "ntuple row", description);
405  }
406 #endif
407 
408  G4RootRNtupleDescription* ntupleDescription
409  = GetNtupleInFunction(ntupleId, "GetNtupleRow");
410  if ( ! ntupleDescription ) return false;
411 
412  tools::rroot::ntuple* ntuple = ntupleDescription->fNtuple;
413  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
414 
415  G4bool isInitialized = ntupleDescription->fIsInitialized;
416  if ( ! isInitialized ) {
417 
418 #ifdef G4VERBOSE
419  if ( fState.GetVerboseL4() ) {
420  G4ExceptionDescription description;
421  description << " ntupleId " << ntupleId;
422  fState.GetVerboseL4()->Message("initialize", "ntuple", description);
423  }
424 #endif
425  if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) {
426  G4ExceptionDescription description;
427  description
428  << " "
429  << "Ntuple initialization failed !!";
430  G4Exception("G4RootRNtuple::GetNtupleRow()",
431  "Analysis_WR021", JustWarning, description);
432  return false;
433  }
434  ntupleDescription->fIsInitialized = true;
435  ntuple->start();
436  }
437 
438  G4bool next = ntuple->next();
439  if ( next ) {
440  if ( ! ntuple->get_row() ) {
441  G4ExceptionDescription description;
442  description
443  << " "
444  << "Ntuple get_row() failed !!";
445  G4Exception("G4RootRNtuple::GetNtupleRow()",
446  "Analysis_WR021", JustWarning, description);
447  return false;
448  }
449  }
450 
451 #ifdef G4VERBOSE
452  if ( fState.GetVerboseL2() ) {
453  G4ExceptionDescription description;
454  description << " ntupleId " << ntupleId;
455  fState.GetVerboseL2()->Message("get", "ntuple row", description, true);
456  }
457 #endif
458 
459  return next;
460 }
tools::rroot::ntuple * GetNtuple() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool SetNtupleIColumn(const G4String &columnName, G4int &value)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4RootRNtupleManager(const G4AnalysisManagerState &state)
virtual G4bool SetNtupleSColumn(const G4String &columnName, G4String &value)
float G4float
Definition: G4Types.hh:77
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL2() const
G4int SetNtuple(G4RootRNtupleDescription *rntupleDescription)
G4GLOB_DLL std::ostream G4cout
const XML_Char int const XML_Char * value
Definition: expat.h:331
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual G4bool SetNtupleDColumn(const G4String &columnName, G4double &value)
virtual G4bool GetNtupleRow()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
tools::rroot::ntuple * fNtuple
tools::ntuple_binding * fNtupleBinding
double G4double
Definition: G4Types.hh:76
G4bool isInitialized()
const G4AnalysisManagerState & fState
virtual G4bool SetNtupleFColumn(const G4String &columnName, G4float &value)