ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Hdf5AnalysisManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file 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 
27 // Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
28 
29 #include "G4Hdf5AnalysisManager.hh"
30 #include "G4Hdf5FileManager.hh"
31 #include "G4Hdf5NtupleManager.hh"
33 #include "G4Threading.hh"
34 #include "G4AutoLock.hh"
35 
36 // mutex in a file scope
37 
38 namespace {
39  //Mutex to lock master manager when opening a file
40  G4Mutex openFileMutex = G4MUTEX_INITIALIZER;
41  //Mutex to lock master manager when merging H1 histograms
42  G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
43  //Mutex to lock master manager when merging H1 histograms
44  G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
45  //Mutex to lock master manager when merging H1 histograms
46  G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
47  //Mutex to lock master manager when merging P1 profiles
48  G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
49  //Mutex to lock master manager when merging P2 profiles
50  G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
51  //Mutex to lock master manager when closing a file
52  G4Mutex closeFileMutex = G4MUTEX_INITIALIZER;
53 }
54 
57 
58 //_____________________________________________________________________________
60 {
61  if ( fgInstance == nullptr ) {
62  G4bool isMaster = ! G4Threading::IsWorkerThread();
63  fgInstance = new G4Hdf5AnalysisManager(isMaster);
64  }
65 
66  return fgInstance;
67 }
68 
69 //_____________________________________________________________________________
71 {
72  return ( fgInstance != 0 );
73 }
74 
75 //_____________________________________________________________________________
77  : G4ToolsAnalysisManager("Hdf5", isMaster),
78  fNtupleManager(nullptr),
79  fFileManager(nullptr)
80 {
81 #ifdef G4MULTITHREADED
82 #ifndef H5_HAVE_THREADSAFE
84  message
85  << "Your HDF5 lib is not built with H5_HAVE_THREADSAFE.";
86  G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
87  "Analysis_F001", FatalException, message);
88 #endif
89 #endif
90 
91  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
92  G4ExceptionDescription description;
93  description
94  << " "
95  << "G4Hdf5AnalysisManager already exists."
96  << "Cannot create another instance.";
97  G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
98  "Analysis_F001", FatalException, description);
99  }
100  if ( isMaster ) fgMasterInstance = this;
101  fgInstance = this;
102 
103  // File manager
104  fFileManager = std::make_shared<G4Hdf5FileManager>(fState);
106  fFileManager->SetBasketSize(fgkDefaultBasketSize);
107 
108  // Ntuple manager
112  // The managers will be deleted by the base class
113 }
114 
115 //_____________________________________________________________________________
117 {
118  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
119  fgInstance = nullptr;
120 }
121 
122 //
123 // private methods
124 //
125 
126 //_____________________________________________________________________________
128 {
129  auto h1Vector = fH1Manager->GetH1Vector();
130  auto hnVector = fH1Manager->GetHnVector();
131 
132  if ( ! h1Vector.size() ) return true;
133 
134  auto result = true;
135 
136  if ( ! G4Threading::IsWorkerThread() ) {
137  auto directoryName = fFileManager->GetHistoDirectoryName();
138  result = WriteHn(h1Vector, hnVector, directoryName, "h1");
139  }
140  else {
141  // The worker manager just adds its histograms to the master
142  // This operation needs a lock
143  G4AutoLock lH1(&mergeH1Mutex);
145  lH1.unlock();
146  }
147 
148  return result;
149 }
150 
151 //_____________________________________________________________________________
153 {
154  auto h2Vector = fH2Manager->GetH2Vector();
155  auto hnVector = fH2Manager->GetHnVector();
156 
157  if ( ! h2Vector.size() ) return true;
158 
159  auto result = true;
160 
161  if ( ! G4Threading::IsWorkerThread() ) {
162  auto directoryName = fFileManager->GetHistoDirectoryName();
163  result = WriteHn(h2Vector, hnVector, directoryName, "h2");
164  }
165  else {
166  // The worker manager just adds its histograms to the master
167  // This operation needs a lock
168  G4AutoLock lH2(&mergeH2Mutex);
170  lH2.unlock();
171  }
172 
173  return result;
174 }
175 
176 //_____________________________________________________________________________
178 {
179  auto h3Vector = fH3Manager->GetH3Vector();
180  auto hnVector = fH3Manager->GetHnVector();
181 
182  if ( ! h3Vector.size() ) return true;
183 
184  auto result = true;
185 
186  if ( ! G4Threading::IsWorkerThread() ) {
187  auto directoryName = fFileManager->GetHistoDirectoryName();
188  result = WriteHn(h3Vector, hnVector, directoryName, "h3");
189  }
190  else {
191  // The worker manager just adds its histograms to the master
192  // This operation needs a lock
193  G4AutoLock lH3(&mergeH3Mutex);
195  lH3.unlock();
196  }
197 
198  return result;
199 }
200 
201 //_____________________________________________________________________________
203 {
204  auto p1Vector = fP1Manager->GetP1Vector();
205  auto hnVector = fP1Manager->GetHnVector();
206 
207  if ( ! p1Vector.size() ) return true;
208 
209  auto result = true;
210 
211  if ( ! G4Threading::IsWorkerThread() ) {
212  auto directoryName = fFileManager->GetHistoDirectoryName();
213  result = WritePn(p1Vector, hnVector, directoryName, "p1");
214  }
215  else {
216  // The worker manager just adds its profiles to the master
217  // This operation needs a lock
218  G4AutoLock lP1(&mergeP1Mutex);
220  lP1.unlock();
221  }
222 
223  return result;
224 }
225 
226 //_____________________________________________________________________________
228 {
229  auto p2Vector = fP2Manager->GetP2Vector();
230  auto hnVector = fP2Manager->GetHnVector();
231 
232  if ( ! p2Vector.size() ) return true;
233 
234  auto result = true;
235 
236  if ( ! G4Threading::IsWorkerThread() ) {
237  auto directoryName = fFileManager->GetHistoDirectoryName();
238  result = WritePn(p2Vector, hnVector, directoryName, "p2");
239  }
240  else {
241  // The worker manager just adds its profiles to the master
242  // This operation needs a lock
243  G4AutoLock lP2(&mergeP2Mutex);
245  lP2.unlock();
246  }
247 
248  return result;
249 }
250 
251 //_____________________________________________________________________________
253 {
254 // Reset histograms and ntuple
255 
256  auto finalResult = true;
257 
258  auto result = G4ToolsAnalysisManager::Reset();
259  finalResult = finalResult && result;
260 
261  result = fNtupleManager->Reset(true);
262  finalResult = finalResult && result;
263 
264  return finalResult;
265 }
266 
267 //
268 // protected methods
269 //
270 
271 //_____________________________________________________________________________
273 {
274  auto finalResult = true;
275  auto result = fFileManager->SetFileName(fileName);
276  finalResult = finalResult && result;
277 
278 #ifdef G4VERBOSE
279  G4String name = fFileManager->GetFullFileName();
280  if ( fState.GetVerboseL4() )
281  fState.GetVerboseL4()->Message("open", "analysis file", name);
282 #endif
283 
284  G4AutoLock lock(&openFileMutex);
285  result = fFileManager->OpenFile(fileName);
286  finalResult = finalResult && result;
287 
288  // fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
290  lock.unlock();
291 
292 #ifdef G4VERBOSE
293  if ( fState.GetVerboseL1() )
294  fState.GetVerboseL1()->Message("open", "analysis file", name, finalResult);
295 #endif
296 
297  return finalResult;
298 }
299 
300 //_____________________________________________________________________________
302 {
303  auto finalResult = true;
304 
305 #ifdef G4VERBOSE
306  auto name = fFileManager->GetFullFileName();
307  if ( fState.GetVerboseL4() )
308  fState.GetVerboseL4()->Message("write", "files", name);
309 #endif
310 
311  // Histo directory
312  // auto result = fFileManager->WriteHistoDirectory();
313  // if ( ! result ) return false;
314 
315  auto result = WriteH1();
316  finalResult = finalResult && result;
317 
318  // H2
319  result = WriteH2();
320  finalResult = finalResult && result;
321 
322  // H3
323  result = WriteH3();
324  finalResult = finalResult && result;
325 
326  // P1
327  result = WriteP1();
328  finalResult = finalResult && result;
329 
330  // P2
331  result = WriteP2();
332  finalResult = finalResult && result;
333 
334  // // Ntuple directory
335  // result = fFileManager->WriteNtupleDirectory();
336  // if ( ! result ) return false;
337 
338  // Write ASCII if activated
339  if ( IsAscii() ) {
340  result = WriteAscii(fFileManager->GetFileName());
341  finalResult = finalResult && result;
342  }
343 
344 #ifdef G4VERBOSE
345  if ( fState.GetVerboseL1() )
347  ->Message("write", "file", fFileManager->GetFullFileName(), finalResult);
348 #endif
349 
350  return finalResult;
351 }
352 
353 //_____________________________________________________________________________
355 {
356  auto finalResult = true;
357 
358  G4AutoLock lock(&closeFileMutex);
359  auto result = fFileManager->CloseFile();
360  finalResult = finalResult && result;
361 
362  if ( reset ) {
363  // reset data
364  result = Reset();
365  } else {
366  // ntuple must be reset
367  result = fNtupleManager->Reset(true);
368  }
369  if ( ! result ) {
370  G4ExceptionDescription description;
371  description << " " << "Resetting data failed";
372  G4Exception("G4Hdf5AnalysisManager::CloseFile()",
373  "Analysis_W021", JustWarning, description);
374  }
375 
376  lock.unlock();
377  finalResult = finalResult && result;
378 
379  // No files clean-up as ntuples are not supported in MT mode
380 
381  return finalResult;
382 }