Hot wo Add a Leaf to an existing Tree?

Hallo, im sure, there is an easy solution, i checked already the manual and didnt find it. i hope someone can halp me here!

i have an .root file. inside is a Tree that has 10 Branches. every Branch has about 100 integers.
i would like to add integers to this Branches. Here my code:
TreeExists is a bool, it just check, if the tree already exists;

if (!TreeExists)
{
Tree=new TTree(“T1”,“Tsample”);
}

else {
Tree = (TTree*)File->Get(“T1”);
}

    Tree->Branch("Time",&Ev.Time,"Time/I");  
    Tree->Branch("LumiBlock",&Ev.LumiBlock,"LumiBlock/I");  
    Tree->Branch("ID",&Ev.ID,"ID/I");  
    Tree->Branch("Item",&Ev.Item,"Item/I");
    
    Tree->Fill();

that code generates me a lot of Trees in my .roor File. I just want to keep me first created tree and fill the branches.

Create another Tree in the same file or (better) in a different file and declare the second Tree to be a friend of the original Tree. See TTree::AddFriend

Rene

ok, it works, but its not a good solution, because i have very huge files, and if i use that method, it copies all the files+new files to a new tree and rewrite the old one. i would like to keep the old one and just add the new leafs.
i tried it with TChain, but it will not work )-;

NOOOOOOOOOOOOOOOO!
It should not copy the old Tree. Read carefully my message.
The new Tree should contain only the new branches. The old Tree and old file remains untouched.

Rene

hmm, it doent work, kan u check my code?
here an example;

int a;
int b;
{
    TFile *File=new TFile("~/T1.root","recreate");
    TTree *Tree=new TTree("T1","Tsample");    
    Tree->Branch("a",&a,"a/I");
    Tree->Branch("b",&b,"b/I");
    
    for (int i=0; i<10; i++) {
        a=i;
        b=i*i;
        Tree->Fill();
    }
    File->Write();
    File->Close();
}


{
    TFile *FileF=new TFile("~/T1F.root","recreate");
    TTree *TreeF=new TTree("T1","Tsample");    
    TreeF->Branch("a",&a,"a/I");
    TreeF->Branch("b",&b,"b/I");
    
    for (int i=10; i<12; i++) {
        a=i;
        b=i*i;
        TreeF->Fill();
    }
    FileF->Write();
    FileF->Close();
}

TFile *t=new TFile("~/T1.root","open");
TFile *f=new TFile("~/T1F.root","open");

TTree *tree;
TTree *frie;
t->GetObject("T1", tree);
f->GetObject("T1", frie);

tree->AddFriend(frie);
tree->Scan();

and when i have the whole tree in my programm and i want to save it into a rootfile. it hast to copy every entry again?

…hallo?..

[quote]hmm, it doent work, kan u check my code?[/quote]How does it fail?

To make it easier to distinguish them you should call your new TTree and your new leaves with a different name. i.e.[code]int a;
int b;
{
TFile *File=new TFile("~/T1.root",“recreate”);
TTree *Tree=new TTree(“T1”,“Tsample”);
Tree->Branch(“a”,&a,“a/I”);
Tree->Branch(“b”,&b,“b/I”);

for (int i=0; i<10; i++) {
a=i;
b=i*i;
Tree->Fill();
}
File->Write();
File->Close();
}

{

TFile *FileF=new TFile("~/T1F.root",“recreate”);
TTree *TreeF=new TTree(“T1F”,“Additional Information”);
int c;
TreeF->Branch(“c”,&c,“c/I”);

for (int i=10; i<12; i++) {
c=iii;
TreeF->Fill();
}
FileF->Write();
FileF->Close();
}

TFile *t=new TFile("~/T1.root",“open”);
TFile *f=new TFile("~/T1F.root",“open”);

TTree *tree;
TTree *frie;
t->GetObject(“T1”, tree);
f->GetObject(“T1F”, frie);

tree->AddFriend(frie);[/code]

[quote]tree->Scan();
[/quote]If I remember correctly the default Scan does not traverse friends. But you can do:tree->Scan("a:b:c:T1F.c");

Cheers,
Philippe.