source: trunk/MagicSoft/Mars/mbase/MReadTree.cc@ 724

Last change on this file since 724 was 706, checked in by tbretz, 24 years ago
*** empty log message ***
File size: 5.4 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// //
3// MReadTree //
4// //
5// This tasks opens all branches in a specified tree and creates the //
6// corresponding parameter containers if not already existing in the //
7// parameter list. //
8// //
9// The Process function reads one events from the tree. To go through the //
10// events of one tree make sure that the event number is increased from //
11// outside. It makes also possible to go back by decreasing the number. //
12// //
13/////////////////////////////////////////////////////////////////////////////
14
15#include "MReadTree.h"
16
17#include <fstream.h>
18
19#include <TFile.h>
20#include <TChain.h>
21#include <TObjArray.h>
22
23#include "MLog.h"
24#include "MLogManip.h"
25
26#include "MTime.h"
27#include "MParList.h"
28
29ClassImp(MReadTree)
30
31MReadTree::MReadTree(const char *tname, const char *fname,
32 const char *name, const char *title)
33{
34 *fName = name ? name : "MReadTree";
35 *fTitle = title ? title : "Task to loop over all events in one single tree";
36
37 //
38 // open the input stream
39 //
40 fChain = new TChain(tname);
41
42 if (fname)
43 fChain->Add(fname);
44}
45
46MReadTree::~MReadTree()
47{
48 delete fChain;
49}
50
51/*Int_t*/ void MReadTree::AddFile(const char *fname)
52{
53 //
54 // FIXME! A check is missing whether the file already exists or not.
55 //
56 //
57 // returns the number of file which were added
58 //
59 /*return root >3.0*/ fChain->Add(fname);
60}
61
62Bool_t MReadTree::PreProcess (MParList *pList)
63{
64 //
65 // open file and check if file is really open
66 //
67
68 //
69 // get number of events in this tree
70 //
71 fNumEntries = (UInt_t)fChain->GetEntries();
72
73 if (!fNumEntries)
74 {
75 *fLog << dbginf << "No Entries found in chain (file/s)." << endl;
76 return kFALSE;
77 }
78
79 //
80 // set pointer to first event
81 //
82 fNumEntry = 0;
83
84 //
85 // output logging information
86 //
87 *fLog << fNumEntries << " Entries found in file(s)." << endl;
88
89 //
90 // Get all branches of this tree and
91 // create the Iterator to loop over all branches
92 //
93 TIter Next(fChain->GetListOfBranches());
94 TBranch *branch=NULL;
95
96 //
97 // loop over all tasks for processing
98 //
99 while ( (branch=(TBranch*)Next()) )
100 {
101 //
102 // Get Name of Branch
103 //
104 const char *name = branch->GetName();
105
106 //
107 // check if object is existing in the list
108 //
109 MParContainer *pcont = pList->FindCreateObj(name);
110
111
112 if (!pcont)
113 {
114 //
115 // if class is not existing in the (root) environment
116 // we cannot proceed reading this branch
117 //
118 *fLog << "MReadTree::PreProcess - Warning: Class '" << name << "' not existing in dictionary. Branch skipped." << endl;
119 continue;
120 }
121
122 //
123 // here pcont is a pointer the to container in which the data from
124 // the actual branch should be stored - enable branch.
125 //
126 branch->SetAddress(&pcont);
127 }
128
129 return kTRUE;
130}
131
132Bool_t MReadTree::Process()
133{
134 //
135 // check for end of file
136 //
137 if (fNumEntry==fNumEntries)
138 return kFALSE;
139
140 //
141 // get entry
142 //
143 fChain->GetEntry(fNumEntry );
144
145 fNumEntry ++ ;
146
147 return kTRUE;
148}
149
150Bool_t MReadTree::PostProcess()
151{
152 //
153 // Close File
154 //
155 //fFile->Close();
156
157 return kTRUE;
158}
159
160Bool_t MReadTree::GetEvent()
161{
162 //
163 // Get the Event with the current EventNumber fNumEntry
164 //
165 fChain->GetEntry(fNumEntry);
166
167 return kTRUE;
168}
169
170Bool_t MReadTree::DecEventNum(UInt_t dec)
171{
172 //
173 // Decrease the number of the event which is read by Process() next
174 // by one or more
175 //
176
177 //!
178 //! this function makes Process() read the event one (or more) before
179 //! the actual position (event) in the tree
180 //!
181 if (fNumEntry < dec/*+1*/)
182 {
183 *fLog << "MReadTree::SetPrevEvent: WARNING: " << fNumEntry/*-1*/ << "-" << dec << " out of Range." << endl;
184 return kFALSE;
185 }
186
187 fNumEntry -= dec/*+1*/;
188 return kTRUE;
189}
190
191Bool_t MReadTree::IncEventNum(UInt_t inc)
192{
193 //
194 // Increase the number of the event which is read by Process() next
195 // by one or more
196 //
197
198 //!
199 //! this function makes Process() read the next (or more) after
200 //! the actual position (event) in the tree
201 //! (Be careful: IncEventNum() or IncEventNum(1) does not chenge anything
202 //! in the standard behaviour of the task)
203 //!
204 if (fNumEntry+inc/*-1*/ >= fNumEntries)
205 {
206 *fLog << "MReadTree::SkipEvents: WARNING: " << fNumEntry/*-1*/ << "+" << inc << " out of Range." << endl;
207 return kFALSE;
208 }
209
210 fNumEntry += inc/*-1*/;
211 return kTRUE;
212}
213
214Bool_t MReadTree::SetEventNum(UInt_t nr)
215{
216 //
217 // this function makes Process() read event number nr next
218 //
219 if (nr>=fNumEntries)
220 {
221 *fLog << "MReadTree::SetEventNum: WARNING: " << nr << " out of Range." << endl;
222 return kFALSE;
223 }
224
225 fNumEntry = nr;
226 return kTRUE;
227}
228
Note: See TracBrowser for help on using the repository browser.