While fitting a histogram

i am getting this type of error

In file included from fit.cc:4:
/home/gursharan/root_software/root-6.28.06-install/include/TF1.h: In instantiation of ‘static void ROOT::Internal::TF1Builder<Func*>::Build(TF1*, Func*) [with Func = float(float, float*)]’:
/home/gursharan/root_software/root-6.28.06-install/include/TF1.h:381:46:   required from ‘TF1::TF1(const char*, Func, Double_t, Double_t, Int_t, Int_t, TF1::EAddToList) [with Func = float (*)(float, float*); Double_t = double; Int_t = int]’
fit.cc:55:63:   required from here
/home/gursharan/root_software/root-6.28.06-install/include/TF1.h:728:103: error: ‘operator()’ is not a member of ‘float(float, float*)’
  728 | rnal::GetFunctorType<decltype(ROOT::Internal::GetTheRightOp(&Func::operator()))>::type;
      |                                                             ^~~~~~~~~~~~~~~~~

/home/gursharan/root_software/root-6.28.06-install/include/TF1.h:728:103: error: ‘operator()’ is not a member of ‘float(float, float*)’
/home/gursharan/root_software/root-6.28.06-install/include/TF1.h:728:103: error: ‘operator()’ is not a member of ‘float(float, float*)’

and here is my code

Float_t fx(Float_t x, Float_t *par){
    Float_t alpha = par[1];
    Float_t n = par[2];
    Float_t delta_m = par[3];
    Float_t sigma_cb = par[4];

    Float_t arg = (x - delta_m) / sigma_cb;

    Float_t mode_alpha = TMath::Abs(alpha); // Mode of alpha is the absolute value of alpha

    Float_t B = n / mode_alpha - mode_alpha;
    Float_t A = TMath::Power((n / mode_alpha), n) * TMath::Exp(-mode_alpha * mode_alpha /2);

    // Define your function based on conditions
    Float_t result;
    if (arg > -mode_alpha) {
        result = TMath::Gaus(x, delta_m, sigma_cb);
    } else {
        result = A * TMath::Power((B - arg), -n);
    }

    return result;
}

void customFit(const char* file_name, const char* hist_name) {
    // Open the ROOT file
    TFile *root_file = new TFile(file_name);

    // Get the histogram from the ROOT file
    TH1F *histogram = (TH1F*)root_file->Get(hist_name);

    // Define a TF1 object with your custom function
    TF1 *custom_func = new TF1("custom_func", fx, 70., 110., 4);  // Assuming the function takes 6 parameters and operates between x = 60 to x = 120

    // Set parameter names (optional but can be helpful for clarity)
  custom_func->SetParNames("swd", "wdw", "tgt", "rffe");  // Replace with your parameter names
  // custom_func->SetParameters(2,3,histogram->GetMean(),histogram->GetRMS());
    //custom_func->SetParNames("Amplitude", "Mean", "Sigma");

    // Fit the histogram using your custom function
    histogram->Fit(custom_func, "R");  // Perform the fit

    // Create a canvas to display the histogram and the fit
    TCanvas *canvas = new TCanvas("canvas", "Histogram with Custom Fit");
    histogram->Draw();
    custom_func->Draw("same");

    // Save the canvas as an image file if needed
    canvas->SaveAs("custom_fit.png");

    // Close the ROOT file
    root_file->Close();
}

Hi @Gursharan_singh,

thank you for your question. The problem turns out to be rather trivial. The solution is:

TF1 *custom_func = new TF1("custom_func", "fx", 70., 110., 4);

you just forgot the “” for fx.

Cheers,
Marta

2 Likes

oh , thanks

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