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