Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooEvaluatorWrapper.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*
4 * Project: RooFit
5 * Authors:
6 * Jonas Rembser, CERN 2023
7 *
8 * Copyright (c) 2023, CERN
9 *
10 * Redistribution and use in source and binary forms,
11 * with or without modification, are permitted according to the terms
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
13 */
14
15/**
16\internal
17\file RooEvaluatorWrapper.cxx
18\class RooEvaluatorWrapper
19\ingroup Roofitcore
20
21Wraps a RooFit::Evaluator that evaluates a RooAbsReal back into a RooAbsReal.
22**/
23
24#include "RooEvaluatorWrapper.h"
25
26#include <RooAbsData.h>
27#include <RooAbsPdf.h>
28#include <RooConstVar.h>
29#include <RooHelpers.h>
30#include <RooMsgService.h>
31#include <RooRealVar.h>
32#include <RooSimultaneous.h>
33
34RooEvaluatorWrapper::RooEvaluatorWrapper(RooAbsReal &topNode, RooAbsData *data, bool useGPU,
35 std::string const &rangeName, RooAbsPdf const *pdf,
36 bool takeGlobalObservablesFromData)
37 : RooAbsReal{"RooEvaluatorWrapper", "RooEvaluatorWrapper"},
38 _evaluator{std::make_unique<RooFit::Evaluator>(topNode, useGPU)},
39 _topNode("topNode", "top node", this, topNode, false, false),
40 _data{data},
41 _paramSet("paramSet", "Set of parameters", this),
42 _rangeName{rangeName},
43 _pdf{pdf},
44 _takeGlobalObservablesFromData{takeGlobalObservablesFromData}
45{
46 if (data) {
47 setData(*data, false);
48 }
49 _paramSet.add(_evaluator->getParameters());
50 for (auto const &item : _dataSpans) {
51 _paramSet.remove(*_paramSet.find(item.first->GetName()));
52 }
53}
54
55RooEvaluatorWrapper::RooEvaluatorWrapper(const RooEvaluatorWrapper &other, const char *name)
56 : RooAbsReal{other, name},
57 _evaluator{other._evaluator},
58 _topNode("topNode", this, other._topNode),
59 _data{other._data},
60 _paramSet("paramSet", "Set of parameters", this),
62 _pdf{other._pdf},
64 _dataSpans{other._dataSpans}
65{
66 _paramSet.add(other._paramSet);
67}
68
69double RooEvaluatorWrapper::evaluate() const
70{
71 if (!_evaluator)
72 return 0.0;
73
74 _evaluator->setOffsetMode(hideOffset() ? RooFit::EvalContext::OffsetMode::WithoutOffset
75 : RooFit::EvalContext::OffsetMode::WithOffset);
76
77 return _evaluator->run()[0];
78}
79
80bool RooEvaluatorWrapper::getParameters(const RooArgSet *observables, RooArgSet &outputSet,
81 bool /*stripDisconnected*/) const
82{
83 outputSet.add(_evaluator->getParameters());
84 if (observables) {
85 outputSet.remove(*observables, /*silent*/ false, /*matchByNameOnly*/ true);
86 }
87 // Exclude the data variables from the parameters which are not global observables
88 for (auto const &item : _dataSpans) {
89 if (_data->getGlobalObservables() && _data->getGlobalObservables()->find(item.first->GetName())) {
90 continue;
91 }
92 RooAbsArg *found = outputSet.find(item.first->GetName());
93 if (found) {
94 outputSet.remove(*found);
95 }
96 }
97 // If we take the global observables as data, we have to return these as
98 // parameters instead of the parameters in the model. Otherwise, the
99 // constant parameters in the fit result that are global observables will
100 // not have the right values.
102 outputSet.replace(*_data->getGlobalObservables());
103 }
104 return false;
105}
106
107bool RooEvaluatorWrapper::setData(RooAbsData &data, bool /*cloneData*/)
108{
109 // To make things easiear for RooFit, we only support resetting with
110 // datasets that have the same structure, e.g. the same columns and global
111 // observables. This is anyway the usecase: resetting same-structured data
112 // when iterating over toys.
113 constexpr auto errMsg = "Error in RooAbsReal::setData(): only resetting with same-structured data is supported.";
114
115 _data = &data;
116 bool isInitializing = _paramSet.empty();
117 const std::size_t oldSize = _dataSpans.size();
118
119 std::stack<std::vector<double>>{}.swap(_vectorBuffers);
120 bool skipZeroWeights = !_pdf || !_pdf->getAttribute("BinnedLikelihoodActive");
121 _dataSpans = RooFit::Detail::BatchModeDataHelpers::getDataSpans(
122 *_data, _rangeName, dynamic_cast<RooSimultaneous const *>(_pdf), skipZeroWeights, _takeGlobalObservablesFromData,
123 _vectorBuffers);
124 if (!isInitializing && _dataSpans.size() != oldSize) {
125 coutE(DataHandling) << errMsg << std::endl;
126 throw std::runtime_error(errMsg);
127 }
128 for (auto const &item : _dataSpans) {
129 const char *name = item.first->GetName();
130 _evaluator->setInput(name, item.second, false);
131 if (_paramSet.find(name)) {
132 coutE(DataHandling) << errMsg << std::endl;
133 throw std::runtime_error(errMsg);
134 }
135 }
136 return true;
137}
138
139/// \endcond
const bool _takeGlobalObservablesFromData
If the global observable values are taken from data.
RooAbsData * _data
Pointer to original input dataset.
RooSetProxy _paramSet
Parameters of the test statistic (=parameters of the input function)
std::string _rangeName
Name of range in which to calculate test statistic.
bool setData(RooAbsData &data, bool cloneData=true) override
#define coutE(a)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
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.
Definition RooAbsData.h:57
RooArgSet const * getGlobalObservables() const
Returns snapshot of global observables stored in this data.
Definition RooAbsData.h:288
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false) override
Remove object 'var' from set and deregister 'var' as server to owner.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
OffsetMode
For setting the offset mode with the Offset() command argument to RooAbsPdf::fitTo()