Logo ROOT  
Reference Guide
RooAbsCategory.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooAbsCategory.h,v 1.38 2007/05/11 09:11:30 verkerke Exp $
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#ifndef ROO_ABS_CATEGORY
17#define ROO_ABS_CATEGORY
18
19#include "RooAbsArg.h"
20#include "RooCatType.h"
21#include "TIterator.h"
22
23class TTree ;
24class RooArgSet ;
25class RooDataSet ;
26class Roo1DTable ;
28
29class RooAbsCategory : public RooAbsArg {
30public:
31 // Constructors, assignment etc.
33 RooAbsCategory(const char *name, const char *title);
34 RooAbsCategory(const RooAbsCategory& other, const char* name=0) ;
35 virtual ~RooAbsCategory();
36
37 // Value accessors
38 virtual Int_t getIndex() const ;
39 virtual const char* getLabel() const ;
40 Bool_t operator==(Int_t index) const ;
41 Bool_t operator!=(Int_t index) { return !operator==(index);}
42 Bool_t operator==(const char* label) const ;
43 Bool_t operator!=(const char* label) { return !operator==(label);}
44 virtual Bool_t operator==(const RooAbsArg& other) ;
45 Bool_t operator!=(const RooAbsArg& other) { return !operator==(other);}
46 virtual Bool_t isIdentical(const RooAbsArg& other, Bool_t assumeSameType=kFALSE) ;
47
48 Bool_t isValidIndex(Int_t index) const ;
49 Bool_t isValidLabel(const char* label) const ;
50 const RooCatType* lookupType(Int_t index, Bool_t printError=kFALSE) const ;
51 const RooCatType* lookupType(const char* label, Bool_t printError=kFALSE) const ;
52 const RooCatType* lookupType(const RooCatType& type, Bool_t printError=kFALSE) const ;
53 /// \deprecated Iterator over types. Use range-based for loops instead.
55 R__SUGGEST_ALTERNATIVE("Use begin(), end() or range-based for loops.")
56 typeIterator() const {
57 return new LegacyIterator(_types);
58 }
59 /// Return number of types defined (in range named rangeName if rangeName!=0)
60 Int_t numTypes(const char* /*rangeName*/=0) const {
61 return _types.size();
62 }
63 Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const ;
64
65 Roo1DTable *createTable(const char *label) const ;
66
67 // I/O streaming interface
68 virtual Bool_t readFromStream(std::istream& is, Bool_t compact, Bool_t verbose=kFALSE) ;
69 virtual void writeToStream(std::ostream& os, Bool_t compact) const ;
70
71 virtual void printValue(std::ostream& os) const ;
72 virtual void printMultiline(std::ostream& os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const ;
73
74 virtual Bool_t isIntegrationSafeLValue(const RooArgSet* /*set*/) const {
75 // Is this l-value object safe for use as integration observable
76 return kTRUE ;
77 }
78
79 RooAbsArg *createFundamental(const char* newname=0) const;
80
81 std::vector<RooCatType*>::const_iterator begin() const {
82 return _types.cbegin();
83 }
84
85 std::vector<RooCatType*>::const_iterator end() const {
86 return _types.cend();
87 }
88
89 std::size_t size() const {
90 return _types.size();
91 }
92
93protected:
94
95 // Function evaluation and error tracing
96 RooCatType traceEval() const ;
97 // coverity[PASS_BY_VALUE]
98 virtual Bool_t traceEvalHook(RooCatType /*value*/) const {
99 // Hook function for trace evaluation (dummy)
100 return kFALSE ;
101 }
102 virtual RooCatType evaluate() const = 0 ;
103
104 // Type definition management
105 const RooCatType* defineType(const char* label) ;
106 const RooCatType* defineType(const char* label, Int_t index) ;
107 const RooCatType* defineTypeUnchecked(const char* label, Int_t index) ;
108 const RooCatType* getOrdinal(UInt_t n, const char* rangeName=0) const;
109 void clearTypes() ;
110
111 virtual Bool_t isValid() const ;
112 virtual Bool_t isValid(const RooCatType& value) const ;
113
114 friend class RooVectorDataStore ;
115 virtual void syncCache(const RooArgSet* set=0) ;
116 virtual void copyCache(const RooAbsArg* source, Bool_t valueOnly=kFALSE, Bool_t setValueDirty=kTRUE) ;
117 virtual void attachToTree(TTree& t, Int_t bufSize=32000) ;
118 virtual void attachToVStore(RooVectorDataStore& vstore) ;
119 virtual void setTreeBranchStatus(TTree& t, Bool_t active) ;
120 virtual void fillTreeBranch(TTree& t) ;
121
122 mutable UChar_t _byteValue ; //! Transient cache for byte values from tree branches
123 mutable RooCatType _value ; // Current value
124 // These need to be pointers, unfortunately, since other classes are holding pointers to the categories.
125 // That's not safe in case of reallocations.
126 std::vector<RooCatType*> _types; // Vector of allowed values.
127
128 Bool_t _treeVar ; //! do not persist
129
130 class LegacyIterator : public TIterator {
131 public:
132 LegacyIterator(const std::vector<RooCatType*>& vec) : _vec(&vec), index(-1) { }
133 const TCollection *GetCollection() const override {
134 return nullptr;
135 }
136 TObject* Next() override {
137 ++index;
138 return this->operator*();
139 }
140 void Reset() override {
141 index = -1;
142 }
143 TObject* operator*() const override {
144 // Need to const_cast, unfortunately because TIterator interface is too permissive
145 return 0 <= index && index < (int)_vec->size() ? const_cast<RooCatType*>((*_vec)[index]) : nullptr;
146 }
148 TIterator& operator=(const TIterator& other) override {
149 auto otherLeg = dynamic_cast<LegacyIterator*>(*other);
150 if (otherLeg)
151 return this->operator=(*otherLeg);
152
153 throw std::logic_error("Cannot assign to category iterators from incompatible types.");
154 }
155
156 private:
157 const std::vector<RooCatType*>* _vec;
158 int index;
159 };
160
161 ClassDef(RooAbsCategory, 2) // Abstract discrete variable
162};
163
164#endif
int Int_t
Definition: RtypesCore.h:41
unsigned char UChar_t
Definition: RtypesCore.h:34
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 ClassDef(name, id)
Definition: Rtypes.h:326
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
Roo1DTable implements a one-dimensional table.
Definition: Roo1DTable.h:24
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:71
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:466
const std::vector< RooCatType * > * _vec
const TCollection * GetCollection() const override
TObject * operator*() const override
Return current object or nullptr.
LegacyIterator & operator=(const LegacyIterator &)=default
LegacyIterator(const std::vector< RooCatType * > &vec)
TIterator & operator=(const TIterator &other) override
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
std::vector< RooCatType * > _types
std::vector< RooCatType * >::const_iterator begin() const
virtual void copyCache(const RooAbsArg *source, Bool_t valueOnly=kFALSE, Bool_t setValueDirty=kTRUE)
Copy the cached value from given source and raise dirty flag.
const RooCatType * getOrdinal(UInt_t n, const char *rangeName=0) const
Return state definition of ordinal nth defined state, needed by the generator mechanism.
TIterator * R__SUGGEST_ALTERNATIVE("Use begin(), end() or range-based for loops.") typeIterator() const
Bool_t operator!=(Int_t index)
virtual void fillTreeBranch(TTree &t)
Fill tree branches associated with current object with current value.
RooCatType _value
Transient cache for byte values from tree branches.
std::vector< RooCatType * >::const_iterator end() const
virtual void attachToVStore(RooVectorDataStore &vstore)
Attach the category index and label to as branches to the given vector store.
Roo1DTable * createTable(const char *label) const
Create a table matching the shape of this category.
virtual Bool_t isValid() const
Check if current value is a valid state.
virtual ~RooAbsCategory()
Destructor.
virtual Int_t getIndex() const
Return index number of current state.
virtual void attachToTree(TTree &t, Int_t bufSize=32000)
Attach the category index and label to as branches to the given TTree.
virtual const char * getLabel() const
Return label string of current state.
Bool_t isValidLabel(const char *label) const
Check if state with given name is defined.
virtual Bool_t isIntegrationSafeLValue(const RooArgSet *) const
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooCategory fundamental object with our properties.
Bool_t operator!=(const char *label)
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
virtual void setTreeBranchStatus(TTree &t, Bool_t active)
(De)activate associate tree branch
const RooCatType * defineTypeUnchecked(const char *label, Int_t index)
Internal version of defineType that does not check if type already exists.
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.
std::size_t size() const
Bool_t operator!=(const RooAbsArg &other)
Int_t numTypes(const char *=0) const
Return number of types defined (in range named rangeName if rangeName!=0)
virtual Bool_t isIdentical(const RooAbsArg &other, Bool_t assumeSameType=kFALSE)
RooCatType traceEval() const
Recalculate current value and check validity of new result.
virtual RooCatType evaluate() const =0
virtual void syncCache(const RooArgSet *set=0)
Explicitly synchronize RooAbsCategory internal cache.
const RooCatType * defineType(const char *label)
Define a new state with given name.
Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const
Determine if category has 2 or 3 states with index values -1,0,1.
virtual Bool_t traceEvalHook(RooCatType) const
virtual void printValue(std::ostream &os) const
Print value (label 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.
Bool_t operator==(Int_t index) const
Equality operator with a integer (compares with state index number)
Bool_t isValidIndex(Int_t index) const
Check if state with given index is defined.
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from stream (dummy for now)
void clearTypes()
Delete all currently defined states.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
Definition: RooCatType.h:22
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
RooVectorDataStore is the abstract base class for data collection that use a TTree as internal storag...
Collection abstract base class.
Definition: TCollection.h:63
Iterator abstract base class.
Definition: TIterator.h:30
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:131
A TTree represents a columnar dataset.
Definition: TTree.h:72
const Int_t n
Definition: legend1.C:16