Logo ROOT   6.10/09
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 <stdio.h>
39 #include "TString.h"
40 #include "TClass.h"
41 #include "RooSuperCategory.h"
42 #include "RooStreamParser.h"
43 #include "RooArgSet.h"
44 #include "RooMultiCatIter.h"
45 #include "RooAbsCategoryLValue.h"
46 #include "RooMsgService.h"
47 
48 using namespace std;
49 
51 ;
52 
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Construct a lvalue product of the given set of input RooAbsCategoryLValues in 'inInputCatList'
56 /// The state names of this product category are {S1;S2,S3,...Sn} where Si are the state names
57 /// of the input categories. A RooSuperCategory is an lvalue.
58 
59 RooSuperCategory::RooSuperCategory(const char *name, const char *title, const RooArgSet& inInputCatList) :
60  RooAbsCategoryLValue(name, title), _catSet("input","Input category set",this,kTRUE,kTRUE)
61 {
62  // Copy category list
63  TIterator* iter = inInputCatList.createIterator() ;
64  RooAbsArg* arg ;
65  while ((arg=(RooAbsArg*)iter->Next())) {
66  if (!arg->IsA()->InheritsFrom(RooAbsCategoryLValue::Class())) {
67  coutE(InputArguments) << "RooSuperCategory::RooSuperCategory(" << GetName() << "): input category " << arg->GetName()
68  << " is not an lvalue" << endl ;
69  }
70  _catSet.add(*arg) ;
71  }
72  delete iter ;
74 
75  updateIndexList() ;
76 }
77 
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Copy constructor
82 
84  RooAbsCategoryLValue(other,name), _catSet("input",this,other._catSet)
85 {
87  updateIndexList() ;
88  setIndex(other.getIndex()) ;
89 }
90 
91 
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Destructor
95 
97 {
98  delete _catIter ;
99 }
100 
101 
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Make an iterator over all state permutations of
105 /// the input categories of this supercategory
106 
108 {
109  return new RooMultiCatIter(_catSet) ;
110 }
111 
112 
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Update the list of possible states of this super category
116 
118 {
119  clearTypes() ;
120 
121  RooMultiCatIter mcIter(_catSet) ;
122  TObjString* obj ;
123  Int_t i(0) ;
124  while((obj = (TObjString*) mcIter.Next())) {
125  // Register composite label
126  defineTypeUnchecked(obj->String(),i++) ;
127  }
128 
129  // Renumbering will invalidate cache
130  setValueDirty() ;
131 }
132 
133 
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Return the name of the current state,
137 /// constructed from the state names of the input categories
138 
140 {
141  _catIter->Reset() ;
142 
143  // Construct composite label name
144  TString label ;
145  RooAbsCategory* cat ;
146  Bool_t first(kTRUE) ;
147  while((cat=(RooAbsCategory*) _catIter->Next())) {
148  label.Append(first?"{":";") ;
149  label.Append(cat->getLabel()) ;
150  first=kFALSE ;
151  }
152  label.Append("}") ;
153 
154  return label ;
155 }
156 
157 
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Calculate and return the current value
161 
163 {
164  if (isShapeDirty()) {
165  const_cast<RooSuperCategory*>(this)->updateIndexList() ;
166  }
167  const RooCatType* ret = lookupType(currentLabel(),kTRUE) ;
168  if (!ret) {
169  coutE(Eval) << "RooSuperCat::evaluate(" << this << ") error: current state not defined: '" << currentLabel() << "'" << endl ;
171  return RooCatType() ;
172  }
173  return *ret ;
174 }
175 
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Set the value of the super category by specifying the state index code
180 /// by setting the states of the corresponding input category lvalues
181 
183 {
184  const RooCatType* type = lookupType(index,kTRUE) ;
185  if (!type) return kTRUE ;
186  return setType(type) ;
187 }
188 
189 
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Set the value of the super category by specifying the state name
193 /// by setting the state names of the corresponding input category lvalues
194 
195 Bool_t RooSuperCategory::setLabel(const char* label, Bool_t /*printError*/)
196 {
197  const RooCatType* type = lookupType(label,kTRUE) ;
198  if (!type) return kTRUE ;
199  return setType(type) ;
200 }
201 
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Set the value of the super category by specifying the state object
206 /// by setting the state names of the corresponding input category lvalues
207 
209 {
210  char buf[1024] ;
211  strlcpy(buf,type->GetName(),1024) ;
212 
213  RooAbsCategoryLValue* arg ;
214  Bool_t error(kFALSE) ;
215 
216  // Parse composite label and set label of components to their values
217  char* ptr=buf+1 ;
218  char* token = ptr ;
219  _catIter->Reset() ;
220  while ((arg=(RooAbsCategoryLValue*)_catIter->Next())) {
221 
222  // Delimit name token for this category
223  if (*ptr=='{') {
224  // Token is composite itself, terminate at matching '}'
225  Int_t nBrak(1) ;
226  while(*(++ptr)) {
227  if (nBrak==0) {
228  *ptr = 0 ;
229  break ;
230  }
231  if (*ptr=='{') {
232  nBrak++ ;
233  } else if (*ptr=='}') {
234  nBrak-- ;
235  }
236  }
237  } else {
238  // Simple token, terminate at next semi-colon
239  ptr = strtok(ptr,";}") ;
240  ptr += strlen(ptr) ;
241  }
242 
243  error |= arg->setLabel(token) ;
244  token = ++ptr ;
245  }
246 
247  return error ;
248 }
249 
250 
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Print the state of this object to the specified output stream.
254 
255 void RooSuperCategory::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
256 {
257  RooAbsCategory::printMultiline(os,content,verbose,indent) ;
258 
259  if (verbose) {
260  os << indent << "--- RooSuperCategory ---" << endl;
261  os << indent << " Input category list:" << endl ;
262  TString moreIndent(indent) ;
263  os << moreIndent << _catSet << endl ;
264  }
265 }
266 
267 
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 /// Read object contents from given stream
271 
272 Bool_t RooSuperCategory::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
273 {
274  return kTRUE ;
275 }
276 
277 
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// Write object contents to given stream
281 
282 void RooSuperCategory::writeToStream(ostream& os, Bool_t compact) const
283 {
284  RooAbsCategory::writeToStream(os,compact) ;
285 }
286 
287 
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 /// Return true of all of the input category states are in the given range
291 
292 Bool_t RooSuperCategory::inRange(const char* rangeName) const
293 {
294  _catIter->Reset() ;
295  RooAbsCategoryLValue* cat ;
296  while((cat = (RooAbsCategoryLValue*)_catIter->Next())) {
297  if (!cat->inRange(rangeName)) {
298  return kFALSE ;
299  }
300  }
301  return kTRUE ;
302 }
303 
304 
305 
306 ////////////////////////////////////////////////////////////////////////////////
307 /// Return true if any of the input categories has a range
308 /// named 'rangeName'
309 
310 Bool_t RooSuperCategory::hasRange(const char* rangeName) const
311 {
312  _catIter->Reset() ;
313  RooAbsCategoryLValue* cat ;
314  while((cat = (RooAbsCategoryLValue*)_catIter->Next())) {
315  if (cat->hasRange(rangeName)) return kTRUE ;
316  }
317 
318  return kFALSE ;
319 }
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:439
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:436
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.
bool verbose
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:92
#define ClassImp(name)
Definition: Rtypes.h:336
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:91
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.
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...