ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Hdf5AnalysisReader.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4Hdf5AnalysisReader.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 "G4Hdf5AnalysisReader.hh"
30 #include "G4Hdf5RNtupleManager.hh"
31 #include "G4AnalysisVerbose.hh"
32 #include "G4AnalysisUtilities.hh"
33 #include "G4Threading.hh"
34 
35 #include <iostream>
36 #include <cstdio>
37 
38 using namespace G4Analysis;
39 
42 
43 //_____________________________________________________________________________
45 {
46  if ( fgInstance == nullptr ) {
47  G4bool isMaster = ! G4Threading::IsWorkerThread();
48  fgInstance = new G4Hdf5AnalysisReader(isMaster);
49  }
50 
51  return fgInstance;
52 }
53 
54 //_____________________________________________________________________________
56  : G4ToolsAnalysisReader("Hdf5", isMaster),
57  fNtupleManager(nullptr),
58  fFileManager(nullptr)
59 {
60  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
61  G4ExceptionDescription description;
62  description
63  << " "
64  << "G4Hdf5AnalysisReader already exists."
65  << "Cannot create another instance.";
66  G4Exception("G4Hdf5AnalysisReader::G4Hdf5AnalysisReader()",
67  "Analysis_F001", FatalException, description);
68  }
69  if ( isMaster ) fgMasterInstance = this;
70  fgInstance = this;
71 
72  // Create managers
75 
76  // Set managers to base class
79 }
80 
81 //_____________________________________________________________________________
83 {
84  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
85  fgInstance = nullptr;
86 }
87 
88 //
89 // private methods
90 //
91 
92 //_____________________________________________________________________________
94 {
95 // Reset histograms and ntuple
96 
97  auto finalResult = true;
98 
99  auto result = G4ToolsAnalysisReader::Reset();
100  finalResult = finalResult && result;
101 
102  result = fNtupleManager->Reset();
103  finalResult = finalResult && result;
104 
105  return finalResult;
106 }
107 
108 //
109 // protected methods
110 //
111 
112 //_____________________________________________________________________________
114  const G4String& fileName,
115  const G4String& dirName,
116  G4bool /*isUserFileName*/)
117 {
118 #ifdef G4VERBOSE
119  if ( fState.GetVerboseL4() )
120  fState.GetVerboseL4()->Message("read", "h1", h1Name);
121 #endif
122 
123  auto h1 = ReadHnImpl<tools::histo::h1d>(h1Name, fileName, dirName);
124 
125  if ( ! h1 ) return kInvalidId;
126 
127  auto id = fH1Manager->AddH1(h1Name, h1);
128 
129 #ifdef G4VERBOSE
130  if ( fState.GetVerboseL2() )
131  fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
132 #endif
133 
134  return id;
135 }
136 
137 //_____________________________________________________________________________
139  const G4String& fileName,
140  const G4String& dirName,
141  G4bool /*isUserFileName*/)
142 {
143 #ifdef G4VERBOSE
144  if ( fState.GetVerboseL4() )
145  fState.GetVerboseL4()->Message("read", "h2", h2Name);
146 #endif
147 
148  auto h2 = ReadHnImpl<tools::histo::h2d>(h2Name, fileName, dirName);
149 
150  if ( ! h2 ) return kInvalidId;
151 
152  auto id = fH2Manager->AddH2(h2Name, h2);
153 
154 #ifdef G4VERBOSE
155  if ( fState.GetVerboseL2() )
156  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
157 #endif
158 
159  return id;
160 }
161 
162 //_____________________________________________________________________________
164  const G4String& fileName,
165  const G4String& dirName,
166  G4bool /*isUserFileName*/)
167 {
168 
169 #ifdef G4VERBOSE
170  if ( fState.GetVerboseL4() )
171  fState.GetVerboseL4()->Message("read", "h3", h3Name);
172 #endif
173 
174  auto h3 = ReadHnImpl<tools::histo::h3d>(h3Name, fileName, dirName);
175 
176  if ( ! h3 ) return kInvalidId;
177 
178  auto id = fH3Manager->AddH3(h3Name, h3);
179 
180 #ifdef G4VERBOSE
181  if ( fState.GetVerboseL2() )
182  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
183 #endif
184 
185  return id;
186 }
187 
188 //_____________________________________________________________________________
190  const G4String& fileName,
191  const G4String& dirName,
192  G4bool /*isUserFileName*/)
193 {
194 #ifdef G4VERBOSE
195  if ( fState.GetVerboseL4() )
196  fState.GetVerboseL4()->Message("read", "p1", p1Name);
197 #endif
198 
199  auto p1 = ReadPnImpl<tools::histo::p1d>(p1Name, fileName, dirName);
200 
201  if ( ! p1 ) return kInvalidId;
202 
203  auto id = fP1Manager->AddP1(p1Name, p1);
204 
205 #ifdef G4VERBOSE
206  if ( fState.GetVerboseL2() )
207  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
208 #endif
209 
210  return id;
211 }
212 
213 //_____________________________________________________________________________
215  const G4String& fileName,
216  const G4String& dirName,
217  G4bool /*isUserFileName*/)
218 {
219 
220 #ifdef G4VERBOSE
221  if ( fState.GetVerboseL4() )
222  fState.GetVerboseL4()->Message("read", "p2", p2Name);
223 #endif
224 
225  auto p2 = ReadPnImpl<tools::histo::p2d>(p2Name, fileName, dirName);
226 
227  if ( ! p2 ) return kInvalidId;
228 
229  auto id = fP2Manager->AddP2(p2Name, p2);
230 
231 #ifdef G4VERBOSE
232  if ( fState.GetVerboseL2() )
233  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
234 #endif
235 
236  return id;
237 }
238 
239 //_____________________________________________________________________________
241  const G4String& fileName,
242  const G4String& dirName,
243  G4bool isUserFileName)
244 {
245 #ifdef G4VERBOSE
246  if ( fState.GetVerboseL4() )
247  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
248 #endif
249 
250  // Ntuples are saved in files per thread
251  // but apply thethe thread suffix only if fileName is not provided explicitly
252  G4String fullFileName = fileName;
253  if ( ! isUserFileName ) {
254  fullFileName = fFileManager->GetFullFileName();
255  }
256 
257  // Get directory
258  auto directory = fFileManager->GetNtupleRDirectory(fullFileName, dirName, false);
259  if ( directory < 0 ) return kInvalidId;
260 
261  // Create ntuple
262  auto rntuple = new tools::hdf5::ntuple(G4cout, directory, ntupleName);
263  auto rntupleDescription = new G4TRNtupleDescription<tools::hdf5::ntuple>(rntuple);
264  auto id = fNtupleManager->SetNtuple(rntupleDescription);
265 
266 #ifdef G4VERBOSE
267  if ( fState.GetVerboseL2() )
268  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
269 #endif
270 
271  return id;
272 }