Re: updating values in a tree

From: Axel Naumann <Axel.Naumann_at_cern.ch>
Date: Tue, 8 Jun 2010 12:19:37 +0200


Hi,

TTrees are read-only - that's part of the reason why they are so efficient. Instead create your own file and tree with just the branch you want to update, and write the changed branch to that tree. See e.g. tutorials/tree/copytree3.C.

If you want to access both the old tree and the modified data later, you can use TTree::AddFriend() to add your new branch to the existing tree. See e.g. tutorials/tree/tree3.C.

Cheers, Axel.

Maximilien Fechner wrote on 06/08/2010 11:52 AM:
> Dear ROOT-ers,
>
> If this question has already been answered, please point to the
> answer, I couldn't find it in the archive...
>
> I have a ROOT tree containing data in one split branch. The branch
> holds a TCLonesArray of identical objects (very lightweight,
> just a struct with a few double values).
> I would like to update some of those values "on the fly" :
> the branch already exists, the variables too, I just want
> to change their values.
> but calling "branch->Fill" doesn't update the values...
> (see example below).
>
> Is there a way to do what I describe ?
>
> Many thanks in advance,
> -Maximilien Fechner
>
> Here's a simple example:
>
> TFile* f = new TFile("aaa.root","update");
> TTree* a = (TTree*)f->Get("tree");
> TClonesArray* p =new TClonesArray("smallobject");
> TBranch* theb;
> a->SetBranchAddress("B",&p,&theb);
>
> for ( int i = 0 ; i < a->GetEntries(); i++)
> {
> a->GetEntry(i);
> smallobject* o = dynamic_cast<smallobject*>( (*p)[0]);
> p->to_be_updated = my_new_result();
> theb->Fill();
> }
>
> a->Write("",TObject::kWriteDelete);
>
>
> -> the values of "to_be_updated" are still the same after this is run...
>
Received on Tue Jun 08 2010 - 12:16:56 CEST

This archive was generated by hypermail 2.2.0 : Tue Jun 08 2010 - 17:50:01 CEST