# 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:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sixie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7993a0/32.png) [@sixie](https://root-forum.cern.ch/u/sixie)\
**Post date:** [October 23, 2013, 10:38pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/1 "2013-10-23T22:38:40Z")

</div>

Hi,

I have a particular pdf shape in the form of a RooHistPdf which I get from a template histogram.  
I would like to fit just for a horizontal shift of this fixed shape - left and right. How can I define a RooHistPdf that allows a free parameter defining this possible shift?

I tried to create a RooHistPdf using a RooFormulaVar as the independent variable ( defined by eg. x - xoffset, where xoffset would be my free parameter to be floated in the fit), but this results in a Seg Fault.

Is it possible to do what I describe in RooFit?

Thanks.  
Si

---

<div class="post-metadata">

**Author:** ![Emil\_R](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emil_r/32/31255_2.png) [@Emil\_R](https://root-forum.cern.ch/u/Emil_R)\
**Post date:** [November 11, 2022, 1:27pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/2 "2022-11-11T13:27:12Z")

</div>

Are there any new on this topic? I do not even need to fit to the horizontal shift. It would be enough to simply shift the Shape manually (Even though a fit would be better).

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 11, 2022, 2:55pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/3 "2022-11-11T14:55:11Z")

</div>

Hello @Emil_R ,

this is a post from 2013 that must have fallen through the cracks. Your best bet is probably to create a new thread describing your specific problem from the start.

Cheers,  
Enrico

---

<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 11, 2022, 3:36pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/4 "2022-11-11T15:36:17Z")

</div>

**Edit: this answer recommends using the RooLinearVar, but it’s probably better to use the more tested RooFormulaVar as explained in my second post.**

Hi @Emil_R,

it’s still a good question though!

You can’t create a RooHistPdf for a non-fundamental variable like a `RooFormulaVar`, as attempted in the original post. But this is what the [RooLinearVar](https://root.cern.ch/doc/v622/classRooLinearVar.html) there for: it behaves like a fundamental variable, but is actually a linearly transformed fundamental variable.

You can therefore use it to implement your horizontal shift. Just create your RooDataHist for the template and the RooHistPdf for the _shifted_ variable, the RooLinearVar:

```c++
// Helper function to create the RooDataHist for the template shape
std::unique_ptr<RooDataHist> createDataHist()
{
    RooRealVar xShifted{"x_shifted", "x_shifted", 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", xShifted, mu, sigma};
    return std::unique_ptr<RooDataHist>{gauss.generateBinned(xShifted, 10000)};
}

void demo()
{
    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

    // Using RooLinearVar is the key here
    RooLinearVar xShifted{"x_shifted", "x_shifted", x, RooConst(1.0), shift};

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

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

```

The RooLinearVar is not so much used and tested though, so expect some rough edges. If you encounter some, please follow up here, or it it is a bug, head over to the [GitHub issues](https://github.com/root-project/root/issues) and open a bug report 🙂

I hope that helps!

Cheers,  
Jonas

---

<div class="post-metadata">

**Author:** ![RENATO\_QUAGLIANI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renato_quagliani/32/19496_2.png) [@RENATO\_QUAGLIANI](https://root-forum.cern.ch/u/RENATO_QUAGLIANI)\
**Post date:** [November 11, 2022, 8:40pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/5 "2022-11-11T20:40:58Z")

</div>

@jonas i Just saw this by accident. Why does one should prefer a RooLinearVar rather than a RooFormulaVar?  
In the past to obtain a shifted rookeypdf from an input dataset i used the rooformulaVar . Just curious to understand why one prefer here the LinearVar to a FormulaVar.

---

<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

---

<div class="post-metadata">

**Author:** ![Emil\_R](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emil_r/32/31255_2.png) [@Emil\_R](https://root-forum.cern.ch/u/Emil_R)\
**Post date:** [November 14, 2022, 2:00pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/8 "2022-11-14T14:00:05Z")

</div>

@Jonas Thanks for your help!  
I have been experimenting with the proposed way (which in general seems to be my solution). However it affects the line shape in a way I do not understand. As an example here are pictures of the original pdf (red) and the shifted pdf (blue).

 ![shiftexample](https://root-forum.cern.ch/uploads/default/original/3X/1/a/1afba10ea6e35bb365518111b1b5dcd1b9c055a5.png)  
 ![shiftexamplezoom](https://root-forum.cern.ch/uploads/default/original/3X/4/3/4340c56beaa7a7662af568031aadefe03c665168.png)

Does anyone know what has gone wrong?  
Here is my code, if that helps:

```c++
RooRealVar mass("Mass","Missing Mass",0,2000);
RooPlot *Massframe = mass.frame(Title(TString("Missing Mass")));
TFile *g=TFile::Open("../Data/KPlusSigmaStar.root");
TString* HistPost = new TString("FittedTrack_BeamEnergyVsMissingMassKaonPlusBE");
TH2* HistSigStar2 = (TH2*) g->FindObjectAny(*HistPost);
 
TH1* HistSigStar = (TH1*) HistSigStar2->ProjectionX(TString("SigStar_"),1,119)->Rebin(5,0);
RooRealVar shiftSigStar("shiftSigStar","Shift of SigStar",100.0,-1000,1000.0);
RooFormulaVar MassShifted("MassShift","Mass + shiftSigStar",RooArgList(mass,shiftSigStar));
RooDataHist SimSigStar("SimSigStar","Simulation of SigStar",mass,Import(*(HistSigStar)));
RooHistPdf* CurveSigStar = new RooHistPdf("CurveSigStar","Kurve SigStar",MassShifted,mass,SimSigStar);
RooHistPdf* CurveSigStarNorm = new RooHistPdf("CurveSigStar","Kurve SigStar",mass,SimSigStar);
CurveSigStar->plotOn(Massframe);
CurveSigStar->paramOn(Massframe);
CurveSigStarNorm->plotOn(Massframe, LineColor(kRed));
CurveSigStarNorm->paramOn(Massframe);
Massframe->Draw();

```

Thanks,  
Emil

---

<div class="post-metadata">

**Author:** ![Emil\_R](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emil_r/32/31255_2.png) [@Emil\_R](https://root-forum.cern.ch/u/Emil_R)\
**Post date:** [November 17, 2022, 4:23pm UTC](https://root-forum.cern.ch/t/how-to-implement-a-horizontal-shift-for-roohistpdf/16787/9 "2022-11-17T16:23:05Z")

</div>

@Jonas just to make sure I also tried your code, which seems fine for the generated data. But as soon as I use my data in your code, it gets slightly distorted. That does not seem to matter at first, but somehow the fitting is not possible with these distorted shapes.  
Thanks,  
Emil
