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