Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4Hdf5AnalysisManager.cc
이 파일의 문서화 페이지로 가기
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$
27 
28 // Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
29 
30 #include "G4Hdf5AnalysisManager.hh"
31 #include "G4Hdf5FileManager.hh"
32 #include "G4Hdf5NtupleManager.hh"
34 #include "G4Threading.hh"
35 #include "G4AutoLock.hh"
36 
37 // mutex in a file scope
38 
39 namespace {
40  //Mutex to lock master manager when opening a file
41  G4Mutex openFileMutex = G4MUTEX_INITIALIZER;
42  //Mutex to lock master manager when merging H1 histograms
43  G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
44  //Mutex to lock master manager when merging H1 histograms
45  G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
46  //Mutex to lock master manager when merging H1 histograms
47  G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
48  //Mutex to lock master manager when merging P1 profiles
49  G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
50  //Mutex to lock master manager when merging P2 profiles
51  G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
52  //Mutex to lock master manager when closing a file
53  G4Mutex closeFileMutex = G4MUTEX_INITIALIZER;
54 }
55 
58 
59 //_____________________________________________________________________________
61 {
62  if ( fgInstance == nullptr ) {
63  G4bool isMaster = ! G4Threading::IsWorkerThread();
64  fgInstance = new G4Hdf5AnalysisManager(isMaster);
65  }
66 
67  return fgInstance;
68 }
69 
70 //_____________________________________________________________________________
72 {
73  return ( fgInstance != 0 );
74 }
75 
76 //_____________________________________________________________________________
78  : G4ToolsAnalysisManager("Hdf5", isMaster),
79  fNtupleManager(nullptr),
80  fFileManager(nullptr)
81 {
82 #ifdef G4MULTITHREADED
83 #ifndef H5_HAVE_THREADSAFE
85  message
86  << "Your HDF5 lib is not built with H5_HAVE_THREADSAFE.";
87  G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
88  "Analysis_F001", FatalException, message);
89 #endif
90 #endif
91 
92  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
93  G4ExceptionDescription description;
94  description
95  << " "
96  << "G4Hdf5AnalysisManager already exists."
97  << "Cannot create another instance.";
98  G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
99  "Analysis_F001", FatalException, description);
100  }
101  if ( isMaster ) fgMasterInstance = this;
102  fgInstance = this;
103 
104  // File manager
105  fFileManager = std::make_shared<G4Hdf5FileManager>(fState);
107  fFileManager->SetBasketSize(fgkDefaultBasketSize);
108 
109  // Ntuple manager
113  // The managers will be deleted by the base class
114 }
115 
116 //_____________________________________________________________________________
118 {
119  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
120  fgInstance = nullptr;
121 }
122 
123 //
124 // private methods
125 //
126 
127 //_____________________________________________________________________________
129 {
130  auto h1Vector = fH1Manager->GetH1Vector();
131  auto hnVector = fH1Manager->GetHnVector();
132 
133  if ( ! h1Vector.size() ) return true;
134 
135  auto result = true;
136 
137  if ( ! G4Threading::IsWorkerThread() ) {
138  auto directoryName = fFileManager->GetHistoDirectoryName();
139  result = WriteHn(h1Vector, hnVector, directoryName, "h1");
140  }
141  else {
142  // The worker manager just adds its histograms to the master
143  // This operation needs a lock
144  G4AutoLock lH1(&mergeH1Mutex);
146  lH1.unlock();
147  }
148 
149  return result;
150 }
151 
152 //_____________________________________________________________________________
154 {
155  auto h2Vector = fH2Manager->GetH2Vector();
156  auto hnVector = fH2Manager->GetHnVector();
157 
158  if ( ! h2Vector.size() ) return true;
159 
160  auto result = true;
161 
162  if ( ! G4Threading::IsWorkerThread() ) {
163  auto directoryName = fFileManager->GetHistoDirectoryName();
164  result = WriteHn(h2Vector, hnVector, directoryName, "h2");
165  }
166  else {
167  // The worker manager just adds its histograms to the master
168  // This operation needs a lock
169  G4AutoLock lH2(&mergeH2Mutex);
171  lH2.unlock();
172  }
173 
174  return result;
175 }
176 
177 //_____________________________________________________________________________
179 {
180  auto h3Vector = fH3Manager->GetH3Vector();
181  auto hnVector = fH3Manager->GetHnVector();
182 
183  if ( ! h3Vector.size() ) return true;
184 
185  auto result = true;
186 
187  if ( ! G4Threading::IsWorkerThread() ) {
188  auto directoryName = fFileManager->GetHistoDirectoryName();
189  result = WriteHn(h3Vector, hnVector, directoryName, "h3");
190  }
191  else {
192  // The worker manager just adds its histograms to the master
193  // This operation needs a lock
194  G4AutoLock lH3(&mergeH3Mutex);
196  lH3.unlock();
197  }
198 
199  return result;
200 }
201 
202 //_____________________________________________________________________________
204 {
205  auto p1Vector = fP1Manager->GetP1Vector();
206  auto hnVector = fP1Manager->GetHnVector();
207 
208  if ( ! p1Vector.size() ) return true;
209 
210  auto result = true;
211 
212  if ( ! G4Threading::IsWorkerThread() ) {
213  auto directoryName = fFileManager->GetHistoDirectoryName();
214  result = WritePn(p1Vector, hnVector, directoryName, "p1");
215  }
216  else {
217  // The worker manager just adds its profiles to the master
218  // This operation needs a lock
219  G4AutoLock lP1(&mergeP1Mutex);
221  lP1.unlock();
222  }
223 
224  return result;
225 }
226 
227 //_____________________________________________________________________________
229 {
230  auto p2Vector = fP2Manager->GetP2Vector();
231  auto hnVector = fP2Manager->GetHnVector();
232 
233  if ( ! p2Vector.size() ) return true;
234 
235  auto result = true;
236 
237  if ( ! G4Threading::IsWorkerThread() ) {
238  auto directoryName = fFileManager->GetHistoDirectoryName();
239  result = WritePn(p2Vector, hnVector, directoryName, "p2");
240  }
241  else {
242  // The worker manager just adds its profiles to the master
243  // This operation needs a lock
244  G4AutoLock lP2(&mergeP2Mutex);
246  lP2.unlock();
247  }
248 
249  return result;
250 }
251 
252 //_____________________________________________________________________________
254 {
255 // Reset histograms and ntuple
256 
257  auto finalResult = true;
258 
260  finalResult = finalResult && result;
261 
262  result = fNtupleManager->Reset(true);
263  finalResult = finalResult && result;
264 
265  return finalResult;
266 }
267 
268 //
269 // protected methods
270 //
271 
272 //_____________________________________________________________________________
274 {
275  auto finalResult = true;
276  auto result = fFileManager->SetFileName(fileName);
277  finalResult = finalResult && result;
278 
279 #ifdef G4VERBOSE
280  G4String name = fFileManager->GetFullFileName();
281  if ( fState.GetVerboseL4() )
282  fState.GetVerboseL4()->Message("open", "analysis file", name);
283 #endif
284 
285  G4AutoLock lock(&openFileMutex);
286  result = fFileManager->OpenFile(fileName);
287  finalResult = finalResult && result;
288 
289  // fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
291  lock.unlock();
292 
293 #ifdef G4VERBOSE
294  if ( fState.GetVerboseL1() )
295  fState.GetVerboseL1()->Message("open", "analysis file", name, finalResult);
296 #endif
297 
298  return finalResult;
299 }
300 
301 //_____________________________________________________________________________
303 {
304  auto finalResult = true;
305 
306 #ifdef G4VERBOSE
307  auto name = fFileManager->GetFullFileName();
308  if ( fState.GetVerboseL4() )
309  fState.GetVerboseL4()->Message("write", "files", name);
310 #endif
311 
312  // Histo directory
313  // auto result = fFileManager->WriteHistoDirectory();
314  // if ( ! result ) return false;
315 
316  auto result = WriteH1();
317  finalResult = finalResult && result;
318 
319  // H2
320  result = WriteH2();
321  finalResult = finalResult && result;
322 
323  // H3
324  result = WriteH3();
325  finalResult = finalResult && result;
326 
327  // P1
328  result = WriteP1();
329  finalResult = finalResult && result;
330 
331  // P2
332  result = WriteP2();
333  finalResult = finalResult && result;
334 
335  // // Ntuple directory
336  // result = fFileManager->WriteNtupleDirectory();
337  // if ( ! result ) return false;
338 
339  // Write ASCII if activated
340  if ( IsAscii() ) {
341  result = WriteAscii(fFileManager->GetFileName());
342  finalResult = finalResult && result;
343  }
344 
345 #ifdef G4VERBOSE
346  if ( fState.GetVerboseL1() )
348  ->Message("write", "file", fFileManager->GetFullFileName(), finalResult);
349 #endif
350 
351  return finalResult;
352 }
353 
354 //_____________________________________________________________________________
356 {
357  auto finalResult = true;
358 
359  G4AutoLock lock(&closeFileMutex);
360  auto result = fFileManager->CloseFile();
361  finalResult = finalResult && result;
362 
363  // reset data
364  result = Reset();
365  if ( ! result ) {
366  G4ExceptionDescription description;
367  description << " " << "Resetting data failed";
368  G4Exception("G4Hdf5AnalysisManager::CloseFile()",
369  "Analysis_W021", JustWarning, description);
370  }
371  lock.unlock();
372  finalResult = finalResult && result;
373 
374  // No files clean-up as ntuples are not supported in MT mode
375 
376  return finalResult;
377 }
const std::vector< tools::histo::p2d * > & GetP2Vector() const
const XML_Char * name
Definition: expat.h:151
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
virtual G4bool OpenFileImpl(const G4String &fileName) final
G4Hdf5NtupleManager * fNtupleManager
void CreateNtuplesFromBooking()
void AddP2Vector(const std::vector< tools::histo::p2d * > &p2Vector)
void message(RunManager *runmanager)
Definition: ts_scorers.cc:72
#define G4ThreadLocal
Definition: tls.hh:69
const std::vector< tools::histo::h1d * > & GetH1Vector() const
virtual G4bool CloseFileImpl() final
static constexpr unsigned int fgkDefaultBasketSize
const std::vector< tools::histo::h3d * > & GetH3Vector() const
bool G4bool
Definition: G4Types.hh:79
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:88
const std::vector< G4HnInformation * > & GetHnVector() const
G4AnalysisManagerState fState
const std::vector< G4HnInformation * > & GetHnVector() const
G4Hdf5AnalysisManager(G4bool isMaster=true)
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
void AddP1Vector(const std::vector< tools::histo::p1d * > &p1Vector)
const G4AnalysisVerbose * GetVerboseL4() const
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< G4HnInformation * > & GetHnVector() const
void AddH2Vector(const std::vector< tools::histo::h2d * > &h2Vector)
G4bool Reset(G4bool deleteNtuple)
void AddH3Vector(const std::vector< tools::histo::h3d * > &h3Vector)
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
G4double G4ParticleHPJENDLHEData::G4double result
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
std::shared_ptr< G4Hdf5FileManager > fFileManager
const std::vector< tools::histo::h2d * > & GetH2Vector() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
const std::vector< tools::histo::p1d * > & GetP1Vector() const
G4bool WriteHn(const std::vector< T * > &htVector, const std::vector< G4HnInformation * > &hnVector, const G4String &directoryName, const G4String &hnType)
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
static G4Hdf5AnalysisManager * fgMasterInstance
virtual G4bool WriteImpl() final
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
G4bool IsAscii() const
void SetFileManager(std::shared_ptr< G4Hdf5FileManager > fileManager)
static G4Hdf5AnalysisManager * Instance()
const std::vector< G4HnInformation * > & GetHnVector() const
const G4AnalysisVerbose * GetVerboseL1() const
G4bool WritePn(const std::vector< T * > &htVector, const std::vector< G4HnInformation * > &hnVector, const G4String &directoryName, const G4String &hnType)
static G4ThreadLocal G4Hdf5AnalysisManager * fgInstance
G4bool WriteAscii(const G4String &fileName)
std::mutex G4Mutex
Definition: G4Threading.hh:84