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