Modifying a leaf value

Hi,

I want to do the following:

a) get a leaf off of a branch in a tree
b) modify its value and “save” the tree

Is there a simple procedure for doing that?

Hi,

TTree are write-once, read many time structure. You will need top copy/modify the TTree to a new file. See $ROOTSYS/tutorials/copytree3.C [but instead of filtering, you would modify).

Cheers,
Philippe.

Phillipe,

Thank you for the suggestion – I think I got it, I’ll give it a try tomorrow.

One question while we are at it…in my case the equivalent of the tutorial’s event object is a structure. I need to modify one of the elements of the structure, say structure.pid. Would the following accomplish what I am trying –

//create an instance of the Structure
   Structure structure;
   oldtree->SetBranchAddress("structure",&structure);
   TTree *newtree = oldtree->CloneTree(0);

   for (Int_t i=0;i<nentries; i++) {
       oldtree->GetEntry(i);
       structure.pid=the_right_particle_id;
       newtree->Fill();
   }

Hi,

Yes this looks fine (except for the missing final call to Write that did no make it in the copy/paste).

Cheers,
Philippe.

Phillipe,

I got the code to work. Many thanks!!

One observation though…

When I pass the pointer oldtree to another function, and then INSIDE that function I try

TTree *newtree =  oldtree->CloneTree(0),

it segfaults. Otherwise, all works well if both the oldtree and newtree are instantiated inside the same function.

[quote]When I pass the pointer oldtree to another function, and then INSIDE that function I try[/quote]I would need to see the code for both the Caller and the Callee in order to investigate this further.

Cheers,
Philippe.