Reading a tree

Hello,

I have a TLeafD in a Branch. How can I reach the entries in a leaf?

Regards,
Eda

several possibilities: eg

-1

double myvar; mytree->SetBranchAddress("branchname",&myvar); //loop on entries and for each entry mytree->GetEntry(entry); //now you can use variable myvar

  • 2

TLeaf *leaf = mytree->GetLeaf("branchname"); //loop on entries and for each entry read only this branch/leaf leaf->GetBranch()->GetEntry(entry); double myvar = (double*)leaf->GetValue();

Rene

I tried both

[color=#FF0000]in the first one, my code is: [/color]

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "TH1.h"
#include "TFile.h"
#include "TTree.h"
#include "TLeaf.h"
#include "TBranch.h"

void add_hists(){

char ttit[64];
char btit[64];
int nums[5]={100718,100772,101461,101472,101481};
double par3;
int nentries;
TH1F *hist = new TH1F("hist","par3",100,0,60);
TH1F *hist_with_cut = new TH1F("hist_with_cut","par3>5",100,0,60);

for(int i=0; i<5; i++){
  sprintf(ttit,"hf_LightLeak%d.root",nums[i]);
  TFile *f = new TFile(ttit);
  TTree *t = (TTree*)f->Get("T");
  sprintf(btit,"run%d",nums[i]);
  t->SetBranchAddress(btit,&par3);
  nentries = t->GetEntries();

  for (int j=0; i<nentries; i++) {
    t->GetEntry(i);
    hist->Fill(par3);
    if(par3>5)
    hist_with_cut->Fill(par3);
}
}
hist->Draw();
hist_with_cut->Draw();
}

it gives:
Error in TTree::SetBranchAddress: The pointer type given “Double_t” (8) does not correspond to the type needed “Int_t” (3) by the branch: run100718

[color=#FF0000]in the second one, I changed the code as:[/color]

void add_hists(){

char ttit[64];
char btit[64];
int nums[5]={100718,100772,101461,101472,101481};
double p;
int nentries;
TH1F *hist = new TH1F("hist","par3",100,0,60);
TH1F *hist_with_cut = new TH1F("hist_with_cut","par3>5",100,0,60);

for(int i=0; i<5; i++){
  sprintf(ttit,"hf_LightLeak%d.root",nums[i]);
  TFile *f = new TFile(ttit);
  TTree *t = (TTree*)f->Get("T");
  sprintf(btit,"run%d",nums[i]);
  TLeaf *par3 = t->GetLeaf(btit);
  nentries = t->GetEntries();

  for (int j=0; i<nentries; i++) {
    par3->GetBranch()->GetEntry(i);
    p= (double*)par3->GetValue();
    hist->Fill(p);
    if(p>5) hist_with_cut->Fill(p);
}
}
hist->Draw();
hist_with_cut->Draw();
}

it gives:
Error: illegal pointer to class object par3 0x0 1132 add_hists.cc:34:
*** Interpreter error recovered ***

line 34 is: par3->GetBranch()->GetEntry(i);

[quote]Error in TTree::SetBranchAddress: The pointer type given “Double_t” (8) does not correspond to the type needed “Int_t” (3) by the branch: run100718[/quote]This explicitly says that your leaf is NOT a TLeafD but rather a TLeafI. As indicated you must pass the address of an ‘int’ to SetBranchAddress.

[quote]Error: illegal pointer to class object par3 0x0 1132 add_hists.cc:34:
*** Interpreter error recovered ***[/quote]This indicates that you have not used the ‘right’ name for the leaf and thus you have not retrieve it from the TTree.

Cheers,
Philippe.