Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "toolx/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 == nullptr) 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 == nullptr) {
73 Warn("Cannot create directory " + directoryName, fkClass, "CreateDirectory");
74 return nullptr;
75 }
76 Message(kVL2, "create", "directory for " + objectType, directoryName);
77
78 return directory;
79}
80
81//_____________________________________________________________________________
82G4String G4RootFileManager::GetNtupleFileName(
83 RootNtupleDescription* ntupleDescription,
84 G4bool perThread,
85 G4int mainNumber) const
86{
87 // get ntuple file name
88
89 auto ntupleFileName = ntupleDescription->GetFileName();
90 if (ntupleFileName.size() != 0u) {
91 if ( perThread ) {
92 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
93 }
94 }
95 else {
96 // get default file name
97 ntupleFileName = GetFullFileName(fFileName, perThread);
98 }
99
100 // update filename per mainNumber
101 if ( mainNumber > -1 ) {
102 // update filename per mainNumber
103 ntupleFileName
104 = G4Analysis::GetNtupleFileName(ntupleFileName, GetFileType(), mainNumber);
105 }
106
107 return ntupleFileName;
108}
109
110//
111// protected methods
112//
113
114//_____________________________________________________________________________
115std::shared_ptr<G4RootFile>
117{
118 // create file
119 std::shared_ptr<wroot::file> file = std::make_shared<wroot::file>(G4cout, fileName);
120 file->add_ziper('Z',toolx::compress_buffer);
121 file->set_compression(fState.GetCompressionLevel());
122
123 if ( ! file->is_open() ) {
124 Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl");
125 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
126 }
127
128 // create histo directory
129 tools::wroot::directory* hdirectory
130 = CreateDirectory(file.get(), fHistoDirectoryName, "histograms");
131 if (hdirectory == nullptr) {
132 // Warning is issued in CreateDirectory
133 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
134 }
135
136 // create ntuple directory
137 tools::wroot::directory* ndirectory
138 = CreateDirectory(file.get(), fNtupleDirectoryName, "ntuples");
139 if (ndirectory == nullptr) {
140 // Warning is issued in CreateDirectory
141 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
142 }
143
144 return std::make_shared<G4RootFile>(file, hdirectory, ndirectory);
145}
146
147//_____________________________________________________________________________
148G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file)
149{
150// New prototype: called by G4TFileManager base classe
151
152 // nothing to be done, but file should exist
153 return (file == nullptr) ? false : true;
154
155}
156
157//_____________________________________________________________________________
158G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
159{
160// New prototype: called by G4TFileManager base classe
161
162 if (file == nullptr) return false;
163
164 // write file (only once)
165 unsigned int n;
166 std::get<0>(*file)->write(n);
167
168 // close file
169 std::get<0>(*file)->close();
170
171 return true;
172}
173
174//
175// public methods
176//
177//_____________________________________________________________________________
179{
180// Open default file
181
182 // Keep file name
183 fFileName = fileName;
184 auto name = GetFullFileName();
185
186 if ( fFile ) {
187 Warn("File " + fileName + " already exists.", fkClass, "OpenFile");
188 fFile.reset();
189 }
190
191 // Create file (and save in in the file map)
192 fFile = CreateTFile(name);
193 if ( ! fFile ) {
194 Warn("Failed to create file " + fileName, fkClass, "OpenFile");
195 return false;
196 }
197
199 fIsOpenFile = true;
200
201 return true;
202}
203
204//_____________________________________________________________________________
205std::shared_ptr<G4RootFile> G4RootFileManager::CreateNtupleFile(
206 RootNtupleDescription* ntupleDescription, G4int mainNumber)
207{
208 // get ntuple file name per object
209 auto perThread = true;
210 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
211
212 auto file = GetTFile(ntupleFileName, false);
213 if (! file) {
214 file = CreateTFile(ntupleFileName);
215 }
216
217 // register file in ntuple description only if it is not main ntuple file
218 // (main ntuple files are managed in main ntuple manager)
219 if ( mainNumber == -1 ) {
220 ntupleDescription->SetFile(file);
221 }
222
223 return file;
224}
225
226//_____________________________________________________________________________
227std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile(
228 RootNtupleDescription* ntupleDescription, G4bool perThread, G4int mainNumber) const
229{
230 // get ntuple file name per object
231 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
232
233 return GetTFile(ntupleFileName, false);
234}
235
236//_____________________________________________________________________________
238 RootNtupleDescription* ntupleDescription, G4int mainNumber)
239{
240 // auto result = true;
241
242 // Notify not empty file
243 auto ntupleFileName = GetNtupleFileName(ntupleDescription, true, mainNumber);
244 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill());
245
246 // Ntuple files will be closed with CloseFiles() calls
247 ntupleDescription->GetFile().reset();
248
249 return result;
250}
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
std::shared_ptr< G4RootFile > CreateFileImpl(const G4String &fileName) final
G4bool CloseNtupleFile(RootNtupleDescription *ntupleDescription, G4int mainNumber=-1)
G4bool CloseFileImpl(std::shared_ptr< G4RootFile > file) final
G4RootFileManager()=delete
std::shared_ptr< G4RootFile > GetNtupleFile(RootNtupleDescription *ntupleDescription, G4bool perThread=true, G4int mainNumber=-1) const
G4bool OpenFile(const G4String &fileName) final
G4bool WriteFileImpl(std::shared_ptr< G4RootFile > file) final
std::shared_ptr< G4RootFile > CreateNtupleFile(RootNtupleDescription *ntupleDescription, G4int mainNumber=-1)
G4String GetFileType() const final
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > CreateTFile(const G4String &fileName)
void SetFile(std::shared_ptr< FT > file)
std::shared_ptr< FT > GetFile() const
G4String GetFileName() const
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
G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< G4RootFile > fFile
constexpr G4int kVL2
G4String GetTnFileName(const G4String &fileName, const G4String &fileType, G4int cycle=0)
G4String GetNtupleFileName(const G4String &fileName, const G4String &fileType, const G4String &ntupleName, G4int cycle=0)
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)