Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
27// Author: Ivana Hrivnacova, 15/06/2011 ([email protected])
28
29#include "G4RootFileManager.hh"
33
34#include "tools/wroot/file"
35#include "tools/wroot/to"
36#include "tools/zlib"
37
38#include <iostream>
39#include <cstdio>
40
41using namespace tools;
42using namespace G4Analysis;
43
44//_____________________________________________________________________________
47 fBasketSize(0),
48 fBasketEntries(0)
49{
50 // Create helpers defined in the base class
51 fH1FileManager = std::make_shared<G4RootHnFileManager<histo::h1d>>(this);
52 fH2FileManager = std::make_shared<G4RootHnFileManager<histo::h2d>>(this);
53 fH3FileManager = std::make_shared<G4RootHnFileManager<histo::h3d>>(this);
54 fP1FileManager = std::make_shared<G4RootHnFileManager<histo::p1d>>(this);
55 fP2FileManager = std::make_shared<G4RootHnFileManager<histo::p2d>>(this);
56}
57
58//_____________________________________________________________________________
60{}
61
62//
63// private methods
64//
65
66//_____________________________________________________________________________
67tools::wroot::directory* G4RootFileManager::CreateDirectory(
68 std::shared_ptr<tools::wroot::file> rfile,
69#ifdef G4VERBOSE
70 const G4String& directoryName, const G4String& objectType) const
71#else
72 const G4String& directoryName, const G4String& /*objectType*/) const
73#endif
74{
75 if ( ! rfile ) return nullptr;
76
77 if ( directoryName == "" ) {
78 // Do not create a new directory if its name is not set
79 return &(rfile->dir());
80 }
81
82#ifdef G4VERBOSE
83 if ( fState.GetVerboseL4() )
84 fState.GetVerboseL4()
85 ->Message("create", "directory for " + objectType, directoryName);
86#endif
87
88 auto directory = rfile->dir().mkdir(directoryName);
89 if ( ! directory ) {
90 G4ExceptionDescription description;
91 description << " "
92 << "cannot create directory " << directoryName;
93 G4Exception("G4RootFileManager::CreateDirectory()",
94 "Analysis_W001", JustWarning, description);
95 return nullptr;
96 }
97#ifdef G4VERBOSE
98 else {
99 if ( fState.GetVerboseL2() )
100 fState.GetVerboseL2()
101 ->Message("create", "directory for " + objectType, directoryName);
102 }
103#endif
104 return directory;
105}
106
107//_____________________________________________________________________________
108G4String G4RootFileManager::GetNtupleFileName(
109 RootNtupleDescription* ntupleDescription,
110 G4bool perThread,
111 G4int mainNumber) const
112{
113 // get ntuple file name
114
115 auto ntupleFileName = ntupleDescription->fFileName;
116 if ( ntupleFileName.size() ) {
117 if ( perThread ) {
118 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
119 }
120 } else {
121 // get default file name
122 ntupleFileName = GetFullFileName(fFileName, perThread);
123 }
124
125 // update filename per mainNumber
126 if ( mainNumber > -1 ) {
127 // update filename per mainNumber
128 ntupleFileName
129 = G4Analysis::GetNtupleFileName(ntupleFileName, GetFileType(), mainNumber);
130 }
131
132 return ntupleFileName;
133}
134
135//
136// protected methods
137//
138
139//_____________________________________________________________________________
140std::shared_ptr<G4RootFile>
142{
143 // create file
144 std::shared_ptr<wroot::file> file = std::make_shared<wroot::file>(G4cout, fileName);
145 file->add_ziper('Z',compress_buffer);
146 file->set_compression(fState.GetCompressionLevel());
147
148 if ( ! file->is_open() ) {
149 G4ExceptionDescription description;
150 description << " " << "Cannot create file " << fileName;
151 G4Exception("G4RootFileManager::CreateFileImpl()",
152 "Analysis_W001", JustWarning, description);
153 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
154 }
155
156 // create histo directory
157 tools::wroot::directory* hdirectory
158 = CreateDirectory(file, fHistoDirectoryName, "histograms");
159 if ( ! hdirectory ) {
160 // Warning is issued in CreateDirectory
161 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
162 }
163
164 // create ntuple directory
165 tools::wroot::directory* ndirectory
166 = CreateDirectory(file, fNtupleDirectoryName, "ntuples");
167 if ( ! ndirectory ) {
168 // Warning is issued in CreateDirectory
169 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
170 }
171
172 return std::make_shared<G4RootFile>(file, hdirectory, ndirectory);
173}
174
175//_____________________________________________________________________________
176G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file)
177{
178// New prototype: called by G4TFileManager base classe
179
180 if ( ! file ) return false;
181
182 unsigned int n;
183 return std::get<0>(*file)->write(n);
184}
185
186//_____________________________________________________________________________
187G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
188{
189// New prototype: called by G4TFileManager base classe
190
191 if ( ! file ) return false;
192
193 // close file
194 std::get<0>(*file)->close();
195
196 return true;
197}
198
199//
200// public methods
201//
202//_____________________________________________________________________________
204{
205// Open default file
206
207 // Keep file name
208 fFileName = fileName;
209 auto name = GetFullFileName();
210
211 if ( fFile ) {
212 G4ExceptionDescription description;
213 description << "File " << fileName << " already exists.";
214 G4Exception("G4RootFileManager::OpenFile()",
215 "Analysis_W001", JustWarning, description);
216 fFile.reset();
217 }
218
219 // Create file (and save in in the file map)
220 fFile = CreateTFile(name);
221 if ( ! fFile ) {
222 G4ExceptionDescription description;
223 description << "Failed to create file " << fileName;
224 G4Exception("G4RootFileManager::OpenFile()",
225 "Analysis_W001", JustWarning, description);
226 return false;
227 }
228
229 fIsOpenFile = true;
230 fLockDirectoryNames = true;
231
232 return true;
233}
234
235//_____________________________________________________________________________
236std::shared_ptr<G4RootFile> G4RootFileManager::CreateNtupleFile(
237 RootNtupleDescription* ntupleDescription, G4int mainNumber)
238{
239 // get ntuple file name per object
240 auto perThread = true;
241 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
242
243 auto file = GetTFile(ntupleFileName, false);
244 if (! file) {
245 file = CreateTFile(ntupleFileName);
246 }
247
248 // register file in ntuple description only if it is not main ntuple file
249 // (main ntuple files are managed in main ntuple manager)
250 if ( mainNumber == -1 ) {
251 ntupleDescription->fFile = file;
252 }
253
254 return file;
255}
256
257//_____________________________________________________________________________
258std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile(
259 RootNtupleDescription* ntupleDescription, G4bool perThread, G4int mainNumber) const
260{
261 // get ntuple file name per object
262 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
263
264 return GetTFile(ntupleFileName, false);
265}
266
267//_____________________________________________________________________________
269 RootNtupleDescription* ntupleDescription)
270{
271 auto finalResult = true;
272
273 if ( ntupleDescription->fFile ) {
274 // Ntuple files are registered in file manager map.
275 // they will be closed with CloseFiles() calls
276 ntupleDescription->fFile.reset();
277 // Notify not empty file
278 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
279 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
280 finalResult = result && finalResult;
281 }
282
283 return finalResult;
284}
285
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
std::tuple< std::shared_ptr< tools::wroot::file >, tools::wroot::directory *, tools::wroot::directory * > G4RootFile
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cout
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisManagerState & fState
G4bool CloseNtupleFile(RootNtupleDescription *ntupleDescription)
virtual std::shared_ptr< G4RootFile > CreateFileImpl(const G4String &fileName) final
virtual ~G4RootFileManager()
virtual G4String GetFileType() const final
G4RootFileManager(const G4AnalysisManagerState &state)
virtual G4bool CloseFileImpl(std::shared_ptr< G4RootFile > file) final
std::shared_ptr< G4RootFile > GetNtupleFile(RootNtupleDescription *ntupleDescription, G4bool perThread=true, G4int mainNumber=-1) const
virtual G4bool OpenFile(const G4String &fileName) final
virtual G4bool WriteFileImpl(std::shared_ptr< G4RootFile > file) final
std::shared_ptr< G4RootFile > CreateNtupleFile(RootNtupleDescription *ntupleDescription, G4int mainNumber=-1)
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > CreateTFile(const G4String &fileName)
std::shared_ptr< G4VTHnFileManager< tools::histo::h3d > > fH3FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::h1d > > fH1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p1d > > fP1FileManager
G4String fNtupleDirectoryName
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager
G4bool fLockDirectoryNames
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager
G4String fHistoDirectoryName
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< G4RootFile > fFile
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
G4String GetNtupleFileName(const G4String &fileName, const G4String &fileType, const G4String &ntupleName)
std::shared_ptr< TF > fFile