Problem with Filling a Tree

Dear Experts,

I’m trying to understand filling a tree with very simple following macro,

void testtree() {

TFile* tfile = new TFile(“treetest.root”, “RECREATE”);

Double_t test1, test2;

fTree = new TTree(“Treetest”,“Treetest”);
fTree->Branch(“test1”,&test1,“test1/D”);
fTree->Branch(“test2”,&test2,“test2/D”);

for(Int_t i=0; i<10; i++) {test1=i;}
for(Int_t j=0; j<20; j++) {test2=j;}

fTree->Fill();
fTree->Write();
}
The entry number of each branch is 1 and I don’t understand why is this. I was expecting 10 entry for the first branch and 20 entry for the second one. Am I dong something wrong?

Thank you,

Kind regards,

Ayben

[quote]The entry number of each branch is 1 and I don’t understand why is this.[/quote]Because you only call Fill once.

[quote] I was expecting 10 entry for the first branch and 20 entry for the second one.[/quote]Even-though this is ‘possible’ to do, we strongly advise against it.

To fill a TTree, you should o:[code]void testtree() {

TFile* tfile = new TFile(“treetest.root”, “RECREATE”);

Double_t test1, test2;

fTree = new TTree(“Treetest”,“Treetest”);
fTree->Branch(“test1”,&test1,“test1/D”);
fTree->Branch(“test2”,&test2,“test2/D”);

for(Int_t i=0; i<10; i++) {
test1=i;
test2=j;
fTree->Fill();
}
fTree->Write();
}[/code]

Cheers,
Philippe.

Dear Philipphe,

Thank you for the reply. But in my case j goes to 20 and to fill it I used following,

void testtree() {

TFile* tfile = new TFile(“treetest.root”, “RECREATE”);
Int_t itrk=0; Int_t jtrk=0;
Double_t test1[50], test2[50];

fTree = new TTree(“Treetest”,“Treetest”);

fTree->Branch(“itrk”, &itrk, “itrk/I”);
fTree->Branch(“jtrk”, &jtrk, “jtrk/I”);
fTree->Branch(“test1”,test1,“test1[itrk]/D”);
fTree->Branch(“test2”,test2,“test2[jtrk]/D”);

for(Int_t i=0; i<10; i++) {test1[itrk]=i; itrk ++;}
for(Int_t j=0; j<20; j++) {test2[jtrk]=j; jtrk ++;}

fTree->Fill();
fTree->Write();

}

Now I can fill the tree correctly. Actually I have about 100 different loop like this and I don’t want to create a new tree for each loop. I just wanted to put the result of each loop in a different branch. So, if you don’t recommend to use this structure, what should I use to collect the results?

Thank you,

Kind regards,

Ayben

[quote] So, if you don’t recommend to use this structure, what should I use to collect the results? [/quote]What is the higher level description of what you need to achieve?

[quote] Actually I have about 100 different loop like this and I don’t want to create a new tree for each loop.[/quote]I would create the TTree outside of the function and pass it around to the other filler.

[quote]I just wanted to put the result of each loop in a different branch.[/quote]Usually, this is done is 3 steps:

  • create all the branches and associate place holder
  • set the value in the branch holders
  • call Fill

Cheers,
Philippe.

VIVE L’AMOUR!
what you are trying to do is a bit crazy, but suit yourself … :mrgreen:

{
  Int_t itrk=0; Int_t jtrk=0;
  Double_t test1[50], test2[50];
  
  TFile* tfile = new TFile("treetest.root", "RECREATE");
  
  TTree *fTree = new TTree("Treetest","Treetest");
  
  TBranch *b_itrk = fTree->Branch("itrk", &itrk, "itrk/I");
  TBranch *b_jtrk = fTree->Branch("jtrk", &jtrk, "jtrk/I");
  TBranch *b_test1 = fTree->Branch("test1", test1, "test1[itrk]/D");
  TBranch *b_test2 = fTree->Branch("test2", test2, "test2[jtrk]/D");
  
  for(Int_t i = 0; i < 10; i++) {
    test1[itrk]=i; itrk ++;
    
    b_itrk->Fill(); b_test1->Fill();
  }
  
  for(Int_t i = 0; i < 20; i++) {
    test2[jtrk]=i; jtrk ++;
    
    b_jtrk->Fill(); b_test2->Fill();
  }
  
  fTree->Write();
  
  delete tfile;
}
{
  Int_t itrk=0; Int_t jtrk=0;
  Double_t test1[50], test2[50];
  
  TFile* tfile = new TFile("treetest.root", "READ");
  
  TTree *fTree = (TTree*)tfile->Get("Treetest");
  
  TBranch *b_itrk, *b_jtrk, *b_test1, *b_test2;
  fTree->SetBranchAddress("itrk", &itrk, &b_itrk);
  fTree->SetBranchAddress("jtrk", &jtrk, &b_jtrk);
  fTree->SetBranchAddress("test1", test1, &b_test1);
  fTree->SetBranchAddress("test2", test2, &b_test2);
  
  cout << endl;
  cout << "fTree->GetEntries()   = " << fTree->GetEntries() << endl
       << "b_itrk->GetEntries()  = " << b_itrk->GetEntries() << endl
       << "b_jtrk->GetEntries()  = " << b_jtrk->GetEntries() << endl
       << "b_test1->GetEntries() = " << b_test1->GetEntries() << endl
       << "b_test2->GetEntries() = " << b_test2->GetEntries() << endl;
  cout << endl;
  
  for(Int_t i = 0; i < b_itrk->GetEntries(); i++) {
    b_itrk->GetEvent(i); b_test1->GetEvent(i);
    
#if 1 /* 0 or 1 */
    cout << "i = " << i << "  itrk = " << itrk << endl;
    cout << "test1[0 ... " << (itrk - 1) << "] = " << endl;
    for(Int_t j = 0; j < itrk; j++) cout << test1[j] << endl;
    cout << endl;
#endif /* 0 or 1 */
  }
  
  for(Int_t i = 0; i < b_jtrk->GetEntries(); i++) {
    b_jtrk->GetEvent(i); b_test2->GetEvent(i);
    
#if 1 /* 0 or 1 */
    cout << "i = " << i << "  jtrk = " << jtrk << endl;
    cout << "test2[0 ... " << (jtrk - 1) << "] = " << endl;
    for(Int_t j = 0; j < jtrk; j++) cout << test2[j] << endl;
    cout << endl;
#endif /* 0 or 1 */
  }
}

See http://root.cern.ch/root/html/TTree.html and http://root.cern.ch/root/html/TBranch.html for details. <img src="/uploads/default/original/2X/8/84c2fe9464a4066c00e1bd5978e913e7869cbb07.gif" width=“22” height=“16” alt=":-"" title=“Whistle”/>

I am stupid. No?
Pepe Le Pew.