63namespace Experimental {
67std::string mathFunc(std::string
const &
name)
69 return "RooFit::Detail::MathFuncs::" +
name;
72void rooHistTranslateImpl(
RooAbsArg const &arg, CodegenContext &ctx,
int intOrder,
RooDataHist const &dataHist,
73 const RooArgSet &obs,
bool correctForBinSize,
bool cdfBoundaries)
75 if (intOrder != 0 && !(!cdfBoundaries && !correctForBinSize && intOrder == 1 && obs.
size() == 1)) {
77 <<
") ERROR: codegen currently only supports non-interpolation cases."
85 ctx.addResult(&arg, ctx.buildCall(mathFunc(
"interpolate1d"), binning.
lowBound(), binning.
highBound(), *obs[0],
91 ctx.addResult(&arg,
"*(" + weightArr +
" + " +
offset +
")");
94std::string realSumPdfTranslateImpl(CodegenContext &ctx,
RooAbsArg const &arg,
RooArgList const &funcList,
97 bool noLastCoeff = funcList.
size() != coefList.
size();
99 std::string
const &funcName = ctx.buildArg(funcList);
100 std::string
const &coeffName = ctx.buildArg(coefList);
101 std::string
const &coeffSize = std::to_string(coefList.
size());
103 std::string
sum = ctx.getTmpVarName();
104 std::string coeffSum = ctx.getTmpVarName();
105 ctx.addToCodeBody(&arg,
"double " +
sum +
" = 0;\ndouble " + coeffSum +
"= 0;\n");
107 std::string iterator =
"i_" + ctx.getTmpVarName();
108 std::string subscriptExpr =
"[" + iterator +
"]";
110 std::string code =
"for(int " + iterator +
" = 0; " + iterator +
" < " + coeffSize +
"; " + iterator +
"++) {\n" +
111 sum +
" += " + funcName + subscriptExpr +
" * " + coeffName + subscriptExpr +
";\n";
112 code += coeffSum +
" += " + coeffName + subscriptExpr +
";\n";
116 code +=
sum +
" += " + funcName +
"[" + coeffSize +
"]" +
" * (1 - " + coeffSum +
");\n";
117 }
else if (normalize) {
118 code +=
sum +
" /= " + coeffSum +
";\n";
120 ctx.addToCodeBody(&arg, code);
141 ctx.
addResult(&arg, paramNames +
"[" + idx +
"]");
148 std::size_t
n = interpCodes.size();
151 for (std::size_t i = 0; i <
n; ++i) {
152 if (interpCodes[i] != interpCodes[0]) {
154 <<
"FlexibleInterpVar::evaluate ERROR: Code Squashing AD does not yet support having "
155 "different interpolation codes for the same class object "
171 std::vector<double> valsNominal;
172 std::vector<double> valsLow;
173 std::vector<double> valsHigh;
174 for (
int i = 0; i < nBins; ++i) {
175 valsNominal.push_back(nomHist.
weight(i));
177 for (
int i = 0; i < nBins; ++i) {
178 for (std::size_t iParam = 0; iParam <
n; ++iParam) {
184 std::string valsNominalStr = ctx.
buildArg(valsNominal);
185 std::string valsLowStr = ctx.
buildArg(valsLow);
186 std::string valsHighStr = ctx.
buildArg(valsHigh);
187 std::string nStr = std::to_string(
n);
193 code +=
"unsigned int " + idxName +
" = " +
197 code +=
"double const* " + lowName +
" = " + valsLowStr +
" + " + nStr +
" * " + idxName +
";\n";
198 code +=
"double const* " + highName +
" = " + valsHighStr +
" + " + nStr +
" * " + idxName +
";\n";
199 code +=
"double " + nominalName +
" = *(" + valsNominalStr +
" + " + idxName +
");\n";
201 std::string funcCall = ctx.
buildCall(mathFunc(
"flexibleInterp"), interpCodes[0], arg.
paramList(),
n, lowName,
202 highName, 1.0, nominalName, 0.0);
203 code +=
"double " + resName +
" = " + funcCall +
";\n";
206 code += resName +
" = " + resName +
" < 0 ? 0 : " + resName +
";\n";
224 std::stringstream errorMsg;
225 errorMsg <<
"Translate function for class \"" << arg.
ClassName() <<
"\" has not yet been implemented.";
238 std::span<const double> covISpan{covI.
GetMatrixArray(),
static_cast<size_t>(covI.GetNoElements())};
253 for (
auto *component : static_range_cast<RooAbsReal *>(arg.
list())) {
255 if (!
dynamic_cast<RooFit::Detail::RooNLLVarNew *
>(component) || arg.
list().
size() == 1) {
307 constexpr auto max_precision{std::numeric_limits<double>::digits10 + 1};
308 std::stringstream ss;
309 ss.precision(max_precision);
402 if (arg.binnedL() && !arg.pdf().getAttribute(
"BinnedLikelihoodActiveYields")) {
403 std::stringstream errorMsg;
404 errorMsg <<
"codegen: binned likelihood optimization is only supported when raw pdf "
405 "values can be interpreted as yields."
406 <<
" This is not the case for HistFactory models written with ROOT versions before 6.26.00";
408 throw std::runtime_error(errorMsg.str());
417 const bool needWeightSum = arg.expectedEvents() || arg.simCount() > 1;
423 if (arg.simCount() > 1) {
424 std::string simCountStr = std::to_string(
static_cast<double>(arg.simCount()));
425 ctx.
addToCodeBody(resName +
" += " + weightSumName +
" * std::log(" + simCountStr +
");\n");
433 std::string term = ctx.
buildCall(mathFunc(
"nll"), arg.pdf(), arg.weightVar(), arg.binnedL(), 0);
436 if (arg.expectedEvents()) {
437 std::string expected = ctx.
getResult(*arg.expectedEvents());
438 ctx.
addToCodeBody(resName +
" += " + expected +
" - " + weightSumName +
" * std::log(" + expected +
");\n");
452 std::string
result = arrName +
"[" + idx +
"]";
458 result +=
" * *(" + weightArr +
" + " + idx +
") * " + std::to_string(binV);
467 xName =
"std::floor(" + xName +
")";
506std::string codegenIntegral(
RooAbsReal &arg,
int code,
const char *rangeName, CodegenContext &ctx)
508 using Func = std::string (*)(
RooAbsReal &,
int,
const char *, CodegenContext &);
515 static std::unordered_map<TClass *, Func> dispatchMap;
517 auto found = dispatchMap.find(tclass);
519 if (found != dispatchMap.end()) {
520 func = found->second;
523 std::stringstream cmd;
524 cmd <<
"&RooFit::Experimental::CodegenIntegralImplCaller<" << tclass->
GetName() <<
">::call;";
525 func =
reinterpret_cast<Func
>(
gInterpreter->ProcessLine(cmd.str().c_str()));
526 dispatchMap[tclass] = func;
529 return func(arg, code, rangeName, ctx);
542 std::stringstream errorMsg;
543 errorMsg <<
"Only analytical integrals and 1D numeric integrals are supported for AD for class"
546 throw std::runtime_error(errorMsg.str().c_str());
552 std::string oldIntVarResult = ctx.
getResult(intVar);
557 std::stringstream ss;
559 ss <<
"double " << obsName <<
"[1];\n";
569 <<
" const int n = 1000; // number of sampling points\n"
570 <<
" double d = " << intVar.getMax(arg.
intRange()) <<
" - " << intVar.getMin(arg.
intRange()) <<
";\n"
571 <<
" double eps = d / n;\n"
572 <<
" for (int i = 0; i < n; ++i) {\n"
573 <<
" " << obsName <<
"[0] = " << intVar.getMin(arg.
intRange()) <<
" + eps * i;\n"
574 <<
" double tmpA = " << funcName <<
"(params, " << obsName <<
", xlArr);\n"
575 <<
" " << obsName <<
"[0] = " << intVar.getMin(arg.
intRange()) <<
" + eps * (i + 1);\n"
576 <<
" double tmpB = " << funcName <<
"(params, " << obsName <<
", xlArr);\n"
577 <<
" " << resName <<
" += (tmpA + tmpB) * 0.5 * eps;\n"
603 constexpr auto max_precision{std::numeric_limits<double>::digits10 + 1};
604 std::stringstream ss;
605 ss.precision(max_precision);
621 unsigned int n = interpCodes.size();
623 int interpCode = interpCodes[0];
625 if (interpCode == 4) {
629 for (
unsigned int i = 1; i <
n; i++) {
630 if (interpCodes[i] != interpCodes[0]) {
632 <<
"FlexibleInterpVar::evaluate ERROR: Code Squashing AD does not yet support having "
633 "different interpolation codes for the same class object "
638 std::string
const &resName = ctx.
buildCall(mathFunc(
"flexibleInterp"), interpCode, arg.
variables(),
n, arg.
low(),
658 std::stringstream errorMsg;
659 errorMsg <<
"An analytical integral function for class \"" << arg.
ClassName() <<
"\" has not yet been implemented.";
661 throw std::runtime_error(errorMsg.str().c_str());
668 return ctx.
buildCall(mathFunc(
"bernsteinIntegral"),
x.getMin(rangeName),
x.getMax(rangeName), arg.
xmin(), arg.
xmax(),
674 auto &constant = code == 1 ? arg.
getMean() : arg.
getX();
677 return ctx.
buildCall(mathFunc(
"bifurGaussIntegral"), integrand.getMin(rangeName), integrand.getMax(rangeName),
684 return ctx.
buildCall(mathFunc(
"cbShapeIntegral"),
m.getMin(rangeName),
m.getMax(rangeName), arg.
getM0(),
696 x.getMax(rangeName));
706 bool isOverX = code == 1;
708 std::string constant;
716 double min = integrand.
getMin(rangeName);
717 double max = integrand.getMax(rangeName);
725 return ctx.
buildCall(mathFunc(
"exponentialIntegral"), min, max, constant);
731 const std::string
a =
733 const std::string
b =
735 return a +
" - " +
b;
740 auto &constant = code == 1 ? arg.
getMean() : arg.
getX();
743 return ctx.
buildCall(mathFunc(
"gaussianIntegral"), integrand.getMin(rangeName), integrand.getMax(rangeName),
749std::string rooHistIntegralTranslateImpl(
int code,
RooAbsArg const &arg,
RooDataHist const &dataHist,
752 if (((2 << obs.
size()) - 1) != code) {
754 <<
") ERROR: AD currently only supports integrating over all histogram observables."
758 return std::to_string(dataHist.
sum(histFuncMode));
765 return rooHistIntegralTranslateImpl(code, arg, arg.
dataHist(), arg.
variables(),
true);
770 return rooHistIntegralTranslateImpl(code, arg, arg.
dataHist(), arg.
variables(),
false);
793 std::stringstream errorMsg;
794 errorMsg <<
"Partial integrals over RooMultiVarGaussian are not supported.";
796 throw std::runtime_error(errorMsg.str().c_str());
804 assert(code == 1 || code == 2);
807 xName =
"std::floor(" + xName +
")";
812 xName = code == 1 ?
"0" : xName;
813 return ctx.
buildCall(mathFunc(
"poissonIntegral"), code, arg.
getMean(), xName, integrand.getMin(rangeName),
820 const double xmin =
x.getMin(rangeName);
821 const double xmax =
x.getMax(rangeName);
832 const double xmin =
x.getMin(rangeName);
833 const double xmax =
x.getMax(rangeName);
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
A class which maps the current values of a RooRealVar (or a set of RooRealVars) to one of a number of...
const RooArgList & paramList() const
const RooArgList & dataVars() const
RooDataHist const & dataHist() const
The PiecewiseInterpolation is a class that can morph distributions into each other,...
const RooArgList & highList() const
const RooAbsReal * nominalHist() const
Return pointer to the nominal hist function.
bool positiveDefinite() const
const RooArgList & lowList() const
const RooArgList & paramList() const
const std::vector< int > & interpolationCodes() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
bool isConstant() const
Check if the "Constant" attribute is set.
Abstract base class for RooRealVar binning definitions.
Int_t numBins() const
Return number of bins.
virtual double highBound() const =0
virtual double lowBound() const =0
A space to attach TBranches.
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Storage_t::size_type size() const
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
Abstract base class for objects that represent a real value and implements functionality common to al...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
TClass * IsA() const override
Efficient implementation of a sum of PDFs of the form.
const RooArgList & coefList() const
const RooArgList & pdfList() const
Calculates the sum of a set of RooAbsReal terms, or when constructed with two sets,...
const RooArgList & list() const
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Bernstein basis polynomials are positive-definite in the range [0,1].
RooAbsRealLValue const & x() const
RooArgList const & coefList() const
Bifurcated Gaussian p.d.f with different widths on left and right side of maximum value.
RooAbsReal const & getSigmaL() const
Get the left sigma parameter.
RooAbsReal const & getSigmaR() const
Get the right sigma parameter.
RooAbsReal const & getX() const
Get the x variable.
RooAbsReal const & getMean() const
Get the mean parameter.
PDF implementing the Crystal Ball line shape.
RooAbsReal const & getSigma() const
RooAbsReal const & getM() const
RooAbsReal const & getN() const
RooAbsReal const & getM0() const
RooAbsReal const & getAlpha() const
Chebychev polynomial p.d.f.
RooAbsReal const & x() const
RooArgList const & coefList() const
const char * refRangeName() const
Represents a constant real-valued object.
Calculates the sum of the -(log) likelihoods of a set of RooAbsPfs that represent constraint function...
const RooArgList & list()
Container class to hold N-dimensional binned data.
double sum(bool correctForBinSize, bool inverseCorr=false) const
Return the sum of the weights of all bins in the histogram.
std::vector< std::unique_ptr< const RooAbsBinning > > const & getBinnings() const
std::string declWeightArrayForCodeSquash(RooFit::Experimental::CodegenContext &ctx, bool correctForBinSize) const
double weight(std::size_t i) const
Return weight of i-th bin.
std::string calculateTreeIndexForCodeSquash(RooAbsArg const *klass, RooFit::Experimental::CodegenContext &ctx, const RooAbsCollection &coords, bool reverse=false) const
double binVolume(std::size_t i) const
Return bin volume of i-th bin.
The class RooEffProd implements the product of a PDF with an efficiency function.
RooAbsReal const & pdf() const
RooAbsReal const & eff() const
A PDF helper class to fit efficiencies parameterized by a supplied function F.
RooAbsCategory const & cat() const
RooAbsReal const & effFunc() const
std::string sigCatName() const
bool negateCoefficient() const
RooAbsReal const & coefficient() const
Get the coefficient "c".
RooAbsReal const & variable() const
Get the x variable.
RooExtendPdf is a wrapper around an existing PDF that adds a parameteric extended likelihood term to ...
RooAbsPdf const & pdf() const
A RooProdPdf with a fixed normalization set can be replaced by this class.
RooProdPdf::CacheElem const & cache() const
RooAbsReal const & normIntegral() const
RooAbsPdf const & pdf() const
A class to maintain the context for squashing of RooFit models into code.
void addToGlobalScope(std::string const &str)
Adds the given string to the string block that will be emitted at the top of the squashed function.
std::string const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
std::string getTmpVarName() const
Get a unique variable name to be used in the generated code.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
auto const & outputSizes() const
void addToCodeBody(RooAbsArg const *klass, std::string const &in)
Adds the input string to the squashed code body.
std::unique_ptr< LoopScope > beginLoop(RooAbsArg const *in)
Create a RAII scope for iterating over vector observables.
void collectFunction(std::string const &name)
Register a function that is only know to the interpreter to the context.
std::string buildFunction(RooAbsArg const &arg, std::map< RooFit::Detail::DataKey, std::size_t > const &outputSizes={})
Assemble and return the final code with the return expression and global statements.
std::string buildCall(std::string const &funcname, Args_t const &...args)
Build the code to call the function with name funcname, passing some arguments.
std::string buildArg(RooAbsCollection const &x)
Function to save a RooListProxy as an array in the squashed code.
Implementation of the Gamma PDF for RooFit/RooStats.
RooAbsReal const & getX() const
RooAbsReal const & getGamma() const
RooAbsReal const & getBeta() const
RooAbsReal const & getMu() const
RooAbsReal const & getX() const
Get the x variable.
RooAbsReal const & getMean() const
Get the mean parameter.
RooAbsReal const & getSigma() const
Get the sigma parameter.
Implementation of a probability density function that takes a RooArgList of servers and a C++ express...
const RooArgList & dependents() const
std::string getUniqueFuncName() const
A real-valued function sampled from a multidimensional histogram.
Int_t getInterpolationOrder() const
Return histogram interpolation order.
bool getCdfBoundaries() const
If true, special boundary conditions for c.d.f.s are used.
RooDataHist & dataHist()
Return RooDataHist that is represented.
RooArgSet const & variables() const
A propability density function sampled from a multidimensional histogram.
Int_t getInterpolationOrder() const
bool haveUnitNorm() const
bool getCdfBoundaries() const
RooArgSet const & variables() const
Landau distribution p.d.f.
RooAbsReal const & getSigma() const
RooAbsReal const & getMean() const
RooAbsReal const & getX() const
bool useStandardParametrization() const
RooAbsReal const & getMedian() const
Get the median parameter.
RooAbsReal const & getShapeK() const
Get the shape parameter.
RooAbsReal const & getX() const
Get the x variable.
Multivariate Gaussian p.d.f.
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Handle full integral here.
const RooArgList & xVec() const
const TMatrixDSym & covarianceMatrixInverse() const
const RooArgList & muVec() const
static std::string toString(double x)
Returns an std::to_string compatible number (i.e.
A histogram function that assigns scale parameters to every bin.
const RooArgList & paramList() const
const RooArgList & xList() const
const RooDataHist & dataHist() const
RooAbsReal const & getX() const
Get the x variable.
bool getProtectNegativeMean() const
bool getNoRounding() const
RooAbsReal const & getMean() const
Get the mean parameter.
A RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficients.
RooRealProxy const & x() const
RooArgList const & coefList() const
RooPolynomial implements a polynomial p.d.f of the form.
RooAbsReal const & x() const
Get the x variable.
int lowestOrder() const
Return the order for the first coefficient in the list.
RooArgList const & coefList() const
Get the coefficient list.
std::unique_ptr< RooAbsReal > _rearrangedNum
std::unique_ptr< RooAbsReal > _rearrangedDen
Represents the product of a given set of RooAbsReal objects.
const RooArgList & realComponents() const
Represents the ratio of two RooAbsReal objects.
RooAbsReal const & numerator() const
RooAbsReal const & denominator() const
Performs hybrid numerical/analytical integrals of RooAbsReal objects.
const RooArgSet & numIntRealVars() const
RooArgSet intVars() const
const RooAbsReal & integrand() const
const RooArgSet & numIntCatVars() const
const char * intRange() const
const RooArgList & coefList() const
const RooArgList & funcList() const
Implements a PDF constructed from a sum of functions:
const RooArgList & funcList() const
const RooArgList & funcIntListFromCache(Int_t code, const char *rangeName=nullptr) const
Collect the list of functions to be integrated from the cache.
const RooArgList & coefList() const
Variable that can be changed from the outside.
A RooAbsReal implementation that calculates the plain fraction of sum of RooAddPdf components from a ...
RooArgList const & variables() const
const std::vector< int > & interpolationCodes() const
double globalBoundary() const
const RooListProxy & variables() const
const std::vector< double > & high() const
const std::vector< double > & low() const
TClass instances represent classes, structs and namespaces in the ROOT type system.
const Element * GetMatrixArray() const override
const char * GetName() const override
Returns name of object.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
std::string makeValidVarName(std::string const &in)
void codegenImpl(RooFit::Detail::RooFixedProdPdf &arg, CodegenContext &ctx)
std::string codegenIntegralImpl(RooAbsReal &arg, int code, const char *rangeName, CodegenContext &ctx)
This function defines the analytical integral translation for the class.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
static uint64_t sum(uint64_t i)