Logo ROOT   6.18/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
22RooCategory represents a fundamental (non-derived) discrete value object. The class
23has 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
41using namespace std;
42
44;
45
47RooCategorySharedProperties RooCategory::_nullProp("00000000-0000-0000-0000-000000000000") ;
48
49////////////////////////////////////////////////////////////////////////////////
50
51RooCategory::RooCategory() : _sharedProp(0)
52{
54}
55
56
57
58////////////////////////////////////////////////////////////////////////////////
59/// Constructor. Types must be defined using defineType() before variable can be used
60
61RooCategory::RooCategory(const char *name, const char *title) :
63{
65
66 setValueDirty() ;
67 setShapeDirty() ;
69}
70
71
72
73////////////////////////////////////////////////////////////////////////////////
74/// Copy constructor
75
76RooCategory::RooCategory(const RooCategory& other, const char* 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
118Bool_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
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
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
154Bool_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
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
183void 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
196void 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
217void RooCategory::setRange(const char* name, const char* stateNameList)
218{
220 addToRange(name,stateNameList) ;
221}
222
223
224
225////////////////////////////////////////////////////////////////////////////////
226/// Check that both input arguments are not null pointers
227
228void 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
273Bool_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
302void 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
#define coutI(a)
Definition: RooMsgService.h:31
#define coutW(a)
Definition: RooMsgService.h:33
#define coutE(a)
Definition: RooMsgService.h:34
#define TRACE_DESTROY
Definition: RooTrace.h:23
#define TRACE_CREATE
Definition: RooTrace.h:22
int Int_t
Definition: RtypesCore.h:41
short Version_t
Definition: RtypesCore.h:61
unsigned int UInt_t
Definition: RtypesCore.h:42
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:365
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
void setShapeDirty() const
Definition: RooAbsArg.h:487
void setValueDirty() const
Definition: RooAbsArg.h:486
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
RooCatType _value
Transient cache for byte values from tree branches.
const RooCatType * defineType(const char *label)
Define a new state with given name.
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.
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
Definition: RooCatType.h:22
RooCategorySharedProperties is the container for all properties that are shared between instance of R...
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
RooCategorySharedProperties * _sharedProp
Definition: RooCategory.h:86
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
void setRange(const char *rangeName, const char *stateNameList)
virtual ~RooCategory()
Destructor.
Definition: RooCategory.cxx:88
static RooSharedPropertiesList _sharedPropList
Definition: RooCategory.h:84
void addToRange(const char *rangeName, const char *stateNameList)
Check that both input arguments are not null pointers.
virtual const char * getLabel() const
Return label string of current state.
Definition: RooCategory.h:39
virtual void writeToStream(std::ostream &os, Bool_t compact) const
compact only at the moment
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...
Bool_t defineType(const char *label)
Define a state with given name, the lowest available positive integer is assigned as index.
void clearRange(const char *name, Bool_t silent)
Check that both input arguments are not null pointers.
virtual Int_t getIndex() const
Return index number of current state.
Definition: RooCategory.h:34
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set value by specifying the index code of the desired state.
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.
static RooCategorySharedProperties _nullProp
Definition: RooCategory.h:85
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:63
Class RooSharedPropertiesList maintains the properties of RooRealVars and RooCategories that are clon...
void unregisterProperties(RooSharedProperties *)
Decrease reference count of property.
RooSharedProperties * registerProperties(RooSharedProperties *, Bool_t canDeleteIncoming=kTRUE)
Register property into list and take ownership.
TString readToken()
Read one token separated by any of the know punctuation characters This function recognizes and handl...
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Bool_t IsReading() const
Definition: TBuffer.h:85
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
void SetName(const char *name)
Definition: TCollection.h:204
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:575
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:399
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:131
@ InputArguments
Definition: RooGlobalFunc.h:58