Proper time , signal and background fitting

“I’ve created a macro that’s designed to fit both proper time and invariant mass. After plotting both parameters, I attempted to fit them using distinct functions. However, I’m encountering issues, either with the functions themselves or the initial parameters. Could someone assist me with this?”

#include <TStyle.h>
#include <TCanvas.h>
#include <TH1.h>
#include <TF1.h>
#include <TFile.h>
#include <TLegend.h>

Double_t background(Double_t *x, Double_t *par) {
    return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}

Double_t GAUSS(Double_t *x, Double_t *par){
    return par[0]*exp(-0.5*(((x[0]-par[1])/par[2])*((x[0]-par[1])/par[2])));
}

Double_t fitFunction(Double_t *x, Double_t *par) {
    return background(x,par) + GAUSS(x,&par[3]);
}

void fit_hist() {
    // Open the input file
    TFile *tf = new TFile("outrfile.root");

    // Get the histograms h_tp and h_m from the file
    TH1F *h_tpp = (TH1F*)tf->Get("h_tpp");
    TH1F *h_m = (TH1F*)tf->Get("h_m");

      // Create a new canvas to draw h_ctp
    TCanvas *c_tpp = new TCanvas("c_tpp", "Proper Time", 800, 600);
    c_tpp->SetLogy();

    // Draw the histogram 
    h_tpp->SetYTitle("Number of #Lambda candidates");
    h_tpp->SetXTitle("tp cm/c");
    h_tpp->Draw("E");

    TF1 *fit_tpp = new TF1("fit_tpp", "[0]*exp(-x/[1])", 0, 90);
    fit_tpp->SetParameters(0,1);
    h_tpp->Fit(fit_tpp,"", "", 30, 40 ); 

    float tau = fit_tpp->GetParameter(1);
    float tau_error = fit_tpp->GetParError(1);

    fit_tpp->SetLineColor(kRed);//set line color
    fit_tpp->Draw("same");// "same" option draws on the same canvas

    TLegend *legend_tpp = new TLegend(0.1, 0.7, 0.3, 0.9);
    legend_tpp->AddEntry(h_tpp, "Data", "l");
    legend_tpp->AddEntry(fit_tpp, "Exponential Fit", "l");
    legend_tpp->Draw();

    c_tpp->Update();

    double chi2 = fit_tpp->GetChisquare();
    int ndf = fit_tpp->GetNDF();
    double reducedChi2 = chi2 / ndf;
    std::cout << "Chi-square: " << chi2 << std::endl;
    std::cout << "Degrees of Freedom: " << ndf << std::endl;
    std::cout << "Reduced Chi-square: " << reducedChi2 << std::endl;
    std::cout << "Tau (proper time): " << tau << std::endl;
    std::cout << "Error on Tau: " << tau_error << std::endl;

    // Fit the h_m histogram using fitFunction
    TCanvas *c_m = new TCanvas("c_m", "Invariant Mass", 800, 600);
    h_m->SetYTitle("Number of #Lambda candidates");
    h_m->SetXTitle("m_{inv} (GeV/c^2)");
    h_m->Draw();

    TF1 *fitFcn = new TF1("fitFcn", fitFunction, 1.1, 1.315, 6); // Number of parameters
    fitFcn->SetParameters(1, 1, 1, 1, 1.2, 0.01);
    h_m->Fit("fitFcn", "V+", "ep");
    // "V+": The fitting option. V stands for verbose mode, showing the fit results,
    // and + means that the function will be added to the list of functions of the histogram     // (and it will be drawn with the histogram
    // "e" means to draw errors 
    // "p" histogram should be drawn as points 

    TF1 *backFcn = new TF1("backFcn", background, 1.1, 1.315, 3);
    backFcn->SetLineColor(kRed);
    TF1 *signalFcn = new TF1("signalFcn", GAUSS, 1.1, 1.315, 3);
    signalFcn->SetLineColor(kBlue);
    signalFcn->SetNpx(500); // SetNpx() sets the number of points used to draw the function.

    Double_t par[6]; // declares an array of Double_t
    // (which is a typedef for double in ROOT) named par with a size of 6. 
    //This array will be used to store the parameters of the fit function.

    fitFcn->GetParameters(par);//extract paramters from the fit 
    backFcn->SetParameters(par);// sets the parameters of backFcn
    backFcn->Draw("same");
    signalFcn->SetParameters(&par[3]); // I am  setting the parameters for the Gaussian function (signalFcn). 
    //The Gaussian parameters start from the fourth position in the par array (0-based index, so it starts from par[3]). 
    //Hence, &par[3] gives the address of the fourth element of the array, 
    //and it effectively passes the last three parameters to the Gaussian function.

    signalFcn->Draw("same");

    TLegend *legend_m = new TLegend(0.6,0.65,0.88,0.85);
    legend_m->SetTextFont(72);
    legend_m->SetTextSize(0.04);
    legend_m->AddEntry(h_m, "Data", "lpe");
    legend_m->AddEntry(backFcn, "Background fit", "l");
    legend_m->AddEntry(signalFcn, "Signal fit", "l");
    legend_m->AddEntry(fitFcn, "Global Fit", "l");
    legend_m->Draw();

    c_m->Update();

    // Write histograms
    TFile f("fit_hist.root", "recreate");
    h_tpp->Write();
    h_m->Write();

    f.Close(); 
}

May be @jonas can help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.