How does a tree is filled

Hi dear Rooters,
I have a question, a dumb one.
Just about the tree filling.
When I create a tree with different branches, how are they filled ?
I mean, I have a branch called “time” with values from 0 to 1 for example.
I can have access the data in “time”, doing

TFile *file_adress = TFile::Open("file_name");
TTree *tree = new TTree("name","title");
Double_t time;
tree->SetBranchAddress("time",&time);
for (Long64_t i=0;i<tree->GetEntries();i++) {
  tree->GetEntry(i);
  cout << time <<  endl;
}

Output is a mess, I mean I thought it should be ordered and increasing from 0 to 1 but it is not…
I know my question is really for beginners and not well expressed, but I really don’t understand…

Thank you for help,
Cloé

For putting data in a TTree you meant:

TFile *file_adress = TFile::Open("file_name");
TTree *tree = new TTree("name","title");
Double_t time;
tree->Branch("time",&time);
for (Long64_t i=0;i< kNumberOfTimesToFillIntoTheTree; ++i) {
  time = i; // or what ever the time is suppose to be for that entry.
  tree->Fill();
}
file_adress->Write();

and to read it back

TFile *file_adress = TFile::Open("file_name","READ");
TTree *tree; file_adress->GetObject("name",tree);
if (!tree) { 
   std::cerr << "Can't find the tree\n";
   return;
} 
Double_t time;
tree->SetBranchAddress("time",&time);
for (Long64_t i=0;i<tree->GetEntries();i++) {
  tree->GetEntry(i);
  cout << time <<  endl;
}

Hi,
Thank you for your reply.

I read the tree exactly the way you wrote, but the output is something like

29.4684
0.904424
240.517
0.441073
176.045
0.549272
32.9066
0.893908
221.46


I thought time output were put in order from 0 to 1500 (for example), as you can see :

But it is obviously not the case and I don’t understand why.

Thanks,
Cloé

Could you share one of your ROOT files?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.