Geant4  10.01.p03
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 85317 2014-10-27 15:58:15Z 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 "G4H3ToolsManager.hh"
35 #include "G4P1ToolsManager.hh"
36 #include "G4P2ToolsManager.hh"
37 #include "G4XmlNtupleManager.hh"
39 #include "G4Threading.hh"
40 #include "G4AutoLock.hh"
41 
42 #include "tools/waxml/histos"
43 
44 // mutex in a file scope
45 
46 namespace {
47  //Mutex to lock master manager when merging H1 histograms
48  G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
49  //Mutex to lock master manager when merging H1 histograms
50  G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
51  //Mutex to lock master manager when merging H1 histograms
52  G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
53  //Mutex to lock master manager when merging P1 profiles
54  G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
55  //Mutex to lock master manager when merging P2 profiles
56  G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
57 }
58 
61 
62 //_____________________________________________________________________________
64 {
65  if ( fgInstance == 0 ) {
66  G4bool isMaster = ! G4Threading::IsWorkerThread();
67  fgInstance = new G4XmlAnalysisManager(isMaster);
68  }
69 
70  return fgInstance;
71 }
72 
73 //_____________________________________________________________________________
75  : G4VAnalysisManager("Xml", isMaster),
76  fH1Manager(0),
77  fH2Manager(0),
78  fH3Manager(0),
79  fP1Manager(0),
80  fP2Manager(0),
81  fNtupleManager(0),
82  fFileManager(0)
83 {
84  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
85  G4ExceptionDescription description;
86  description
87  << " "
88  << "G4XmlAnalysisManager already exists."
89  << "Cannot create another instance.";
90  G4Exception("G4XmlAnalysisManager::G4XmlAnalysisManager",
91  "Analysis_F001", FatalException, description);
92  }
93  if ( isMaster ) fgMasterInstance = this;
94  fgInstance = this;
95 
96  // Create managers
105  // The managers will be deleted by the base class
106 
107  // Set managers to base class
115 }
116 
117 //_____________________________________________________________________________
119 {
120  if ( fState.GetIsMaster() ) fgMasterInstance = 0;
121  fgInstance = 0;
122 }
123 
124 //
125 // private methods
126 //
127 
128 //_____________________________________________________________________________
130 {
131  const std::vector<tools::histo::h1d*>& h1Vector
132  = fH1Manager->GetH1Vector();
133  const std::vector<G4HnInformation*>& hnVector
134  = fH1Manager->GetHnVector();
135 
136  if ( ! h1Vector.size() ) return true;
137 
138  if ( ! G4Threading::IsWorkerThread() ) {
139 
140  for ( G4int i=0; i<G4int(h1Vector.size()); ++i ) {
141  G4HnInformation* info = hnVector[i];
142  G4bool activation = info->GetActivation();
143  G4String name = info->GetName();
144  // skip writing if activation is enabled and H1 is inactivated
145  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
146  tools::histo::h1d* h1 = h1Vector[i];
147 #ifdef G4VERBOSE
148  if ( fState.GetVerboseL3() )
149  fState.GetVerboseL3()->Message("write", "h1d", name);
150 #endif
151  G4String path = "/";
153  std::ofstream* hnFile = fFileManager->GetHnFile();
154  G4bool result
155  = tools::waxml::write(*hnFile, *h1, path, name);
156  if ( ! result ) {
157  G4ExceptionDescription description;
158  description << " " << "saving histogram " << name << " failed";
159  G4Exception("G4XmlAnalysisManager::Write()",
160  "Analysis_W022", JustWarning, description);
161  return false;
162  }
164  }
165  }
166  else {
167  // The worker manager just adds its histograms to the master
168  // This operation needs a lock
169  G4AutoLock lH1(&mergeH1Mutex);
171  lH1.unlock();
172  }
173 
174  return true;
175 }
176 
177 //_____________________________________________________________________________
179 {
180  const std::vector<tools::histo::h2d*>& h2Vector
181  = fH2Manager->GetH2Vector();
182  const std::vector<G4HnInformation*>& hnVector
183  = fH2Manager->GetHnVector();
184 
185  if ( ! h2Vector.size() ) return true;
186 
187  if ( ! G4Threading::IsWorkerThread() ) {
188 
189  // h2 histograms
190  for ( G4int i=0; i<G4int(h2Vector.size()); ++i ) {
191  G4HnInformation* info = hnVector[i];
192  G4bool activation = info->GetActivation();
193  G4String name = info->GetName();
194  // skip writing if inactivated
195  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
196  tools::histo::h2d* h2 = h2Vector[i];
197 #ifdef G4VERBOSE
198  if ( fState.GetVerboseL3() )
199  fState.GetVerboseL3()->Message("write", "h2d", name);
200 #endif
201  G4String path = "/";
203  std::ofstream* hnFile = fFileManager->GetHnFile();
204  G4bool result
205  = tools::waxml::write(*hnFile, *h2, path, name);
206  if ( ! result ) {
207  G4ExceptionDescription description;
208  description << " " << "saving histogram " << name << " failed";
209  G4Exception("G4XmlAnalysisManager::Write()",
210  "Analysis_W022", JustWarning, description);
211  return false;
212  }
214  }
215  }
216  else {
217  // The worker manager just adds its histograms to the master
218  // This operation needs a lock
219  G4AutoLock lH2(&mergeH2Mutex);
221  lH2.unlock();
222  }
223 
224  return true;
225 }
226 
227 //_____________________________________________________________________________
229 {
230  const std::vector<tools::histo::h3d*>& h3Vector
231  = fH3Manager->GetH3Vector();
232  const std::vector<G4HnInformation*>& hnVector
233  = fH3Manager->GetHnVector();
234 
235  if ( ! h3Vector.size() ) return true;
236 
237  if ( ! G4Threading::IsWorkerThread() ) {
238 
239  // h3 histograms
240  for ( G4int i=0; i<G4int(h3Vector.size()); ++i ) {
241  G4HnInformation* info = hnVector[i];
242  G4bool activation = info->GetActivation();
243  G4String name = info->GetName();
244  // skip writing if inactivated
245  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
246  tools::histo::h3d* h3 = h3Vector[i];
247 #ifdef G4VERBOSE
248  if ( fState.GetVerboseL3() )
249  fState.GetVerboseL3()->Message("write", "h3d", name);
250 #endif
251  G4String path = "/";
253  std::ofstream* hnFile = fFileManager->GetHnFile();
254  G4bool result
255  = tools::waxml::write(*hnFile, *h3, path, name);
256  if ( ! result ) {
257  G4ExceptionDescription description;
258  description << " " << "saving histogram " << name << " failed";
259  G4Exception("G4XmlAnalysisManager::Write()",
260  "Analysis_W022", JustWarning, description);
261  return false;
262  }
264  }
265  }
266  else {
267  // The worker manager just adds its histograms to the master
268  // This operation needs a lock
269  G4AutoLock lH3(&mergeH3Mutex);
271  lH3.unlock();
272  }
273 
274  return true;
275 }
276 
277 //_____________________________________________________________________________
279 {
280  const std::vector<tools::histo::p1d*>& p1Vector
281  = fP1Manager->GetP1Vector();
282  const std::vector<G4HnInformation*>& hnVector
283  = fP1Manager->GetHnVector();
284 
285  if ( ! p1Vector.size() ) return true;
286 
287  if ( ! G4Threading::IsWorkerThread() ) {
288 
289  for ( G4int i=0; i<G4int(p1Vector.size()); ++i ) {
290  G4HnInformation* info = hnVector[i];
291  G4bool activation = info->GetActivation();
292  G4String name = info->GetName();
293  // skip writing if activation is enabled and P1 is inactivated
294  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
295  tools::histo::p1d* p1 = p1Vector[i];
296 #ifdef G4VERBOSE
297  if ( fState.GetVerboseL3() )
298  fState.GetVerboseL3()->Message("write", "p1d", name);
299 #endif
300  G4String path = "/";
302  std::ofstream* hnFile = fFileManager->GetHnFile();
303  G4bool result
304  = tools::waxml::write(*hnFile, *p1, path, name);
305  if ( ! result ) {
306  G4ExceptionDescription description;
307  description << " " << "saving profile " << name << " failed";
308  G4Exception("G4XmlAnalysisManager::Write()",
309  "Analysis_W022", JustWarning, description);
310  return false;
311  }
313  }
314  }
315  else {
316  // The worker manager just adds its profiles to the master
317  // This operation needs a lock
318  G4AutoLock lP1(&mergeP1Mutex);
320  lP1.unlock();
321  }
322 
323  return true;
324 }
325 
326 //_____________________________________________________________________________
328 {
329  const std::vector<tools::histo::p2d*>& p2Vector
330  = fP2Manager->GetP2Vector();
331  const std::vector<G4HnInformation*>& hnVector
332  = fP2Manager->GetHnVector();
333 
334  if ( ! p2Vector.size() ) return true;
335 
336  if ( ! G4Threading::IsWorkerThread() ) {
337 
338  for ( G4int i=0; i<G4int(p2Vector.size()); ++i ) {
339  G4HnInformation* info = hnVector[i];
340  G4bool activation = info->GetActivation();
341  G4String name = info->GetName();
342  // skip writing if activation is enabled and P2 is inactivated
343  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
344  tools::histo::p2d* p2 = p2Vector[i];
345 #ifdef G4VERBOSE
346  if ( fState.GetVerboseL3() )
347  fState.GetVerboseL3()->Message("write", "p2d", name);
348 #endif
349  G4String path = "/";
351  std::ofstream* hnFile = fFileManager->GetHnFile();
352  G4bool result
353  = tools::waxml::write(*hnFile, *p2, path, name);
354  if ( ! result ) {
355  G4ExceptionDescription description;
356  description << " " << "saving profile " << name << " failed";
357  G4Exception("G4XmlAnalysisManager::Write()",
358  "Analysis_W022", JustWarning, description);
359  return false;
360  }
362  }
363  }
364  else {
365  // The worker manager just adds its profiles to the master
366  // This operation needs a lock
367  G4AutoLock lP2(&mergeP2Mutex);
369  lP2.unlock();
370  }
371 
372  return true;
373 }
374 
375 //_____________________________________________________________________________
377 {
378  const std::vector<G4XmlNtupleDescription*>& ntupleVector
380 
381  for ( G4int i=0; i<G4int(ntupleVector.size()); ++i ) {
382  if ( ntupleVector[i]->fNtuple ) ntupleVector[i]->fNtuple->write_trailer();
383  }
384 
385  return true;
386 }
387 
388 //_____________________________________________________________________________
390 {
391  const std::vector<G4XmlNtupleDescription*>& ntupleVector
393 
394  // Close ntuple files
395  std::vector<G4XmlNtupleDescription*>::const_iterator it;
396  for (it = ntupleVector.begin(); it != ntupleVector.end(); it++ ) {
398  }
399 
400  return true;
401 }
402 
403 
404 //_____________________________________________________________________________
406 {
407 // Reset histograms and ntuple
408 
409  G4bool finalResult = true;
410 
411  G4bool result = fH1Manager->Reset();
412  finalResult = finalResult && result;
413 
414  result = fH2Manager->Reset();
415  finalResult = finalResult && result;
416 
417  result = fH3Manager->Reset();
418  finalResult = finalResult && result;
419 
420  result = fP1Manager->Reset();
421  finalResult = finalResult && result;
422 
423  result = fP2Manager->Reset();
424  finalResult = finalResult && result;
425 
426  result = fNtupleManager->Reset();
427  finalResult = finalResult && result;
428 
429  return finalResult;
430 }
431 
432 //
433 // protected methods
434 //
435 
436 //_____________________________________________________________________________
438 {
439  G4bool finalResult = true;
440  G4bool result = fFileManager->SetFileName(fileName);
441  finalResult = finalResult && result;
442 
443 #ifdef G4VERBOSE
445  if ( fState.GetVerboseL4() ) {
446  fState.GetVerboseL4()->Message("open", "analysis file", name);
447  }
448 #endif
449 
450  // Only lock file name in file manager
451  result = fFileManager->OpenFile(fileName);
452  finalResult = finalResult && result;
453 
454  // Create histograms file (on master)
455  if ( fState.GetIsMaster() ) {
456  result = fFileManager->CreateHnFile();
457  finalResult = finalResult && result;
458  }
459 
460  // Create ntuples if they are booked
461  // (The files will be created with creating ntuples)
463 
464 #ifdef G4VERBOSE
465  if ( fState.GetVerboseL1() )
466  fState.GetVerboseL1()->Message("open", "analysis file", name);
467 #endif
468 
469  return finalResult;
470 }
471 
472 //_____________________________________________________________________________
474 {
475  G4bool finalResult = true;
476 
477 #ifdef G4VERBOSE
479  if ( fState.GetVerboseL4() )
480  fState.GetVerboseL4()->Message("write", "files", name);
481 #endif
482 
483  // ntuples
484  WriteNtuple();
485 
486  if ( ! fgMasterInstance &&
487  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
488  ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
489  ( ! fP2Manager->IsEmpty() ) ) ) {
490 
491  G4ExceptionDescription description;
492  description
493  << " " << "No master G4XmlAnalysisManager instance exists."
494  << G4endl
495  << " " << "Histogram data will not be merged.";
496  G4Exception("G4XmlAnalysisManager::Write()",
497  "Analysis_W031", JustWarning, description);
498 
499  // Create Hn file per thread
500  G4bool result = fFileManager->CreateHnFile();
501  if ( ! result ) return false;
502  }
503 
504  // H1
505  G4bool result = WriteH1();
506  finalResult = finalResult && result;
507 
508  // H2
509  result = WriteH2();
510  finalResult = finalResult && result;
511 
512  // H3
513  result = WriteH3();
514  finalResult = finalResult && result;
515 
516  // P1
517  result = WriteP1();
518  finalResult = finalResult && result;
519 
520  // P2
521  result = WriteP2();
522  finalResult = finalResult && result;
523 
524  // Write ASCII if activated
525  if ( IsAscii() ) {
526  result = WriteAscii(fFileManager->GetFileName());
527  finalResult = finalResult && result;
528  }
529 
530 #ifdef G4VERBOSE
531  if ( fState.GetVerboseL1() )
533  ->Message("write", "file", fFileManager->GetFullFileName(), finalResult);
534 #endif
535 
536  return finalResult;
537 }
538 
539 //_____________________________________________________________________________
541 {
542  G4bool finalResult = true;
543 
544 #ifdef G4VERBOSE
545  if ( fState.GetVerboseL4() )
546  fState.GetVerboseL4()->Message("close", "files", "");
547 #endif
548 
549  // Unlock file name only
550  G4bool result = fFileManager->CloseFile();
551  finalResult = finalResult && result;
552 
553  // Close Hn file
554  result = fFileManager->CloseHnFile();
555  finalResult = finalResult && result;
556 
557  // Close ntuple files
559  ( ! fState.GetIsMaster() ) ) {
560  // In sequential mode or in MT mode only on workers
561  result = CloseNtupleFiles();
562  finalResult = finalResult && result;
563  }
564 
565  // reset data
566  result = Reset();
567  if ( ! result ) {
568  G4ExceptionDescription description;
569  description << " " << "Resetting data failed";
570  G4Exception("G4XmlAnalysisManager::CloseFile()",
571  "Analysis_W021", JustWarning, description);
572  }
573  finalResult = finalResult && result;
574 
575  // delete files if empty
576  // (ntuple files are created only if an ntuple is created)
577  if ( fFileManager->GetHnFile() &&
579  fP1Manager->IsEmpty() && fP2Manager->IsEmpty() ) {
580  result = ! std::remove(fFileManager->GetFullFileName());
581  // std::remove returns 0 when success
582  if ( ! result ) {
583  G4ExceptionDescription description;
584  description << " " << "Removing file "
585  << fFileManager->GetFullFileName() << " failed";
586  G4Exception("G4XmlAnalysisManager::CloseFile()",
587  "Analysis_W021", JustWarning, description);
588  }
589  finalResult = finalResult && result;
590 #ifdef G4VERBOSE
591  if ( fState.GetVerboseL1() )
593  ->Message("delete", "empty file", fFileManager->GetFullFileName());
594 #endif
595  }
596  else {
597 #ifdef G4VERBOSE
598  if ( fState.GetVerboseL2() )
600  ->Message("close", "files", "");
601 #endif
602  }
603 
604  return finalResult;
605 }
void SetP1Manager(G4VP1Manager *p1Manager)
static G4XmlAnalysisManager * fgMasterInstance
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
void AddH3Vector(const std::vector< tools::histo::h3d * > &h3Vector)
G4XmlFileManager * fFileManager
virtual G4bool CloseFileImpl()
const std::vector< G4HnInformation * > & GetHnVector() const
G4bool GetActivation() const
G4bool IsEmpty() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetProfileDirectoryName() const
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
G4H3ToolsManager * fH3Manager
static G4XmlAnalysisManager * Instance()
G4H1ToolsManager * fH1Manager
G4H2ToolsManager * fH2Manager
G4XmlAnalysisManager(G4bool isMaster=true)
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static G4ThreadLocal G4XmlAnalysisManager * fgInstance
const G4AnalysisVerbose * GetVerboseL2() const
void SetH3Manager(G4VH3Manager *h3Manager)
void AddP2Vector(const std::vector< tools::histo::p2d * > &p2Vector)
void LockProfileDirectoryName()
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
void SetFileManager(G4XmlFileManager *fileManager)
const std::vector< G4XmlNtupleDescription * > & GetNtupleDescriptionVector() const
const G4AnalysisVerbose * GetVerboseL3() const
virtual G4bool OpenFileImpl(const G4String &fileName)
void SetP2Manager(G4VP2Manager *p2Manager)
const G4AnalysisVerbose * GetVerboseL4() const
G4String GetHistoDirectoryName() const
const std::vector< tools::histo::p1d * > & GetP1Vector() const
G4P2ToolsManager * fP2Manager
bool G4bool
Definition: G4Types.hh:79
const std::vector< G4HnInformation * > & GetHnVector() const
virtual G4bool CloseFile()
virtual G4bool OpenFile(const G4String &fileName)
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
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
void SetH2Manager(G4VH2Manager *h2Manager)
G4bool IsEmpty() const
std::ofstream * GetHnFile() const
G4XmlNtupleManager * fNtupleManager
G4int G4Mutex
Definition: G4Threading.hh:173
G4P1ToolsManager * fP1Manager
G4bool CloseNtupleFile(G4XmlNtupleDescription *ntupleDescription)
G4String & append(const G4String &)
const std::vector< tools::histo::p2d * > & GetP2Vector() const
#define G4endl
Definition: G4ios.hh:61
virtual G4bool SetFileName(const G4String &fileName)
G4String GetName() const
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
void LockHistoDirectoryName()
G4String GetFileName() const
G4AnalysisManagerState fState
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisVerbose * GetVerboseL1() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
G4bool IsEmpty() const
const std::vector< tools::histo::h2d * > & GetH2Vector() const
G4bool WriteAscii(const G4String &fileName)
const std::vector< G4HnInformation * > & GetHnVector() const