28 :
RooAbsReal{
name, title}, _params{
"!params",
"List of parameters", this}, _hasGradient{createGradient}
40 if (!param->isConstant()) {
41 floatingParamSet.
add(*param);
56 _params(
"!params", this, other._params),
59 _hasGradient(other._hasGradient),
60 _gradientVarBuffer(other._gradientVarBuffer),
61 _observables(other._observables)
69 std::stack<std::vector<double>> vectorBuffers;
70 std::map<RooFit::Detail::DataKey, std::span<const double>> spans;
73 spans = RooFit::BatchModeDataHelpers::getDataSpans(*
data,
"", simPdf,
true,
false, vectorBuffers);
77 for (
auto const &item : spans) {
78 std::size_t
n = item.second.size();
81 for (std::size_t i = 0; i <
n; ++i) {
88 for (
auto *param : paramSet) {
90 std::stringstream errorMsg;
91 errorMsg <<
"In creation of function " << GetName()
92 <<
" wrapper: input param expected to be of type RooAbsReal.";
93 coutE(InputArguments) << errorMsg.str() << std::endl;
94 throw std::runtime_error(errorMsg.str().c_str());
96 if (spans.find(param) == spans.end()) {
100 _gradientVarBuffer.resize(_params.size());
105 auto found = spans.find(key);
106 return found != spans.end() ? found->second.size() : 0;
113 static int iFuncWrapper = 0;
114 _funcName =
"roo_func_wrapper_" + std::to_string(iFuncWrapper++);
116 std::string gradName =
_funcName +
"_grad_0";
117 std::string requestName =
_funcName +
"_req";
118 std::string wrapperName =
_funcName +
"_derivativeWrapper";
123 std::stringstream bodyWithSigStrm;
124 bodyWithSigStrm <<
"double " <<
_funcName <<
"(double* params, double const* obs) {\n" << funcBody <<
"\n}";
125 bool comp =
gInterpreter->Declare(bodyWithSigStrm.str().c_str());
127 std::stringstream errorMsg;
128 errorMsg <<
"Function " <<
_funcName <<
" could not be compiled. See above for details.";
129 coutE(InputArguments) << errorMsg.str() << std::endl;
130 throw std::runtime_error(errorMsg.str().c_str());
138 gInterpreter->ProcessLine(
"#include <Math/CladDerivator.h>");
141 std::stringstream requestFuncStrm;
142 requestFuncStrm <<
"#pragma clad ON\n"
143 "void " << requestName <<
"() {\n"
144 " clad::gradient(" <<
_funcName <<
", \"params\");\n"
148 comp =
gInterpreter->Declare(requestFuncStrm.str().c_str());
150 std::stringstream errorMsg;
151 errorMsg <<
"Function " <<
GetName() <<
" could not be differentiated. See above for details.";
152 coutE(InputArguments) << errorMsg.str() << std::endl;
153 throw std::runtime_error(errorMsg.str().c_str());
159 std::stringstream dWrapperStrm;
160 dWrapperStrm <<
"void " << wrapperName <<
"(double* params, double const* obs, double* out) {\n"
161 " clad::array_ref<double> cladOut(out, " <<
_params.
size() <<
");\n"
162 " " << gradName <<
"(params, obs, cladOut);\n"
180 [](
RooAbsArg *obj) { return static_cast<RooAbsReal *>(obj)->getVal(); });
204 ctx.
addResult(param,
"params[" + std::to_string(idx) +
"]");
209 const char *
name = item.first->GetName();
213 if (item.second.size == 1) {
214 ctx.
addResult(
name,
"obs[" + std::to_string(item.second.idx) +
"]");
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Common abstract base class for objects that represent a value and a "shape" in RooFit.
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
const_iterator end() const
Storage_t::size_type size() const
const_iterator begin() const
Abstract base class for binned and unbinned datasets.
Abstract base class for objects that represent a real value and implements functionality common to al...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
A class to maintain the context for squashing of RooFit models into code.
std::string assembleCode(std::string const &returnExpr)
Assemble and return the final code with the return expression and global statements.
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.
void addVecObs(const char *key, int idx)
Since the squashed code represents all observables as a single flattened array, it is important to ke...
std::string const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
A wrapper class to store a C++ function of type 'double (*)(double*, double*)'.
std::vector< double > _gradientVarBuffer
void dumpGradient()
Prints the derivative code body to console.
void declareAndDiffFunction(std::string const &funcBody, bool createGradient)
std::map< RooFit::Detail::DataKey, std::size_t > _nodeOutputSizes
std::map< RooFit::Detail::DataKey, ObsInfo > _obsInfos
std::string buildCode(RooAbsReal const &head)
void gradient(double *out) const override
double(*)(double *, double const *) Func
void loadParamsAndData(RooAbsArg const *head, RooArgSet const ¶mSet, const RooAbsData *data, RooSimultaneous const *simPdf)
std::vector< double > _observables
void(*)(double *, double const *, double *) Grad
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void updateGradientVarBuffer() const
void dumpCode()
Prints the squashed code body to console.
RooFuncWrapper(const char *name, const char *title, RooAbsReal const &obj, RooArgSet const &normSet, const RooAbsData *data, RooSimultaneous const *simPdf, bool createGradient)
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
const char * GetName() const override
Returns name of object.
std::unique_ptr< T > compileForNormSet(T const &arg, RooArgSet const &normSet)