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();
}