Logo ROOT   6.10/09
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' RooFit tutorial macro #508
5 ///
6 /// RooArgSet and RooArgList tools and tricks
7 ///
8 /// \macro_output
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
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"
23 using namespace RooFit ;
24 
25 
26 void rf508_listsetmanip()
27 {
28 
29  // C r e a t e d u m m y o b j e c t s
30  // ---------------------------------------
31 
32  // Create some variables
33  RooRealVar a("a","a",1,-10,10) ;
34  RooRealVar b("b","b",2,-10,10) ;
35  RooRealVar c("c","c",3,-10,10) ;
36  RooRealVar d("d","d",4,-10,10) ;
37  RooRealVar x("x","x",0,-10,10) ;
38  c.setError(0.5) ;
39  a.setConstant() ;
40  b.setConstant() ;
41 
42  // Create a category
43  RooCategory e("e","e") ;
44  e.defineType("sig") ;
45  e.defineType("bkg") ;
46 
47  // Create a pdf
48  RooGaussian g("g","g",x,a,b) ;
49 
50 
51 
52  // 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
53  // -------------------------------------------------------
54 
55  // A RooArgSet is a set of RooAbsArg objects. Each object in the set must have
56  // a unique name
57 
58  // Set constructors exists with up to 9 initial arguments
59  RooArgSet s(a,b) ;
60 
61  // At any time objects can be added with add()
62  s.add(e) ;
63 
64  // Add up to 9 additional arguments in one call
65  s.add(RooArgSet(c,d)) ;
66 
67  // Sets can contain any type of RooAbsArg, also pdf and functions
68  s.add(g) ;
69 
70  // Remove element d
71  s.remove(d) ;
72 
73 
74 
75  // 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
76  // -------------------------------------------------------
77 
78  // You can look up objects by name
79  RooAbsArg* aptr = s.find("a") ;
80 
81  // Construct a subset by name
82  RooArgSet* subset1 = (RooArgSet*) s.selectByName("a,b,c") ;
83 
84  // Construct asubset by attribute
85  RooArgSet* subset2 = (RooArgSet*) s.selectByAttrib("Constant",kTRUE) ;
86 
87  // Construct the subset of overlapping contents with another set
88  RooArgSet s1(a,b,c) ;
89  RooArgSet s2(c,d,e) ;
90  RooArgSet* subset3 = (RooArgSet*) s1.selectCommon(s2) ;
91 
92 
93 
94  // O w n i n g R o o A r g S e t s
95  // ---------------------------------
96 
97  // Create a RooArgSet that owns its components
98  // A set either owns all of its components or none,
99  // so once addOwned() is used, add() can no longer be
100  // used and will result in an error message
101 
102  RooRealVar* ac = (RooRealVar*) a.clone("a") ;
103  RooRealVar* bc = (RooRealVar*) b.clone("b") ;
104  RooRealVar* cc = (RooRealVar*) c.clone("c") ;
105 
106  RooArgSet s3 ;
107  s3.addOwned(RooArgSet(*ac,*bc,*cc)) ;
108 
109  // Another possibility is to add an owned clone
110  // of an object instead of the original
111  s3.addClone(RooArgSet(d,e,g)) ;
112 
113  // A clone of a owning set is non-owning and its
114  // contents is owned by the originating owning set
115  RooArgSet* sclone = (RooArgSet*) s3.Clone("sclone") ;
116 
117  // To make a clone of a set and its contents use
118  // the snapshot method
119  RooArgSet* sclone2 = (RooArgSet*) s3.snapshot() ;
120 
121  // If a set contains function objects, only the head node
122  // is cloned in a snapshot. To make a snapshot of all
123  // servers of a function object do as follows. The result
124  // of a RooArgSet snapshot with deepCloning option is a set
125  // of cloned objects, and all their clone (recursive) server
126  // dependencies, that together form a self-consistent
127  // set that is free of external dependencies
128 
129  RooArgSet* sclone3 = (RooArgSet*) s3.snapshot(kTRUE) ;
130 
131 
132 
133  // S e t p r i n t i n g
134  // ------------------------
135 
136  // Inline printing only show list of names of contained objects
137  cout << "sclone = " << (*sclone) << endl ;
138 
139  // Plain print shows the same, prefixed by name of the set
140  sclone->Print() ;
141 
142  // Standard printing shows one line for each item with the items name, class name and value
143  sclone->Print("s") ;
144 
145  // Verbose printing adds each items arguments, address and 'extras' as defined by the object
146  sclone->Print("v") ;
147 
148 
149 
150  // U s i n g R o o A r g L i s t s
151  // ---------------------------------
152 
153  // List constructors exists with up to 9 initial arguments
154  RooArgList l(a,b,c,d) ;
155 
156  // Lists have an explicit order and allow multiple arguments with the same name
157  l.add(RooArgList(a,b,c,d)) ;
158 
159  // Access by index is provided
160  RooAbsArg* arg4 = l.at(4) ;
161 
162 
163 }
virtual TObject * clone(const char *newname) const
Definition: RooRealVar.h:47
RooAbsCollection * selectCommon(const RooAbsCollection &refColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
TArc * a
Definition: textangle.C:12
Double_t x[n]
Definition: legend1.C:17
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooAbsCollection * selectByAttrib(const char *name, Bool_t value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
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:94
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
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:90
TLine * l
Definition: textangle.C:4
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
RooAbsCollection * selectByName(const char *nameList, Bool_t verbose=kFALSE) const
Create a subset of the current collection, consisting only of those elements with names matching the ...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: RtypesCore.h:91