otsdaq  v1_01_04
 All Classes Namespaces Functions
MonicelliFileReader.cpp
1 #include "otsdaq-core/MonicelliInterface/MonicelliFileReader.h"
2 
3 #include "otsdaq-core/MonicelliInterface/Event.h"
4 #include "otsdaq-core/MonicelliInterface/EventHeader.h"
5 #include "otsdaq-core/MonicelliInterface/Geometry.h"
6 
7 #include <TFile.h>
8 #include <TTree.h>
9 #include <TBranch.h>
10 
11 using namespace ots;
12 
13 //========================================================================================================================
14 MonicelliFileReader::MonicelliFileReader(void) :
15  argv (1),
16  argc (new char*[1]),
17  theApp_ ("MonicelliFileReaderApplication",&argv,argc),
18  theGeoFile_ (0),
19  theEventsFile_ (0),
20  inputGeometryTree_(0),
21  inputEventTree_ (0),
22  inputEventHeader_ (0),
23  theEvent_ (new monicelli::Event()),
24  theEventHeader_ (new monicelli::EventHeader()),
25  theGeometry_ (new monicelli::Geometry()),
26  theEventBranch_ (0),
27  theEventTree_ (0)
28 {
29 }
30 
31 //========================================================================================================================
32 MonicelliFileReader::~MonicelliFileReader(void)
33 {
34  delete theEvent_ ;
35  delete theGeometry_ ;
36  delete theEventHeader_;
37 }
38 
39 //========================================================================================================================
40 bool MonicelliFileReader::openGeoFile(std::string fileName)
41 {
42  inputGeometryTree_ = 0;
43  closeGeoFile();
44  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Opening file " << fileName << std::endl;
45  TFile* theGeoFile_ = TFile::Open(fileName.c_str(), "read");
46  if( !theGeoFile_->IsOpen() )
47  {
48  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Can't open file " << fileName << std::endl;
49  return false;
50  }
51 
52  const std::string geometryTreeName = "Geometry";
53  if ( (TTree*)theGeoFile_->Get(geometryTreeName.c_str()) )
54  {
55  inputGeometryTree_ = (TTree*)theGeoFile_->Get(geometryTreeName.c_str());
56  inputGeometryTree_->SetBranchAddress("GeometryBranch", &theGeometry_);
57  inputGeometryTree_->GetEntry(0);
58  }
59  return (inputGeometryTree_ != 0);
60 }
61 
62 //========================================================================================================================
63 bool MonicelliFileReader::openEventsFile(std::string fileName)
64 {
65  inputEventTree_ = 0;
66  inputEventHeader_ = 0;
67 
68  closeEventsFile();
69  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Opening file " << fileName << std::endl;
70  TFile* theEventsFile_ = TFile::Open(fileName.c_str(), "read");
71  if( !theEventsFile_->IsOpen() )
72  {
73  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Can't open file " << fileName << std::endl;
74  return false;
75  }
76 
77  std::string eventsTreeName = fileName + "Events";
78 std::string eventsHeaderName = fileName + "Header";
79  if( (inputEventTree_ = (TTree*)theEventsFile_->Get(eventsTreeName.c_str())) )
80  {
81  inputEventTree_->SetBranchAddress("EventBranch", &theEvent_);
82  inputEventTree_->GetEntry(0);
83 
84  if( (inputEventHeader_ = (TTree*)theEventsFile_->Get(eventsHeaderName.c_str())) )
85  {
86  inputEventHeader_->SetBranchAddress("EventHeader", &theEventHeader_);
87  inputEventHeader_->GetEntry(0);
88  }
89  }
90 
91  if( !inputEventTree_ )
92  {
93  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Can't find any tree in " << fileName << std::endl;
94  return false; ;
95  }
96  return true;
97 }
98 
99 //========================================================================================================================
100 void MonicelliFileReader::closeGeoFile(void)
101 {
102  if(theGeoFile_)
103  {
104  theGeoFile_->Close();
105  theGeoFile_ = 0;
106  }
107 }
108 
109 //========================================================================================================================
110 void MonicelliFileReader::closeEventsFile(void)
111 {
112  if(theEventsFile_)
113  {
114  theEventsFile_->Close();
115  theEventsFile_ = 0;
116  }
117 }
118 
119 //========================================================================================================================
120 unsigned int MonicelliFileReader::getNumberOfEvents(void)
121 {
122  if(inputEventTree_)
123  return inputEventTree_->GetEntries();
124  return 0;
125 }
126 
127 //========================================================================================================================
128 void MonicelliFileReader::readEvent(unsigned int event)
129 {
130  if(inputEventTree_)
131  inputEventTree_->GetEntry(event);
132 }
133 
134 //========================================================================================================================
135 monicelli::Event* MonicelliFileReader::getEventPointer(void)
136 {
137  return theEvent_;
138 }
139 
140 //========================================================================================================================
141 monicelli::EventHeader* MonicelliFileReader::getEventHeaderPointer(void)
142 {
143  return theEventHeader_;
144 }
145 
146 //========================================================================================================================
147 monicelli::Geometry* MonicelliFileReader::getGeometryPointer(void)
148 {
149  return theGeometry_;
150 }