Geant4  10.02
G4RootFileManager.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: G4RootFileManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootFileManager.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include "tools/wroot/file"
35 #include "tools/rroot/file"
36 #include <tools/zlib>
37 
38 #include <iostream>
39 #include <cstdio>
40 
41 using namespace G4Analysis;
42 
43 //_____________________________________________________________________________
45  : G4VFileManager(state),
46  fFile(nullptr),
47  fHistoDirectory(nullptr),
48  fNtupleDirectory(nullptr)
49 {}
50 
51 //_____________________________________________________________________________
53 {}
54 
55 //
56 // public methods
57 //
58 //_____________________________________________________________________________
60 {
61  // Keep file name
62  fFileName = fileName;
63  auto name = GetFullFileName();
64 
65  // delete previous file if exists
66  //if ( fFile ) delete fFile;
67 
68  // create new file
69  fFile = G4Analysis::make_unique<tools::wroot::file>(G4cout, name);
70  fFile->add_ziper('Z',tools::compress_buffer);
71  fFile->set_compression(fState.GetCompressionLevel());
72 
73  if ( ! fFile->is_open() ) {
74  G4ExceptionDescription description;
75  description << " " << "Cannot open file " << fileName;
76  G4Exception("G4RootAnalysisManager::OpenFile()",
77  "Analysis_W001", JustWarning, description);
78  return false;
79  }
80 
81  // Create directories
82  if ( ! CreateHistoDirectory() ) return false;
83  if ( ! CreateNtupleDirectory() ) return false;
84 
85  fLockFileName = true;
88 
89  fIsOpenFile = true;
90 
91  return true;
92 }
93 
94 //_____________________________________________________________________________
96 {
97 #ifdef G4VERBOSE
98  if ( fState.GetVerboseL4() )
99  fState.GetVerboseL4()->Message("write", "file", GetFullFileName());
100 #endif
101 
102  unsigned int n;
103  auto result = fFile->write(n);
104 
105 #ifdef G4VERBOSE
106  if ( fState.GetVerboseL1() )
107  fState.GetVerboseL1()->Message("write", "file", GetFullFileName(), result);
108 #endif
109 
110  return result;
111 }
112 
113 //_____________________________________________________________________________
115 {
116  // close file
117  fFile->close();
118  fLockFileName = false;
119  fIsOpenFile = false;
120 
121  return true;
122 }
123 
124 //_____________________________________________________________________________
126 {
127  if ( fHistoDirectoryName == "" ) {
128  // Do not create a new directory if its name is not set
129  fHistoDirectory = &(fFile->dir());
130  return true;
131  }
132 
133 #ifdef G4VERBOSE
134  if ( fState.GetVerboseL4() )
136  ->Message("create", "directory for histograms", fHistoDirectoryName);
137 #endif
138 
139  fHistoDirectory = fFile->dir().mkdir(fHistoDirectoryName);
140  if ( ! fHistoDirectory ) {
141  G4ExceptionDescription description;
142  description << " "
143  << "cannot create directory " << fHistoDirectoryName;
144  G4Exception("G4RootFileManager::CreateHistoDirectory()",
145  "Analysis_W001", JustWarning, description);
146  return false;
147  }
148 #ifdef G4VERBOSE
149  else {
150  if ( fState.GetVerboseL2() )
152  ->Message("create", "directory for histograms", fHistoDirectoryName);
153  }
154 #endif
155  return true;
156 }
157 
158 //_____________________________________________________________________________
160 {
161  if ( fNtupleDirectoryName == "" ) {
162  // Do not create a new directory if its name is not set
163  fNtupleDirectory = &(fFile->dir());
164  return true;
165  }
166 
167 #ifdef G4VERBOSE
168  if ( fState.GetVerboseL4() )
170  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
171 #endif
172 
174  if ( ! fNtupleDirectory ) {
175  G4ExceptionDescription description;
176  description << " "
177  << "cannot create directory " << fNtupleDirectoryName;
178  G4Exception("G4RootFileManager::CreateNtupleDirectory()",
179  "Analysis_W001", JustWarning, description);
180  return false;
181  }
182 #ifdef G4VERBOSE
183  else {
184  if ( fState.GetVerboseL2() )
186  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
187  }
188 #endif
189  return true;
190 }
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool fLockHistoDirectoryName
G4String fNtupleDirectoryName
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
tools::wroot::directory * fNtupleDirectory
G4String name
Definition: TRTMaterials.hh:40
virtual G4bool CloseFile() final
virtual G4bool WriteFile() final
G4String fHistoDirectoryName
tools::wroot::directory * fHistoDirectory
const G4AnalysisVerbose * GetVerboseL2() const
G4RootFileManager(const G4AnalysisManagerState &state)
G4GLOB_DLL std::ostream G4cout
virtual G4bool OpenFile(const G4String &fileName) final
const G4AnalysisVerbose * GetVerboseL4() const
virtual ~G4RootFileManager()
bool G4bool
Definition: G4Types.hh:79
std::unique_ptr< tools::wroot::file > fFile
const G4int n
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool fLockNtupleDirectoryName
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisVerbose * GetVerboseL1() const