Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAddition.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18\file RooAddition.cxx
19\class RooAddition
20\ingroup Roofitcore
21
22Calculates the sum of a set of RooAbsReal terms, or
23when constructed with two sets, it sums the product of the terms
24in the two sets.
25**/
26
27#include "RooAddition.h"
28#include "RooRealSumPdf.h"
29#include "RooProduct.h"
30#include "RooErrorHandler.h"
31#include "RooArgSet.h"
32#include "RooNameReg.h"
34#include "RooMsgService.h"
35#include "RooBatchCompute.h"
36
37
38#include <ostream>
39
40////////////////////////////////////////////////////////////////////////////////
41/// Constructor with a single set consisting of RooAbsReal.
42/// \param[in] name Name of the PDF
43/// \param[in] title Title
44/// \param[in] sumSet The value of the function will be the sum of the values in this set
45
46RooAddition::RooAddition(const char *name, const char *title, const RooArgList &sumSet)
47 : RooAbsReal(name, title), _set("!set", "set of components", this), _cacheMgr(this, 10)
48{
49 _set.addTyped<RooAbsReal>(sumSet);
50}
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor with two sets of RooAbsReals.
56///
57/// The sum of pair-wise products of elements in the sets will be computed:
58/// \f[
59/// A = \sum_i \mathrm{Set1}[i] * \mathrm{Set2}[i]
60/// \f]
61///
62/// \param[in] name Name of the PDF
63/// \param[in] title Title
64/// \param[in] sumSet1 Left-hand element of the pair-wise products
65/// \param[in] sumSet2 Right-hand element of the pair-wise products
66///
67RooAddition::RooAddition(const char *name, const char *title, const RooArgList &sumSet1, const RooArgList &sumSet2)
68 : RooAbsReal(name, title), _set("!set", "set of components", this), _cacheMgr(this, 10)
69{
70 if (sumSet1.size() != sumSet2.size()) {
71 coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: input lists should be of equal length" << std::endl;
73 }
74
75 for (unsigned int i = 0; i < sumSet1.size(); ++i) {
76 const auto comp1 = &sumSet1[i];
77 const auto comp2 = &sumSet2[i];
78
79 if (!dynamic_cast<RooAbsReal*>(comp1)) {
80 coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp1->GetName()
81 << " in first list is not of type RooAbsReal" << std::endl;
83 }
84
85 if (!dynamic_cast<RooAbsReal*>(comp2)) {
86 coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp2->GetName()
87 << " in first list is not of type RooAbsReal" << std::endl;
89 }
90 TString _name(name);
91 _name.Append( "_[");
92 _name.Append(comp1->GetName());
93 _name.Append( "_x_");
94 _name.Append(comp2->GetName());
95 _name.Append( "]");
96 auto prod = std::make_unique<RooProduct>( _name, _name , RooArgSet(*comp1, *comp2));
97 _set.add(*prod);
98 _ownedList.addOwned(std::move(prod));
99 }
100}
101
102
103
104////////////////////////////////////////////////////////////////////////////////
105/// Copy constructor
106
107RooAddition::RooAddition(const RooAddition& other, const char* name)
108 : RooAbsReal(other, name)
109 , _set("!set",this,other._set)
110 , _cacheMgr(other._cacheMgr,this)
111{
112 // Member _ownedList is intentionally not copy-constructed -- ownership is not transferred
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Calculate and return current value of self
117
119{
120 double sum(0);
121 const RooArgSet* nset = _set.nset() ;
122
123 for (auto* comp : static_range_cast<RooAbsReal*>(_set)) {
124 const double tmp = comp->getVal(nset);
125 sum += tmp ;
126 }
127 return sum ;
128}
129
130
131////////////////////////////////////////////////////////////////////////////////
132/// Compute addition of PDFs in batches.
134{
135 std::vector<std::span<const double>> pdfs;
136 std::vector<double> coefs;
137 pdfs.reserve(_set.size());
138 coefs.reserve(_set.size());
139 for (const auto arg : _set) {
140 pdfs.push_back(ctx.at(arg));
141 coefs.push_back(1.0);
142 }
143 RooBatchCompute::compute(ctx.config(this), RooBatchCompute::AddPdf, ctx.output(), pdfs, coefs);
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Return the default error level for MINUIT error analysis.
148/// If the addition contains a test statistic implemented by RooNLLVarNew,
149/// return its defaultErrorLevel(). Otherwise, issue a warning message and
150/// return a value of 1.
151
153{
154 RooAbsReal* nllArg(nullptr) ;
155
156 std::unique_ptr<RooArgSet> comps{getComponents()};
157 for(RooAbsArg * arg : *comps) {
158 if (dynamic_cast<RooFit::Detail::RooNLLVarNew*>(arg)) {
159 nllArg = static_cast<RooAbsReal*>(arg) ;
160 }
161 }
162
163 if (nllArg) {
164 coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName()
165 << ") Summation contains a test statistic, using its error level" << std::endl;
166 return nllArg->defaultErrorLevel() ;
167 }
168
169 coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() << ") WARNING: "
170 << "Summation contains no test statistic server, using default level of 1.0" << std::endl;
171
172 return 1.0 ;
173}
174
175
176
177////////////////////////////////////////////////////////////////////////////////
178
180{
181 for (const auto arg : _set) {
182 static_cast<RooAbsReal*>(arg)->setData(data,cloneData) ;
183 }
184 return true ;
185}
186
187
188
189////////////////////////////////////////////////////////////////////////////////
190
191void RooAddition::printMetaArgs(std::ostream& os) const
192{
193 // We can use the implementation of RooRealSumPdf with an empty coefficient list.
194 static const RooArgList coefs{};
196}
197
198////////////////////////////////////////////////////////////////////////////////
199
200Int_t RooAddition::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* rangeName) const
201{
202 // we always do things ourselves -- actually, always delegate further down the line ;-)
203 analVars.add(allVars);
204
205 // check if we already have integrals for this combination of factors
206 Int_t sterileIndex(-1);
207 CacheElem* cache = static_cast<CacheElem*>(_cacheMgr.getObj(&analVars,&analVars,&sterileIndex,RooNameReg::ptr(rangeName)));
208 if (cache!=nullptr) {
209 Int_t code = _cacheMgr.lastIndex();
210 return code+1;
211 }
212
213 // we don't, so we make it right here....
214 cache = new CacheElem;
215 for (auto *arg : static_range_cast<RooAbsReal const*>(_set)) {// checked in c'tor that this will work...
216 cache->_I.addOwned(std::unique_ptr<RooAbsReal>{arg->createIntegral(analVars,rangeName)});
217 }
218
219 Int_t code = _cacheMgr.setObj(&analVars,&analVars,(RooAbsCacheElement*)cache,RooNameReg::ptr(rangeName));
220 return 1+code;
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Calculate integral internally from appropriate integral cache
225
226double RooAddition::analyticalIntegral(Int_t code, const char* rangeName) const
227{
228 // note: rangeName implicit encoded in code: see _cacheMgr.setObj in getPartIntList...
229 CacheElem *cache = static_cast<CacheElem*>(_cacheMgr.getObjByIndex(code-1));
230 if (cache==nullptr) {
231 // cache got sterilized, trigger repopulation of this slot, then try again...
232 std::unique_ptr<RooArgSet> vars( getParameters(RooArgSet()) );
233 RooArgSet iset = _cacheMgr.selectFromSet2(*vars, code-1);
234 RooArgSet dummy;
235 Int_t code2 = getAnalyticalIntegral(iset,dummy,rangeName);
236 assert(code==code2); // must have revived the right (sterilized) slot...
237 return analyticalIntegral(code2,rangeName);
238 }
239 assert(cache!=nullptr);
240
241 // loop over cache, and sum...
242 double result(0);
243 for (auto I : cache->_I) {
244 result += static_cast<const RooAbsReal*>(I)->getVal();
245 }
246 return result;
247
248}
249
250
251////////////////////////////////////////////////////////////////////////////////
252
253std::list<double>* RooAddition::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const
254{
255 return RooRealSumPdf::binBoundaries(_set, obs, xlo, xhi);
256}
257
258
263
264
265////////////////////////////////////////////////////////////////////////////////
266
267std::list<double>* RooAddition::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
268{
269 return RooRealSumPdf::plotSamplingHint(_set, obs, xlo, xhi);
270}
#define coutI(a)
#define coutE(a)
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 result
char name[80]
Definition TGX11.cxx:142
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
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...
RooFit::OwningPtr< RooArgSet > getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
Abstract base class for objects to be stored in RooAbsCache cache manager objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
bool addTyped(const RooAbsCollection &list, bool silent=false)
Adds elements of a given RooAbsCollection to the container if they match the specified type.
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:55
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
virtual double defaultErrorLevel() const
Definition RooAbsReal.h:245
Calculates the sum of a set of RooAbsReal terms, or when constructed with two sets,...
Definition RooAddition.h:27
RooArgList _ownedList
List of owned components.
Definition RooAddition.h:61
void doEval(RooFit::EvalContext &) const override
Compute addition of PDFs in batches.
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
Retrieve bin boundaries if this distribution is binned in obs.
RooListProxy _set
set of terms to be summed
Definition RooAddition.h:62
void printMetaArgs(std::ostream &os) const override
bool setData(RooAbsData &data, bool cloneData=true) override
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Calculate integral internally from appropriate integral cache.
RooObjCacheManager _cacheMgr
! The cache manager
Definition RooAddition.h:70
double defaultErrorLevel() const override
Return the default error level for MINUIT error analysis.
double evaluate() const override
Calculate and return current value of self.
std::list< double > * plotSamplingHint(RooAbsRealLValue &, double, double) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
bool isBinnedDistribution(const RooArgSet &obs) const override
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=nullptr)
Setter function without integration set.
T * getObjByIndex(Int_t index) const
Retrieve payload object by slot index.
RooArgSet selectFromSet2(RooArgSet const &argSet, int index) const
Create RooArgSet containing the objects that are both in the cached set 2 with a given index and an i...
Int_t lastIndex() const
Return index of slot used in last get or set operation.
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=nullptr, const TNamed *isetRangeName=nullptr)
Getter function without integration set.
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...
static void softAbort()
Soft abort function that interrupts macro execution but doesn't kill ROOT.
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
RooBatchCompute::Config config(RooAbsArg const *arg) const
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
std::list< double > * plotSamplingHint(RooAbsRealLValue &, double, double) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
std::list< double > * binBoundaries(RooAbsRealLValue &, double, double) const override
Retrieve bin boundaries if this distribution is binned in obs.
void printMetaArgs(std::ostream &os) const override
Customized printing of arguments of a RooRealSumPdf to more intuitively reflect the contents of the p...
bool isBinnedDistribution(const RooArgSet &obs) const override
Check if all components that depend on obs are binned.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Basic string class.
Definition TString.h:137
TString & Append(const char *cs)
Definition TString.h:582
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan extraArgs={})
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2335