Pointers to TLeaves & chain of root-tuples


I have a piece of code that gets data from a root-tuple (let’s call it the wrapper class) and then presents it to a different piece of code (let’s call it the algorithm class) where the actual calculation is performed. I want my algorithm to be completely unaware of the input format. The way I do this is by getting the pointers to the leaves that contain the data and passing them on to the algorithm:

Float_t * ptr2data= (Float_t *) b_MYBRANCH->GetLeaf(“myLeaf”)->GetValuePointer();

Float x = ptr2data[i]*ptr2data[i] + 3;

This works beautifully when the input is a signle root-tuple. When I have a chain of root-tuples it fails with a segmentation fault. I believe the memory address of the ptr2data is not constant when I’m dealing with multiple input files.

Is there a way around it?


EDIT: I forgot to mention that I currently do the pointer initialization only at the beginning of the job (ie. at the constructor). I could try to put this in the event loop, but then this would make the code much slower.


You must recompute the branch address and the pointer ptrdata
when you load a new file.


Ok, but how do I know when the next file is loaded? All I do is chain together a bunch of root-tuples and then run my code on it. Is there a method I can call to know if the chain->GetEntry() will give me an event from the next file?


Ok, how about this:

  TBranch * last_MYBRANCH = 0;

  (for int jentry = 0; jentry != chain->GetEntries(); ++)
   if(chain->GetBranch("MYBRANCH") != last_b_MYBRANCH)
last_MYBRANCH = (TBranch *) chain->GetBranch("MYBRANCH");

    Float_t * ptr2data= 
    (Float_t *) last_MYBRANCH->GetLeaf("myLeaf")->GetValuePointer(); 
     // etc.

   } // event loop


Hi Christos,

You cannot rely on the address to change. Use GetTreeNumber() to get the index
of the current tree within the chain and reset the addresses when it changes.



Ok, but if the address does not change when I move from one tree to the next one, doesn’t this mean that I could use the pointers that have been initialized with the previous tree’s addresses?


What you really want to know is if the object hast changed. You cannot judge that from
the address as it could have been deleted and the new object could have been
created at the same location. The tree number is intrinsically a correct way to check
if the tree has changed. It is also an inlined getter so it is just as fast as comparing
the pointers …