Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4RootAnalysisReader.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: G4RootAnalysisReader.cc 74257 2013-10-02 14:24:55Z gcosmo $
27 
28 // Author: Ivana Hrivnacova, 09/04/2014 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootAnalysisReader.hh"
31 #include "G4RootRFileManager.hh"
32 #include "G4RootRNtupleManager.hh"
33 #include "G4AnalysisVerbose.hh"
34 #include "G4AnalysisUtilities.hh"
35 #include "G4Threading.hh"
36 
37 #include <tools/rroot/file>
38 #include <tools/rroot/streamers>
39 #include <tools/rroot/fac>
40 #include <tools/rroot/tree>
41 #include <tools/rroot/ntuple>
42 
43 #include <iostream>
44 #include <cstdio>
45 
46 using namespace G4Analysis;
47 
50 
51 //_____________________________________________________________________________
53 {
54  if ( fgInstance == nullptr ) {
55  G4bool isMaster = ! G4Threading::IsWorkerThread();
56  fgInstance = new G4RootAnalysisReader(isMaster);
57  }
58 
59  return fgInstance;
60 }
61 
62 //_____________________________________________________________________________
64  : G4ToolsAnalysisReader("Root", isMaster),
65  fNtupleManager(nullptr),
66  fFileManager(nullptr)
67 {
68  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
69  G4ExceptionDescription description;
70  description
71  << " "
72  << "G4RootAnalysisReader already exists."
73  << "Cannot create another instance.";
74  G4Exception("G4RootAnalysisReader::G4RootAnalysisReader()",
75  "Analysis_F001", FatalException, description);
76  }
77  if ( isMaster ) fgMasterInstance = this;
78  fgInstance = this;
79 
80  // Create managers
83  // The managers will be deleted by the base class
84 
85  // Set managers to base class
88 }
89 
90 //_____________________________________________________________________________
92 {
93  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
94  fgInstance = nullptr;
95 }
96 
97 //
98 // private methods
99 //
100 
101 //_____________________________________________________________________________
103  const G4String& fileName,
104  const G4String& objectName,
105  const G4String& inFunction)
106 {
107 // Get buffer for reading histogram or profile specified by objectNmae
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  G4bool isPerThread = false;
113 
114  // Get or open a file
115  auto rfile = fFileManager->GetRFile(fileName, isPerThread);
116  if ( ! rfile ) {
117  if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return nullptr;
118  rfile = fFileManager->GetRFile(fileName, isPerThread);
119  }
120 
121  auto key
122  = ( ! rfile ) ? nullptr : rfile->dir().find_key(objectName);
123 
124  unsigned int size;
125  //char* charBuffer
126  // = ( ! key ) ? 0 : key->get_object_buffer(size);
127  char* charBuffer = 0;
128  if ( key ) charBuffer = key->get_object_buffer(*rfile, size);
129 
130  if ( ! charBuffer ) {
131  G4ExceptionDescription description;
132  description
133  << " "
134  << "Cannot get " << objectName << " in file " << fileName;
135  G4Exception(inFunction, "Analysis_WR011", JustWarning, description);
136  return nullptr;
137  }
138 
139  auto verbose = false;
140  return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
141  key->key_length(), verbose);
142 }
143 
144 //_____________________________________________________________________________
146 {
147 // Reset histograms and ntuple
148 
149  auto finalResult = true;
150 
152  finalResult = finalResult && result;
153 
154  result = fNtupleManager->Reset();
155  finalResult = finalResult && result;
156 
157  return finalResult;
158 }
159 
160 //
161 // protected methods
162 //
163 
164 //_____________________________________________________________________________
166  const G4String& fileName,
167  const G4String& /*dirName*/,
168  G4bool /*isUserFileName*/)
169 {
170 #ifdef G4VERBOSE
171  if ( fState.GetVerboseL4() )
172  fState.GetVerboseL4()->Message("read", "h1", h1Name);
173 #endif
174 
175  auto buffer = GetBuffer(fileName, h1Name, "ReadH1Impl");
176  if ( ! buffer ) return kInvalidId;
177 
178  auto h1 = tools::rroot::TH1D_stream(*buffer);
179  delete buffer;
180 
181  if ( ! h1 ) {
182  G4ExceptionDescription description;
183  description
184  << " "
185  << "Streaming " << h1Name << " in file " << fileName << " failed.";
186  G4Exception("G4RootAnalysisReader::ReadH1Impl",
187  "Analysis_WR011", JustWarning, description);
188  return kInvalidId;
189  }
190 
191  auto id = fH1Manager->AddH1(h1Name, h1);
192 
193 #ifdef G4VERBOSE
194  if ( fState.GetVerboseL2() )
195  fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
196 #endif
197 
198  return id;
199 }
200 
201 //_____________________________________________________________________________
203  const G4String& fileName,
204  const G4String& /*dirName*/,
205  G4bool /*isUserFileName*/)
206 {
207 #ifdef G4VERBOSE
208  if ( fState.GetVerboseL4() )
209  fState.GetVerboseL4()->Message("read", "h2", h2Name);
210 #endif
211 
212  auto buffer = GetBuffer(fileName, h2Name, "ReadH2Impl");
213  if ( ! buffer ) return kInvalidId;
214 
215  // if h2Name represents H1, then we get !!
216  // tools::rroot::buffer::check_byte_count : object of class "TNamed" read too few bytes (603979762 missing).
217  // tools::rroot::buffer::check_byte_count : "TNamed" streamer not in sync with data on file, fix streamer.
218  // Segmentation fault (core dumped)
219 
220  auto h2 = tools::rroot::TH2D_stream(*buffer);
221  delete buffer;
222 
223  if ( ! h2 ) {
224  G4ExceptionDescription description;
225  description
226  << " "
227  << "Streaming " << h2Name << " in file " << fileName << " failed.";
228  G4Exception("G4RootAnalysisReader::ReadH2Impl",
229  "Analysis_WR011", JustWarning, description);
230  return kInvalidId;
231  }
232 
233  auto id = fH2Manager->AddH2(h2Name, h2);
234 
235 #ifdef G4VERBOSE
236  if ( fState.GetVerboseL2() )
237  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
238 #endif
239 
240  return id;
241 }
242 
243 //_____________________________________________________________________________
245  const G4String& fileName,
246  const G4String& /*dirName*/,
247  G4bool /*isUserFileName*/)
248 {
249 
250 #ifdef G4VERBOSE
251  if ( fState.GetVerboseL4() )
252  fState.GetVerboseL4()->Message("read", "h3", h3Name);
253 #endif
254 
255  auto buffer = GetBuffer(fileName, h3Name, "ReadH3Impl");
256  if ( ! buffer ) return kInvalidId;
257 
258  auto h3 = tools::rroot::TH3D_stream(*buffer);
259  delete buffer;
260 
261  if ( ! h3 ) {
262  G4ExceptionDescription description;
263  description
264  << " "
265  << "Streaming " << h3Name << " in file " << fileName << " failed.";
266  G4Exception("G4RootAnalysisReader::ReadH3Impl",
267  "Analysis_WR011", JustWarning, description);
268  return kInvalidId;
269  }
270 
271  auto id = fH3Manager->AddH3(h3Name, h3);
272 
273 #ifdef G4VERBOSE
274  if ( fState.GetVerboseL2() )
275  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
276 #endif
277 
278  return id;
279 /*
280  // not yet available
281  return kInvalidId;
282 */
283 }
284 
285 //_____________________________________________________________________________
287  const G4String& fileName,
288  const G4String& /*dirName*/,
289  G4bool /*isUserFileName*/)
290 {
291 #ifdef G4VERBOSE
292  if ( fState.GetVerboseL4() )
293  fState.GetVerboseL4()->Message("read", "p1", p1Name);
294 #endif
295 
296  auto buffer = GetBuffer(fileName, p1Name, "ReadP1Impl");
297  if ( ! buffer ) return kInvalidId;
298 
299  auto p1 = tools::rroot::TProfile_stream(*buffer);
300  delete buffer;
301 
302  if ( ! p1 ) {
303  G4ExceptionDescription description;
304  description
305  << " "
306  << "Streaming " << p1Name << " in file " << fileName << " failed.";
307  G4Exception("G4RootAnalysisReader::ReadP1Impl",
308  "Analysis_WR011", JustWarning, description);
309  return kInvalidId;
310  }
311 
312  auto id = fP1Manager->AddP1(p1Name, p1);
313 
314 #ifdef G4VERBOSE
315  if ( fState.GetVerboseL2() )
316  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
317 #endif
318 
319  return id;
320 }
321 
322 //_____________________________________________________________________________
324  const G4String& fileName,
325  const G4String& /*dirName*/,
326  G4bool /*isUserFileName*/)
327 {
328 
329 #ifdef G4VERBOSE
330  if ( fState.GetVerboseL4() )
331  fState.GetVerboseL4()->Message("read", "p2", p2Name);
332 #endif
333 
334  auto buffer = GetBuffer(fileName, p2Name, "ReadP2Impl");
335  if ( ! buffer ) return kInvalidId;
336 
337  auto p2 = tools::rroot::TProfile2D_stream(*buffer);
338  delete buffer;
339 
340  if ( ! p2 ) {
341  G4ExceptionDescription description;
342  description
343  << " "
344  << "Streaming " << p2Name << " in file " << fileName << " failed.";
345  G4Exception("G4RootAnalysisReader::ReadP2Impl",
346  "Analysis_WR011", JustWarning, description);
347  return kInvalidId;
348  }
349 
350  auto id = fP2Manager->AddP2(p2Name, p2);
351 
352 #ifdef G4VERBOSE
353  if ( fState.GetVerboseL2() )
354  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
355 #endif
356 
357  return id;
358 }
359 
360 //_____________________________________________________________________________
362  const G4String& fileName,
363  const G4String& /*dirName*/,
364  G4bool isUserFileName)
365 {
366 #ifdef G4VERBOSE
367  if ( fState.GetVerboseL4() )
368  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
369 #endif
370 
371  // Ntuples are saved per thread
372  // but do not apply the thread suffix if fileName is provided explicitly
373  auto isPerThread = true;
374  if ( isUserFileName ) isPerThread = false;
375 
376  // Get or open a file
377  auto rfile = fFileManager->GetRFile(fileName, isPerThread);
378  if ( ! rfile ) {
379  if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId;
380  rfile = fFileManager->GetRFile(fileName, isPerThread);
381  }
382 
383  auto key = rfile->dir().find_key(ntupleName);
384  if ( ! key ) {
385  G4ExceptionDescription description;
386  description
387  << " "
388  << "Key " << ntupleName << " for Ntuple not found in file " << fileName;
389  G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
390  "Analysis_WR011", JustWarning, description);
391  return kInvalidId;
392  }
393 
394  unsigned int size;
395  char* charBuffer = key->get_object_buffer(*rfile, size);
396  if ( ! charBuffer ) {
397  G4ExceptionDescription description;
398  description
399  << " "
400  << "Cannot get data buffer for Ntuple " << ntupleName << " in file " << fileName;
401  G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
402  "Analysis_WR021", JustWarning, description);
403  return kInvalidId;
404  }
405 
406  auto verbose = false;
407  auto buffer
408  = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
409  key->key_length(), verbose);
410  buffer->set_map_objs(true);
411 
412  auto fac = new tools::rroot::fac(G4cout);
413 
414  auto tree = new tools::rroot::tree(*rfile, *fac);
415  if ( ! tree->stream(*buffer) ) {
416  G4ExceptionDescription description;
417  description
418  << " "
419  << "TTree streaming failed for Ntuple " << ntupleName << " in file " << fileName;
420  G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
421  "Analysis_WR021", JustWarning, description);
422 
423  delete buffer;
424  delete tree;
425  return kInvalidId;
426  }
427 
428  auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API.
429  auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple);
430 
431  auto id = fNtupleManager->SetNtuple(rntupleDescription);
432 
433 #ifdef G4VERBOSE
434  if ( fState.GetVerboseL2() )
435  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
436 #endif
437 
438  return id;
439 }
const G4AnalysisVerbose * GetVerboseL2() const
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
G4H3ToolsManager * fH3Manager
G4AnalysisManagerState fState
tools::rroot::file * GetRFile(const G4String &fileName, G4bool isPerThread) const
TTree * ntuple
TH1F * h3
#define buffer
Definition: xmlparse.cc:628
virtual G4int ReadP1Impl(const G4String &p1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
#define G4ThreadLocal
Definition: tls.hh:69
void SetNtupleManager(G4VRNtupleManager *ntupleManager)
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
void SetFileManager(G4BaseFileManager *fileManager)
static G4RootAnalysisReader * fgMasterInstance
G4RootRNtupleManager * fNtupleManager
virtual G4int ReadH3Impl(const G4String &h3Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
bool G4bool
Definition: G4Types.hh:79
G4H2ToolsManager * fH2Manager
G4RootRFileManager * fFileManager
G4int AddH3(const G4String &name, tools::histo::h3d *h3d)
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
static G4RootAnalysisReader * Instance()
const G4AnalysisVerbose * GetVerboseL4() const
virtual G4int ReadP2Impl(const G4String &p2Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4int AddP1(const G4String &name, tools::histo::p1d *p1d)
G4P1ToolsManager * fP1Manager
const G4int kInvalidId
virtual G4int ReadH2Impl(const G4String &h2Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4double G4ParticleHPJENDLHEData::G4double result
static G4ThreadLocal G4RootAnalysisReader * fgInstance
TH1F * h2
virtual G4bool OpenRFile(const G4String &fileName, G4bool isPerThread)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4int SetNtuple(G4TRNtupleDescription< TNTUPLE > *rntupleDescription)
tools::rroot::buffer * GetBuffer(const G4String &fileName, const G4String &name, const G4String &inFunction)
virtual G4int ReadH1Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
int G4int
Definition: G4Types.hh:78
G4int AddP2(const G4String &name, tools::histo::p2d *p2d)
G4P2ToolsManager * fP2Manager
static const G4double fac
G4RootAnalysisReader(G4bool isMaster=true)
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4GLOB_DLL std::ostream G4cout
G4int AddH2(const G4String &name, tools::histo::h2d *h2d)
TH1F * h1
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4H1ToolsManager * fH1Manager