41 :
RooAbsReal{
"RooEvaluatorWrapper",
"RooEvaluatorWrapper"},
45 _paramSet(
"paramSet",
"Set of parameters",
this),
51 setData(*
data,
false);
53 _paramSet.add(_evaluator->getParameters());
55 _paramSet.remove(*_paramSet.find(
item.first->GetName()));
61 _evaluator{
other._evaluator},
62 _topNode(
"topNode",
this,
other._topNode),
64 _paramSet(
"paramSet",
"Set of parameters",
this),
65 _rangeName{
other._rangeName},
67 _takeGlobalObservablesFromData{
other._takeGlobalObservablesFromData},
70 _paramSet.add(
other._paramSet);
73RooEvaluatorWrapper::~RooEvaluatorWrapper() =
default;
78 outputSet.add(_evaluator->getParameters());
80 outputSet.remove(*observables,
false,
true);
84 if (_data->getGlobalObservables() && _data->getGlobalObservables()->find(
item.first->GetName())) {
96 if (_takeGlobalObservablesFromData && _data->getGlobalObservables()) {
97 outputSet.replace(*_data->getGlobalObservables());
125 constexpr auto errMsg =
"Error in RooAbsReal::setData(): only resetting with same-structured data is supported.";
131 std::stack<std::vector<double>>{}.swap(_vectorBuffers);
132 bool skipZeroWeights = !_pdf || !_pdf->getAttribute(
"BinnedLikelihoodActive");
134 RooFit::BatchModeDataHelpers::getDataSpans(*_data, _rangeName,
dynamic_cast<RooSimultaneous const *
>(_pdf),
138 throw std::runtime_error(
errMsg);
141 const char *
name =
item.first->GetName();
142 _evaluator->setInput(
name,
item.second,
false);
143 if (_paramSet.find(
name)) {
145 throw std::runtime_error(
errMsg);
160 void gradient(
double *out)
const
163 std::fill(out, out + _params.size(), 0.0);
172 std::vector<std::string>
const &collectedFunctions() {
return _collectedFunctions; }
183 std::map<RooFit::Detail::DataKey, std::span<const double>>
188 using Func =
double (*)(
double *,
double const *,
double const *);
189 using Grad = void (*)(
double *,
double const *,
double const *,
double *);
194 std::size_t
size = 0;
203 std::vector<double> _observables;
204 std::map<RooFit::Detail::DataKey, ObsInfo>
_obsInfos;
205 std::vector<double> _xlArr;
206 std::vector<std::string> _collectedFunctions;
211void replaceAll(std::string &str,
const std::string &from,
const std::string &to)
217 str.replace(
start_pos, from.length(), to);
233 auto found =
spans.find(key);
234 return found !=
spans.
end() ? found->second.size() : -1;
242 ctx.
addResult(param,
"params[" + std::to_string(idx) +
"]");
251 if (
item.second.size == 1) {
252 ctx.addResult(
obsName,
"obs[" + std::to_string(
item.second.idx) +
"]");
262 auto print = [](std::string
const &
msg) {
oocoutI(
nullptr, Fitting) <<
msg << std::endl; };
267 _xlArr = ctx.xlArr();
268 _collectedFunctions = ctx.collectedFunctions();
271std::map<RooFit::Detail::DataKey, std::span<const double>>
276 std::map<RooFit::Detail::DataKey, std::span<const double>>
spans;
284 std::size_t
n =
item.second.size();
286 _observables.reserve(_observables.size() +
n);
287 for (std::size_t i = 0; i <
n; ++i) {
288 _observables.push_back(
item.second[i]);
303void RooFuncWrapper::createGradient()
310 gInterpreter->Declare(
"#include <Math/CladDerivator.h>\n");
316 " clad::gradient(" <<
_funcName <<
", \"params\");\n"
320 auto print = [](std::string
const &
msg) {
oocoutI(
nullptr, Fitting) <<
msg << std::endl; };
329 errorMsg <<
"Function could not be differentiated. See above for details.";
331 throw std::runtime_error(
errorMsg.str().c_str());
337 std::stringstream
ss;
339 ss <<
"static_cast<void (*)(double *, double const *, double const *, double *)>(" <<
gradName <<
");";
345 errorMsg <<
"Function could not be differentiated since ROOT was built without Clad support.";
347 throw std::runtime_error(
errorMsg.str().c_str());
351void RooFuncWrapper::updateGradientVarBuffer()
const
354 return obj->isCategory() ? static_cast<RooAbsCategory *>(obj)->getCurrentIndex()
355 : static_cast<RooAbsReal *>(obj)->getVal();
360void RooFuncWrapper::writeDebugMacro(std::string
const &
filename)
const
366 for (std::string
const &
name : _collectedFunctions) {
371 std::unique_ptr<TInterpreterValue>
v =
gInterpreter->MakeInterpreterValue();
373 std::string s =
v->ToString();
374 for (
int i = 0; i < 2; ++i) {
375 s = s.erase(0, s.find(
"\n") + 1);
382 outFile << R
"(//auto-generated test macro
383#include <RooFit/Detail/MathFuncs.h>
384#include <Math/CladDerivator.h>
386#pragma cling optimize(2)
390void gradient_request() {
400 std::stringstream
decl;
401 decl <<
"std::vector<double> " <<
name <<
" = {";
402 for (std::size_t i = 0; i <
vec.size(); ++i) {
406 if (i <
vec.size() - 1)
419 outFile <<
"// clang-format off\n" << std::endl;
426 outFile <<
"// clang-format on\n" << std::endl;
429// To run as a ROOT macro
433 std::vector<double> gradientVec(parametersVec.size());
435 auto func = [&](std::span<double> params) {
437 << _funcName << R"((params.data(), observablesVec.data(), auxConstantsVec.data());
439 auto grad = [&](std::span<double> params, std::span<double> out) {
441 << _funcName << R"(_grad_0(parametersVec.data(), observablesVec.data(), auxConstantsVec.data(),
445 grad(parametersVec, gradientVec);
447 auto numDiff = [&](int i) {
448 const double eps = 1e-6;
449 std::vector<double> p{parametersVec};
450 p[i] = parametersVec[i] - eps;
451 double funcValDown = func(p);
452 p[i] = parametersVec[i] + eps;
453 double funcValUp = func(p);
454 return (funcValUp - funcValDown) / (2 * eps);
457 for (std::size_t i = 0; i < parametersVec.size(); ++i) {
458 std::cout << i << ":" << std::endl;
459 std::cout << " numr : " << numDiff(i) << std::endl;
460 std::cout << " clad : " << gradientVec[i] << std::endl;
466double RooEvaluatorWrapper::evaluate()
const
475 :
RooFit::EvalContext::OffsetMode::WithOffset);
477 return _evaluator->run()[0];
480void RooEvaluatorWrapper::createFuncWrapper()
484 this->getParameters(_data ? _data->get() : nullptr,
paramSet,
false);
490void RooEvaluatorWrapper::generateGradient()
497void RooEvaluatorWrapper::setUseGeneratedFunctionCode(
bool flag)
504void RooEvaluatorWrapper::gradient(
double *out)
const
509bool RooEvaluatorWrapper::hasGradient()
const
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 begin() const
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Abstract base class for binned and unbinned datasets.
Abstract interface for all probability density functions.
Abstract base class for objects that represent a real value and implements functionality common to al...
RooArgList is a container object that can hold multiple RooAbsArg objects.
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.
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...
void evaluate(typename Architecture_t::Tensor_t &A, EActivationFunction f)
Apply the given activation function to each value in the given tensor A.