Geant4  10.01.p01
G4RootAnalysisReader.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: 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 "G4H1ToolsManager.hh"
33 #include "G4H2ToolsManager.hh"
34 #include "G4H3ToolsManager.hh"
35 #include "G4P1ToolsManager.hh"
36 #include "G4P2ToolsManager.hh"
37 #include "G4RootRNtupleManager.hh"
39 #include "G4AnalysisVerbose.hh"
40 #include "G4AnalysisUtilities.hh"
41 #include "G4Threading.hh"
42 
43 #include <tools/rroot/file>
44 #include <tools/rroot/streamers>
45 #include <tools/rroot/fac>
46 #include <tools/rroot/tree>
47 #include <tools/rroot/ntuple>
48 
49 #include <iostream>
50 #include <cstdio>
51 
52 using namespace G4Analysis;
53 
56 
57 //_____________________________________________________________________________
59 {
60  if ( fgInstance == 0 ) {
61  G4bool isMaster = ! G4Threading::IsWorkerThread();
62  fgInstance = new G4RootAnalysisReader(isMaster);
63  }
64 
65  return fgInstance;
66 }
67 
68 //_____________________________________________________________________________
70  : G4VAnalysisReader("Root", isMaster),
71  fH1Manager(0),
72  fH2Manager(0),
73  fH3Manager(0),
74  fP1Manager(0),
75  fP2Manager(0),
76  fNtupleManager(0),
77  fFileManager(0)
78 {
79  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
80  G4ExceptionDescription description;
81  description
82  << " "
83  << "G4RootAnalysisReader already exists."
84  << "Cannot create another instance.";
85  G4Exception("G4RootAnalysisReader::G4RootAnalysisReader()",
86  "Analysis_F001", FatalException, description);
87  }
88  if ( isMaster ) fgMasterInstance = this;
89  fgInstance = this;
90 
91  // Create managers
99  // The managers will be deleted by the base class
100 
101  // Set managers to base class
109 }
110 
111 //_____________________________________________________________________________
113 {
114  if ( fState.GetIsMaster() ) fgMasterInstance = 0;
115  fgInstance = 0;
116 }
117 
118 //
119 // private methods
120 //
121 
122 //_____________________________________________________________________________
124  const G4String& fileName,
125  const G4String& objectName,
126  const G4String& inFunction)
127 {
128 // Get buffer for reading histogram or profile specified by objectNmae
129 // for a file specified by fileName;
130 // open the file if it was not yet open
131 
132  // Histograms and profiles are not saved per thread
133  G4bool isPerThread = false;
134 
135  // Get or open a file
136  tools::rroot::file* rfile
137  = fFileManager->GetRFile(fileName, isPerThread);
138  if ( ! rfile ) {
139  if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return 0;
140  rfile = fFileManager->GetRFile(fileName, isPerThread);
141  }
142 
143  tools::rroot::key* key
144  = ( ! rfile ) ? 0 : rfile->dir().find_key(objectName);
145 
146  unsigned int size;
147  //char* charBuffer
148  // = ( ! key ) ? 0 : key->get_object_buffer(size);
149  char* charBuffer = 0;
150  if ( key ) charBuffer = key->get_object_buffer(size);
151 
152  if ( ! charBuffer ) {
153  G4ExceptionDescription description;
154  description
155  << " "
156  << "Cannot get " << objectName << " in file " << fileName;
157  G4Exception(inFunction, "Analysis_WR011", JustWarning, description);
158  return 0;
159  }
160 
161  G4bool verbose = false;
162  return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
163  key->key_length(), verbose);
164 }
165 
166 //_____________________________________________________________________________
168 {
169 // Reset histograms and ntuple
170 
171  G4bool finalResult = true;
172 
173  G4bool result = fH1Manager->Reset();
174  finalResult = finalResult && result;
175 
176  result = fH2Manager->Reset();
177  finalResult = finalResult && result;
178 
179  result = fH3Manager->Reset();
180  finalResult = finalResult && result;
181 
182  result = fP1Manager->Reset();
183  finalResult = finalResult && result;
184 
185  result = fP2Manager->Reset();
186  finalResult = finalResult && result;
187 
188  result = fNtupleManager->Reset();
189  finalResult = finalResult && result;
190 
191  return finalResult;
192 }
193 
194 //
195 // protected methods
196 //
197 
198 //_____________________________________________________________________________
200  const G4String& fileName,
201  G4bool /*isUserFileName*/)
202 {
203 #ifdef G4VERBOSE
204  if ( fState.GetVerboseL4() )
205  fState.GetVerboseL4()->Message("read", "h1", h1Name);
206 #endif
207 
209  = GetBuffer(fileName, h1Name, "ReadH1Impl");
210  if ( ! buffer ) return kInvalidId;
211 
212  tools::histo::h1d* h1 = tools::rroot::TH1D_stream(*buffer);
213  delete buffer;
214 
215  if ( ! h1 ) {
216  G4ExceptionDescription description;
217  description
218  << " "
219  << "Streaming " << h1Name << " in file " << fileName << " failed.";
220  G4Exception("G4RootAnalysisReader::ReadH1Impl",
221  "Analysis_WR011", JustWarning, description);
222  return kInvalidId;
223  }
224 
225  G4int id = fH1Manager->AddH1(h1Name, h1);
226 
227 #ifdef G4VERBOSE
228  if ( fState.GetVerboseL2() )
229  fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
230 #endif
231 
232  return id;
233 }
234 
235 //_____________________________________________________________________________
237  const G4String& fileName,
238  G4bool /*isUserFileName*/)
239 {
240 #ifdef G4VERBOSE
241  if ( fState.GetVerboseL4() )
242  fState.GetVerboseL4()->Message("read", "h2", h2Name);
243 #endif
244 
245  tools::rroot::buffer* buffer = GetBuffer(fileName, h2Name, "ReadH2Impl");
246  if ( ! buffer ) return kInvalidId;
247 
248  // if h2Name represents H1, then we get !!
249  // tools::rroot::buffer::check_byte_count : object of class "TNamed" read too few bytes (603979762 missing).
250  // tools::rroot::buffer::check_byte_count : "TNamed" streamer not in sync with data on file, fix streamer.
251  // Segmentation fault (core dumped)
252 
253  tools::histo::h2d* h2 = tools::rroot::TH2D_stream(*buffer);
254  delete buffer;
255 
256  if ( ! h2 ) {
257  G4ExceptionDescription description;
258  description
259  << " "
260  << "Streaming " << h2Name << " in file " << fileName << " failed.";
261  G4Exception("G4RootAnalysisReader::ReadH2Impl",
262  "Analysis_WR011", JustWarning, description);
263  return kInvalidId;
264  }
265 
266  G4int id = fH2Manager->AddH2(h2Name, h2);
267 
268 #ifdef G4VERBOSE
269  if ( fState.GetVerboseL2() )
270  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
271 #endif
272 
273  return id;
274 }
275 
276 //_____________________________________________________________________________
278  const G4String& fileName,
279  G4bool /*isUserFileName*/)
280 {
281 
282 #ifdef G4VERBOSE
283  if ( fState.GetVerboseL4() )
284  fState.GetVerboseL4()->Message("read", "h3", h3Name);
285 #endif
286 
287  tools::rroot::buffer* buffer = GetBuffer(fileName, h3Name, "ReadH3Impl");
288  if ( ! buffer ) return kInvalidId;
289 
290  tools::histo::h3d* h3 = tools::rroot::TH3D_stream(*buffer);
291  delete buffer;
292 
293  if ( ! h3 ) {
294  G4ExceptionDescription description;
295  description
296  << " "
297  << "Streaming " << h3Name << " in file " << fileName << " failed.";
298  G4Exception("G4RootAnalysisReader::ReadH3Impl",
299  "Analysis_WR011", JustWarning, description);
300  return kInvalidId;
301  }
302 
303  G4int id = fH3Manager->AddH3(h3Name, h3);
304 
305 #ifdef G4VERBOSE
306  if ( fState.GetVerboseL2() )
307  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
308 #endif
309 
310  return id;
311 /*
312  // not yet available
313  return kInvalidId;
314 */
315 }
316 
317 //_____________________________________________________________________________
319  const G4String& fileName,
320  G4bool /*isUserFileName*/)
321 {
322 #ifdef G4VERBOSE
323  if ( fState.GetVerboseL4() )
324  fState.GetVerboseL4()->Message("read", "p1", p1Name);
325 #endif
326 
327  tools::rroot::buffer* buffer = GetBuffer(fileName, p1Name, "ReadP1Impl");
328  if ( ! buffer ) return kInvalidId;
329 
330  tools::histo::p1d* p1 = tools::rroot::TProfile_stream(*buffer);
331  delete buffer;
332 
333  if ( ! p1 ) {
334  G4ExceptionDescription description;
335  description
336  << " "
337  << "Streaming " << p1Name << " in file " << fileName << " failed.";
338  G4Exception("G4RootAnalysisReader::ReadP1Impl",
339  "Analysis_WR011", JustWarning, description);
340  return kInvalidId;
341  }
342 
343  G4int id = fP1Manager->AddP1(p1Name, p1);
344 
345 #ifdef G4VERBOSE
346  if ( fState.GetVerboseL2() )
347  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
348 #endif
349 
350  return id;
351 }
352 
353 //_____________________________________________________________________________
355  const G4String& fileName,
356  G4bool /*isUserFileName*/)
357 {
358 
359 #ifdef G4VERBOSE
360  if ( fState.GetVerboseL4() )
361  fState.GetVerboseL4()->Message("read", "p2", p2Name);
362 #endif
363 
364  tools::rroot::buffer* buffer = GetBuffer(fileName, p2Name, "ReadP2Impl");
365  if ( ! buffer ) return kInvalidId;
366 
367  tools::histo::p2d* p2 = tools::rroot::TProfile2D_stream(*buffer);
368  delete buffer;
369 
370  if ( ! p2 ) {
371  G4ExceptionDescription description;
372  description
373  << " "
374  << "Streaming " << p2Name << " in file " << fileName << " failed.";
375  G4Exception("G4RootAnalysisReader::ReadP2Impl",
376  "Analysis_WR011", JustWarning, description);
377  return kInvalidId;
378  }
379 
380  G4int id = fP2Manager->AddP2(p2Name, p2);
381 
382 #ifdef G4VERBOSE
383  if ( fState.GetVerboseL2() )
384  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
385 #endif
386 
387  return id;
388 }
389 
390 //_____________________________________________________________________________
392  const G4String& fileName,
393  G4bool isUserFileName)
394 {
395 #ifdef G4VERBOSE
396  if ( fState.GetVerboseL4() )
397  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
398 #endif
399 
400  // Ntuples are saved per thread
401  // but do not apply the thread suffix if fileName is provided explicitly
402  G4bool isPerThread = true;
403  if ( isUserFileName ) isPerThread = false;
404 
405  // Get or open a file
406  tools::rroot::file* rfile
407  = fFileManager->GetRFile(fileName, isPerThread);
408  if ( ! rfile ) {
409  if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId;
410  rfile = fFileManager->GetRFile(fileName, isPerThread);
411  }
412 
413  tools::rroot::key* key = rfile->dir().find_key(ntupleName);
414  if ( ! key ) {
415  G4ExceptionDescription description;
416  description
417  << " "
418  << "Key " << ntupleName << " for Ntuple not found in file " << fileName;
419  G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
420  "Analysis_WR011", JustWarning, description);
421  return kInvalidId;
422  }
423 
424  unsigned int size;
425  char* charBuffer = key->get_object_buffer(size);
426  if ( ! charBuffer ) {
427  G4ExceptionDescription description;
428  description
429  << " "
430  << "Cannot get data buffer for Ntuple " << ntupleName << " in file " << fileName;
431  G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
432  "Analysis_WR021", JustWarning, description);
433  return kInvalidId;
434  }
435 
436  G4bool verbose = false;
438  = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
439  key->key_length(), verbose);
441  = new tools::rroot::fac(*rfile);
442 
443  tools::rroot::tree* tree
444  = new tools::rroot::tree(*rfile, *fac);
445  if ( ! tree->stream(*buffer) ) {
446  G4ExceptionDescription description;
447  description
448  << " "
449  << "TTree streaming failed for Ntuple " << ntupleName << " in file " << fileName;
450  G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
451  "Analysis_WR021", JustWarning, description);
452 
453  delete buffer;
454  delete tree;
455  return kInvalidId;
456  }
457 
458  tools::rroot::ntuple* rntuple
459  = new tools::rroot::ntuple(*tree); //use the flat ntuple API.
460  G4RootRNtupleDescription* rntupleDescription
461  = new G4RootRNtupleDescription(rntuple, buffer, fac, tree);
462 
463  G4int id = fNtupleManager->SetNtuple(rntupleDescription);
464 
465 #ifdef G4VERBOSE
466  if ( fState.GetVerboseL2() )
467  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
468 #endif
469 
470  return id;
471 }
virtual G4int ReadH2Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4H2ToolsManager * fH2Manager
void SetP1Manager(G4VP1Manager *p1Manager)
G4int AddH3(const G4String &name, tools::histo::h3d *h3d)
void SetP2Manager(G4VP2Manager *p2Manager)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4H1ToolsManager * fH1Manager
#define buffer
Definition: xmlparse.cc:611
G4RootRFileManager * fFileManager
#define G4ThreadLocal
Definition: tls.hh:89
G4AnalysisManagerState fState
int G4int
Definition: G4Types.hh:78
G4int AddP2(const G4String &name, tools::histo::p2d *p2d)
const G4AnalysisVerbose * GetVerboseL2() const
void SetH3Manager(G4VH3Manager *h3Manager)
G4RootRNtupleManager * fNtupleManager
G4int SetNtuple(G4RootRNtupleDescription *rntupleDescription)
virtual G4bool OpenRFile(const G4String &fileName, G4bool isPerThread)
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL4() const
static G4ThreadLocal G4RootAnalysisReader * fgInstance
bool G4bool
Definition: G4Types.hh:79
tools::rroot::file * GetRFile(const G4String &fileName, G4bool isPerThread) const
void SetNtupleManager(G4VRNtupleManager *ntupleManager)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
virtual G4int ReadP1Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
void SetH1Manager(G4VH1Manager *h1Manager)
G4int AddP1(const G4String &name, tools::histo::p1d *p1d)
static G4RootAnalysisReader * Instance()
G4int AddH2(const G4String &name, tools::histo::h2d *h2d)
tools::rroot::buffer * GetBuffer(const G4String &fileName, const G4String &name, const G4String &inFunction)
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
static const G4double fac
static G4RootAnalysisReader * fgMasterInstance
virtual G4int ReadH3Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
G4P2ToolsManager * fP2Manager
G4RootAnalysisReader(G4bool isMaster=true)
G4P1ToolsManager * fP1Manager
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, G4bool isUserFileName)
virtual G4int ReadP2Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
void SetH2Manager(G4VH2Manager *h2Manager)
const G4int kInvalidId
virtual G4int ReadH1Impl(const G4String &h1Name, const G4String &fileName, G4bool isUserFileName)
G4H3ToolsManager * fH3Manager
void SetFileManager(G4BaseFileManager *fileManager)