How to access the value of branch variables?

Hello,

I’m a beginner.I create a file pixels.root and a tree with a pixel branch and totE branch. In my programming, I use a loop ( for (int i=0; i< 36; i++) {pixel =i)};I obtain a histogram corresponding to pixels.
My problem: I would like to retrieve the tenth pixel with its energy associated (and so forth) and to create a energy histogram per pixel. Or simply how to access the value of branch variables?

TFile *myfile= new TFile(“pixels.root”,“READ”);
Ttree tree= (Ttree myfile)->Get(“tree”);
tree->SetBranchAddress(“totE”,&totE);
tree->SetBranchAddress(“pixel”,&pixel);

entries= tree->GetEntries();
for (int i=0; i<entries; i++)
{
h->Fill(pixel);
h1->Fil(totE);
}

Thanks in advance

1 Like

Hi,

I think for each loop iteration you have to make GetEntry(i):

TFile *myfile= new TFile("pixels.root","READ");
Ttree *tree= (Ttree* myfile)->Get("tree");
tree->SetBranchAddress("totE",&totE);
tree->SetBranchAddress("pixel",&pixel);

entries= tree->GetEntries();
for (int i=0; i<entries; i++)
{
tree->GetEntry(i);//            <-----     !!!!!!!!!!!!!
h->Fill(pixel);
h1->Fil(totE);
}

If it does not work could you please post here your pixels.root?

Hi,

I have a problem:
Error in TTree::SetBranchAddress: The pointer type given “Int_t” (3) does not correspond to the type needed “UInt_t” (13) by the branch: pixel

#include
#include
#include
#include <TH1F.h>
#include <TROOT.h>
#include <TRandom.h>
#include <TTree.h>
#include <TFile.h>

void readTree(){

gROOT->SetStyle(“Plain”);

double totE;
int pixel;

TCanvas *c1= new TCanvas (“c1”,“c1”,800,800);
gPad->Divide(0,2);

TH1F *h= new TH1F(“h”,“h”,1000,0,40);
TH1F *h1= new TH1F(“h1”,“h1”,1000,0,600);

TFile *myfile= new TFile(“test4pixels.root”,“READ”);
TTree tree= (TTree)myfile->Get(“tree”);
tree->SetBranchAddress(“pixel”,&pixel);
tree->SetBranchAddress(“totE”,&totE);

entries= tree->GetEntries();

for (int i=0; i<entries; i++)
{
tree->GetEntry(i);
h->Fill(pixel);
h1->Fill(totE);
}

}test4pixels.root (498 KB)

I found my error:
TTree tree= (TTree)myfile->Get(“Tree”);
but now, I have another problem:

Error: illegal pointer to class object tree 0x0 952 C:27:

This works fine for me:

[code]{

//gROOT->SetStyle(“Plain”);

double totE;
unsigned int pixel;// <---- !!!

TCanvas *c1= new TCanvas (“c1”,“c1”,800,800);
gPad->Divide(0,2);

TH1F *h= new TH1F(“h”,“h”,1000,0,40);
TH1F *h1= new TH1F(“h1”,“h1”,1000,0,600);

TFile *myfile= new TFile(“test4pixels.root”,“READ”);
TTree tree= (TTree)myfile->Get(“tree”);
tree->SetBranchAddress(“pixel”,&pixel);
tree->SetBranchAddress(“totE”,&totE);

entries= tree->GetEntries();

for (int i=0; i<entries; i++)
{
tree->GetEntry(i);
h->Fill(pixel);
h1->Fill(totE);
}
h->Draw();

}
[/code]

I always have this error

That’s very weird. Anyway here is the last version of the code, now even without any warnings. I deleted all unnecessary stuff that could lead to your error. It works as well. I run it - I get histo.

[code] {

double totE;
unsigned int pixel;//  <----   !!!!

TCanvas *c1= new TCanvas ("c1","c1",800,800);
gPad->Divide(0,2);

TH1F *h= new TH1F("h","h",1000,0,40);
TH1F *h1= new TH1F("h1","h1",1000,0,600);

TFile *myfile= TFile::Open("test4pixels.root");
TTree *tree= (TTree*)myfile->Get("tree");
tree->SetBranchAddress("pixel",&pixel);
tree->SetBranchAddress("totE",&totE);

for (int i=0; i<tree->GetEntries(); i++)
{
tree->GetEntry(i);
h->Fill(pixel);
h1->Fill(totE);
}
h->Draw();

}

[/code]

If it still does not work for you, it’s quite hard for me to tell why - this works perfect for me :smiley:

oki thanks,
but I would like to draw for example the pixel n°5 with its energy.

oki I found

Hi,

I have another problem. When I draw the pixel n°36 I have an error:
Error: illegal pointer to class object histoTab[36] 0x0 863 readTree.C:72:

When I open the file .root, in my pixel branch I have 36 pixels.
I don’t understand why I have this error. Can you help me?
readTree.C (2.42 KB)

It’s because you do not have histoTab[36]. You had

for(Int_t j=0 ; j<36 ; j++)
{ 
histoTab[j]=(TH1D*)histo->Clone(TString(Form("histo%d" ,j+1))); 
}

it means histoTab[0], histoTab[1], histoTab[35] and that’s all. What you need to do is to take histoTab[35] instead of histoTab[36]

yep, thanks

Re

I have another question: I would like to select one particle and draw its energy by using my file .root, it’s possible?

Yeap, you simply make

Where instead of N you write a digit - your particle’s No. in the whole tree

yep

For example, I have one particle which arrives in my first pixel. It deposits 80% of his energy (histo 1) and dies in my second pixel where it deposits 20% of his energy (histo 2). I should have a correlation between histo 1 and histo 2. How to proceed?

Hi,

Were you able to accomplish what you needed? If not could you be specific on what you are missing?

Thanks,
Philippe.

Hi there,
Can you do the same with chain? Imagine you have 2 root files in a chain with same tree name? To see what the branch content, is that your way to create a histogram to store (fill) them to visualize them? You can follow my question under my name.

Thanks.