How to implement a horizontal shift for RooHistPdf

Hi @RENATO_QUAGLIANI,

thanks for your post! Indeed it would be better to use a RooFormulaVar, because the RooLinearVar is not battle-tested so much. I just thought that it’s not possible to use non-fundamental variables in the specific case of the RooHistPdf.

But I remember now that this is not true! There is a RooHistPdf constructor that allows you to specify PDF variables and RooDataHist variables separately, so you should use that one. The code would look somewhat like this:

// Helper function to create the RooDataHist for the template shape
std::unique_ptr<RooDataHist> createDataHist()
{
    RooRealVar x{"x", "x", 0.0, -10, 10};
    RooRealVar mu{"mu", "mu", 0.0, -10, 10};
    RooRealVar sigma{"sigma", "sigma", 2.0, 0.1, 10};
    RooGaussian gauss{"gauss", "gauss", x, mu, sigma};
    return std::unique_ptr<RooDataHist>{gauss.generateBinned(x, 10000)};
}

void demo2()
{
    using namespace RooFit;

    // Observable "x"
    RooRealVar x{"x", "x", 0.0, -10, 10};

    // The shift will be a fit parameter
    RooRealVar shift{"shift", "shift", 5.0, -10, 10}; // shift by 5 to the left

    // The shifted variable is represetned by a RooFormulaVar
    RooFormulaVar xShifted{"x_shifted", "x + shift", {x, shift}};

    // Create the template RooHistPdf
    std::unique_ptr<RooDataHist> dataHist = createDataHist();
    RooHistPdf histPdf{"histPdf", "histPdf", xShifted, x, *dataHist};

    // Just to check that it works
    auto xframe = x.frame();
    histPdf.plotOn(xframe);
    xframe->Draw();
}

Cheers,
Jonas

1 Like