38void replaceAll(std::string &str,
const std::string &from,
const std::string &to)
44 str.replace(
start_pos, from.length(), to);
53namespace Experimental {
73 auto found =
spans.find(key);
74 return found !=
spans.
end() ? found->second.size() : -1;
82 ctx.
addResult(param,
"params[" + std::to_string(idx) +
"]");
87 const char *
obsName = item.first->GetName();
91 if (item.second.size == 1) {
92 ctx.addResult(
obsName,
"obs[" + std::to_string(item.second.idx) +
"]");
95 ctx.addVecObs(
obsName, item.second.idx);
114 _funcName(
other._funcName),
117 _hasGradient(
other._hasGradient),
118 _gradientVarBuffer(
other._gradientVarBuffer),
119 _observables(
other._observables)
123std::map<RooFit::Detail::DataKey, std::span<const double>>
128 std::map<RooFit::Detail::DataKey, std::span<const double>>
spans;
135 for (
auto const &item :
spans) {
136 std::size_t
n = item.second.size();
139 for (std::size_t i = 0; i <
n; ++i) {
149 errorMsg <<
"In creation of function " << GetName()
150 <<
" wrapper: input param expected to be of type RooAbsReal.";
152 throw std::runtime_error(
errorMsg.str().c_str());
158 _gradientVarBuffer.resize(_params.size());
163void RooFuncWrapper::createGradient()
165 std::string
gradName = _funcName +
"_grad_0";
169 gInterpreter->Declare(
"#include <Math/CladDerivator.h>\n");
175 " clad::gradient(" << _funcName <<
", \"params\");\n"
188 errorMsg <<
"Function " << GetName() <<
" could not be differentiated. See above for details.";
190 throw std::runtime_error(
errorMsg.str().c_str());
196 std::stringstream
ss;
198 ss <<
"static_cast<void (*)(double *, double const *, double const *, double *)>(" <<
gradName <<
");";
203void RooFuncWrapper::gradient(
double *out)
const
205 updateGradientVarBuffer();
206 std::fill(out, out + _params.size(), 0.0);
208 _grad(_gradientVarBuffer.data(), _observables.data(), _xlArr.data(), out);
211void RooFuncWrapper::updateGradientVarBuffer()
const
213 std::transform(_params.begin(), _params.end(), _gradientVarBuffer.begin(),
214 [](
RooAbsArg *obj) { return static_cast<RooAbsReal *>(obj)->getVal(); });
217double RooFuncWrapper::evaluate()
const
220 return _absReal->getVal();
221 updateGradientVarBuffer();
223 return _func(_gradientVarBuffer.data(), _observables.data(), _xlArr.data());
226void RooFuncWrapper::gradient(
const double *
x,
double *
g)
const
228 std::fill(
g,
g + _params.size(), 0.0);
230 _grad(
const_cast<double *
>(
x), _observables.data(), _xlArr.data(),
g);
234void RooFuncWrapper::writeDebugMacro(std::string
const &
filename)
const
240 for (std::string
const &
name : _collectedFunctions) {
245 std::unique_ptr<TInterpreterValue>
v =
gInterpreter->MakeInterpreterValue();
247 std::string s =
v->ToString();
248 for (
int i = 0; i < 2; ++i) {
249 s = s.erase(0, s.find(
"\n") + 1);
256 outFile << R
"(//auto-generated test macro
257#include <RooFit/Detail/MathFuncs.h>
258#include <Math/CladDerivator.h>
260#pragma cling optimize(2)
264void gradient_request() {
266 << _funcName << R"(, "params");
271 updateGradientVarBuffer();
274 std::stringstream
decl;
275 decl <<
"std::vector<double> " <<
name <<
" = {";
276 for (std::size_t i = 0; i <
vec.size(); ++i) {
280 if (i <
vec.size() - 1)
287 replaceAll(
declStr,
"inf",
"std::numeric_limits<double>::infinity()");
288 replaceAll(
declStr,
"nan",
"NAN");
293 outFile <<
"// clang-format off\n" << std::endl;
300 outFile <<
"// clang-format on\n" << std::endl;
303// To run as a ROOT macro
307 std::vector<double> gradientVec(parametersVec.size());
309 auto func = [&](std::span<double> params) {
311 << _funcName << R"((params.data(), observablesVec.data(), auxConstantsVec.data());
313 auto grad = [&](std::span<double> params, std::span<double> out) {
315 << _funcName << R"(_grad_0(parametersVec.data(), observablesVec.data(), auxConstantsVec.data(),
319 grad(parametersVec, gradientVec);
321 auto numDiff = [&](int i) {
322 const double eps = 1e-6;
323 std::vector<double> p{parametersVec};
324 p[i] = parametersVec[i] - eps;
325 double funcValDown = func(p);
326 p[i] = parametersVec[i] + eps;
327 double funcValUp = func(p);
328 return (funcValUp - funcValDown) / (2 * eps);
331 for (std::size_t i = 0; i < parametersVec.size(); ++i) {
332 std::cout << i << ":" << std::endl;
333 std::cout << " numr : " << numDiff(i) << std::endl;
334 std::cout << " clad : " << gradientVec[i] << std::endl;
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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 filename
const_iterator end() const
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...
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.
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.
A wrapper class to store a C++ function of type 'double (*)(double*, double*)'.
std::unique_ptr< RooAbsReal > _absReal
std::vector< std::string > _collectedFunctions
std::vector< double > _xlArr
std::vector< double > _observables
std::map< RooFit::Detail::DataKey, std::span< const double > > loadParamsAndData(RooArgSet const ¶mSet, const RooAbsData *data, RooSimultaneous const *simPdf)
std::map< RooFit::Detail::DataKey, ObsInfo > _obsInfos
RooFuncWrapper(const char *name, const char *title, RooAbsReal &obj, const RooAbsData *data=nullptr, RooSimultaneous const *simPdf=nullptr, bool useEvaluator=false)
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
void replaceAll(std::string &inOut, std::string_view what, std::string_view with)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...