RooFit variables not recognised


Please read tips for efficient and successful posting and posting code

ROOT Version: root-6.38.00
Platform: FreeBSD
Compiler: FreeBSD clang version 19.1.7


Hello,

This code:

#include “TH1.h”
#include “TMath.h”
#include “TF1.h”
#include “TLegend.h”
#include “TCanvas.h”

using namespace RooFit;

void region_1() {

std::vector<std::vector > values;
std::ifstream fin(“Region_1.csv”);
for (std::string line; std::getline(fin, line); )
{
std::replace(line.begin(), line.end(), ‘,’, ’ ');
std::istringstream in(line);
values.push_back(
std::vector(std::istream_iterator(in),
std::istream_iterator()));
}

double xlow = values.at(0).at(0);
double xhigh = values.at(values.size()-1).at(0);

std::cout << "Read " << values.size() << " data points" << " x low: " << xlow << " xhigh: " << xhigh << "\\n";
TH1F\* hist = new TH1F("hist", "Region_1", values.size(), xlow, xhigh );
for (int i=1; i < values.size(); ++i) // setting bin contents to values
  {
hist->Fill(values.at(i).at(0), values.at(i).at(1)); // uncertainties are of course screwed up
  }
// hist->Draw("AP");

// Import into RooDataHist
RooRealVar x("x","x",xlow,xhigh);
RooDataHist data("data","dataset with x",x,hist);
RooPlot\* frame = x.frame() ;
data.plotOn(frame) ;
frame->Draw();

// Lorentzian
// A/pi ((G/2)/((x-P)^2 + (G/2)^2))
RooRealVar A("amplitude","Amplitude of Lorentzian", 0.5, 0,100);
RooRealVar P("peak", "Peak of Lorentzian", -77,-80, -70);
RooRealVar G("fwhm", "Width of lorentzian",0.1,0,2);
RooGenericPdf lorentzian{"lorentzian", "A \* ((G/2)/((x-P)^2 + (G/2)^2))", RooArgList(A,P,G)};

}

when invoked in cling results in this error:

root [0] .x region_1.C
Read 971 data points x low: -96.4057 xhigh: 96.2172
Fontconfig warning: using without calling FcInit()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
input_line_37:2:73: error: use of undeclared identifier 'A'
Double_t TFormula____id11031697281559057077(Double_t const *x){ return {A}*(({G}/2)/(TMath::Sq((x[0]-{P}))+TMath::Sq(({G}/2)))) ; }
                                                                        ^
input_line_38:2:73: error: use of undeclared identifier 'A'
Double_t TFormula____id11031697281559057077(Double_t const *x){ return {A}*(({G}/2)/(TMath::Sq((x[0]-{P}))+TMath::Sq(({G}/2)))) ; }
                                                                        ^
Error in <prepareMethod>: Can't compile function TFormula____id11031697281559057077 prototype with arguments Double_t const*
Error in <TFormula::InputFormulaIntoCling>: Error compiling formula expression in Cling
Error in <TFormula::ProcessFormula>: "A" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: "G" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: "P" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: "G" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: Formula "A*((G/2)/(TMath::Sq((x-P))+TMath::Sq((G/2))))" is invalid !
[#0] FATAL:InputArguments -- RooFormula 'lorentzian' did not compile or is invalid.
Input:
        A * ((G/2)/((x-P)^2 + (G/2)^2))
Passed over to TFormula:
        A * ((G/2)/((x-P)^2 + (G/2)^2))
Error in <TRint::HandleTermInput()>: std::runtime_error caught: RooFormula 'lorentzian' did not compile or is invalid.
Input:
        A * ((G/2)/((x-P)^2 + (G/2)^2))
Passed over to TFormula:
        A * ((G/2)/((x-P)^2 + (G/2)^2))

Can someone point out what I’m doing wrong?

Thanks,

Sprock

Welcome to the ROOT Forum!
RooFit expert @jonas can help you with this question.

Aaron

Hi!

The mismatch is between the RooFit names of your variables and the names used in the formula string.

In RooFit, what matters in a RooGenericPdf formula is the first argument to the RooRealVar constructor (the RooFit name), not the C++ variable name. You named them:

  • “amplitude” (C++ var A)
  • “peak” (C++ var P)
  • “fwhm” (C++ var G)

but in the formula you wrote A, P, G, which RooFit/TFormula doesn’t know about. That’s why TFormula leaves them as literal {A}, {G}, {P} placeholders and then fails to compile.

Three ways to fix it:

  1. Use the RooFit names in the formula:

    RooGenericPdf lorentzian{"lorentzian",
         "amplitude * ((fwhm/2)/((x-peak)^2 + (fwhm/2)^2))",
         RooArgList(A, P, G, x)}; // you forgot to add x as input by the way
    
  2. Rename the RooRealVars so their RooFit names match:

    RooRealVar A("A", "Amplitude of Lorentzian", 0.5, 0, 100);
    RooRealVar P("P", "Peak of Lorentzian", -77, -80, -70);
    RooRealVar G("G", "Width of Lorentzian", 0.1, 0, 2);
    
  3. Use positional placeholders (@0, @1, @2 in the order of the RooArgList):

    RooGenericPdf lorentzian{"lorentzian",
        "@0 * ((@2/2)/((x-@1)^2 + (@2/2)^2))",
        RooArgList(A, P, G, x)};
    

Note that you must include x in the list too (and reference it as e.g. @3), or keep x referenced by name and the other parameters by @.

Option 1 or 2 is what most people do.

I hope that helps!
Jonas

Thank you Jonas, that was it.

I fear there may be more questions as I proceed with this work

sprock