Geant4  10.01.p03
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 95250 2016-02-02 10:33:05Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootAnalysisManager.hh"
31 #include "G4RootFileManager.hh"
32 #include "G4H1ToolsManager.hh"
33 #include "G4H2ToolsManager.hh"
34 #include "G4H3ToolsManager.hh"
35 #include "G4P1ToolsManager.hh"
36 #include "G4P2ToolsManager.hh"
37 #include "G4RootNtupleManager.hh"
38 #include "G4AnalysisVerbose.hh"
40 #include "G4Threading.hh"
41 #include "G4AutoLock.hh"
42 
43 #include "tools/wroot/to"
44 #include "tools/wroot/file"
45 
46 #include <iostream>
47 #include <cstdio>
48 
49 // mutex in a file scope
50 
51 namespace {
52  //Mutex to lock master manager when merging H1 histograms
53  G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
54  //Mutex to lock master manager when merging H1 histograms
55  G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
56  //Mutex to lock master manager when merging H1 histograms
57  G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
58  //Mutex to lock master manager when merging P1 profiles
59  G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
60  //Mutex to lock master manager when merging P2 profiles
61  G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
62 }
63 
66 
67 //_____________________________________________________________________________
69 {
70  if ( fgInstance == 0 ) {
71  G4bool isMaster = ! G4Threading::IsWorkerThread();
72  fgInstance = new G4RootAnalysisManager(isMaster);
73  }
74 
75  return fgInstance;
76 }
77 
78 //_____________________________________________________________________________
80  : G4VAnalysisManager("Root", isMaster),
81  fH1Manager(0),
82  fH2Manager(0),
83  fH3Manager(0),
84  fP1Manager(0),
85  fP2Manager(0),
86  fNtupleManager(0),
87  fFileManager(0)
88 {
89  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
90  G4ExceptionDescription description;
91  description
92  << " "
93  << "G4RootAnalysisManager already exists."
94  << "Cannot create another instance.";
95  G4Exception("G4RootAnalysisManager::G4RootAnalysisManager()",
96  "Analysis_F001", FatalException, description);
97  }
98  if ( isMaster ) fgMasterInstance = this;
99  fgInstance = this;
100 
101  // Create managers
109  // The managers will be deleted by the base class
110 
111  // Set managers to base class
119 }
120 
121 //_____________________________________________________________________________
123 {
124  if ( fState.GetIsMaster() ) fgMasterInstance = 0;
125  fgInstance = 0;
126 }
127 
128 //
129 // private methods
130 //
131 
132 //_____________________________________________________________________________
134 {
135  const std::vector<tools::histo::h1d*>& h1Vector
136  = fH1Manager->GetH1Vector();
137  const std::vector<G4HnInformation*>& hnVector
138  = fH1Manager->GetHnVector();
139 
140  if ( ! h1Vector.size() ) return true;
141 
142  if ( ! G4Threading::IsWorkerThread() ) {
143 
144  for ( G4int i=0; i<G4int(h1Vector.size()); ++i ) {
145  G4HnInformation* info = hnVector[i];
146  G4bool activation = info->GetActivation();
147  G4String name = info->GetName();
148  // skip writing if activation is enabled and H1 is inactivated
149  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
150  tools::histo::h1d* h1 = h1Vector[i];
151 #ifdef G4VERBOSE
152  if ( fState.GetVerboseL3() )
153  fState.GetVerboseL3()->Message("write", "h1d", name);
154 #endif
155  tools::wroot::directory* histoDirectory
157  G4bool result
158  = to(*histoDirectory, *h1, name);
159  if ( ! result ) {
160  G4ExceptionDescription description;
161  description << " " << "saving histogram " << name << " failed";
162  G4Exception("G4RootAnalysisManager::Write()",
163  "Analysis_W022", JustWarning, description);
164  return false;
165  }
166  }
167  }
168  else {
169  // The worker manager just adds its histograms to the master
170  // This operation needs a lock
171  G4AutoLock lH1(&mergeH1Mutex);
173  lH1.unlock();
174  }
175 
176  return true;
177 }
178 
179 //_____________________________________________________________________________
181 {
182  const std::vector<tools::histo::h2d*>& h2Vector
183  = fH2Manager->GetH2Vector();
184  const std::vector<G4HnInformation*>& hnVector
185  = fH2Manager->GetHnVector();
186 
187  if ( ! h2Vector.size() ) return true;
188 
189  if ( ! G4Threading::IsWorkerThread() ) {
190 
191  for ( G4int i=0; i<G4int(h2Vector.size()); ++i ) {
192  G4HnInformation* info = hnVector[i];
193  G4bool activation = info->GetActivation();
194  G4String name = info->GetName();
195  // skip writing if inactivated
196  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
197  tools::histo::h2d* h2 = h2Vector[i];
198 #ifdef G4VERBOSE
199  if ( fState.GetVerboseL3() )
200  fState.GetVerboseL3()->Message("write", "h2d", name);
201 #endif
202  tools::wroot::directory* histoDirectory
204  G4bool result
205  = to(*histoDirectory, *h2, name);
206  if ( ! result ) {
207  G4ExceptionDescription description;
208  description << " " << "saving histogram " << name << " failed";
209  G4Exception("G4RootAnalysisManager::Write()",
210  "Analysis_W022", JustWarning, description);
211  return false;
212  }
213  }
214  }
215  else {
216  // The worker manager just adds its histograms to the master
217  // This operation needs a lock
218  G4AutoLock lH2(&mergeH2Mutex);
220  lH2.unlock();
221  }
222 
223  return true;
224 }
225 
226 //_____________________________________________________________________________
228 {
229  const std::vector<tools::histo::h3d*>& h3Vector
230  = fH3Manager->GetH3Vector();
231  const std::vector<G4HnInformation*>& hnVector
232  = fH3Manager->GetHnVector();
233 
234  if ( ! h3Vector.size() ) return true;
235 
236  if ( ! G4Threading::IsWorkerThread() ) {
237 
238  for ( G4int i=0; i<G4int(h3Vector.size()); ++i ) {
239  G4HnInformation* info = hnVector[i];
240  G4bool activation = info->GetActivation();
241  G4String name = info->GetName();
242  // skip writing if inactivated
243  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
244  tools::histo::h3d* h3 = h3Vector[i];
245 #ifdef G4VERBOSE
246  if ( fState.GetVerboseL3() )
247  fState.GetVerboseL3()->Message("write", "h3d", name);
248 #endif
249  tools::wroot::directory* histoDirectory
251  G4bool result
252  = to(*histoDirectory, *h3, name);
253  if ( ! result ) {
254  G4ExceptionDescription description;
255  description << " " << "saving histogram " << name << " failed";
256  G4Exception("G4RootAnalysisManager::Write()",
257  "Analysis_W022", JustWarning, description);
258  return false;
259  }
260  }
261  }
262  else {
263  // The worker manager just adds its histograms to the master
264  // This operation needs a lock
265  G4AutoLock lH3(&mergeH3Mutex);
267  lH3.unlock();
268  }
269 
270  return true;
271 }
272 
273 //_____________________________________________________________________________
275 {
276  const std::vector<tools::histo::p1d*>& p1Vector
277  = fP1Manager->GetP1Vector();
278  const std::vector<G4HnInformation*>& hnVector
279  = fP1Manager->GetHnVector();
280 
281  if ( ! p1Vector.size() ) return true;
282 
283  if ( ! G4Threading::IsWorkerThread() ) {
284 
285  for ( G4int i=0; i<G4int(p1Vector.size()); ++i ) {
286  G4HnInformation* info = hnVector[i];
287  G4bool activation = info->GetActivation();
288  G4String name = info->GetName();
289  // skip writing if activation is enabled and P1 is inactivated
290  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
291  tools::histo::p1d* p1 = p1Vector[i];
292 #ifdef G4VERBOSE
293  if ( fState.GetVerboseL3() )
294  fState.GetVerboseL3()->Message("write", "p1d", name);
295 #endif
296  tools::wroot::directory* profileDirectory
298  G4bool result
299  = to(*profileDirectory, *p1, name);
300  if ( ! result ) {
301  G4ExceptionDescription description;
302  description << " " << "saving profile " << name << " failed";
303  G4Exception("G4RootAnalysisManager::Write()",
304  "Analysis_W022", JustWarning, description);
305  return false;
306  }
307  }
308  }
309  else {
310  // The worker manager just adds its histograms to the master
311  // This operation needs a lock
312  G4AutoLock lP1(&mergeP1Mutex);
314  lP1.unlock();
315  }
316 
317  return true;
318 }
319 
320 //_____________________________________________________________________________
322 {
323  const std::vector<tools::histo::p2d*>& p2Vector
324  = fP2Manager->GetP2Vector();
325  const std::vector<G4HnInformation*>& hnVector
326  = fP2Manager->GetHnVector();
327 
328  if ( ! p2Vector.size() ) return true;
329 
330  if ( ! G4Threading::IsWorkerThread() ) {
331 
332  for ( G4int i=0; i<G4int(p2Vector.size()); ++i ) {
333  G4HnInformation* info = hnVector[i];
334  G4bool activation = info->GetActivation();
335  G4String name = info->GetName();
336  // skip writing if activation is enabled and P2 is inactivated
337  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
338  tools::histo::p2d* p2 = p2Vector[i];
339 #ifdef G4VERBOSE
340  if ( fState.GetVerboseL3() )
341  fState.GetVerboseL3()->Message("write", "p2d", name);
342 #endif
343  tools::wroot::directory* profileDirectory
345  G4bool result
346  = to(*profileDirectory, *p2, name);
347  if ( ! result ) {
348  G4ExceptionDescription description;
349  description << " " << "saving profile " << name << " failed";
350  G4Exception("G4RootAnalysisManager::Write()",
351  "Analysis_W022", JustWarning, description);
352  return false;
353  }
354  }
355  }
356  else {
357  // The worker manager just adds its histograms to the master
358  // This operation needs a lock
359  G4AutoLock lP2(&mergeP2Mutex);
361  lP2.unlock();
362  }
363 
364  return true;
365 }
366 
367 //_____________________________________________________________________________
369 {
370 // Reset histograms and ntuple
371 
372  G4bool finalResult = true;
373 
374  G4bool result = fH1Manager->Reset();
375  finalResult = finalResult && result;
376 
377  result = fH2Manager->Reset();
378  finalResult = finalResult && result;
379 
380  result = fH3Manager->Reset();
381  finalResult = finalResult && result;
382 
383  result = fP1Manager->Reset();
384  finalResult = finalResult && result;
385 
386  result = fP2Manager->Reset();
387  finalResult = finalResult && result;
388 
389  result = fNtupleManager->Reset();
390  finalResult = finalResult && result;
391 
392  return finalResult;
393 }
394 
395 //
396 // protected methods
397 //
398 
399 //_____________________________________________________________________________
401 {
402  G4bool finalResult = true;
403  G4bool result = fFileManager->SetFileName(fileName);
404  finalResult = finalResult && result;
405 
406 #ifdef G4VERBOSE
408  if ( fState.GetVerboseL4() )
409  fState.GetVerboseL4()->Message("open", "analysis file", name);
410 #endif
411 
412  result = fFileManager->OpenFile(fileName);
413  finalResult = finalResult && result;
414 
417 
418 #ifdef G4VERBOSE
419  if ( fState.GetVerboseL1() )
420  fState.GetVerboseL1()->Message("open", "analysis file", name);
421 #endif
422 
423  return finalResult;
424 }
425 
426 //_____________________________________________________________________________
428 {
429 
430  G4bool finalResult = true;
431 
432  if ( ! fgMasterInstance &&
433  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
434  ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
435  ( ! fP2Manager->IsEmpty() ) ) ) {
436  G4ExceptionDescription description;
437  description
438  << " " << "No master G4RootAnalysisManager instance exists."
439  << G4endl
440  << " " << "Histogram/profile data will not be merged.";
441  G4Exception("G4RootAnalysisManager::Write()",
442  "Analysis_W031", JustWarning, description);
443  }
444 
445  // H1
446  G4bool result = WriteH1();
447  finalResult = finalResult && result;
448 
449  // H2
450  result = WriteH2();
451  finalResult = finalResult && result;
452 
453  // H3
454  result = WriteH3();
455  finalResult = finalResult && result;
456 
457  // P1
458  result = WriteP1();
459  finalResult = finalResult && result;
460 
461  // P2
462  result = WriteP2();
463  finalResult = finalResult && result;
464 
465  // File
466  result = fFileManager->WriteFile();
467  finalResult = finalResult && result;
468 
469  // Write ASCII if activated
470  if ( IsAscii() ) {
471  result = WriteAscii(fFileManager->GetFileName());
472  finalResult = finalResult && result;
473  }
474 
475  return finalResult;
476 }
477 
478 //_____________________________________________________________________________
480 {
481  G4bool finalResult = true;
482 
483 #ifdef G4VERBOSE
484  if ( fState.GetVerboseL4() )
486  ->Message("close", "file", fFileManager->GetFullFileName());
487 #endif
488 
489  // reset data
490  G4bool result = Reset();
491  if ( ! result ) {
492  G4ExceptionDescription description;
493  description << " " << "Resetting data failed";
494  G4Exception("G4RootAnalysisManager::Write()",
495  "Analysis_W021", JustWarning, description);
496  }
497  finalResult = finalResult && result;
498 
499  // close file
501 
502  // No files clean-up in sequential mode
503  if ( ! G4Threading::IsMultithreadedApplication() ) return finalResult;
504 
505  // Delete files if empty in MT mode
506  if ( ( fState.GetIsMaster() &&
509  ( ( ! fState.GetIsMaster() ) && fNtupleManager->IsEmpty() ) ) {
510  result = ! std::remove(fFileManager->GetFullFileName());
511  // std::remove returns 0 when success
512  if ( ! result ) {
513  G4ExceptionDescription description;
514  description << " " << "Removing file "
515  << fFileManager->GetFullFileName() << " failed";
516  G4Exception("G4XmlAnalysisManager::CloseFile()",
517  "Analysis_W021", JustWarning, description);
518  }
519  finalResult = finalResult && result;
520 #ifdef G4VERBOSE
521  if ( fState.GetVerboseL1() )
523  ->Message("delete", "empty file", fFileManager->GetFullFileName());
524 #endif
525  }
526  else {
527 #ifdef G4VERBOSE
528  if ( fState.GetVerboseL1() )
530  ->Message("close", "file", fFileManager->GetFullFileName());
531 #endif
532  }
533 
534  return finalResult;
535 }
536 
void SetP1Manager(G4VP1Manager *p1Manager)
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)
virtual G4bool CloseFile()
const std::vector< G4HnInformation * > & GetHnVector() const
G4RootFileManager * fFileManager
G4bool GetActivation() const
G4bool IsEmpty() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
void SetH1Manager(G4VH1Manager *h1Manager)
const std::vector< tools::histo::h1d * > & GetH1Vector() const
G4String name
Definition: TRTMaterials.hh:40
void AddP1Vector(const std::vector< tools::histo::p1d * > &p1Vector)
G4bool IsEmpty() const
const std::vector< G4HnInformation * > & GetHnVector() const
G4bool IsEmpty() const
G4bool IsAscii() const
static G4ThreadLocal G4RootAnalysisManager * fgInstance
virtual G4bool OpenFileImpl(const G4String &fileName)
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
virtual G4bool WriteFile()
void SetH3Manager(G4VH3Manager *h3Manager)
void AddP2Vector(const std::vector< tools::histo::p2d * > &p2Vector)
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
const G4AnalysisVerbose * GetVerboseL3() const
void SetP2Manager(G4VP2Manager *p2Manager)
G4RootNtupleManager * fNtupleManager
G4H2ToolsManager * fH2Manager
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 G4double p2
const G4double p1
const std::vector< tools::histo::h3d * > & GetH3Vector() const
void SetFileManager(G4VFileManager *fileManager)
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:134
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
tools::wroot::directory * GetNtupleDirectory() const
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
void SetH2Manager(G4VH2Manager *h2Manager)
G4H1ToolsManager * fH1Manager
G4bool IsEmpty() const
G4int G4Mutex
Definition: G4Threading.hh:173
const std::vector< tools::histo::p2d * > & GetP2Vector() const
static G4RootAnalysisManager * Instance()
virtual G4bool OpenFile(const G4String &fileName)
G4P2ToolsManager * fP2Manager
#define G4endl
Definition: G4ios.hh:61
virtual G4bool SetFileName(const G4String &fileName)
G4P1ToolsManager * fP1Manager
G4String GetName() const
tools::wroot::directory * GetHistoDirectory() const
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
G4String GetFileName() const
G4AnalysisManagerState fState
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
tools::wroot::directory * GetProfileDirectory() const
const G4AnalysisVerbose * GetVerboseL1() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
G4bool IsEmpty() const
void SetNtupleDirectory(tools::wroot::directory *directory)
const std::vector< tools::histo::h2d * > & GetH2Vector() const
G4H3ToolsManager * fH3Manager
G4bool WriteAscii(const G4String &fileName)
const std::vector< G4HnInformation * > & GetHnVector() const