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 "RooAbsArg.h"
21#include "UniqueId.h"
22
23
24class RooArgList ;
25
26// Use a memory pool for RooArgSet.
27// RooFit assumes (e.g. for caching results) that arg sets that have the same pointer have
28// the same contents. Trying to remove that memory pool lead to wrong results, because the
29// OS *occasionally* returns the same address, and the caching goes wrong.
30// It's hard to track down, so disable this only when e.g. looking for memory leaks!
31#define USEMEMPOOLFORARGSET
32template <class RooSet_t, size_t>
34
36public:
37
38#ifdef USEMEMPOOLFORARGSET
39 void* operator new (size_t bytes);
40 void* operator new (size_t bytes, void* ptr) noexcept;
41 void operator delete (void *ptr);
42#endif
43
44 // Constructors, assignment etc.
45 RooArgSet();
46
47 /// Construct a (non-owning) RooArgSet from one or more
48 /// RooFit objects. The set will not own its contents.
49 /// \tparam Ts Parameter pack of objects that derive from RooAbsArg or RooFit collections; or a name.
50 /// \param arg A RooFit object.
51 /// Note that you can also pass a `double` as first argument
52 /// when constructing a RooArgSet, and another templated
53 /// constructor will be used where a RooConstVar is implicitly
54 /// created from the `double` value.
55 /// \param moreArgsOrName Arbitrary number of
56 /// - Further RooFit objects that derive from RooAbsArg
57 /// - RooFit collections of such objects
58 /// - `double`s from which a RooConstVar is implicitly created via `RooFit::RooConst`.
59 /// - A name for the set. Given multiple names, the last-given name prevails.
60 template<typename... Args_t>
61 RooArgSet(const RooAbsArg& arg, Args_t &&... moreArgsOrName)
62 /*NB: Making this a delegating constructor led to linker errors with MSVC*/
63 {
64 // This constructor should cause a failed static_assert if any of the input
65 // arguments is a temporary (r-value reference), which will be checked in
66 // processArg. This works statically because of the universal reference
67 // mechanism with templated functions.
68 // Unfortunately, we can't check the first arg, because it's type can't be
69 // a template parameter and hence a universal reference can't be used.
70 // This problem is solved by introducing another templated constructor below,
71 // which accepts a RooAbsArg && as the first argument which is forwarded to
72 // be the second argument for this constructor.
73 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
74 }
75
76 /// This constructor will provoke a `static_assert`, because passing a
77 /// RooAbsArg as r-value reference is not allowed.
78 template<typename... Args_t>
79 RooArgSet(RooAbsArg && arg, Args_t &&... moreArgsOrName)
80 : RooArgSet{arg, std::move(arg), std::forward<Args_t>(moreArgsOrName)...} {}
81
82 template<typename... Args_t>
83 explicit RooArgSet(double arg, Args_t &&... moreArgsOrName) {
84 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
85 }
86
87 /// Construct a (non-owning) RooArgSet from iterators.
88 /// \tparam Iterator_t An iterator pointing to RooFit objects or to pointers/references of those.
89 /// \param beginIt Iterator to first element to add.
90 /// \param endIt Iterator to end of range to be added.
91 /// \param name Optional name of the collection.
92 template<typename Iterator_t,
93 typename value_type = typename std::remove_pointer<typename std::iterator_traits<Iterator_t>::value_type>::type,
94 typename = std::enable_if<std::is_convertible<const value_type*, const RooAbsArg*>::value> >
95 RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char* name="") :
97 for (auto it = beginIt; it != endIt; ++it) {
98 processArg(*it);
99 }
100 }
101
102 /// Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
103 /// This constructor is mainly intended for pyROOT. With cppyy, a Python list
104 /// or tuple can be implicitly converted to an std::vector, and by enabling
105 /// implicit construction of a RooArgSet from a std::vector, we indirectly
106 /// enable implicit conversion from a Python list/tuple to RooArgSets.
107 /// \param vec A vector with pointers to the arguments or doubles for RooFit::RooConst().
108 RooArgSet(std::vector<RooAbsArgPtrOrDouble> const& vec) {
109 for(auto const& arg : vec) {
110 if(arg.hasPtr) processArg(arg.ptr);
111 else processArg(arg.val);
112 }
113 }
114
115 RooArgSet(const RooArgSet& other, const char *name="");
116 /// Move constructor.
117 RooArgSet(RooArgSet && other) : RooAbsCollection(std::move(other)) {}
118
119 RooArgSet(const RooArgSet& set1, const RooArgSet& set2,
120 const char *name="");
121
122 RooArgSet(const RooAbsCollection& coll) ;
123 RooArgSet(const RooAbsCollection& collection, const RooAbsArg* var1);
124 explicit RooArgSet(const TCollection& tcoll, const char* name="") ;
125 explicit RooArgSet(const char *name);
126
127 ~RooArgSet() override;
128 TObject* clone(const char* newname) const override { return new RooArgSet(*this,newname); }
129 TObject* create(const char* newname) const override { return new RooArgSet(newname); }
130 RooArgSet& operator=(const RooArgSet& other) { RooAbsCollection::operator=(other) ; return *this ;}
131
132 using RooAbsCollection::operator[];
133 RooAbsArg& operator[](const TString& str) const;
134
135
136 /// Shortcut for readFromStream(std::istream&, Bool_t, const char*, const char*, Bool_t), setting
137 /// `flagReadAtt` and `section` to 0.
138 virtual bool readFromStream(std::istream& is, bool compact, bool verbose=false) {
139 // I/O streaming interface (machine readable)
140 return readFromStream(is, compact, 0, 0, verbose) ;
141 }
142 Bool_t readFromStream(std::istream& is, Bool_t compact, const char* flagReadAtt, const char* section, Bool_t verbose=kFALSE) ;
143 virtual void writeToStream(std::ostream& os, bool compact, const char* section=0) const;
144 void writeToFile(const char* fileName) const ;
145 Bool_t readFromFile(const char* fileName, const char* flagReadAtt=0, const char* section=0, Bool_t verbose=kFALSE) ;
146
147
148
149 static void cleanup() ;
150
151 Bool_t isInRange(const char* rangeSpec) ;
152
153 /// Use RooAbsCollection::snapshot(), but return as RooArgSet.
154 RooArgSet * snapshot(bool deepCopy = true) const {
155 return static_cast<RooArgSet*>(RooAbsCollection::snapshot(deepCopy));
156 }
157
158 /// \copydoc RooAbsCollection::snapshot()
160 return RooAbsCollection::snapshot(output, deepCopy);
161 }
162
163 /// Returns a unique ID that is different for every instantiated RooArgSet.
164 /// This ID can be used to check whether two RooAbsData are the same object,
165 /// which is safer than memory address comparisons that might result in false
166 /// positives when memory is recycled.
167 UniqueId<RooArgSet> const& uniqueId() const { return _uniqueId; }
168
169protected:
170 Bool_t checkForDup(const RooAbsArg& arg, Bool_t silent) const ;
171 virtual bool canBeAdded(const RooAbsArg& arg, bool silent) const override {
172 return !checkForDup(arg, silent);
173 }
174
175private:
176
177 template<typename... Args_t>
178 void processArgs(Args_t &&... args) {
179 // Expand parameter pack in C++ 11 way:
180 int dummy[] = { 0, (processArg(std::forward<Args_t>(args)), 0) ... };
181 (void)dummy;
182 }
183 void processArg(const RooAbsArg& arg) { add(arg); }
184 void processArg(const RooAbsArg* arg) { add(*arg); }
185 void processArg(RooAbsArg* var) { add(*var); }
186 template<class Arg_t>
187 void processArg(Arg_t && arg) {
188 assert_is_no_temporary(std::forward<Arg_t>(arg));
189 add(arg);
190 }
191 void processArg(const char* name) { _name = name; }
192 void processArg(double value);
193 void processArg(const RooAbsCollection& coll) { add(coll); if (_name.Length() == 0) _name = coll.GetName(); }
194 // this overload with r-value references is needed so we don't trigger the
195 // templated function with the failing static_assert for r-value references
196 void processArg(RooAbsCollection && coll) { processArg(coll); }
197 void processArg(const RooArgList& list);
198
199#ifdef USEMEMPOOLFORARGSET
200 typedef MemPoolForRooSets<RooArgSet, 10*600> MemPool; //600 = about 100 kb
201 //Initialise a static mem pool. It has to happen inside a function to solve the
202 //static initialisation order fiasco. At the end of the program, this might have
203 //to leak depending if RooArgSets are still alive. This depends on the order of destructions.
204 static MemPool* memPool();
205#endif
207
208 ClassDefOverride(RooArgSet,1) // Set of RooAbsArg objects
209};
210
211
212namespace RooFitShortHand {
213
214template<class... Args_t>
215RooArgSet S(Args_t&&... args) {
216 return {std::forward<Args_t>(args)...};
217}
218
219} // namespace RooFitShortHand
220
221
222#endif
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassDefOverride(name, id)
Definition: Rtypes.h:329
char name[80]
Definition: TGX11.cxx:110
int type
Definition: TGX11.cxx:121
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:72
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
static void assert_is_no_temporary(T &&)
const char * GetName() const
Returns name of object.
RooAbsCollection & operator=(const RooAbsCollection &other)
Assign values from the elements in other to our elements.
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:35
void processArg(RooAbsCollection &&coll)
Definition: RooArgSet.h:196
TObject * clone(const char *newname) const override
Definition: RooArgSet.h:128
RooArgSet & operator=(const RooArgSet &other)
Definition: RooArgSet.h:130
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:79
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:154
void processArg(Arg_t &&arg)
Definition: RooArgSet.h:187
RooArgSet()
Default constructor.
Definition: RooArgSet.cxx:146
RooArgSet(std::vector< RooAbsArgPtrOrDouble > const &vec)
Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
Definition: RooArgSet.h:108
~RooArgSet() override
Destructor.
Definition: RooArgSet.cxx:241
Bool_t snapshot(RooAbsCollection &output, Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents.
Definition: RooArgSet.h:159
Bool_t readFromFile(const char *fileName, const char *flagReadAtt=0, const char *section=0, Bool_t verbose=kFALSE)
Read contents of the argset from specified file.
Definition: RooArgSet.cxx:317
Bool_t isInRange(const char *rangeSpec)
Definition: RooArgSet.cxx:615
UniqueId< RooArgSet > const & uniqueId() const
Returns a unique ID that is different for every instantiated RooArgSet.
Definition: RooArgSet.h:167
virtual void writeToStream(std::ostream &os, bool compact, const char *section=0) const
Write the contents of the argset in ASCII form to given stream.
Definition: RooArgSet.cxx:343
void processArg(const char *name)
Definition: RooArgSet.h:191
RooArgSet(RooArgSet &&other)
Move constructor.
Definition: RooArgSet.h:117
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:301
TObject * create(const char *newname) const override
Definition: RooArgSet.h:129
static MemPool * memPool()
void processArgs(Args_t &&... args)
Definition: RooArgSet.h:178
virtual 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:171
void processArg(const RooAbsArg *arg)
Definition: RooArgSet.h:184
void processArg(const RooAbsCollection &coll)
Definition: RooArgSet.h:193
const UniqueId< RooArgSet > _uniqueId
Definition: RooArgSet.h:206
RooArgSet(double arg, Args_t &&... moreArgsOrName)
Definition: RooArgSet.h:83
MemPoolForRooSets< RooArgSet, 10 *600 > MemPool
Definition: RooArgSet.h:200
void processArg(const RooArgList &list)
void processArg(RooAbsArg *var)
Definition: RooArgSet.h:185
static void cleanup()
Definition: RooArgSet.cxx:73
Bool_t checkForDup(const RooAbsArg &arg, Bool_t silent) const
Check if element with var's name is already in set.
Definition: RooArgSet.cxx:274
virtual bool readFromStream(std::istream &is, bool compact, bool verbose=false)
Shortcut for readFromStream(std::istream&, Bool_t, const char*, const char*, Bool_t),...
Definition: RooArgSet.h:138
RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char *name="")
Construct a (non-owning) RooArgSet from iterators.
Definition: RooArgSet.h:95
void processArg(const RooAbsArg &arg)
Definition: RooArgSet.h:183
RooAbsArg & operator[](const TString &str) const
Get reference to an element using its name.
Definition: RooArgSet.cxx:259
RooArgSet(const RooAbsArg &arg, Args_t &&... moreArgsOrName)
Construct a (non-owning) RooArgSet from one or more RooFit objects.
Definition: RooArgSet.h:61
Collection abstract base class.
Definition: TCollection.h:65
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:136
Ssiz_t Length() const
Definition: TString.h:410
RooArgSet S(Args_t &&... args)
Definition: RooArgSet.h:215
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:546
A UniqueId can be added as a class member to enhance any class with a unique identifier for each inst...
Definition: UniqueId.h:27
Definition: civetweb.c:2228
static void output(int code)
Definition: gifencode.c:226