Logo ROOT  
Reference Guide
rf508_listsetmanip.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Organization and simultaneous fits: RooArgSet and RooArgList tools and tricks
5///
6/// \macro_output
7/// \macro_code
8///
9/// \date July 2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooConstVar.h"
16#include "TCanvas.h"
17#include "TAxis.h"
18#include "RooPlot.h"
19#include "RooArgSet.h"
20#include "RooArgList.h"
21#include "RooCategory.h"
22using namespace RooFit;
23
24void rf508_listsetmanip()
25{
26
27 // C r e a t e d u m m y o b j e c t s
28 // ---------------------------------------
29
30 // Create some variables
31 RooRealVar a("a", "a", 1, -10, 10);
32 RooRealVar b("b", "b", 2, -10, 10);
33 RooRealVar c("c", "c", 3, -10, 10);
34 RooRealVar d("d", "d", 4, -10, 10);
35 RooRealVar x("x", "x", 0, -10, 10);
36 c.setError(0.5);
37 a.setConstant();
38 b.setConstant();
39
40 // Create a category
41 RooCategory e("e", "e");
42 e.defineType("sig");
43 e.defineType("bkg");
44
45 // Create a pdf
46 RooGaussian g("g", "g", x, a, b);
47
48 // C r e a t i n g , f i l l i n g R o o A r g S e t s
49 // -------------------------------------------------------
50
51 // A RooArgSet is a set of RooAbsArg objects. Each object in the set must have
52 // a unique name
53
54 // Set constructors exists with up to 9 initial arguments
55 RooArgSet s(a, b);
56
57 // At any time objects can be added with add()
58 s.add(e);
59
60 // Add up to 9 additional arguments in one call
61 s.add(RooArgSet(c, d));
62
63 // Sets can contain any type of RooAbsArg, also pdf and functions
64 s.add(g);
65
66 // Remove element d
67 s.remove(d);
68
69 // A c c e s s i n g R o o A r g S e t c o n t e n t s
70 // -------------------------------------------------------
71
72 // You can look up objects by name
73 RooAbsArg *aptr = s.find("a");
74
75 // Construct a subset by name
76 RooArgSet *subset1 = (RooArgSet *)s.selectByName("a,b,c");
77
78 // Construct asubset by attribute
79 RooArgSet *subset2 = (RooArgSet *)s.selectByAttrib("Constant", kTRUE);
80
81 // Construct the subset of overlapping contents with another set
82 RooArgSet s1(a, b, c);
83 RooArgSet s2(c, d, e);
84 RooArgSet *subset3 = (RooArgSet *)s1.selectCommon(s2);
85
86 // O w n i n g R o o A r g S e t s
87 // ---------------------------------
88
89 // Create a RooArgSet that owns its components
90 // A set either owns all of its components or none,
91 // so once addOwned() is used, add() can no longer be
92 // used and will result in an error message
93
94 RooRealVar *ac = (RooRealVar *)a.clone("a");
95 RooRealVar *bc = (RooRealVar *)b.clone("b");
96 RooRealVar *cc = (RooRealVar *)c.clone("c");
97
98 RooArgSet s3;
99 s3.addOwned(RooArgSet(*ac, *bc, *cc));
100
101 // Another possibility is to add an owned clone
102 // of an object instead of the original
103 s3.addClone(RooArgSet(d, e, g));
104
105 // A clone of a owning set is non-owning and its
106 // contents is owned by the originating owning set
107 RooArgSet *sclone = (RooArgSet *)s3.Clone("sclone");
108
109 // To make a clone of a set and its contents use
110 // the snapshot method
111 RooArgSet *sclone2 = (RooArgSet *)s3.snapshot();
112
113 // If a set contains function objects, only the head node
114 // is cloned in a snapshot. To make a snapshot of all
115 // servers of a function object do as follows. The result
116 // of a RooArgSet snapshot with deepCloning option is a set
117 // of cloned objects, and all their clone (recursive) server
118 // dependencies, that together form a self-consistent
119 // set that is free of external dependencies
120
121 RooArgSet *sclone3 = (RooArgSet *)s3.snapshot(kTRUE);
122
123 // S e t p r i n t i n g
124 // ------------------------
125
126 // Inline printing only show list of names of contained objects
127 cout << "sclone = " << (*sclone) << endl;
128
129 // Plain print shows the same, prefixed by name of the set
130 sclone->Print();
131
132 // Standard printing shows one line for each item with the items name, class name and value
133 sclone->Print("s");
134
135 // Verbose printing adds each items arguments, address and 'extras' as defined by the object
136 sclone->Print("v");
137
138 // U s i n g R o o A r g L i s t s
139 // ---------------------------------
140
141 // List constructors exists with up to 9 initial arguments
142 RooArgList l(a, b, c, d);
143
144 // Lists have an explicit order and allow multiple arguments with the same name
145 l.add(RooArgList(a, b, c, d));
146
147 // Access by index is provided
148 RooAbsArg *arg4 = l.at(4);
149}
#define d(i)
Definition: RSha256.hxx:102
#define b(i)
Definition: RSha256.hxx:100
#define c(i)
Definition: RSha256.hxx:101
#define g(i)
Definition: RSha256.hxx:105
#define s1(x)
Definition: RSha256.hxx:91
#define e(i)
Definition: RSha256.hxx:103
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:118
RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE) override
Add clone of specified element to an owning set.
Definition: RooArgSet.cxx:288
Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE) override
Add element to an owning set.
Definition: RooArgSet.cxx:274
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
static constexpr double s
auto * l
Definition: textangle.C:4
auto * a
Definition: textangle.C:12