Geant4  10.02.p02
G4RootAnalysisManager.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: G4RootAnalysisManager.cc 93815 2015-11-02 11:32:20Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootAnalysisManager.hh"
31 #include "G4RootFileManager.hh"
32 #include "G4RootNtupleManager.hh"
33 #include "G4AnalysisVerbose.hh"
35 #include "G4Threading.hh"
36 #include "G4AutoLock.hh"
37 
38 #include <iostream>
39 #include <cstdio>
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 
58 
59 //_____________________________________________________________________________
61 {
62  if ( fgInstance == nullptr ) {
63  G4bool isMaster = ! G4Threading::IsWorkerThread();
64  fgInstance = new G4RootAnalysisManager(isMaster);
65  }
66 
67  return fgInstance;
68 }
69 
70 //_____________________________________________________________________________
72 {
73  return ( fgInstance != 0 );
74 }
75 
76 //_____________________________________________________________________________
78  : G4ToolsAnalysisManager("Root", isMaster),
79  fNtupleManager(nullptr),
80  fFileManager(nullptr)
81 {
82  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
83  G4ExceptionDescription description;
84  description
85  << " "
86  << "G4RootAnalysisManager already exists."
87  << "Cannot create another instance.";
88  G4Exception("G4RootAnalysisManager::G4RootAnalysisManager()",
89  "Analysis_F001", FatalException, description);
90  }
91  if ( isMaster ) fgMasterInstance = this;
92  fgInstance = this;
93 
94  // Create managers
96  fFileManager = std::make_shared<G4RootFileManager>(fState);
97  // The managers will be deleted by the base class
98 
99  // Set managers to base class which takes then their ownership
102 }
103 
104 //_____________________________________________________________________________
106 {
107  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
108  fgInstance = nullptr;
109 }
110 
111 //
112 // private methods
113 //
114 
115 //_____________________________________________________________________________
117 {
118  auto h1Vector = fH1Manager->GetH1Vector();
119  auto hnVector = fH1Manager->GetHnVector();
120 
121  if ( ! h1Vector.size() ) return true;
122 
123  auto result = true;
124 
125  if ( ! G4Threading::IsWorkerThread() ) {
126  auto directory = fFileManager->GetHistoDirectory();
127  result = WriteT(h1Vector, hnVector, directory, "h1");
128  }
129  else {
130  // The worker manager just adds its histograms to the master
131  // This operation needs a lock
132  G4AutoLock lH1(&mergeH1Mutex);
134  lH1.unlock();
135  }
136 
137  return result;
138 }
139 
140 //_____________________________________________________________________________
142 {
143  auto h2Vector = fH2Manager->GetH2Vector();
144  auto hnVector = fH2Manager->GetHnVector();
145 
146  if ( ! h2Vector.size() ) return true;
147 
148  auto result = true;
149 
150  if ( ! G4Threading::IsWorkerThread() ) {
151  auto directory = fFileManager->GetHistoDirectory();
152  result = WriteT(h2Vector, hnVector, directory, "h2");
153  }
154  else {
155  // The worker manager just adds its histograms to the master
156  // This operation needs a lock
157  G4AutoLock lH2(&mergeH2Mutex);
159  lH2.unlock();
160  }
161 
162  return result;
163 }
164 
165 //_____________________________________________________________________________
167 {
168  auto h3Vector = fH3Manager->GetH3Vector();
169  auto hnVector = fH3Manager->GetHnVector();
170 
171  if ( ! h3Vector.size() ) return true;
172 
173  auto result = true;
174 
175  if ( ! G4Threading::IsWorkerThread() ) {
176  auto directory = fFileManager->GetHistoDirectory();
177  result = WriteT(h3Vector, hnVector, directory, "h3");
178  }
179  else {
180  // The worker manager just adds its histograms to the master
181  // This operation needs a lock
182  G4AutoLock lH3(&mergeH3Mutex);
184  lH3.unlock();
185  }
186 
187  return result;
188 }
189 
190 //_____________________________________________________________________________
192 {
193  auto p1Vector = fP1Manager->GetP1Vector();
194  auto hnVector = fP1Manager->GetHnVector();
195 
196  if ( ! p1Vector.size() ) return true;
197 
198  auto result = true;
199 
200  if ( ! G4Threading::IsWorkerThread() ) {
201  auto directory = fFileManager->GetHistoDirectory();
202  result = WriteT(p1Vector, hnVector, directory, "p1");
203  }
204  else {
205  // The worker manager just adds its histograms to the master
206  // This operation needs a lock
207  G4AutoLock lP1(&mergeP1Mutex);
209  lP1.unlock();
210  }
211 
212  return result;
213 }
214 
215 //_____________________________________________________________________________
217 {
218  auto p2Vector = fP2Manager->GetP2Vector();
219  auto hnVector = fP2Manager->GetHnVector();
220 
221  if ( ! p2Vector.size() ) return true;
222 
223  auto result = true;
224 
225  if ( ! G4Threading::IsWorkerThread() ) {
226  auto directory = fFileManager->GetHistoDirectory();
227  result = WriteT(p2Vector, hnVector, directory, "p2");
228  }
229  else {
230  // The worker manager just adds its histograms to the master
231  // This operation needs a lock
232  G4AutoLock lP2(&mergeP2Mutex);
234  lP2.unlock();
235  }
236 
237  return result;
238 }
239 
240 //_____________________________________________________________________________
242 {
243 // Reset histograms and ntuple
244 
245  auto finalResult = true;
246 
247  auto result = G4ToolsAnalysisManager::Reset();
248  finalResult = finalResult && result;
249 
250  result = fNtupleManager->Reset(false);
251  finalResult = finalResult && result;
252 
253  return finalResult;
254 }
255 
256 //
257 // protected methods
258 //
259 
260 //_____________________________________________________________________________
262 {
263  auto finalResult = true;
264  auto result = fFileManager->SetFileName(fileName);
265  finalResult = finalResult && result;
266 
267 #ifdef G4VERBOSE
268  G4String name = fFileManager->GetFullFileName();
269  if ( fState.GetVerboseL4() )
270  fState.GetVerboseL4()->Message("open", "analysis file", name);
271 #endif
272 
273  result = fFileManager->OpenFile(fileName);
274  finalResult = finalResult && result;
275 
276  fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
278 
279 #ifdef G4VERBOSE
280  if ( fState.GetVerboseL1() )
281  fState.GetVerboseL1()->Message("open", "analysis file", name);
282 #endif
283 
284  return finalResult;
285 }
286 
287 //_____________________________________________________________________________
289 {
290 
291  auto finalResult = true;
292 
293  if ( ! fgMasterInstance &&
294  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
295  ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
296  ( ! fP2Manager->IsEmpty() ) ) ) {
297  G4ExceptionDescription description;
298  description
299  << " " << "No master G4RootAnalysisManager instance exists."
300  << G4endl
301  << " " << "Histogram/profile data will not be merged.";
302  G4Exception("G4RootAnalysisManager::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  // File
327  result = fFileManager->WriteFile();
328  finalResult = finalResult && result;
329 
330  // Write ASCII if activated
331  if ( IsAscii() ) {
332  result = WriteAscii(fFileManager->GetFileName());
333  finalResult = finalResult && result;
334  }
335 
336  return finalResult;
337 }
338 
339 //_____________________________________________________________________________
341 {
342  auto finalResult = true;
343 
344 #ifdef G4VERBOSE
345  if ( fState.GetVerboseL4() )
347  ->Message("close", "file", fFileManager->GetFullFileName());
348 #endif
349 
350  // reset data
351  auto result = Reset();
352  if ( ! result ) {
353  G4ExceptionDescription description;
354  description << " " << "Resetting data failed";
355  G4Exception("G4RootAnalysisManager::Write()",
356  "Analysis_W021", JustWarning, description);
357  }
358  finalResult = finalResult && result;
359 
360  // close file
361  fFileManager->CloseFile();
362 
363  // No files clean-up in sequential mode
364  if ( ! G4Threading::IsMultithreadedApplication() ) return finalResult;
365 
366  // Delete files if empty in MT mode
367  if ( ( fState.GetIsMaster() &&
370  ( ( ! fState.GetIsMaster() ) && fNtupleManager->IsEmpty() ) ) {
371  result = ! std::remove(fFileManager->GetFullFileName());
372  // std::remove returns 0 when success
373  if ( ! result ) {
374  G4ExceptionDescription description;
375  description << " " << "Removing file "
376  << fFileManager->GetFullFileName() << " failed";
377  G4Exception("G4XmlAnalysisManager::CloseFile()",
378  "Analysis_W021", JustWarning, description);
379  }
380  finalResult = finalResult && result;
381 #ifdef G4VERBOSE
382  if ( fState.GetVerboseL1() )
384  ->Message("delete", "empty file", fFileManager->GetFullFileName());
385 #endif
386  }
387  else {
388 #ifdef G4VERBOSE
389  if ( fState.GetVerboseL1() )
391  ->Message("close", "file", fFileManager->GetFullFileName());
392 #endif
393  }
394 
395  return finalResult;
396 }
virtual G4bool OpenFileImpl(const G4String &fileName) final
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
static G4RootAnalysisManager * fgMasterInstance
void AddH3Vector(const std::vector< tools::histo::h3d * > &h3Vector)
G4bool WriteT(const std::vector< T * > &htVector, const std::vector< G4HnInformation * > &hnVector, tools::wroot::directory *directory, const G4String &hnType)
const std::vector< G4HnInformation * > & GetHnVector() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
const std::vector< tools::histo::h1d * > & GetH1Vector() const
G4String name
Definition: TRTMaterials.hh:40
void AddP1Vector(const std::vector< tools::histo::p1d * > &p1Vector)
const std::vector< G4HnInformation * > & GetHnVector() const
G4bool IsAscii() const
static G4ThreadLocal G4RootAnalysisManager * fgInstance
virtual G4bool CloseFileImpl() final
void CreateNtuplesFromBooking()
#define G4ThreadLocal
Definition: tls.hh:89
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4bool Reset(G4bool deleteNtuple)
void AddP2Vector(const std::vector< tools::histo::p2d * > &p2Vector)
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
G4RootNtupleManager * fNtupleManager
const G4AnalysisVerbose * GetVerboseL4() const
const std::vector< tools::histo::p1d * > & GetP1Vector() const
bool G4bool
Definition: G4Types.hh:79
G4RootAnalysisManager(G4bool isMaster=true)
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::h3d * > & GetH3Vector() const
G4bool IsEmpty() const
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:136
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
virtual G4bool WriteImpl() final
G4int G4Mutex
Definition: G4Threading.hh:173
const std::vector< tools::histo::p2d * > & GetP2Vector() const
static G4RootAnalysisManager * Instance()
#define G4endl
Definition: G4ios.hh:61
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
G4AnalysisManagerState fState
G4bool IsEmpty() const
const G4AnalysisVerbose * GetVerboseL1() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
std::shared_ptr< G4RootFileManager > fFileManager
void SetNtupleDirectory(tools::wroot::directory *directory)
const std::vector< tools::histo::h2d * > & GetH2Vector() const
G4bool WriteAscii(const G4String &fileName)
const std::vector< G4HnInformation * > & GetHnVector() const