Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4CsvAnalysisManager.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 "G4CsvFileManager.hh"
32#include "G4AnalysisVerbose.hh"
34#include "G4UnitsTable.hh"
35#include "G4Threading.hh"
36#include "G4AutoLock.hh"
37
38#include <iostream>
39
40// mutex in a file scope
41
42namespace {
43 //Mutex to lock master manager when merging H1 histograms
44 G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
45 //Mutex to lock master manager when merging H1 histograms
46 G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
47 //Mutex to lock master manager when merging H1 histograms
48 G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
49 //Mutex to lock master manager when merging P1 profiles
50 G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
51 //Mutex to lock master manager when merging P2 profiles
52 G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
53}
54
55G4CsvAnalysisManager* G4CsvAnalysisManager::fgMasterInstance = nullptr;
56G4ThreadLocal G4CsvAnalysisManager* G4CsvAnalysisManager::fgInstance = nullptr;
57
58//_____________________________________________________________________________
60{
61 if ( fgInstance == nullptr ) {
63 fgInstance = new G4CsvAnalysisManager(isMaster);
64 }
65
66 return fgInstance;
67}
68
69//_____________________________________________________________________________
71{
72 return ( fgInstance != 0 );
73}
74
75//_____________________________________________________________________________
77 : G4ToolsAnalysisManager("Csv", isMaster),
78 fFileManager(nullptr),
79 fNtupleFileManager(nullptr)
80{
81 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
82 G4ExceptionDescription description;
83 description << " "
84 << "G4CsvAnalysisManager already exists."
85 << "Cannot create another instance.";
86 G4Exception("G4CsvAnalysisManager::G4CsvAnalysisManager()",
87 "Analysis_F001", FatalException, description);
88 }
89
90 if ( isMaster ) fgMasterInstance = this;
91 fgInstance = this;
92
93 // File Manager
94 fFileManager = std::make_shared<G4CsvFileManager>(fState);
95 SetFileManager(fFileManager);
96
97 // Ntuple file manager
98 fNtupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
99 fNtupleFileManager->SetFileManager(fFileManager);
100 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
101}
102
103//_____________________________________________________________________________
105{
106 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
107 fgInstance = nullptr;
108}
109
110//
111// private methods
112//
113
114//_____________________________________________________________________________
115G4bool G4CsvAnalysisManager::WriteH1()
116{
117 auto h1Vector = fH1Manager->GetH1Vector();
118 auto hnVector = fH1Manager->GetHnVector();
119
120 if ( ! h1Vector.size() ) return true;
121
122 auto result = true;
123
124 if ( ! G4Threading::IsWorkerThread() ) {
125 result = WriteT(h1Vector, hnVector, "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 G4CsvAnalysisManager::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 result = WriteT(h2Vector, hnVector, "h2");
150 }
151 else {
152 // The worker manager just adds its histograms to the master
153 // This operation needs a lock
154 G4AutoLock lH2(&mergeH2Mutex);
155 fgMasterInstance->fH2Manager->AddH2Vector(h2Vector);
156 lH2.unlock();
157 }
158
159 return result;
160}
161
162//_____________________________________________________________________________
163G4bool G4CsvAnalysisManager::WriteH3()
164{
165 auto h3Vector = fH3Manager->GetH3Vector();
166 auto hnVector = fH3Manager->GetHnVector();
167
168 if ( ! h3Vector.size() ) return true;
169
170 auto result = true;
171
172 if ( ! G4Threading::IsWorkerThread() ) {
173 result = WriteT(h3Vector, hnVector, "h3");
174 }
175 else {
176 // The worker manager just adds its histograms to the master
177 // This operation needs a lock
178 G4AutoLock lH3(&mergeH3Mutex);
179 fgMasterInstance->fH3Manager->AddH3Vector(h3Vector);
180 lH3.unlock();
181 }
182
183 return result;
184}
185
186//_____________________________________________________________________________
187G4bool G4CsvAnalysisManager::WriteP1()
188{
189 auto p1Vector = fP1Manager->GetP1Vector();
190 auto hnVector = fP1Manager->GetHnVector();
191
192 if ( ! p1Vector.size() ) return true;
193
194 auto result = true;
195
196 if ( ! G4Threading::IsWorkerThread() ) {
197 result = WriteT(p1Vector, hnVector, "p1");
198 }
199 else {
200 // The worker manager just adds its profiles to the master
201 // This operation needs a lock
202 G4AutoLock lP1(&mergeP1Mutex);
203 fgMasterInstance->fP1Manager->AddP1Vector(p1Vector);
204 lP1.unlock();
205 }
206
207 return result;
208}
209
210//_____________________________________________________________________________
211G4bool G4CsvAnalysisManager::WriteP2()
212{
213 auto p2Vector = fP2Manager->GetP2Vector();
214 auto hnVector = fP2Manager->GetHnVector();
215
216 if ( ! p2Vector.size() ) return true;
217
218 auto result = true;
219
220 if ( ! G4Threading::IsWorkerThread() ) {
221 result = WriteT(p2Vector, hnVector, "p2");
222 }
223 else {
224 // The worker manager just adds its profiles to the master
225 // This operation needs a lock
226 G4AutoLock lP2(&mergeP2Mutex);
227 fgMasterInstance->fP2Manager->AddP2Vector(p2Vector);
228 lP2.unlock();
229 }
230
231 return result;
232}
233
234//_____________________________________________________________________________
235G4bool G4CsvAnalysisManager::Reset()
236{
237// Reset histograms and ntuple
238
239 auto finalResult = true;
240
241 auto result = G4ToolsAnalysisManager::Reset();
242 finalResult = finalResult && result;
243
244 result = fNtupleFileManager->Reset();
245 finalResult = finalResult && result;
246
247 return finalResult;
248}
249
250//_____________________________________________________________________________
252{
253 // Create ntuple manager(s)
254 // and set it to base class which takes then their ownership
255 SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
256
257 auto finalResult = true;
258
259 // Save file name in file manager
260 auto result = fFileManager->OpenFile(fileName);
261 finalResult = finalResult && result;
262
263 // Open ntuple files and create ntuples from bookings
264 result = fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
265 finalResult = finalResult && result;
266
267 return finalResult;
268}
269
270//_____________________________________________________________________________
272{
273 // nothing to be done for Csv file
274 auto finalResult = true;
275
276#ifdef G4VERBOSE
277 if ( fState.GetVerboseL4() ) {
278 fState.GetVerboseL4()->Message("write", "files", "");
279 }
280#endif
281
282
283 if ( ! fgMasterInstance &&
284 ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
285 ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
286 ( ! fP2Manager->IsEmpty() ) ) ) {
287
288 G4ExceptionDescription description;
289 description
290 << " " << "No master G4CsvAnalysisManager instance exists."
291 << G4endl
292 << " " << "Histogram data will not be merged.";
293 G4Exception("G4CsvAnalysisManager::Write()",
294 "Analysis_W031", JustWarning, description);
295 }
296
297 // H1
298 auto result = WriteH1();
299 finalResult = finalResult && result;
300
301 // H2
302 result = WriteH2();
303 finalResult = finalResult && result;
304
305 // H3
306 result = WriteH3();
307 finalResult = finalResult && result;
308
309 // P1
310 result = WriteP1();
311 finalResult = finalResult && result;
312
313 // P2
314 result = WriteP2();
315 finalResult = finalResult && result;
316
317 // Ntuples
318 // Nothing to be done
319
320 // Write ASCII if activated
321 // Not available
322 //if ( IsAscii() ) {
323 // result = WriteAscii();
324 //}
325
326#ifdef G4VERBOSE
327 if ( fState.GetVerboseL2() ) {
328 fState.GetVerboseL2()->Message("write", "files", "", finalResult);
329 }
330#endif
331
332 return result;
333}
334
335//_____________________________________________________________________________
337{
338 auto finalResult = true;
339
340 // close open files
341 auto result = fFileManager->CloseFiles();
342 finalResult = finalResult && result;
343
344 // Histogram and profile files are created/closed indivudually for each
345 // object in WriteHn{Pn] function
346
347 result = fNtupleFileManager->ActionAtCloseFile(reset);
348 finalResult = finalResult && result;
349
350 // Reset data
351 if ( reset ) {
352 result = Reset();
353 if ( ! result ) {
354 G4ExceptionDescription description;
355 description << " " << "Resetting data failed";
356 G4Exception("G4CsvAnalysisManager::CloseFile()",
357 "Analysis_W021", JustWarning, description);
358 }
359 }
360 finalResult = finalResult && result;
361
362 return finalResult;
363}
@ 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 * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4CsvAnalysisManager(G4bool isMaster=true)
virtual G4bool OpenFileImpl(const G4String &fileName) final
static G4CsvAnalysisManager * Instance()
virtual G4bool CloseFileImpl(G4bool reset) final
virtual G4bool WriteImpl() final
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)
void SetNtupleManager(std::shared_ptr< G4VNtupleManager > ntupleManager)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
#define G4ThreadLocal
Definition: tls.hh:77