# How to implement a horizontal shift for RooHistPdf

**URL:** <https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787>\
**Category:** Roofit and RooStats\
**Created:** [October 23, 2013, 10:38pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787 "2013-10-23T22:38:40Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [November 12, 2022, 9:31am UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/6 "2022-11-12T09:31:31Z")

</div>

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](https://root.cern.ch/doc/master/classRooHistPdf.html#a99fbb46807505e28d99e161298a42de1) that allows you to specify PDF variables and RooDataHist variables separately, so you should use that one. The code would look somewhat like this:

```c++
// 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

---

_[View the full topic](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787)._
