41 ClassImp(RooHistFunc);
49 RooHistFunc::RooHistFunc() :
52 _cdfBoundaries(kFALSE),
67 RooHistFunc::RooHistFunc(
const char *name,
const char *title,
const RooArgSet& vars,
68 const RooDataHist& dhist, Int_t intOrder) :
69 RooAbsReal(name,title),
70 _depList(
"depList",
"List of dependents",this),
71 _dataHist((RooDataHist*)&dhist),
74 _cdfBoundaries(kFALSE),
78 _histObsList.addClone(vars) ;
82 const RooArgSet* dvars = dhist.get() ;
83 if (vars.getSize()!=dvars->getSize()) {
84 coutE(InputArguments) <<
"RooHistFunc::ctor(" << GetName()
85 <<
") ERROR variable list and RooDataHist must contain the same variables." << endl ;
89 for (
const auto arg : vars) {
90 if (!dvars->find(arg->GetName())) {
91 coutE(InputArguments) <<
"RooHistFunc::ctor(" << GetName()
92 <<
") ERROR variable list and RooDataHist must contain the same variables." << endl ;
109 RooHistFunc::RooHistFunc(
const char *name,
const char *title,
const RooArgList& funcObs,
const RooArgList& histObs,
110 const RooDataHist& dhist, Int_t intOrder) :
111 RooAbsReal(name,title),
112 _depList(
"depList",
"List of dependents",this),
113 _dataHist((RooDataHist*)&dhist),
116 _cdfBoundaries(kFALSE),
120 _histObsList.addClone(histObs) ;
121 _depList.add(funcObs) ;
124 const RooArgSet* dvars = dhist.get() ;
125 if (histObs.getSize()!=dvars->getSize()) {
126 coutE(InputArguments) <<
"RooHistFunc::ctor(" << GetName()
127 <<
") ERROR variable list and RooDataHist must contain the same variables." << endl ;
131 for (
const auto arg : histObs) {
132 if (!dvars->find(arg->GetName())) {
133 coutE(InputArguments) <<
"RooHistFunc::ctor(" << GetName()
134 <<
") ERROR variable list and RooDataHist must contain the same variables." << endl ;
147 RooHistFunc::RooHistFunc(
const RooHistFunc& other,
const char* name) :
148 RooAbsReal(other,name),
149 _depList(
"depList",this,other._depList),
150 _dataHist(other._dataHist),
151 _codeReg(other._codeReg),
152 _intOrder(other._intOrder),
153 _cdfBoundaries(other._cdfBoundaries),
154 _totVolume(other._totVolume),
155 _unitNorm(other._unitNorm)
159 _histObsList.addClone(other._histObsList) ;
166 RooHistFunc::~RooHistFunc()
179 Double_t RooHistFunc::evaluate()
const
182 if (_depList.getSize()>0) {
183 for (
auto i = 0u; i < _histObsList.size(); ++i) {
184 const auto harg = _histObsList[i];
185 const auto parg = _depList[i];
189 harg->copyCache(parg,kTRUE) ;
190 if (!harg->inRange(0)) {
197 Double_t ret = _dataHist->weight(_histObsList,_intOrder,kFALSE,_cdfBoundaries) ;
204 Int_t RooHistFunc::getMaxVal(
const RooArgSet& vars)
const
206 RooAbsCollection* common = _depList.selectCommon(vars) ;
207 if (common->getSize()==_depList.getSize()) {
217 Double_t RooHistFunc::maxVal(Int_t code)
const
222 for (Int_t i=0 ; i<_dataHist->numEntries() ; i++) {
224 Double_t wgt = _dataHist->weight() ;
225 if (wgt>max) max=wgt ;
234 Double_t RooHistFunc::totVolume()
const
241 for (
const auto arg : _depList) {
242 RooRealVar* real =
dynamic_cast<RooRealVar*
>(arg) ;
244 _totVolume *= (real->getMax()-real->getMin()) ;
246 RooCategory* cat =
dynamic_cast<RooCategory*
>(arg) ;
248 _totVolume *= cat->numTypes() ;
265 Int_t RooHistFunc::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars,
const char* rangeName)
const
274 RooAbsCollection *allVarsCommon = allVars.selectCommon(_depList) ;
275 Bool_t intAllObs = (allVarsCommon->getSize()==_depList.getSize()) ;
276 delete allVarsCommon ;
277 if (intAllObs && matchArgs(allVars,analVars,_depList)) {
287 RooArgSet* allVarsSel = (RooArgSet*) allVars.selectCommon(_depList) ;
288 if (allVarsSel->getSize()==0) {
296 for (
const auto arg : _depList) {
297 if (allVars.find(arg->GetName())) code |= (1<<n) ;
301 analVars.add(*allVarsSel) ;
314 Double_t RooHistFunc::analyticalIntegral(Int_t code,
const char* )
const
320 return _dataHist->sum(kTRUE) ;
326 for (
const auto arg : _depList) {
333 if (_depList.getSize()>0) {
334 for (
auto i = 0u; i < _histObsList.size(); ++i) {
335 const auto harg = _histObsList[i];
336 const auto parg = _depList[i];
340 harg->copyCache(parg,kTRUE) ;
341 if (!harg->inRange(0)) {
348 Double_t ret = _dataHist->sum(intSet,_histObsList,kTRUE) ;
359 list<Double_t>* RooHistFunc::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi)
const
369 for (
auto i = 0u; i < _histObsList.size(); ++i) {
370 const auto harg = _histObsList[i];
371 const auto parg = _depList[i];
372 if (
string(parg->GetName())==obs.GetName()) {
381 RooAbsLValue* lvarg =
dynamic_cast<RooAbsLValue*
>(_dataHist->get()->find(hobs->GetName())) ;
387 const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
388 Double_t* boundaries = binning->array() ;
390 list<Double_t>* hint =
new list<Double_t> ;
393 xlo = xlo - 0.01*(xhi-xlo) ;
394 xhi = xhi + 0.01*(xhi-xlo) ;
396 Double_t delta = (xhi-xlo)*1e-8 ;
400 for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
401 if (boundaries[i]>=xlo && boundaries[i]<=xhi) {
402 hint->push_back(boundaries[i]-delta) ;
403 hint->push_back(boundaries[i]+delta) ;
416 std::list<Double_t>* RooHistFunc::binBoundaries(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi)
const
425 for (
auto i = 0u; i < _histObsList.size(); ++i) {
426 const auto harg = _histObsList[i];
427 const auto parg = _depList[i];
428 if (
string(parg->GetName())==obs.GetName()) {
436 RooAbsRealLValue* transform(0) ;
441 for (
auto i = 0u; i < _histObsList.size(); ++i) {
442 const auto harg = _histObsList[i];
443 const auto parg = _depList[i];
444 if (
string(harg->GetName())==obs.GetName()) {
451 if (!hobs || !(pobs->dependsOn(obs) &&
dynamic_cast<RooAbsRealLValue*
>(pobs))) {
452 cout <<
"RooHistFunc::binBoundaries(" << GetName() <<
") obs = " << obs.GetName() <<
" hobs is not found, returning null" << endl ;
458 transform =
dynamic_cast<RooAbsRealLValue*
>(pobs) ;
466 RooAbsArg* xtmp = _dataHist->get()->find(hobs->GetName()) ;
468 cout <<
"RooHistFunc::binBoundaries(" << GetName() <<
") hobs = " << hobs->GetName() <<
" is not found in dataset?" << endl ;
469 _dataHist->get()->Print(
"v") ;
472 RooAbsLValue* lvarg =
dynamic_cast<RooAbsLValue*
>(_dataHist->get()->find(hobs->GetName())) ;
474 cout <<
"RooHistFunc::binBoundaries(" << GetName() <<
") hobs = " << hobs->GetName() <<
" but is not an LV, returning null" << endl ;
479 const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
480 Double_t* boundaries = binning->array() ;
482 list<Double_t>* hint =
new list<Double_t> ;
484 Double_t delta = (xhi-xlo)*1e-8 ;
488 for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
489 if (boundaries[i]>xlo-delta && boundaries[i]<xhi+delta) {
491 Double_t boundary = boundaries[i] ;
493 transform->setVal(boundary) ;
495 hint->push_back(obs.getVal()) ;
497 hint->push_back(boundary) ;
510 Bool_t RooHistFunc::importWorkspaceHook(RooWorkspace& ws)
512 std::list<RooAbsData*> allData = ws.allEmbeddedData() ;
513 std::list<RooAbsData*>::const_iterator iter ;
514 for (iter = allData.begin() ; iter != allData.end() ; ++iter) {
516 if (*iter == _dataHist) {
522 RooAbsData* wsdata = ws.embeddedData(_dataHist->GetName()) ;
527 if (wsdata->InheritsFrom(RooDataHist::Class())) {
530 if (areIdentical((RooDataHist&)*wsdata,*_dataHist)) {
533 _dataHist = (RooDataHist*) wsdata ;
537 TString uniqueName = Form(
"%s_%s",_dataHist->GetName(),GetName()) ;
538 Bool_t flag = ws.import(*_dataHist,RooFit::Rename(uniqueName.Data()),RooFit::Embedded()) ;
540 coutE(ObjectHandling) <<
" RooHistPdf::importWorkspaceHook(" << GetName() <<
") unable to import clone of underlying RooDataHist with unique name " << uniqueName <<
", abort" << endl ;
543 _dataHist = (RooDataHist*) ws.embeddedData(uniqueName.Data()) ;
549 TString uniqueName = Form(
"%s_%s",_dataHist->GetName(),GetName()) ;
550 Bool_t flag = ws.import(*_dataHist,RooFit::Rename(uniqueName.Data()),RooFit::Embedded()) ;
552 coutE(ObjectHandling) <<
" RooHistPdf::importWorkspaceHook(" << GetName() <<
") unable to import clone of underlying RooDataHist with unique name " << uniqueName <<
", abort" << endl ;
555 _dataHist = (RooDataHist*) ws.embeddedData(uniqueName.Data()) ;
562 ws.import(*_dataHist,RooFit::Embedded()) ;
565 _dataHist = (RooDataHist*) ws.embeddedData(_dataHist->GetName()) ;
572 Bool_t RooHistFunc::areIdentical(
const RooDataHist& dh1,
const RooDataHist& dh2)
574 if (fabs(dh1.sumEntries()-dh2.sumEntries())>1e-8)
return kFALSE ;
575 if (dh1.numEntries() != dh2.numEntries())
return kFALSE ;
576 for (
int i=0 ; i < dh1.numEntries() ; i++) {
579 if (fabs(dh1.weight()-dh2.weight())>1e-8)
return kFALSE ;
581 if (!(RooNameSet(*dh1.get())==RooNameSet(*dh2.get())))
return kFALSE ;
590 void RooHistFunc::Streamer(TBuffer &R__b)
592 if (R__b.IsReading()) {
593 R__b.ReadClassBuffer(RooHistFunc::Class(),
this);
596 registerProxy(_depList) ;
598 R__b.WriteClassBuffer(RooHistFunc::Class(),
this);
608 void RooHistFunc::ioStreamerPass2()
610 if (_histObsList.getSize()==0) {
611 _histObsList.addClone(_depList) ;