Modified tree1.C crashes

I modified the example tree1.C to read in a root file. I receive a segmentation violation. Why is that?
By the way, it works when I run only over 10 entries: nentries = 10.

Here is the program:

#include “TFile.h”
#include “TTree.h”
#include “TBrowser.h”
#include “TH2.h”
#include “TRandom.h”

// This example is a variant of hsimple.C but using a TTree instead
// of a TNtuple. It shows :
// -how to fill a Tree with a few simple variables.
// -how to read this Tree
// -how to browse and analyze the Tree via the TBrowser and TTreeViewer
// This example can be run in many different ways:
// way1: .x tree1.C using the CINT interpreter
// way2: .x tree1.C++ using the automatic compiler interface
// way3: .L tree1.C or .L tree1.C++
// tree1()
// One can also run the write and read parts in two separate sessions.
// For example following one of the sessions above, one can start the session:
// .L tree1.C
// tree1r();
//
// Author: Rene Brun

void tree1r()
{
//read the Tree generated by tree1w and fill two histograms

//note that we use “new” to create the TFile and TTree objects !
//because we want to keep these objects alive when we leave this function.

TFile *myF = new TFile(“myRootFiles/SP-1593-Run1-1-50k.root”,“read”);
TTree ntp1 = (TTree)myF->Get(“ntp1”);
Float_t BpreFitMes;
Int_t ev;
ntp1->SetBranchAddress(“BpreFitMes”,&BpreFitMes);

//create two histograms
TH1F *hMes = new TH1F(“hMes”,“Mes distribution”,100,4.5,5.5);

//read all entries and fill the histograms
Int_t nentries = (Int_t)ntp1->GetEntries();
cout << "Number of entries are : " << nentries << endl;
for (Int_t i=0;i<nentries;i++) {
if (i%100==0) cout<<“Processing event “<<i<<”…”<<endl;
ntp1->GetEntry(i);
hMes->Fill(BpreFitMes);
}

//we do not close the file. We want to keep the generated histograms
//we open a browser and the TreeViewer
if (gROOT->IsBatch()) return;
new TBrowser();
ntp1->StartViewer();
// in the browser, click on “ROOT Files”, then on “tree1.root”.
// you can click on the histogram icons in the right panel to draw them.
// in the TreeViewer, follow the instructions in the Help button.
}

void myTree1_a() {
tree1r();
}

Hi nielsvb,
I’d need to know of what type the branch BpreFitMes is (ntp1->GetBranch(“BpreFitMes”)->Print() will do). Assuming that it contains a single leaf TLeafF, SetBranchAdress expects the address of a pointer to a Float_t, whereas you pass the address of a Float_t. You could also use ntp1->GetLeaf(“BpreFitMes”)->GetValue().
Axel.

[quote=“Axel”]Hi nielsvb,
I’d need to know of what type the branch BpreFitMes is (ntp1->GetBranch(“BpreFitMes”)->Print() will do). Assuming that it contains a single leaf TLeafF, SetBranchAdress expects the address of a pointer to a Float_t, whereas you pass the address of a Float_t. You could also use ntp1->GetLeaf(“BpreFitMes”)->GetValue().
Axel.[/quote]

Hi Axel,

This is what I get:

root [1] myTree1_a()
*Br 0 :BpreFitMes : BpreFitMes[nB]/F *
*Entries : 3847 : Total Size= 51077 bytes File Size = 20829 *
*Baskets : 6 : Basket Size= 8000 bytes Compression= 1.65 *

Number of entries are : 3847
P

So, probably the problem is that I have an array, or not?

Niels

Hi Niels,

[quote=“nielsvb”]So, probably the problem is that I have an array, or not?[/quote] That’s right. You can simply run ntp1->MakeProxy to generate code that allows you to analyze your tree, without having to deal with branch addresses etc.
Axel.

[quote=“Axel”]Hi Niels,

[quote=“nielsvb”]So, probably the problem is that I have an array, or not?[/quote] That’s right. You can simply run ntp1->MakeProxy to generate code that allows you to analyze your tree, without having to deal with branch addresses etc.
Axel.[/quote]

Can you elaborate a bit more how to use MakeProxy?

If I do:

ntp1->MakeProxy(“proxyTest”,“myTree1_a.C”);

I get:

Error: Can’t call TTree::MakeProxy(“proxyTest”,“myRootMacros/myTree1_a.C”) in current scope FILE:myRootMacros/myTree1_a.C LINE:38
Possible candidates are…
filename line:size busy function type and name (in TTree)
filename line:size busy function type and name (in TNamed)
filename line:size busy function type and name (in TObject)
filename line:size busy function type and name (in TAttLine)
filename line:size busy function type and name (in TAttFill)
filename line:size busy function type and name (in TAttMarker)
*** Interpreter error recovered ***

Hi,

You need to use ROOT v4.00/08 or higher.

Cheers,
Philippe.

[quote=“pcanal”]Hi,

You need to use ROOT v4.00/08 or higher.

Cheers,
Philippe.[/quote]

I have this version running now. I produce a .h file.
But now I need some help how to make use of the class? See my code in the original posting.

Now I include in my macro: ntp1->MakeProxy(“proxyTest”,“myRootMacros/myTree1_a.C”);

Do I finally #include proxyTest.h in my macro and skip the MakeProxy?

The following code works now for all the entries. Is it OK now or do one of you recommend some changes?

#include “TFile.h”
#include “TTree.h”
#include “TBrowser.h”
#include “TH2.h”
#include “TRandom.h”

void tree1r()
{
//read the Tree generated by tree1w and fill two histograms

//note that we use “new” to create the TFile and TTree objects !
//because we want to keep these objects alive when we leave this function.

TFile *myF = new TFile(“myRootFiles/SP-1593-Run1-1-50k.root”,“read”);
TTree ntp1 = (TTree)myF->Get(“ntp1”);

ntp1->MakeProxy(“proxyTest”,“myRootMacros/myTree1_a.C”);

ntp1->GetBranch(“BpreFitMes”)->Print();

// //create two histograms
TH1F *hMes = new TH1F(“hMes”,“Mes distribution”,100,4.5,5.5);

// //read all entries and fill the histograms
Int_t nentries = (Int_t)ntp1->GetEntries();
Float_t myMes;
cout << "Number of entries are : " << nentries << endl;
for (Int_t i=0;i<nentries;i++) {
ntp1->GetEntry(i);
myMes = ntp1->GetLeaf(“BpreFitMes”)->GetValue();
hMes->Fill(myMes);
if (i%100==0) cout<<"Processing event “<<i<<”…myMes = "<<myMes <<endl;
}

//we do not close the file. We want to keep the generated histograms
//we open a browser and the TreeViewer
if (gROOT->IsBatch()) return;
new TBrowser();
ntp1->StartViewer();
// in the browser, click on “ROOT Files”, then on “tree1.root”.
// you can click on the histogram icons in the right panel to draw them.
// in the TreeViewer, follow the instructions in the Help button.
}

void myTree1_a() {
tree1r();
}

Niels

You need a file myTree1_a.h containing:

#include "TH1.h" #include "Riostream.h"and a file myTree1_a.C containing:

TH1F *hMes; void myTree1_a_Begin() { cout << "Number of entries are : " << fChain->GetEntries() << endl; hMes = new TH1F("hMes","Mes distribution",100,4.5,5.5); } void myTree1_a(Long64_t entry) { if (entry%100==0) cout<<"Processing event "<<i<<"...myMes = "<<BpreFitMes[0]<<endl; UInt_t nb = nB; Float_t myMes; for(int i=0;i<nb;++I) hMes->Fill(BpreFitMes[i]); } void myTree1_a_Terminate() { hMes->Draw(); } and then your function tree1r can be:

[code]void tree1r()
{
//read the Tree generated by tree1w and fill two histograms

//note that we use “new” to create the TFile and TTree objects !
//because we want to keep these objects alive when we leave this function.

TFile *myF = new TFile(“myRootFiles/SP-1593-Run1-1-50k.root”,“read”);
TTree ntp1 = (TTree)myF->Get(“ntp1”);

ntp1->MakeProxy(“proxyTest”,“myRootMacros/myTree1_a.C”);

gROOT->ProcessLine(".L proxyTest.h+");
proxyTest p;
ntp1->Process(&p);
// or just ntp1->Process(“proxyTest.h+”);

… etc …
}[/code]

Cheers,
Philippe