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