Geant4  10.00.p01
G4CsvNtupleManager.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: G4CsvNtupleManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4CsvNtupleManager.hh"
32 #include "G4CsvFileManager.hh"
34 
35 #include <iostream>
36 
37 //_____________________________________________________________________________
39  : G4VNtupleManager(state),
40  fFileManager(0),
41  fNtupleVector()
42 {
43 }
44 
45 //_____________________________________________________________________________
47 {
48  std::vector<G4CsvNtupleDescription*>::iterator it;
49  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
50  delete (*it);
51  }
52 }
53 
54 //
55 // private methods
56 //
57 
58 //_____________________________________________________________________________
59 tools::wcsv::ntuple::column<int>*
61 {
62  G4CsvNtupleDescription* ntupleDecription
63  = GetNtupleInFunction(ntupleId, "GetNtupleIColumn");
64  if ( ! ntupleDecription ) {
65  // add exception
66  return 0;
67  }
68 
69  std::map<G4int, tools::wcsv::ntuple::column<int>* >& ntupleIColumnMap
70  = ntupleDecription->fNtupleIColumnMap;
71  std::map<G4int, tools::wcsv::ntuple::column<int>* >::const_iterator it
72  = ntupleIColumnMap.find(columnId);
73  if ( it == ntupleIColumnMap.end() ) {
74  G4ExceptionDescription description;
75  description << " " << "ntupleId " << ntupleId
76  << "column " << columnId << " does not exist.";
77  G4Exception("G4CsvNtupleManager::GetNtupleIColumn()",
78  "Analysis_W009", JustWarning, description);
79  return 0;
80  }
81 
82  return it->second;
83 }
84 
85 //_____________________________________________________________________________
86 tools::wcsv::ntuple::column<float>*
88 {
89  G4CsvNtupleDescription* ntupleDecription
90  = GetNtupleInFunction(ntupleId, "GetNtupleFColumn");
91  if ( ! ntupleDecription ) {
92  // add exception
93  return 0;
94  }
95 
96  std::map<G4int, tools::wcsv::ntuple::column<float>* >& ntupleFColumnMap
97  = ntupleDecription->fNtupleFColumnMap;
98  std::map<G4int, tools::wcsv::ntuple::column<float>* >::const_iterator it
99  = ntupleFColumnMap.find(columnId);
100  if ( it == ntupleFColumnMap.end() ) {
101  G4ExceptionDescription description;
102  description << " " << "ntupleId " << ntupleId
103  << "column " << columnId << " does not exist.";
104  G4Exception("G4CsvNtupleManager::GetNtupleFColumn()",
105  "Analysis_W009", JustWarning, description);
106  return 0;
107  }
108 
109  return it->second;
110 }
111 
112 
113 //_____________________________________________________________________________
114 tools::wcsv::ntuple::column<double>*
116 {
117  G4CsvNtupleDescription* ntupleDecription
118  = GetNtupleInFunction(ntupleId, "GetNtupleDColumn");
119  if ( ! ntupleDecription ) {
120  // add exception
121  return 0;
122  }
123 
124  std::map<G4int, tools::wcsv::ntuple::column<double>* >& ntupleDColumnMap
125  = ntupleDecription->fNtupleDColumnMap;
126  std::map<G4int, tools::wcsv::ntuple::column<double>* >::const_iterator it
127  = ntupleDColumnMap.find(columnId);
128  if ( it == ntupleDColumnMap.end() ) {
129  G4ExceptionDescription description;
130  description << " " << "ntupleId " << ntupleId
131  << "column " << columnId << " does not exist.";
132  G4Exception("G4CsvNtupleManager::GetNtupleDColumn()",
133  "Analysis_W009", JustWarning, description);
134  return 0;
135  }
136 
137  return it->second;
138 }
139 
140 //_____________________________________________________________________________
142  G4String functionName, G4bool warn,
143  G4bool /*onlyIfActive*/) const
144 {
145  G4int index = id - fFirstId;
146  if ( index < 0 || index >= G4int(fNtupleVector.size()) ) {
147  if ( warn) {
148  G4String inFunction = "G4CsvNtupleManager::";
149  inFunction += functionName;
150  G4ExceptionDescription description;
151  description << " " << "ntuple " << id << " does not exist.";
152  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
153  }
154  return 0;
155  }
156 
157  return fNtupleVector[index];
158 }
159 
160 //
161 // protected methods
162 //
163 
164 //_____________________________________________________________________________
166 {
167 // Create ntuple from ntuple_booking.
168 
169  // Do not create ntuples on master thread
170  if ( G4AnalysisManagerState::IsMT() && fState.GetIsMaster() ) return;
171 
172  std::vector<G4CsvNtupleDescription*>::iterator itn;
173  for (itn = fNtupleVector.begin(); itn != fNtupleVector.end(); itn++ ) {
174 
175  tools::ntuple_booking* ntupleBooking = (*itn)->fNtupleBooking;
176  if ( ! ntupleBooking ) continue;
177 
178 #ifdef G4VERBOSE
179  if ( fState.GetVerboseL4() )
181  ->Message("create from booking", "ntuple", ntupleBooking->m_name);
182 #endif
183 
184  // create a file for this ntuple
185  if ( ! fFileManager->CreateNtupleFile((*itn)) ) continue;
186 
187  // create ntuple
188  (*itn)->fNtuple
189  = new tools::wcsv::ntuple(*((*itn)->fFile), G4cerr, *ntupleBooking);
190 
191  if ( ntupleBooking->m_columns.size() ) {
192  // store ntuple columns in local maps
193  const std::vector<tools::ntuple_booking::col_t>& columns
194  = ntupleBooking->m_columns;
195  std::vector<tools::ntuple_booking::col_t>::const_iterator it;
196  G4int index = 0;
197  for ( it = columns.begin(); it!=columns.end(); ++it) {
198  if ( (*it).second == tools::_cid(int(0) ) ) {
199  (*itn)->fNtupleIColumnMap[index++]
200  = (*itn)->fNtuple->find_column<int>((*it).first);
201  }
202  else if ( (*it).second == tools::_cid(float(0) ) ) {
203  (*itn)->fNtupleFColumnMap[index++]
204  = (*itn)->fNtuple->find_column<float>((*it).first);
205  }
206  else if ( (*it).second== tools::_cid(double(0))) {
207  (*itn)->fNtupleDColumnMap[index++]
208  = (*itn)->fNtuple->find_column<double>((*it).first);
209  }
210  else {
211  G4ExceptionDescription description;
212  description << " "
213  << "Unsupported column type " << (*it).first;
214  G4Exception("G4CsvNtupleManager::CreateNtupleFromBooking()",
215  "Analysis_W004", JustWarning, description);
216  }
217  }
218  }
219 #ifdef G4VERBOSE
220  if ( fState.GetVerboseL3() )
222  ->Message("create from booking", "ntuple", ntupleBooking->m_name);
223 #endif
224  }
225 }
226 
227 //_____________________________________________________________________________
229 {
230  return ! fNtupleVector.size();
231 }
232 
233 //_____________________________________________________________________________
235 {
236  std::vector<G4CsvNtupleDescription*>::iterator it;
237  for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) {
238  delete (*it)->fNtuple;
239  (*it)->fNtuple = 0;
240  }
241 
242  return true;
243 }
244 
245 //_____________________________________________________________________________
246 tools::wcsv::ntuple* G4CsvNtupleManager::GetNtuple() const
247 {
248  return GetNtuple(fFirstId);
249 }
250 
251 //_____________________________________________________________________________
252 tools::wcsv::ntuple* G4CsvNtupleManager::GetNtuple(G4int ntupleId) const
253 {
254  G4CsvNtupleDescription* ntupleDescription
255  = GetNtupleInFunction(ntupleId, "GetNtuple");
256 
257  return ntupleDescription->fNtuple;
258 }
259 
260 //_____________________________________________________________________________
262  const G4String& title)
263 {
264 #ifdef G4VERBOSE
265  if ( fState.GetVerboseL4() )
266  fState.GetVerboseL4()->Message("create", "ntuple", name);
267 #endif
268 
269  // Create ntuple description
270  G4int index = fNtupleVector.size();
271  G4CsvNtupleDescription* ntupleDescription
272  = new G4CsvNtupleDescription();
273  fNtupleVector.push_back(ntupleDescription);
274 
275  // Create ntuple booking
276  ntupleDescription->fNtupleBooking = new tools::ntuple_booking();
277  ntupleDescription->fNtupleBooking->m_name = name;
278  ntupleDescription->fNtupleBooking->m_title = title;
279  // ntuple booking object is deleted in destructor
280 
281  // Create ntuple if the file is open (what means here that
282  // a filename was already set)
283  if ( fFileManager->GetFileName().size() ) {
284  if ( fFileManager->CreateNtupleFile(ntupleDescription) ) {
285  ntupleDescription->fNtuple
286  = new tools::wcsv::ntuple(*(ntupleDescription->fFile));
287  // ntuple object is deleted when closing a file
288  }
289  }
290 
291  fLockFirstId = true;
292 
293 #ifdef G4VERBOSE
294  if ( fState.GetVerboseL2() ) {
295  G4ExceptionDescription description;
296  description << name << " ntupleId " << index + fFirstId;
297  fState.GetVerboseL2()->Message("create", "ntuple", description);
298  }
299 #endif
300 
301  return index + fFirstId;
302 }
303 
304 //_____________________________________________________________________________
306 {
307  G4int ntupleId = fNtupleVector.size() + fFirstId - 1;
308  return CreateNtupleIColumn(ntupleId, name);
309 }
310 
311 //_____________________________________________________________________________
313 {
314  G4int ntupleId = fNtupleVector.size() + fFirstId - 1;
315  return CreateNtupleFColumn(ntupleId, name);
316 }
317 
318 //_____________________________________________________________________________
320 {
321  G4int ntupleId = fNtupleVector.size() + fFirstId - 1;
322  return CreateNtupleDColumn(ntupleId, name);
323 }
324 
325 //_____________________________________________________________________________
327 {
328  // nothing to be done here
329 }
330 
331 //_____________________________________________________________________________
333 {
334 #ifdef G4VERBOSE
335  if ( fState.GetVerboseL4() ) {
336  G4ExceptionDescription description;
337  description << name << " ntupleId " << ntupleId;
338  fState.GetVerboseL4()->Message("create", "ntuple I column", description);
339  }
340 #endif
341 
342  G4CsvNtupleDescription* ntupleDescription
343  = GetNtupleInFunction(ntupleId, "CreateNtupleIColumn");
344  tools::ntuple_booking* ntupleBooking
345  = ntupleDescription->fNtupleBooking;
346 
347  if ( ! ntupleBooking ) {
348  G4ExceptionDescription description;
349  description << " "
350  << "Ntuple " << ntupleId << " has to be created first. ";
351  G4Exception("G4CsvNtupleManager::CreateNtupleIColumn()",
352  "Analysis_W005", JustWarning, description);
353  return -1;
354  }
355 
356  // Save column info in booking
357  G4int index = ntupleBooking->m_columns.size();
358  ntupleBooking->add_column<int>(name);
359 
360  // Create column if ntuple already exists
361  if ( ntupleDescription->fNtuple ) {
362  tools::wcsv::ntuple::column<int>* column
363  = ntupleDescription->fNtuple->create_column<int>(name);
364  ntupleDescription->fNtupleIColumnMap[index] = column;
365  }
366 
368 
369 #ifdef G4VERBOSE
370  if ( fState.GetVerboseL2() ) {
371  G4ExceptionDescription description;
372  description << name << " ntupleId " << ntupleId;
373  fState.GetVerboseL2()->Message("create", "ntuple I column", description);
374  }
375 #endif
376 
377  return index + fFirstNtupleColumnId;
378 }
379 
380 //_____________________________________________________________________________
382 {
383 #ifdef G4VERBOSE
384  if ( fState.GetVerboseL4() ) {
385  G4ExceptionDescription description;
386  description << name << " ntupleId " << ntupleId;
387  fState.GetVerboseL4()->Message("create", "ntuple F column", description);
388  }
389 #endif
390 
391  G4CsvNtupleDescription* ntupleDescription
392  = GetNtupleInFunction(ntupleId, "CreateNtupleFColumn");
393  tools::ntuple_booking* ntupleBooking
394  = ntupleDescription->fNtupleBooking;
395 
396  if ( ! ntupleBooking ) {
397  G4ExceptionDescription description;
398  description << " "
399  << "Ntuple " << ntupleId << " has to be created first. ";
400  G4Exception("G4CsvNtupleManager::CreateNtupleFColumn()",
401  "Analysis_W005", JustWarning, description);
402  return -1;
403  }
404 
405  // Save column info in booking
406  G4int index = ntupleBooking->m_columns.size();
407  ntupleBooking->add_column<float>(name);
408 
409  // Create column if ntuple already exists
410  if ( ntupleDescription->fNtuple ) {
411  tools::wcsv::ntuple::column<float>* column
412  = ntupleDescription->fNtuple->create_column<float>(name);
413  ntupleDescription->fNtupleFColumnMap[index] = column;
414  }
415 
417 
418 #ifdef G4VERBOSE
419  if ( fState.GetVerboseL2() ) {
420  G4ExceptionDescription description;
421  description << name << " ntupleId " << ntupleId;
422  fState.GetVerboseL2()->Message("create", "ntuple F column", description);
423  }
424 #endif
425 
426  return index + fFirstNtupleColumnId;
427 }
428 
429 //_____________________________________________________________________________
431 {
432 #ifdef G4VERBOSE
433  if ( fState.GetVerboseL4() ) {
434  G4ExceptionDescription description;
435  description << name << " ntupleId " << ntupleId;
436  fState.GetVerboseL4()->Message("create", "ntuple D column", description);
437  }
438 #endif
439 
440  G4CsvNtupleDescription* ntupleDescription
441  = GetNtupleInFunction(ntupleId, "CreateNtupleDColumn");
442  tools::ntuple_booking* ntupleBooking
443  = ntupleDescription->fNtupleBooking;
444 
445  if ( ! ntupleBooking ) {
446  G4ExceptionDescription description;
447  description << " "
448  << "Ntuple " << ntupleId << " has to be created first. ";
449  G4Exception("G4CsvNtupleManager::CreateNtupleDColumn()",
450  "Analysis_W005", JustWarning, description);
451  return -1;
452  }
453 
454  // Save column info in booking
455  G4int index = ntupleBooking->m_columns.size();
456  ntupleBooking->add_column<double>(name);
457 
458  // Create column if ntuple already exists
459  if ( ntupleDescription->fNtuple ) {
460  tools::wcsv::ntuple::column<double>* column
461  = ntupleDescription->fNtuple->create_column<double>(name);
462  ntupleDescription->fNtupleDColumnMap[index] = column;
463  }
464 
466 
467 #ifdef G4VERBOSE
468  if ( fState.GetVerboseL2() ) {
469  G4ExceptionDescription description;
470  description << name << " ntupleId " << ntupleId;
471  fState.GetVerboseL2()->Message("create", "ntuple D column", description);
472  }
473 #endif
474 
475  return index + fFirstNtupleColumnId;
476 }
477 
478 //_____________________________________________________________________________
480 {
481  // nothing to be done here
482 }
483 
484 //_____________________________________________________________________________
486 {
487  return FillNtupleIColumn(fFirstId, columnId, value);
488 }
489 
490 //_____________________________________________________________________________
492 {
493  return FillNtupleFColumn(fFirstId, columnId, value);
494 }
495 
496 //_____________________________________________________________________________
498 {
499  return FillNtupleDColumn(fFirstId, columnId, value);
500 }
501 
502 //_____________________________________________________________________________
504 {
505  return AddNtupleRow(fFirstId);
506 }
507 
508 //_____________________________________________________________________________
510  G4int value)
511 {
512  tools::wcsv::ntuple::column<int>* column
513  = GetNtupleIColumn(ntupleId, columnId);
514  if ( ! column ) {
515  G4ExceptionDescription description;
516  description << " " << "ntupleId " << ntupleId
517  << "column " << columnId << " does not exist.";
518  G4Exception("G4CsvNtupleManager::FillNtupleIColumn()",
519  "Analysis_W009", JustWarning, description);
520  return false;
521  }
522 
523  column->fill(value);
524 #ifdef G4VERBOSE
525  if ( fState.GetVerboseL4() ) {
526  G4ExceptionDescription description;
527  description << " ntupleId " << ntupleId
528  << " columnId " << columnId << " value " << value;
529  fState.GetVerboseL4()->Message("fill", "ntuple I column", description);
530  }
531 #endif
532  return true;
533 }
534 //_____________________________________________________________________________
536  G4float value)
537 {
538  tools::wcsv::ntuple::column<float>* column
539  = GetNtupleFColumn(ntupleId, columnId);
540  if ( ! column ) {
541  G4ExceptionDescription description;
542  description << " " << "ntupleId " << ntupleId
543  << "column " << columnId << " does not exist.";
544  G4Exception("G4CsvNtupleManager::FillNtupleFColumn()",
545  "Analysis_W009", JustWarning, description);
546  return false;
547  }
548 
549  column->fill(value);
550 #ifdef G4VERBOSE
551  if ( fState.GetVerboseL4() ) {
552  G4ExceptionDescription description;
553  description << " ntupleId " << ntupleId
554  << " columnId " << columnId << " value " << value;
555  fState.GetVerboseL4()->Message("fill", "ntuple F column", description);
556  }
557 #endif
558  return true;
559 }
560 
561 //_____________________________________________________________________________
563  G4double value)
564 {
565  tools::wcsv::ntuple::column<double>* column
566  = GetNtupleDColumn(ntupleId, columnId);
567  if ( ! column ) {
568  G4ExceptionDescription description;
569  description << " " << "ntupleId " << ntupleId
570  << "column " << columnId << " does not exist.";
571  G4Exception("G4CsvNtupleManager::FillNtupleDColumn()",
572  "Analysis_W009", JustWarning, description);
573  return false;
574  }
575 
576  column->fill(value);
577 #ifdef G4VERBOSE
578  if ( fState.GetVerboseL4() ) {
579  G4ExceptionDescription description;
580  description << " ntupleId " << ntupleId
581  << " columnId " << columnId << " value " << value;
582  fState.GetVerboseL4()->Message("fill", "ntuple D column", description);
583  }
584 #endif
585  return true;
586 }
587 
588 //_____________________________________________________________________________
590 {
591 #ifdef G4VERBOSE
592  if ( fState.GetVerboseL4() ) {
593  G4ExceptionDescription description;
594  description << " ntupleId " << ntupleId;
595  fState.GetVerboseL4()->Message("add", "ntuple row", description);
596  }
597 #endif
598 
599  G4CsvNtupleDescription* ntupleDescription
600  = GetNtupleInFunction(ntupleId, "AddNtupleRow");
601 
602  if ( ! ntupleDescription || ! ntupleDescription->fNtuple ) {
603  G4ExceptionDescription description;
604  description << " " << "ntuple does not exist. ";
605  G4Exception("G4CsvNtupleManager::AddNtupleRow()",
606  "Analysis_W008", JustWarning, description);
607  return false;
608  }
609 
610  ntupleDescription->fNtuple->add_row();
611 #ifdef G4VERBOSE
612  if ( fState.GetVerboseL4() ) {
613  G4ExceptionDescription description;
614  description << " ntupleId " << ntupleId;
615  fState.GetVerboseL4()->Message("add", "ntuple row", description);
616  }
617 #endif
618 
619  return true;
620 }
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4CsvFileManager * fFileManager
G4String name
Definition: TRTMaterials.hh:40
virtual G4int CreateNtuple(const G4String &name, const G4String &title)
float G4float
Definition: G4Types.hh:77
G4CsvNtupleManager(const G4AnalysisManagerState &state)
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL2() const
tools::wcsv::ntuple * GetNtuple() const
virtual G4CsvNtupleDescription * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true, G4bool onlyIfActive=true) const
tools::wcsv::ntuple::column< float > * GetNtupleFColumn(G4int ntupleId, G4int columnId) const
G4CsvFileManager * fFileManager
const G4AnalysisVerbose * GetVerboseL3() const
virtual void FinishNtuple()
std::map< G4int, tools::wcsv::ntuple::column< float > * > fNtupleFColumnMap
const G4AnalysisVerbose * GetVerboseL4() const
G4String GetFileName() const
tools::wcsv::ntuple::column< double > * GetNtupleDColumn(G4int ntupleId, G4int columnId) const
bool G4bool
Definition: G4Types.hh:79
G4bool IsEmpty() const
virtual G4bool FillNtupleFColumn(G4int columnId, G4float value)
virtual G4int CreateNtupleFColumn(const G4String &name)
tools::ntuple_booking * fNtupleBooking
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual G4int CreateNtupleDColumn(const G4String &name)
G4bool CreateNtupleFile(G4CsvNtupleDescription *ntupleDescription)
tools::wcsv::ntuple * fNtuple
virtual G4bool FillNtupleIColumn(G4int columnId, G4int value)
std::map< G4int, tools::wcsv::ntuple::column< double > * > fNtupleDColumnMap
tools::wcsv::ntuple::column< int > * GetNtupleIColumn(G4int ntupleId, G4int columnId) const
G4bool fLockFirstNtupleColumnId
virtual G4bool FillNtupleDColumn(G4int columnId, G4double value)
std::map< G4int, tools::wcsv::ntuple::column< int > * > fNtupleIColumnMap
double G4double
Definition: G4Types.hh:76
std::vector< G4CsvNtupleDescription * > fNtupleVector
virtual G4int CreateNtupleIColumn(const G4String &name)
const G4AnalysisManagerState & fState
virtual G4bool AddNtupleRow()
G4GLOB_DLL std::ostream G4cerr