ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4XmlAnalysisReader.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4XmlAnalysisReader.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, 25/07/2014 (ivana@ipno.in2p3.fr)
28 
29 #include "G4XmlAnalysisReader.hh"
30 #include "G4XmlRFileManager.hh"
31 #include "G4H1ToolsManager.hh"
32 #include "G4H2ToolsManager.hh"
33 #include "G4H3ToolsManager.hh"
34 #include "G4P1ToolsManager.hh"
35 #include "G4P2ToolsManager.hh"
36 #include "G4XmlRNtupleManager.hh"
37 #include "G4AnalysisVerbose.hh"
38 #include "G4AnalysisUtilities.hh"
39 #include "G4Threading.hh"
40 
41 #include <iostream>
42 #include <cstdio>
43 
44 using namespace G4Analysis;
45 
48 
49 //_____________________________________________________________________________
51 {
52  if ( fgInstance == nullptr ) {
53  G4bool isMaster = ! G4Threading::IsWorkerThread();
54  fgInstance = new G4XmlAnalysisReader(isMaster);
55  }
56 
57  return fgInstance;
58 }
59 
60 //_____________________________________________________________________________
62  : G4ToolsAnalysisReader("Xml", isMaster),
63  fNtupleManager(nullptr),
64  fFileManager(nullptr)
65 {
66  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
67  G4ExceptionDescription description;
68  description
69  << " "
70  << "G4XmlAnalysisReader already exists."
71  << "Cannot create another instance.";
72  G4Exception("G4XmlAnalysisReader::G4XmlAnalysisReader()",
73  "Analysis_F001", FatalException, description);
74  }
75  if ( isMaster ) fgMasterInstance = this;
76  fgInstance = this;
77 
78  // Create managers
81  // The managers will be deleted by the base class
82 
83  // Set managers to base class
86 }
87 
88 //_____________________________________________________________________________
90 {
91  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
92  fgInstance = nullptr;
93 }
94 
95 //
96 // private methods
97 //
98 
99 //_____________________________________________________________________________
101  const G4String& fileName,
102  const G4String& objectName,
103  const G4String& objectType,
104  const G4String& inFunction)
105 {
106 // Get buffer for reading object specified by objectName and objectType
107 // for a file specified by fileName;
108 // open the file if it was not yet open
109 
110  // Histograms and profiles are not saved per thread
111  // and ntuple file name is already updated
112  auto rfile = fFileManager->GetRFile(fileName);
113  if ( ! rfile ) {
114  if ( ! fFileManager->OpenRFile(fileName) ) return nullptr;
115  rfile = fFileManager->GetRFile(fileName);
116  }
117 
118  tools::raxml_out* handler = nullptr;
119  if ( rfile ) {
120  std::vector<tools::raxml_out>& objects = rfile->objects();
121  std::vector<tools::raxml_out>::iterator it;
122  for (it = objects.begin(); it!=objects.end(); ++it) {
123  tools::raxml_out& object = *it;
124  if ( object.cls() == objectType && object.name() == objectName ) {
125  handler = &object;
126  break;
127  }
128  }
129  }
130 
131  if ( ! handler ) {
132  G4ExceptionDescription description;
133  description
134  << " "
135  << "Cannot get "<< objectName << " in file " << fileName;
136  G4String inFunctionFull = "G4XmlAnalysisReader::";
137  inFunctionFull.append(inFunction);
138  G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
139  return nullptr;
140  }
141 
142  return handler;
143 }
144 
145 //_____________________________________________________________________________
147 {
148 // Reset histograms and ntuple
149 
150  auto finalResult = true;
151 
152  auto result = G4ToolsAnalysisReader::Reset();
153  finalResult = finalResult && result;
154 
155  result = fNtupleManager->Reset();
156  finalResult = finalResult && result;
157 
158  return finalResult;
159 }
160 
161 //
162 // protected methods
163 //
164 
165 //_____________________________________________________________________________
167  const G4String& fileName,
168  const G4String& /*dirName*/,
169  G4bool /*isUserFileName*/)
170 {
171 #ifdef G4VERBOSE
172  if ( fState.GetVerboseL4() )
173  fState.GetVerboseL4()->Message("read", "h1", h1Name);
174 #endif
175 
176  tools::raxml_out* handler
177  = GetHandler(fileName, h1Name, tools::histo::h1d::s_class(), "ReadH1Impl");
178  if ( ! handler ) return kInvalidId;
179 
180  auto h1 = static_cast<tools::histo::h1d*>(handler->object());
181  auto id = fH1Manager->AddH1(h1Name, h1);
182 
183 #ifdef G4VERBOSE
184  if ( fState.GetVerboseL2() )
185  fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
186 #endif
187 
188  return id;
189 }
190 
191 //_____________________________________________________________________________
193  const G4String& fileName,
194  const G4String& /*dirName*/,
195  G4bool /*isUserFileName*/)
196 {
197 #ifdef G4VERBOSE
198  if ( fState.GetVerboseL4() )
199  fState.GetVerboseL4()->Message("read", "h2", h2Name);
200 #endif
201 
202  auto handler
203  = GetHandler(fileName, h2Name, tools::histo::h2d::s_class(), "ReadH2Impl");
204  if ( ! handler ) return kInvalidId;
205 
206  auto h2 = static_cast<tools::histo::h2d*>(handler->object());
207  auto id = fH2Manager->AddH2(h2Name, h2);
208 
209 #ifdef G4VERBOSE
210  if ( fState.GetVerboseL2() )
211  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
212 #endif
213 
214  return id;
215 }
216 
217 //_____________________________________________________________________________
219  const G4String& fileName,
220  const G4String& /*dirName*/,
221  G4bool /*isUserFileName*/)
222 {
223 #ifdef G4VERBOSE
224  if ( fState.GetVerboseL4() )
225  fState.GetVerboseL4()->Message("read", "h3", h3Name);
226 #endif
227 
228  auto handler
229  = GetHandler(fileName, h3Name, tools::histo::h3d::s_class(), "ReadH3Impl");
230  if ( ! handler ) return kInvalidId;
231 
232  auto h3 = static_cast<tools::histo::h3d*>(handler->object());
233  auto id = fH3Manager->AddH3(h3Name, h3);
234 
235 #ifdef G4VERBOSE
236  if ( fState.GetVerboseL2() )
237  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
238 #endif
239 
240  return id;
241 }
242 
243 //_____________________________________________________________________________
245  const G4String& fileName,
246  const G4String& /*dirName*/,
247  G4bool /*isUserFileName*/)
248 {
249 #ifdef G4VERBOSE
250  if ( fState.GetVerboseL4() )
251  fState.GetVerboseL4()->Message("read", "p1", p1Name);
252 #endif
253 
254  auto handler
255  = GetHandler(fileName, p1Name, tools::histo::p1d::s_class(), "ReadP1Impl");
256  if ( ! handler ) return kInvalidId;
257 
258  auto p1 = static_cast<tools::histo::p1d*>(handler->object());
259  auto id = fP1Manager->AddP1(p1Name, p1);
260 
261 #ifdef G4VERBOSE
262  if ( fState.GetVerboseL2() )
263  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
264 #endif
265 
266  return id;
267 }
268 
269 //_____________________________________________________________________________
271  const G4String& fileName,
272  const G4String& /*dirName*/,
273  G4bool /*isUserFileName*/)
274 {
275 #ifdef G4VERBOSE
276  if ( fState.GetVerboseL4() )
277  fState.GetVerboseL4()->Message("read", "p2", p2Name);
278 #endif
279 
280  auto handler
281  = GetHandler(fileName, p2Name, tools::histo::p2d::s_class(), "ReadP2Impl");
282  if ( ! handler ) return kInvalidId;
283 
284  auto p2 = static_cast<tools::histo::p2d*>(handler->object());
285  auto id = fP2Manager->AddP2(p2Name, p2);
286 
287 #ifdef G4VERBOSE
288  if ( fState.GetVerboseL2() )
289  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
290 #endif
291 
292  return id;
293 }
294 
295 //_____________________________________________________________________________
297  const G4String& fileName,
298  const G4String& /*dirName*/,
299  G4bool isUserFileName)
300 {
301 #ifdef G4VERBOSE
302  if ( fState.GetVerboseL4() )
303  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
304 #endif
305 
306  // Ntuples are saved per object and per thread
307  // but apply the ntuple name and the thread suffixes
308  // only if fileName is not provided explicitly
309  auto fullFileName = fileName;
310  if ( ! isUserFileName ) {
311  fullFileName = fFileManager->GetNtupleFileName(ntupleName);
312  }
313 
314  auto handler
315  = GetHandler(fullFileName, ntupleName, tools::aida::ntuple::s_class(),
316  "ReadNtupleImpl");
317  if ( ! handler ) return kInvalidId;
318 
319  auto rntuple = static_cast<tools::aida::ntuple*>(handler->object());
321 
322 #ifdef G4VERBOSE
323  if ( fState.GetVerboseL2() )
324  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
325 #endif
326 
327  return id;
328 }