Geant4-11
G4RootAnalysisManager.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, 18/06/2013 (ivana@ipno.in2p3.fr)
28
30#include "G4RootFileManager.hh"
35#include "G4Threading.hh"
36
37using namespace G4Analysis;
38
39//_____________________________________________________________________________
41{
43 fgIsInstance = true;
44 return instance.Instance();
45}
46
47//_____________________________________________________________________________
49{
50 return fgIsInstance;
51}
52
53//_____________________________________________________________________________
56{
58
59 // File manager
60 fFileManager = std::make_shared<G4RootFileManager>(fState);
62
63 // Ntuple file manager
64 fNtupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
65 fNtupleFileManager->SetFileManager(fFileManager);
66 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
67}
68
69//_____________________________________________________________________________
71{
72 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
73 fgIsInstance = false;
74}
75
76//
77// protected methods
78//
79
80//_____________________________________________________________________________
82{
83 // Create ntuple manager(s)
84 // and set it to base class which takes then their ownership
85 SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
86
87 auto result = true;
88
89 // Open file
90 if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
91 result &= fFileManager->OpenFile(fileName);
92 }
93
94 // Open ntuple file(s) and create ntuples from bookings
95 result &= fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
96
97 return result;
98}
99
100//_____________________________________________________________________________
102{
103
104 auto result = true;
105
106 Message(kVL4, "write", "files");
107
110 }
111 else {
112 // Open all files registered with objects
113 fFileManager->OpenFiles();
114
115 // Write all histograms/profile on master
117 }
118
119 // Ntuples
120 result &= fNtupleFileManager->ActionAtWrite();
121
122 // File
123 if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
124 // write all open files
125 result &= fFileManager->WriteFiles();
126 }
127
128 // Write ASCII if activated
129 if ( IsAscii() ) {
130 result &= WriteAscii(fFileManager->GetFileName());
131 }
132
133 Message(kVL3, "write", "files", "", result);
134
135 return result;
136}
137
138//_____________________________________________________________________________
140{
141 auto result = true;
142
143 Message(kVL4, "close", "files");
144
145 if ( reset ) {
146 if ( ! Reset() ) {
147 Warn("Resetting data failed", fkClass, "CloseFileImpl");
148 result = false;
149 }
150 }
151
152 result &= fNtupleFileManager->ActionAtCloseFile(reset);
153
154 if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
155 // close all open files
156 result &= fFileManager->CloseFiles();
157 }
158
159 // No files clean-up in sequential mode
160 if ( ! G4Threading::IsMultithreadedApplication() ) return result;
161
162 G4bool isNtupleManagerEmpty = fNtupleBookingManager->IsEmpty();
163
164 // Delete files if empty in MT mode
166 isNtupleManagerEmpty ) ||
167 ( (! fState.GetIsMaster()) && isNtupleManagerEmpty &&
168 fNtupleFileManager->GetMergeMode() == G4NtupleMergeMode::kNone ) ) {
169
170 if ( std::remove(fFileManager->GetFullFileName()) ) {
171 // std::remove returns 0 when success
172 Warn( "Removing file " + fFileManager->GetFullFileName() + " failed",
173 fkClass, "CloseFileImpl");
174 result = false;
175 }
176
177 Message(kVL1, "delete", "empty file", fFileManager->GetFullFileName());
178 }
179 else {
180 Message(kVL3, "close", "files");
181 }
182
183 return result;
184}
185
186//_____________________________________________________________________________
188{
189// Reset histograms and ntuple
190
191 auto result = true;
192
194 if ( fNtupleFileManager != nullptr ) {
195 result &= fNtupleFileManager->Reset();
196 }
197
198 return result;
199}
200
201//
202// public methods
203//
204
205//_____________________________________________________________________________
207 G4int nofNtupleFiles)
208{
209 fNtupleFileManager->SetNtupleMerging(mergeNtuples, nofNtupleFiles);
210}
211
212//_____________________________________________________________________________
214{
215 fNtupleFileManager->SetNtupleRowWise(rowWise, rowMode);
216}
217
218//_____________________________________________________________________________
219void G4RootAnalysisManager::SetBasketSize(unsigned int basketSize)
220{
221 fFileManager->SetBasketSize(basketSize);
222}
223
224//_____________________________________________________________________________
225void G4RootAnalysisManager::SetBasketEntries(unsigned int basketEntries)
226{
227 fFileManager->SetBasketEntries(basketEntries);
228}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
virtual void SetBasketSize(unsigned int basketSize) override
virtual void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles=0) override
virtual G4bool CloseFileImpl(G4bool reset) override
static G4ThreadLocal G4bool fgIsInstance
std::shared_ptr< G4RootFileManager > fFileManager
static G4RootAnalysisManager * fgMasterInstance
std::shared_ptr< G4RootNtupleFileManager > fNtupleFileManager
static G4RootAnalysisManager * Instance()
virtual G4bool WriteImpl() override
virtual G4bool OpenFileImpl(const G4String &fileName) override
static constexpr std::string_view fkClass
virtual void SetNtupleRowWise(G4bool rowWise, G4bool rowMode=true) override
virtual void SetBasketEntries(unsigned int basketEntries) override
virtual G4bool ResetImpl() override
virtual G4bool ResetImpl() override
virtual G4bool WriteImpl() override
std::shared_ptr< G4NtupleBookingManager > fNtupleBookingManager
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4AnalysisManagerState fState
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
G4bool WriteAscii(const G4String &fileName)
void SetNtupleManager(std::shared_ptr< G4VNtupleManager > ntupleManager)
constexpr G4int kVL1
constexpr G4int kVL3
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:130