Skip to content

Commit

Permalink
[RF] Avoid separate RooXYChi2Var constructors for pdfs and functions
Browse files Browse the repository at this point in the history
It can be checked at runtime if a given `RooAbsReal` is a pdf or not.
Like this, we also don't need a separate override of
`createChi2(RooDataSet &)` in RooAbsPdf.
  • Loading branch information
guitargeek committed Sep 15, 2023
1 parent 69b3c77 commit bfc3d9c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 124 deletions.
6 changes: 0 additions & 6 deletions roofit/roofitcore/inc/RooAbsPdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,6 @@ class RooAbsPdf : public RooAbsReal {
return createNLL(data, *RooFit::Detail::createCmdList(&arg1, &args...));
}

// Chi^2 fits to histograms
using RooAbsReal::createChi2 ;

// Chi^2 fits to X-Y datasets
RooFit::OwningPtr<RooAbsReal> createChi2(RooDataSet& data, const RooLinkedList& cmdList) override ;

// Constraint management
virtual RooArgSet* getConstraints(const RooArgSet& /*observables*/, RooArgSet& /*constrainedParams*/,
bool /*stripDisconnected*/, bool /*removeConstraintsFromPdf*/=false) const
Expand Down
36 changes: 0 additions & 36 deletions roofit/roofitcore/src/RooAbsPdf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ called for each data event.
#include "RooFitResult.h"
#include "RooNumGenConfig.h"
#include "RooCachedReal.h"
#include "RooXYChi2Var.h"
#include "RooChi2Var.h"
#include "RooMinimizer.h"
#include "RooRealIntegral.h"
Expand Down Expand Up @@ -1796,41 +1795,6 @@ RooFit::OwningPtr<RooFitResult> RooAbsPdf::fitTo(RooAbsData& data, const RooLink
}


////////////////////////////////////////////////////////////////////////////////
/// Argument-list version of RooAbsPdf::createChi2()

RooFit::OwningPtr<RooAbsReal> RooAbsPdf::createChi2(RooDataSet& data, const RooLinkedList& cmdList)
{
// Select the pdf-specific commands
RooCmdConfig pc("RooAbsPdf::createChi2(" + std::string(GetName()) + ")");

pc.defineInt("integrate","Integrate",0,0) ;
pc.defineObject("yvar","YVar",0,0) ;

// Process and check varargs
pc.process(cmdList) ;
if (!pc.ok(true)) {
return nullptr;
}

// Decode command line arguments
bool integrate = pc.getInt("integrate") ;
RooRealVar* yvar = (RooRealVar*) pc.getObject("yvar") ;

std::string name = "chi2_" + std::string(GetName()) + "_" + std::string(data.GetName());

std::unique_ptr<RooAbsReal> out;
if (yvar) {
out = std::make_unique<RooXYChi2Var>(name.c_str(),name.c_str(),*this,data,*yvar,integrate) ;
} else {
out = std::make_unique<RooXYChi2Var>(name.c_str(),name.c_str(),*this,data,integrate) ;
}
return RooFit::Detail::owningPtr(std::move(out));
}




////////////////////////////////////////////////////////////////////////////////
/// Print value of p.d.f, also print normalization integral that was last used, if any

Expand Down
2 changes: 1 addition & 1 deletion roofit/roofitcore/src/RooAbsReal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4376,7 +4376,7 @@ RooFit::OwningPtr<RooAbsReal> RooAbsReal::createChi2(RooDataSet& data, const Roo
RooFit::OwningPtr<RooAbsReal> RooAbsReal::createChi2(RooDataSet& data, const RooLinkedList& cmdList)
{
// Select the pdf-specific commands
RooCmdConfig pc("RooAbsPdf::fitTo(" + std::string(GetName()) + ")");
RooCmdConfig pc("RooAbsReal::createChi2(" + std::string(GetName()) + ")");

pc.defineInt("integrate","Integrate",0,0) ;
pc.defineObject("yvar","YVar",0,nullptr) ;
Expand Down
126 changes: 45 additions & 81 deletions roofit/roofitcore/src/RooXYChi2Var.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ namespace {

////////////////////////////////////////////////////////////////////////////////
///
/// RooXYChi2Var constructor with function and X-Y values dataset
/// RooXYChi2Var constructor with function and X-Y values dataset
///
/// If the function is a pdf, it must be extendable. in this case, hhe value of
/// the function that defines the chi^2 in this form is takes as the p.d.f.
/// times the expected number of events
///
/// An X-Y dataset is a weighted dataset with one or more observables X where the weight is interpreted
/// as the Y value and the weight error is interpreted as the Y value error. The weight must have an
Expand All @@ -73,88 +77,41 @@ namespace {
/// are the double values that correspond to the Y and its error
///

RooXYChi2Var::RooXYChi2Var(const char *name, const char* title, RooAbsReal& func, RooDataSet& xydata, bool integrate) :
RooAbsOptTestStatistic(name,title,func,xydata,RooArgSet(),makeRooAbsTestStatisticCfg()),
_extended(false),
_integrate(integrate),
_intConfig(*defaultIntegratorConfig())
RooXYChi2Var::RooXYChi2Var(const char *name, const char *title, RooAbsReal &func, RooDataSet &xydata, bool integrate)
: RooAbsOptTestStatistic(name, title, func, xydata, RooArgSet(), makeRooAbsTestStatisticCfg()),
_integrate(integrate),
_intConfig(*defaultIntegratorConfig())
{
_extended = false ;
_yvar = nullptr ;

initialize() ;
}
bool isPdf = dynamic_cast<RooAbsPdf const *>(&func) != nullptr;

if (isPdf) {
auto &extPdf = static_cast<RooAbsPdf const &>(func);
if (!extPdf.canBeExtended()) {
throw std::runtime_error(
Form("RooXYChi2Var::RooXYChi2Var(%s) ERROR: Input p.d.f. must be extendible", GetName()));
}
}

////////////////////////////////////////////////////////////////////////////////
///
/// RooXYChi2Var constructor with function and X-Y values dataset
///
/// An X-Y dataset is a weighted dataset with one or more observables X where given yvar is interpreted
/// as the Y value. The Y variable must have a non-zero error defined at each point for the chi^2 calculation to be meaningful.
///
/// To store errors associated with the x and y values in a RooDataSet, call RooRealVar::setAttribute("StoreError")
/// on each X-type observable for which the error should be stored and add datapoints to the dataset as follows
///
/// RooDataSet::add(xset,yval,yerr) where xset is the RooArgSet of x observables (with or without errors) and yval and yerr
/// are the double values that correspond to the Y and its error
///

RooXYChi2Var::RooXYChi2Var(const char *name, const char* title, RooAbsReal& func, RooDataSet& xydata, RooRealVar& yvar, bool integrate) :
RooAbsOptTestStatistic(name,title,func,xydata,RooArgSet(),makeRooAbsTestStatisticCfg()),
_extended(false),
_integrate(integrate),
_intConfig(*defaultIntegratorConfig())
{
_extended = false ;
_yvar = (RooRealVar*) _dataClone->get()->find(yvar.GetName()) ;
_extended = isPdf;
_yvar = nullptr;

initialize() ;
initialize();
}


////////////////////////////////////////////////////////////////////////////////
///
/// RooXYChi2Var constructor with an extended p.d.f. and X-Y values dataset
/// The value of the function that defines the chi^2 in this form is takes as
/// the p.d.f. times the expected number of events
///
/// An X-Y dataset is a weighted dataset with one or more observables X where the weight is interpreted
/// as the Y value and the weight error is interpreted as the Y value error. The weight must have an
/// non-zero error defined at each point for the chi^2 calculation to be meaningful.
///
/// To store errors associated with the x and y values in a RooDataSet, call RooRealVar::setAttribute("StoreError")
/// on each X-type observable for which the error should be stored and add datapoints to the dataset as follows
///
/// RooDataSet::add(xset,yval,yerr) where xset is the RooArgSet of x observables (with or without errors) and yval and yerr
/// are the double values that correspond to the Y and its error
///

RooXYChi2Var::RooXYChi2Var(const char *name, const char* title, RooAbsPdf& extPdf, RooDataSet& xydata, bool integrate) :
RooAbsOptTestStatistic(name,title,extPdf,xydata,RooArgSet(),makeRooAbsTestStatisticCfg()),
_extended(true),
_integrate(integrate),
_intConfig(*defaultIntegratorConfig())
{
if (!extPdf.canBeExtended()) {
throw std::runtime_error(Form("RooXYChi2Var::RooXYChi2Var(%s) ERROR: Input p.d.f. must be extendible",GetName()));
}
_yvar = nullptr ;
initialize() ;
}




////////////////////////////////////////////////////////////////////////////////
///
/// RooXYChi2Var constructor with an extended p.d.f. and X-Y values dataset
/// The value of the function that defines the chi^2 in this form is takes as
/// the p.d.f. times the expected number of events
/// RooXYChi2Var constructor with function and X-Y values dataset.
///
/// An X-Y dataset is a weighted dataset with one or more observables X where the weight is interpreted
/// as the Y value and the weight error is interpreted as the Y value error. The weight must have an
/// non-zero error defined at each point for the chi^2 calculation to be meaningful.
/// If the function is a pdf, it must be extendable. in this case, hhe value of
/// the function that defines the chi^2 in this form is takes as the p.d.f.
/// times the expected number of events
///
/// An X-Y dataset is a weighted dataset with one or more observables X where given yvar is interpreted
/// as the Y value. The Y variable must have a non-zero error defined at each point for the chi^2 calculation to be meaningful.
///
/// To store errors associated with the x and y values in a RooDataSet, call RooRealVar::setAttribute("StoreError")
/// on each X-type observable for which the error should be stored and add datapoints to the dataset as follows
Expand All @@ -163,20 +120,27 @@ RooXYChi2Var::RooXYChi2Var(const char *name, const char* title, RooAbsPdf& extPd
/// are the double values that correspond to the Y and its error
///

RooXYChi2Var::RooXYChi2Var(const char *name, const char* title, RooAbsPdf& extPdf, RooDataSet& xydata, RooRealVar& yvar, bool integrate) :
RooAbsOptTestStatistic(name,title,extPdf,xydata,RooArgSet(),makeRooAbsTestStatisticCfg()),
_extended(true),
_integrate(integrate),
_intConfig(*defaultIntegratorConfig())
RooXYChi2Var::RooXYChi2Var(const char *name, const char *title, RooAbsReal &func, RooDataSet &xydata, RooRealVar &yvar,
bool integrate)
: RooAbsOptTestStatistic(name, title, func, xydata, RooArgSet(), makeRooAbsTestStatisticCfg()),
_integrate(integrate),
_intConfig(*defaultIntegratorConfig())
{
if (!extPdf.canBeExtended()) {
throw std::runtime_error(Form("RooXYChi2Var::ctor(%s) ERROR: Input p.d.f. must be an extendible",GetName()));
}
_yvar = (RooRealVar*) _dataClone->get()->find(yvar.GetName()) ;
initialize() ;
}
bool isPdf = dynamic_cast<RooAbsPdf const *>(&func) != nullptr;

if (isPdf) {
auto &extPdf = static_cast<RooAbsPdf const &>(func);
if (!extPdf.canBeExtended()) {
throw std::runtime_error(
Form("RooXYChi2Var::RooXYChi2Var(%s) ERROR: Input p.d.f. must be extendible", GetName()));
}
}

_extended = isPdf;
_yvar = static_cast<RooRealVar *>(_dataClone->get()->find(yvar.GetName()));

initialize();
}


////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit bfc3d9c

Please sign in to comment.