Logo ROOT   6.16/01
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 consolidates 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 as modification
26of its state will back propagate into a modification of its input categories.
27To define a consolidated category of multiple non-lvalye categories
28use class RooMultiCategory
29RooSuperCategory state 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 ;
73
75}
76
77
78
79////////////////////////////////////////////////////////////////////////////////
80/// Copy constructor
81
83 RooAbsCategoryLValue(other,name), _catSet("input",this,other._catSet)
84{
87 setIndex(other.getIndex()) ;
88}
89
90
91
92////////////////////////////////////////////////////////////////////////////////
93/// Destructor
94
96{
97 delete _catIter ;
98}
99
100
101
102////////////////////////////////////////////////////////////////////////////////
103/// Make an iterator over all state permutations of
104/// the input categories of this supercategory
105
107{
108 return new RooMultiCatIter(_catSet) ;
109}
110
111
112
113////////////////////////////////////////////////////////////////////////////////
114/// Update the list of possible states of this super category
115
117{
118 clearTypes() ;
119
120 RooMultiCatIter mcIter(_catSet) ;
121 TObjString* obj ;
122 Int_t i(0) ;
123 while((obj = (TObjString*) mcIter.Next())) {
124 // Register composite label
125 defineTypeUnchecked(obj->String(),i++) ;
126 }
127
128 // Renumbering will invalidate cache
129 setValueDirty() ;
130}
131
132
133
134////////////////////////////////////////////////////////////////////////////////
135/// Return the name of the current state,
136/// constructed from the state names of the input categories
137
139{
140 _catIter->Reset() ;
141
142 // Construct composite label name
143 TString label ;
144 RooAbsCategory* cat ;
146 while((cat=(RooAbsCategory*) _catIter->Next())) {
147 label.Append(first?"{":";") ;
148 label.Append(cat->getLabel()) ;
149 first=kFALSE ;
150 }
151 label.Append("}") ;
152
153 return label ;
154}
155
156
157
158////////////////////////////////////////////////////////////////////////////////
159/// Calculate and return the current value
160
162{
163 if (isShapeDirty()) {
164 const_cast<RooSuperCategory*>(this)->updateIndexList() ;
165 }
166 const RooCatType* ret = lookupType(currentLabel(),kTRUE) ;
167 if (!ret) {
168 coutE(Eval) << "RooSuperCat::evaluate(" << this << ") error: current state not defined: '" << currentLabel() << "'" << endl ;
170 return RooCatType() ;
171 }
172 return *ret ;
173}
174
175
176
177////////////////////////////////////////////////////////////////////////////////
178/// Set the value of the super category by specifying the state index code
179/// by setting the states of the corresponding input category lvalues
180
182{
183 const RooCatType* type = lookupType(index,kTRUE) ;
184 if (!type) return kTRUE ;
185 return setType(type) ;
186}
187
188
189
190////////////////////////////////////////////////////////////////////////////////
191/// Set the value of the super category by specifying the state name
192/// by setting the state names of the corresponding input category lvalues
193
194Bool_t RooSuperCategory::setLabel(const char* label, Bool_t /*printError*/)
195{
196 const RooCatType* type = lookupType(label,kTRUE) ;
197 if (!type) return kTRUE ;
198 return setType(type) ;
199}
200
201
202
203////////////////////////////////////////////////////////////////////////////////
204/// Set the value of the super category by specifying the state object
205/// by setting the state names of the corresponding input category lvalues
206
208{
209 char buf[1024] ;
210 strlcpy(buf,type->GetName(),1024) ;
211
213 Bool_t error(kFALSE) ;
214
215 // Parse composite label and set label of components to their values
216 char* ptr=buf+1 ;
217 char* token = ptr ;
218 _catIter->Reset() ;
219 while ((arg=(RooAbsCategoryLValue*)_catIter->Next())) {
220
221 // Delimit name token for this category
222 if (*ptr=='{') {
223 // Token is composite itself, terminate at matching '}'
224 Int_t nBrak(1) ;
225 while(*(++ptr)) {
226 if (nBrak==0) {
227 *ptr = 0 ;
228 break ;
229 }
230 if (*ptr=='{') {
231 nBrak++ ;
232 } else if (*ptr=='}') {
233 nBrak-- ;
234 }
235 }
236 } else {
237 // Simple token, terminate at next semi-colon
238 ptr = strtok(ptr,";}") ;
239 ptr += strlen(ptr) ;
240 }
241
242 error |= arg->setLabel(token) ;
243 token = ++ptr ;
244 }
245
246 return error ;
247}
248
249
250
251////////////////////////////////////////////////////////////////////////////////
252/// Print the state of this object to the specified output stream.
253
254void RooSuperCategory::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
255{
256 RooAbsCategory::printMultiline(os,content,verbose,indent) ;
257
258 if (verbose) {
259 os << indent << "--- RooSuperCategory ---" << endl;
260 os << indent << " Input category list:" << endl ;
261 TString moreIndent(indent) ;
262 os << moreIndent << _catSet << endl ;
263 }
264}
265
266
267
268////////////////////////////////////////////////////////////////////////////////
269/// Read object contents from given stream
270
271Bool_t RooSuperCategory::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
272{
273 return kTRUE ;
274}
275
276
277
278////////////////////////////////////////////////////////////////////////////////
279/// Write object contents to given stream
280
281void RooSuperCategory::writeToStream(ostream& os, Bool_t compact) const
282{
284}
285
286
287
288////////////////////////////////////////////////////////////////////////////////
289/// Return true of all of the input category states are in the given range
290
291Bool_t RooSuperCategory::inRange(const char* rangeName) const
292{
293 _catIter->Reset() ;
295 while((cat = (RooAbsCategoryLValue*)_catIter->Next())) {
296 if (!cat->inRange(rangeName)) {
297 return kFALSE ;
298 }
299 }
300 return kTRUE ;
301}
302
303
304
305////////////////////////////////////////////////////////////////////////////////
306/// Return true if any of the input categories has a range
307/// named 'rangeName'
308
309Bool_t RooSuperCategory::hasRange(const char* rangeName) const
310{
311 _catIter->Reset() ;
313 while((cat = (RooAbsCategoryLValue*)_catIter->Next())) {
314 if (cat->hasRange(rangeName)) return kTRUE ;
315 }
316
317 return kFALSE ;
318}
void Class()
Definition: Class.C:29
#define ccoutE(a)
Definition: RooMsgService.h:41
#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:363
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:66
Bool_t isShapeDirty() const
Definition: RooAbsArg.h:331
virtual Bool_t inRange(const char *) const
Definition: RooAbsArg.h:289
void setValueDirty() const
Definition: RooAbsArg.h:441
virtual Bool_t hasRange(const char *) const
Definition: RooAbsArg.h:293
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)=0
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 const char * getLabel() const
Return label string 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
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 consolidates 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.
TIterator * _catIter
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
Iterator over set of input categories.
Iterator abstract base class.
Definition: TIterator.h:30
virtual void Reset()=0
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:49
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
@ InputArguments
Definition: RooGlobalFunc.h:58
Definition: first.py:1
STL namespace.