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 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
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 | // If you don't want to start reading the first event you have to call //
|
---|
38 | // MReadTree::SetEventNum after instantiating your MReadTree-object. //
|
---|
39 | // //
|
---|
40 | // To make reading much faster (up to a factor of 10 to 20) you can //
|
---|
41 | // ensure that only the data you are really processing is enabled by //
|
---|
42 | // calling MReadTree::UseLeaf. //
|
---|
43 | // //
|
---|
44 | // If the chain switches from one file to another file all //
|
---|
45 | // TObject::Notify() functions are called of TObject objects which were //
|
---|
46 | // added to the Notifier list view MReadTree::AddNotify. If MReadTree //
|
---|
47 | // is the owner (viw MReadTree::SetOwner) all this objects are deleted //
|
---|
48 | // by the destructor of MReadTree //
|
---|
49 | // //
|
---|
50 | /////////////////////////////////////////////////////////////////////////////
|
---|
51 | #include "MReadTree.h"
|
---|
52 |
|
---|
53 | #include <fstream>
|
---|
54 |
|
---|
55 | #include <TFile.h> // TFile::GetName
|
---|
56 | #include <TSystem.h> // gSystem->ExpandPath
|
---|
57 | #include <TChainElement.h>
|
---|
58 | #include <TOrdCollection.h>
|
---|
59 |
|
---|
60 | #include "MChain.h"
|
---|
61 | #include "MFilter.h"
|
---|
62 | #include "MParList.h"
|
---|
63 | #include "MTaskList.h"
|
---|
64 |
|
---|
65 | #include "MLog.h"
|
---|
66 | #include "MLogManip.h"
|
---|
67 |
|
---|
68 |
|
---|
69 | ClassImp(MReadTree);
|
---|
70 |
|
---|
71 | using namespace std;
|
---|
72 |
|
---|
73 | // --------------------------------------------------------------------------
|
---|
74 | //
|
---|
75 | // Default constructor. Don't use it.
|
---|
76 | //
|
---|
77 | MReadTree::MReadTree()
|
---|
78 | : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
|
---|
79 | {
|
---|
80 | fName = "MRead";
|
---|
81 | fTitle = "Task to loop over all events in one single tree";
|
---|
82 |
|
---|
83 | fVetoList = NULL;
|
---|
84 | fNotify = NULL;
|
---|
85 |
|
---|
86 | fChain = NULL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // --------------------------------------------------------------------------
|
---|
90 | //
|
---|
91 | // Constructor. It creates an TChain instance which represents the
|
---|
92 | // the Tree you want to read and adds the given file (if you gave one).
|
---|
93 | // More files can be added using MReadTree::AddFile.
|
---|
94 | // Also an empty veto list is created. This list is used if you want to
|
---|
95 | // veto (disable or "don't enable") a branch in the tree, it vetos also
|
---|
96 | // the creation of the corresponding object.
|
---|
97 | // An empty list of TObjects are also created. This objects are called
|
---|
98 | // at any time the TChain starts to read from another file.
|
---|
99 | //
|
---|
100 | MReadTree::MReadTree(const char *tname, const char *fname,
|
---|
101 | const char *name, const char *title)
|
---|
102 | : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
|
---|
103 | {
|
---|
104 | fName = name ? name : "MRead";
|
---|
105 | fTitle = title ? title : "Task to loop over all events in one single tree";
|
---|
106 |
|
---|
107 | fVetoList = new TList;
|
---|
108 | fVetoList->SetOwner();
|
---|
109 |
|
---|
110 | fNotify = new TList;
|
---|
111 |
|
---|
112 | //
|
---|
113 | // open the input stream
|
---|
114 | //
|
---|
115 | fChain = new MChain(tname);
|
---|
116 |
|
---|
117 | // root 3.02:
|
---|
118 | // In TChain::Addfile remove the limitation that the file name must contain
|
---|
119 | // the string ".root". ".root" is necessary only in case one wants to specify
|
---|
120 | // a Tree in a subdirectory of a Root file with eg, the format:
|
---|
121 |
|
---|
122 | if (fname)
|
---|
123 | AddFile(fname);
|
---|
124 | // if (fChain->Add(fname)>0)
|
---|
125 | // SetBit(kChainWasChanged);
|
---|
126 | }
|
---|
127 |
|
---|
128 | // --------------------------------------------------------------------------
|
---|
129 | //
|
---|
130 | // Destructor. It deletes the TChain and veto list object
|
---|
131 | //
|
---|
132 | MReadTree::~MReadTree()
|
---|
133 | {
|
---|
134 | //
|
---|
135 | // Delete all the pointers to pointers to the objects where the
|
---|
136 | // branche data gets stored. FIXME: When PreProcessed twice this
|
---|
137 | // creates a memory leak!
|
---|
138 | //
|
---|
139 | TIter Next(fChain->GetStatus());
|
---|
140 |
|
---|
141 | TChainElement *element = NULL;
|
---|
142 | while ((element=(TChainElement*)Next()))
|
---|
143 | if (element->GetBaddress())
|
---|
144 | delete (MParContainer**)element->GetBaddress();
|
---|
145 |
|
---|
146 | //
|
---|
147 | // Delete the chain and the veto list
|
---|
148 | //
|
---|
149 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,00)
|
---|
150 | if (fChain->GetFile())
|
---|
151 | delete fChain->GetFile();
|
---|
152 | #endif
|
---|
153 | delete fChain;
|
---|
154 |
|
---|
155 | delete fNotify;
|
---|
156 | delete fVetoList;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // --------------------------------------------------------------------------
|
---|
160 | //
|
---|
161 | // This check whether all branches in the tree have the same size. If
|
---|
162 | // this is not the case the events in the different branches cannot
|
---|
163 | // be ordered correctly.
|
---|
164 | //
|
---|
165 | Bool_t MReadTree::CheckBranchSize()
|
---|
166 | {
|
---|
167 | TArrayI entries(fChain->GetStatus()->GetSize());
|
---|
168 | Int_t num=0;
|
---|
169 |
|
---|
170 | // Loop over all branches which have a corresponding container
|
---|
171 | TIter Next(fChain->GetStatus());
|
---|
172 |
|
---|
173 | TChainElement *element = NULL;
|
---|
174 | while ((element=(TChainElement*)Next()))
|
---|
175 | {
|
---|
176 | // Get branch name and find pointer to corresponding branch
|
---|
177 | const TString name = element->GetName();
|
---|
178 | const TBranch *b = fChain->FindBranch(name);
|
---|
179 |
|
---|
180 | // Skip element without corresponding branches (like "*")
|
---|
181 | if (!b)
|
---|
182 | continue;
|
---|
183 |
|
---|
184 | entries[num++] = (Int_t)b->GetEntries();
|
---|
185 | }
|
---|
186 |
|
---|
187 | // Check the number of entries of the branches pair-wise
|
---|
188 | for (int i=0; i<num; i++)
|
---|
189 | for (int j=i; j<num; j++)
|
---|
190 | {
|
---|
191 | if (entries[i]==entries[j])
|
---|
192 | continue;
|
---|
193 |
|
---|
194 | *fLog << err << "ERROR - File corruption detected:" << endl;
|
---|
195 | *fLog << " Due to several circumstances (such at a bug in MReadTree or wrong" << endl;
|
---|
196 | *fLog << " usage of the file UPDATE mode) you may have produced a file in which" << endl;
|
---|
197 | *fLog << " at least two branches in the same tree (" << fChain->GetName() << ") have different" << endl;
|
---|
198 | *fLog << " number of entries. Sorry, but this file (" << GetFileName() << ")" << endl;
|
---|
199 | *fLog << " is unusable." << endl;
|
---|
200 | return kFALSE;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return kTRUE;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // --------------------------------------------------------------------------
|
---|
207 | //
|
---|
208 | // If the owner flag is set all TObjects which are scheduled via
|
---|
209 | // AddNotify are deleted by the destructor of MReadTree
|
---|
210 | //
|
---|
211 | void MReadTree::SetOwner(Bool_t flag)
|
---|
212 | {
|
---|
213 | flag ? fNotify->SetBit(kIsOwner) : fNotify->ResetBit(kIsOwner);
|
---|
214 | }
|
---|
215 |
|
---|
216 | // --------------------------------------------------------------------------
|
---|
217 | //
|
---|
218 | // This function is called each time MReadTree changes the file to read
|
---|
219 | // from. It calls all TObject::Notify() functions which are scheduled
|
---|
220 | // via AddNotify.
|
---|
221 | //
|
---|
222 | Bool_t MReadTree::Notify()
|
---|
223 | {
|
---|
224 | //
|
---|
225 | // Do a consistency check for all branches
|
---|
226 | //
|
---|
227 | if (!CheckBranchSize())
|
---|
228 | return kFALSE;
|
---|
229 |
|
---|
230 | *fLog << inf << GetDescriptor() << ": Switching to #" << GetFileIndex();
|
---|
231 | *fLog << " '" << GetFileName() << "' (before event #";
|
---|
232 | *fLog << GetNumEntry()-1 << ")" << endl;
|
---|
233 |
|
---|
234 | if (!fNotify)
|
---|
235 | return kTRUE;
|
---|
236 |
|
---|
237 | TIter Next(fNotify);
|
---|
238 | TObject *o=NULL;
|
---|
239 | while ((o=Next()))
|
---|
240 | if (!o->Notify())
|
---|
241 | {
|
---|
242 | *fLog << err << "Calling Notify() for object " << o->GetName() << " failed... abort." << endl;
|
---|
243 | return kFALSE;
|
---|
244 | }
|
---|
245 |
|
---|
246 | return kTRUE;
|
---|
247 | }
|
---|
248 |
|
---|
249 | // --------------------------------------------------------------------------
|
---|
250 | //
|
---|
251 | // If you want to read the given tree over several files you must add
|
---|
252 | // the files here before PreProcess is called. Be careful: If the tree
|
---|
253 | // doesn't have the same contents (branches) it may confuse your
|
---|
254 | // program (trees which are are not existing in later files are not read
|
---|
255 | // anymore, tree wich are not existing in the first file are never read)
|
---|
256 | //
|
---|
257 | // Name may use the wildcarding notation, eg "xxx*.root" means all files
|
---|
258 | // starting with xxx in the current file system directory.
|
---|
259 | //
|
---|
260 | // AddFile returns the number of files added to the chain.
|
---|
261 | //
|
---|
262 | // For more information see TChain::Add
|
---|
263 | //
|
---|
264 | Int_t MReadTree::AddFile(const char *fname, Int_t entries)
|
---|
265 | {
|
---|
266 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
|
---|
267 | //
|
---|
268 | // This is a workaround to get rid of crashed if the file doesn't
|
---|
269 | // exist due to non initialized TFile::fProcessIDs
|
---|
270 | //
|
---|
271 | // (Code taken from TFile::TFile
|
---|
272 | //
|
---|
273 | TString newname; // char-array must overcome comming block
|
---|
274 |
|
---|
275 | if (strrchr(fname, '?') || strrchr(fname, '*'))
|
---|
276 | {
|
---|
277 | *fLog << warn;
|
---|
278 | *fLog<< "WARNING: Using widcards with older root versions:" << endl;
|
---|
279 | *fLog << " You may encounter crashes closing the files..." << endl;
|
---|
280 | }
|
---|
281 | else
|
---|
282 | {
|
---|
283 | const char *name;
|
---|
284 |
|
---|
285 | if ((name = gSystem->ExpandPathName(fname)))
|
---|
286 | {
|
---|
287 | newname = name;
|
---|
288 | delete [] name;
|
---|
289 | }
|
---|
290 |
|
---|
291 | if (newname.IsNull())
|
---|
292 | {
|
---|
293 | *fLog << err << dbginf << "Error expanding path " << fname << "." << endl;
|
---|
294 | return 0;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (gSystem->AccessPathName(newname, kFileExists))
|
---|
298 | {
|
---|
299 | *fLog << err << "ERROR - File '" << fname << "' does not exist." << endl;
|
---|
300 | return 0;
|
---|
301 | }
|
---|
302 |
|
---|
303 | fname = newname.Data();
|
---|
304 | }
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | //
|
---|
308 | // FIXME! A check is missing whether the file already exists or not.
|
---|
309 | //
|
---|
310 | const Int_t numfiles = fChain->Add(fname, entries<0?TChain::kBigNumber:entries);
|
---|
311 |
|
---|
312 | if (numfiles>0)
|
---|
313 | SetBit(kChainWasChanged);
|
---|
314 | else
|
---|
315 | *fLog << warn << "WARNING: '" << fname << "' not added to " << GetDescriptor() << endl;
|
---|
316 |
|
---|
317 | return numfiles;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /*
|
---|
321 | // --------------------------------------------------------------------------
|
---|
322 | //
|
---|
323 | //
|
---|
324 | Int_t MReadTree::AddFile(const TChainElement &obj)
|
---|
325 | {
|
---|
326 | return AddFile(obj.GetTitle(), obj.GetEntries());
|
---|
327 | }
|
---|
328 | */
|
---|
329 |
|
---|
330 | // --------------------------------------------------------------------------
|
---|
331 | //
|
---|
332 | // Adds all files from another MReadTree to this instance
|
---|
333 | //
|
---|
334 | // Returns the number of file which were added
|
---|
335 | //
|
---|
336 | Int_t MReadTree::AddFiles(const MReadTree &read)
|
---|
337 | {
|
---|
338 | const Int_t rc = fChain->Add(read.fChain);
|
---|
339 |
|
---|
340 | if (rc>0)
|
---|
341 | SetBit(kChainWasChanged);
|
---|
342 |
|
---|
343 | /*
|
---|
344 | Int_t rc = 0;
|
---|
345 |
|
---|
346 | TIter Next(read.fChain->GetListOfFiles());
|
---|
347 | TObject *obj = NULL;
|
---|
348 | while ((obj=Next()))
|
---|
349 | rc += AddFile(*(TChainElement*)obj);
|
---|
350 | */
|
---|
351 |
|
---|
352 | return rc;
|
---|
353 | }
|
---|
354 |
|
---|
355 | // --------------------------------------------------------------------------
|
---|
356 | //
|
---|
357 | // This function is called if Branch choosing method should get enabled.
|
---|
358 | // Branch choosing means, that only the enabled branches are read into
|
---|
359 | // memory. To use an enableing scheme we have to disable all branches first.
|
---|
360 | // This is done, if this function is called the first time.
|
---|
361 | //
|
---|
362 | void MReadTree::EnableBranchChoosing()
|
---|
363 | {
|
---|
364 | if (fBranchChoosing)
|
---|
365 | return;
|
---|
366 |
|
---|
367 | *fLog << inf << GetDescriptor() << ": Branch choosing enabled (only enabled branches are read)." << endl;
|
---|
368 | fChain->SetBranchStatus("*", kFALSE);
|
---|
369 | fBranchChoosing = kTRUE;
|
---|
370 | }
|
---|
371 |
|
---|
372 | // --------------------------------------------------------------------------
|
---|
373 | //
|
---|
374 | // The first time this function is called all branches are disabled.
|
---|
375 | // The given branch is enabled. By enabling only the branches you
|
---|
376 | // are processing you can speed up your calculation many times (up to
|
---|
377 | // a factor of 10 or 20)
|
---|
378 | //
|
---|
379 | void MReadTree::EnableBranch(const char *name)
|
---|
380 | {
|
---|
381 | if (fChain->GetListOfFiles()->GetEntries()==0)
|
---|
382 | {
|
---|
383 | *fLog << err << "Chain contains no file... Branch '";
|
---|
384 | *fLog << name << "' ignored." << endl;
|
---|
385 | return;
|
---|
386 | }
|
---|
387 |
|
---|
388 | EnableBranchChoosing();
|
---|
389 |
|
---|
390 | TNamed branch(name, "");
|
---|
391 | SetBranchStatus(&branch, kTRUE);
|
---|
392 | }
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // Set branch status of branch name
|
---|
397 | //
|
---|
398 | void MReadTree::SetBranchStatus(const char *name, Bool_t status)
|
---|
399 | {
|
---|
400 | fChain->SetBranchStatus(name, status);
|
---|
401 |
|
---|
402 | *fLog << inf << (status ? "Enabled" : "Disabled");
|
---|
403 | *fLog << " subbranch '" << name << "'." << endl;
|
---|
404 | }
|
---|
405 |
|
---|
406 | // --------------------------------------------------------------------------
|
---|
407 | //
|
---|
408 | // Checks whether a branch with the given name exists in the chain
|
---|
409 | // and sets the branch status of this branch corresponding to status.
|
---|
410 | //
|
---|
411 | void MReadTree::SetBranchStatus(TObject *branch, Bool_t status)
|
---|
412 | {
|
---|
413 | //
|
---|
414 | // Get branch name
|
---|
415 | //
|
---|
416 | const char *name = branch->GetName();
|
---|
417 |
|
---|
418 | //
|
---|
419 | // Check whether this branch really exists
|
---|
420 | //
|
---|
421 | TString bn(name);
|
---|
422 | if (bn.EndsWith("*"))
|
---|
423 | bn.Remove(bn.Length()-1);
|
---|
424 |
|
---|
425 | if (fChain->GetBranch(bn))
|
---|
426 | SetBranchStatus(name, status);
|
---|
427 |
|
---|
428 | //
|
---|
429 | // Remove trailing '.' if one and try to enable the subbranch without
|
---|
430 | // the master branch name. This is to be compatible with older mars
|
---|
431 | // and camera files.
|
---|
432 | //
|
---|
433 | const char *dot = strrchr(name, '.');
|
---|
434 | if (!dot)
|
---|
435 | return;
|
---|
436 |
|
---|
437 | if (fChain->GetBranch(dot+1))
|
---|
438 | SetBranchStatus(dot+1, status);
|
---|
439 | }
|
---|
440 |
|
---|
441 | // --------------------------------------------------------------------------
|
---|
442 | //
|
---|
443 | // Set the status of all branches in the list to status.
|
---|
444 | //
|
---|
445 | void MReadTree::SetBranchStatus(const TList *list, Bool_t status)
|
---|
446 | {
|
---|
447 | //
|
---|
448 | // Loop over all subbranches in this master branch
|
---|
449 | //
|
---|
450 | TIter Next(list);
|
---|
451 |
|
---|
452 | TObject *obj;
|
---|
453 | while ((obj=Next()))
|
---|
454 | SetBranchStatus(obj, status);
|
---|
455 | }
|
---|
456 |
|
---|
457 | // --------------------------------------------------------------------------
|
---|
458 | //
|
---|
459 | // This is the implementation of the Auto Enabling Scheme.
|
---|
460 | // For more information see MTask::AddBranchToList.
|
---|
461 | // This function loops over all tasks and its filters in the tasklist
|
---|
462 | // and enables all branches which are requested by the tasks and its
|
---|
463 | // filters.
|
---|
464 | //
|
---|
465 | // To enable 'unknown' branches which are not in the branchlist of
|
---|
466 | // the tasks you can call EnableBranch
|
---|
467 | //
|
---|
468 | void MReadTree::EnableBranches(MParList *plist)
|
---|
469 | {
|
---|
470 | //
|
---|
471 | // check whether branch choosing must be switched on
|
---|
472 | //
|
---|
473 | EnableBranchChoosing();
|
---|
474 |
|
---|
475 | //
|
---|
476 | // request the tasklist from the parameter list.
|
---|
477 | // FIXME: Tasklist can have a different name
|
---|
478 | //
|
---|
479 | const MTaskList *tlist = (MTaskList*)plist->FindObject("MTaskList");
|
---|
480 | if (!tlist)
|
---|
481 | {
|
---|
482 | *fLog << warn << GetDescriptor() << "Cannot use auto enabeling scheme for branches. 'MTaskList' not found." << endl;
|
---|
483 | return;
|
---|
484 | }
|
---|
485 |
|
---|
486 | //
|
---|
487 | // This loop is not necessary. We could do it like in the commented
|
---|
488 | // loop below. But this loop makes sure, that we don't try to enable
|
---|
489 | // one branch several times. This would not harm, but we would get
|
---|
490 | // an output for each attempt. To have several outputs for one subbranch
|
---|
491 | // may confuse the user, this we don't want.
|
---|
492 | // This loop creates a new list of subbranches and for each branch
|
---|
493 | // which is added we check before whether it already exists or not.
|
---|
494 | //
|
---|
495 | TList list;
|
---|
496 |
|
---|
497 | MTask *task;
|
---|
498 | TIter NextTask(tlist->GetList());
|
---|
499 | while ((task=(MTask*)NextTask()))
|
---|
500 | {
|
---|
501 | TObject *obj;
|
---|
502 |
|
---|
503 | TIter NextTBranch(task->GetListOfBranches());
|
---|
504 | while ((obj=NextTBranch()))
|
---|
505 | if (!list.FindObject(obj->GetName()))
|
---|
506 | list.Add(obj);
|
---|
507 |
|
---|
508 | const MFilter *filter = task->GetFilter();
|
---|
509 |
|
---|
510 | if (!filter)
|
---|
511 | continue;
|
---|
512 |
|
---|
513 | TIter NextFBranch(filter->GetListOfBranches());
|
---|
514 | while ((obj=NextFBranch()))
|
---|
515 | if (!list.FindObject(obj->GetName()))
|
---|
516 | list.Add(obj);
|
---|
517 | }
|
---|
518 |
|
---|
519 | SetBranchStatus(&list, kTRUE);
|
---|
520 | /*
|
---|
521 | //
|
---|
522 | // Loop over all tasks iand its filters n the task list.
|
---|
523 | //
|
---|
524 | MTask *task;
|
---|
525 | TIter NextTask(tlist->GetList());
|
---|
526 | while ((task=(MTask*)NextTask()))
|
---|
527 | {
|
---|
528 | SetBranchStatus(task->GetListOfBranches(), kTRUE);
|
---|
529 |
|
---|
530 | const MFilter *filter = task->GetFilter();
|
---|
531 | if (!filter)
|
---|
532 | continue;
|
---|
533 |
|
---|
534 | SetBranchStatus(filter->GetListOfBranches(), kTRUE);
|
---|
535 |
|
---|
536 | }
|
---|
537 | */
|
---|
538 | }
|
---|
539 |
|
---|
540 | // --------------------------------------------------------------------------
|
---|
541 | //
|
---|
542 | // If the chain has been changed (by calling AddFile or using a file
|
---|
543 | // in the constructors argument) the number of entries is newly
|
---|
544 | // calculated from the files in the chain - this may take a while.
|
---|
545 | // The number of entries is returned.
|
---|
546 | //
|
---|
547 | UInt_t MReadTree::GetEntries()
|
---|
548 | {
|
---|
549 | if (TestBit(kChainWasChanged))
|
---|
550 | {
|
---|
551 | *fLog << inf << "Scanning chain... " << flush;
|
---|
552 | fNumEntries = (UInt_t)fChain->GetEntries();
|
---|
553 | *fLog << fNumEntries << " events found." << endl;
|
---|
554 | ResetBit(kChainWasChanged);
|
---|
555 | }
|
---|
556 | return fNumEntries;
|
---|
557 | }
|
---|
558 |
|
---|
559 | // --------------------------------------------------------------------------
|
---|
560 | //
|
---|
561 | // The disables all subbranches of the given master branch.
|
---|
562 | //
|
---|
563 | void MReadTree::DisableSubBranches(TBranch *branch)
|
---|
564 | {
|
---|
565 | //
|
---|
566 | // This is not necessary, it would work without. But the output
|
---|
567 | // may confuse the user...
|
---|
568 | //
|
---|
569 | if (fAutoEnable || fBranchChoosing)
|
---|
570 | return;
|
---|
571 |
|
---|
572 | SetBranchStatus(branch->GetListOfBranches(), kFALSE);
|
---|
573 | }
|
---|
574 |
|
---|
575 | // --------------------------------------------------------------------------
|
---|
576 | //
|
---|
577 | // The PreProcess loops (till now) over the branches in the given tree.
|
---|
578 | // It checks if the corresponding containers (containers with the same
|
---|
579 | // name than the branch name) are existing in the Parameter Container List.
|
---|
580 | // If not, a container of objec type 'branch-name' is created (everything
|
---|
581 | // after the last semicolon in the branch name is stripped). Only
|
---|
582 | // branches which don't have a veto (see VetoBranch) are enabled If the
|
---|
583 | // object isn't found in the root dictionary (a list of classes known by the
|
---|
584 | // root environment) the branch is skipped and an error message is printed
|
---|
585 | // out.
|
---|
586 | // If a selector is specified it is preprocessed after the
|
---|
587 | // MReadTree::PreProcess
|
---|
588 | //
|
---|
589 | Int_t MReadTree::PreProcess(MParList *pList)
|
---|
590 | {
|
---|
591 | //
|
---|
592 | // Make sure, that all the following calls doesn't result in
|
---|
593 | // Notifications. This may be dangerous, because the notified
|
---|
594 | // tasks are not preprocessed.
|
---|
595 | //
|
---|
596 | fChain->SetNotify(NULL);
|
---|
597 |
|
---|
598 | //
|
---|
599 | // get number of events in this tree
|
---|
600 | //
|
---|
601 | if (!GetEntries())
|
---|
602 | {
|
---|
603 | *fLog << warn << GetDescriptor() << ": No entries found in file(s)" << endl;
|
---|
604 | return kFALSE;
|
---|
605 | }
|
---|
606 |
|
---|
607 | //
|
---|
608 | // output logging information
|
---|
609 | //
|
---|
610 | *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
|
---|
611 |
|
---|
612 | //
|
---|
613 | // Get all branches of this tree and
|
---|
614 | // create the Iterator to loop over all branches
|
---|
615 | //
|
---|
616 | TIter Next(fChain->GetListOfBranches());
|
---|
617 | TBranch *branch=NULL;
|
---|
618 |
|
---|
619 | Int_t num=0;
|
---|
620 |
|
---|
621 | //
|
---|
622 | // loop over all tasks for processing
|
---|
623 | //
|
---|
624 | while ( (branch=(TBranch*)Next()) )
|
---|
625 | {
|
---|
626 | //
|
---|
627 | // Get Name of Branch and Object
|
---|
628 | //
|
---|
629 | const char *bname = branch->GetName();
|
---|
630 |
|
---|
631 | TString oname(bname);
|
---|
632 | if (oname.EndsWith("."))
|
---|
633 | oname.Remove(oname.Length()-1);
|
---|
634 |
|
---|
635 | //
|
---|
636 | // Check if enabeling the branch is allowed
|
---|
637 | //
|
---|
638 | if (fVetoList->FindObject(oname))
|
---|
639 | {
|
---|
640 | *fLog << inf << "Master branch " << bname << " has veto... skipped." << endl;
|
---|
641 | DisableSubBranches(branch);
|
---|
642 | continue;
|
---|
643 | }
|
---|
644 |
|
---|
645 | //
|
---|
646 | // Create a pointer to the pointer to the object in which the
|
---|
647 | // branch data is stored. The pointers are stored in the TChain
|
---|
648 | // object and we get the pointers from there to delete it.
|
---|
649 | //
|
---|
650 | MParContainer **pcont= new MParContainer*;
|
---|
651 |
|
---|
652 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
|
---|
653 | const char *classname = oname;
|
---|
654 | #else
|
---|
655 | const char *classname = branch->GetClassName();
|
---|
656 | #endif
|
---|
657 |
|
---|
658 | //
|
---|
659 | // check if object is existing in the list
|
---|
660 | //
|
---|
661 | *pcont=pList->FindCreateObj(classname, oname);
|
---|
662 |
|
---|
663 | if (!*pcont)
|
---|
664 | {
|
---|
665 | //
|
---|
666 | // if class is not existing in the (root) environment
|
---|
667 | // we cannot proceed reading this branch
|
---|
668 | //
|
---|
669 | *fLog << warn << dbginf << "Warning: Class '" << classname;
|
---|
670 | *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
|
---|
671 | DisableSubBranches(branch);
|
---|
672 | continue;
|
---|
673 | }
|
---|
674 |
|
---|
675 | //
|
---|
676 | // Check whether a Pointer to a pointer already exists.
|
---|
677 | // If we created one already, delete it.
|
---|
678 | //
|
---|
679 | TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
|
---|
680 | if (element)
|
---|
681 | delete (MParContainer**)element->GetBaddress();
|
---|
682 |
|
---|
683 | //
|
---|
684 | // here pcont is a pointer the to container in which the data from
|
---|
685 | // the actual branch should be stored - enable branch.
|
---|
686 | //
|
---|
687 | fChain->SetBranchAddress(bname, pcont);
|
---|
688 |
|
---|
689 | *fLog << inf << "Master branch address " << bname << " [";
|
---|
690 | *fLog << classname << "] setup for reading." << endl;
|
---|
691 |
|
---|
692 | //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
|
---|
693 | //branch->SetAutoDelete();
|
---|
694 |
|
---|
695 | num++;
|
---|
696 | }
|
---|
697 |
|
---|
698 | *fLog << inf << GetDescriptor() << " setup " << num << " master branches addresses." << endl;
|
---|
699 |
|
---|
700 | //
|
---|
701 | // If auto enabling scheme isn't disabled, do auto enabling
|
---|
702 | //
|
---|
703 | if (fAutoEnable)
|
---|
704 | EnableBranches(pList);
|
---|
705 |
|
---|
706 | //
|
---|
707 | // Now we can start notifying. Reset tree makes sure, that TChain thinks
|
---|
708 | // that the correct file is not yet initialized and reinitilizes it
|
---|
709 | // as soon as the first event is read. This is necessary to call
|
---|
710 | // the notifiers when the first event is read, but after the
|
---|
711 | // PreProcess-function.
|
---|
712 | //
|
---|
713 | fChain->ResetTree();
|
---|
714 | fChain->SetNotify(this);
|
---|
715 |
|
---|
716 | return GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
|
---|
717 | }
|
---|
718 |
|
---|
719 | // --------------------------------------------------------------------------
|
---|
720 | //
|
---|
721 | // Set the ready to save flag of all containers which branchaddresses are
|
---|
722 | // set for. This is necessary to copy data.
|
---|
723 | //
|
---|
724 | void MReadTree::SetReadyToSave(Bool_t flag)
|
---|
725 | {
|
---|
726 | TIter Next(fChain->GetStatus());
|
---|
727 |
|
---|
728 | TChainElement *element = NULL;
|
---|
729 | while ((element=(TChainElement*)Next()))
|
---|
730 | {
|
---|
731 | //
|
---|
732 | // Check whether the branch is enabled
|
---|
733 | //
|
---|
734 | if (!element->GetStatus())
|
---|
735 | continue;
|
---|
736 |
|
---|
737 | //
|
---|
738 | // Get the pointer to the pointer of the corresponding container
|
---|
739 | //
|
---|
740 | MParContainer **pcont = (MParContainer**)element->GetBaddress();
|
---|
741 |
|
---|
742 | //
|
---|
743 | // Check whether the pointer is not NULL
|
---|
744 | //
|
---|
745 | if (!pcont || !*pcont)
|
---|
746 | continue;
|
---|
747 |
|
---|
748 | //
|
---|
749 | // Set the ready to save status of the container.
|
---|
750 | //
|
---|
751 | (*pcont)->SetReadyToSave(flag);
|
---|
752 | }
|
---|
753 |
|
---|
754 | //
|
---|
755 | // Set the ready to save status of this task (used?), too
|
---|
756 | //
|
---|
757 | MTask::SetReadyToSave(flag);
|
---|
758 | }
|
---|
759 |
|
---|
760 | // --------------------------------------------------------------------------
|
---|
761 | //
|
---|
762 | // The Process-function reads one event from the tree (this contains all
|
---|
763 | // enabled branches) and increases the position in the file by one event.
|
---|
764 | // (Remark: The position can also be set by some member functions
|
---|
765 | // If the end of the file is reached the Eventloop is stopped.
|
---|
766 | // In case an event selector is given its value is checked before
|
---|
767 | // reading the event. If it returns kAFLSE the event is skipped.
|
---|
768 | //
|
---|
769 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
|
---|
770 | #include "MRawEvtData.h"
|
---|
771 | #endif
|
---|
772 | Int_t MReadTree::Process()
|
---|
773 | {
|
---|
774 | //
|
---|
775 | // This is necessary due to a bug in TChain::LoadTree in root.
|
---|
776 | // will be fixed in 3.03
|
---|
777 | //
|
---|
778 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
|
---|
779 | if (fNumEntry >= GetEntries())
|
---|
780 | return kFALSE;
|
---|
781 | #endif
|
---|
782 |
|
---|
783 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
|
---|
784 | //
|
---|
785 | // This fixes 99.9% of a memory leak using a root version prior
|
---|
786 | // to 3.02/??
|
---|
787 | //
|
---|
788 | TChainElement *element=NULL;
|
---|
789 | TIter Next(fChain->GetStatus());
|
---|
790 | while ((element=(TChainElement*)Next()))
|
---|
791 | {
|
---|
792 | MParContainer **c = (MParContainer**)element->GetBaddress();
|
---|
793 | if (!c) continue;
|
---|
794 | if ((*c)->InheritsFrom(MRawEvtData::Class()))
|
---|
795 | static_cast<MRawEvtData*>(*c)->DeletePixels(kFALSE);
|
---|
796 |
|
---|
797 | }
|
---|
798 | #endif
|
---|
799 |
|
---|
800 | if (GetSelector())
|
---|
801 | {
|
---|
802 | //
|
---|
803 | // Make sure selector is processed
|
---|
804 | //
|
---|
805 | if (!GetSelector()->CallProcess())
|
---|
806 | {
|
---|
807 | *fLog << err << dbginf << "Processing Selector failed." << endl;
|
---|
808 | return kFALSE;
|
---|
809 | }
|
---|
810 |
|
---|
811 | //
|
---|
812 | // Skip Event
|
---|
813 | //
|
---|
814 | if (!GetSelector()->IsConditionTrue())
|
---|
815 | {
|
---|
816 | fNumEntry++;
|
---|
817 | return kCONTINUE;
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 | const Bool_t rc = fChain->GetEntry(fNumEntry++) != 0;
|
---|
822 |
|
---|
823 | if (rc)
|
---|
824 | SetReadyToSave();
|
---|
825 |
|
---|
826 | return rc;
|
---|
827 | }
|
---|
828 |
|
---|
829 | // --------------------------------------------------------------------------
|
---|
830 | //
|
---|
831 | // If a selector is given the selector is post processed
|
---|
832 | //
|
---|
833 | Int_t MReadTree::PostProcess()
|
---|
834 | {
|
---|
835 | return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
|
---|
836 | }
|
---|
837 |
|
---|
838 | // --------------------------------------------------------------------------
|
---|
839 | //
|
---|
840 | // Get the Event with the current EventNumber fNumEntry
|
---|
841 | //
|
---|
842 | Bool_t MReadTree::GetEvent()
|
---|
843 | {
|
---|
844 | Bool_t rc = fChain->GetEntry(fNumEntry) != 0;
|
---|
845 |
|
---|
846 | if (rc)
|
---|
847 | SetReadyToSave();
|
---|
848 |
|
---|
849 | return rc;
|
---|
850 | }
|
---|
851 |
|
---|
852 | // --------------------------------------------------------------------------
|
---|
853 | //
|
---|
854 | // Decrease the number of the event which is read by Process() next
|
---|
855 | // by one or more
|
---|
856 | //
|
---|
857 | Bool_t MReadTree::DecEventNum(UInt_t dec)
|
---|
858 | {
|
---|
859 | if (fNumEntry-dec >= GetEntries())
|
---|
860 | {
|
---|
861 | *fLog << warn << GetDescriptor() << ": DecEventNum, WARNING - Event " << fNumEntry << "-";
|
---|
862 | *fLog << dec << "=" << (Int_t)fNumEntry-dec << " out of Range." << endl;
|
---|
863 | return kFALSE;
|
---|
864 | }
|
---|
865 |
|
---|
866 | fNumEntry -= dec;
|
---|
867 | return kTRUE;
|
---|
868 | }
|
---|
869 |
|
---|
870 | // --------------------------------------------------------------------------
|
---|
871 | //
|
---|
872 | // Increase the number of the event which is read by Process() next
|
---|
873 | // by one or more
|
---|
874 | //
|
---|
875 | Bool_t MReadTree::IncEventNum(UInt_t inc)
|
---|
876 | {
|
---|
877 | if (fNumEntry+inc >= GetEntries())
|
---|
878 | {
|
---|
879 | *fLog << warn << GetDescriptor() << ": IncEventNum, WARNING - Event " << fNumEntry << "+";
|
---|
880 | *fLog << inc << "=" << (Int_t)fNumEntry+inc << " out of Range." << endl;
|
---|
881 | return kFALSE;
|
---|
882 | }
|
---|
883 |
|
---|
884 | fNumEntry += inc;
|
---|
885 | return kTRUE;
|
---|
886 | }
|
---|
887 |
|
---|
888 | // --------------------------------------------------------------------------
|
---|
889 | //
|
---|
890 | // This function makes Process() read event number nr next
|
---|
891 | //
|
---|
892 | // Remark: You can use this function after instatiating you MReadTree-object
|
---|
893 | // to set the event number from which you want to start reading.
|
---|
894 | //
|
---|
895 | Bool_t MReadTree::SetEventNum(UInt_t nr)
|
---|
896 | {
|
---|
897 | if (nr >= GetEntries())
|
---|
898 | {
|
---|
899 | *fLog << warn << GetDescriptor() << ": SetEventNum, WARNING - " << nr << " out of Range." << endl;
|
---|
900 | return kFALSE;
|
---|
901 | }
|
---|
902 |
|
---|
903 | fNumEntry = nr;
|
---|
904 | return kTRUE;
|
---|
905 | }
|
---|
906 |
|
---|
907 | // --------------------------------------------------------------------------
|
---|
908 | //
|
---|
909 | // For the branch with the given name:
|
---|
910 | // 1) no object is automatically created
|
---|
911 | // 2) the branch address for this branch is not set
|
---|
912 | // (because we lack the object, see 1)
|
---|
913 | // 3) The whole branch (exactly: all its subbranches) are disabled
|
---|
914 | // this means are not read in memory by TTree:GetEntry
|
---|
915 | //
|
---|
916 | void MReadTree::VetoBranch(const char *name)
|
---|
917 | {
|
---|
918 | fVetoList->Add(new TNamed(name, ""));
|
---|
919 | }
|
---|
920 |
|
---|
921 | // --------------------------------------------------------------------------
|
---|
922 | //
|
---|
923 | // Return the name of the file we are actually reading from.
|
---|
924 | //
|
---|
925 | TString MReadTree::GetFileName() const
|
---|
926 | {
|
---|
927 | const TFile *file = fChain->GetFile();
|
---|
928 |
|
---|
929 | if (!file)
|
---|
930 | return TString("<unknown>");
|
---|
931 |
|
---|
932 | TString name(file->GetName());
|
---|
933 | name.Remove(0, name.Last('/')+1);
|
---|
934 | return name;
|
---|
935 | }
|
---|
936 |
|
---|
937 | // --------------------------------------------------------------------------
|
---|
938 | //
|
---|
939 | // Return the number of the file in the chain, -1 in case of an error
|
---|
940 | //
|
---|
941 | Int_t MReadTree::GetFileIndex() const
|
---|
942 | {
|
---|
943 | return fChain->GetTreeNumber();
|
---|
944 | /*
|
---|
945 | const TString filename = fChain->GetFile()->GetName();
|
---|
946 |
|
---|
947 | int i=0;
|
---|
948 | TObject *file = NULL;
|
---|
949 |
|
---|
950 | TIter Next(fChain->GetListOfFiles());
|
---|
951 | while ((file=Next()))
|
---|
952 | {
|
---|
953 | if (filename==gSystem->ExpandPathName(file->GetTitle()))
|
---|
954 | return i;
|
---|
955 | i++;
|
---|
956 | }
|
---|
957 | return -1;
|
---|
958 | */
|
---|
959 | }
|
---|
960 |
|
---|
961 | // --------------------------------------------------------------------------
|
---|
962 | //
|
---|
963 | // This schedules a TObject which Notify(9 function is called in case
|
---|
964 | // of MReadTree (TChain) switches from one file in the chain to another
|
---|
965 | // one.
|
---|
966 | //
|
---|
967 | void MReadTree::AddNotify(TObject *obj)
|
---|
968 | {
|
---|
969 | fNotify->Add(obj);
|
---|
970 | }
|
---|
971 |
|
---|
972 | void MReadTree::Print(Option_t *o) const
|
---|
973 | {
|
---|
974 | *fLog << all << underline << GetDescriptor() << ":" << endl << dec;
|
---|
975 | *fLog << " Files [Tree]:" << endl;
|
---|
976 |
|
---|
977 | int i = 0;
|
---|
978 | TIter Next(fChain->GetListOfFiles());
|
---|
979 | TObject *obj = NULL;
|
---|
980 | while ((obj=Next()))
|
---|
981 | *fLog << " " << i++ << ") " << obj->GetTitle() << " [" << obj->GetName() << "]" << endl;
|
---|
982 |
|
---|
983 | *fLog << " Total Number of Entries: " << fNumEntries << endl;
|
---|
984 | *fLog << " Next Entry to read: " << fNumEntry << endl;
|
---|
985 | }
|
---|
986 |
|
---|
987 | // --------------------------------------------------------------------------
|
---|
988 | //
|
---|
989 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
990 | // to a macro. In the original root implementation it is used to write
|
---|
991 | // gui elements to a macro-file.
|
---|
992 | //
|
---|
993 | void MReadTree::StreamPrimitive(ofstream &out) const
|
---|
994 | {
|
---|
995 | out << " " << ClassName() << " " << GetUniqueName() << "(\"";
|
---|
996 | out << fChain->GetName() << "\", \"" << fName << "\", \"" << fTitle << "\");" << endl;
|
---|
997 |
|
---|
998 | TIter Next(fChain->GetListOfFiles());
|
---|
999 | TObject *obj = NULL;
|
---|
1000 | while ((obj=Next()))
|
---|
1001 | out << " " << GetUniqueName() << ".AddFile(\"" << obj->GetTitle() << "\");" << endl;
|
---|
1002 |
|
---|
1003 | if (!fAutoEnable)
|
---|
1004 | out << " " << GetUniqueName() << ".DisableAutoScheme();" << endl;
|
---|
1005 |
|
---|
1006 | if (fNumEntry!=0)
|
---|
1007 | out << " " << GetUniqueName() << ".SetEventNum(" << fNumEntry << ");" << endl;
|
---|
1008 |
|
---|
1009 |
|
---|
1010 | }
|
---|