1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 12/2000 (tbretz@uni-sw.gwdg.de)
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2001
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | // //
|
---|
27 | // MReadTree //
|
---|
28 | // //
|
---|
29 | // This tasks opens all branches in a specified tree and creates the //
|
---|
30 | // corresponding parameter containers if not already existing in the //
|
---|
31 | // parameter list. //
|
---|
32 | // //
|
---|
33 | // The Process function reads one events from the tree. To go through the //
|
---|
34 | // events of one tree make sure that the event number is increased from //
|
---|
35 | // outside. It makes also possible to go back by decreasing the number. //
|
---|
36 | // //
|
---|
37 | /////////////////////////////////////////////////////////////////////////////
|
---|
38 |
|
---|
39 | #include "MReadTree.h"
|
---|
40 |
|
---|
41 | #include <fstream.h>
|
---|
42 |
|
---|
43 | #include <TFile.h>
|
---|
44 | #include <TChain.h>
|
---|
45 | #include <TObjArray.h>
|
---|
46 |
|
---|
47 | #include "MLog.h"
|
---|
48 | #include "MLogManip.h"
|
---|
49 |
|
---|
50 | #include "MTime.h"
|
---|
51 | #include "MParList.h"
|
---|
52 |
|
---|
53 | ClassImp(MReadTree)
|
---|
54 |
|
---|
55 | // --------------------------------------------------------------------------
|
---|
56 | MReadTree::MReadTree(const char *tname, const char *fname,
|
---|
57 | const char *name, const char *title)
|
---|
58 | {
|
---|
59 | *fName = name ? name : "MReadTree";
|
---|
60 | *fTitle = title ? title : "Task to loop over all events in one single tree";
|
---|
61 |
|
---|
62 | //
|
---|
63 | // open the input stream
|
---|
64 | //
|
---|
65 | fChain = new TChain(tname);
|
---|
66 |
|
---|
67 | if (fname)
|
---|
68 | fChain->Add(fname);
|
---|
69 | }
|
---|
70 |
|
---|
71 | // --------------------------------------------------------------------------
|
---|
72 | MReadTree::~MReadTree()
|
---|
73 | {
|
---|
74 | delete fChain;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // --------------------------------------------------------------------------
|
---|
78 | /*Int_t*/ void MReadTree::AddFile(const char *fname)
|
---|
79 | {
|
---|
80 | //
|
---|
81 | // FIXME! A check is missing whether the file already exists or not.
|
---|
82 | //
|
---|
83 | //
|
---|
84 | // returns the number of file which were added
|
---|
85 | //
|
---|
86 | /*return root >3.0*/ fChain->Add(fname);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // --------------------------------------------------------------------------
|
---|
90 | //
|
---|
91 | // open file and check if file is really open
|
---|
92 | //
|
---|
93 | Bool_t MReadTree::PreProcess (MParList *pList)
|
---|
94 | {
|
---|
95 | //
|
---|
96 | // get number of events in this tree
|
---|
97 | //
|
---|
98 | fNumEntries = (UInt_t)fChain->GetEntries();
|
---|
99 |
|
---|
100 | if (!fNumEntries)
|
---|
101 | {
|
---|
102 | *fLog << dbginf << "No Entries found in chain (file/s)." << endl;
|
---|
103 | return kFALSE;
|
---|
104 | }
|
---|
105 |
|
---|
106 | //
|
---|
107 | // set pointer to first event
|
---|
108 | //
|
---|
109 | fNumEntry = 0;
|
---|
110 |
|
---|
111 | //
|
---|
112 | // output logging information
|
---|
113 | //
|
---|
114 | *fLog << fNumEntries << " Entries found in file(s)." << endl;
|
---|
115 |
|
---|
116 | //
|
---|
117 | // Get all branches of this tree and
|
---|
118 | // create the Iterator to loop over all branches
|
---|
119 | //
|
---|
120 | TIter Next(fChain->GetListOfBranches());
|
---|
121 | TBranch *branch=NULL;
|
---|
122 |
|
---|
123 | //
|
---|
124 | // loop over all tasks for processing
|
---|
125 | //
|
---|
126 | while ( (branch=(TBranch*)Next()) )
|
---|
127 | {
|
---|
128 | //
|
---|
129 | // Get Name of Branch
|
---|
130 | //
|
---|
131 | const char *name = branch->GetName();
|
---|
132 |
|
---|
133 | //
|
---|
134 | // check if object is existing in the list
|
---|
135 | //
|
---|
136 | MParContainer *pcont = pList->FindCreateObj(name);
|
---|
137 |
|
---|
138 |
|
---|
139 | if (!pcont)
|
---|
140 | {
|
---|
141 | //
|
---|
142 | // if class is not existing in the (root) environment
|
---|
143 | // we cannot proceed reading this branch
|
---|
144 | //
|
---|
145 | *fLog << "MReadTree::PreProcess - Warning: Class '" << name << "' not existing in dictionary. Branch skipped." << endl;
|
---|
146 | continue;
|
---|
147 | }
|
---|
148 |
|
---|
149 | //
|
---|
150 | // here pcont is a pointer the to container in which the data from
|
---|
151 | // the actual branch should be stored - enable branch.
|
---|
152 | //
|
---|
153 | branch->SetAddress(&pcont);
|
---|
154 | }
|
---|
155 |
|
---|
156 | return kTRUE;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // --------------------------------------------------------------------------
|
---|
160 | Bool_t MReadTree::Process()
|
---|
161 | {
|
---|
162 | //
|
---|
163 | // check for end of file
|
---|
164 | //
|
---|
165 | if (fNumEntry==fNumEntries)
|
---|
166 | return kFALSE;
|
---|
167 |
|
---|
168 | //
|
---|
169 | // get entry
|
---|
170 | //
|
---|
171 | fChain->GetEntry(fNumEntry );
|
---|
172 |
|
---|
173 | fNumEntry ++ ;
|
---|
174 |
|
---|
175 | return kTRUE;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // --------------------------------------------------------------------------
|
---|
179 | //
|
---|
180 | // Close File
|
---|
181 | //
|
---|
182 | Bool_t MReadTree::PostProcess()
|
---|
183 | {
|
---|
184 | //fFile->Close();
|
---|
185 |
|
---|
186 | return kTRUE;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // --------------------------------------------------------------------------
|
---|
190 | //
|
---|
191 | // Get the Event with the current EventNumber fNumEntry
|
---|
192 | //
|
---|
193 | Bool_t MReadTree::GetEvent()
|
---|
194 | {
|
---|
195 | fChain->GetEntry(fNumEntry);
|
---|
196 |
|
---|
197 | return kTRUE;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // --------------------------------------------------------------------------
|
---|
201 | //
|
---|
202 | // Decrease the number of the event which is read by Process() next
|
---|
203 | // by one or more
|
---|
204 | //
|
---|
205 | Bool_t MReadTree::DecEventNum(UInt_t dec)
|
---|
206 | {
|
---|
207 | //!
|
---|
208 | //! this function makes Process() read the event one (or more) before
|
---|
209 | //! the actual position (event) in the tree
|
---|
210 | //!
|
---|
211 | if (fNumEntry < dec/*+1*/)
|
---|
212 | {
|
---|
213 | *fLog << "MReadTree::SetPrevEvent: WARNING: " << fNumEntry/*-1*/ << "-" << dec << " out of Range." << endl;
|
---|
214 | return kFALSE;
|
---|
215 | }
|
---|
216 |
|
---|
217 | fNumEntry -= dec/*+1*/;
|
---|
218 | return kTRUE;
|
---|
219 | }
|
---|
220 |
|
---|
221 | // --------------------------------------------------------------------------
|
---|
222 | //
|
---|
223 | // Increase the number of the event which is read by Process() next
|
---|
224 | // by one or more
|
---|
225 | //
|
---|
226 | Bool_t MReadTree::IncEventNum(UInt_t inc)
|
---|
227 | {
|
---|
228 | //!
|
---|
229 | //! this function makes Process() read the next (or more) after
|
---|
230 | //! the actual position (event) in the tree
|
---|
231 | //! (Be careful: IncEventNum() or IncEventNum(1) does not chenge anything
|
---|
232 | //! in the standard behaviour of the task)
|
---|
233 | //!
|
---|
234 | if (fNumEntry+inc/*-1*/ >= fNumEntries)
|
---|
235 | {
|
---|
236 | *fLog << "MReadTree::SkipEvents: WARNING: " << fNumEntry/*-1*/ << "+" << inc << " out of Range." << endl;
|
---|
237 | return kFALSE;
|
---|
238 | }
|
---|
239 |
|
---|
240 | fNumEntry += inc/*-1*/;
|
---|
241 | return kTRUE;
|
---|
242 | }
|
---|
243 |
|
---|
244 | // --------------------------------------------------------------------------
|
---|
245 | //
|
---|
246 | // this function makes Process() read event number nr next
|
---|
247 | //
|
---|
248 | Bool_t MReadTree::SetEventNum(UInt_t nr)
|
---|
249 | {
|
---|
250 | if (nr>=fNumEntries)
|
---|
251 | {
|
---|
252 | *fLog << "MReadTree::SetEventNum: WARNING: " << nr << " out of Range." << endl;
|
---|
253 | return kFALSE;
|
---|
254 | }
|
---|
255 |
|
---|
256 | fNumEntry = nr;
|
---|
257 | return kTRUE;
|
---|
258 | }
|
---|
259 |
|
---|