#include #include #include #include #include #include #include #include #include void Esercizio1() { //create empty histogram TH1D * h = new TH1D("histo", "", 40,0,40); //disable display of histogram statistics h->SetStats(false); //fill with Landau distribution for(double i = 0; i < 10000; i++) h->Fill(gRandom->Landau(20,5)); //fill with Gaus distribution for(double i = 0; i < 5000; i++) h->Fill(gRandom->Gaus(5,3)); //define fit functions TF1 * FitFunc1 = new TF1("FitFunc1","[0]*TMath::Gaus(x,[1],[2])",0,40); TF1 * FitFunc2 = new TF1("FitFunc2","[0]*TMath::Landau(x,[1],[2])",0,40); TF1 * FitFuncCombined = new TF1("FitFunc2","[0]*TMath::Gaus(x,[1],[2])+[3]*TMath::Landau(x,[4],[5])",0,40); //fit both peaks individually with reasonable initial parameters and fitting range FitFunc1->SetParameters(1,3,4); h->Fit(FitFunc1,"0","",0,10); FitFunc2->SetParameters(1,17,7); h->Fit(FitFunc2,"0","",10,40); //use fit parameters as initial parameters for combined fit FitFuncCombined->SetParameters(FitFunc1->GetParameter(0), FitFunc1->GetParameter(1), FitFunc1->GetParameter(2), FitFunc2->GetParameter(0), FitFunc2->GetParameter(1), FitFunc2->GetParameter(2)); h->Fit(FitFuncCombined,"0",""); //display what we did TCanvas * c = new TCanvas("c ref","c title", 200,10,600,600); c->SetLeftMargin(0.15); c->SetRightMargin(0.04); c->SetTopMargin(0.04); //Legend TLegend* Leg = new TLegend(0.3,0.8,0.99,0.99); Leg->SetFillColor(0); Leg->SetTextFont(62); h->SetLineWidth(2); h->SetLineColor(kBlue); h->GetXaxis()->SetTitle("x-axis title"); h->GetYaxis()->SetTitleOffset(1.4); h->GetYaxis()->SetTitle("entries"); h->Draw(); TString text; text = Form("N=%5.0f Mean=%5.1f RMS=%5.1f", h->GetEntries(), h->GetMean(), h->GetRMS()); Leg->AddEntry(h,text,"l"); FitFunc1->SetLineStyle(2); FitFunc1->SetLineColor(kRed); FitFunc1->Draw("same"); text = Form("Gaus: Mean=%5.1f#pm%5.1f, #sigma=%5.1f#pm%5.1f Landau: MOP=%5.1f#pm%5.1f, #sigma=%5.1f#pm%5.1f", FitFuncCombined->GetParameter(1), FitFuncCombined->GetParError(1), FitFuncCombined->GetParameter(2), FitFuncCombined->GetParError(2), FitFuncCombined->GetParameter(4), FitFuncCombined->GetParError(4), FitFuncCombined->GetParameter(5), FitFuncCombined->GetParError(5)); Leg->AddEntry(FitFuncCombined,text,"l"); FitFunc2->SetLineStyle(2); FitFunc2->SetLineColor(kRed); FitFunc2->Draw("same"); FitFuncCombined->SetLineColor(kBlack); FitFuncCombined->Draw("same"); Leg->Draw(); //Save canvas c->SaveAs("ex1.eps"); c->SaveAs("ex1.png"); c->SaveAs("ex1.root"); }