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