Geant4  10.00.p01
G4XmlAnalysisManager.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: G4XmlAnalysisManager.cc 74257 2013-10-02 14:24:55Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4XmlAnalysisManager.hh"
31 #include "G4XmlFileManager.hh"
32 #include "G4H1ToolsManager.hh"
33 #include "G4H2ToolsManager.hh"
34 #include "G4XmlNtupleManager.hh"
35 #include "G4Threading.hh"
36 #include "G4AutoLock.hh"
37 
38 #include "tools/waxml/histos"
39 
40 // mutex in a file scope
41 
42 namespace {
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 }
48 
51 
52 //_____________________________________________________________________________
54 {
55  if ( fgInstance == 0 ) {
56  G4bool isMaster = ! G4Threading::IsWorkerThread();
57  fgInstance = new G4XmlAnalysisManager(isMaster);
58  }
59 
60  return fgInstance;
61 }
62 
63 //_____________________________________________________________________________
65  : G4VAnalysisManager("Xml", isMaster),
66  fH1Manager(0),
67  fH2Manager(0),
68  fNtupleManager(0),
69  fFileManager(0)
70 {
71  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
72  G4ExceptionDescription description;
73  description
74  << " "
75  << "G4XmlAnalysisManager already exists."
76  << "Cannot create another instance.";
77  G4Exception("G4XmlAnalysisManager::G4XmlAnalysisManager",
78  "Analysis_F001", FatalException, description);
79  }
80  if ( isMaster ) fgMasterInstance = this;
81  fgInstance = this;
82 
83  // Create managers
89  // The managers will be deleted by the base class
90 
91  // Set managers to base class
96 }
97 
98 //_____________________________________________________________________________
100 {
101  if ( fState.GetIsMaster() ) fgMasterInstance = 0;
102  fgInstance = 0;
103 }
104 
105 //
106 // private methods
107 //
108 
109 //_____________________________________________________________________________
111 {
112  const std::vector<tools::histo::h1d*>& h1Vector
113  = fH1Manager->GetH1Vector();
114  const std::vector<G4HnInformation*>& hnVector
115  = fH1Manager->GetHnVector();
116 
117  if ( ! h1Vector.size() ) return true;
118 
119  if ( ! G4Threading::IsWorkerThread() ) {
120 
121  for ( G4int i=0; i<G4int(h1Vector.size()); ++i ) {
122  G4HnInformation* info = hnVector[i];
123  G4bool activation = info->fActivation;
124  G4String name = info->fName;
125  // skip writing if activation is enabled and H1 is inactivated
126  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
127  tools::histo::h1d* h1 = h1Vector[i];
128 #ifdef G4VERBOSE
129  if ( fState.GetVerboseL3() )
130  fState.GetVerboseL3()->Message("write", "h1d", name);
131 #endif
132  G4String path = "/";
134  std::ofstream* hnFile = fFileManager->GetHnFile();
135  G4bool result
136  = tools::waxml::write(*hnFile, *h1, path, name);
137  if ( ! result ) {
138  G4ExceptionDescription description;
139  description << " " << "saving histogram " << name << " failed";
140  G4Exception("G4XmlAnalysisManager::Write()",
141  "Analysis_W003", JustWarning, description);
142  return false;
143  }
145  }
146  }
147  else {
148  // The worker manager just adds its histograms to the master
149  // This operation needs a lock
150  G4AutoLock lH1(&mergeH1Mutex);
152  lH1.unlock();
153  }
154 
155  return true;
156 }
157 
158 //_____________________________________________________________________________
160 {
161  const std::vector<tools::histo::h2d*>& h2Vector
162  = fH2Manager->GetH2Vector();
163  const std::vector<G4HnInformation*>& hnVector
164  = fH2Manager->GetHnVector();
165 
166  if ( ! h2Vector.size() ) return true;
167 
168  if ( ! G4Threading::IsWorkerThread() ) {
169 
170  // h2 histograms
171  for ( G4int i=0; i<G4int(h2Vector.size()); ++i ) {
172  G4HnInformation* info = hnVector[i];
173  G4bool activation = info->fActivation;
174  G4String name = info->fName;
175  // skip writing if inactivated
176  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
177  tools::histo::h2d* h2 = h2Vector[i];
178 #ifdef G4VERBOSE
179  if ( fState.GetVerboseL3() )
180  fState.GetVerboseL3()->Message("write", "h2d", name);
181 #endif
182  G4String path = "/";
184  std::ofstream* hnFile = fFileManager->GetHnFile();
185  G4bool result
186  = tools::waxml::write(*hnFile, *h2, path, name);
187  if ( ! result ) {
188  G4ExceptionDescription description;
189  description << " " << "saving histogram " << name << " failed";
190  G4Exception("G4XmlAnalysisManager::Write()",
191  "Analysis_W003", JustWarning, description);
192  return false;
193  }
195  }
196  }
197  else {
198  // The worker manager just adds its histograms to the master
199  // This operation needs a lock
200  G4AutoLock lH2(&mergeH2Mutex);
202  lH2.unlock();
203  }
204 
205  return true;
206 }
207 
208 //_____________________________________________________________________________
210 {
211  const std::vector<G4XmlNtupleDescription*>& ntupleVector
213 
214  for ( G4int i=0; i<G4int(ntupleVector.size()); ++i ) {
215  if ( ntupleVector[i]->fNtuple ) ntupleVector[i]->fNtuple->write_trailer();
216  }
217 
218  return true;
219 }
220 
221 //_____________________________________________________________________________
223 {
224  const std::vector<G4XmlNtupleDescription*>& ntupleVector
226 
227  // Close ntuple files
228  std::vector<G4XmlNtupleDescription*>::const_iterator it;
229  for (it = ntupleVector.begin(); it != ntupleVector.end(); it++ ) {
231  }
232 
233  return true;
234 }
235 
236 
237 //_____________________________________________________________________________
239 {
240 // Reset histograms and ntuple
241 
242  G4bool finalResult = true;
243 
244  G4bool result = fH1Manager->Reset();
245  finalResult = finalResult && result;
246 
247  result = fH2Manager->Reset();
248  finalResult = finalResult && result;
249 
250  result = fNtupleManager->Reset();
251  finalResult = finalResult && result;
252 
253  return finalResult;
254 }
255 
256 //
257 // protected methods
258 //
259 
260 //_____________________________________________________________________________
262 {
263  G4bool finalResult = true;
264  G4bool result = fFileManager->SetFileName(fileName);
265  finalResult = finalResult && result;
266 
267 #ifdef G4VERBOSE
269  if ( fState.GetVerboseL4() ) {
270  fState.GetVerboseL4()->Message("open", "analysis file", name);
271  }
272 #endif
273 
274  // Only lock file name in file manager
275  result = fFileManager->OpenFile(fileName);
276  finalResult = finalResult && result;
277 
278  // Create histograms file (on master)
279  if ( fState.GetIsMaster() ) {
280  result = fFileManager->CreateHnFile();
281  finalResult = finalResult && result;
282  }
283 
284  // Create ntuples if they are booked
285  // (The files will be created with creating ntuples)
287 
288 #ifdef G4VERBOSE
289  if ( fState.GetVerboseL1() )
290  fState.GetVerboseL1()->Message("open", "analysis file", name);
291 #endif
292 
293  return finalResult;
294 }
295 
296 //_____________________________________________________________________________
298 {
299  G4bool finalResult = true;
300 
301 #ifdef G4VERBOSE
303  if ( fState.GetVerboseL4() )
304  fState.GetVerboseL4()->Message("write", "files", name);
305 #endif
306 
307  // ntuples
308  WriteNtuple();
309 
310  if ( ! fgMasterInstance &&
311  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ) ) {
312 
313  G4ExceptionDescription description;
314  description
315  << " " << "No master G4XmlAnalysisManager instance exists."
316  << G4endl
317  << " " << "Histogram data will not be merged.";
318  G4Exception("G4XmlAnalysisManager::Write()",
319  "Analysis_W014", JustWarning, description);
320 
321  // Create Hn file per thread
322  G4bool result = fFileManager->CreateHnFile();
323  if ( ! result ) return false;
324  }
325 
326  // H1
327  G4bool result = WriteH1();
328  finalResult = finalResult && result;
329 
330  // H2
331  result = WriteH2();
332  finalResult = finalResult && result;
333 
334  // Write ASCII if activated
335  if ( IsAscii() ) {
336  result = WriteAscii(fFileManager->GetFileName());
337  finalResult = finalResult && result;
338  }
339 
340 #ifdef G4VERBOSE
341  if ( fState.GetVerboseL1() )
343  ->Message("write", "files", fFileManager->GetFullFileName(), finalResult);
344 #endif
345 
346  return finalResult;
347 }
348 
349 //_____________________________________________________________________________
351 {
352  G4bool finalResult = true;
353 
354 #ifdef G4VERBOSE
355  if ( fState.GetVerboseL4() )
356  fState.GetVerboseL4()->Message("close", "files", "");
357 #endif
358 
359  // Unlock file name only
360  G4bool result = fFileManager->CloseFile();
361  finalResult = finalResult && result;
362 
363  // Close Hn file
364  result = fFileManager->CloseHnFile();
365  finalResult = finalResult && result;
366 
367  // Close ntuple files
368  if ( ( ! G4AnalysisManagerState::IsMT() ) || ( ! fState.GetIsMaster() ) ) {
369  // In sequential mode or in MT mode only on workers
370  result = CloseNtupleFiles();
371  finalResult = finalResult && result;
372  }
373 
374  // reset data
375  result = Reset();
376  if ( ! result ) {
377  G4ExceptionDescription description;
378  description << " " << "Resetting data failed";
379  G4Exception("G4XmlAnalysisManager::CloseFile()",
380  "Analysis_W002", JustWarning, description);
381  }
382  finalResult = finalResult && result;
383 
384  // delete files if empty
385  // (ntuple files are created only if an ntuple is created)
386  if ( fFileManager->GetHnFile() &&
387  fH1Manager->IsEmpty() && fH2Manager->IsEmpty() ) {
388  std::remove(fFileManager->GetFullFileName());
389 #ifdef G4VERBOSE
390  if ( fState.GetVerboseL1() )
392  ->Message("delete", "empty file", fFileManager->GetFullFileName());
393 #endif
394  }
395  else {
396 #ifdef G4VERBOSE
397  if ( fState.GetVerboseL1() )
399  ->Message("close", "files", fFileManager->GetFullFileName());
400 #endif
401  }
402 
403  return finalResult;
404 }
static G4XmlAnalysisManager * fgMasterInstance
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4XmlFileManager * fFileManager
virtual G4bool CloseFileImpl()
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetFullFileName() const
void SetH1Manager(G4VH1Manager *h1Manager)
const std::vector< tools::histo::h1d * > & GetH1Vector() const
G4String name
Definition: TRTMaterials.hh:40
G4bool IsEmpty() const
G4bool IsAscii() const
static G4XmlAnalysisManager * Instance()
G4H1ToolsManager * fH1Manager
G4H2ToolsManager * fH2Manager
G4XmlAnalysisManager(G4bool isMaster=true)
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:158
static G4ThreadLocal G4XmlAnalysisManager * fgInstance
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
void SetFileManager(G4XmlFileManager *fileManager)
const G4AnalysisVerbose * GetVerboseL3() const
const std::vector< G4XmlNtupleDescription * > & GetNtupleVector() const
virtual G4bool OpenFileImpl(const G4String &fileName)
const G4AnalysisVerbose * GetVerboseL4() const
G4String GetFileName() const
G4String GetHistoDirectoryName() const
bool G4bool
Definition: G4Types.hh:79
const std::vector< G4HnInformation * > & GetHnVector() const
virtual G4bool CloseFile()
virtual G4bool OpenFile(const G4String &fileName)
void SetFileManager(G4VFileManager *fileManager)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:104
void SetH2Manager(G4VH2Manager *h2Manager)
std::ofstream * GetHnFile() const
G4XmlNtupleManager * fNtupleManager
G4int G4Mutex
Definition: G4Threading.hh:156
G4bool CloseNtupleFile(G4XmlNtupleDescription *ntupleDescription)
G4String & append(const G4String &)
#define G4endl
Definition: G4ios.hh:61
G4bool SetFileName(const G4String &fileName)
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
const std::vector< tools::histo::h2d * > & GetH2Vector() const
G4bool WriteAscii(const G4String &fileName)