TTree Add Branch

Dear ROOT,

I am trying to add a branch for which the object will be a array of Double_t. In my case an array containing the track etas for an event. I have tried but been unsuccessful at writing this to a ttree (see below code - tries to write a single entry into a tree). Can you help?

TTree* tree = new TTree("name", "title"); unsigned int n_tracks = 100; Double_t eta[n_tracks]; tree->Branch("eta", &eta); tree->Branch("n_tracks", &n_tracks); for(unsigned int i=0; i<n_tracks; i++) { eta[i] = double(rand() % 200)/100; cout << eta[i] << endl; } tree->Fill();

the data would then be read back as such from a different script,

[code] TFile* tfile = new TFile::Open(“output.root”);
TTree t1 = (TTree)f->Get(“name”);
Int_t n_tracks;
Float_t eta[n_tracks];

t1->SetBranchAddress("n_tracks", &n_tracks);
t1->SetBranchAddress("track_eta", &eta);

TH1F *hist = new TH1F("eta_name", "eta_title", 400, 0., 400.);

// all entries and fill the histograms
Int_t nentries = (Int_t)t1->GetEntries();
for (Int_t i=0;i<nentries;i++) {
	cout << i << endl;
	for(Int_t j=0; j<n_tracks; j++)
	{
		hist->Fill(eta[j]);
	}
}

[/code]

Resolved

TTree* tree = new TTree("name", "title"); unsigned int n_tracks = 10; vector<Double_t>* track_eta = new vector<Double_t>(); // Double_t track_eta[n_tracks]; tree->Branch("track_eta", &track_eta); tree->Branch("n_tracks", &n_tracks); for(unsigned int i=0; i<n_tracks; i++) track_eta->push_back(1); tree->Fill(); tree->Scan();

VIVE L’AMOUR!
see the “fixed” source code below.
Your main problems were:

  1. when creating TTree - improper definition of the “eta” branch (and it’s better to create the “n_tracks” branch before the “eta” one, as the “eta” branch depends on “n_tracks”, maybe it’s event a “requirement” in ROOT, I’m not sure)
  2. when reading TTree - you did not initialize the “n_tracks” variable in the beginning, you defined “eta[]” as “Float_t” instead of “Double_t” (and I prefer to “SetBranchAddress” for the “eta” branch using just “eta”, not “&eta”), you tried to access a nonexistent branch named “track_eta” (instead of “eta”), you did not call “GetEntry(i)” in the loop (and the way you try to create the “output.root” file in the beginning is also wrong)
    I am stupid. No?
    Pepe Le Pew.
{
   Int_t n_tracks = 100; // the MAXIMUM allowed value MUST be set here
   Double_t eta[n_tracks];

   TFile* tfile = new TFile("output.root", "RECREATE");
   TTree* tree = new TTree("name", "title");

   tree->Branch("n_tracks", &n_tracks);
   tree->Branch("eta", eta, "eta[n_tracks]/D");

   for(unsigned int i=0; i<n_tracks; i++)
   {
      eta[i] = double(rand() % 200)/100;
      cout << eta[i] << endl;
   }
   tree->Fill();

   tree->Write();
   delete tfile;
}
{
   Int_t n_tracks = 100; // the MAXIMUM allowed value MUST be set here
   Double_t eta[n_tracks];

   TH1F *hist = new TH1F("eta_name", "eta_title", 400, 0., 400.);

   TFile* tfile = new TFile("output.root", "READ");
   TTree *t1 = (TTree*)tfile->Get("name");
   TBranch *b_n_tracks;
   TBranch *b_eta;

   t1->SetBranchAddress("n_tracks", &n_tracks, &b_n_tracks);
   t1->SetBranchAddress("eta", eta, &b_eta);

   // all entries and fill the histograms
   Int_t nentries = (Int_t)t1->GetEntries();
   for (Int_t i=0;i<nentries;i++) {
      cout << i << endl;
#if 1 /* 0 or 1 */
      t1->GetEntry(i);
#else  /* 0 or 1 */
      b_n_tracks->GetEntry(i); // "n_tracks" branch first
      b_eta->GetEntry(i); // "eta" branch depends on "n_tracks"
#endif /* 0 or 1 */
      for(Int_t j=0; j<n_tracks; j++)
      {
         hist->Fill(eta[j]);
      }
   }

   hist->Draw();
}