Geant4  10.01.p03
G4XmlAnalysisReader.cc
Go to the documentation of this file.
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: G4XmlAnalysisReader.cc 74257 2013-10-02 14:24:55Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 25/07/2014 (ivana@ipno.in2p3.fr)
29 
30 #include "G4XmlAnalysisReader.hh"
31 #include "G4XmlRFileManager.hh"
32 #include "G4H1ToolsManager.hh"
33 #include "G4H2ToolsManager.hh"
34 #include "G4H3ToolsManager.hh"
35 #include "G4P1ToolsManager.hh"
36 #include "G4P2ToolsManager.hh"
37 #include "G4XmlRNtupleManager.hh"
39 #include "G4AnalysisVerbose.hh"
40 #include "G4AnalysisUtilities.hh"
41 #include "G4Threading.hh"
42 
43 #include <iostream>
44 #include <cstdio>
45 
46 using namespace G4Analysis;
47 
50 
51 //_____________________________________________________________________________
53 {
54  if ( fgInstance == 0 ) {
55  G4bool isMaster = ! G4Threading::IsWorkerThread();
56  fgInstance = new G4XmlAnalysisReader(isMaster);
57  }
58 
59  return fgInstance;
60 }
61 
62 //_____________________________________________________________________________
64  : G4VAnalysisReader("Xml", isMaster),
65  fH1Manager(0),
66  fH2Manager(0),
67  fH3Manager(0),
68  fP1Manager(0),
69  fP2Manager(0),
70  fNtupleManager(0),
71  fFileManager(0)
72 {
73  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
74  G4ExceptionDescription description;
75  description
76  << " "
77  << "G4XmlAnalysisReader already exists."
78  << "Cannot create another instance.";
79  G4Exception("G4XmlAnalysisReader::G4XmlAnalysisReader()",
80  "Analysis_F001", FatalException, description);
81  }
82  if ( isMaster ) fgMasterInstance = this;
83  fgInstance = this;
84 
85  // Create managers
93  // The managers will be deleted by the base class
94 
95  // Set managers to base class
103 }
104 
105 //_____________________________________________________________________________
107 {
108  if ( fState.GetIsMaster() ) fgMasterInstance = 0;
109  fgInstance = 0;
110 }
111 
112 //
113 // private methods
114 //
115 
116 //_____________________________________________________________________________
118  const G4String& fileName,
119  const G4String& objectName,
120  const G4String& objectType,
121  const G4String& inFunction)
122 {
123 // Get buffer for reading object specified by objectName and objectType
124 // for a file specified by fileName;
125 // open the file if it was not yet open
126 
127  // Histograms and profiles are not saved per thread
128  // and ntuple file name is already updated
129  tools::raxml* rfile = fFileManager->GetRFile(fileName);
130  if ( ! rfile ) {
131  if ( ! fFileManager->OpenRFile(fileName) ) return 0;
132  rfile = fFileManager->GetRFile(fileName);
133  }
134 
135  tools::raxml_out* handler = 0;
136  if ( rfile ) {
137  std::vector<tools::raxml_out>& objects = rfile->objects();
138  std::vector<tools::raxml_out>::iterator it;
139  for (it = objects.begin(); it!=objects.end(); ++it) {
140  tools::raxml_out& object = *it;
141  if ( object.cls() == objectType && object.name() == objectName ) {
142  handler = &object;
143  break;
144  }
145  }
146  }
147 
148  if ( ! handler ) {
149  G4ExceptionDescription description;
150  description
151  << " "
152  << "Cannot get "<< objectName << " in file " << fileName;
153  G4String inFunctionFull = "G4XmlAnalysisReader::";
154  inFunctionFull.append(inFunction);
155  G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
156  return 0;
157  }
158 
159  return handler;
160 }
161 
162 //_____________________________________________________________________________
164 {
165 // Reset histograms and ntuple
166 
167  G4bool finalResult = true;
168 
169  G4bool result = fH1Manager->Reset();
170  finalResult = finalResult && result;
171 
172  result = fH2Manager->Reset();
173  finalResult = finalResult && result;
174 
175  result = fNtupleManager->Reset();
176  finalResult = finalResult && result;
177 
178  return finalResult;
179 }
180 
181 //
182 // protected methods
183 //
184 
185 //_____________________________________________________________________________
187  const G4String& fileName,
188  G4bool /*isUserFileName*/)
189 {
190 #ifdef G4VERBOSE
191  if ( fState.GetVerboseL4() )
192  fState.GetVerboseL4()->Message("read", "h1", h1Name);
193 #endif
194 
195  tools::raxml_out* handler
196  = GetHandler(fileName, h1Name, tools::histo::h1d::s_class(), "ReadH1Impl");
197  if ( ! handler ) return kInvalidId;
198 
199  tools::histo::h1d* h1
200  = static_cast<tools::histo::h1d*>(handler->object());
201  G4int id = fH1Manager->AddH1(h1Name, h1);
202 
203 #ifdef G4VERBOSE
204  if ( fState.GetVerboseL2() )
205  fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
206 #endif
207 
208  return id;
209 }
210 
211 //_____________________________________________________________________________
213  const G4String& fileName,
214  G4bool /*isUserFileName*/)
215 {
216 #ifdef G4VERBOSE
217  if ( fState.GetVerboseL4() )
218  fState.GetVerboseL4()->Message("read", "h2", h2Name);
219 #endif
220 
221  tools::raxml_out* handler
222  = GetHandler(fileName, h2Name, tools::histo::h2d::s_class(), "ReadH2Impl");
223  if ( ! handler ) return kInvalidId;
224 
225  tools::histo::h2d* h2
226  = static_cast<tools::histo::h2d*>(handler->object());
227  G4int id = fH2Manager->AddH2(h2Name, h2);
228 
229 #ifdef G4VERBOSE
230  if ( fState.GetVerboseL2() )
231  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
232 #endif
233 
234  return id;
235 }
236 
237 //_____________________________________________________________________________
239  const G4String& fileName,
240  G4bool /*isUserFileName*/)
241 {
242 #ifdef G4VERBOSE
243  if ( fState.GetVerboseL4() )
244  fState.GetVerboseL4()->Message("read", "h3", h3Name);
245 #endif
246 
247  tools::raxml_out* handler
248  = GetHandler(fileName, h3Name, tools::histo::h3d::s_class(), "ReadH3Impl");
249  if ( ! handler ) return kInvalidId;
250 
251  tools::histo::h3d* h3
252  = static_cast<tools::histo::h3d*>(handler->object());
253  G4int id = fH3Manager->AddH3(h3Name, h3);
254 
255 #ifdef G4VERBOSE
256  if ( fState.GetVerboseL2() )
257  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
258 #endif
259 
260  return id;
261 }
262 
263 //_____________________________________________________________________________
265  const G4String& fileName,
266  G4bool /*isUserFileName*/)
267 {
268 #ifdef G4VERBOSE
269  if ( fState.GetVerboseL4() )
270  fState.GetVerboseL4()->Message("read", "p1", p1Name);
271 #endif
272 
273  tools::raxml_out* handler
274  = GetHandler(fileName, p1Name, tools::histo::p1d::s_class(), "ReadP1Impl");
275  if ( ! handler ) return kInvalidId;
276 
277  tools::histo::p1d* p1
278  = static_cast<tools::histo::p1d*>(handler->object());
279  G4int id = fP1Manager->AddP1(p1Name, p1);
280 
281 #ifdef G4VERBOSE
282  if ( fState.GetVerboseL2() )
283  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
284 #endif
285 
286  return id;
287 }
288 
289 //_____________________________________________________________________________
291  const G4String& fileName,
292  G4bool /*isUserFileName*/)
293 {
294 #ifdef G4VERBOSE
295  if ( fState.GetVerboseL4() )
296  fState.GetVerboseL4()->Message("read", "p2", p2Name);
297 #endif
298 
299  tools::raxml_out* handler
300  = GetHandler(fileName, p2Name, tools::histo::p2d::s_class(), "ReadP2Impl");
301  if ( ! handler ) return kInvalidId;
302 
303  tools::histo::p2d* p2
304  = static_cast<tools::histo::p2d*>(handler->object());
305  G4int id = fP2Manager->AddP2(p2Name, p2);
306 
307 #ifdef G4VERBOSE
308  if ( fState.GetVerboseL2() )
309  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
310 #endif
311 
312  return id;
313 }
314 
315 //_____________________________________________________________________________
317  const G4String& fileName,
318  G4bool isUserFileName)
319 {
320 #ifdef G4VERBOSE
321  if ( fState.GetVerboseL4() )
322  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
323 #endif
324 
325  // Ntuples are saved per object and per thread
326  // but apply the ntuple name and the thread suffixes
327  // only if fileName is not provided explicitly
328  G4String fullFileName = fileName;
329  if ( ! isUserFileName ) {
330  fullFileName = fFileManager->GetNtupleFileName(ntupleName);
331  }
332 
333  tools::raxml_out* handler
334  = GetHandler(fullFileName, ntupleName, tools::aida::ntuple::s_class(),
335  "ReadNtupleImpl");
336  if ( ! handler ) return kInvalidId;
337 
338  tools::aida::ntuple* rntuple
339  = static_cast<tools::aida::ntuple*>(handler->object());
340  G4int id
342 
343 #ifdef G4VERBOSE
344  if ( fState.GetVerboseL2() )
345  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
346 #endif
347 
348  return id;
349 }
virtual G4int ReadH2Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
G4H2ToolsManager * fH2Manager
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
void SetP1Manager(G4VP1Manager *p1Manager)
G4int AddH3(const G4String &name, tools::histo::h3d *h3d)
void SetP2Manager(G4VP2Manager *p2Manager)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetNtupleFileName(const G4String &ntupleName) const
G4XmlRFileManager * fFileManager
G4XmlRNtupleManager * fNtupleManager
virtual G4int ReadP1Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, G4bool isUserFileName)
#define G4ThreadLocal
Definition: tls.hh:89
G4AnalysisManagerState fState
int G4int
Definition: G4Types.hh:78
G4H1ToolsManager * fH1Manager
G4int AddP2(const G4String &name, tools::histo::p2d *p2d)
const G4AnalysisVerbose * GetVerboseL2() const
void SetH3Manager(G4VH3Manager *h3Manager)
static G4ThreadLocal G4XmlAnalysisReader * fgInstance
virtual G4int ReadP2Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
tools::raxml_out * GetHandler(const G4String &fileName, const G4String &objectName, const G4String &objectType, const G4String &inFunction)
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual G4int ReadH3Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
const G4double p2
const G4double p1
void SetNtupleManager(G4VRNtupleManager *ntupleManager)
virtual G4int ReadH1Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
static G4XmlAnalysisReader * fgMasterInstance
tools::raxml * GetRFile(const G4String &fileName) const
G4H3ToolsManager * fH3Manager
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
void SetH1Manager(G4VH1Manager *h1Manager)
G4int AddP1(const G4String &name, tools::histo::p1d *p1d)
G4String & append(const G4String &)
G4int AddH2(const G4String &name, tools::histo::h2d *h2d)
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
G4XmlAnalysisReader(G4bool isMaster=true)
G4P2ToolsManager * fP2Manager
G4int SetNtuple(G4XmlRNtupleDescription *rntupleDescription)
void SetH2Manager(G4VH2Manager *h2Manager)
virtual G4bool OpenRFile(const G4String &fileName)
static G4XmlAnalysisReader * Instance()
const G4int kInvalidId
void SetFileManager(G4BaseFileManager *fileManager)
G4P1ToolsManager * fP1Manager