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