Geant4-11
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 (ivana@ipno.in2p3.fr)
28
29#include "G4RootFileManager.hh"
33
34#include "tools/wroot/file"
35#include "tools/wroot/to"
36#include "tools/zlib"
37
38using namespace tools;
39using namespace G4Analysis;
40
41//_____________________________________________________________________________
44{
45 // Create helpers defined in the base class
46 fH1FileManager = std::make_shared<G4RootHnFileManager<histo::h1d>>(this);
47 fH2FileManager = std::make_shared<G4RootHnFileManager<histo::h2d>>(this);
48 fH3FileManager = std::make_shared<G4RootHnFileManager<histo::h3d>>(this);
49 fP1FileManager = std::make_shared<G4RootHnFileManager<histo::p1d>>(this);
50 fP2FileManager = std::make_shared<G4RootHnFileManager<histo::p2d>>(this);
51}
52
53//
54// private methods
55//
56
57//_____________________________________________________________________________
58tools::wroot::directory* G4RootFileManager::CreateDirectory(
59 tools::wroot::file* rfile,
60 const G4String& directoryName, [[maybe_unused]] const G4String& objectType) const
61{
62 if ( ! rfile ) return nullptr;
63
64 if ( directoryName == "" ) {
65 // Do not create a new directory if its name is not set
66 return &(rfile->dir());
67 }
68
69 Message(kVL4, "create", "directory for " + objectType, directoryName);
70
71 auto directory = rfile->dir().mkdir(directoryName);
72 if ( ! directory ) {
73 Warn("Cannot create directory " + directoryName, fkClass, "CreateDirectory");
74 return nullptr;
75 }
76 else {
77 Message(kVL2, "create", "directory for " + objectType, directoryName);
78 }
79
80 return directory;
81}
82
83//_____________________________________________________________________________
85 RootNtupleDescription* ntupleDescription,
86 G4bool perThread,
87 G4int mainNumber) const
88{
89 // get ntuple file name
90
91 auto ntupleFileName = ntupleDescription->fFileName;
92 if ( ntupleFileName.size() ) {
93 if ( perThread ) {
94 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
95 }
96 } else {
97 // get default file name
98 ntupleFileName = GetFullFileName(fFileName, perThread);
99 }
100
101 // update filename per mainNumber
102 if ( mainNumber > -1 ) {
103 // update filename per mainNumber
104 ntupleFileName
105 = G4Analysis::GetNtupleFileName(ntupleFileName, GetFileType(), mainNumber);
106 }
107
108 return ntupleFileName;
109}
110
111//
112// protected methods
113//
114
115//_____________________________________________________________________________
116std::shared_ptr<G4RootFile>
118{
119 // create file
120 std::shared_ptr<wroot::file> file = std::make_shared<wroot::file>(G4cout, fileName);
121 file->add_ziper('Z',compress_buffer);
122 file->set_compression(fState.GetCompressionLevel());
123
124 if ( ! file->is_open() ) {
125 Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl");
126 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
127 }
128
129 // create histo directory
130 tools::wroot::directory* hdirectory
131 = CreateDirectory(file.get(), fHistoDirectoryName, "histograms");
132 if ( ! hdirectory ) {
133 // Warning is issued in CreateDirectory
134 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
135 }
136
137 // create ntuple directory
138 tools::wroot::directory* ndirectory
139 = CreateDirectory(file.get(), fNtupleDirectoryName, "ntuples");
140 if ( ! ndirectory ) {
141 // Warning is issued in CreateDirectory
142 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
143 }
144
145 return std::make_shared<G4RootFile>(file, hdirectory, ndirectory);
146}
147
148//_____________________________________________________________________________
149G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file)
150{
151// New prototype: called by G4TFileManager base classe
152
153 if ( ! file ) return false;
154
155 unsigned int n;
156 return std::get<0>(*file)->write(n);
157}
158
159//_____________________________________________________________________________
160G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
161{
162// New prototype: called by G4TFileManager base classe
163
164 if ( ! file ) return false;
165
166 // close file
167 std::get<0>(*file)->close();
168
169 return true;
170}
171
172//
173// public methods
174//
175//_____________________________________________________________________________
177{
178// Open default file
179
180 // Keep file name
181 fFileName = fileName;
182 auto name = GetFullFileName();
183
184 if ( fFile ) {
185 Warn("File " + fileName + " already exists.", fkClass, "OpenFile");
186 fFile.reset();
187 }
188
189 // Create file (and save in in the file map)
191 if ( ! fFile ) {
192 Warn("Failed to create file " + fileName, fkClass, "OpenFile");
193 return false;
194 }
195
197 fIsOpenFile = true;
198
199 return true;
200}
201
202//_____________________________________________________________________________
203std::shared_ptr<G4RootFile> G4RootFileManager::CreateNtupleFile(
204 RootNtupleDescription* ntupleDescription, G4int mainNumber)
205{
206 // get ntuple file name per object
207 auto perThread = true;
208 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
209
210 auto file = GetTFile(ntupleFileName, false);
211 if (! file) {
212 file = CreateTFile(ntupleFileName);
213 }
214
215 // register file in ntuple description only if it is not main ntuple file
216 // (main ntuple files are managed in main ntuple manager)
217 if ( mainNumber == -1 ) {
218 ntupleDescription->fFile = file;
219 }
220
221 return file;
222}
223
224//_____________________________________________________________________________
225std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile(
226 RootNtupleDescription* ntupleDescription, G4bool perThread, G4int mainNumber) const
227{
228 // get ntuple file name per object
229 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
230
231 return GetTFile(ntupleFileName, false);
232}
233
234//_____________________________________________________________________________
236 RootNtupleDescription* ntupleDescription)
237{
238 auto result = true;
239
240 if ( ntupleDescription->fFile ) {
241 // Ntuple files are registered in file manager map.
242 // they will be closed with CloseFiles() calls
243 ntupleDescription->fFile.reset();
244 // Notify not empty file
245 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
246 result &= SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
247 }
248
249 return result;
250}
251
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
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
static constexpr std::string_view fkClass
G4bool CloseNtupleFile(RootNtupleDescription *ntupleDescription)
virtual std::shared_ptr< G4RootFile > CreateFileImpl(const G4String &fileName) final
tools::wroot::directory * CreateDirectory(tools::wroot::file *rfile, const G4String &directoryName, const G4String &objectType) const
G4String GetNtupleFileName(RootNtupleDescription *ntupleDescription, G4bool perThread=true, G4int mainNumber=-1) const
virtual G4String GetFileType() const final
virtual G4bool CloseFileImpl(std::shared_ptr< G4RootFile > file) final
G4RootFileManager()=delete
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< G4RootFile > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< G4RootFile > 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
void LockDirectoryNames()
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager
G4String fHistoDirectoryName
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< G4RootFile > fFile
constexpr G4int kVL2
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4String GetNtupleFileName(const G4String &fileName, const G4String &fileType, const G4String &ntupleName)
const char * name(G4int ptype)
std::shared_ptr< FT > fFile