Logo ROOT  
Reference Guide
RooSuperCategory.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18\file RooSuperCategory.cxx
19\class RooSuperCategory
20\ingroup Roofitcore
21
22RooSuperCategory can join several RooAbsCategoryLValue objects into
23a single category. The states of the super category consist of all the permutations
24of the input categories. The super category is an lvalue and requires that
25all input categories are lvalues as well. This is because a modification
26of its state will back propagate into a modification of its input categories.
27To define a joined category of multiple non-lvalue categories,
28use the class RooMultiCategory.
29RooSuperCategory states are automatically defined and updated whenever an input
30category modifies its list of states.
31**/
32
33#include "RooFit.h"
34
35#include "Riostream.h"
36#include "Riostream.h"
37#include <stdlib.h>
38#include "TString.h"
39#include "TClass.h"
40#include "RooSuperCategory.h"
41#include "RooStreamParser.h"
42#include "RooArgSet.h"
43#include "RooMultiCatIter.h"
45#include "RooMsgService.h"
46
47using namespace std;
48
50;
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Construct a lvalue product of the given set of input RooAbsCategoryLValues in 'inInputCatList'
55/// The state names of this product category are {S1;S2,S3,...Sn} where Si are the state names
56/// of the input categories. A RooSuperCategory is an lvalue.
57
58RooSuperCategory::RooSuperCategory(const char *name, const char *title, const RooArgSet& inInputCatList) :
59 RooAbsCategoryLValue(name, title), _catSet("input","Input category set",this,kTRUE,kTRUE)
60{
61 // Copy category list
62 TIterator* iter = inInputCatList.createIterator() ;
63 RooAbsArg* arg ;
64 while ((arg=(RooAbsArg*)iter->Next())) {
65 if (!arg->IsA()->InheritsFrom(RooAbsCategoryLValue::Class())) {
66 coutE(InputArguments) << "RooSuperCategory::RooSuperCategory(" << GetName() << "): input category " << arg->GetName()
67 << " is not an lvalue" << endl ;
68 }
69 _catSet.add(*arg) ;
70 }
71 delete iter ;
72
74}
75
76
77
78////////////////////////////////////////////////////////////////////////////////
79/// Copy constructor
80
82 RooAbsCategoryLValue(other,name), _catSet("input",this,other._catSet)
83{
85 setIndex(other.getIndex()) ;
86}
87
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// Destructor
92
94{
95
96}
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Make an iterator over all state permutations of
102/// the input categories of this supercategory
103
105{
106 return new RooMultiCatIter(_catSet) ;
107}
108
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// Update the list of possible states of this super category
113
115{
116 clearTypes() ;
117
118 RooMultiCatIter mcIter(_catSet) ;
119 TObjString* obj ;
120 Int_t i(0) ;
121 while((obj = (TObjString*) mcIter.Next())) {
122 // Register composite label
123 defineTypeUnchecked(obj->String(),i++) ;
124 }
125
126 // Renumbering will invalidate cache
127 setValueDirty() ;
128}
129
130
131
132////////////////////////////////////////////////////////////////////////////////
133/// Return the name of the current state,
134/// constructed from the state names of the input categories
135
137{
138
139 // Construct composite label name
140 TString label ;
142 for (const auto c : _catSet) {
143 auto cat = static_cast<RooAbsCategory*>(c);
144
145 label.Append(first?"{":";") ;
146 label.Append(cat->getLabel()) ;
147 first=kFALSE ;
148 }
149 label.Append("}") ;
150
151 return label ;
152}
153
154
155
156////////////////////////////////////////////////////////////////////////////////
157/// Calculate and return the current value
158
160{
161 if (isShapeDirty()) {
162 const_cast<RooSuperCategory*>(this)->updateIndexList() ;
163 }
164 const RooCatType* ret = lookupType(currentLabel(),kTRUE) ;
165 if (!ret) {
166 coutE(Eval) << "RooSuperCat::evaluate(" << this << ") error: current state not defined: '" << currentLabel() << "'" << endl ;
168 return RooCatType() ;
169 }
170 return *ret ;
171}
172
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// Set the value of the super category by specifying the state index code
177/// by setting the states of the corresponding input category lvalues
178
180{
181 const RooCatType* type = lookupType(index,kTRUE) ;
182 if (!type) return kTRUE ;
183 return setType(type) ;
184}
185
186
187
188////////////////////////////////////////////////////////////////////////////////
189/// Set the value of the super category by specifying the state name
190/// by setting the state names of the corresponding input category lvalues
191
192Bool_t RooSuperCategory::setLabel(const char* label, Bool_t /*printError*/)
193{
194 const RooCatType* type = lookupType(label,kTRUE) ;
195 if (!type) return kTRUE ;
196 return setType(type) ;
197}
198
199
200
201////////////////////////////////////////////////////////////////////////////////
202/// Set the value of the super category by specifying the state object
203/// by setting the state names of the corresponding input category lvalues
204
206{
207 char buf[1024] ;
208 strlcpy(buf,type->GetName(),1024) ;
209
210 Bool_t error(kFALSE) ;
211
212 // Parse composite label and set label of components to their values
213 char* ptr=buf+1 ;
214 char* token = ptr ;
215 for (const auto c : _catSet) {
216 auto arg = static_cast<RooAbsCategoryLValue*>(c);
217
218 // Delimit name token for this category
219 if (*ptr=='{') {
220 // Token is composite itself, terminate at matching '}'
221 Int_t nBrak(1) ;
222 while(*(++ptr)) {
223 if (nBrak==0) {
224 *ptr = 0 ;
225 break ;
226 }
227 if (*ptr=='{') {
228 nBrak++ ;
229 } else if (*ptr=='}') {
230 nBrak-- ;
231 }
232 }
233 } else {
234 // Simple token, terminate at next semi-colon
235 ptr = strtok(ptr,";}") ;
236 ptr += strlen(ptr) ;
237 }
238
239 error |= arg->setLabel(token) ;
240 token = ++ptr ;
241 }
242
243 return error ;
244}
245
246
247
248////////////////////////////////////////////////////////////////////////////////
249/// Print the state of this object to the specified output stream.
250
252{
254
255 if (verbose) {
256 os << indent << "--- RooSuperCategory ---" << endl;
257 os << indent << " Input category list:" << endl ;
258 TString moreIndent(indent) ;
259 os << moreIndent << _catSet << endl ;
260 }
261}
262
263
264
265////////////////////////////////////////////////////////////////////////////////
266/// Read object contents from given stream
267
268Bool_t RooSuperCategory::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
269{
270 return kTRUE ;
271}
272
273
274
275////////////////////////////////////////////////////////////////////////////////
276/// Write object contents to given stream
277
278void RooSuperCategory::writeToStream(ostream& os, Bool_t compact) const
279{
281}
282
283
284
285////////////////////////////////////////////////////////////////////////////////
286/// Return true of all of the input category states are in the given range
287
288Bool_t RooSuperCategory::inRange(const char* rangeName) const
289{
290 for (const auto c : _catSet) {
291 auto cat = static_cast<RooAbsCategoryLValue*>(c);
292 if (!cat->inRange(rangeName)) {
293 return kFALSE ;
294 }
295 }
296 return kTRUE ;
297}
298
299
300
301////////////////////////////////////////////////////////////////////////////////
302/// Return true if any of the input categories has a range
303/// named 'rangeName'
304
305Bool_t RooSuperCategory::hasRange(const char* rangeName) const
306{
307 for (const auto c : _catSet) {
308 auto cat = static_cast<RooAbsCategoryLValue*>(c);
309 if (cat->hasRange(rangeName)) return kTRUE ;
310 }
311
312 return kFALSE ;
313}
void Class()
Definition: Class.C:29
#define c(i)
Definition: RSha256.hxx:101
#define ccoutE(a)
Definition: RooMsgService.h:42
#define coutE(a)
Definition: RooMsgService.h:34
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:71
Bool_t isShapeDirty() const
Definition: RooAbsArg.h:385
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:466
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
virtual Int_t getIndex() const
Return index number of current state.
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
const RooCatType * defineTypeUnchecked(const char *label, Int_t index)
Internal version of defineType that does not check if type already exists.
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
const RooCatType * lookupType(Int_t index, Bool_t printError=kFALSE) const
Find our type corresponding to the specified index, or return 0 for no match.
void clearTypes()
Delete all currently defined states.
TIterator * createIterator(Bool_t dir=kIterForward) const R__SUGGEST_ALTERNATIVE("begin()
TIterator-style iteration over contained elements.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
Definition: RooCatType.h:22
RooMultiCatIter iterators over all state permutations of a list of categories.
virtual TObject * Next()
Iterator increment operator.
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer,...
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Overloaded RooArgSet::add() method inserts 'var' into set and registers 'var' as server to owner with...
RooSuperCategory can join several RooAbsCategoryLValue objects into a single category.
virtual Bool_t inRange(const char *rangeName) const
Return true of all of the input category states are in the given range.
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
TString currentLabel() const
Return the name of the current state, constructed from the state names of the input categories.
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set the value of the super category by specifying the state index code by setting the states of the c...
Bool_t setType(const RooCatType *type, Bool_t prinError=kTRUE)
Set the value of the super category by specifying the state object by setting the state names of the ...
virtual ~RooSuperCategory()
Destructor.
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)
Set the value of the super category by specifying the state name by setting the state names of the co...
TIterator * MakeIterator() const
Make an iterator over all state permutations of the input categories of this supercategory.
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
RooSetProxy _catSet
virtual Bool_t hasRange(const char *rangeName) const
Return true if any of the input categories has a range named 'rangeName'.
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print the state of this object to the specified output stream.
void updateIndexList()
Update the list of possible states of this super category.
virtual RooCatType evaluate() const
Calculate and return the current value.
Iterator abstract base class.
Definition: TIterator.h:30
virtual TObject * Next()=0
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Collectable string class.
Definition: TObjString.h:28
TString & String()
Definition: TObjString.h:48
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
Basic string class.
Definition: TString.h:131
TString & Append(const char *cs)
Definition: TString.h:559
@ InputArguments
Definition: RooGlobalFunc.h:68
Definition: first.py:1