Geant4  10.00.p03
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  << " columnId " << 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  << " columnId " << 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  << " columnId " << 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  if ( ! ntupleDescription ) return -1;
345 
346  tools::ntuple_booking* ntupleBooking
347  = ntupleDescription->fNtupleBooking;
348 
349  if ( ! ntupleBooking ) {
350  G4ExceptionDescription description;
351  description << " "
352  << "Ntuple " << ntupleId << " has to be created first. ";
353  G4Exception("G4CsvNtupleManager::CreateNtupleIColumn()",
354  "Analysis_W005", JustWarning, description);
355  return -1;
356  }
357 
358  // Save column info in booking
359  G4int index = ntupleBooking->m_columns.size();
360  ntupleBooking->add_column<int>(name);
361 
362  // Create column if ntuple already exists
363  if ( ntupleDescription->fNtuple ) {
364  tools::wcsv::ntuple::column<int>* column
365  = ntupleDescription->fNtuple->create_column<int>(name);
366  ntupleDescription->fNtupleIColumnMap[index] = column;
367  }
368 
370 
371 #ifdef G4VERBOSE
372  if ( fState.GetVerboseL2() ) {
373  G4ExceptionDescription description;
374  description << name << " ntupleId " << ntupleId;
375  fState.GetVerboseL2()->Message("create", "ntuple I column", description);
376  }
377 #endif
378 
379  return index + fFirstNtupleColumnId;
380 }
381 
382 //_____________________________________________________________________________
384 {
385 #ifdef G4VERBOSE
386  if ( fState.GetVerboseL4() ) {
387  G4ExceptionDescription description;
388  description << name << " ntupleId " << ntupleId;
389  fState.GetVerboseL4()->Message("create", "ntuple F column", description);
390  }
391 #endif
392 
393  G4CsvNtupleDescription* ntupleDescription
394  = GetNtupleInFunction(ntupleId, "CreateNtupleFColumn");
395  if ( ! ntupleDescription ) return -1;
396 
397  tools::ntuple_booking* ntupleBooking
398  = ntupleDescription->fNtupleBooking;
399 
400  if ( ! ntupleBooking ) {
401  G4ExceptionDescription description;
402  description << " "
403  << "Ntuple " << ntupleId << " has to be created first. ";
404  G4Exception("G4CsvNtupleManager::CreateNtupleFColumn()",
405  "Analysis_W005", JustWarning, description);
406  return -1;
407  }
408 
409  // Save column info in booking
410  G4int index = ntupleBooking->m_columns.size();
411  ntupleBooking->add_column<float>(name);
412 
413  // Create column if ntuple already exists
414  if ( ntupleDescription->fNtuple ) {
415  tools::wcsv::ntuple::column<float>* column
416  = ntupleDescription->fNtuple->create_column<float>(name);
417  ntupleDescription->fNtupleFColumnMap[index] = column;
418  }
419 
421 
422 #ifdef G4VERBOSE
423  if ( fState.GetVerboseL2() ) {
424  G4ExceptionDescription description;
425  description << name << " ntupleId " << ntupleId;
426  fState.GetVerboseL2()->Message("create", "ntuple F column", description);
427  }
428 #endif
429 
430  return index + fFirstNtupleColumnId;
431 }
432 
433 //_____________________________________________________________________________
435 {
436 #ifdef G4VERBOSE
437  if ( fState.GetVerboseL4() ) {
438  G4ExceptionDescription description;
439  description << name << " ntupleId " << ntupleId;
440  fState.GetVerboseL4()->Message("create", "ntuple D column", description);
441  }
442 #endif
443 
444  G4CsvNtupleDescription* ntupleDescription
445  = GetNtupleInFunction(ntupleId, "CreateNtupleDColumn");
446  if ( ! ntupleDescription ) return -1;
447 
448  tools::ntuple_booking* ntupleBooking
449  = ntupleDescription->fNtupleBooking;
450 
451  if ( ! ntupleBooking ) {
452  G4ExceptionDescription description;
453  description << " "
454  << "Ntuple " << ntupleId << " has to be created first. ";
455  G4Exception("G4CsvNtupleManager::CreateNtupleDColumn()",
456  "Analysis_W005", JustWarning, description);
457  return -1;
458  }
459 
460  // Save column info in booking
461  G4int index = ntupleBooking->m_columns.size();
462  ntupleBooking->add_column<double>(name);
463 
464  // Create column if ntuple already exists
465  if ( ntupleDescription->fNtuple ) {
466  tools::wcsv::ntuple::column<double>* column
467  = ntupleDescription->fNtuple->create_column<double>(name);
468  ntupleDescription->fNtupleDColumnMap[index] = column;
469  }
470 
472 
473 #ifdef G4VERBOSE
474  if ( fState.GetVerboseL2() ) {
475  G4ExceptionDescription description;
476  description << name << " ntupleId " << ntupleId;
477  fState.GetVerboseL2()->Message("create", "ntuple D column", description);
478  }
479 #endif
480 
481  return index + fFirstNtupleColumnId;
482 }
483 
484 //_____________________________________________________________________________
486 {
487  // nothing to be done here
488 }
489 
490 //_____________________________________________________________________________
492 {
493  return FillNtupleIColumn(fFirstId, columnId, value);
494 }
495 
496 //_____________________________________________________________________________
498 {
499  return FillNtupleFColumn(fFirstId, columnId, value);
500 }
501 
502 //_____________________________________________________________________________
504 {
505  return FillNtupleDColumn(fFirstId, columnId, value);
506 }
507 
508 //_____________________________________________________________________________
510 {
511  return AddNtupleRow(fFirstId);
512 }
513 
514 //_____________________________________________________________________________
516  G4int value)
517 {
518  tools::wcsv::ntuple::column<int>* column
519  = GetNtupleIColumn(ntupleId, columnId);
520  if ( ! column ) {
521  G4ExceptionDescription description;
522  description << " " << "ntupleId " << ntupleId
523  << " columnId " << columnId << " does not exist.";
524  G4Exception("G4CsvNtupleManager::FillNtupleIColumn()",
525  "Analysis_W009", JustWarning, description);
526  return false;
527  }
528 
529  column->fill(value);
530 #ifdef G4VERBOSE
531  if ( fState.GetVerboseL4() ) {
532  G4ExceptionDescription description;
533  description << " ntupleId " << ntupleId
534  << " columnId " << columnId << " value " << value;
535  fState.GetVerboseL4()->Message("fill", "ntuple I column", description);
536  }
537 #endif
538  return true;
539 }
540 //_____________________________________________________________________________
542  G4float value)
543 {
544  tools::wcsv::ntuple::column<float>* column
545  = GetNtupleFColumn(ntupleId, columnId);
546  if ( ! column ) {
547  G4ExceptionDescription description;
548  description << " " << "ntupleId " << ntupleId
549  << " columnId " << columnId << " does not exist.";
550  G4Exception("G4CsvNtupleManager::FillNtupleFColumn()",
551  "Analysis_W009", JustWarning, description);
552  return false;
553  }
554 
555  column->fill(value);
556 #ifdef G4VERBOSE
557  if ( fState.GetVerboseL4() ) {
558  G4ExceptionDescription description;
559  description << " ntupleId " << ntupleId
560  << " columnId " << columnId << " value " << value;
561  fState.GetVerboseL4()->Message("fill", "ntuple F column", description);
562  }
563 #endif
564  return true;
565 }
566 
567 //_____________________________________________________________________________
569  G4double value)
570 {
571  tools::wcsv::ntuple::column<double>* column
572  = GetNtupleDColumn(ntupleId, columnId);
573  if ( ! column ) {
574  G4ExceptionDescription description;
575  description << " " << "ntupleId " << ntupleId
576  << " columnId " << columnId << " does not exist.";
577  G4Exception("G4CsvNtupleManager::FillNtupleDColumn()",
578  "Analysis_W009", JustWarning, description);
579  return false;
580  }
581 
582  column->fill(value);
583 #ifdef G4VERBOSE
584  if ( fState.GetVerboseL4() ) {
585  G4ExceptionDescription description;
586  description << " ntupleId " << ntupleId
587  << " columnId " << columnId << " value " << value;
588  fState.GetVerboseL4()->Message("fill", "ntuple D column", description);
589  }
590 #endif
591  return true;
592 }
593 
594 //_____________________________________________________________________________
596 {
597 #ifdef G4VERBOSE
598  if ( fState.GetVerboseL4() ) {
599  G4ExceptionDescription description;
600  description << " ntupleId " << ntupleId;
601  fState.GetVerboseL4()->Message("add", "ntuple row", description);
602  }
603 #endif
604 
605  G4CsvNtupleDescription* ntupleDescription
606  = GetNtupleInFunction(ntupleId, "AddNtupleRow");
607  if ( ! ntupleDescription ) return false;
608 
609  if ( ! ntupleDescription->fNtuple ) {
610  G4ExceptionDescription description;
611  description << " " << "ntuple does not exist. ";
612  G4Exception("G4CsvNtupleManager::AddNtupleRow()",
613  "Analysis_W008", JustWarning, description);
614  return false;
615  }
616 
617  ntupleDescription->fNtuple->add_row();
618 #ifdef G4VERBOSE
619  if ( fState.GetVerboseL4() ) {
620  G4ExceptionDescription description;
621  description << " ntupleId " << ntupleId;
622  fState.GetVerboseL4()->Message("add", "ntuple row", description);
623  }
624 #endif
625 
626  return true;
627 }
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