Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $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"
38 #include "G4AnalysisVerbose.hh"
39 #include "G4AnalysisUtilities.hh"
40 #include "G4Threading.hh"
41 
42 #include <iostream>
43 #include <cstdio>
44 
45 using namespace G4Analysis;
46 
49 
50 //_____________________________________________________________________________
52 {
53  if ( fgInstance == nullptr ) {
54  G4bool isMaster = ! G4Threading::IsWorkerThread();
55  fgInstance = new G4XmlAnalysisReader(isMaster);
56  }
57 
58  return fgInstance;
59 }
60 
61 //_____________________________________________________________________________
63  : G4ToolsAnalysisReader("Xml", isMaster),
64  fNtupleManager(nullptr),
65  fFileManager(nullptr)
66 {
67  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
68  G4ExceptionDescription description;
69  description
70  << " "
71  << "G4XmlAnalysisReader already exists."
72  << "Cannot create another instance.";
73  G4Exception("G4XmlAnalysisReader::G4XmlAnalysisReader()",
74  "Analysis_F001", FatalException, description);
75  }
76  if ( isMaster ) fgMasterInstance = this;
77  fgInstance = this;
78 
79  // Create managers
82  // The managers will be deleted by the base class
83 
84  // Set managers to base class
87 }
88 
89 //_____________________________________________________________________________
91 {
92  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
93  fgInstance = nullptr;
94 }
95 
96 //
97 // private methods
98 //
99 
100 //_____________________________________________________________________________
102  const G4String& fileName,
103  const G4String& objectName,
104  const G4String& objectType,
105  const G4String& inFunction)
106 {
107 // Get buffer for reading object specified by objectName and objectType
108 // for a file specified by fileName;
109 // open the file if it was not yet open
110 
111  // Histograms and profiles are not saved per thread
112  // and ntuple file name is already updated
113  auto rfile = fFileManager->GetRFile(fileName);
114  if ( ! rfile ) {
115  if ( ! fFileManager->OpenRFile(fileName) ) return nullptr;
116  rfile = fFileManager->GetRFile(fileName);
117  }
118 
119  tools::raxml_out* handler = nullptr;
120  if ( rfile ) {
121  std::vector<tools::raxml_out>& objects = rfile->objects();
122  std::vector<tools::raxml_out>::iterator it;
123  for (it = objects.begin(); it!=objects.end(); ++it) {
124  tools::raxml_out& object = *it;
125  if ( object.cls() == objectType && object.name() == objectName ) {
126  handler = &object;
127  break;
128  }
129  }
130  }
131 
132  if ( ! handler ) {
133  G4ExceptionDescription description;
134  description
135  << " "
136  << "Cannot get "<< objectName << " in file " << fileName;
137  G4String inFunctionFull = "G4XmlAnalysisReader::";
138  inFunctionFull.append(inFunction);
139  G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
140  return nullptr;
141  }
142 
143  return handler;
144 }
145 
146 //_____________________________________________________________________________
148 {
149 // Reset histograms and ntuple
150 
151  auto finalResult = true;
152 
154  finalResult = finalResult && result;
155 
156  result = fNtupleManager->Reset();
157  finalResult = finalResult && result;
158 
159  return finalResult;
160 }
161 
162 //
163 // protected methods
164 //
165 
166 //_____________________________________________________________________________
168  const G4String& fileName,
169  const G4String& /*dirName*/,
170  G4bool /*isUserFileName*/)
171 {
172 #ifdef G4VERBOSE
173  if ( fState.GetVerboseL4() )
174  fState.GetVerboseL4()->Message("read", "h1", h1Name);
175 #endif
176 
177  tools::raxml_out* handler
178  = GetHandler(fileName, h1Name, tools::histo::h1d::s_class(), "ReadH1Impl");
179  if ( ! handler ) return kInvalidId;
180 
181  auto h1 = static_cast<tools::histo::h1d*>(handler->object());
182  auto id = fH1Manager->AddH1(h1Name, h1);
183 
184 #ifdef G4VERBOSE
185  if ( fState.GetVerboseL2() )
186  fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
187 #endif
188 
189  return id;
190 }
191 
192 //_____________________________________________________________________________
194  const G4String& fileName,
195  const G4String& /*dirName*/,
196  G4bool /*isUserFileName*/)
197 {
198 #ifdef G4VERBOSE
199  if ( fState.GetVerboseL4() )
200  fState.GetVerboseL4()->Message("read", "h2", h2Name);
201 #endif
202 
203  auto handler
204  = GetHandler(fileName, h2Name, tools::histo::h2d::s_class(), "ReadH2Impl");
205  if ( ! handler ) return kInvalidId;
206 
207  auto h2 = static_cast<tools::histo::h2d*>(handler->object());
208  auto id = fH2Manager->AddH2(h2Name, h2);
209 
210 #ifdef G4VERBOSE
211  if ( fState.GetVerboseL2() )
212  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
213 #endif
214 
215  return id;
216 }
217 
218 //_____________________________________________________________________________
220  const G4String& fileName,
221  const G4String& /*dirName*/,
222  G4bool /*isUserFileName*/)
223 {
224 #ifdef G4VERBOSE
225  if ( fState.GetVerboseL4() )
226  fState.GetVerboseL4()->Message("read", "h3", h3Name);
227 #endif
228 
229  auto handler
230  = GetHandler(fileName, h3Name, tools::histo::h3d::s_class(), "ReadH3Impl");
231  if ( ! handler ) return kInvalidId;
232 
233  auto h3 = static_cast<tools::histo::h3d*>(handler->object());
234  auto id = fH3Manager->AddH3(h3Name, h3);
235 
236 #ifdef G4VERBOSE
237  if ( fState.GetVerboseL2() )
238  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
239 #endif
240 
241  return id;
242 }
243 
244 //_____________________________________________________________________________
246  const G4String& fileName,
247  const G4String& /*dirName*/,
248  G4bool /*isUserFileName*/)
249 {
250 #ifdef G4VERBOSE
251  if ( fState.GetVerboseL4() )
252  fState.GetVerboseL4()->Message("read", "p1", p1Name);
253 #endif
254 
255  auto handler
256  = GetHandler(fileName, p1Name, tools::histo::p1d::s_class(), "ReadP1Impl");
257  if ( ! handler ) return kInvalidId;
258 
259  auto p1 = static_cast<tools::histo::p1d*>(handler->object());
260  auto id = fP1Manager->AddP1(p1Name, p1);
261 
262 #ifdef G4VERBOSE
263  if ( fState.GetVerboseL2() )
264  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
265 #endif
266 
267  return id;
268 }
269 
270 //_____________________________________________________________________________
272  const G4String& fileName,
273  const G4String& /*dirName*/,
274  G4bool /*isUserFileName*/)
275 {
276 #ifdef G4VERBOSE
277  if ( fState.GetVerboseL4() )
278  fState.GetVerboseL4()->Message("read", "p2", p2Name);
279 #endif
280 
281  auto handler
282  = GetHandler(fileName, p2Name, tools::histo::p2d::s_class(), "ReadP2Impl");
283  if ( ! handler ) return kInvalidId;
284 
285  auto p2 = static_cast<tools::histo::p2d*>(handler->object());
286  auto id = fP2Manager->AddP2(p2Name, p2);
287 
288 #ifdef G4VERBOSE
289  if ( fState.GetVerboseL2() )
290  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
291 #endif
292 
293  return id;
294 }
295 
296 //_____________________________________________________________________________
298  const G4String& fileName,
299  const G4String& /*dirName*/,
300  G4bool isUserFileName)
301 {
302 #ifdef G4VERBOSE
303  if ( fState.GetVerboseL4() )
304  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
305 #endif
306 
307  // Ntuples are saved per object and per thread
308  // but apply the ntuple name and the thread suffixes
309  // only if fileName is not provided explicitly
310  auto fullFileName = fileName;
311  if ( ! isUserFileName ) {
312  fullFileName = fFileManager->GetNtupleFileName(ntupleName);
313  }
314 
315  auto handler
316  = GetHandler(fullFileName, ntupleName, tools::aida::ntuple::s_class(),
317  "ReadNtupleImpl");
318  if ( ! handler ) return kInvalidId;
319 
320  auto rntuple = static_cast<tools::aida::ntuple*>(handler->object());
322 
323 #ifdef G4VERBOSE
324  if ( fState.GetVerboseL2() )
325  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
326 #endif
327 
328  return id;
329 }
const G4AnalysisVerbose * GetVerboseL2() const
virtual G4int ReadH1Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
G4H3ToolsManager * fH3Manager
G4AnalysisManagerState fState
G4String GetNtupleFileName(const G4String &ntupleName) const
TTree * ntuple
TH1F * h3
G4XmlRNtupleManager * fNtupleManager
#define G4ThreadLocal
Definition: tls.hh:69
TH1D * h1d
void SetNtupleManager(G4VRNtupleManager *ntupleManager)
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
void SetFileManager(G4BaseFileManager *fileManager)
bool G4bool
Definition: G4Types.hh:79
G4H2ToolsManager * fH2Manager
tools::raxml * GetRFile(const G4String &fileName) const
virtual G4int ReadP2Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4int AddH3(const G4String &name, tools::histo::h3d *h3d)
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
virtual G4int ReadH3Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
const G4AnalysisVerbose * GetVerboseL4() const
G4int AddP1(const G4String &name, tools::histo::p1d *p1d)
G4P1ToolsManager * fP1Manager
const G4int kInvalidId
virtual G4int ReadH2Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
tools::raxml_out * GetHandler(const G4String &fileName, const G4String &objectName, const G4String &objectType, const G4String &inFunction)
virtual G4int ReadP1Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4double G4ParticleHPJENDLHEData::G4double result
static G4XmlAnalysisReader * fgMasterInstance
TH1F * h2
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4int SetNtuple(G4TRNtupleDescription< TNTUPLE > *rntupleDescription)
int G4int
Definition: G4Types.hh:78
G4int AddP2(const G4String &name, tools::histo::p2d *p2d)
G4P2ToolsManager * fP2Manager
G4XmlAnalysisReader(G4bool isMaster=true)
static G4XmlAnalysisReader * Instance()
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool OpenRFile(const G4String &fileName)
G4int AddH2(const G4String &name, tools::histo::h2d *h2d)
TH1F * h1
G4String & append(const G4String &)
G4H1ToolsManager * fH1Manager
G4XmlRFileManager * fFileManager
static G4ThreadLocal G4XmlAnalysisReader * fgInstance