Geant4-11
G4XmlAnalysisManager.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 "G4XmlFileManager.hh"
35#include "G4Threading.hh"
36
37using namespace G4Analysis;
38using std::make_shared;
39
40//_____________________________________________________________________________
42{
44 fgIsInstance = true;
45 return instance.Instance();
46}
47
48//_____________________________________________________________________________
50{
51 return fgIsInstance;
52}
53
54//_____________________________________________________________________________
57{
59
60 // File Manager
61 fFileManager = std::make_shared<G4XmlFileManager>(fState);
63
64 // Ntuple file manager
65 fNtupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
66 fNtupleFileManager->SetFileManager(fFileManager);
67 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
68}
69
70//_____________________________________________________________________________
72{
73 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
74 fgIsInstance = false;
75}
76
77//
78// protected methods
79//
80
81//_____________________________________________________________________________
83{
84 // Create ntuple manager(s)
85 // and set it to base class which takes then their ownership
86 SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
87
88 auto result = true;
89
90 // Save file name in file manager
91 result &= fFileManager->OpenFile(fileName);
92
93 // Open ntuple files and create ntuples from bookings
94 result &= fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
95
96 return result;
97}
98
99//_____________________________________________________________________________
101{
102 auto result = true;
103
104 Message(kVL4, "write", "files");
105
106 // ntuples
107 fNtupleFileManager->ActionAtWrite();
108
111 }
112 else {
113 // Open all files registered with objects
114 fFileManager->OpenFiles();
115
116 // Write all histograms/profile on master
118 }
119
120 // Write ASCII if activated
121 if ( IsAscii() ) {
122 result &= WriteAscii(fFileManager->GetFileName());
123 }
124
125 // File
126 result &= fFileManager->WriteFiles();
127
128 Message(kVL3, "write", "files", "", result);
129
130 return result;
131}
132
133//_____________________________________________________________________________
135{
136 auto result = true;
137
138 Message(kVL4, "close", "files");
139
140 // Close open files
141 result &= fFileManager->CloseFiles();
142
143 // Close ntuple files
144 result &= fNtupleFileManager->ActionAtCloseFile(reset);
145
146 // Reset data
147 if ( reset ) {
148 if ( ! Reset() ) {
149 Warn("Resetting data failed", fkClass, "CloseFileImpl");
150 result = false;
151 }
152 }
153
154 // delete files if empty
155 // (ntuple files are created only if an ntuple is created)
156 if ( fFileManager->GetFile() && G4ToolsAnalysisManager::IsEmpty() ) {
157 if ( std::remove(fFileManager->GetFullFileName()) ) {
158 // std::remove returns 0 when success
160 "Removing file ") + fFileManager->GetFullFileName() + " failed",
161 fkClass, "CloseFileImpl");
162 result = false;
163 }
164 Message(kVL1, "delete", "empty file", fFileManager->GetFullFileName());
165 }
166 else {
167 Message(kVL3, "close", "files");
168 }
169
170 return result;
171}
172
173//_____________________________________________________________________________
175{
176// Reset histograms and ntuple
177
178 auto result = true;
179
181 if ( fNtupleFileManager != nullptr ) {
182 result &= fNtupleFileManager->Reset();
183 }
184
185 return result;
186}
bool G4bool
Definition: G4Types.hh:86
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)
static G4XmlAnalysisManager * Instance()
virtual G4bool CloseFileImpl(G4bool reset) final
static G4XmlAnalysisManager * fgMasterInstance
virtual G4bool WriteImpl() final
static constexpr std::string_view fkClass
static G4ThreadLocal G4bool fgIsInstance
std::shared_ptr< G4XmlNtupleFileManager > fNtupleFileManager
virtual G4bool OpenFileImpl(const G4String &fileName) final
virtual G4bool ResetImpl() final
std::shared_ptr< G4XmlFileManager > fFileManager
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