#include #include #include #include void Fitterfinal1() { TFile* file5 = new TFile("data.root"); TH1F* data = (TH1F*)file5->Get("data"); TH1F * Data1 = (TH1F*) data->Clone(); int nBins1 = data->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins1 << std::endl; TFile* file1 = new TFile("silver_background.root"); TH1F* h2 = (TH1F*)file1->Get("h2"); TH1F * BKG1 = (TH1F*) h2->Clone("BKG1"); int nBins2 = h2->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins2 << std::endl; TFile* file2 = new TFile("7He.root"); TH1F* mc00 = (TH1F*)file2->Get("mc00"); TH1F *mc000 = (TH1F*) mc00->Clone(); int nBins3 = mc00->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins3 << std::endl; TFile* file3 = new TFile("6He.root"); TH1F* mc01 = (TH1F*)file3->Get("mc01"); TH1F * mc001 = (TH1F*) mc01->Clone(); int nBins4 = mc01->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins4 << std::endl; TFile* file4 = new TFile("4He.root"); TH1F* mc02 = (TH1F*)file4->Get("mc02"); TH1F * mc002 = (TH1F*) mc02->Clone(); int nBins5 = mc02->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins5 << std::endl; //TFile* file6 = new TFile("5He.root"); //TH1F* mc03 = (TH1F*)file6->Get("mc03"); //TH1F * mc003 = (TH1F*) mc03->Clone(); //int nBins6 = mc03->GetNbinsX(); // get the number of bins in the x-axis //std::cout << "Number of bins: " << nBins6 << std::endl; TFile* file6 = new TFile("r1.root"); TH1F* h9 = (TH1F*)file6->Get("h9"); TH1F * mc003 = (TH1F*) h9->Clone(); int nBins6 = h9->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins6 << std::endl; TH1F* h3 = (TH1F*) Data1->Clone("h3"); TH1F* h4 = (TH1F*) BKG1->Clone("h4"); int nBins7 = h4->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins7 << std::endl; h4->Scale(2.31); TH1F *h5 = (TH1F*) h4->Clone("h5"); int nBins8 = h5->GetNbinsX(); // get the number of bins in the x-axis std::cout << "Number of bins: " << nBins8 << std::endl; h3->Add(h4,-1); //Draw the histograms and save them to a PDF file TCanvas *c = new TCanvas("c", "Data and MC Histograms",1600,1600); h3->Draw(""); h4->Draw("SAME"); h4->SetLineColor(kBlack); mc000->Draw("same"); mc000->SetLineColor(kRed); mc001->Draw("same"); mc001->SetLineColor(kGreen); mc002->Draw("same"); mc002->SetLineColor(kBlue); mc003->Draw("same"); mc003->SetLineColor(kOrange); //mc04->Draw("same"); //mc04->SetLineColor(kOrange); //mc05->Draw("same"); //mc05->SetLineColor(kPink); c->Print("data_and_mc_histograms.pdf"); // retrieve histograms TObjArray *mc = new TObjArray(4); // MC histograms are put in this array mc->Add(mc000); mc->Add(mc001); mc->Add(mc002); mc->Add(mc003); TFractionFitter* fit = new TFractionFitter(h3, mc); // initialise fit->Constrain(0,0,1.0); // constrain fraction 1 to be between 0 and 1 fit->Constrain(1,0,1.0); // constrain fraction 2 to be between 0 and 1 fit->Constrain(2,0,1.0); fit->Constrain(3,0,1.0); // constrain fraction 3 to be between 0 and 1 fit->SetRangeX(0,60); // use only the first 15 bins in the fit for mc0 // use only the first 15 bins in the fit for mc2 Int_t status = fit->Fit(); // perform the fit std::cout << "fit status: " << status << std::endl; if (status == 0) { // check on fit status TH1F* result = (TH1F*) fit->GetPlot(); h3->Draw("Ep"); result->Draw("same"); Double_t chi2 = fit->GetChisquare(); // Get the number of degrees of freedom Int_t ndf = fit->GetNDF(); // Print the result std::cout << "Chi-square = " << chi2 << std::endl; std::cout << "Number of degrees of freedom = " << ndf << std::endl; double chiSquareNDF = chi2 / ndf; std::cout << "Chi-square / ndf = " << chiSquareNDF << std::endl; } }