Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // $Id: G4CsvAnalysisManager.cc 93815 2015-11-02 11:32:20Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4CsvAnalysisManager.hh"
31 #include "G4CsvFileManager.hh"
32 #include "G4CsvNtupleManager.hh"
33 #include "G4AnalysisVerbose.hh"
35 #include "G4UnitsTable.hh"
36 #include "G4Threading.hh"
37 #include "G4AutoLock.hh"
38 
39 #include <iostream>
40 
41 // mutex in a file scope
42 
43 namespace {
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 }
55 
56 G4CsvAnalysisManager* G4CsvAnalysisManager::fgMasterInstance = nullptr;
57 G4ThreadLocal G4CsvAnalysisManager* G4CsvAnalysisManager::fgInstance = nullptr;
58 
59 //_____________________________________________________________________________
61 {
62  if ( fgInstance == nullptr ) {
63  G4bool isMaster = ! G4Threading::IsWorkerThread();
64  fgInstance = new G4CsvAnalysisManager(isMaster);
65  }
66 
67  return fgInstance;
68 }
69 
70 //_____________________________________________________________________________
72 {
73  return ( fgInstance != 0 );
74 }
75 
76 //_____________________________________________________________________________
78  : G4ToolsAnalysisManager("Csv", isMaster),
79  fNtupleManager(nullptr),
80  fFileManager(nullptr)
81 {
82  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
83  G4ExceptionDescription description;
84  description << " "
85  << "G4CsvAnalysisManager already exists."
86  << "Cannot create another instance.";
87  G4Exception("G4CsvAnalysisManager::G4CsvAnalysisManager()",
88  "Analysis_F001", FatalException, description);
89  }
90 
91  if ( isMaster ) fgMasterInstance = this;
92  fgInstance = this;
93  fNtupleManager = new G4CsvNtupleManager(fState);
94  fFileManager = std::make_shared<G4CsvFileManager>(fState);
95  fNtupleManager->SetFileManager(fFileManager);
96  // The managers will be deleted by the base class
97 
98  // Set managers to base class which takes then their ownreship
99  SetNtupleManager(fNtupleManager);
100  SetFileManager(fFileManager);
101 }
102 
103 //_____________________________________________________________________________
105 {
106  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
107  fgInstance = nullptr;
108 }
109 
110 //
111 // private methods
112 //
113 
114 //_____________________________________________________________________________
115 G4bool G4CsvAnalysisManager::CloseNtupleFiles()
116 {
117  auto ntupleVector
118  = fNtupleManager->GetNtupleDescriptionVector();
119 
120  // Close ntuple files
121  for ( auto ntupleDescription : ntupleVector) {
122  fFileManager->CloseNtupleFile(ntupleDescription);
123  }
124 
125  return true;
126 }
127 
128 
129 //
130 // protected methods
131 //
132 
133 //
134 // private methods
135 //
136 
137 
138 //_____________________________________________________________________________
139 G4bool G4CsvAnalysisManager::WriteH1()
140 {
141  auto h1Vector = fH1Manager->GetH1Vector();
142  auto hnVector = fH1Manager->GetHnVector();
143 
144  if ( ! h1Vector.size() ) return true;
145 
146  auto result = true;
147 
148  if ( ! G4Threading::IsWorkerThread() ) {
149  result = WriteT(h1Vector, hnVector, "h1");
150  }
151  else {
152  // The worker manager just adds its histograms to the master
153  // This operation needs a lock
154  G4AutoLock lH1(&mergeH1Mutex);
155  fgMasterInstance->fH1Manager->AddH1Vector(h1Vector);
156  lH1.unlock();
157  }
158 
159  return result;
160 }
161 
162 //_____________________________________________________________________________
163 G4bool G4CsvAnalysisManager::WriteH2()
164 {
165  auto h2Vector = fH2Manager->GetH2Vector();
166  auto hnVector = fH2Manager->GetHnVector();
167 
168  if ( ! h2Vector.size() ) return true;
169 
170  auto result = true;
171 
172  if ( ! G4Threading::IsWorkerThread() ) {
173  result = WriteT(h2Vector, hnVector, "h2");
174  }
175  else {
176  // The worker manager just adds its histograms to the master
177  // This operation needs a lock
178  G4AutoLock lH2(&mergeH2Mutex);
179  fgMasterInstance->fH2Manager->AddH2Vector(h2Vector);
180  lH2.unlock();
181  }
182 
183  return result;
184 }
185 
186 //_____________________________________________________________________________
187 G4bool G4CsvAnalysisManager::WriteH3()
188 {
189  auto h3Vector = fH3Manager->GetH3Vector();
190  auto hnVector = fH3Manager->GetHnVector();
191 
192  if ( ! h3Vector.size() ) return true;
193 
194  auto result = true;
195 
196  if ( ! G4Threading::IsWorkerThread() ) {
197  result = WriteT(h3Vector, hnVector, "h3");
198  }
199  else {
200  // The worker manager just adds its histograms to the master
201  // This operation needs a lock
202  G4AutoLock lH3(&mergeH3Mutex);
203  fgMasterInstance->fH3Manager->AddH3Vector(h3Vector);
204  lH3.unlock();
205  }
206 
207  return result;
208 }
209 
210 //_____________________________________________________________________________
211 G4bool G4CsvAnalysisManager::WriteP1()
212 {
213  auto p1Vector = fP1Manager->GetP1Vector();
214  auto hnVector = fP1Manager->GetHnVector();
215 
216  if ( ! p1Vector.size() ) return true;
217 
218  auto result = true;
219 
220  if ( ! G4Threading::IsWorkerThread() ) {
221  result = WriteT(p1Vector, hnVector, "p1");
222  }
223  else {
224  // The worker manager just adds its profiles to the master
225  // This operation needs a lock
226  G4AutoLock lP1(&mergeP1Mutex);
227  fgMasterInstance->fP1Manager->AddP1Vector(p1Vector);
228  lP1.unlock();
229  }
230 
231  return result;
232 }
233 
234 //_____________________________________________________________________________
235 G4bool G4CsvAnalysisManager::WriteP2()
236 {
237  auto p2Vector = fP2Manager->GetP2Vector();
238  auto hnVector = fP2Manager->GetHnVector();
239 
240  if ( ! p2Vector.size() ) return true;
241 
242  auto result = true;
243 
244  if ( ! G4Threading::IsWorkerThread() ) {
245  result = WriteT(p2Vector, hnVector, "p2");
246  }
247  else {
248  // The worker manager just adds its profiles to the master
249  // This operation needs a lock
250  G4AutoLock lP2(&mergeP2Mutex);
251  fgMasterInstance->fP2Manager->AddP2Vector(p2Vector);
252  lP2.unlock();
253  }
254 
255  return result;
256 }
257 
258 //_____________________________________________________________________________
259 G4bool G4CsvAnalysisManager::Reset()
260 {
261 // Reset histograms and ntuple
262 
263  auto finalResult = true;
264 
266  finalResult = finalResult && result;
267 
268  result = fNtupleManager->Reset(true);
269  finalResult = finalResult && result;
270 
271  return finalResult;
272 }
273 
274 //_____________________________________________________________________________
276 {
277  auto finalResult = true;
278  auto result = fFileManager->SetFileName(fileName);
279  finalResult = finalResult && result;
280 
281  // Only lock file name in file manager
282  result = fFileManager->OpenFile(fileName);
283  finalResult = finalResult && result;
284 
285  // Histogram and profile files are created/closed indivudually for each
286  // object in WriteHn{Pn] function
287 
288  // Create ntuples if they are booked
289  // (The files will be created with creating ntuples)
290  fNtupleManager->CreateNtuplesFromBooking();
291 
292  return finalResult;
293 }
294 
295 //_____________________________________________________________________________
297 {
298  // nothing to be done for Csv file
299  auto finalResult = true;
300 
301 #ifdef G4VERBOSE
302  if ( fState.GetVerboseL4() )
303  fState.GetVerboseL4()->Message("write", "files", "");
304 #endif
305 
306 
307  if ( ! fgMasterInstance &&
308  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
309  ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
310  ( ! fP2Manager->IsEmpty() ) ) ) {
311 
312  G4ExceptionDescription description;
313  description
314  << " " << "No master G4CsvAnalysisManager instance exists."
315  << G4endl
316  << " " << "Histogram data will not be merged.";
317  G4Exception("G4CsvAnalysisManager::Write()",
318  "Analysis_W031", JustWarning, description);
319  }
320 
321  // H1
322  auto result = WriteH1();
323  finalResult = finalResult && result;
324 
325  // H2
326  result = WriteH2();
327  finalResult = finalResult && result;
328 
329  // H3
330  result = WriteH3();
331  finalResult = finalResult && result;
332 
333  // P1
334  result = WriteP1();
335  finalResult = finalResult && result;
336 
337  // P2
338  result = WriteP2();
339  finalResult = finalResult && result;
340 
341  // Ntuples
342  // Nothing to be done
343 
344  // Write ASCII if activated
345  // Not available
346  //if ( IsAscii() ) {
347  // result = WriteAscii();
348  //}
349 
350 #ifdef G4VERBOSE
351  if ( fState.GetVerboseL1() )
353  ->Message("write", "files", "", finalResult);
354 #endif
355 
356  return result;
357 }
358 
359 //_____________________________________________________________________________
361 {
362  auto finalResult = true;
363 
364  // Unlock file name only
365  auto result = fFileManager->CloseFile();
366  finalResult = finalResult && result;
367 
368  // Histogram and profile files are created/closed indivudually for each
369  // object in WriteHn{Pn] function
370  // In sequential mode or in MT mode only on workers
371  result = CloseNtupleFiles();
372  finalResult = finalResult && result;
373 
374  // reset data
375  result = Reset();
376  if ( ! result ) {
377  G4ExceptionDescription description;
378  description << " " << "Resetting data failed";
379  G4Exception("G4CsvAnalysisManager::CloseFile()",
380  "Analysis_W021", JustWarning, description);
381  result = false;
382  }
383  finalResult = finalResult && result;
384 
385  return finalResult;
386 }
G4double G4ParticleHPJENDLHEData::G4double result
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
void AddH3Vector(const std::vector< tools::histo::h3d * > &h3Vector)
const std::vector< G4HnInformation * > & GetHnVector() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
const std::vector< tools::histo::h1d * > & GetH1Vector() const
void AddP1Vector(const std::vector< tools::histo::p1d * > &p1Vector)
const std::vector< G4HnInformation * > & GetHnVector() const
G4CsvAnalysisManager(G4bool isMaster=true)
virtual G4bool WriteImpl() final
void CreateNtuplesFromBooking()
#define G4ThreadLocal
Definition: tls.hh:89
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static G4CsvAnalysisManager * Instance()
G4bool Reset(G4bool deleteNtuple)
void AddP2Vector(const std::vector< tools::histo::p2d * > &p2Vector)
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
const G4AnalysisVerbose * GetVerboseL4() const
const std::vector< tools::histo::p1d * > & GetP1Vector() const
bool G4bool
Definition: G4Types.hh:79
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::h3d * > & GetH3Vector() const
G4bool IsEmpty() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:145
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
G4int G4Mutex
Definition: G4Threading.hh:173
const std::vector< tools::histo::p2d * > & GetP2Vector() const
#define G4endl
Definition: G4ios.hh:61
virtual G4bool OpenFileImpl(const G4String &fileName) final
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
G4AnalysisManagerState fState
virtual G4bool CloseFileImpl() final
const G4AnalysisVerbose * GetVerboseL1() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
const std::vector< tools::histo::h2d * > & GetH2Vector() const
const std::vector< G4HnInformation * > & GetHnVector() const