How one can add the freedom to a RooKeysPDF to move/shift in x-axis direction?

Dear experts!
I would like to add the option to a background model using a RooKeysPDF made from a histogram to shift in x-axis direction. I do need this for shifting a MC data distribution to improve the calibration wrt to the data. The shape of the background PDF is OK …

Thanks in advance and cheers, Arno


If I have understood you well, I think you would need to pass as input to the RooKeysPdf not the RooRealVar representing x, but a RooAbsReal function describing x+shift.


Hi Lorentzo!

Yep, I think so too. Unfortunately, I do not know how. :blush:

Cheers, Arno

Hi Arno,

I am facing the same problem at the moment - did you figure out how to do this in the end?


Hi Emmy!

This question I had long time ago and I solved my problem in another way. Nowadays, I understand Lorenzo’s answer in the following way:

If you’d map an x to a whatever f(x) e.g. one has computed a RooKeysPDF. Then one could compute a shift by later using the same obtained f(x) but when asking for a value for e.g. a concrete x_0 with a shift, e.g. s_0 for the whole f(x) one can calculate f(x_0) using x_0 = x - s_0.


Cheers, Arno

Hello to both of you,

If I understood it correctly, we are talking about something like this:

RooRealVar x("x", "x", 0, -100, 100);
RooRealVar shift("shift", "Shift for x", 0, -100, 100);
RooAddition xPrime("xPrime", "Shifted x", RooArgSet(x, shift));

And now, when you construct the KeysPdf, don’t give it x, give it xPrime as its variable.

Hi all,

thank you for your replies. I was trying a similar thing using RooFormulaVar instead of RooAddition. I believe these should be the same, but I tried implementing this using RooAddition as well.

However, I keep running into either a segfault (using RooFormulaVar), or a

teminate called after throwing an instance of 'std::bad_alloc'

when using RooAddition. It looks like some memory allocation is going wrong, and I am not sure why.
Here are the relevant lines in my code:

RooRealVar x("x", "x", 0, 300);
RooRealVar shiftval("shiftval", "shiftval", 0, -10, 10);
RooAddition shift("shift", "shift", RooArgSet(x, shiftval));
//RooFormulaVar shift("shift",  "x+shiftval", RooArgSet(x, shiftval));

RooKeysPdf feed("feed", "feed", shift, data, RooKeysPdf::NoMirror);

The break occurs when I try to create the RooKeysPdf.

std::bad_alloc might mean that you are running out of memory. Try to reduce the size of the dataset by a factor 100, and see if it works.

And yes, the two should do the same, only that the FormulaVar gets interpreted, while the Addition is compiled. Therefore, the addition should be a bit faster.

Hi Stephan,

The dataset I am fitting is ~100mb and the dataset I am converting to a RooKeysPDF I have reduced to ~50kb and I still get the same error. I am wondering whether constructing the RooKeysPDF in this way is too memory intensive even with very small samples…

edit –
I managed to solve this issue using a different approach:

  • Create a TH1 from the RooDataSet
  • Apply smoothing
  • Create a RooDataHist from the TH1
  • Create a RooHistPdf which can be parameterised using the approach given here: Fit an offset for RooHistPdf

Thanks for all the help!

– second edit since I am not allowed to reply any more:
I have opened a bug report, as suggested by Stephan:

Hi Emmy,

that’s great. However, the memory error doesn’t seem to be that you are running out of memory if the dataset is only 50k.
If you have the time, would you open a bug report, and attach the dataset and the macro to run it so I can try to reproduce the error?

1 Like


This is supported in RooFit and there is no actual bug. You just called the wrong constructor of the RooKeysPdf.

Here is what you did:

  RooDataSet data_R1("data_R1","dataset with x", RooArgSet(Delta_Mcorr), Import(*tree_R1));
  RooKeysPdf R1_feed("R1_feed","R1_feed",R1_shift,data_R1,RooKeysPdf::NoMirror) ;

But if the x variable of the RooKeysPdf is not the same as the one in data, you should use the constructor that you tell which variable from the data you’re going to map to the RooKeysPdf.

  RooDataSet data_R1("data_R1","dataset with x", RooArgSet(Delta_Mcorr), Import(*tree_R1));
  RooKeysPdf R1_feed("R1_feed","R1_feed",R1_shift,Delta_Mcorr,data_R1,RooKeysPdf::NoMirror) ;

This should do the job and I’ll close the Jira issue.