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

Last change on this file since 703 was 666, 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 <TTree.h>
21#include <TObjArray.h>
22
23#include "MLog.h"
24#include "MTime.h"
25#include "MParList.h"
26
27ClassImp(MReadTree)
28
29MReadTree::MReadTree(const char *fname, const char *tname,
30 const char *name, const char *title)
31{
32 *fName = name ? name : "MReadTree";
33 *fTitle = title ? title : "Task to loop over all events in one single tree";
34
35 //
36 // open the input stream
37 //
38 fFileName = fname;
39 fTreeName = tname;
40}
41
42Bool_t MReadTree::PreProcess (MParList *pList)
43{
44 //
45 // open file and check if file is really open
46 //
47 fFile = new TFile(fFileName, "READ");
48
49 if (!fFile->IsOpen())
50 {
51 *fLog << "MReadTree::PreProcess: ERROR: Cannot open file '";
52 *fLog << fFileName << "'" << endl;
53 return kFALSE;
54 }
55
56 //
57 // try to get the tree and check if it was found
58 //
59 fTree = (TTree*)fFile->Get(fTreeName);
60 if (!fTree)
61 {
62 *fLog << "MReadTree::PreProcess: ERROR: Cannot open tree '";
63 *fLog << fTreeName << "'" << endl;
64 return kFALSE;
65 }
66
67 //
68 // get number of events in this tree
69 //
70 fNumEntries = (UInt_t)fTree->GetEntries();
71
72 //
73 // set pointer to first event
74 //
75 fNumEntry = 0;
76
77 //
78 // output logging information
79 //
80 *fLog << "File: '" << fFileName << "' Tree: '" << fTreeName;
81 *fLog << "' with " << fNumEntries << " Entries opened." << endl;
82
83 //
84 // Get all branches of this tree and
85 // create the Iterator to loop over all branches
86 //
87 TIter Next(fTree->GetListOfBranches());
88 TBranch *branch=NULL;
89
90 //
91 // loop over all tasks for processing
92 //
93 while ( (branch=(TBranch*)Next()) )
94 {
95 //
96 // Get Name of Branch
97 //
98 const char *name = branch->GetName();
99
100 //
101 // check if object is existing in the list
102 //
103 MParContainer *pcont = pList->FindCreateObj(name);
104
105 if (!pcont)
106 {
107 //
108 // if class is not existing in the (root) environment
109 // we cannot proceed reading this branch
110 //
111 *fLog << "MReadTree::PreProcess - Warning: Class '" << name << "' not existing in dictionary. Branch skipped." << endl;
112 continue;
113 }
114
115 //
116 // here pcont is a pointer the to container in which the data from
117 // the actual branch should be stored - enable branch.
118 //
119 branch->SetAddress(&pcont);
120 }
121
122 return kTRUE;
123}
124
125Bool_t MReadTree::Process()
126{
127 //
128 // check for end of file
129 //
130 if (fNumEntry==fNumEntries)
131 return kFALSE;
132
133 //
134 // get entry
135 //
136 fTree->GetEntry(fNumEntry );
137
138 fNumEntry ++ ;
139
140 return kTRUE;
141}
142
143Bool_t MReadTree::PostProcess()
144{
145 //
146 // Close File
147 //
148 fFile->Close();
149
150 return kTRUE;
151}
152
153Bool_t MReadTree::GetEvent()
154{
155 //
156 // Get the Event with the current EventNumber fNumEntry
157 //
158 fTree->GetEntry(fNumEntry);
159
160 return kTRUE;
161}
162
163Bool_t MReadTree::DecEventNum(UInt_t dec)
164{
165 //
166 // Decrease the number of the event which is read by Process() next
167 // by one or more
168 //
169
170 //!
171 //! this function makes Process() read the event one (or more) before
172 //! the actual position (event) in the tree
173 //!
174 if (fNumEntry < dec/*+1*/)
175 {
176 *fLog << "MReadTree::SetPrevEvent: WARNING: " << fNumEntry/*-1*/ << "-" << dec << " out of Range." << endl;
177 return kFALSE;
178 }
179
180 fNumEntry -= dec/*+1*/;
181 return kTRUE;
182}
183
184Bool_t MReadTree::IncEventNum(UInt_t inc)
185{
186 //
187 // Increase the number of the event which is read by Process() next
188 // by one or more
189 //
190
191 //!
192 //! this function makes Process() read the next (or more) after
193 //! the actual position (event) in the tree
194 //! (Be careful: IncEventNum() or IncEventNum(1) does not chenge anything
195 //! in the standard behaviour of the task)
196 //!
197 if (fNumEntry+inc/*-1*/ >= fNumEntries)
198 {
199 *fLog << "MReadTree::SkipEvents: WARNING: " << fNumEntry/*-1*/ << "+" << inc << " out of Range." << endl;
200 return kFALSE;
201 }
202
203 fNumEntry += inc/*-1*/;
204 return kTRUE;
205}
206
207Bool_t MReadTree::SetEventNum(UInt_t nr)
208{
209 //
210 // this function makes Process() read event number nr next
211 //
212 if (nr>=fNumEntries)
213 {
214 *fLog << "MReadTree::SetEventNum: WARNING: " << nr << " out of Range." << endl;
215 return kFALSE;
216 }
217
218 fNumEntry = nr;
219 return kTRUE;
220}
221
Note: See TracBrowser for help on using the repository browser.