Enabling/disabling Branches in event loop

Hi,

I have a chain of very large trees (a few M events). These trees contain TClonesArray’s and other branches. What I want to do is: loop through the events, identify a handful of “interesting” events (through an event list that I have), and then make a skim tree with only those events.

Since these trees are very large, I do not want to unpack all the branches in the event loop for the majority of the events. Therefore, I disable all branches except for one that contains the event information (run #, event #), and if the event should be kept, I then want to enable all the branches and make a copy of the tree for that particular event, or process the event, etc. The logic looks like this:

Initialization:

  • Load chain of trees
  • Disable all braches (i.e. SetBranchStatus("*", 0) )
  • Enable branches with event, run numbers only

Event loop

  • chain->GetEntry(i);

  • get event #, run #

  • if (interesting event){
    // enable all branches
    SetBranchStatus("*", 1);
    // load event again (all branches this time)
    chain->GetEntry(i);

    Make copy of tree, process event, etc.

    Disable all braches again (i.e. SetBranchStatus("*", 0) )
    Enable branches with event, run numbers only
    }

Now, my problem is that when I try to load the event again in the “interesting event” loop, I get a segmentation fault. I do not understand why…

Part of my confusion is the lack of experience with calling SetBranchStatus("*", 1 or 0) inside the event loop. At what point do I need to do things like

Event *event = new Event();
chain.SetBranchAddress(“event”, &event);

Just outside the event loop? Does turning the branch off and then on again screw up the above?

How do I deal with the fact that I have chain (and not a sigle tree)? Do I need to re-set the branch address when I read the next tree in the chain?

Probably too many questions, but I’m a little lost. :wink:

Thanks a lot!

–Christos

Oops! Forgot the ROOT version:

% which root
/D0/ups/root/Linux-2-4/v3_05_07aKCC_4_0-exception-opt-thread/bin/root

Hi,

If I remember correctly there were issues regarding SetBranchStatus in 3.05.07.

In your case, it will simplier and quicker to NOT use SetBranchStatus but to use TBranch::GetEntry instead. For all version of ROOT this will be quicker because disabling/enabling branches is non-trivial operation (i.e. slow).

So you synopsis becomes:

Initialization:

  • Load chain of trees

Event loop

  • chain->LoadEntry(i)

  • EventBranch->GetEntry(i)

  • RunBranch->GetEntry(i)

  • get event #, run #

  • if (interesting event){
    // load event again (all branches this time)
    chain->GetEntry(i);
    }

  • Make copy of tree, process event, etc.

}

Cheers,
Philippe

That was an excellent piece of advice.

Thanks a lot, Philippe!

–Christos