Geant4-11
G4GenericAnalysisManager.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
36#include "G4Exception.hh"
37
38using namespace G4Analysis;
39using std::to_string;
40
41// mutex in a file scope
42
43namespace {
44
45//_____________________________________________________________________________
46void WriteHnWarning(const G4String& hnType, G4int id,
47 std::string_view inClass,
48 std::string_view inFunction)
49{
50 Warn("Failed to get " + hnType + " id " + to_string(id), inClass, inFunction);
51}
52
53}
54
55//_____________________________________________________________________________
57{
59 fgIsInstance = true;
60 return instance.Instance();
61}
62
63//_____________________________________________________________________________
65{
66 return fgIsInstance;
67}
68
69//_____________________________________________________________________________
72{
74
75 // File manager
76 fFileManager = std::make_shared<G4GenericFileManager>(fState);
78}
79
80//_____________________________________________________________________________
82{
83 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
84 fgIsInstance = false;
85}
86
87//
88// private methods
89//
90
91//_____________________________________________________________________________
93{
94 if ( fNtupleFileManager ) {
95 Warn("The ntuple file manager already exists.",
96 fkClass, "CreateNtupleFileManager");
97 return;
98 }
99
100 auto fileType = GetExtension(fileName);
101 auto output = G4Analysis::GetOutput(fileType);
102 if ( output == G4AnalysisOutput::kNone ) {
103 Warn("The file type " + fileType + "is not supported.",
104 fkClass, "CreateNtupleFileManager");
105 return;
106 }
107
108 // Set file type to booked ntuples
109 fNtupleBookingManager->SetFileType(fileType);
110
111 Message(kVL4, "create", "ntuple file manager", fileType);
112
113 fNtupleFileManager = fFileManager->CreateNtupleFileManager(output);
114 if (fNtupleFileManager) {
115 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
116
117 if ( fNtupleFileManager->IsNtupleMergingSupported() ) {
118 // set merginng
121 fNtupleFileManager->SetBasketSize(fBasketSize);
122 fNtupleFileManager->SetBasketEntries(fBasketEntries);
123 }
124 else if ( fIsNtupleMergingSet && fMergeNtuples ) {
125 Warn("Ntuple merging is not available with " + fileType + " output.\n" +
126 "Setting is ignored.",
127 fkClass, "CreateNtupleFileManager");
128 }
129 }
130
131 Message(kVL3, "create", "ntuple file manager", fileType);
132}
133
134//
135// protected methods
136//
137
138//_____________________________________________________________________________
140{
141 Message(kVL4, "open", "file", fileName);
142
143 // Add file name extension, if missing
144 auto fullFileName = fileName;
145 if ( ! GetExtension(fileName).size() ) {
146 auto defaultFileType = fFileManager->GetDefaultFileType();
147 // G4cout << "File type is not defined, using default: " << defaultFileType << G4endl;
148 if ( ! defaultFileType.size() ) {
149 G4Exception("G4GenericAnalysisManager::OpenFileImpl", "Analysis_F001",
151 G4String("Cannot open file \"" + fileName + "\".\n"
152 "Please, use a file name with an extension or define the default file type\n"
153 "via G4AnalysisManager::SetDefaultFileType()"));
154 }
155
156 fullFileName = fileName + "." + fFileManager->GetDefaultFileType();
157 }
158
159 // Create ntuple file manager if there are booked ntuples
160 if (! fNtupleFileManager) {
161 CreateNtupleFileManager(fullFileName);
162 }
163
164 // Create ntuple manager
165 // and set it to base class which takes then its ownership
166 if (fNtupleFileManager) {
167 SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
168 }
169
170 auto result = true;
171
172 // Open file for histograms/profiles
173 result &= fFileManager->OpenFile(fullFileName);
174
175 // Open ntuple file(s) and create ntuples from bookings
176 if (fNtupleFileManager) {
177 result &= fNtupleFileManager->ActionAtOpenFile(fullFileName);
178 }
179
180 Message(kVL3, "open", "file", fileName);
181
182 return result;
183}
184
185//_____________________________________________________________________________
187{
188 Message(kVL4, "write", "files");
189
190 auto result = true;
193 }
194 else {
195 // Open all files registered with objects
196 fFileManager->OpenFiles();
197
198 // Write all histograms/profile on master
200 }
201
202 // Ntuples
203 if (fNtupleFileManager) {
204 result &= fNtupleFileManager->ActionAtWrite();
205 }
206
207 // File
208 result &= fFileManager->WriteFiles();
209
210 // Write ASCII if activated
211 if ( IsAscii() ) {
212 result &= WriteAscii(fFileManager->GetFileName());
213 }
214
215 Message(kVL3, "write", "files", "", result);
216
217 return result;
218}
219
220//_____________________________________________________________________________
222{
223 Message(kVL4, "close", "files");
224
225 auto result = true;
226 if (fNtupleFileManager) {
227 result &= fNtupleFileManager->ActionAtCloseFile(reset);
228 }
229
230 // close file
231 if ( ! fFileManager->CloseFiles() ) {
232 Warn("Closing files failed", fkClass, "CloseFileImpl");
233 result = false;
234 }
235
236 // delete empty files
237 if ( ! fFileManager->DeleteEmptyFiles() ) {
238 Warn("Deleting empty files failed", fkClass, "CloseFileImpl");
239 result = false;
240 }
241
242 if ( reset ) {
243 if ( ! Reset() ) {
244 Warn("Resetting data failed", fkClass, "CloseFileImpl");
245 result = false;
246 }
247 }
248
249 Message(kVL3, "close", "files", "", result);
250
251 return result;
252}
253
254//_____________________________________________________________________________
256{
257// Reset histograms and ntuple
258
259 Message(kVL4, "reset", "");
260
261 auto result = true;
263 if ( fNtupleFileManager != nullptr ) {
264 result &= fNtupleFileManager->Reset();
265 }
266
267 Message(kVL3, "reset", "", "", result);
268
269 return result;
270}
271
272//_____________________________________________________________________________
274{
275 // Experimental extra write
276
277 // Do not write histo on worker (redundant and fails in hdf5 )
278 // If default file is not used, users have to call Merge from their code
279 if ( G4Threading::IsWorkerThread() ) return false;
280
281 auto h1d = GetH1(id, false);
282 if ( ! h1d ) {
283 WriteHnWarning("H1", id, fkClass, "WriteH1");
284 return false;
285 }
286
287 auto h1Name = GetH1Name(id);
288 return fFileManager->WriteTExtra<tools::histo::h1d>(fileName, h1d, h1Name);
289}
290
291//_____________________________________________________________________________
293{
294 // Experimental extra write
295
296 // Do not write histo on worker (redundant and fails in hdf5 )
297 // If default file is not used, users have to call Merge from their code
298 if ( G4Threading::IsWorkerThread() ) return false;
299
300 auto h2d = GetH2(id, false);
301 if ( ! h2d ) {
302 WriteHnWarning("H2", id, fkClass, "WriteH2");
303 return false;
304 }
305
306 auto h2Name = GetH2Name(id);
307 return fFileManager->WriteTExtra<tools::histo::h2d>(fileName, h2d, h2Name);
308}
309//_____________________________________________________________________________
311{
312 // Experimental extra write
313
314 // Do not write histo on worker (redundant and fails in hdf5 )
315 // If default file is not used, users have to call Merge from their code
316 if ( G4Threading::IsWorkerThread() ) return false;
317
318 auto h3d = GetH3(id, false);
319 if ( ! h3d ) {
320 WriteHnWarning("H3", id, fkClass, "WriteH3");
321 return false;
322 }
323
324 auto h3Name = GetH3Name(id);
325 return fFileManager->WriteTExtra<tools::histo::h3d>(fileName, h3d, h3Name);
326}
327
328//_____________________________________________________________________________
330{
331 // Experimental extra write
332
333 // Do not write histo on worker (redundant and fails in hdf5 )
334 // If default file is not used, users have to call Merge from their code
335 if ( G4Threading::IsWorkerThread() ) return false;
336
337 auto p1d = GetP1(id, false);
338 if ( ! p1d ) {
339 WriteHnWarning("P1", id, fkClass, "WriteP1");
340 return false;
341 }
342
343 auto p1Name = GetP1Name(id);
344 return fFileManager->WriteTExtra<tools::histo::p1d>(fileName, p1d, p1Name);
345}
346//_____________________________________________________________________________
348{
349 // Experimental extra write
350
351 // Do not write histo on worker (redundant and fails in hdf5 )
352 // If default file is not used, users have to call Merge from their code
353 if ( G4Threading::IsWorkerThread() ) return false;
354
355 auto p2d = GetP2(id, false);
356 if ( ! p2d ) {
357 WriteHnWarning("P2", id, fkClass, "WriteP2");
358 return false;
359 }
360
361 auto p2Name = GetP2Name(id);
362 return fFileManager->WriteTExtra<tools::histo::p2d>(fileName, p2d, p2Name);
363}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4bool WriteP2(G4int id, const G4String &fileName)
void CreateNtupleFileManager(const G4String &fileName)
static G4ThreadLocal G4bool fgIsInstance
G4bool WriteH2(G4int id, const G4String &fileName)
virtual G4bool CloseFileImpl(G4bool reset=true) override
static constexpr std::string_view fkClass
G4bool WriteH3(G4int id, const G4String &fileName)
virtual G4bool ResetImpl() final
G4bool WriteH1(G4int id, const G4String &fileName)
static G4GenericAnalysisManager * fgMasterInstance
virtual G4bool WriteImpl() final
std::shared_ptr< G4VNtupleFileManager > fNtupleFileManager
G4bool WriteP1(G4int id, const G4String &fileName)
static G4GenericAnalysisManager * Instance()
virtual G4bool OpenFileImpl(const G4String &fileName) override
std::shared_ptr< G4GenericFileManager > fFileManager
tools::histo::h3d * GetH3(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
virtual G4bool ResetImpl() override
tools::histo::p1d * GetP1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
tools::histo::p2d * GetP2(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
virtual G4bool WriteImpl() override
tools::histo::h2d * GetH2(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
G4String GetH1Name(G4int id) const
G4String GetH2Name(G4int id) const
G4String GetP2Name(G4int id) const
std::shared_ptr< G4NtupleBookingManager > fNtupleBookingManager
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4AnalysisManagerState fState
G4String GetP1Name(G4int id) const
G4String GetH3Name(G4int id) const
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
G4bool WriteAscii(const G4String &fileName)
void SetNtupleManager(std::shared_ptr< G4VNtupleManager > ntupleManager)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
constexpr G4int kVL3
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
void WriteHnWarning(const G4String &hnType, G4int id, std::string_view inClass, std::string_view inFunction)