Infrequent branch reading

Hello,

I have two TChains, one is the friend of the other, and each TChain contains one branch in it that holds an custom object. I am trying to optimize my code, and have noticed that reading objects seems to have quite an overhead. I am trying to reduce read-time by using the fact that I only need to read the object in the second chain occasionally.

Load up chain1 (which automatically means chain2 is loaded, since it is a friend). Than I do:

chain1.SetBranchStatus(“branch2”,0);
Class1 *obj1 = 0;
chain1.SetBranchAddress(“branch1”,&obj);

then I start my loop over events, loading just the branch1. But, after some logic, I want to load the corresponding object in branch2. What command can I give, in terms of chain1, to load this branch in to my pointer, just for that event?

Class2 *obj2 = 0;
chain1. ???

Given my setup, it would be useful to know if there are any other ways I can speed up the read process, since that is really what is slowing my processing down.

Cheers,
Will

Hi Will,

The best is to use

Class1 *obj1 = 0; TBranch *obj1_branch = 0; chain1.SetBranchAddress("branch1",&obj,&obj1_branch);and instead of relying on SetBranchStatus, when you need to return the branches use:Long64_t localEntry = chain1.LoadTree(entry); obj1_branch->GetEntry(localEntry);(and the same or similar for obj2).

Cheers,
Philippe.

Hi Philippe,

Thanks for your suggestion. Unfortunately my code is running within a framework where a GetEntry call is made which I cannot avoid (it’s in some code that I cannot change). Is there any way to stop the GetEntry call from reading branch2, while still allowing me to read it when it is needed? Using SetBranchStatus does stop GetEntry reading the branch, but it also stops me being able to read it when I want.

Sorry about things getting a little complicated!

To complicate matters further (and following on from a previous thread), these two chains do not have the same number of entries, they are connected to each other through the index that was built for the second chain. This setup works fine if chain1->GetEntry() is used, because it ensures the correct entry from chain2 is located. But if I use the branches directly, it isn’t clear to me how to query chain1 to find out which entry to load.

And either way, I still have the problem that I cannot avoid using GetEntry because of my framework

Hi Will,

[quote]But if I use the branches directly, it isn’t clear to me how to query chain1 to find out which entry to load. [/quote]For the 2nd chain, since it was made a friend of the 1st chain, the call to 1st_chain->LoadTree would induce the call to the 2nd chain’s LoadTree which the right entry number. You would then recover the equivalent of ‘localEntry’ by calling chain2->GetTree()->GetReadEntry().

[quote] Is there any way to stop the GetEntry call from reading branch2, while still allowing me to read it when it is needed? Using SetBranchStatus does stop GetEntry reading the branch, but it also stops me being able to read it when I want.[/quote]Yes, you will need to use:[code]chain1.SetBranchAddress(“branch1”,&obj);
chain1.SetBranchStatus(“branch2*”,0); // to disable the branch and all its subbranches.

chain1.SetBranchStatus(“branch2*”,1); // to re-enable the branch and all its subbranches.[/code]

Cheers,
Philippe.