Logo ROOT   6.14/05
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 
22 RooSuperCategory consolidates several RooAbsCategoryLValue objects into
23 a single category. The states of the super category consist of all the permutations
24 of the input categories. The super category is an lvalue and requires that
25 all input categories are lvalues as well as modification
26 of its state will back propagate into a modification of its input categories.
27 To define a consolidated category of multiple non-lvalye categories
28 use class RooMultiCategory
29 RooSuperCategory state are automatically defined and updated whenever an input
30 category 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"
44 #include "RooAbsCategoryLValue.h"
45 #include "RooMsgService.h"
46 
47 using 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 
58 RooSuperCategory::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 
74  updateIndexList() ;
75 }
76 
77 
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// Copy constructor
81 
83  RooAbsCategoryLValue(other,name), _catSet("input",this,other._catSet)
84 {
86  updateIndexList() ;
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 ;
145  Bool_t first(kTRUE) ;
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 
194 Bool_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 
212  RooAbsCategoryLValue* arg ;
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 
254 void 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 
271 Bool_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 
281 void RooSuperCategory::writeToStream(ostream& os, Bool_t compact) const
282 {
283  RooAbsCategory::writeToStream(os,compact) ;
284 }
285 
286 
287 
288 ////////////////////////////////////////////////////////////////////////////////
289 /// Return true of all of the input category states are in the given range
290 
291 Bool_t RooSuperCategory::inRange(const char* rangeName) const
292 {
293  _catIter->Reset() ;
294  RooAbsCategoryLValue* cat ;
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 
309 Bool_t RooSuperCategory::hasRange(const char* rangeName) const
310 {
311  _catIter->Reset() ;
312  RooAbsCategoryLValue* cat ;
313  while((cat = (RooAbsCategoryLValue*)_catIter->Next())) {
314  if (cat->hasRange(rangeName)) return kTRUE ;
315  }
316 
317  return kFALSE ;
318 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TIterator * createIterator(Bool_t dir=kIterForward) const
#define coutE(a)
Definition: RooMsgService.h:34
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, which is interpreted as an OR of &#39;enum ContentsOptions&#39; values and in the style given by &#39;enum StyleOption&#39;.
virtual void Reset()=0
Collectable string class.
Definition: TObjString.h:28
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
RooSetProxy _catSet
void clearTypes()
Delete all currently defined states.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Int_t getIndex() const
Return index number of current state.
STL namespace.
Iterator abstract base class.
Definition: TIterator.h:30
virtual Bool_t inRange(const char *) const
Definition: RooAbsArg.h:289
void setValueDirty() const
Definition: RooAbsArg.h:441
TIterator * _catIter
void Class()
Definition: Class.C:29
TIterator * MakeIterator() const
Make an iterator over all state permutations of the input categories of this supercategory.
virtual TObject * Next()
Iterator increment operator.
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state...
Definition: RooCatType.h:22
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
#define ccoutE(a)
Definition: RooMsgService.h:41
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 const Text_t * GetName() const
Returns name of object.
Definition: RooCatType.h:44
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
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.
virtual ~RooSuperCategory()
Destructor.
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.
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...
virtual Bool_t hasRange(const char *) const
Definition: RooAbsArg.h:293
virtual const char * getLabel() const
Return label string of current state.
TString & String()
Definition: TObjString.h:49
virtual Bool_t hasRange(const char *rangeName) const
Return true if any of the input categories has a range named &#39;rangeName&#39;.
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
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.
int type
Definition: TGX11.cxx:120
void updateIndexList()
Update the list of possible states of this super category.
virtual TObject * Next()=0
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
virtual RooCatType evaluate() const
Iterator over set of input categories.
RooSuperCategory consolidates several RooAbsCategoryLValue objects into a single category.
Definition: first.py:1
Bool_t isShapeDirty() const
Definition: RooAbsArg.h:331
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
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...
TString currentLabel() const
Return the name of the current state, constructed from the state names of the input categories...
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual Bool_t inRange(const char *rangeName) const
Return true of all of the input category states are in the given range.
const RooCatType * defineTypeUnchecked(const char *label, Int_t index)
Internal version of defineType that does not check if type already exists.
RooMultiCatIter iterators over all state permutations of a list of categories.
char name[80]
Definition: TGX11.cxx:109
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Overloaded RooArgSet::add() method inserts &#39;var&#39; into set and registers &#39;var&#39; as server to owner with...