Geant4  10.02.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"
31 #include "G4CsvFileManager.hh"
33 #include "G4AnalysisUtilities.hh"
34 
35 #include "G4Threading.hh"
36 
37 #include <iostream>
38 
39 using namespace G4Analysis;
40 
41 //
42 // utility methods
43 //
44 
45 //_____________________________________________________________________________
47  : G4TNtupleManager<tools::wcsv::ntuple>(state),
48  fFileManager(nullptr),
49  fIsCommentedHeader(true),
50  fIsHippoHeader(false)
51 {}
52 
53 //_____________________________________________________________________________
55 {}
56 
57 //
58 // private methods
59 //
60 
61 //_____________________________________________________________________________
64  const G4String& /*name*/, const G4String& title)
65 {
66  // Create ntuple if the file is open (what means here that
67  // a filename was already set)
68  if ( fFileManager->GetFileName().size() ) {
69  if ( fFileManager->CreateNtupleFile(ntupleDescription) ) {
70  ntupleDescription->fNtuple
71  = new tools::wcsv::ntuple(*(ntupleDescription->fFile));
72  // ntuple object is deleted when closing a file
73  (ntupleDescription->fNtuple)->set_title(title);
74  fNtupleVector.push_back(ntupleDescription->fNtuple);
75  }
76  }
77 }
78 
79 //_____________________________________________________________________________
82 {
83  // create a file for this ntuple
84  if ( ! fFileManager->CreateNtupleFile(ntupleDescription) ) return;
85 
86  // create ntuple
87  ntupleDescription->fNtuple
88  = new tools::wcsv::ntuple(
89  *(ntupleDescription->fFile), G4cerr, ntupleDescription->fNtupleBooking);
90  fNtupleVector.push_back(ntupleDescription->fNtuple);
91 
92  // write header (if activated)
93  if ( ! WriteHeader(ntupleDescription->fNtuple) ) {
94  G4ExceptionDescription description;
95  description << " "
96  << "Writing ntuple header has failed. ";
97  G4Exception("G4CsvNtupleManager::CreateNtupleFromBooking()",
98  "Analysis_W021", JustWarning, description);
99  }
100  }
101 
102 //_____________________________________________________________________________
105 {
106  // Write header if ntuple already exists
107  if ( ! WriteHeader(ntupleDescription->fNtuple) ) {
108  G4ExceptionDescription description;
109  description << " "
110  << "Writing ntuple header has failed. ";
111  G4Exception("G4CsvNtupleManager::FinishTNtuple()",
112  "Analysis_W022", JustWarning, description);
113  }
114 }
115 
116 //_____________________________________________________________________________
117 G4bool G4CsvNtupleManager::WriteHeader(tools::wcsv::ntuple* ntuple) const
118 {
119 // Write header if ntuple already exists and if this option is activated.
120 // When both Hippo and Commented headers are selected, only Commented
121 // header, which reading is supported.
122 // Return false only if an error occurred.
123 
124  if ( fIsCommentedHeader ) {
125  return ntuple->write_commented_header(G4cout);
126  }
127 
128  // write hippo header (if activated and if not commented header)
129  if ( fIsHippoHeader ) {
130  ntuple->write_hippo_header();
131  return true;
132  }
133 
134  return true;
135 }
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4CsvNtupleManager(const G4AnalysisManagerState &state)
virtual void FinishTNtuple(NtupleDescriptionType *ntupleDescription) final
std::shared_ptr< G4CsvFileManager > fFileManager
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
virtual void CreateTNtupleFromBooking(NtupleDescriptionType *ntupleDescription) final
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
std::vector< tools::wcsv::ntuple * > fNtupleVector
virtual void CreateTNtuple(NtupleDescriptionType *ntupleDescription, const G4String &name, const G4String &title) final
G4bool WriteHeader(NtupleType *ntuple) const
tools::ntuple_booking fNtupleBooking
G4GLOB_DLL std::ostream G4cerr