Geant4  10.01
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 //_____________________________________________________________________________
36  : G4VRNtupleManager(state),
37  fNtupleVector()
38 {
39 }
40 
41 //_____________________________________________________________________________
43 {
44  std::vector<G4XmlRNtupleDescription*>::iterator it;
45  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
46  delete (*it);
47  }
48 }
49 
50 //
51 // private methods
52 //
53 
54 //_____________________________________________________________________________
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 = "G4XmlRNtupleManager::";
62  inFunction += functionName;
63  G4ExceptionDescription description;
64  description << " " << "ntuple " << id << " does not exist.";
65  G4Exception(inFunction, "Analysis_WR011", JustWarning, description);
66  }
67  return 0;
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<G4XmlRNtupleDescription*>::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::aida::ntuple* G4XmlRNtupleManager::GetNtuple() const
100 {
101  return GetNtuple(fFirstId);
102 }
103 
104 //_____________________________________________________________________________
105 tools::aida::ntuple* G4XmlRNtupleManager::GetNtuple(G4int ntupleId) const
106 {
107  G4XmlRNtupleDescription* rntupleDescription
108  = GetNtupleInFunction(ntupleId, "GetRNtuple");
109 
110  if ( ! rntupleDescription ) return 0;
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  G4XmlRNtupleDescription* 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  G4XmlRNtupleDescription* 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 // Add protection if ntuple is initialized
242 
243 #ifdef G4VERBOSE
244  if ( fState.GetVerboseL4() ) {
245  G4ExceptionDescription description;
246  description << " ntupleId " << ntupleId << " " << columnName;
247  fState.GetVerboseL4()->Message("set", "ntuple D column", description);
248  }
249 #endif
250 
251  G4XmlRNtupleDescription* ntupleDescription
252  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
253  if ( ! ntupleDescription ) return false;
254 
255  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
256  ntupleBinding->add_column(columnName, value);
257 
258 #ifdef G4VERBOSE
259  if ( fState.GetVerboseL2() ) {
260  G4ExceptionDescription description;
261  description << " ntupleId " << ntupleId << " " << columnName;
262  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
263  }
264 #endif
265 
266  return true;
267 }
268 
269 //_____________________________________________________________________________
271  const G4String& columnName,
272  G4String& value)
273 {
274 // Add protection if ntuple is initialized
275 
276 #ifdef G4VERBOSE
277  if ( fState.GetVerboseL4() ) {
278  G4ExceptionDescription description;
279  description << " ntupleId " << ntupleId << " " << columnName;
280  fState.GetVerboseL4()->Message("set", "ntuple S column", description);
281  }
282 #endif
283 
284  G4XmlRNtupleDescription* ntupleDescription
285  = GetNtupleInFunction(ntupleId, "SetNtupleSColumn");
286  if ( ! ntupleDescription ) return false;
287 
288  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
289  ntupleBinding->add_column(columnName, value);
290 
291 #ifdef G4VERBOSE
292  if ( fState.GetVerboseL2() ) {
293  G4ExceptionDescription description;
294  description << " ntupleId " << ntupleId << " " << columnName;
295  fState.GetVerboseL2()->Message("set", "ntuple S colum", description, true);
296  }
297 #endif
298 
299  return true;
300 }
301 
302 //_____________________________________________________________________________
304  const G4String& columnName,
305  std::vector<G4int>& vector)
306 {
307 // Add protection if ntuple is initialized
308 
309 #ifdef G4VERBOSE
310  if ( fState.GetVerboseL4() ) {
311  G4ExceptionDescription description;
312  description << " ntupleId " << ntupleId << " " << columnName;
313  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
314  }
315 #endif
316 
317  G4XmlRNtupleDescription* ntupleDescription
318  = GetNtupleInFunction(ntupleId, "SetNtupleIColumn");
319  if ( ! ntupleDescription ) return false;
320 
321  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
322  ntupleBinding->add_column(columnName, vector);
323 
324 #ifdef G4VERBOSE
325  if ( fState.GetVerboseL2() ) {
326  G4ExceptionDescription description;
327  description << " ntupleId " << ntupleId << " " << columnName;
328  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
329  }
330 #endif
331 
332  return true;
333 }
334 
335 //_____________________________________________________________________________
337  const G4String& columnName,
338  std::vector<G4float>& vector)
339 {
340 // Add protection if ntuple is initialized
341 
342 #ifdef G4VERBOSE
343  if ( fState.GetVerboseL4() ) {
344  G4ExceptionDescription description;
345  description << " ntupleId " << ntupleId << " " << columnName;
346  fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description);
347  }
348 #endif
349 
350  G4XmlRNtupleDescription* ntupleDescription
351  = GetNtupleInFunction(ntupleId, "SetNtupleFColumn");
352  if ( ! ntupleDescription ) return false;
353 
354  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
355  ntupleBinding->add_column(columnName, vector);
356 
357 #ifdef G4VERBOSE
358  if ( fState.GetVerboseL2() ) {
359  G4ExceptionDescription description;
360  description << " ntupleId " << ntupleId << " " << columnName;
361  fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
362  }
363 #endif
364 
365  return true;
366 }
367 
368 //_____________________________________________________________________________
370  const G4String& columnName,
371  std::vector<G4double>& 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 D column of vector", description);
380  }
381 #endif
382 
383  G4XmlRNtupleDescription* ntupleDescription
384  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
385  if ( ! ntupleDescription ) return false;
386 
387  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
388  ntupleBinding->add_column(columnName, vector);
389 
390 #ifdef G4VERBOSE
391  if ( fState.GetVerboseL2() ) {
392  G4ExceptionDescription description;
393  description << " ntupleId " << ntupleId << " " << columnName;
394  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
395  }
396 #endif
397 
398  return true;
399 }
400 
401 //_____________________________________________________________________________
403 {
404  return GetNtupleRow(fFirstId);
405 }
406 
407 //_____________________________________________________________________________
409 {
410 #ifdef G4VERBOSE
411  if ( fState.GetVerboseL4() ) {
412  G4ExceptionDescription description;
413  description << " ntupleId " << ntupleId;
414  fState.GetVerboseL4()->Message("get", "ntuple row", description);
415  }
416 #endif
417 
418  G4XmlRNtupleDescription* ntupleDescription
419  = GetNtupleInFunction(ntupleId, "GetNtupleRow");
420  if ( ! ntupleDescription ) return false;
421 
422  tools::aida::ntuple* ntuple = ntupleDescription->fNtuple;
423 
424  G4bool isInitialized = ntupleDescription->fIsInitialized;
425  if ( ! isInitialized ) {
426  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
427  if ( ! ntuple->set_binding(std::cout, *ntupleBinding) ) {
428  G4ExceptionDescription description;
429  description
430  << " "
431  << "Ntuple initialization failed !!";
432  G4Exception("G4XmlRNtuple::GetNtupleRow()",
433  "Analysis_WR021", JustWarning, description);
434  return false;
435  }
436  ntupleDescription->fIsInitialized = true;
437  ntuple->start();
438  }
439 
440  G4bool next = ntuple->next();
441  if ( next ) {
442  if ( ! ntuple->get_row() ) {
443  G4ExceptionDescription description;
444  description
445  << " "
446  << "Ntuple get_row() failed !!";
447  G4Exception("G4XmlRNtuple::GetNtupleRow()",
448  "Analysis_WR021", JustWarning, description);
449  return false;
450  }
451  }
452 
453 #ifdef G4VERBOSE
454  if ( fState.GetVerboseL2() ) {
455  G4ExceptionDescription description;
456  description << " ntupleId " << ntupleId;
457  fState.GetVerboseL2()->Message("get", "ntuple row", description, true);
458  }
459 #endif
460 
461  return next;
462 }
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
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
const G4AnalysisVerbose * GetVerboseL2() const
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
double G4double
Definition: G4Types.hh:76
G4int SetNtuple(G4XmlRNtupleDescription *rntupleDescription)
virtual G4bool GetNtupleRow()
G4bool isInitialized()
Check if the generator is initialized.
const G4AnalysisManagerState & fState