Logo ROOT   6.14/05
Reference Guide
RooCategory.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 RooCategory.cxx
19 \class RooCategory
20 \ingroup Roofitcore
21 
22 RooCategory represents a fundamental (non-derived) discrete value object. The class
23 has a public interface to define the possible value states.
24 **/
25 
26 
27 #include "RooFit.h"
28 
29 #include "Riostream.h"
30 #include "Riostream.h"
31 #include <stdlib.h>
32 #include "TTree.h"
33 #include "TString.h"
34 #include "TH1.h"
35 #include "RooCategory.h"
36 #include "RooArgSet.h"
37 #include "RooStreamParser.h"
38 #include "RooMsgService.h"
39 #include "RooTrace.h"
40 
41 using namespace std;
42 
44 ;
45 
47 RooCategorySharedProperties RooCategory::_nullProp("00000000-0000-0000-0000-000000000000") ;
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 RooCategory::RooCategory() : _sharedProp(0)
52 {
54 }
55 
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Constructor. Types must be defined using defineType() before variable can be used
60 
61 RooCategory::RooCategory(const char *name, const char *title) :
62  RooAbsCategoryLValue(name,title)
63 {
65 
66  setValueDirty() ;
67  setShapeDirty() ;
69 }
70 
71 
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Copy constructor
75 
76 RooCategory::RooCategory(const RooCategory& other, const char* name) :
77  RooAbsCategoryLValue(other, name)
78 {
81 }
82 
83 
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Destructor
87 
89 {
92 }
93 
94 
95 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Set value by specifying the index code of the desired state.
99 /// If printError is set, a message will be printed if
100 /// the specified index does not represent a valid state.
101 
103 {
104  const RooCatType* type = lookupType(index,printError) ;
105  if (!type) return kTRUE ;
106  _value = *type ;
107  setValueDirty() ;
108  return kFALSE ;
109 }
110 
111 
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Set value by specifying the name of the desired state
115 /// If printError is set, a message will be printed if
116 /// the specified label does not represent a valid state.
117 
118 Bool_t RooCategory::setLabel(const char* label, Bool_t printError)
119 {
120  const RooCatType* type = lookupType(label,printError) ;
121  if (!type) return kTRUE ;
122  _value = *type ;
123  setValueDirty() ;
124  return kFALSE ;
125 }
126 
127 
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Define a state with given name, the lowest available
131 /// positive integer is assigned as index. Category
132 /// state labels may not contain semicolons.
133 /// Error status is return if state with given name
134 /// is already defined
135 
136 Bool_t RooCategory::defineType(const char* label)
137 {
138  if (TString(label).Contains(";")) {
139  coutE(InputArguments) << "RooCategory::defineType(" << GetName()
140  << "): semicolons not allowed in label name" << endl ;
141  return kTRUE ;
142  }
143 
144  return RooAbsCategory::defineType(label)?kFALSE:kTRUE ;
145 }
146 
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Define a state with given name and index. Category
150 /// state labels may not contain semicolons
151 /// Error status is return if state with given name
152 /// or index is already defined
153 
154 Bool_t RooCategory::defineType(const char* label, Int_t index)
155 {
156  if (TString(label).Contains(";")) {
157  coutE(InputArguments) << "RooCategory::defineType(" << GetName()
158  << "): semicolons not allowed in label name" << endl ;
159  return kTRUE ;
160  }
161 
162  return RooAbsCategory::defineType(label,index)?kFALSE:kTRUE ;
163 }
164 
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Read object contents from given stream
168 
169 Bool_t RooCategory::readFromStream(istream& is, Bool_t /*compact*/, Bool_t verbose)
170 {
171  // Read single token
172  RooStreamParser parser(is) ;
173  TString token = parser.readToken() ;
174 
175  return setLabel(token,verbose) ;
176 }
177 
178 
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// compact only at the moment
182 
183 void RooCategory::writeToStream(ostream& os, Bool_t compact) const
184 {
185  if (compact) {
186  os << getIndex() ;
187  } else {
188  os << getLabel() ;
189  }
190 }
191 
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 /// Check that both input arguments are not null pointers
195 
196 void RooCategory::clearRange(const char* name, Bool_t silent)
197 {
198  if (!name) {
199  coutE(InputArguments) << "RooCategory::clearRange(" << GetName() << ") ERROR: must specificy valid range name" << endl ;
200  return ;
201  }
202 
203  // Find the list that represents this range
204  TList* rangeNameList = static_cast<TList*>(_sharedProp->_altRanges.FindObject(name)) ;
205 
206  // If it exists, clear it
207  if (rangeNameList) {
208  rangeNameList->Clear() ;
209  } else if (!silent) {
210  coutE(InputArguments) << "RooCategory::clearRange(" << GetName() << ") ERROR: range '" << name << "' does not exist" << endl ;
211  }
212 }
213 
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 
217 void RooCategory::setRange(const char* name, const char* stateNameList)
218 {
219  clearRange(name,kTRUE) ;
220  addToRange(name,stateNameList) ;
221 }
222 
223 
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Check that both input arguments are not null pointers
227 
228 void RooCategory::addToRange(const char* name, const char* stateNameList)
229 {
230  if (!name || !stateNameList) {
231  coutE(InputArguments) << "RooCategory::setRange(" << GetName() << ") ERROR: must specificy valid name and state name list" << endl ;
232  return ;
233  }
234 
235  // Find the list that represents this range
236  TList* rangeNameList = static_cast<TList*>(_sharedProp->_altRanges.FindObject(name)) ;
237 
238  // If it does not exist, create it on the fly
239  if (!rangeNameList) {
240  coutI(Contents) << "RooCategory::setRange(" << GetName()
241  << ") new range named '" << name << "' created with state list " << stateNameList << endl ;
242 
243  rangeNameList = new TList ;
244  rangeNameList->SetOwner(kTRUE) ;
245  rangeNameList->SetName(name) ;
246  _sharedProp->_altRanges.Add(rangeNameList) ;
247  }
248 
249  // Parse list of state names, verify that each is valid and add them to the list
250  const size_t bufSize = strlen(stateNameList)+1;
251  char* buf = new char[bufSize] ;
252  strlcpy(buf,stateNameList,bufSize) ;
253  char* token = strtok(buf,",") ;
254  while(token) {
255  const RooCatType* state = lookupType(token,kFALSE) ;
256  if (state && !rangeNameList->FindObject(token)) {
257  rangeNameList->Add(new RooCatType(*state)) ;
258  } else {
259  coutW(InputArguments) << "RooCategory::setRange(" << GetName() << ") WARNING: Ignoring invalid state name '"
260  << token << "' in state name list" << endl ;
261  }
262  token = strtok(0,",") ;
263  }
264 
265  delete[] buf ;
266 }
267 
268 
269 
270 ////////////////////////////////////////////////////////////////////////////////
271 /// If no range is specified [ i.e. the default range ] all category states are in range
272 
273 Bool_t RooCategory::isStateInRange(const char* rangeName, const char* stateName) const
274 {
275  if (!rangeName) {
276  return kTRUE ;
277  }
278 
279  // Check that both input arguments are not null pointers
280  if (!stateName) {
281  coutE(InputArguments) << "RooCategory::isStateInRange(" << GetName() << ") ERROR: must specificy valid state name" << endl ;
282  return kFALSE ;
283  }
284 
285 
286  // Find the list that represents this range
287  TList* rangeNameList = static_cast<TList*>(_sharedProp->_altRanges.FindObject(rangeName)) ;
288 
289  // If the range doesn't exist create range with all valid states included
290  if (rangeNameList) {
291  return rangeNameList->FindObject(stateName) ? kTRUE : kFALSE ;
292  }
293 
294  // Range does not exists -- create it on the fly with full set of states (analoguous to RooRealVar)
295  return kTRUE ;
296 
297 }
298 
299 
300 ////////////////////////////////////////////////////////////////////////////////
301 
302 void RooCategory::Streamer(TBuffer &R__b)
303 {
304  UInt_t R__s, R__c;
305  if (R__b.IsReading()) {
306 
307  Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
308  RooAbsCategoryLValue::Streamer(R__b);
309  if (R__v==1) {
310  // Implement V1 streamer here
311  R__b >> _sharedProp;
312  } else {
314  tmpSharedProp->Streamer(R__b) ;
315  if (!(_nullProp==*tmpSharedProp)) {
317  } else {
318  delete tmpSharedProp ;
319  _sharedProp = 0 ;
320  }
321  }
322 
323  R__b.CheckByteCount(R__s, R__c, RooCategory::IsA());
324 
325  } else {
326 
327  R__c = R__b.WriteVersion(RooCategory::IsA(), kTRUE);
328  RooAbsCategoryLValue::Streamer(R__b);
329  if (_sharedProp) {
330  _sharedProp->Streamer(R__b) ;
331  } else {
332  _nullProp.Streamer(R__b) ;
333  }
334  R__b.SetByteCount(R__c, kTRUE);
335 
336  }
337 }
338 
339 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Bool_t IsReading() const
Definition: TBuffer.h:83
#define coutE(a)
Definition: RooMsgService.h:34
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
short Version_t
Definition: RtypesCore.h:61
#define coutI(a)
Definition: RooMsgService.h:31
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set value by specifying the index code of the desired state.
RooCatType _value
Transient cache for byte values from tree branches.
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
void clearRange(const char *name, Bool_t silent)
Check that both input arguments are not null pointers.
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooCategorySharedProperties is the container for all properties that are shared between instance of R...
STL namespace.
#define coutW(a)
Definition: RooMsgService.h:33
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
virtual TObject * FindObject(const char *name) const
Delete a TObjLink object.
Definition: TList.cxx:574
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)
Set value by specifying the name of the desired state If printError is set, a message will be printed...
void setValueDirty() const
Definition: RooAbsArg.h:441
#define TRACE_CREATE
Definition: RooTrace.h:22
static RooSharedPropertiesList _sharedPropList
Definition: RooCategory.h:84
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...
RooSharedProperties * registerProperties(RooSharedProperties *, Bool_t canDeleteIncoming=kTRUE)
Register property into list and take ownership.
Bool_t isStateInRange(const char *rangeName, const char *stateName) const
If no range is specified [ i.e. the default range ] all category states are in range.
void addToRange(const char *rangeName, const char *stateNameList)
Check that both input arguments are not null pointers.
RooCategorySharedProperties * _sharedProp
Definition: RooCategory.h:86
A doubly linked list.
Definition: TList.h:44
const RooCatType * defineType(const char *label)
Define a new state with given name.
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
void unregisterProperties(RooSharedProperties *)
Decrease reference count of property.
TString readToken()
Read one token separated by any of the know punctuation characters This function recognizes and handl...
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.
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
void SetName(const char *name)
Definition: TCollection.h:204
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
virtual Int_t getIndex() const
Return index number of current state.
Definition: RooCategory.h:34
virtual void writeToStream(std::ostream &os, Bool_t compact) const
compact only at the moment
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
static RooCategorySharedProperties _nullProp
Definition: RooCategory.h:85
int type
Definition: TGX11.cxx:120
#define TRACE_DESTROY
Definition: RooTrace.h:23
virtual const char * getLabel() const
Return label string of current state.
Definition: RooCategory.h:39
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:399
Class RooSharedPropertiesList maintains the properties of RooRealVars and RooCategories that are clon...
virtual void Add(TObject *obj)
Definition: TList.h:87
void setShapeDirty() const
Definition: RooAbsArg.h:442
virtual ~RooCategory()
Destructor.
Definition: RooCategory.cxx:88
Bool_t defineType(const char *label)
Define a state with given name, the lowest available positive integer is assigned as index...
const Bool_t kTRUE
Definition: RtypesCore.h:87
void setRange(const char *rangeName, const char *stateNameList)
char name[80]
Definition: TGX11.cxx:109
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0