Geant4  10.01.p02
G4CsvAnalysisManager.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: G4CsvAnalysisManager.cc 85317 2014-10-27 15:58:15Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4CsvAnalysisManager.hh"
31 #include "G4CsvFileManager.hh"
32 #include "G4H1ToolsManager.hh"
33 #include "G4H2ToolsManager.hh"
34 #include "G4H3ToolsManager.hh"
35 #include "G4P1ToolsManager.hh"
36 #include "G4P2ToolsManager.hh"
37 #include "G4CsvNtupleManager.hh"
38 #include "G4AnalysisVerbose.hh"
40 #include "G4UnitsTable.hh"
41 #include "G4Threading.hh"
42 #include "G4AutoLock.hh"
43 
44 #include "tools/wcsv_histo"
45 
46 #include <iostream>
47 
48 // mutex in a file scope
49 
50 namespace {
51  //Mutex to lock master manager when merging H1 histograms
52  G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
53  //Mutex to lock master manager when merging H1 histograms
54  G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
55  //Mutex to lock master manager when merging H1 histograms
56  G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
57  //Mutex to lock master manager when merging P1 profiles
58  G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
59  //Mutex to lock master manager when merging P2 profiles
60  G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
61 }
62 
65 
66 //_____________________________________________________________________________
68 {
69  if ( fgInstance == 0 ) {
70  G4bool isMaster = ! G4Threading::IsWorkerThread();
71  fgInstance = new G4CsvAnalysisManager(isMaster);
72  }
73 
74  return fgInstance;
75 }
76 
77 //_____________________________________________________________________________
79  : G4VAnalysisManager("Csv", isMaster),
80  fH1Manager(0),
81  fH2Manager(0),
82  fH3Manager(0),
83  fP1Manager(0),
84  fP2Manager(0),
85  fNtupleManager(0),
86  fFileManager(0)
87 {
88  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
89  G4ExceptionDescription description;
90  description << " "
91  << "G4CsvAnalysisManager already exists."
92  << "Cannot create another instance.";
93  G4Exception("G4CsvAnalysisManager::G4CsvAnalysisManager()",
94  "Analysis_F001", FatalException, description);
95  }
96 
97  if ( isMaster ) fgMasterInstance = this;
98  fgInstance = this;
99 
100  // 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<G4CsvNtupleDescription*>& ntupleVector
137 
138  // Close ntuple files
139  std::vector<G4CsvNtupleDescription*>::const_iterator it;
140  for (it = ntupleVector.begin(); it != ntupleVector.end(); it++ ) {
142  }
143 
144  return true;
145 }
146 
147 
148 //
149 // protected methods
150 //
151 
152 //
153 // private methods
154 //
155 
156 //_____________________________________________________________________________
158 {
159  const std::vector<tools::histo::h1d*>& h1Vector
160  = fH1Manager->GetH1Vector();
161  const std::vector<G4HnInformation*>& hnVector
162  = fH1Manager->GetHnVector();
163 
164  if ( ! h1Vector.size() ) return true;
165 
166  if ( ! G4Threading::IsWorkerThread() ) {
167 
168  for ( G4int i=0; i<G4int(h1Vector.size()); ++i ) {
169  G4HnInformation* info = hnVector[i];
170  G4bool activation = info->GetActivation();
171  G4String name = info->GetName();
172  // skip writing if activation is enabled and H1 is inactivated
173  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
174  tools::histo::h1d* h1 = h1Vector[i];
175  G4String fileName = fFileManager->GetHnFileName("h1", name);
176  std::ofstream hnFile(fileName);
177 
178  G4bool result
179  = tools::wcsv::hto(hnFile, h1->s_cls(), *h1);
180  if ( ! result ) {
181  G4ExceptionDescription description;
182  description << " " << "saving histogram " << name << " failed";
183  G4Exception("G4CsvAnalysisManager::Write()",
184  "Analysis_W022", JustWarning, description);
185  return false;
186  }
187  hnFile.close();
188 #ifdef G4VERBOSE
189  if ( fState.GetVerboseL1() )
190  fState.GetVerboseL1()->Message("write", "file", fileName);
191 #endif
193  }
194  }
195  else {
196  // The worker manager just adds its histograms to the master
197  // This operation needs a lock
198  G4AutoLock lH1(&mergeH1Mutex);
200  lH1.unlock();
201  }
202 
203  return true;
204 }
205 
206 //_____________________________________________________________________________
208 {
209  const std::vector<tools::histo::h2d*>& h2Vector
210  = fH2Manager->GetH2Vector();
211  const std::vector<G4HnInformation*>& hnVector
212  = fH2Manager->GetHnVector();
213 
214  if ( ! h2Vector.size() ) return true;
215 
216  if ( ! G4Threading::IsWorkerThread() ) {
217 
218  // h2 histograms
219  for ( G4int i=0; i<G4int(h2Vector.size()); ++i ) {
220  G4HnInformation* info = hnVector[i];
221  G4bool activation = info->GetActivation();
222  G4String name = info->GetName();
223  // skip writing if inactivated
224  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
225  tools::histo::h2d* h2 = h2Vector[i];
226  G4String fileName = fFileManager->GetHnFileName("h2", name);
227  std::ofstream hnFile(fileName);
228  G4bool result
229  = tools::wcsv::hto(hnFile, h2->s_cls(), *h2);
230  if ( ! result ) {
231  G4ExceptionDescription description;
232  description << " " << "saving histogram " << name << " failed";
233  G4Exception("G4CsvAnalysisManager::Write()",
234  "Analysis_W022", JustWarning, description);
235  return false;
236  }
237  hnFile.close();
238 #ifdef G4VERBOSE
239  if ( fState.GetVerboseL1() )
240  fState.GetVerboseL1()->Message("write", "file", fileName);
241 #endif
243  }
244  }
245  else {
246  // The worker manager just adds its histograms to the master
247  // This operation needs a lock
248  G4AutoLock lH2(&mergeH2Mutex);
250  lH2.unlock();
251  }
252 
253  return true;
254 }
255 
256 //_____________________________________________________________________________
258 {
259  const std::vector<tools::histo::h3d*>& h3Vector
260  = fH3Manager->GetH3Vector();
261  const std::vector<G4HnInformation*>& hnVector
262  = fH3Manager->GetHnVector();
263 
264  if ( ! h3Vector.size() ) return true;
265 
266  if ( ! G4Threading::IsWorkerThread() ) {
267 
268  // h3 histograms
269  for ( G4int i=0; i<G4int(h3Vector.size()); ++i ) {
270  G4HnInformation* info = hnVector[i];
271  G4bool activation = info->GetActivation();
272  G4String name = info->GetName();
273  // skip writing if inactivated
274  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
275  tools::histo::h3d* h3 = h3Vector[i];
276  G4String fileName = fFileManager->GetHnFileName("h3", name);
277  std::ofstream hnFile(fileName);
278  G4bool result
279  = tools::wcsv::hto(hnFile, h3->s_cls(), *h3);
280  if ( ! result ) {
281  G4ExceptionDescription description;
282  description << " " << "saving histogram " << name << " failed";
283  G4Exception("G4CsvAnalysisManager::Write()",
284  "Analysis_W022", JustWarning, description);
285  return false;
286  }
287  hnFile.close();
288 #ifdef G4VERBOSE
289  if ( fState.GetVerboseL1() )
290  fState.GetVerboseL1()->Message("write", "file", fileName);
291 #endif
293  }
294  }
295  else {
296  // The worker manager just adds its histograms to the master
297  // This operation needs a lock
298  G4AutoLock lH3(&mergeH3Mutex);
300  lH3.unlock();
301  }
302 
303  return true;
304 }
305 
306 //_____________________________________________________________________________
308 {
309  const std::vector<tools::histo::p1d*>& p1Vector
310  = fP1Manager->GetP1Vector();
311  const std::vector<G4HnInformation*>& hnVector
312  = fP1Manager->GetHnVector();
313 
314  if ( ! p1Vector.size() ) return true;
315 
316  if ( ! G4Threading::IsWorkerThread() ) {
317 
318  for ( G4int i=0; i<G4int(p1Vector.size()); ++i ) {
319  G4HnInformation* info = hnVector[i];
320  G4bool activation = info->GetActivation();
321  G4String name = info->GetName();
322  // skip writing if activation is enabled and P1 is inactivated
323  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
324  tools::histo::p1d* p1 = p1Vector[i];
325  G4String fileName = fFileManager->GetHnFileName("p1", name);
326  std::ofstream hnFile(fileName);
327  G4bool result
328  = tools::wcsv::pto(hnFile, p1->s_cls(), *p1);
329  if ( ! result ) {
330  G4ExceptionDescription description;
331  description << " " << "saving profile " << name << " failed";
332  G4Exception("G4CsvAnalysisManager::Write()",
333  "Analysis_W022", JustWarning, description);
334  return false;
335  }
336  hnFile.close();
337 #ifdef G4VERBOSE
338  if ( fState.GetVerboseL1() )
339  fState.GetVerboseL1()->Message("write", "file", fileName);
340 #endif
342  }
343  }
344  else {
345  // The worker manager just adds its profiles to the master
346  // This operation needs a lock
347  G4AutoLock lP1(&mergeP1Mutex);
349  lP1.unlock();
350  }
351 
352  return true;
353 }
354 
355 //_____________________________________________________________________________
357 {
358  const std::vector<tools::histo::p2d*>& p2Vector
359  = fP2Manager->GetP2Vector();
360  const std::vector<G4HnInformation*>& hnVector
361  = fP2Manager->GetHnVector();
362 
363  if ( ! p2Vector.size() ) return true;
364 
365  if ( ! G4Threading::IsWorkerThread() ) {
366 
367  for ( G4int i=0; i<G4int(p2Vector.size()); ++i ) {
368  G4HnInformation* info = hnVector[i];
369  G4bool activation = info->GetActivation();
370  G4String name = info->GetName();
371  // skip writing if activation is enabled and P2 is inactivated
372  if ( fState.GetIsActivation() && ( ! activation ) ) continue;
373  tools::histo::p2d* p2 = p2Vector[i];
374  G4String fileName = fFileManager->GetHnFileName("p2", name);
375  std::ofstream hnFile(fileName);
376  G4bool result
377  = tools::wcsv::pto(hnFile, p2->s_cls(), *p2);
378  if ( ! result ) {
379  G4ExceptionDescription description;
380  description << " " << "saving profile " << name << " failed";
381  G4Exception("G4CsvAnalysisManager::Write()",
382  "Analysis_W022", JustWarning, description);
383  return false;
384  }
385  hnFile.close();
386 #ifdef G4VERBOSE
387  if ( fState.GetVerboseL1() )
388  fState.GetVerboseL1()->Message("write", "file", fileName);
389 #endif
391  }
392  }
393  else {
394  // The worker manager just adds its profiles to the master
395  // This operation needs a lock
396  G4AutoLock lP2(&mergeP2Mutex);
398  lP2.unlock();
399  }
400 
401  return true;
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 //_____________________________________________________________________________
434 {
435  G4bool finalResult = true;
436  G4bool result = fFileManager->SetFileName(fileName);
437  finalResult = finalResult && result;
438 
439  // Only lock file name in file manager
440  result = fFileManager->OpenFile(fileName);
441  finalResult = finalResult && result;
442 
443  // Histogram and profile files are created/closed indivudually for each
444  // object in WriteHn{Pn] function
445 
446  // Create ntuples if they are booked
447  // (The files will be created with creating ntuples)
449 
450  return finalResult;
451 }
452 
453 //_____________________________________________________________________________
455 {
456  // nothing to be done for Csv file
457  G4bool finalResult = true;
458 
459 #ifdef G4VERBOSE
460  if ( fState.GetVerboseL4() )
461  fState.GetVerboseL4()->Message("write", "files", "");
462 #endif
463 
464 
465  if ( ! fgMasterInstance &&
466  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
467  ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
468  ( ! fP2Manager->IsEmpty() ) ) ) {
469 
470  G4ExceptionDescription description;
471  description
472  << " " << "No master G4CsvAnalysisManager instance exists."
473  << G4endl
474  << " " << "Histogram data will not be merged.";
475  G4Exception("G4CsvAnalysisManager::Write()",
476  "Analysis_W031", JustWarning, description);
477  }
478 
479  // H1
480  G4bool result = WriteH1();
481  finalResult = finalResult && result;
482 
483  // H2
484  result = WriteH2();
485  finalResult = finalResult && result;
486 
487  // H3
488  result = WriteH3();
489  finalResult = finalResult && result;
490 
491  // P1
492  result = WriteP1();
493  finalResult = finalResult && result;
494 
495  // P2
496  result = WriteP2();
497  finalResult = finalResult && result;
498 
499  // Ntuples
500  // Nothing to be done
501 
502  // Write ASCII if activated
503  // Not available
504  //if ( IsAscii() ) {
505  // result = WriteAscii();
506  //}
507 
508 #ifdef G4VERBOSE
509  if ( fState.GetVerboseL4() )
511  ->Message("write", "files", "", finalResult);
512 #endif
513 
514  return result;
515 }
516 
517 //_____________________________________________________________________________
519 {
520  G4bool finalResult = true;
521 
522  // Unlock file name only
523  G4bool result = fFileManager->CloseFile();
524  finalResult = finalResult && result;
525 
526  // Histogram and profile files are created/closed indivudually for each
527  // object in WriteHn{Pn] function
528 
529  // Close ntuple files
531  ( ! fState.GetIsMaster() ) ) {
532  // In sequential mode or in MT mode only on workers
533  result = CloseNtupleFiles();
534  finalResult = finalResult && result;
535  }
536 
537  // reset data
538  result = Reset();
539  if ( ! result ) {
540  G4ExceptionDescription description;
541  description << " " << "Resetting data failed";
542  G4Exception("G4CsvAnalysisManager::CloseFile()",
543  "Analysis_W021", JustWarning, description);
544  result = false;
545  }
546  finalResult = finalResult && result;
547 
548  return finalResult;
549 }
void SetP1Manager(G4VP1Manager *p1Manager)
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
void AddH3Vector(const std::vector< tools::histo::h3d * > &h3Vector)
void SetFileManager(G4CsvFileManager *fileManager)
G4H3ToolsManager * fH3Manager
const std::vector< G4HnInformation * > & GetHnVector() const
G4bool GetActivation() const
G4bool IsEmpty() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4H2ToolsManager * fH2Manager
static G4ThreadLocal G4CsvAnalysisManager * fgInstance
virtual G4bool CloseFile()
void SetH1Manager(G4VH1Manager *h1Manager)
const std::vector< tools::histo::h1d * > & GetH1Vector() const
G4CsvFileManager * fFileManager
G4String name
Definition: TRTMaterials.hh:40
G4P1ToolsManager * fP1Manager
void AddP1Vector(const std::vector< tools::histo::p1d * > &p1Vector)
virtual G4bool OpenFileImpl(const G4String &fileName)
G4bool IsEmpty() const
const std::vector< G4HnInformation * > & GetHnVector() const
G4bool IsEmpty() const
G4CsvAnalysisManager(G4bool isMaster=true)
G4P2ToolsManager * fP2Manager
G4bool CloseNtupleFile(G4CsvNtupleDescription *ntupleDescription)
#define G4ThreadLocal
Definition: tls.hh:89
G4String GetHnFileName(const G4String &hnType, const G4String &hnName) const
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static G4CsvAnalysisManager * Instance()
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 SetP2Manager(G4VP2Manager *p2Manager)
const G4AnalysisVerbose * GetVerboseL4() const
virtual G4bool CloseFileImpl()
const std::vector< tools::histo::p1d * > & GetP1Vector() const
bool G4bool
Definition: G4Types.hh:79
const std::vector< G4HnInformation * > & GetHnVector() const
const G4double p2
const G4double p1
G4CsvNtupleManager * fNtupleManager
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
G4int G4Mutex
Definition: G4Threading.hh:173
const std::vector< G4CsvNtupleDescription * > & GetNtupleDescriptionVector() const
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()
static G4CsvAnalysisManager * fgMasterInstance
virtual G4bool OpenFile(const G4String &fileName)
G4AnalysisManagerState fState
const G4AnalysisVerbose * GetVerboseL1() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
G4H1ToolsManager * fH1Manager
G4bool IsEmpty() const
const std::vector< tools::histo::h2d * > & GetH2Vector() const
const std::vector< G4HnInformation * > & GetHnVector() const