Modifying a TTree on file

Right, I know how to create a tree, fill it, write it to file and read it back.
But what if I want to modify some of the objects in the tree that is already on file?
What do I have to do?
Also, what if I want to completely remove some of the objects already in the tree on file?
I can not find any clear documentation or examples for these two cases.
Please help.

Thank you.

See TTree documentation at
root.cern.ch/root/htmldoc/TTree.html
look at section “Adding a Branch to an Existing Tree”

Rene

Yes, yes I know about adding a new branch.
But I do not want to add a new branch, I want to change the contents of a branch I already filled.
Here is an example:

{
// Create a tree with a branch of MyClass objects and write it to file.
TFile* file = new TFile(“test.root”, “UPDATE”);
TTree* tree = new TTree(“tree”, “some tree”);
MyClass* obj = NULL;
tree->Branch(“mybranch”, “MyClass”, &obj);
obj = new MyClass();
tree->Fill();
tree->Write();
file->Close();
delete file;
}

{
// Now open the same file to modify the object added to the tree.
TFile* file = new TFile(“test.root”, “UPDATE”);
TTree* tree = (TTree*) file->Get(“tree”);
MyClass* obj = NULL;
tree->SetBranchAddess(“mybranch”, &obj);
tree->GetEntry(0);
obj->Modify(); // Do my modifications…
// Now what? Are the changes already applied in the tree?
// Do I call Fill again? will that not just add another object?
// What if I want to delete the object from the tree, how do I do that?
}

Hi,

TTree is designed as a write-once, read-many times container. The updating of existing object in a TTree is not supported.
As an alternative you can Clone (and modify) an existing TTree object (see TTree::CloneTree).

Cheers,
Philippe.