51void checkObservableSpanInRange(
RooRealVar const &var, std::string
const &obsName, std::string
const &datasetName,
52 std::span<const double> span)
54 for (
double val : span) {
55 if (!var.
inRange(val,
nullptr)) {
56 const double lo = var.
getMin();
58 std::stringstream errMsg;
59 errMsg <<
"RooAbsPdf::fitTo/createNLL: cannot evaluate the likelihood because dataset \"" << datasetName
60 <<
"\" has an entry for observable \"" << obsName <<
"\" with value " << val
61 <<
", which is outside of its range [" << lo <<
", " <<
hi <<
"]. The probability density is "
62 <<
"normalized over exactly that range, so events outside of it would silently bias the fit. If "
63 <<
"you want to fit only a subset of the data, define a named range and use it in the fit, for example:\n"
64 <<
" " << obsName <<
".setRange(\"fitRange\", " << lo <<
", " <<
hi <<
");\n"
65 <<
" pdf.fitTo(data, RooFit::Range(\"fitRange\"));\n"
66 <<
"This way, only the events inside \"fitRange\" enter the likelihood, consistent with how the "
67 <<
"pdf is normalized.";
68 oocoutE(
nullptr, InputArguments) << errMsg.str() << std::endl;
69 throw std::runtime_error(errMsg.str());
91 for (
auto const &nameIdx : simPdf->indexCat()) {
92 RooAbsPdf *channelPdf = simPdf->getPdf(nameIdx.first);
95 const std::string prefix =
"_" + nameIdx.first +
"_";
96 std::unique_ptr<RooArgSet> vars{channelPdf->
getVariables()};
97 std::unique_ptr<RooArgSet> obs{vars->selectByAttrib(
"__obs__",
true)};
99 auto *realVar =
dynamic_cast<RooRealVar *
>(arg);
103 if (it == dataSpans.end())
105 std::string obsName = realVar->GetName();
106 if (obsName.rfind(prefix, 0) == 0) {
107 obsName = obsName.substr(prefix.size());
109 checkObservableSpanInRange(*realVar, obsName,
data.GetName(), it->second);
118 auto *realVar =
dynamic_cast<RooRealVar *
>(arg);
122 if (it == dataSpans.end())
124 checkObservableSpanInRange(*realVar, realVar->GetName(),
data.GetName(), it->second);
138 std::string
const &prefix, std::string
const &
channelName, std::string
const &rangeName,
140 std::function<
bool()>
const &hasEntries)
142 auto fail = [&](std::string
const &reason) {
143 std::stringstream errMsg;
144 errMsg <<
"RooAbsPdf::fitTo/createNLL: the dataset \"" <<
data.GetName() <<
"\" ";
148 errMsg <<
"can't be used with the binned likelihood optimization: " << reason
149 <<
". The optimization requires exactly one entry in each bin of the pdf, in the order of the bins, "
150 "like in a RooDataHist with the binning of the pdf. To create the likelihood with this dataset, "
151 "disable the binned likelihood optimization by calling "
152 "setAttribute(\"BinnedLikelihood\", false) on the pdf that has this attribute. If you think that the "
153 "optimization should work with this dataset, please report it by opening an issue on the ROOT GitHub "
155 oocoutE(
nullptr, InputArguments) << errMsg.str() << std::endl;
156 throw std::invalid_argument(errMsg.str());
159 const std::vector<std::string> subRanges =
ROOT::Split(rangeName,
",",
true);
163 enum RangeState :
char {
171 std::span<const double> values;
172 std::vector<double> boundaries;
173 std::vector<std::vector<RangeState>> inSubRange;
174 std::size_t stride = 1;
177 std::size_t nBins()
const {
return boundaries.size() - 1; }
178 std::size_t bin(std::size_t iBin)
const {
return (iBin / stride) % nBins(); }
179 double binWidth(std::size_t i)
const {
return boundaries[i + 1] - boundaries[i]; }
181 std::vector<Dim> dims;
183 std::size_t nBins = 1;
186 dataVar ?
dynamic_cast<RooRealVar *
>(observables.
find((prefix + dataVar->GetName()).c_str())) : nullptr;
190 std::unique_ptr<std::list<double>> boundaries{pdf.
binBoundaries(*obs, obs->getMin(), obs->getMax())};
191 if (!boundaries || boundaries->size() < 2) {
192 fail(std::string(
"the pdf provides no bin boundaries for the observable \"") + dataVar->GetName() +
"\"");
194 dims.push_back({dataVar->GetName(),
195 found != dataSpans.end() ? found->second : std::span<const
double>{},
196 {boundaries->begin(), boundaries->end()},
198 Dim &dim = dims.back();
199 dim.tol = 8 * std::numeric_limits<double>::epsilon() *
200 std::max(std::abs(dim.boundaries.front()), std::abs(dim.boundaries.back()));
202 for (std::string
const &subRange : subRanges) {
203 dim.inSubRange.emplace_back();
204 const double rangeLo = dataVar->getMin(subRange.c_str());
205 const double rangeHi = dataVar->getMax(subRange.c_str());
206 for (std::size_t i = 0; i < dim.nBins(); ++i) {
207 const double lo = dim.boundaries[i];
208 const double hi = dim.boundaries[i + 1];
209 const bool isOut = rangeHi < lo - dim.tol || rangeLo >
hi + dim.tol;
210 dim.inSubRange.back().push_back(rangeLo <= lo &&
hi <= rangeHi ? In : (isOut ? Out : Maybe));
213 nBins *= dim.nBins();
219 for (std::size_t i = dims.size() - 1; i > 0; --i) {
220 dims[i - 1].stride = dims[i].stride * dims[i].nBins();
223 const std::size_t nEntries = dims.front().values.size();
226 const bool isRanged = !subRanges.empty() && !(nBins == 1 && nEntries == 1);
229 if (nEntries == 0 && (!isRanged || !hasEntries()))
234 std::vector<RangeState> binStates(nBins, isRanged ? Out : In);
235 std::size_t nBinsInRange = 0;
236 std::size_t nBinsMaybeInRange = 0;
237 std::size_t endOfBinsInRange = 0;
238 for (std::size_t iBin = 0; iBin < nBins; ++iBin) {
239 RangeState &state = binStates[iBin];
240 for (std::size_t iSub = 0; isRanged && iSub < subRanges.size(); ++iSub) {
241 RangeState inSub = In;
242 for (Dim
const &dim : dims) {
243 inSub = std::min(inSub, dim.inSubRange[iSub][dim.bin(iBin)]);
245 state = std::max(state, inSub);
247 nBinsMaybeInRange += state == Maybe;
250 endOfBinsInRange = iBin + 1;
253 auto failNumEntries = [&]() {
254 const std::string inRange = isRanged ?
" in the fit range" :
"";
255 fail(
"it has " + std::to_string(nEntries) +
" entries" + inRange +
", which doesn't match the " +
256 std::to_string(nBinsInRange) +
" bins of the pdf" + inRange);
258 if (nEntries < nBinsInRange || nEntries > nBinsInRange + nBinsMaybeInRange) {
265 auto isInBin = [&](std::size_t iEntry, std::size_t iBin) {
266 return std::all_of(dims.begin(), dims.end(), [&](Dim
const &dim) {
267 const double val = dim.values[iEntry];
268 const std::size_t i = dim.bin(iBin);
269 return val >= dim.boundaries[i] - dim.tol &&
270 (val < dim.boundaries[i + 1] || (i + 1 == dim.nBins() && val <= dim.boundaries[i + 1] + dim.tol));
274 auto isAtUpperEdge = [&](std::size_t iEntry, std::size_t iBin) {
275 return std::any_of(dims.begin(), dims.end(), [&](Dim
const &dim) {
276 return dim.values[iEntry] >= dim.boundaries[dim.bin(iBin) + 1] - dim.tol;
282 const bool usesBinWidths = !pdf.
getAttribute(
"BinnedLikelihoodActiveYields") && dims.size() == 1;
287 std::vector<std::size_t> positions{0};
288 std::vector<std::size_t> nextPositions;
290 for (std::size_t iEntry = 0; iEntry < nEntries; ++iEntry) {
291 Dim
const &dim0 = dims.front();
292 bool binWidthMismatch =
false;
293 nextPositions.clear();
294 for (std::size_t iBin : positions) {
296 std::size_t iBinEnd = nBins;
297 for (; iBin < iBinEnd; ++iBin) {
298 const RangeState state = binStates[iBin];
299 if (state != Out && isInBin(iEntry, iBin)) {
301 std::abs(dim0.binWidth(iBin) - dim0.binWidth(iEntry)) > 1
e-6 * dim0.binWidth(iEntry)) {
302 binWidthMismatch =
true;
303 }
else if (std::find(nextPositions.begin(), nextPositions.end(), iBin + 1) == nextPositions.end()) {
304 nextPositions.push_back(iBin + 1);
306 if (state == In || !isAtUpperEdge(iEntry, iBin))
308 iBinEnd = std::min(iBinEnd, iBin + dim0.stride + 1);
309 }
else if (state == In) {
315 if (nextPositions.empty()) {
316 if (binWidthMismatch) {
317 fail(
"the fit range excludes bins, which is not supported for a pdf with bins of different widths that "
318 "is not built with RooBinWidthFunctions");
320 std::stringstream reason;
321 reason <<
"entry " << iEntry <<
" (";
322 for (Dim
const &dim : dims) {
323 reason << (&dim == &dims.front() ?
"" :
", ") << dim.name <<
" = " << dim.values[iEntry];
325 reason <<
") is not in the bin that is expected at its position";
328 positions.swap(nextPositions);
331 if (std::none_of(positions.begin(), positions.end(), [&](std::size_t iBin) { return iBin >= endOfBinsInRange; })) {
345 if (simPdf && simPdf->indexCatIsObservable(*
data.get())) {
347 auto *simCatAsRooCategory =
dynamic_cast<RooCategory const *
>(&simCat);
348 const bool splitRange = simPdf->
getAttribute(
"SplitRange");
350 std::unique_ptr<Roo1DTable> table;
351 for (
auto const &nameIdx : simCat) {
354 if (!channelPdf || !channelPdf->
getAttribute(
"BinnedLikelihoodActive"))
357 if (!rangeName.empty() && simCatAsRooCategory &&
358 !simCatAsRooCategory->isStateInRange(rangeName.c_str(), nameIdx.second))
360 auto hasEntries = [&]() {
362 table.reset(
data.table(simCat));
364 return table->get(
channelName.c_str(),
true) != 0.;
367 std::unique_ptr<RooArgSet> vars{channelPdf->
getVariables()};
368 std::unique_ptr<RooArgSet> obs{vars->
selectByAttrib(
"__obs__",
true)};
371 dataSpans, hasEntries);
373 }
else if (pdf->
getAttribute(
"BinnedLikelihoodActive")) {
376 checkBinnedLikelihoodData(*pdf, obs,
data,
"",
"", rangeName, dataSpans, [&]() {
return data.numEntries() > 0; });
385 std::string
const &rangeName,
RooAbsPdf const *pdf,
386 bool takeGlobalObservablesFromData,
int nWorkers)
387 :
RooAbsReal{
"RooEvaluatorWrapper",
"RooEvaluatorWrapper"},
388 _evaluator{std::make_unique<
RooFit::Evaluator>(topNode, useGPU)},
389 _topNode(
"topNode",
"top node", this, topNode, false, false),
391 _paramSet(
"paramSet",
"Set of parameters", this),
392 _rangeName{rangeName},
394 _takeGlobalObservablesFromData{takeGlobalObservablesFromData}
396 if (nWorkers > 1 && !useGPU) {
397 _evaluator->setNThreads(nWorkers);
400 setData(*
data,
false);
402 _paramSet.add(_evaluator->getParameters());
403 for (
auto const &item : _dataSpans) {
404 _paramSet.remove(*_paramSet.find(item.first->GetName()));
408RooEvaluatorWrapper::RooEvaluatorWrapper(
const RooEvaluatorWrapper &other,
const char *
name)
410 _evaluator{other._evaluator},
411 _topNode(
"topNode", this, other._topNode),
413 _paramSet(
"paramSet",
"Set of parameters", this),
414 _rangeName{other._rangeName},
416 _takeGlobalObservablesFromData{other._takeGlobalObservablesFromData},
417 _dataSpans{other._dataSpans}
419 _paramSet.add(other._paramSet);
422RooEvaluatorWrapper::~RooEvaluatorWrapper() =
default;
424bool RooEvaluatorWrapper::getParameters(
const RooArgSet *observables,
RooArgSet &outputSet,
425 bool stripDisconnected)
const
427 outputSet.
add(_evaluator->getParameters());
429 outputSet.
remove(*observables,
false,
true);
432 for (
auto const &item : _dataSpans) {
433 if (_data->getGlobalObservables() && _data->getGlobalObservables()->find(item.first->GetName())) {
445 if (_takeGlobalObservablesFromData && _data->getGlobalObservables()) {
446 outputSet.
replace(*_data->getGlobalObservables());
453 if (stripDisconnected) {
455 _topNode->getParameters(observables, paramsStripped,
true);
458 if (!paramsStripped.
find(param->GetName())) {
459 toRemove.
add(*param);
462 outputSet.
remove(toRemove,
false,
true);
472class RooFuncWrapper {
475 std::string
const &rangeName,
bool skipZeroWeights);
477 bool hasGradient()
const {
return _hasGradient; }
478 bool hasHessian()
const {
return _hasHessian; }
479 void gradient(
double *out)
const
481 updateGradientVarBuffer();
482 std::fill(out, out + _params.size(), 0.0);
483 _grad(_varBuffer.data(), _observables.data(), _xlArr.data(), out);
485 void hessian(
double *out)
const
487 updateGradientVarBuffer();
488 std::fill(out, out + _params.size() * _params.size(), 0.0);
489 _hessian(_varBuffer.data(), _observables.data(), _xlArr.data(), out);
492 void createGradient();
493 void createHessian();
495 void writeDebugMacro(std::string
const &)
const;
497 std::vector<std::string>
const &collectedFunctions() {
return _collectedFunctions; }
501 updateGradientVarBuffer();
502 return _func(_varBuffer.data(), _observables.data(), _xlArr.data());
509 void updateGradientVarBuffer()
const;
511 void buildFuncAndGradFunctors();
513 using Func =
double (*)(
double *,
double const *,
double const *);
514 using Grad = void (*)(
double *,
double const *,
double const *,
double *);
515 using Hessian = void (*)(
double *,
double const *,
double const *,
double *);
518 std::string _funcName;
522 bool _hasGradient =
false;
523 bool _hasHessian =
false;
524 mutable std::vector<double> _varBuffer;
525 std::vector<double> _observables;
526 std::unordered_map<RooFit::Detail::DataKey, std::size_t> _obsInfos;
527 std::vector<double> _xlArr;
528 std::vector<std::string> _collectedFunctions;
533void replaceAll(std::string &str,
const std::string &from,
const std::string &to)
537 size_t start_pos = 0;
538 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
539 str.replace(start_pos, from.length(), to);
540 start_pos += to.length();
549 std::unordered_set<RooFit::Detail::DataKey> dependsOnData;
551 dependsOnData.insert(arg);
555 if (arg->getAttribute(
"__obs__")) {
556 dependsOnData.insert(arg);
558 for (
RooAbsArg *server : arg->servers()) {
559 if (server->isValueServer(*arg)) {
560 if (dependsOnData.find(server) != dependsOnData.end() && !arg->isReducerNode()) {
561 dependsOnData.insert(arg);
568 return dependsOnData;
574 RooArgSet const ¶mSet, std::string
const &rangeName,
bool skipZeroWeights)
578 loadData(*
data, simPdf, rangeName, skipZeroWeights);
582 for (
auto *param : paramSet) {
583 if (_obsInfos.find(param) == _obsInfos.end()) {
587 _varBuffer.resize(_params.size());
590 std::unordered_set<RooFit::Detail::DataKey> dependsOnData;
592 dependsOnData = getDependsOnData(obj, *
data->get());
601 ctx.
addResult(param,
"params[" + std::to_string(idx) +
"]");
605 for (
auto const &item : _obsInfos) {
606 const char *obsName = item.first->GetName();
612 auto print = [](std::string
const &msg) {
oocoutI(
nullptr, Fitting) << msg << std::endl; };
617 gInterpreter->Declare(
"#include <RooFit/CodegenImpl.h>\n");
620 std::stringstream errorMsg;
621 std::string debugFileName =
"_codegen_" + _funcName +
".cxx";
622 errorMsg <<
"Function " << _funcName <<
" could not be compiled. See above for details. Full code dumped to file "
623 << debugFileName <<
" for debugging";
625 std::ofstream outFile;
626 outFile.open(debugFileName.c_str());
629 oocoutE(
nullptr, InputArguments) << errorMsg.str() << std::endl;
630 throw std::runtime_error(errorMsg.str().c_str());
633 _func =
reinterpret_cast<Func
>(
gInterpreter->ProcessLine((_funcName +
";").c_str()));
635 _xlArr = ctx.
xlArr();
640 bool skipZeroWeights)
643 std::stack<std::vector<double>> vectorBuffers;
645 RooFit::BatchModeDataHelpers::getDataSpans(
data, rangeName, simPdf, skipZeroWeights,
false, vectorBuffers);
647 _observables.clear();
649 std::size_t
total = 0;
650 _observables.reserve(2 * spans.size());
652 for (
auto const &item : spans) {
653 _obsInfos.emplace(item.first, idx);
654 _observables.push_back(
total + 2 * spans.size());
655 _observables.push_back(item.second.size());
656 total += item.second.size();
660 for (
auto const &item : spans) {
661 std::size_t
n = item.second.size();
662 _observables.reserve(_observables.size() +
n);
663 for (std::size_t i = 0; i <
n; ++i) {
664 _observables.push_back(item.second[i]);
670void RooFuncWrapper::createGradient()
673 std::string gradName = _funcName +
"_grad_0";
674 std::string requestName = _funcName +
"_req";
677 gInterpreter->Declare(
"#include <Math/CladDerivator.h>\n");
680 std::stringstream requestFuncStrm;
681 requestFuncStrm <<
"#pragma clad ON\n"
682 "void " << requestName <<
"() {\n"
683 " clad::gradient(" << _funcName <<
", \"params\");\n"
687 auto print = [](std::string
const &msg) {
oocoutI(
nullptr, Fitting) << msg << std::endl; };
689 bool cladSuccess =
false;
692 cladSuccess = !
gInterpreter->Declare(requestFuncStrm.str().c_str());
695 std::stringstream errorMsg;
696 errorMsg <<
"Function could not be differentiated. See above for details.";
697 oocoutE(
nullptr, InputArguments) << errorMsg.str() << std::endl;
698 throw std::runtime_error(errorMsg.str().c_str());
704 std::stringstream ss;
706 ss <<
"static_cast<void (*)(double *, double const *, double const *, double *)>(" << gradName <<
");";
707 _grad =
reinterpret_cast<Grad
>(
gInterpreter->ProcessLine(ss.str().c_str()));
710 _hasGradient =
false;
711 std::stringstream errorMsg;
712 errorMsg <<
"Function could not be differentiated since ROOT was built without Clad support.";
713 oocoutE(
nullptr, InputArguments) << errorMsg.str() << std::endl;
714 throw std::runtime_error(errorMsg.str().c_str());
718void RooFuncWrapper::createHessian()
721 std::string hessianName = _funcName +
"_hessian_0";
722 std::string requestName = _funcName +
"_hessian_req";
725 gInterpreter->Declare(
"#include <Math/CladDerivator.h>\n");
728 std::stringstream requestFuncStrm;
729 std::string paramsStr =
730 _params.size() == 1 ?
"\"params[0]\"" : (
"\"params[0:" + std::to_string(_params.size() - 1) +
"]\"");
731 requestFuncStrm <<
"#pragma clad ON\n"
732 "void " << requestName <<
"() {\n"
733 " clad::hessian(" << _funcName <<
", " << paramsStr <<
");\n"
737 auto print = [](std::string
const &msg) {
oocoutI(
nullptr, Fitting) << msg << std::endl; };
739 bool cladSuccess =
false;
742 cladSuccess = !
gInterpreter->Declare(requestFuncStrm.str().c_str());
745 std::stringstream errorMsg;
746 errorMsg <<
"Function could not be differentiated. See above for details.";
747 oocoutE(
nullptr, InputArguments) << errorMsg.str() << std::endl;
748 throw std::runtime_error(errorMsg.str().c_str());
754 std::stringstream ss;
756 ss <<
"static_cast<void (*)(double *, double const *, double const *, double *)>(" << hessianName <<
");";
757 _hessian =
reinterpret_cast<Hessian
>(
gInterpreter->ProcessLine(ss.str().c_str()));
761 std::stringstream errorMsg;
762 errorMsg <<
"Function could not be differentiated since ROOT was built without Clad support.";
763 oocoutE(
nullptr, InputArguments) << errorMsg.str() << std::endl;
764 throw std::runtime_error(errorMsg.str().c_str());
768void RooFuncWrapper::updateGradientVarBuffer()
const
770 std::transform(_params.begin(), _params.end(), _varBuffer.begin(), [](
RooAbsArg *obj) {
771 return obj->isCategory() ? static_cast<RooAbsCategory *>(obj)->getCurrentIndex()
772 : static_cast<RooAbsReal *>(obj)->getVal();
777void RooFuncWrapper::writeDebugMacro(std::string
const &
filename)
const
779 std::stringstream allCode;
780 std::set<std::string> seenFunctions;
783 for (std::string
const &
name : _collectedFunctions) {
784 if (seenFunctions.count(
name) > 0) {
787 seenFunctions.insert(
name);
788 std::unique_ptr<TInterpreterValue>
v =
gInterpreter->MakeInterpreterValue();
790 std::string s =
v->ToString();
791 for (
int i = 0; i < 2; ++i) {
792 s = s.erase(0, s.find(
'\n') + 1);
794 allCode << s << std::endl;
797 std::ofstream outFile;
798 std::string paramsStr =
799 _params.size() == 1 ?
"\"params[0]\"" : (
"\"params[0:" + std::to_string(_params.size() - 1) +
"]\"");
801 outFile << R
"(//auto-generated test macro
802#include <RooFit/Detail/MathFuncs.h>
803#include <Math/CladDerivator.h>
810void gradient_request() {
812 << _funcName << R"(, "params");
815 << _funcName << ", " << paramsStr << R
"();
821 updateGradientVarBuffer();
823 auto writeVector = [&](std::string
const &
name, std::span<const double> vec) {
824 std::stringstream decl;
825 decl <<
"std::vector<double> " <<
name <<
" = {";
826 for (std::size_t i = 0; i < vec.size(); ++i) {
830 if (i < vec.size() - 1)
835 std::string declStr = decl.str();
837 replaceAll(declStr,
"inf",
"std::numeric_limits<double>::infinity()");
843 outFile <<
"// clang-format off\n" << std::endl;
844 writeVector(
"parametersVec", _varBuffer);
845 outFile << std::endl;
846 writeVector(
"observablesVec", _observables);
847 outFile << std::endl;
848 writeVector(
"auxConstantsVec", _xlArr);
849 outFile << std::endl;
850 outFile <<
"// clang-format on\n" << std::endl;
853// To run as a ROOT macro
857 const std::size_t n = parametersVec.size();
859 std::vector<double> gradientVec(n);
861 auto func = [&](std::span<double> params) {
863 << _funcName << R"((params.data(), observablesVec.data(), auxConstantsVec.data());
865 auto grad = [&](std::span<double> params, std::span<double> out) {
867 << _funcName << R"(_grad_0(parametersVec.data(), observablesVec.data(), auxConstantsVec.data(),
871 grad(parametersVec, gradientVec);
873 auto numDiff = [&](int i) {
874 const double eps = 1e-6;
875 std::vector<double> p{parametersVec};
876 p[i] = parametersVec[i] - eps;
877 double funcValDown = func(p);
878 p[i] = parametersVec[i] + eps;
879 double funcValUp = func(p);
880 return (funcValUp - funcValDown) / (2 * eps);
883 for (std::size_t i = 0; i < parametersVec.size(); ++i) {
884 std::cout << i << ":" << std::endl;
885 std::cout << " numr : " << numDiff(i) << std::endl;
886 std::cout << " clad : " << gradientVec[i] << std::endl;
892 auto hess = [&](std::span<double> params, std::span<double> out) {
894 << _funcName << R"(_hessian_0(params.data(), observablesVec.data(), auxConstantsVec.data(), out.data());
897 std::vector<double> hessianVec(n * n);
898 hess(parametersVec, hessianVec);
900 // ---------- Numerical Hessian ----------
901 // Uses central differences:
902 // diag: (f(x+ei)-2f(x)+f(x-ei))/eps^2
903 // offdiag: (f(++ ) - f(+-) - f(-+) + f(--)) / (4 eps^2)
904 auto numHess = [&](std::size_t i, std::size_t j) {
905 const double eps = 1e-5; // often needs to be a bit larger than grad eps
906 std::vector<double> p(parametersVec.begin(), parametersVec.end());
909 const double f0 = func(p);
911 p[i] = parametersVec[i] + eps;
912 const double fUp = func(p);
914 p[i] = parametersVec[i] - eps;
915 const double fDown = func(p);
917 return (fUp - 2.0 * f0 + fDown) / (eps * eps);
919 // f(x_i + eps, x_j + eps)
920 p[i] = parametersVec[i] + eps;
921 p[j] = parametersVec[j] + eps;
922 const double fPP = func(p);
924 // f(x_i + eps, x_j - eps)
925 p[i] = parametersVec[i] + eps;
926 p[j] = parametersVec[j] - eps;
927 const double fPM = func(p);
929 // f(x_i - eps, x_j + eps)
930 p[i] = parametersVec[i] - eps;
931 p[j] = parametersVec[j] + eps;
932 const double fMP = func(p);
934 // f(x_i - eps, x_j - eps)
935 p[i] = parametersVec[i] - eps;
936 p[j] = parametersVec[j] - eps;
937 const double fMM = func(p);
939 return (fPP - fPM - fMP + fMM) / (4.0 * eps * eps);
943 // Compute full numerical Hessian
944 std::vector<double> numHessianVec(n * n);
945 for (std::size_t i = 0; i < n; ++i) {
946 for (std::size_t j = 0; j < n; ++j) {
947 numHessianVec[i + n * j] = numHess(i, j); // keep same layout as your print
951 // ---------- Compare & print ----------
952 std::cout << "Hessian comparison (clad vs numeric vs diff):\n\n";
954 for (std::size_t i = 0; i < n; ++i) {
955 for (std::size_t j = 0; j < n; ++j) {
956 const std::size_t idx = i + n * j; // same indexing you used
957 const double cladH = hessianVec[idx];
958 const double numH = numHessianVec[idx];
959 const double diff = cladH - numH;
961 std::cout << "[" << i << "," << j << "] "
962 << "clad=" << cladH << " num=" << numH << " diff=" << diff << "\n";
966 std::cout << "\nRaw Clad Hessian matrix:\n";
967 for (std::size_t i = 0; i < n; ++i) {
968 for (std::size_t j = 0; j < n; ++j) {
969 std::cout << hessianVec[i + n * j] << " ";
974 std::cout << "\nRaw Numerical Hessian matrix:\n";
975 for (std::size_t i = 0; i < n; ++i) {
976 for (std::size_t j = 0; j < n; ++j) {
977 std::cout << numHessianVec[i + n * j] << " ";
986double RooEvaluatorWrapper::evaluate()
const
988 if (_useGeneratedFunctionCode)
989 return _funcWrapper->evaluate();
995 :
RooFit::EvalContext::OffsetMode::WithOffset);
997 return _evaluator->run()[0];
1006 constexpr auto errMsg =
"Error in RooAbsReal::setData(): only resetting with same-structured data is supported.";
1009 bool isInitializing = _paramSet.empty();
1010 const std::size_t oldSize = _dataSpans.size();
1012 std::stack<std::vector<double>>{}.swap(_vectorBuffers);
1013 const bool isChi2 = _topNode->getAttribute(
"Chi2EvaluationActive");
1014 bool skipZeroWeights = !isChi2 && (!_pdf || !_pdf->getAttribute(
"BinnedLikelihoodActive"));
1016 _dataSpans = RooFit::BatchModeDataHelpers::getDataSpans(*_data, _rangeName, simPdf, skipZeroWeights,
1017 _takeGlobalObservablesFromData, _vectorBuffers);
1018 if (_rangeName.empty()) {
1019 validateObservableRanges(_pdf, *_data, _dataSpans);
1021 validateBinnedLikelihoodData(_pdf, *_data, _rangeName, _dataSpans);
1022 if (!isInitializing && _dataSpans.size() != oldSize) {
1023 coutE(DataHandling) << errMsg << std::endl;
1024 throw std::runtime_error(errMsg);
1026 for (
auto const &item : _dataSpans) {
1027 const char *
name = item.first->GetName();
1028 _evaluator->setInput(
name, item.second,
false);
1029 if (_paramSet.find(
name)) {
1030 coutE(DataHandling) << errMsg << std::endl;
1031 throw std::runtime_error(errMsg);
1035 _funcWrapper->loadData(*_data, simPdf, _rangeName, skipZeroWeights);
1040void RooEvaluatorWrapper::createFuncWrapper()
1044 this->getParameters(_data ? _data->get() : nullptr, paramSet, false);
1046 const bool isChi2 = _topNode->getAttribute(
"Chi2EvaluationActive");
1047 const bool skipZeroWeights = !isChi2 && (!_pdf || !_pdf->getAttribute(
"BinnedLikelihoodActive"));
1048 _funcWrapper = std::make_unique<RooFuncWrapper>(*_topNode, _data,
dynamic_cast<RooSimultaneous const *
>(_pdf),
1049 paramSet, _rangeName, skipZeroWeights);
1052void RooEvaluatorWrapper::generateGradient()
1055 createFuncWrapper();
1056 if (!_funcWrapper->hasGradient())
1057 _funcWrapper->createGradient();
1060void RooEvaluatorWrapper::generateHessian()
1063 createFuncWrapper();
1064 if (!_funcWrapper->hasHessian())
1065 _funcWrapper->createHessian();
1068void RooEvaluatorWrapper::setUseGeneratedFunctionCode(
bool flag)
1070 _useGeneratedFunctionCode = flag;
1071 if (!_funcWrapper && _useGeneratedFunctionCode)
1072 createFuncWrapper();
1075void RooEvaluatorWrapper::gradient(
double *out)
const
1077 _funcWrapper->gradient(out);
1080void RooEvaluatorWrapper::hessian(
double *out)
const
1082 _funcWrapper->hessian(out);
1085bool RooEvaluatorWrapper::hasGradient()
const
1087 return _funcWrapper && _funcWrapper->hasGradient();
1090bool RooEvaluatorWrapper::hasHessian()
const
1092 return _funcWrapper && _funcWrapper->hasHessian();
1095void RooEvaluatorWrapper::writeDebugMacro(std::string
const &
filename)
const
1098 return _funcWrapper->writeDebugMacro(
filename);
1101std::unique_ptr<ChangeOperModeRAII> RooEvaluatorWrapper::setOperModes(
RooAbsArg::OperMode opMode)
1103 return _evaluator->setOperModes(opMode);
ROOT::RRangeCast< T, true, Range_t > dynamic_range_cast(Range_t &&coll)
static unsigned int total
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
Common abstract base class for objects that represent a value and a "shape" in RooFit.
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
RooFit::OwningPtr< RooArgSet > getVariables(bool stripDisconnected=true) const
Return RooArgSet with all variables (tree leaf nodes of expression tree)
Abstract base class for objects that represent a discrete value that can be set from the outside,...
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
virtual bool replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return true for success.
RooAbsArg * find(const char *name) const
Find object with given name in list.
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 that may appear on the left hand side of ...
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
bool inRange(const char *name) const override
Check if current value is inside range with given name.
Abstract base class for objects that represent a real value and implements functionality common to al...
virtual std::list< double > * binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const
Retrieve bin boundaries if this distribution is binned in obs.
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooArgSet * selectByAttrib(const char *name, bool value) const
Use RooAbsCollection::selectByAttrib(), but return as RooArgSet.
Object to represent discrete states.
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.
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 buildFunction(RooAbsArg const &arg, std::unordered_set< RooFit::Detail::DataKey > const &dependsOnData={})
Assemble and return the final code with the return expression and global statements.
std::vector< std::string > const & collectedFunctions()
std::string const & collectedCode()
std::vector< double > const & xlArr()
Variable that can be changed from the outside.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
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...
std::string getRangeNameForSimComponent(std::string const &rangeName, bool splitRange, std::string const &catName)
void getSortedComputationGraph(RooAbsArg const &func, RooArgSet &out)
void evaluate(typename Architecture_t::Tensor_t &A, EActivationFunction f)
Apply the given activation function to each value in the given tensor A.