Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Hdf5AnalysisManager.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, 20/07/2017 ([email protected])
28
30#include "G4Hdf5FileManager.hh"
34#include "G4Threading.hh"
35#include "G4AutoLock.hh"
36
37using namespace G4Analysis;
38
39// mutex in a file scope
40
41namespace {
42 //Mutex to lock master manager when opening a file
43 G4Mutex openFileMutex = G4MUTEX_INITIALIZER;
44 //Mutex to lock master manager when merging H1 histograms
45 G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
46 //Mutex to lock master manager when merging H1 histograms
47 G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
48 //Mutex to lock master manager when merging H1 histograms
49 G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
50 //Mutex to lock master manager when merging P1 profiles
51 G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
52 //Mutex to lock master manager when merging P2 profiles
53 G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
54 //Mutex to lock master manager when closing a file
55 G4Mutex closeFileMutex = G4MUTEX_INITIALIZER;
56}
57
58G4Hdf5AnalysisManager* G4Hdf5AnalysisManager::fgMasterInstance = nullptr;
59G4ThreadLocal G4Hdf5AnalysisManager* G4Hdf5AnalysisManager::fgInstance = nullptr;
60
61//_____________________________________________________________________________
63{
64 if ( fgInstance == nullptr ) {
66 fgInstance = new G4Hdf5AnalysisManager(isMaster);
67 }
68
69 return fgInstance;
70}
71
72//_____________________________________________________________________________
74{
75 return ( fgInstance != 0 );
76}
77
78//_____________________________________________________________________________
80 : G4ToolsAnalysisManager("Hdf5", isMaster),
81 fNtupleFileManager(nullptr),
82 fFileManager(nullptr)
83{
84#ifdef G4MULTITHREADED
85#ifndef H5_HAVE_THREADSAFE
87 message
88 << "Your HDF5 lib is not built with H5_HAVE_THREADSAFE.";
89 G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
90 "Analysis_F002", FatalException, message);
91#endif
92#endif
93
94 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
95 G4ExceptionDescription description;
96 description
97 << " "
98 << "G4Hdf5AnalysisManager already exists."
99 << "Cannot create another instance.";
100 G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
101 "Analysis_F001", FatalException, description);
102 }
103 if ( isMaster ) fgMasterInstance = this;
104 fgInstance = this;
105
106 // File manager
107 fFileManager = std::make_shared<G4Hdf5FileManager>(fState);
108 SetFileManager(fFileManager);
109 fFileManager->SetBasketSize(fgkDefaultBasketSize);
110
111 // Ntuple file manager
112 fNtupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(fState);
113 fNtupleFileManager->SetFileManager(fFileManager);
114 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
115}
116
117//_____________________________________________________________________________
119{
120 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
121 fgInstance = nullptr;
122}
123
124//
125// private methods
126//
127
128//_____________________________________________________________________________
129G4bool G4Hdf5AnalysisManager::WriteH1()
130{
131 auto h1Vector = fH1Manager->GetH1Vector();
132 auto hnVector = fH1Manager->GetHnVector();
133
134 if ( ! h1Vector.size() ) return true;
135
136 auto result = true;
137
138 if ( ! G4Threading::IsWorkerThread() ) {
139 result = WriteHn(h1Vector, hnVector, "h1");
140 }
141 else {
142 // The worker manager just adds its histograms to the master
143 // This operation needs a lock
144 G4AutoLock lH1(&mergeH1Mutex);
145 fgMasterInstance->fH1Manager->AddH1Vector(h1Vector);
146 lH1.unlock();
147 }
148
149 return result;
150}
151
152//_____________________________________________________________________________
153G4bool G4Hdf5AnalysisManager::WriteH2()
154{
155 auto h2Vector = fH2Manager->GetH2Vector();
156 auto hnVector = fH2Manager->GetHnVector();
157
158 if ( ! h2Vector.size() ) return true;
159
160 auto result = true;
161
162 if ( ! G4Threading::IsWorkerThread() ) {
163 result = WriteHn(h2Vector, hnVector, "h2");
164 }
165 else {
166 // The worker manager just adds its histograms to the master
167 // This operation needs a lock
168 G4AutoLock lH2(&mergeH2Mutex);
169 fgMasterInstance->fH2Manager->AddH2Vector(h2Vector);
170 lH2.unlock();
171 }
172
173 return result;
174}
175
176//_____________________________________________________________________________
177G4bool G4Hdf5AnalysisManager::WriteH3()
178{
179 auto h3Vector = fH3Manager->GetH3Vector();
180 auto hnVector = fH3Manager->GetHnVector();
181
182 if ( ! h3Vector.size() ) return true;
183
184 auto result = true;
185
186 if ( ! G4Threading::IsWorkerThread() ) {
187 result = WriteHn(h3Vector, hnVector, "h3");
188 }
189 else {
190 // The worker manager just adds its histograms to the master
191 // This operation needs a lock
192 G4AutoLock lH3(&mergeH3Mutex);
193 fgMasterInstance->fH3Manager->AddH3Vector(h3Vector);
194 lH3.unlock();
195 }
196
197 return result;
198}
199
200//_____________________________________________________________________________
201G4bool G4Hdf5AnalysisManager::WriteP1()
202{
203 auto p1Vector = fP1Manager->GetP1Vector();
204 auto hnVector = fP1Manager->GetHnVector();
205
206 if ( ! p1Vector.size() ) return true;
207
208 auto result = true;
209
210 if ( ! G4Threading::IsWorkerThread() ) {
211 result = WritePn(p1Vector, hnVector, "p1");
212 }
213 else {
214 // The worker manager just adds its profiles to the master
215 // This operation needs a lock
216 G4AutoLock lP1(&mergeP1Mutex);
217 fgMasterInstance->fP1Manager->AddP1Vector(p1Vector);
218 lP1.unlock();
219 }
220
221 return result;
222}
223
224//_____________________________________________________________________________
225G4bool G4Hdf5AnalysisManager::WriteP2()
226{
227 auto p2Vector = fP2Manager->GetP2Vector();
228 auto hnVector = fP2Manager->GetHnVector();
229
230 if ( ! p2Vector.size() ) return true;
231
232 auto result = true;
233
234 if ( ! G4Threading::IsWorkerThread() ) {
235 result = WritePn(p2Vector, hnVector, "p2");
236 }
237 else {
238 // The worker manager just adds its profiles to the master
239 // This operation needs a lock
240 G4AutoLock lP2(&mergeP2Mutex);
241 fgMasterInstance->fP2Manager->AddP2Vector(p2Vector);
242 lP2.unlock();
243 }
244
245 return result;
246}
247
248//_____________________________________________________________________________
249G4bool G4Hdf5AnalysisManager::Reset()
250{
251// Reset histograms and ntuple
252
253 auto finalResult = true;
254
255 auto result = G4ToolsAnalysisManager::Reset();
256 finalResult = finalResult && result;
257
258 result = fNtupleFileManager->Reset();
259 finalResult = finalResult && result;
260
261 return finalResult;
262}
263
264//
265// protected methods
266//
267
268//_____________________________________________________________________________
270{
271 // Create ntuple manager(s)
272 // and set it to base class which takes then their ownership
273 SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
274
275 auto finalResult = true;
276
277 G4AutoLock lock(&openFileMutex);
278 auto result = fFileManager->OpenFile(fileName);
279 finalResult = finalResult && result;
280
281 result = fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
282 finalResult = finalResult && result;
283 lock.unlock();
284
285 return finalResult;
286}
287
288//_____________________________________________________________________________
290{
291 auto finalResult = true;
292
293#ifdef G4VERBOSE
294 if ( fState.GetVerboseL4() ) {
295 fState.GetVerboseL4()->Message("write", "files", "");
296 }
297#endif
298
299 auto result = WriteH1();
300 finalResult = finalResult && result;
301
302 // H2
303 result = WriteH2();
304 finalResult = finalResult && result;
305
306 // H3
307 result = WriteH3();
308 finalResult = finalResult && result;
309
310 // P1
311 result = WriteP1();
312 finalResult = finalResult && result;
313
314 // P2
315 result = WriteP2();
316 finalResult = finalResult && result;
317
318 // Write ASCII if activated
319 if ( IsAscii() ) {
320 result = WriteAscii(fFileManager->GetFileName());
321 finalResult = finalResult && result;
322 }
323
324#ifdef G4VERBOSE
325 if ( fState.GetVerboseL2() ) {
326 fState.GetVerboseL2()->Message("write", "files", "", finalResult);
327 }
328#endif
329
330 return finalResult;
331}
332
333//_____________________________________________________________________________
335{
336 auto finalResult = true;
337
338 G4AutoLock lock(&closeFileMutex);
339 auto result = fFileManager->CloseFiles();
340 finalResult = finalResult && result;
341
342 if ( reset ) {
343 // reset data
344 result = Reset();
345 if ( ! result ) {
346 G4ExceptionDescription description;
347 description << " " << "Resetting data failed";
348 G4Exception("G4Hdf5AnalysisManager::CloseFile()",
349 "Analysis_W021", JustWarning, description);
350 }
351 }
352 finalResult = finalResult && result;
353
354 result = fNtupleFileManager->ActionAtCloseFile(reset);
355 finalResult = finalResult && result;
356
357 lock.unlock();
358
359 return finalResult;
360}
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
bool G4bool
Definition: G4Types.hh:86
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::h1d * > & GetH1Vector() const
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
const std::vector< tools::histo::h2d * > & GetH2Vector() const
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
void AddH3Vector(const std::vector< tools::histo::h3d * > &h3Vector)
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::h3d * > & GetH3Vector() const
virtual G4bool CloseFileImpl(G4bool reset) final
G4Hdf5AnalysisManager(G4bool isMaster=true)
virtual G4bool OpenFileImpl(const G4String &fileName) final
virtual G4bool WriteImpl() final
static G4Hdf5AnalysisManager * Instance()
const std::vector< G4HnInformation * > & GetHnVector() const
void AddP1Vector(const std::vector< tools::histo::p1d * > &p1Vector)
const std::vector< tools::histo::p1d * > & GetP1Vector() const
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::p2d * > & GetP2Vector() const
void AddP2Vector(const std::vector< tools::histo::p2d * > &p2Vector)
std::shared_ptr< G4NtupleBookingManager > fNtupleBookingManager
G4AnalysisManagerState fState
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
G4bool WriteAscii(const G4String &fileName)
void SetNtupleManager(std::shared_ptr< G4VNtupleManager > ntupleManager)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
#define G4ThreadLocal
Definition: tls.hh:77