# Subtract a ROOchebychev function from a histogram in ROOFIT

Hi,

In root we can subtract a function from a histogram by using:

``````h->Add(func, -1)
``````

I was wondering if we can do the same in Roofit ? So basically I would like to subtract “bkg” from the histogram “h”.

you can find my code down below.

``````RooRealVar x("x","MM_{(#pi #pi)} GeV/c^{2}",3.04,3.17) ;
RooDataHist dh("dh","dataset",x,h);
RooPlot* frame = x.frame(Title("")) ;
dh.plotOn(frame,Name("dh")) ;
RooRealVar mean1("#mu","mean of gaussians",3.095,3.099);
RooRealVar sigma1("#sigma","width of gaussians",0.0025,0.006);

RooGaussian sig1("sig1", "sig1", x, mean1, sigma1);

RooRealVar a0("a0","a0",-2.,1.) ;

RooChebychev bkg("bkg","background p.d.f.",x,RooArgList(a0));

RooRealVar nsig("N_{SIG}","signal events",0,1000000);
RooRealVar nbkg("N_{BKG}","signal background even0ts",0,100000000);

RooExtendPdf esig("esig","esig",sig1,nsig) ;

RooExtendPdf ebkg("ebkg","ebkg",bkg,nbkg) ;

RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;        //sig +bcg

RooFitResult* r = all.fitTo(dh,Extended(kTRUE),Save()) ;

all.paramOn(frame,Layout(0.5,0.90,0.55));
all.plotOn(frame,Name("all"));
all.plotOn(frame,Components(bkg),LineStyle(kDashed));

r->Print();
frame->Draw();
``````

Hi,

we discussed this here:

but it’s a bit long to read.

The gist is:
You can create a standard ROOT histogram from a PDF:
RooAbsReal::createHistogram

The options for the generation of the histogram are also listed in the documentation I linked above.
When you have the TH1x, subtraction of the histograms should be easy.

Note:

• You have to match the binning of the histograms, so something like `Binning(200, 0., 500.)` is what you should use when you ask for a histogram.
• You likely have to adjust the normalisation. Without doing anything, RooFit will always normalise the PDF to unity. If you need custom scaling, you can either scale the histogram manually `hxx->Add(hyy, -1. * nbkg.getVal())` , or you can have it scaled when creating the histogram. Have a look at the documentation again:
RooAbsPdf::createHistogram docs
`Extended()` should enable automatic scaling (hopefully to the value of nbkg).
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.