Overwriting existing ttree entries


could someone point me to a relevant tutorial or example which shows how to overwrite an existing entry in a ttree? i have a branch of data which has a few errors and needs to be corrected. i have naively tried something of the following, but to no avail.

... TFile *_file0 = TFile::Open("Data.root","UPDATE"); TTree * ntp1=(TTree*) _file0->Get("ntp1"); myClass * ptr; TBranch *br = ntp1->GetBranch("mydata"); br->SetAddress(&ptr); ntp1->GetEntry(x);//x is the location of the corrupted value ptr->SetVal(y);//y is the corrected value br->Fill(); ntp1->Write("",TObject::kOverwrite); _file0->Close();



TTree are designed for write-once, read many time. I.e. it is not really possible to change the value in a TTree.

You can either create a new additional tree with the new information and make it a friend of the original.
Or you can Clone (via CloneTree) your original tree into a new tree and modifying the proper value during the copy.



that’s a shame - i think this kind of functionality would be extremely useful and there must be a way to do it, even if it means adding a method to TBranch along the lines of ModifyEntry(int _entry). i am pretty sure i am not the only person who would use this. i am prepared to have a go at writing the functionality i need, but i suspect i dont understand enough of what is going on under the hood of root for it to not be buggy. if i was to do this are there any suggestions as to the best way to go forward? in fact while i am on the subject, would it be possible to insert a new entry between existing entries?



i’ve wanted to do this in the past. would it be possible to implement something like this as part of the functionality? or perhaps inherit TTree to have a TModifiableTree…? I’d be interested.


As already reported by Philippe, it does not make much sense to use
Trees to solve this problem. Any implementation would be extremely slow.
Use the normal object.Write mechanism (or TDirectory::WriteObject equivalent).