Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions roofit/roofit/inc/RooParametricStepFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#ifndef ROO_PARAMETRIC_STEP_FUNCTION
#define ROO_PARAMETRIC_STEP_FUNCTION

#include "TArrayD.h"
#include "RooAbsPdf.h"
#include "RooRealProxy.h"
#include "RooListProxy.h"
#include <RooRealProxy.h>
#include <RooListProxy.h>
#include <RooAbsPdf.h>

#include <TArrayD.h>

class RooRealVar;
class RooArgList ;

class RooParametricStepFunction : public RooAbsPdf {
Expand All @@ -29,7 +29,7 @@ class RooParametricStepFunction : public RooAbsPdf {
RooParametricStepFunction() {}

RooParametricStepFunction(const char *name, const char *title,
RooAbsReal& x, const RooArgList& coefList, TArrayD& limits, Int_t nBins=1) ;
RooAbsReal& x, const RooArgList& coefList, TArrayD const& limits, Int_t nBins=1) ;

RooParametricStepFunction(const RooParametricStepFunction& other, const char* name = nullptr);
TObject* clone(const char* newname) const override { return new RooParametricStepFunction(*this, newname); }
Expand All @@ -39,6 +39,8 @@ class RooParametricStepFunction : public RooAbsPdf {
Int_t getnBins() const { return _nBins; }
double* getLimits() { return _limits.GetArray(); }

std::list<double>* plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const override ;

protected:

double lastBinValue() const ;
Expand Down
14 changes: 7 additions & 7 deletions roofit/roofit/inc/RooStepFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
#ifndef ROO_STEP_FUNCTION
#define ROO_STEP_FUNCTION

#include "TArrayD.h"
#include "RooAbsReal.h"
#include "RooRealProxy.h"
#include "RooListProxy.h"
#include <RooAbsReal.h>
#include <RooListProxy.h>
#include <RooRealProxy.h>

class RooRealVar;
class RooArgList ;

class RooStepFunction : public RooAbsReal {
public:

RooStepFunction() ;
RooStepFunction() {}
RooStepFunction(const char *name, const char *title,
RooAbsReal& x, const RooArgList& coefList, const RooArgList& limits, bool interpolate=false) ;

Expand All @@ -38,6 +36,8 @@ class RooStepFunction : public RooAbsReal {
const RooArgList& coefficients() { return _coefList; }
const RooArgList& boundaries() { return _boundaryList; }

std::list<double>* plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const override ;

protected:

double evaluate() const override;
Expand All @@ -47,7 +47,7 @@ class RooStepFunction : public RooAbsReal {
RooRealProxy _x;
RooListProxy _coefList ;
RooListProxy _boundaryList ;
bool _interpolate ;
bool _interpolate = false;

ClassDefOverride(RooStepFunction,1) // Step Function
};
Expand Down
54 changes: 27 additions & 27 deletions roofit/roofit/src/RooParametricStepFunction.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,18 @@ xframe->Draw();
~~~
*/

#include "Riostream.h"
#include "TArrayD.h"
#include <cmath>
#include <RooParametricStepFunction.h>

#include "RooParametricStepFunction.h"
#include "RooRealVar.h"
#include "RooArgList.h"

#include "TError.h"
#include <RooCurve.h>
#include <RooRealVar.h>

ClassImp(RooParametricStepFunction);

////////////////////////////////////////////////////////////////////////////////
/// Constructor

RooParametricStepFunction::RooParametricStepFunction(const char* name, const char* title,
RooAbsReal& x, const RooArgList& coefList, TArrayD& limits, Int_t nBins) :
RooAbsReal& x, const RooArgList& coefList, TArrayD const& limits, Int_t nBins) :
RooAbsPdf(name, title),
_x("x", "Dependent", this, x),
_coefList("coefList","List of coefficients",this),
Expand All @@ -110,9 +105,11 @@ RooParametricStepFunction::RooParametricStepFunction(const char* name, const cha

for (auto *coef : coefList) {
if (!dynamic_cast<RooAbsReal*>(coef)) {
std::cout << "RooParametricStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
<< " is not of type RooAbsReal" << std::endl ;
R__ASSERT(0) ;
std::stringstream errorMsg;
errorMsg << "RooParametricStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
<< " is not of type RooAbsReal";
coutE(InputArguments) << errorMsg.str() << std::endl;
throw std::invalid_argument(errorMsg.str().c_str());
}
_coefList.add(*coef) ;
}
Expand Down Expand Up @@ -144,10 +141,8 @@ Int_t RooParametricStepFunction::getAnalyticalIntegral(RooArgSet& allVars, RooAr

////////////////////////////////////////////////////////////////////////////////

double RooParametricStepFunction::analyticalIntegral(Int_t code, const char* rangeName) const
double RooParametricStepFunction::analyticalIntegral(Int_t /*code*/, const char* rangeName) const
{
R__ASSERT(code==1) ;

// Case without range is trivial: p.d.f is by construction normalized
if (!rangeName) {
return 1.0 ;
Expand Down Expand Up @@ -210,20 +205,10 @@ double RooParametricStepFunction::evaluate() const
// in Bin i-1 (starting with Bin 0)
if (i<_nBins) {
// not in last Bin
RooRealVar* tmp = (RooRealVar*) _coefList.at(i-1);
value = tmp->getVal();
value = static_cast<RooRealVar*>(_coefList.at(i-1))->getVal();
break;
} else {
// in last Bin
double sum(0.);
double binSize(0.);
for (Int_t j=1;j<_nBins;j++){
RooRealVar* tmp = (RooRealVar*) _coefList.at(j-1);
binSize = _limits[j] - _limits[j-1];
sum = sum + tmp->getVal()*binSize;
}
binSize = _limits[_nBins] - _limits[_nBins-1];
value = (1.0 - sum)/binSize;
value = lastBinValue();
if (value<=0.0){
value = 0.000001;
// cout << "RooParametricStepFunction: sum of values gt 1.0 -- beware!!" <<endl;
Expand All @@ -237,3 +222,18 @@ double RooParametricStepFunction::evaluate() const
return value;

}


std::list<double>* RooParametricStepFunction::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
{
if(obs.namePtr() != _x->namePtr()) {
return nullptr;
}

// Retrieve position of all bin boundaries
std::span<const double> boundaries{_limits.GetArray(), static_cast<std::size_t>(_limits.GetSize())};

// Use the helper function from RooCurve to make sure to get sampling hints
// that work with the RooFitPlotting.
return RooCurve::plotSamplingHintForBinBoundaries(boundaries, xlo, xhi);
}
65 changes: 36 additions & 29 deletions roofit/roofit/src/RooStepFunction.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,19 @@ Note that in contrast to RooParametricStepFunction, a RooStepFunction is NOT a P
but a not-normalized function (RooAbsReal)
**/

#include "Riostream.h"
#include "TArrayD.h"
#include <cmath>
#include <RooStepFunction.h>

#include "RooStepFunction.h"
#include "RooRealVar.h"
#include "RooArgList.h"
#include "RooMsgService.h"
#include "RooMath.h"

using namespace std;
#include <RooArgList.h>
#include <RooCurve.h>
#include <RooMsgService.h>
#include <RooMath.h>
#include <RooRealVar.h>

ClassImp(RooStepFunction);

////////////////////////////////////////////////////////////////////////////////
/// Constructor

RooStepFunction::RooStepFunction()
{
_interpolate = false ;
}

////////////////////////////////////////////////////////////////////////////////
/// Constructor

RooStepFunction::RooStepFunction(const char* name, const char* title,
RooAbsReal& x, const RooArgList& coefList, const RooArgList& boundaryList, bool interpolate) :
RooAbsReal(name, title),
Expand All @@ -63,25 +51,25 @@ RooStepFunction::RooStepFunction(const char* name, const char* title,
{
for (auto *coef : coefList) {
if (!dynamic_cast<RooAbsReal*>(coef)) {
cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
<< " is not of type RooAbsReal" << endl ;
std::cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
<< " is not of type RooAbsReal" << std::endl ;
assert(0) ;
}
_coefList.add(*coef) ;
}

for (auto *boundary : boundaryList) {
if (!dynamic_cast<RooAbsReal*>(boundary)) {
cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: boundary " << boundary->GetName()
<< " is not of type RooAbsReal" << endl ;
std::cout << "RooStepFunction::ctor(" << GetName() << ") ERROR: boundary " << boundary->GetName()
<< " is not of type RooAbsReal" << std::endl;
assert(0) ;
}
_boundaryList.add(*boundary) ;
}

if (_boundaryList.getSize()!=_coefList.getSize()+1) {
coutE(InputArguments) << "RooStepFunction::ctor(" << GetName() << ") ERROR: Number of boundaries must be number of coefficients plus 1" << endl ;
throw string("RooStepFunction::ctor() ERROR: Number of boundaries must be number of coefficients plus 1") ;
if (_boundaryList.size()!=_coefList.size()+1) {
coutE(InputArguments) << "RooStepFunction::ctor(" << GetName() << ") ERROR: Number of boundaries must be number of coefficients plus 1" << std::endl ;
throw std::invalid_argument("RooStepFunction::ctor() ERROR: Number of boundaries must be number of coefficients plus 1") ;
}

}
Expand All @@ -100,12 +88,12 @@ RooStepFunction::RooStepFunction(const RooStepFunction& other, const char* name)


////////////////////////////////////////////////////////////////////////////////
/// Transfer contents to vector for use below
/// Transfer contents to std::vector for use below

double RooStepFunction::evaluate() const
{
vector<double> b(_boundaryList.getSize()) ;
vector<double> c(_coefList.getSize()+3) ;
std::vector<double> b(_boundaryList.size()) ;
std::vector<double> c(_coefList.size()+3) ;
Int_t nb(0) ;
for (auto * boundary : static_range_cast<RooAbsReal*>(_boundaryList)) {
b[nb++] = boundary->getVal() ;
Expand Down Expand Up @@ -136,7 +124,7 @@ double RooStepFunction::evaluate() const

// Make array of (0,coefficient values,0)
Int_t nc(0) ;
vector<double> y(_coefList.size()+3) ;
std::vector<double> y(_coefList.size()+3) ;
y[nc++] = 0 ;
for(auto * coef : static_range_cast<RooAbsReal*>(_coefList)) {
y[nc++] = coef->getVal() ;
Expand All @@ -153,3 +141,22 @@ double RooStepFunction::evaluate() const
return 0;
}
}


std::list<double> *RooStepFunction::plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const
{
if (obs.namePtr() != _x->namePtr()) {
return nullptr;
}

// Retrieve position of all bin boundaries
std::vector<double> boundaries;
boundaries.reserve(_boundaryList.size());
for (auto *boundary : static_range_cast<RooAbsReal *>(_boundaryList)) {
boundaries.push_back(boundary->getVal());
}

// Use the helper function from RooCurve to make sure to get sampling hints
// that work with the RooFitPlotting.
return RooCurve::plotSamplingHintForBinBoundaries(boundaries, xlo, xhi);
}
21 changes: 17 additions & 4 deletions roofit/roofitcore/inc/RooCurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
#ifndef ROO_CURVE
#define ROO_CURVE

#include "TGraph.h"
#include "RooPlotable.h"
#include <RooPlotable.h>

#include <ROOT/RSpan.hxx>

#include <TGraph.h>
#include <TMatrixDfwd.h>

#include <list>
#include <vector>
#include "TMatrixDfwd.h"

class RooAbsReal;
class RooRealVar;
Expand Down Expand Up @@ -71,6 +75,8 @@ class RooCurve : public TGraph, public RooPlotable {
RooCurve* makeErrorBand(const std::vector<RooCurve*>& variations, double Z=1) const ;
RooCurve* makeErrorBand(const std::vector<RooCurve*>& plusVar, const std::vector<RooCurve*>& minusVar, const TMatrixD& V, double Z=1) const ;

static std::list<double>* plotSamplingHintForBinBoundaries(std::span<const double> boundaries, double xlo, double xhi);

protected:

void calcBandInterval(const std::vector<RooCurve*>& variations,Int_t i,double Z,double& lo, double& hi, bool approxGauss) const ;
Expand All @@ -83,13 +89,20 @@ class RooCurve : public TGraph, public RooPlotable {
Int_t numee=0, bool doEEVal=false, double eeVal=0.0,std::list<double>* samplingHint=nullptr) ;
void addRange(const RooAbsFunc& func, double x1, double x2, double y1,
double y2, double minDy, double minDx,
Int_t numee=0, bool doEEVal=false, double eeVal=0.0);
int numee, bool doEEVal, double eeVal, double epsilon);


void shiftCurveToZero();

bool _showProgress = false; ///<! Show progress indication when adding points

private:
/// The distance between two points x1 and x2 relative to the full plot range
/// below which two points are considered identical. No further sampling
/// points will be added to the curve when the distance between two sampling
/// points falls below this threshold.
static constexpr double relativeXEpsilon() { return 1e-9; }

ClassDefOverride(RooCurve,1) // 1-dimensional smooth curve for use in RooPlots
};

Expand Down
Loading