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 
21 #include <string>
22 #include <map>
23 #include <functional>
24 #include <vector>
25 
26 #ifdef R__LESS_INCLUDES
27 class RooCatType;
28 #else
29 #include "RooCatType.h"
30 #endif
31 
32 class TTree;
33 class RooVectorDataStore;
34 class Roo1DTable;
35 class TIterator;
36 
37 class RooAbsCategory : public RooAbsArg {
38 public:
39  /// The type used to denote a specific category state.
40  using value_type = int;
41 
42  // Constructors, assignment etc.
44  RooAbsCategory(const char *name, const char *title);
45  RooAbsCategory(const RooAbsCategory& other, const char* name=0) ;
46  virtual ~RooAbsCategory();
47 
48  // Value accessors
49  virtual value_type getCurrentIndex() const ;
50  virtual const char* getCurrentLabel() const ;
51 
52  const std::map<std::string, value_type>::value_type& getOrdinal(unsigned int n) const;
53  unsigned int getCurrentOrdinalNumber() const;
54 
55  Bool_t operator==(value_type index) const ;
56  Bool_t operator!=(value_type index) { return !operator==(index);}
57  Bool_t operator==(const char* label) const ;
58  Bool_t operator!=(const char* label) { return !operator==(label);}
59  virtual Bool_t operator==(const RooAbsArg& other) const ;
60  Bool_t operator!=(const RooAbsArg& other) { return !operator==(other);}
61  virtual Bool_t isIdentical(const RooAbsArg& other, Bool_t assumeSameType=kFALSE) const;
62 
63  /// Check if a state with name `label` exists.
64  bool hasLabel(const std::string& label) const {
65  return stateNames().find(label) != stateNames().end();
66  }
67  /// Check if a state with index `index` exists.
68  bool hasIndex(value_type index) const;
69 
70  /// Get the name corresponding to the given index.
71  /// \return Name or empty string if index is invalid.
72  const std::string& lookupName(value_type index) const;
73  value_type lookupIndex(const std::string& stateName) const;
74 
75 
76  Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const ;
77 
78  Roo1DTable *createTable(const char *label) const ;
79 
80  // I/O streaming interface
81  virtual Bool_t readFromStream(std::istream& is, Bool_t compact, Bool_t verbose=kFALSE) ;
82  virtual void writeToStream(std::ostream& os, Bool_t compact) const ;
83 
84  virtual void printValue(std::ostream& os) const ;
85  virtual void printMultiline(std::ostream& os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const ;
86 
87  virtual Bool_t isIntegrationSafeLValue(const RooArgSet* /*set*/) const {
88  // Is this l-value object safe for use as integration observable
89  return kTRUE ;
90  }
91 
92  RooAbsArg *createFundamental(const char* newname=0) const;
93 
94  /// Iterator for category state names. Points to pairs of index and name.
95  std::map<std::string, value_type>::const_iterator begin() const {
96  return stateNames().cbegin();
97  }
98  /// Iterator for category state names. Points to pairs of index and name.
99  std::map<std::string, value_type>::const_iterator end() const {
100  return stateNames().cend();
101  }
102  /// Number of states defined.
103  std::size_t size() const {
104  return stateNames().size();
105  }
106 
107 
108  /// \name Legacy interface
109  /// Previous versions of RooAbsCategory were based on RooCatType, a class containing a state and a label.
110  /// It has been replaced by integers, which use less space and allow for faster access. The following part of the interface
111  /// should not be used if possible.
112  /// Since RooCatType in essence is only an index and a state name, equivalent functionality can be achieved using begin()
113  /// and end() to iterate through pairs of <index, stateName> and by using using lookupName() and lookupIndex().
114  /// @{
115  const RooCatType*
116  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName()")
117  lookupType(value_type index, Bool_t printError=kFALSE) const;
118  const RooCatType*
119  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupIndex()")
120  lookupType(const char* label, Bool_t printError=kFALSE) const;
121  const RooCatType*
122  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName() / lookupIndex()")
123  lookupType(const RooCatType& type, Bool_t printError=kFALSE) const;
124  TIterator*
125  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use begin(), end() or range-based for loops.")
126  typeIterator() const;
127  /// Return number of types defined (in range named rangeName if rangeName!=0)
128  Int_t numTypes(const char* /*rangeName*/=0) const {
129  return stateNames().size();
130  }
131  /// Retrieve the current index. Use getCurrentIndex() for more clarity.
132  Int_t getIndex() const { return getCurrentIndex(); }
133  /// Retrieve current label. Use getCurrentLabel() for more clarity.
134  const char* getLabel() const { return getCurrentLabel(); }
135 protected:
136  virtual Bool_t
137  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use hasIndex() or hasLabel().")
138  isValid(const RooCatType& value) const ;
139  /// \deprecated Use defineState(const std::string& label)
140  const RooCatType*
141  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
142  defineType(const char* label);
143  /// \deprecated Use defineState(const std::string& label, value_type index)
144  const RooCatType*
145  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
146  defineType(const char* label, int index);
147  /// \deprecated Use defineStateUnchecked(const std::string& label, value_type index)
148  const RooCatType*
149  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineTypeUnchecked().")
150  defineTypeUnchecked(const char* label, value_type index);
151  /// @}
152 
153 
154 protected:
155  /// Access the map of state names to index numbers. Triggers a recomputation
156  /// if the shape is dirty.
157  const std::map<std::string, value_type>& stateNames() const {
158  if (isShapeDirty()) {
159  _legacyStates.clear();
160  const_cast<RooAbsCategory*>(this)->recomputeShape();
161  clearShapeDirty();
162  }
163 
164  return _stateNames;
165  }
166  /// \copydoc stateNames() const
167  std::map<std::string, value_type>& stateNames() {
168  if (isShapeDirty()) {
169  _legacyStates.clear();
170  recomputeShape();
171  clearShapeDirty();
172  }
173 
174  //Somebody might modify the states
175  setShapeDirty();
176 
177  return _stateNames;
178  }
179 
180  /// Evaluate the category state and return.
181  /// The returned state index should correspond to a state name that has been defined via e.g. defineType().
182  virtual value_type evaluate() const = 0;
183 
184  // Type definition management
185  virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label);
186  virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label, value_type index);
187 
188  void defineStateUnchecked(const std::string& label, value_type index);
189  void clearTypes() ;
190 
191  virtual bool isValid() const {
192  return hasIndex(_currentIndex);
193  }
194 
195  /// If a category depends on the shape of others, i.e.\ its state numbers or names depend
196  /// on the states of other categories, this function has to be implemented to recompute
197  /// _stateNames and _insertionOrder.
198  /// If one of these two changes, setShapeDirty() has to be called to propagate this information
199  /// to possible users of this category.
200  virtual void recomputeShape() = 0;
201 
202  friend class RooVectorDataStore ;
203  virtual void syncCache(const RooArgSet* set=0) ;
204  virtual void copyCache(const RooAbsArg* source, Bool_t valueOnly=kFALSE, Bool_t setValueDirty=kTRUE) ;
205  void setCachedValue(double value, bool notifyClients = true) final;
206  virtual void attachToTree(TTree& t, Int_t bufSize=32000) ;
207  virtual void attachToVStore(RooVectorDataStore& vstore) ;
208  virtual void setTreeBranchStatus(TTree& t, Bool_t active) ;
209  virtual void fillTreeBranch(TTree& t) ;
210 
213 
214 
215  mutable value_type _currentIndex{std::numeric_limits<int>::min()}; /// Current category state
216  std::map<std::string, value_type> _stateNames; /// Map state names to index numbers. Make sure state names are updated in recomputeShape().
217  std::vector<std::string> _insertionOrder; /// Keeps track in which order state numbers have been inserted. Make sure this is updated in recomputeShape().
218  mutable UChar_t _byteValue{0}; //! Transient cache for byte values from tree branches
219  mutable std::map<value_type, std::unique_ptr<RooCatType, std::function<void(RooCatType*)>> > _legacyStates; //! Map holding pointers to RooCatType instances. Only for legacy interface. Don't use if possible.
220  bool _treeVar{false}; /// Is this category attached to a tree?
221 
222  static const decltype(_stateNames)::value_type& invalidCategory();
223 
224  ClassDef(RooAbsCategory, 3) // Abstract discrete variable
225 };
226 
227 #endif
RooAbsCategory::operator!=
Bool_t operator!=(value_type index)
Definition: RooAbsCategory.h:56
RooAbsCategory::copyCache
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.
Definition: RooAbsCategory.cxx:530
n
const Int_t n
Definition: legend1.C:16
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
RooAbsCategory::evaluate
virtual value_type evaluate() const =0
Evaluate the category state and return.
RooAbsCategory::defineStateUnchecked
void defineStateUnchecked(const std::string &label, value_type index)
Internal version of defineState() that does not check if type already exists.
Definition: RooAbsCategory.cxx:222
RooAbsCategory::stateNames
std::map< std::string, value_type > & stateNames()
Access the map of state names to index numbers.
Definition: RooAbsCategory.h:167
RooAbsCategory::getCurrentIndex
virtual value_type getCurrentIndex() const
Return index number of current state.
Definition: RooAbsCategory.cxx:114
Roo1DTable
Roo1DTable implements a one-dimensional table.
Definition: Roo1DTable.h:23
RooAbsCategory::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
Definition: RooAbsCategory.cxx:383
RooAbsCategory::_byteValue
UChar_t _byteValue
Keeps track in which order state numbers have been inserted. Make sure this is updated in recomputeSh...
Definition: RooAbsCategory.h:218
RooAbsCategory::lookupName
const std::string & lookupName(value_type index) const
Get the name corresponding to the given index.
Definition: RooAbsCategory.cxx:201
RooAbsCategory::setCachedValue
void setCachedValue(double value, bool notifyClients=true) final
Overwrite the value stored in this object's cache.
Definition: RooAbsCategory.cxx:573
RooAbsCategory::value_type
int value_type
The type used to denote a specific category state.
Definition: RooAbsCategory.h:40
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
RooAbsCategory::RooAbsCategory
RooAbsCategory()
Definition: RooAbsCategory.h:43
RooAbsCategory::createTable
Roo1DTable * createTable(const char *label) const
Create a table matching the shape of this category.
Definition: RooAbsCategory.cxx:363
RooAbsArg.h
RooAbsCategory::syncCache
virtual void syncCache(const RooArgSet *set=0)
Explicitly synchronize RooAbsCategory internal cache.
Definition: RooAbsCategory.cxx:517
RooAbsCategory::isIdentical
virtual Bool_t isIdentical(const RooAbsArg &other, Bool_t assumeSameType=kFALSE) const
Definition: RooAbsCategory.cxx:175
R__SUGGEST_ALTERNATIVE
#define R__SUGGEST_ALTERNATIVE(ALTERNATIVE)
Definition: RConfig.hxx:523
RooAbsCategory::defineState
virtual const std::map< std::string, RooAbsCategory::value_type >::value_type & defineState(const std::string &label)
Define a new state with given label.
Definition: RooAbsCategory.cxx:213
RooAbsCategory::getLabel
const char * getLabel() const
Retrieve current label. Use getCurrentLabel() for more clarity.
Definition: RooAbsCategory.h:134
RooAbsCategory::isSignType
Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const
Determine if category has 2 or 3 states with index values -1,0,1.
Definition: RooAbsCategory.cxx:653
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
RooAbsCategory::isIntegrationSafeLValue
virtual Bool_t isIntegrationSafeLValue(const RooArgSet *) const
Definition: RooAbsCategory.h:87
RooAbsCategory::setTreeBranchStatus
virtual void setTreeBranchStatus(TTree &t, Bool_t active)
(De)activate associate tree branch
Definition: RooAbsCategory.cxx:504
TString
Basic string class.
Definition: TString.h:136
RooAbsCategory::operator!=
Bool_t operator!=(const RooAbsArg &other)
Definition: RooAbsCategory.h:60
RooAbsCategory::getCurrentLabel
virtual const char * getCurrentLabel() const
Return label string of current state.
Definition: RooAbsCategory.cxx:130
RooAbsCategory::getIndex
Int_t getIndex() const
Retrieve the current index. Use getCurrentIndex() for more clarity.
Definition: RooAbsCategory.h:132
RooAbsCategory::attachToVStore
virtual void attachToVStore(RooVectorDataStore &vstore)
Attach the category index and label to as branches to the given vector store.
Definition: RooAbsCategory.cxx:428
RooAbsCategory::numTypes
Int_t numTypes(const char *=0) const
Return number of types defined (in range named rangeName if rangeName!=0)
Definition: RooAbsCategory.h:128
bool
TIterator
Iterator abstract base class.
Definition: TIterator.h:30
RooAbsCategory
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
Definition: RooAbsCategory.h:37
RooAbsCategory::end
std::map< std::string, value_type >::const_iterator end() const
Iterator for category state names. Points to pairs of index and name.
Definition: RooAbsCategory.h:99
RooAbsCategory::hasIndex
bool hasIndex(value_type index) const
Check if a state with index index exists.
Definition: RooAbsCategory.cxx:188
RooCatType.h
RooAbsCategory::invalidCategory
static decltype(_stateNames) const ::value_type & invalidCategory()
Is this category attached to a tree?
RooAbsCategory::isValid
virtual bool isValid() const
WVE (08/21/01) Probably obsolete now.
Definition: RooAbsCategory.h:191
RooAbsCategory::_legacyStates
std::map< value_type, std::unique_ptr< RooCatType, std::function< void(RooCatType *)> > > _legacyStates
Transient cache for byte values from tree branches.
Definition: RooAbsCategory.h:219
RooAbsCategory::createFundamental
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooCategory fundamental object with our properties.
Definition: RooAbsCategory.cxx:635
RooAbsCategory::nextAvailableStateIndex
value_type nextAvailableStateIndex() const
Definition: RooAbsCategory.cxx:702
RooAbsCategory::attachToTree
virtual void attachToTree(TTree &t, Int_t bufSize=32000)
Attach the category index and label as branches to the given TTree.
Definition: RooAbsCategory.cxx:441
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
RooAbsCategory::_stateNames
std::map< std::string, value_type > _stateNames
Current category state.
Definition: RooAbsCategory.h:216
ROOT::R::function
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
RooAbsCategory::defineType
const RooCatType * defineType(const char *label)
Definition: RooAbsCategory.cxx:675
RooAbsCategory::_currentIndex
value_type _currentIndex
Definition: RooAbsCategory.h:215
RooAbsCategory::printValue
virtual void printValue(std::ostream &os) const
Print value (label name)
Definition: RooAbsCategory.cxx:393
RooAbsCategory::begin
std::map< std::string, value_type >::const_iterator begin() const
Iterator for category state names. Points to pairs of index and name.
Definition: RooAbsCategory.h:95
RooAbsCategory::lookupType
const RooCatType * lookupType(value_type index, Bool_t printError=kFALSE) const
Find our type corresponding to the specified index, or return nullptr for no match.
Definition: RooAbsCategory.cxx:307
RooAbsCategory::typeIterator
TIterator * typeIterator() const
Definition: RooAbsCategory.cxx:671
RooAbsCategory::defineTypeUnchecked
const RooCatType * defineTypeUnchecked(const char *label, value_type index)
Definition: RooAbsCategory.cxx:685
void
typedef void((*Func_t)())
RooAbsCategory::~RooAbsCategory
virtual ~RooAbsCategory()
Destructor.
Definition: RooAbsCategory.cxx:104
RooAbsCategory::fillTreeBranch
virtual void fillTreeBranch(TTree &t)
Fill tree branches associated with current object with current value.
Definition: RooAbsCategory.cxx:484
RooAbsCategory::printMultiline
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.
Definition: RooAbsCategory.cxx:406
RooAbsCategory::_insertionOrder
std::vector< std::string > _insertionOrder
Map state names to index numbers. Make sure state names are updated in recomputeShape().
Definition: RooAbsCategory.h:217
RooAbsArg::clearShapeDirty
void clearShapeDirty() const
Definition: RooAbsArg.h:577
RooAbsArg::isShapeDirty
Bool_t isShapeDirty() const
Definition: RooAbsArg.h:416
RooAbsCategory::getOrdinal
const std::map< std::string, value_type >::value_type & getOrdinal(unsigned int n) const
Return name and index of the nth defined state.
Definition: RooAbsCategory.cxx:591
RooAbsCategory::size
std::size_t size() const
Number of states defined.
Definition: RooAbsCategory.h:103
RooAbsArg::setShapeDirty
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:495
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
RooVectorDataStore
RooVectorDataStore uses std::vectors to store data columns.
Definition: RooVectorDataStore.h:37
RooAbsCategory::recomputeShape
virtual void recomputeShape()=0
If a category depends on the shape of others, i.e. its state numbers or names depend on the states of...
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
RooAbsCategory::hasLabel
bool hasLabel(const std::string &label) const
Check if a state with name label exists.
Definition: RooAbsCategory.h:64
RooAbsCategory::lookupIndex
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Definition: RooAbsCategory.cxx:283
name
char name[80]
Definition: TGX11.cxx:110
RooAbsCategory::_treeVar
bool _treeVar
Map holding pointers to RooCatType instances. Only for legacy interface. Don't use if possible.
Definition: RooAbsCategory.h:220
RooAbsCategory::stateNames
const std::map< std::string, value_type > & stateNames() const
Access the map of state names to index numbers.
Definition: RooAbsCategory.h:157
RooCatType
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
Definition: RooCatTypeLegacy.h:23
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooAbsCategory::operator!=
Bool_t operator!=(const char *label)
Definition: RooAbsCategory.h:58
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooAbsArg::setValueDirty
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:490
RooAbsCategory::operator==
Bool_t operator==(value_type index) const
Equality operator with a integer (compares with state index number)
Definition: RooAbsCategory.cxx:145
type
int type
Definition: TGX11.cxx:121
for
for(Int_t i=0;i< n;i++)
Definition: legend1.C:18
RooAbsCategory::clearTypes
void clearTypes()
Delete all currently defined states.
Definition: RooAbsCategory.cxx:270
RooAbsCategory::getCurrentOrdinalNumber
unsigned int getCurrentOrdinalNumber() const
Return ordinal number of the current state.
Definition: RooAbsCategory.cxx:611
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:33
RooAbsCategory::retrieveLegacyState
RooCatType * retrieveLegacyState(value_type index) const
Return the legacy RooCatType corresponding to index. If it doesn't exist, create one.
Definition: RooAbsCategory.cxx:691
int
RooAbsCategory::readFromStream
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from stream (dummy for now)
Definition: RooAbsCategory.cxx:373