EDepSimRootPersistencyManager.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////
2 //
3 
4 #include "EDepSimLog.hh"
5 
8 
9 #include <globals.hh>
10 
11 #include <G4Event.hh>
12 #include <G4Run.hh>
13 
14 #include <TROOT.h>
15 #include <TFile.h>
16 #include <TTree.h>
17 #include <TGeoManager.h>
18 
19 
21  : EDepSim::PersistencyManager(), fOutput(NULL), fEventTree(NULL) {}
22 
24  if (fOutput) delete fOutput;
25  fOutput = NULL;
26 }
27 
29  if (fOutput && fOutput->IsOpen()) {
30  fOutput->cd();
31  return true;
32  }
33  return false;
34 }
35 
37  if (fOutput) {
38  EDepSimLog("EDepSim::RootPersistencyManager::Open "
39  << "-- Delete current file pointer" );
40  }
41 
42  SetFilename(filename);
43 
44  EDepSimLog("EDepSim::RootPersistencyManager::Open " << GetFilename());
45 
46  fOutput = TFile::Open(GetFilename(), "RECREATE", "EDepSim Root Output");
47  fOutput->cd();
48 
49  fEventTree = new TTree("EDepSimEvents",
50  "Energy Deposition for Simulated Events");
51 
52  static TG4Event *pEvent = &fEventSummary;
53  fEventTree->Branch("Event","TG4Event",&pEvent);
54 
55  fEventsNotSaved = 0;
56 
57  return true;
58 }
59 
61  if (!fOutput) {
62  EDepSimError("EDepSim::RootPersistencyManager::Close "
63  << "-- No Output File");
64  return false;
65  }
66 
67  fOutput->cd();
68 
69  fOutput->Write();
70  fOutput->Close();
71 
72  fEventTree = NULL;
73 
74  return true;
75 }
76 
77 bool EDepSim::RootPersistencyManager::Store(const G4Event* anEvent) {
78  if (!fOutput) {
79  EDepSimError("EDepSim::RootPersistencyManager::Store "
80  << "-- No Output File");
81  return false;
82  }
83 
84  UpdateSummaries(anEvent);
85 
86  fOutput->cd();
87 
88  fEventTree->Fill();
89 
90  return true;
91 }
92 
94  return false;
95 }
96 
97 bool EDepSim::RootPersistencyManager::Store(const G4VPhysicalVolume*) {
98  if (!fOutput) {
99  EDepSimError("EDepSim::RootPersistencyManager::Store "
100  << "-- No Output File");
101  return false;
102  }
103  if (!gGeoManager) {
104  EDepSimError("EDepSim::RootPersistencyManage::Store(world)"
105  << " -- Cannot be run before /edep/update");
106  return false;
107  }
108  fOutput->cd();
109  gGeoManager->Write();
110  return true;
111 }
112 
#define EDepSimLog(outStream)
Definition: EDepSimLog.hh:717
int fEventsNotSaved
The number of events saved to the output file since the last write.
virtual G4bool Open(G4String dbname)
virtual G4String GetFilename(void) const
Return the output file name.
string filename
Definition: train.py:213
virtual G4bool Store(const G4Event *anEvent)
Stores an event to the output file.
Construct a module from components.
Definition: TG4HitSegment.h:10
TG4Event fEventSummary
A summary of the primary vertices in the event.
TFile * fOutput
The ROOT output file that events are saved into.
virtual G4bool Close(void)
Make sure the output file is closed.
#define EDepSimError(outStream)
Definition: EDepSimLog.hh:503
TTree * fEventTree
The event tree that contains the output events.
void UpdateSummaries(const G4Event *event)
Update the event summary fields.