Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 74257 2013-10-02 14:24:55Z 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 "G4RootNtupleManager.hh"
35 #include "G4AnalysisVerbose.hh"
36 #include "G4Threading.hh"
37 #include "G4AutoLock.hh"
38 
39 #include "tools/wroot/to"
40 #include "tools/wroot/file"
41 
42 #include <iostream>
43 #include <cstdio>
44 
45 // mutex in a file scope
46 
47 namespace {
48  //Mutex to lock master manager when merging H1 histograms
49  G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
50  //Mutex to lock master manager when merging H1 histograms
51  G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
52 }
53 
54 G4RootAnalysisManager* G4RootAnalysisManager::fgMasterInstance = 0;
55 G4ThreadLocal G4RootAnalysisManager* G4RootAnalysisManager::fgInstance = 0;
56 
57 //_____________________________________________________________________________
59 {
60  if ( fgInstance == 0 ) {
61  G4bool isMaster = ! G4Threading::IsWorkerThread();
62  fgInstance = new G4RootAnalysisManager(isMaster);
63  }
64 
65  return fgInstance;
66 }
67 
68 //_____________________________________________________________________________
70  : G4VAnalysisManager("Root", isMaster),
71  fH1Manager(0),
72  fH2Manager(0),
73  fNtupleManager(0),
74  fFileManager(0)
75 {
76  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
77  G4ExceptionDescription description;
78  description
79  << " "
80  << "G4RootAnalysisManager already exists."
81  << "Cannot create another instance.";
82  G4Exception("G4RootAnalysisManager::G4RootAnalysisManager()",
83  "Analysis_F001", FatalException, description);
84  }
85  if ( isMaster ) fgMasterInstance = this;
86  fgInstance = this;
87 
88  // Create managers
89  fH1Manager = new G4H1ToolsManager(fState);
90  fH2Manager = new G4H2ToolsManager(fState);
91  fNtupleManager = new G4RootNtupleManager(fState);
92  fFileManager = new G4RootFileManager(fState);
93  // The managers will be deleted by the base class
94 
95  // Set managers to base class
96  SetH1Manager(fH1Manager);
97  SetH2Manager(fH2Manager);
98  SetNtupleManager(fNtupleManager);
99  SetFileManager(fFileManager);
100 }
101 
102 //_____________________________________________________________________________
104 {
105  if ( fState.GetIsMaster() ) fgMasterInstance = 0;
106  fgInstance = 0;
107 }
108 
109 //
110 // private methods
111 //
112 
113 //_____________________________________________________________________________
114 G4bool G4RootAnalysisManager::WriteH1()
115 {
116  const std::vector<tools::histo::h1d*>& h1Vector
117  = fH1Manager->GetH1Vector();
118  const std::vector<G4HnInformation*>& hnVector
119  = fH1Manager->GetHnVector();
120 
121  if ( ! h1Vector.size() ) return true;
122 
123  if ( ! G4Threading::IsWorkerThread() ) {
124 
125  for ( G4int i=0; i<G4int(h1Vector.size()); ++i ) {
126  G4HnInformation* info = hnVector[i];
127  G4bool activation = info->fActivation;
128  G4String name = info->fName;
129  // skip writing if activation is enabled and H1 is inactivated
130  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
131  tools::histo::h1d* h1 = h1Vector[i];
132 #ifdef G4VERBOSE
133  if ( fState.GetVerboseL3() )
134  fState.GetVerboseL3()->Message("write", "h1d", name);
135 #endif
136  tools::wroot::directory* histoDirectory
137  = fFileManager->GetHistoDirectory();
138  G4bool result
139  = to(*histoDirectory, *h1, name);
140  if ( ! result ) {
141  G4ExceptionDescription description;
142  description << " " << "saving histogram " << name << " failed";
143  G4Exception("G4RootAnalysisManager::Write()",
144  "Analysis_W003", JustWarning, description);
145  return false;
146  }
147  }
148  }
149  else {
150  // The worker manager just adds its histograms to the master
151  // This operation needs a lock
152  G4AutoLock lH1(&mergeH1Mutex);
153  fgMasterInstance->fH1Manager->AddH1Vector(h1Vector);
154  lH1.unlock();
155  }
156 
157  return true;
158 }
159 
160 //_____________________________________________________________________________
161 G4bool G4RootAnalysisManager::WriteH2()
162 {
163  const std::vector<tools::histo::h2d*>& h2Vector
164  = fH2Manager->GetH2Vector();
165  const std::vector<G4HnInformation*>& hnVector
166  = fH2Manager->GetHnVector();
167 
168  if ( ! h2Vector.size() ) return true;
169 
170  if ( ! G4Threading::IsWorkerThread() ) {
171 
172  for ( G4int i=0; i<G4int(h2Vector.size()); ++i ) {
173  G4HnInformation* info = hnVector[i];
174  G4bool activation = info->fActivation;
175  G4String name = info->fName;
176  // skip writing if inactivated
177  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
178  tools::histo::h2d* h2 = h2Vector[i];
179 #ifdef G4VERBOSE
180  if ( fState.GetVerboseL3() )
181  fState.GetVerboseL3()->Message("write", "h2d", name);
182 #endif
183  tools::wroot::directory* histoDirectory
184  = fFileManager->GetHistoDirectory();
185  G4bool result
186  = to(*histoDirectory, *h2, name);
187  if ( ! result ) {
188  G4ExceptionDescription description;
189  description << " " << "saving histogram " << name << " failed";
190  G4Exception("G4RootAnalysisManager::Write()",
191  "Analysis_W003", JustWarning, description);
192  return false;
193  }
194  }
195  }
196  else {
197  // The worker manager just adds its histograms to the master
198  // This operation needs a lock
199  G4AutoLock lH2(&mergeH2Mutex);
200  fgMasterInstance->fH2Manager->AddH2Vector(h2Vector);
201  lH2.unlock();
202  }
203 
204  return true;
205 }
206 
207 //_____________________________________________________________________________
208 G4bool G4RootAnalysisManager::Reset()
209 {
210 // Reset histograms and ntuple
211 
212  G4bool finalResult = true;
213 
214  G4bool result = fH1Manager->Reset();
215  finalResult = finalResult && result;
216 
217  result = fH2Manager->Reset();
218  finalResult = finalResult && result;
219 
220  result = fNtupleManager->Reset();
221  finalResult = finalResult && result;
222 
223  return finalResult;
224 }
225 
226 //
227 // protected methods
228 //
229 
230 //_____________________________________________________________________________
232 {
233  G4bool finalResult = true;
234  G4bool result = fFileManager->SetFileName(fileName);
235  finalResult = finalResult && result;
236 
237 #ifdef G4VERBOSE
238  G4String name = fFileManager->GetFullFileName();
239  if ( fState.GetVerboseL4() )
240  fState.GetVerboseL4()->Message("open", "analysis file", name);
241 #endif
242 
243  result = fFileManager->OpenFile(fileName);
244  finalResult = finalResult && result;
245 
246  fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
247  fNtupleManager->CreateNtuplesFromBooking();
248 
249 #ifdef G4VERBOSE
250  if ( fState.GetVerboseL1() )
251  fState.GetVerboseL1()->Message("open", "analysis file", name);
252 #endif
253 
254  return finalResult;
255 }
256 
257 //_____________________________________________________________________________
259 {
260 
261  G4bool finalResult = true;
262 
263  if ( ! fgMasterInstance &&
264  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ) ) {
265  G4ExceptionDescription description;
266  description
267  << " " << "No master G4RootAnalysisManager instance exists."
268  << G4endl
269  << " " << "Histogram data will not be merged.";
270  G4Exception("G4RootAnalysisManager::Write()",
271  "Analysis_W014", JustWarning, description);
272  }
273 
274  // H1
275  G4bool result = WriteH1();
276  finalResult = finalResult && result;
277 
278  // H2
279  result = WriteH2();
280  finalResult = finalResult && result;
281 
282  // File
283  result = fFileManager->WriteFile();
284  finalResult = finalResult && result;
285 
286  // Write ASCII if activated
287  if ( IsAscii() ) {
288  result = WriteAscii(fFileManager->GetFileName());
289  finalResult = finalResult && result;
290  }
291 
292  return finalResult;
293 }
294 
295 //_____________________________________________________________________________
297 {
298  G4bool result = true;
299 
300 #ifdef G4VERBOSE
301  if ( fState.GetVerboseL4() )
303  ->Message("close", "file", fFileManager->GetFullFileName());
304 #endif
305 
306  // reset data
307  result = Reset();
308  if ( ! result ) {
309  G4ExceptionDescription description;
310  description << " " << "Resetting data failed";
311  G4Exception("G4RootAnalysisManager::Write()",
312  "Analysis_W002", JustWarning, description);
313  }
314 
315  // close file
316  fFileManager->CloseFile();
317 
318  // No files clean-up in sequential mode
319  if ( ! fState.IsMT() ) return result;
320 
321  // Delete files if empty in MT mode
322  if ( ( fState.GetIsMaster() &&
323  fH1Manager->IsEmpty() && fH2Manager->IsEmpty() ) ||
324  ( ( ! fState.GetIsMaster() ) && fNtupleManager->IsEmpty() ) ) {
325  std::remove(fFileManager->GetFullFileName());
326 #ifdef G4VERBOSE
327  if ( fState.GetVerboseL1() )
329  ->Message("delete", "empty file", fFileManager->GetFullFileName());
330 #endif
331  }
332  else {
333 #ifdef G4VERBOSE
334  if ( fState.GetVerboseL1() )
336  ->Message("close", "file", fFileManager->GetFullFileName());
337 #endif
338  }
339 
340  return result;
341 }
342 
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool CloseFile()
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetFullFileName() const
void SetH1Manager(G4VH1Manager *h1Manager)
const std::vector< tools::histo::h1d * > & GetH1Vector() const
G4bool IsEmpty() const
G4bool IsAscii() const
const XML_Char * name
virtual G4bool OpenFileImpl(const G4String &fileName)
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:158
virtual G4bool WriteFile()
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
const G4AnalysisVerbose * GetVerboseL3() const
const G4AnalysisVerbose * GetVerboseL4() const
G4String GetFileName() const
bool G4bool
Definition: G4Types.hh:79
G4RootAnalysisManager(G4bool isMaster=true)
const std::vector< G4HnInformation * > & GetHnVector() const
void SetFileManager(G4VFileManager *fileManager)
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:104
void SetH2Manager(G4VH2Manager *h2Manager)
G4int G4Mutex
Definition: G4Threading.hh:156
static G4RootAnalysisManager * Instance()
virtual G4bool OpenFile(const G4String &fileName)
const XML_Char XML_Encoding * info
#define G4endl
Definition: G4ios.hh:61
G4bool SetFileName(const G4String &fileName)
tools::wroot::directory * GetHistoDirectory() const
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
G4AnalysisManagerState fState
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
G4bool WriteAscii(const G4String &fileName)