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