I have a RooDataHist data which I fit to a RooAddPdf signal+background distribution. Now I want to draw only my signal distribution, by subtracting the background (known from the fit) from my original data. In addition I do want to preserve the error bars size on the resulting histogram, so only a bin amplitude should change, but not its error bar.
And the last point: original histogram cannot have negative bin contents, but after a background subtraction negative values are allowed, and also error bars can go to negative region. My code (where I use RooDataHist::set()) does not do this.
Please, help!! What do I misss?
Here is my function:
RooDataHist *remove_data(RooRealVar &mass,RooDataHist &dh,const RooArgList &pdf,const RooArgList &frac)
{
assert(pdf.getSize()==frac.getSize());
RooArgSet margs(mass);
RooDataHist *dh_new = dynamic_cast<RooDataHist*>(dh.emptyClone("no_bkg"));
for(int i=0; i<dh.numEntries(); i++ )
{
const RooArgSet *args = dh.get(i);
assert(args->getSize()==1);
const RooAbsArg *x = args->first();
const RooAbsReal *xx = dynamic_cast<const RooAbsReal *>(x);
assert(xx!=NULL);
mass = xx->getVal();
double
weight = dh.weight(margs,1,true),
weight_error = dh.weightError(RooAbsData::SumW2);
//printf("bin=%2d mass=%g weight = %g +/- %g\n",i,xx->getVal(),weight,weight_error);
double v=0;
for( int j=0; j<pdf.getSize(); j++ )
{
const RooAbsPdf *f = dynamic_cast<const RooAbsPdf *>(&pdf [j]);
const RooRealVar *c = dynamic_cast<const RooRealVar*>(&frac[j]);
//printf("expected signal (%s): pdf=%g coef=%g\n",f->GetName(),f->getVal(args),c->getVal());
v += f->getVal(args) * c->getVal();
}
// printf(" %g : %g\n",weight,v);
double new_val = weight-v;
dh_new->set(*args,new_val,weight_error);
}
return dh_new;
}
c1.ps (35.6 KB)