Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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 ([email protected])
28
30#include "G4XmlFileManager.hh"
34#include "G4Threading.hh"
35#include "G4AutoLock.hh"
36
37using std::make_shared;
38
39// mutex in a file scope
40
41namespace {
42 //Mutex to lock master manager when merging H1 histograms
43 G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
44 //Mutex to lock master manager when merging H1 histograms
45 G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
46 //Mutex to lock master manager when merging H1 histograms
47 G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
48 //Mutex to lock master manager when merging P1 profiles
49 G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
50 //Mutex to lock master manager when merging P2 profiles
51 G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
52}
53
54G4XmlAnalysisManager* G4XmlAnalysisManager::fgMasterInstance = nullptr;
55G4ThreadLocal G4XmlAnalysisManager* G4XmlAnalysisManager::fgInstance = nullptr;
56
57//_____________________________________________________________________________
59{
60 if ( fgInstance == nullptr ) {
62 fgInstance = new G4XmlAnalysisManager(isMaster);
63 }
64
65 return fgInstance;
66}
67
68//_____________________________________________________________________________
70{
71 return ( fgInstance != 0 );
72}
73
74//_____________________________________________________________________________
76 : G4ToolsAnalysisManager("Xml", isMaster),
77 fFileManager(nullptr),
78 fNtupleFileManager(nullptr)
79{
80 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
81 G4ExceptionDescription description;
82 description
83 << " "
84 << "G4XmlAnalysisManager already exists."
85 << "Cannot create another instance.";
86 G4Exception("G4XmlAnalysisManager::G4XmlAnalysisManager",
87 "Analysis_F001", FatalException, description);
88 }
89 if ( isMaster ) fgMasterInstance = this;
90 fgInstance = this;
91
92 // File Manager
93 fFileManager = std::make_shared<G4XmlFileManager>(fState);
94 SetFileManager(fFileManager);
95
96 // Ntuple file manager
97 fNtupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
98 fNtupleFileManager->SetFileManager(fFileManager);
99 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
100}
101
102//_____________________________________________________________________________
104{
105 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
106 fgInstance = nullptr;
107}
108
109//
110// private methods
111//
112
113//_____________________________________________________________________________
114G4bool G4XmlAnalysisManager::WriteH1()
115{
116 auto h1Vector = fH1Manager->GetH1Vector();
117 auto hnVector = fH1Manager->GetHnVector();
118
119 if ( ! h1Vector.size() ) return true;
120
121 auto result = true;
122
123 if ( ! G4Threading::IsWorkerThread() ) {
124 auto directoryName = fFileManager->GetHistoDirectoryName();
125 result = WriteT(h1Vector, hnVector, directoryName, "h1");
126 }
127 else {
128 // The worker manager just adds its histograms to the master
129 // This operation needs a lock
130 G4AutoLock lH1(&mergeH1Mutex);
131 fgMasterInstance->fH1Manager->AddH1Vector(h1Vector);
132 lH1.unlock();
133 }
134
135 return result;
136}
137
138//_____________________________________________________________________________
139G4bool G4XmlAnalysisManager::WriteH2()
140{
141 auto h2Vector = fH2Manager->GetH2Vector();
142 auto hnVector = fH2Manager->GetHnVector();
143
144 if ( ! h2Vector.size() ) return true;
145
146 auto result = true;
147
148 if ( ! G4Threading::IsWorkerThread() ) {
149 auto directoryName = fFileManager->GetHistoDirectoryName();
150 result = WriteT(h2Vector, hnVector, directoryName, "h2");
151 }
152 else {
153 // The worker manager just adds its histograms to the master
154 // This operation needs a lock
155 G4AutoLock lH2(&mergeH2Mutex);
156 fgMasterInstance->fH2Manager->AddH2Vector(h2Vector);
157 lH2.unlock();
158 }
159
160 return result;
161}
162
163//_____________________________________________________________________________
164G4bool G4XmlAnalysisManager::WriteH3()
165{
166 auto h3Vector = fH3Manager->GetH3Vector();
167 auto hnVector = fH3Manager->GetHnVector();
168
169 if ( ! h3Vector.size() ) return true;
170
171 auto result = true;
172
173 if ( ! G4Threading::IsWorkerThread() ) {
174 auto directoryName = fFileManager->GetHistoDirectoryName();
175 result = WriteT(h3Vector, hnVector, directoryName, "h3");
176 }
177 else {
178 // The worker manager just adds its histograms to the master
179 // This operation needs a lock
180 G4AutoLock lH3(&mergeH3Mutex);
181 fgMasterInstance->fH3Manager->AddH3Vector(h3Vector);
182 lH3.unlock();
183 }
184
185 return result;
186}
187
188//_____________________________________________________________________________
189G4bool G4XmlAnalysisManager::WriteP1()
190{
191 auto p1Vector = fP1Manager->GetP1Vector();
192 auto hnVector = fP1Manager->GetHnVector();
193
194 if ( ! p1Vector.size() ) return true;
195
196 auto result = true;
197
198 if ( ! G4Threading::IsWorkerThread() ) {
199 auto directoryName = fFileManager->GetHistoDirectoryName();
200 result = WriteT(p1Vector, hnVector, directoryName, "p1");
201 }
202 else {
203 // The worker manager just adds its profiles to the master
204 // This operation needs a lock
205 G4AutoLock lP1(&mergeP1Mutex);
206 fgMasterInstance->fP1Manager->AddP1Vector(p1Vector);
207 lP1.unlock();
208 }
209
210 return result;
211}
212
213//_____________________________________________________________________________
214G4bool G4XmlAnalysisManager::WriteP2()
215{
216 auto p2Vector = fP2Manager->GetP2Vector();
217 auto hnVector = fP2Manager->GetHnVector();
218
219 if ( ! p2Vector.size() ) return true;
220
221 auto result = true;
222
223 if ( ! G4Threading::IsWorkerThread() ) {
224 auto directoryName = fFileManager->GetHistoDirectoryName();
225 result = WriteT(p2Vector, hnVector, directoryName, "p2");
226 }
227 else {
228 // The worker manager just adds its profiles to the master
229 // This operation needs a lock
230 G4AutoLock lP2(&mergeP2Mutex);
231 fgMasterInstance->fP2Manager->AddP2Vector(p2Vector);
232 lP2.unlock();
233 }
234
235 return result;
236}
237
238//_____________________________________________________________________________
239G4bool G4XmlAnalysisManager::Reset()
240{
241// Reset histograms and ntuple
242
243 auto finalResult = true;
244
245 auto result = G4ToolsAnalysisManager::Reset();
246 finalResult = finalResult && result;
247
248 result = fNtupleFileManager->Reset();
249 finalResult = finalResult && result;
250
251 return finalResult;
252}
253
254//
255// protected methods
256//
257
258//_____________________________________________________________________________
260{
261 // Create ntuple manager(s)
262 // and set it to base class which takes then their ownership
263 SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
264
265 auto finalResult = true;
266
267 // Save file name in file manager
268 auto result = fFileManager->OpenFile(fileName);
269 finalResult = finalResult && result;
270
271 // Open ntuple files and create ntuples from bookings
272 result = fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
273 finalResult = finalResult && result;
274
275 return finalResult;
276}
277
278//_____________________________________________________________________________
280{
281 auto finalResult = true;
282
283#ifdef G4VERBOSE
284 if ( fState.GetVerboseL4() ) {
285 fState.GetVerboseL4()->Message("write", "files", "");
286 }
287#endif
288
289 // ntuples
290 fNtupleFileManager->ActionAtWrite();
291
292 if ( ! fgMasterInstance &&
293 ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
294 ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
295 ( ! fP2Manager->IsEmpty() ) ) ) {
296
297 G4ExceptionDescription description;
298 description
299 << " " << "No master G4XmlAnalysisManager instance exists."
300 << G4endl
301 << " " << "Histogram data will not be merged.";
302 G4Exception("G4XmlAnalysisManager::Write()",
303 "Analysis_W031", JustWarning, description);
304 }
305
306 // H1
307 auto result = WriteH1();
308 finalResult = finalResult && result;
309
310 // H2
311 result = WriteH2();
312 finalResult = finalResult && result;
313
314 // H3
315 result = WriteH3();
316 finalResult = finalResult && result;
317
318 // P1
319 result = WriteP1();
320 finalResult = finalResult && result;
321
322 // P2
323 result = WriteP2();
324 finalResult = finalResult && result;
325
326 // Write ASCII if activated
327 if ( IsAscii() ) {
328 result = WriteAscii(fFileManager->GetFileName());
329 finalResult = finalResult && result;
330 }
331
332#ifdef G4VERBOSE
333 if ( fState.GetVerboseL2() ) {
334 fState.GetVerboseL2()->Message("write", "files", "", finalResult);
335 }
336#endif
337
338 return finalResult;
339}
340
341//_____________________________________________________________________________
343{
344 auto finalResult = true;
345
346#ifdef G4VERBOSE
347 if ( fState.GetVerboseL4() )
348 fState.GetVerboseL4()->Message("close", "files", "");
349#endif
350
351 // close open files
352 auto result = fFileManager->CloseFiles();
353 finalResult = finalResult && result;
354
355 // Close Hn file
356 // result = fFileManager->CloseHnFile();
357 // finalResult = finalResult && result;
358
359 // Close ntuple files
360 result = fNtupleFileManager->ActionAtCloseFile(reset);
361 finalResult = finalResult && result;
362
363 // Reset data
364 if ( reset ) {
365 result = Reset();
366 if ( ! result ) {
367 G4ExceptionDescription description;
368 description << " " << "Resetting data failed";
369 G4Exception("G4XmlAnalysisManager::CloseFile()",
370 "Analysis_W021", JustWarning, description);
371 }
372 finalResult = finalResult && result;
373 }
374
375 // delete files if empty
376 // (ntuple files are created only if an ntuple is created)
377 if ( fFileManager->GetFile() &&
380 result = ! std::remove(fFileManager->GetFullFileName());
381 // std::remove returns 0 when success
382 if ( ! result ) {
383 G4ExceptionDescription description;
384 description << " " << "Removing file "
385 << fFileManager->GetFullFileName() << " failed";
386 G4Exception("G4XmlAnalysisManager::CloseFile()",
387 "Analysis_W021", JustWarning, description);
388 }
389 finalResult = finalResult && result;
390#ifdef G4VERBOSE
391 if ( fState.GetVerboseL1() )
393 ->Message("delete", "empty file", fFileManager->GetFullFileName());
394#endif
395 }
396 else {
397#ifdef G4VERBOSE
398 if ( fState.GetVerboseL2() )
400 ->Message("close", "files", "");
401#endif
402 }
403
404 return finalResult;
405}
@ 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
#define G4endl
Definition: G4ios.hh:57
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL1() 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
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)
G4bool IsEmpty() const
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)
static G4XmlAnalysisManager * Instance()
virtual G4bool CloseFileImpl(G4bool reset) final
virtual G4bool WriteImpl() final
virtual G4bool OpenFileImpl(const G4String &fileName) final
G4XmlAnalysisManager(G4bool isMaster=true)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
#define G4ThreadLocal
Definition: tls.hh:77