Hi,
I am a new programmer and trying to use RooFit for the first time to fit a landau curve on a histogram. I have used MakeClass to create a .h and a .C file from my original root file. Hence I am using the macro in the .C file for filling my histogram. Initially I chose an individual histogram and imported the data of the histogram using the code:
//importing the histogram data
RooRealVar x("x","x",-0.5,100.5);
TH1F *hh=(TH1F*)gDirectory->Get("set2"); //set2 is the histogram name
RooDataHist *dh=new RooDataHist("dh","data histogram",x,hh);
//creating a landau pdf
RooRealVar ml("mean","mean of landau",x1);
RooRealVar sl("sigma","sigma of landau",0.5,0.445,0.523);
RooLandau landau("landau","landau pdf",x,ml,sl);
RooPlot *frame1=x.frame(Title("adc1_vs_count_set2_1757"),Bins(101));
dh->plotOn(frame1);
landau.fitTo(*dh,Range(40.5,70.5));
landau.plotOn(frame1);
TCanvas *c_set2=new TCanvas(“c_set2”,“adc1_vs_count”,1000,1000);
c_set2->SetGrid();
frame1->SetXTitle(“count”);
frame1->SetYTitle(“adc1_raw”);
frame1->Draw();
This code worked nicely and the histogram was properly imported and the landau pdf was fitted nicely.
But now I want to fit 20 histograms with the landau pdf and would like to run a for loop for this.
I tried using the code as follows:
ostringstream var_set2,varml_set2,varsl_set2,varlan_set2 varframe_set2;
RooDataHist *dh[40]
{
TCanvas *c_set2=new TCanvas("c_set2","adc_count",1000,1000);
c_set2->Divide(5,4);
for(int ievt=1;ievt<21;ievt++)
{
RooRealVar x("x","x",-0.5,100.5);
vardh_set2<<"dh"<<ievt;
dh[ievt-1]=new RooDataHist(vardh_set2.str().c_str(),"data hist",x,h2b_set2[ievt-1]);//!!!h2b_set2[40] are the array of histograms declared before
vardh_set2.str("");
RooRealVar ml("ml","landau mean",53.0);
RooRealVar sl("sl","landau sigma",1.0);
RooLandau landau("landau","landau pdf",x,ml,sl);
varframe_set2<<"xframes"<<ievt;
RooPlot *xframe=x.frame(Name(varframe_set2.str().c_str()),Title("framework"),Bins(101));
varframe_set2.str("");
dh[ievt-1]->plotOn(xframe);//!!!!!!!!!!THIS IS WHERE MY CODE CRASHES DURING THE FIRST ENTRY!
landau.fitTo(*dh[ievt-1],Range(40.5,70.5));
landau.plotOn(frame1);
c_set2->cd(ievt);
xframe->Draw();
c_set2->Update();
}
When I tried running this the code crashed. I might have made serious mistakes in the code as I am very naive with Root. Basically I wanted to know how to import 20 histograms and create plot frames for each individual histograms in a “for loop” and fit them with the same landau pdf and draw them on different pads on a canvas using RooFit. Eventually I have to fit 90000 adc histograms. So I just tried to run a for loop for 20 histograms first. Please help.
timewalk2bset2.C (7.86 KB)