Logo ROOT  
Reference Guide
RooArgSet.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooArgSet.h,v 1.45 2007/08/09 19:55:47 wouter Exp $
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#ifndef ROO_ARG_SET
17#define ROO_ARG_SET
18
19#include "RooAbsCollection.h"
20#include "RooFit/UniqueId.h"
21
22class RooAbsArg ;
23class RooArgList ;
24
25// # Original comment on USEMEMPOOLFORARGSET:
26//
27// Use a memory pool for RooArgSet.
28// RooFit assumes (e.g. for caching results) that arg sets that have the same pointer have
29// the same contents. Trying to remove that memory pool lead to wrong results, because the
30// OS *occasionally* returns the same address, and the caching goes wrong.
31// It's hard to track down, so disable this only when e.g. looking for memory leaks!
32//
33// # Update April 2022:
34//
35// Using pointers comparisons for caching RooFit results caused too many bugs,
36// even wih the memory pool. For example, if the RooArgSet is created on the
37// stack, there is no guarantee that memory is not reused. Also, pointer
38// comparisons still work if the RooArgSets for the cache entry are already out
39// of scope, which can also cause problems. Therefore, when RooArgSets are used
40// for caching, RooFit now uses the `RooArgSet::uniqueId()` as of PR [1].
41//
42// Since pointers are not used as cache keys anymore, the custom memory pool
43// is not necessary anymore. It was decided to deactivate it, because it also
44// caused quite some trouble on its own. It caused unexpected memory increases,
45// possibly because of heap fragmentation [2], and overloading `operator new`
46// and `delete` caused PyROOT issues on some platforms.
47//
48// [1] https://github.com/root-project/root/pull/10333
49// [2] https://github.com/root-project/root/issues/8323
50
51// #define USEMEMPOOLFORARGSET
52
53template <class RooSet_t, size_t>
55
57public:
58
59#ifdef USEMEMPOOLFORARGSET
60 void* operator new (size_t bytes);
61 void* operator new (size_t bytes, void* ptr) noexcept;
62 void operator delete (void *ptr);
63#endif
64
65 // Constructors, assignment etc.
66 RooArgSet();
67
68 /// Construct a (non-owning) RooArgSet from one or more
69 /// RooFit objects. The set will not own its contents.
70 /// \tparam Ts Parameter pack of objects that derive from RooAbsArg or RooFit collections; or a name.
71 /// \param arg A RooFit object.
72 /// Note that you can also pass a `double` as first argument
73 /// when constructing a RooArgSet, and another templated
74 /// constructor will be used where a RooConstVar is implicitly
75 /// created from the `double` value.
76 /// \param moreArgsOrName Arbitrary number of
77 /// - Further RooFit objects that derive from RooAbsArg
78 /// - RooFit collections of such objects
79 /// - `double`s from which a RooConstVar is implicitly created via `RooFit::RooConst`.
80 /// - A name for the set. Given multiple names, the last-given name prevails.
81 template<typename... Args_t>
82 RooArgSet(const RooAbsArg& arg, Args_t &&... moreArgsOrName)
83 /*NB: Making this a delegating constructor led to linker errors with MSVC*/
84 {
85 // This constructor should cause a failed static_assert if any of the input
86 // arguments is a temporary (r-value reference), which will be checked in
87 // processArg. This works statically because of the universal reference
88 // mechanism with templated functions.
89 // Unfortunately, we can't check the first arg, because it's type can't be
90 // a template parameter and hence a universal reference can't be used.
91 // This problem is solved by introducing another templated constructor below,
92 // which accepts a RooAbsArg && as the first argument which is forwarded to
93 // be the second argument for this constructor.
94 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
95 }
96
97 /// This constructor will provoke a `static_assert`, because passing a
98 /// RooAbsArg as r-value reference is not allowed.
99 template<typename... Args_t>
100 RooArgSet(RooAbsArg && arg, Args_t &&... moreArgsOrName)
101 : RooArgSet{arg, std::move(arg), std::forward<Args_t>(moreArgsOrName)...} {}
102
103 template<typename... Args_t>
104 explicit RooArgSet(double arg, Args_t &&... moreArgsOrName) {
105 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
106 }
107
108 /// Construct a (non-owning) RooArgSet from iterators.
109 /// \tparam Iterator_t An iterator pointing to RooFit objects or to pointers/references of those.
110 /// \param beginIt Iterator to first element to add.
111 /// \param endIt Iterator to end of range to be added.
112 /// \param name Optional name of the collection.
113 template<typename Iterator_t,
114 typename value_type = typename std::remove_pointer<typename std::iterator_traits<Iterator_t>::value_type>::type,
116 RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char* name="") :
117 RooArgSet(name) {
118 for (auto it = beginIt; it != endIt; ++it) {
119 processArg(*it);
120 }
121 }
122
123 /// Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
124 /// This constructor is mainly intended for pyROOT. With cppyy, a Python list
125 /// or tuple can be implicitly converted to an std::vector, and by enabling
126 /// implicit construction of a RooArgSet from a std::vector, we indirectly
127 /// enable implicit conversion from a Python list/tuple to RooArgSets.
128 /// \param vec A vector with pointers to the arguments or doubles for RooFit::RooConst().
129 RooArgSet(std::vector<RooAbsArgPtrOrDouble> const& vec) {
130 for(auto const& arg : vec) {
131 if(arg.hasPtr) processArg(arg.ptr);
132 else processArg(arg.val);
133 }
134 }
135
136 RooArgSet(const RooArgSet& other, const char *name="");
137 /// Move constructor.
138 RooArgSet(RooArgSet && other) : RooAbsCollection(std::move(other)) {}
139
140 RooArgSet(const RooArgSet& set1, const RooArgSet& set2,
141 const char *name="");
142
143 RooArgSet(const RooAbsCollection& coll) ;
144 RooArgSet(const RooAbsCollection& collection, const RooAbsArg* var1);
145 explicit RooArgSet(const TCollection& tcoll, const char* name="") ;
146 explicit RooArgSet(const char *name);
147
148 ~RooArgSet() override;
149 TObject* clone(const char* newname) const override { return new RooArgSet(*this,newname); }
150 TObject* create(const char* newname) const override { return new RooArgSet(newname); }
151 RooArgSet& operator=(const RooArgSet& other) { RooAbsCollection::operator=(other) ; return *this ;}
152
153 using RooAbsCollection::operator[];
154 RooAbsArg& operator[](const TString& str) const;
155
156
157 /// Shortcut for readFromStream(std::istream&, bool, const char*, const char*, bool), setting
158 /// `flagReadAtt` and `section` to 0.
159 virtual bool readFromStream(std::istream& is, bool compact, bool verbose=false) {
160 // I/O streaming interface (machine readable)
161 return readFromStream(is, compact, nullptr, nullptr, verbose) ;
162 }
163 bool readFromStream(std::istream& is, bool compact, const char* flagReadAtt, const char* section, bool verbose=false) ;
164 virtual void writeToStream(std::ostream& os, bool compact, const char* section=nullptr) const;
165 void writeToFile(const char* fileName) const ;
166 bool readFromFile(const char* fileName, const char* flagReadAtt=nullptr, const char* section=nullptr, bool verbose=false) ;
167
168
169 /// Check if this exact instance is in this collection.
170 bool containsInstance(const RooAbsArg& var) const override {
171 return find(var) == &var;
172 }
173
174 static void cleanup() ;
175
176 bool isInRange(const char* rangeSpec) ;
177
178 /// Use RooAbsCollection::snapshot(), but return as RooArgSet.
179 RooArgSet * snapshot(bool deepCopy = true) const {
180 return static_cast<RooArgSet*>(RooAbsCollection::snapshot(deepCopy));
181 }
182
183 /// \copydoc RooAbsCollection::snapshot()
184 bool snapshot(RooAbsCollection& output, bool deepCopy=true) const {
185 return RooAbsCollection::snapshot(output, deepCopy);
186 }
187
188 /// Returns a unique ID that is different for every instantiated RooArgSet.
189 /// This ID can be used to check whether two RooAbsData are the same object,
190 /// which is safer than memory address comparisons that might result in false
191 /// positives when memory is recycled.
193
194protected:
195 bool checkForDup(const RooAbsArg& arg, bool silent) const ;
196 bool canBeAdded(const RooAbsArg& arg, bool silent) const override {
197 return !checkForDup(arg, silent);
198 }
199
200private:
201
202 template<typename... Args_t>
203 void processArgs(Args_t &&... args) {
204 // Expand parameter pack in C++ 11 way:
205 int dummy[] = { 0, (processArg(std::forward<Args_t>(args)), 0) ... };
206 (void)dummy;
207 }
208 void processArg(const RooAbsArg& arg) { add(arg); }
209 void processArg(const RooAbsArg* arg) { add(*arg); }
210 void processArg(RooAbsArg* var) { add(*var); }
211 template<class Arg_t>
212 void processArg(Arg_t && arg) {
213 assert_is_no_temporary(std::forward<Arg_t>(arg));
214 add(arg);
215 }
216 void processArg(const char* name) { _name = name; }
217 void processArg(double value);
218 void processArg(const RooAbsCollection& coll) { add(coll); if (_name.Length() == 0) _name = coll.GetName(); }
219 // this overload with r-value references is needed so we don't trigger the
220 // templated function with the failing static_assert for r-value references
221 void processArg(RooAbsCollection && coll) { processArg(coll); }
222 void processArg(const RooArgList& list);
223
224#ifdef USEMEMPOOLFORARGSET
225 typedef MemPoolForRooSets<RooArgSet, 10*600> MemPool; //600 = about 100 kb
226 //Initialise a static mem pool. It has to happen inside a function to solve the
227 //static initialisation order fiasco. At the end of the program, this might have
228 //to leak depending if RooArgSets are still alive. This depends on the order of destructions.
229 static MemPool* memPool();
230#endif
232
233 ClassDefOverride(RooArgSet,1) // Set of RooAbsArg objects
234};
235
236
237namespace RooFitShortHand {
238
239template<class... Args_t>
240RooArgSet S(Args_t&&... args) {
241 return {std::forward<Args_t>(args)...};
242}
243
244} // namespace RooFitShortHand
245
246
247#endif
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t bytes
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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition: TGX11.cxx:110
Memory pool for RooArgSet and RooDataSet.
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:71
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooAbsCollection * snapshot(bool deepCopy=true) const
Take a snap shot of current collection contents.
const char * GetName() const override
Returns name of object.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
TString _name
Our name.
static void assert_is_no_temporary(T &&)
RooAbsCollection & operator=(const RooAbsCollection &other)
Assign values from the elements in other to our elements.
RooAbsArg * find(const char *name) const
Find object with given name in list.
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:56
bool isInRange(const char *rangeSpec)
Definition: RooArgSet.cxx:619
void processArg(RooAbsCollection &&coll)
Definition: RooArgSet.h:221
TObject * clone(const char *newname) const override
Definition: RooArgSet.h:149
RooArgSet & operator=(const RooArgSet &other)
Definition: RooArgSet.h:151
bool checkForDup(const RooAbsArg &arg, bool silent) const
Check if element with var's name is already in set.
Definition: RooArgSet.cxx:278
RooArgSet(RooAbsArg &&arg, Args_t &&... moreArgsOrName)
This constructor will provoke a static_assert, because passing a RooAbsArg as r-value reference is no...
Definition: RooArgSet.h:100
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:179
void processArg(Arg_t &&arg)
Definition: RooArgSet.h:212
RooArgSet()
Default constructor.
Definition: RooArgSet.cxx:153
RooFit::UniqueId< RooArgSet > const & uniqueId() const
Returns a unique ID that is different for every instantiated RooArgSet.
Definition: RooArgSet.h:192
RooArgSet(std::vector< RooAbsArgPtrOrDouble > const &vec)
Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
Definition: RooArgSet.h:129
~RooArgSet() override
Destructor.
Definition: RooArgSet.cxx:245
bool containsInstance(const RooAbsArg &var) const override
Check if this exact instance is in this collection.
Definition: RooArgSet.h:170
bool snapshot(RooAbsCollection &output, bool deepCopy=true) const
Take a snap shot of current collection contents.
Definition: RooArgSet.h:184
void processArg(const char *name)
Definition: RooArgSet.h:216
RooArgSet(RooArgSet &&other)
Move constructor.
Definition: RooArgSet.h:138
bool canBeAdded(const RooAbsArg &arg, bool silent) const override
Determine whether it's possible to add a given RooAbsArg to the collection or not.
Definition: RooArgSet.h:196
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:305
TObject * create(const char *newname) const override
Definition: RooArgSet.h:150
void processArgs(Args_t &&... args)
Definition: RooArgSet.h:203
void processArg(const RooAbsArg *arg)
Definition: RooArgSet.h:209
void processArg(const RooAbsCollection &coll)
Definition: RooArgSet.h:218
RooArgSet(double arg, Args_t &&... moreArgsOrName)
Definition: RooArgSet.h:104
void processArg(const RooArgList &list)
bool readFromFile(const char *fileName, const char *flagReadAtt=nullptr, const char *section=nullptr, bool verbose=false)
Read contents of the argset from specified file.
Definition: RooArgSet.cxx:321
void processArg(RooAbsArg *var)
Definition: RooArgSet.h:210
const RooFit::UniqueId< RooArgSet > _uniqueId
Definition: RooArgSet.h:231
static void cleanup()
Definition: RooArgSet.cxx:80
virtual bool readFromStream(std::istream &is, bool compact, bool verbose=false)
Shortcut for readFromStream(std::istream&, bool, const char*, const char*, bool), setting flagReadAtt...
Definition: RooArgSet.h:159
RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char *name="")
Construct a (non-owning) RooArgSet from iterators.
Definition: RooArgSet.h:116
void processArg(const RooAbsArg &arg)
Definition: RooArgSet.h:208
RooAbsArg & operator[](const TString &str) const
Get reference to an element using its name.
Definition: RooArgSet.cxx:263
RooArgSet(const RooAbsArg &arg, Args_t &&... moreArgsOrName)
Construct a (non-owning) RooArgSet from one or more RooFit objects.
Definition: RooArgSet.h:82
virtual void writeToStream(std::ostream &os, bool compact, const char *section=nullptr) const
Write the contents of the argset in ASCII form to given stream.
Definition: RooArgSet.cxx:347
Collection abstract base class.
Definition: TCollection.h:65
Mother of all ROOT objects.
Definition: TObject.h:41
Basic string class.
Definition: TString.h:136
Ssiz_t Length() const
Definition: TString.h:410
void(off) SmallVectorTemplateBase< T
RooArgSet S(Args_t &&... args)
Definition: RooArgSet.h:240
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:546
Definition: civetweb.c:1856
static void output()