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

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