ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4CsvAnalysisManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4CsvAnalysisManager.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, 18/06/2013 (ivana@ipno.in2p3.fr)
28 
29 #include "G4CsvAnalysisManager.hh"
30 #include "G4CsvFileManager.hh"
31 #include "G4CsvNtupleManager.hh"
32 #include "G4AnalysisVerbose.hh"
34 #include "G4UnitsTable.hh"
35 #include "G4Threading.hh"
36 #include "G4AutoLock.hh"
37 
38 #include <iostream>
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  //Mutex to lock master manager when merging H1 histograms
48  G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
49  //Mutex to lock master manager when merging P1 profiles
50  G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
51  //Mutex to lock master manager when merging P2 profiles
52  G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
53 }
54 
57 
58 //_____________________________________________________________________________
60 {
61  if ( fgInstance == nullptr ) {
62  G4bool isMaster = ! G4Threading::IsWorkerThread();
63  fgInstance = new G4CsvAnalysisManager(isMaster);
64  }
65 
66  return fgInstance;
67 }
68 
69 //_____________________________________________________________________________
71 {
72  return ( fgInstance != 0 );
73 }
74 
75 //_____________________________________________________________________________
77  : G4ToolsAnalysisManager("Csv", isMaster),
78  fNtupleManager(nullptr),
79  fFileManager(nullptr)
80 {
81  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
82  G4ExceptionDescription description;
83  description << " "
84  << "G4CsvAnalysisManager already exists."
85  << "Cannot create another instance.";
86  G4Exception("G4CsvAnalysisManager::G4CsvAnalysisManager()",
87  "Analysis_F001", FatalException, description);
88  }
89 
90  if ( isMaster ) fgMasterInstance = this;
91  fgInstance = this;
93  fFileManager = std::make_shared<G4CsvFileManager>(fState);
95  // The managers will be deleted by the base class
96 
97  // Set managers to base class which takes then their ownreship
100 }
101 
102 //_____________________________________________________________________________
104 {
105  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
106  fgInstance = nullptr;
107 }
108 
109 //
110 // private methods
111 //
112 
113 //_____________________________________________________________________________
115 {
116  auto ntupleVector
118 
119  // Close ntuple files
120  for ( auto ntupleDescription : ntupleVector) {
121  fFileManager->CloseNtupleFile(ntupleDescription);
122  }
123 
124  return true;
125 }
126 
127 
128 //
129 // protected methods
130 //
131 
132 //
133 // private methods
134 //
135 
136 
137 //_____________________________________________________________________________
139 {
140  auto h1Vector = fH1Manager->GetH1Vector();
141  auto hnVector = fH1Manager->GetHnVector();
142 
143  if ( ! h1Vector.size() ) return true;
144 
145  auto result = true;
146 
147  if ( ! G4Threading::IsWorkerThread() ) {
148  result = WriteT(h1Vector, hnVector, "h1");
149  }
150  else {
151  // The worker manager just adds its histograms to the master
152  // This operation needs a lock
153  G4AutoLock lH1(&mergeH1Mutex);
155  lH1.unlock();
156  }
157 
158  return result;
159 }
160 
161 //_____________________________________________________________________________
163 {
164  auto h2Vector = fH2Manager->GetH2Vector();
165  auto hnVector = fH2Manager->GetHnVector();
166 
167  if ( ! h2Vector.size() ) return true;
168 
169  auto result = true;
170 
171  if ( ! G4Threading::IsWorkerThread() ) {
172  result = WriteT(h2Vector, hnVector, "h2");
173  }
174  else {
175  // The worker manager just adds its histograms to the master
176  // This operation needs a lock
177  G4AutoLock lH2(&mergeH2Mutex);
179  lH2.unlock();
180  }
181 
182  return result;
183 }
184 
185 //_____________________________________________________________________________
187 {
188  auto h3Vector = fH3Manager->GetH3Vector();
189  auto hnVector = fH3Manager->GetHnVector();
190 
191  if ( ! h3Vector.size() ) return true;
192 
193  auto result = true;
194 
195  if ( ! G4Threading::IsWorkerThread() ) {
196  result = WriteT(h3Vector, hnVector, "h3");
197  }
198  else {
199  // The worker manager just adds its histograms to the master
200  // This operation needs a lock
201  G4AutoLock lH3(&mergeH3Mutex);
203  lH3.unlock();
204  }
205 
206  return result;
207 }
208 
209 //_____________________________________________________________________________
211 {
212  auto p1Vector = fP1Manager->GetP1Vector();
213  auto hnVector = fP1Manager->GetHnVector();
214 
215  if ( ! p1Vector.size() ) return true;
216 
217  auto result = true;
218 
219  if ( ! G4Threading::IsWorkerThread() ) {
220  result = WriteT(p1Vector, hnVector, "p1");
221  }
222  else {
223  // The worker manager just adds its profiles to the master
224  // This operation needs a lock
225  G4AutoLock lP1(&mergeP1Mutex);
227  lP1.unlock();
228  }
229 
230  return result;
231 }
232 
233 //_____________________________________________________________________________
235 {
236  auto p2Vector = fP2Manager->GetP2Vector();
237  auto hnVector = fP2Manager->GetHnVector();
238 
239  if ( ! p2Vector.size() ) return true;
240 
241  auto result = true;
242 
243  if ( ! G4Threading::IsWorkerThread() ) {
244  result = WriteT(p2Vector, hnVector, "p2");
245  }
246  else {
247  // The worker manager just adds its profiles to the master
248  // This operation needs a lock
249  G4AutoLock lP2(&mergeP2Mutex);
251  lP2.unlock();
252  }
253 
254  return result;
255 }
256 
257 //_____________________________________________________________________________
259 {
260 // Reset histograms and ntuple
261 
262  auto finalResult = true;
263 
264  auto result = G4ToolsAnalysisManager::Reset();
265  finalResult = finalResult && result;
266 
267  result = fNtupleManager->Reset(true);
268  finalResult = finalResult && result;
269 
270  return finalResult;
271 }
272 
273 //_____________________________________________________________________________
275 {
276  auto finalResult = true;
277  auto result = fFileManager->SetFileName(fileName);
278  finalResult = finalResult && result;
279 
280  // Only lock file name in file manager
281  result = fFileManager->OpenFile(fileName);
282  finalResult = finalResult && result;
283 
284  // Histogram and profile files are created/closed indivudually for each
285  // object in WriteHn{Pn] function
286 
287  // Create ntuples if they are booked
288  // (The files will be created with creating ntuples)
290 
291  return finalResult;
292 }
293 
294 //_____________________________________________________________________________
296 {
297  // nothing to be done for Csv file
298  auto finalResult = true;
299 
300 #ifdef G4VERBOSE
301  if ( fState.GetVerboseL4() )
302  fState.GetVerboseL4()->Message("write", "files", "");
303 #endif
304 
305 
306  if ( ! fgMasterInstance &&
307  ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
308  ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
309  ( ! fP2Manager->IsEmpty() ) ) ) {
310 
311  G4ExceptionDescription description;
312  description
313  << " " << "No master G4CsvAnalysisManager instance exists."
314  << G4endl
315  << " " << "Histogram data will not be merged.";
316  G4Exception("G4CsvAnalysisManager::Write()",
317  "Analysis_W031", JustWarning, description);
318  }
319 
320  // H1
321  auto result = WriteH1();
322  finalResult = finalResult && result;
323 
324  // H2
325  result = WriteH2();
326  finalResult = finalResult && result;
327 
328  // H3
329  result = WriteH3();
330  finalResult = finalResult && result;
331 
332  // P1
333  result = WriteP1();
334  finalResult = finalResult && result;
335 
336  // P2
337  result = WriteP2();
338  finalResult = finalResult && result;
339 
340  // Ntuples
341  // Nothing to be done
342 
343  // Write ASCII if activated
344  // Not available
345  //if ( IsAscii() ) {
346  // result = WriteAscii();
347  //}
348 
349 #ifdef G4VERBOSE
350  if ( fState.GetVerboseL1() )
352  ->Message("write", "files", "", finalResult);
353 #endif
354 
355  return result;
356 }
357 
358 //_____________________________________________________________________________
360 {
361  auto finalResult = true;
362 
363  // Unlock file name only
364  auto result = fFileManager->CloseFile();
365  finalResult = finalResult && result;
366 
367  // Histogram and profile files are created/closed indivudually for each
368  // object in WriteHn{Pn] function
369  // In sequential mode or in MT mode only on workers
370  result = CloseNtupleFiles();
371  finalResult = finalResult && result;
372 
373  // reset data
374  if ( reset ) {
375  result = Reset();
376  } else {
377  // ntuple must be reset
378  result = fNtupleManager->Reset(true);
379  }
380  if ( ! result ) {
381  G4ExceptionDescription description;
382  description << " " << "Resetting data failed";
383  G4Exception("G4CsvAnalysisManager::CloseFile()",
384  "Analysis_W021", JustWarning, description);
385  }
386  finalResult = finalResult && result;
387 
388  return finalResult;
389 }