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) +
"]");
91 if (
item.second.size == 1) {
92 ctx.addResult(
obsName,
"obs[" + std::to_string(
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;
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()
166 std::string
gradName = _funcName +
"_grad_0";
170 gInterpreter->Declare(
"#include <Math/CladDerivator.h>\n");
176 " clad::gradient(" << _funcName <<
", \"params\");\n"
189 errorMsg <<
"Function " << GetName() <<
" could not be differentiated. See above for details.";
191 throw std::runtime_error(
errorMsg.str().c_str());
197 std::stringstream
ss;
199 ss <<
"static_cast<void (*)(double *, double const *, double const *, double *)>(" <<
gradName <<
");";
203 _hasGradient =
false;
205 errorMsg <<
"Function " << GetName() <<
" could not be differentiated since ROOT was built without Clad support.";
207 throw std::runtime_error(
errorMsg.str().c_str());
211void RooFuncWrapper::gradient(
double *out)
const
213 updateGradientVarBuffer();
214 std::fill(out, out + _params.size(), 0.0);
216 _grad(_gradientVarBuffer.data(), _observables.data(), _xlArr.data(), out);
219void RooFuncWrapper::updateGradientVarBuffer()
const
221 std::transform(_params.begin(), _params.end(), _gradientVarBuffer.begin(),
222 [](
RooAbsArg *obj) { return static_cast<RooAbsReal *>(obj)->getVal(); });
225double RooFuncWrapper::evaluate()
const
228 return _absReal->getVal();
229 updateGradientVarBuffer();
231 return _func(_gradientVarBuffer.data(), _observables.data(), _xlArr.data());
234void RooFuncWrapper::gradient(
const double *
x,
double *
g)
const
236 std::fill(
g,
g + _params.size(), 0.0);
238 _grad(
const_cast<double *
>(
x), _observables.data(), _xlArr.data(),
g);
242void RooFuncWrapper::writeDebugMacro(std::string
const &
filename)
const
248 for (std::string
const &
name : _collectedFunctions) {
253 std::unique_ptr<TInterpreterValue>
v =
gInterpreter->MakeInterpreterValue();
255 std::string s =
v->ToString();
256 for (
int i = 0; i < 2; ++i) {
257 s = s.erase(0, s.find(
"\n") + 1);
264 outFile << R
"(//auto-generated test macro
265#include <RooFit/Detail/MathFuncs.h>
266#include <Math/CladDerivator.h>
268#pragma cling optimize(2)
272void gradient_request() {
274 << _funcName << R"(, "params");
279 updateGradientVarBuffer();
282 std::stringstream
decl;
283 decl <<
"std::vector<double> " <<
name <<
" = {";
284 for (std::size_t i = 0; i <
vec.size(); ++i) {
288 if (i <
vec.size() - 1)
295 replaceAll(
declStr,
"inf",
"std::numeric_limits<double>::infinity()");
296 replaceAll(
declStr,
"nan",
"NAN");
301 outFile <<
"// clang-format off\n" << std::endl;
308 outFile <<
"// clang-format on\n" << std::endl;
311// To run as a ROOT macro
315 std::vector<double> gradientVec(parametersVec.size());
317 auto func = [&](std::span<double> params) {
319 << _funcName << R"((params.data(), observablesVec.data(), auxConstantsVec.data());
321 auto grad = [&](std::span<double> params, std::span<double> out) {
323 << _funcName << R"(_grad_0(parametersVec.data(), observablesVec.data(), auxConstantsVec.data(),
327 grad(parametersVec, gradientVec);
329 auto numDiff = [&](int i) {
330 const double eps = 1e-6;
331 std::vector<double> p{parametersVec};
332 p[i] = parametersVec[i] - eps;
333 double funcValDown = func(p);
334 p[i] = parametersVec[i] + eps;
335 double funcValUp = func(p);
336 return (funcValUp - funcValDown) / (2 * eps);
339 for (std::size_t i = 0; i < parametersVec.size(); ++i) {
340 std::cout << i << ":" << std::endl;
341 std::cout << " numr : " << numDiff(i) << std::endl;
342 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...