Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4CsvRNtupleManager.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 "G4CsvRNtupleManager.hh"
33 
34 //_____________________________________________________________________________
36  : G4VRNtupleManager(state),
37  fNtupleVector()
38 {
39 }
40 
41 //_____________________________________________________________________________
43 {
44  std::vector<G4CsvRNtupleDescription*>::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 G4CsvRNtupleDescription* G4CsvRNtupleManager::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 = "G4CsvRNtupleManager::";
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<G4CsvRNtupleDescription*>::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::rcsv::ntuple* G4CsvRNtupleManager::GetNtuple() const
100 {
101  return GetNtuple(fFirstId);
102 }
103 
104 //_____________________________________________________________________________
105 tools::rcsv::ntuple* G4CsvRNtupleManager::GetNtuple(G4int ntupleId) const
106 {
107  G4CsvRNtupleDescription* rntupleDescription
108  = GetNtupleInFunction(ntupleId, "GetNtuple");
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  std::vector<G4int>& vector)
149 {
150  return SetNtupleIColumn(fFirstId, columnName, vector);
151 }
152 
153 //_____________________________________________________________________________
155  std::vector<G4float>& vector)
156 {
157  return SetNtupleFColumn(fFirstId, columnName, vector);
158 }
159 
160 //_____________________________________________________________________________
162  std::vector<G4double>& vector)
163 {
164  return SetNtupleDColumn(fFirstId, columnName, vector);
165 }
166 
167 //_____________________________________________________________________________
169  G4String& value)
170 {
171  return SetNtupleSColumn(fFirstId, columnName, value);
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  G4CsvRNtupleDescription* 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  G4CsvRNtupleDescription* 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  G4CsvRNtupleDescription* 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  std::vector<G4int>& vector)
271 {
272 #ifdef G4VERBOSE
273  if ( fState.GetVerboseL4() ) {
274  G4ExceptionDescription description;
275  description << " ntupleId " << ntupleId << " " << columnName;
276  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
277  }
278 #endif
279 
280  G4CsvRNtupleDescription* ntupleDescription
281  = GetNtupleInFunction(ntupleId, "SetNtupleIColumn");
282  if ( ! ntupleDescription ) return false;
283 
284  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
285  ntupleBinding->add_column(columnName, vector);
286 
287 #ifdef G4VERBOSE
288  if ( fState.GetVerboseL2() ) {
289  G4ExceptionDescription description;
290  description << " ntupleId " << ntupleId << " " << columnName;
291  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
292  }
293 #endif
294 
295  return true;
296 }
297 
298 //_____________________________________________________________________________
300  const G4String& columnName,
301  std::vector<G4float>& vector)
302 {
303 #ifdef G4VERBOSE
304  if ( fState.GetVerboseL4() ) {
305  G4ExceptionDescription description;
306  description << " ntupleId " << ntupleId << " " << columnName;
307  fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description);
308  }
309 #endif
310 
311  G4CsvRNtupleDescription* ntupleDescription
312  = GetNtupleInFunction(ntupleId, "SetNtupleFColumn");
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 F colum", description, true);
323  }
324 #endif
325 
326  return true;
327 }
328 
329 //_____________________________________________________________________________
331  const G4String& columnName,
332  std::vector<G4double>& vector)
333 {
334 #ifdef G4VERBOSE
335  if ( fState.GetVerboseL4() ) {
336  G4ExceptionDescription description;
337  description << " ntupleId " << ntupleId << " " << columnName;
338  fState.GetVerboseL4()->Message("set", "ntuple D column of vector", description);
339  }
340 #endif
341 
342  G4CsvRNtupleDescription* ntupleDescription
343  = GetNtupleInFunction(ntupleId, "SetNtupleDColumn");
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 D colum", description, true);
354  }
355 #endif
356 
357  return true;
358 }
359 
360 //_____________________________________________________________________________
362  const G4String& columnName,
363  G4String& value)
364 {
365 #ifdef G4VERBOSE
366  if ( fState.GetVerboseL4() ) {
367  G4ExceptionDescription description;
368  description << " ntupleId " << ntupleId << " " << columnName;
369  fState.GetVerboseL4()->Message("set", "ntuple S column", description);
370  }
371 #endif
372 
373  G4CsvRNtupleDescription* ntupleDescription
374  = GetNtupleInFunction(ntupleId, "SetNtupleSColumn");
375  if ( ! ntupleDescription ) return false;
376 
377  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
378  ntupleBinding->add_column(columnName, value);
379 
380 #ifdef G4VERBOSE
381  if ( fState.GetVerboseL2() ) {
382  G4ExceptionDescription description;
383  description << " ntupleId " << ntupleId << " " << columnName;
384  fState.GetVerboseL2()->Message("set", "ntuple S colum", description, true);
385  }
386 #endif
387 
388  return true;
389 }
390 
391 //_____________________________________________________________________________
393 {
394  return GetNtupleRow(fFirstId);
395 }
396 
397 //_____________________________________________________________________________
399 {
400 
401 #ifdef G4VERBOSE
402  if ( fState.GetVerboseL4() ) {
403  G4ExceptionDescription description;
404  description << " ntupleId " << ntupleId;
405  fState.GetVerboseL4()->Message("get", "ntuple row", description);
406  }
407 #endif
408 
409  G4CsvRNtupleDescription* ntupleDescription
410  = GetNtupleInFunction(ntupleId, "GetNtupleRow");
411  if ( ! ntupleDescription ) return false;
412 
413  tools::rcsv::ntuple* ntuple = ntupleDescription->fNtuple;
414 
415  G4bool isInitialized = ntupleDescription->fIsInitialized;
416  if ( ! isInitialized ) {
417  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
418  if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) {
419  G4ExceptionDescription description;
420  description
421  << " "
422  << "Ntuple initialization failed !!";
423  G4Exception("G4CsvRNtuple::GetNtupleRow()",
424  "Analysis_WR021", JustWarning, description);
425  return false;
426  }
427  ntupleDescription->fIsInitialized = true;
428  ntuple->start();
429  }
430 
431  G4bool next = ntuple->next();
432  if ( next ) {
433  if ( ! ntuple->get_row() ) {
434  G4ExceptionDescription description;
435  description
436  << " "
437  << "Ntuple get_row() failed !!";
438  G4Exception("G4CsvRNtuple::GetNtupleRow()",
439  "Analysis_WR021", JustWarning, description);
440  return false;
441  }
442  }
443 
444 #ifdef G4VERBOSE
445  if ( fState.GetVerboseL2() ) {
446  G4ExceptionDescription description;
447  description << " ntupleId " << ntupleId;
448  fState.GetVerboseL2()->Message("get", "ntuple row", description, true);
449  }
450 #endif
451 
452  return next;
453 }
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4CsvRNtupleManager(const G4AnalysisManagerState &state)
tools::ntuple_binding * fNtupleBinding
virtual G4bool SetNtupleSColumn(const G4String &columnName, G4String &value)
float G4float
Definition: G4Types.hh:77
virtual G4bool SetNtupleFColumn(const G4String &columnName, G4float &value)
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL2() const
virtual G4bool GetNtupleRow()
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
tools::rcsv::ntuple * fNtuple
tools::rcsv::ntuple * GetNtuple() const
G4int SetNtuple(G4CsvRNtupleDescription *rntupleDescription)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual G4bool SetNtupleIColumn(const G4String &columnName, G4int &value)
virtual G4bool SetNtupleDColumn(const G4String &columnName, G4double &value)
double G4double
Definition: G4Types.hh:76
G4bool isInitialized()
const G4AnalysisManagerState & fState