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 "RooSpan.h"
21 
22 #include <string>
23 #include <map>
24 #include <functional>
25 #include <vector>
26 
27 #ifdef R__LESS_INCLUDES
28 class RooCatType;
29 #else
30 #include "RooCatType.h"
31 #endif
32 
33 class TTree;
34 class RooVectorDataStore;
35 class Roo1DTable;
36 class TIterator;
37 
38 class RooAbsCategory : public RooAbsArg {
39 public:
40  /// The type used to denote a specific category state.
41  using value_type = int;
42  /// A category state to signify an invalid category. The `first` member is std::numeric_limits<int>::min(), the name is empty.
43  static const std::map<std::string, RooAbsCategory::value_type>::value_type _invalidCategory;
44 
45  // Constructors, assignment etc.
47  RooAbsCategory(const char *name, const char *title);
48  RooAbsCategory(const RooAbsCategory& other, const char* name=0) ;
49  virtual ~RooAbsCategory();
50 
51  // Value accessors
52  virtual value_type getCurrentIndex() const ;
53  /// Retrieve a batch of category values for events in the range [begin, begin+batchSize).
54  virtual RooSpan<const value_type> getValBatch(std::size_t /*begin*/, std::size_t /*batchSize*/) const {
55  throw std::logic_error("Batch values are not implemented for RooAbsCategory.");
56  }
57  virtual const char* getCurrentLabel() const ;
58 
59  const std::map<std::string, value_type>::value_type& getOrdinal(unsigned int n) const;
60  unsigned int getCurrentOrdinalNumber() const;
61 
62  Bool_t operator==(value_type index) const ;
63  Bool_t operator!=(value_type index) { return !operator==(index);}
64  Bool_t operator==(const char* label) const ;
65  Bool_t operator!=(const char* label) { return !operator==(label);}
66  virtual Bool_t operator==(const RooAbsArg& other) const ;
67  Bool_t operator!=(const RooAbsArg& other) { return !operator==(other);}
68  virtual Bool_t isIdentical(const RooAbsArg& other, Bool_t assumeSameType=kFALSE) const;
69 
70  /// Check if a state with name `label` exists.
71  bool hasLabel(const std::string& label) const {
72  return stateNames().find(label) != stateNames().end();
73  }
74  /// Check if a state with index `index` exists.
75  bool hasIndex(value_type index) const;
76 
77  /// Get the name corresponding to the given index.
78  /// \return Name or empty string if index is invalid.
79  const std::string& lookupName(value_type index) const;
80  value_type lookupIndex(const std::string& stateName) const;
81 
82 
83  Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const ;
84 
85  Roo1DTable *createTable(const char *label) const ;
86 
87  // I/O streaming interface
88  virtual Bool_t readFromStream(std::istream& is, Bool_t compact, Bool_t verbose=kFALSE) ;
89  virtual void writeToStream(std::ostream& os, Bool_t compact) const ;
90 
91  virtual void printValue(std::ostream& os) const ;
92  virtual void printMultiline(std::ostream& os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const ;
93 
94  virtual Bool_t isIntegrationSafeLValue(const RooArgSet* /*set*/) const {
95  // Is this l-value object safe for use as integration observable
96  return kTRUE ;
97  }
98 
99  RooAbsArg *createFundamental(const char* newname=0) const;
100 
101  /// Iterator for category state names. Points to pairs of index and name.
102  std::map<std::string, value_type>::const_iterator begin() const {
103  return stateNames().cbegin();
104  }
105  /// Iterator for category state names. Points to pairs of index and name.
106  std::map<std::string, value_type>::const_iterator end() const {
107  return stateNames().cend();
108  }
109  /// Number of states defined.
110  std::size_t size() const {
111  return stateNames().size();
112  }
113 
114 
115  /// \name Legacy interface
116  /// Previous versions of RooAbsCategory were based on RooCatType, a class containing a state and a label.
117  /// It has been replaced by integers, which use less space and allow for faster access. The following part of the interface
118  /// should not be used if possible.
119  /// Since RooCatType in essence is only an index and a state name, equivalent functionality can be achieved using begin()
120  /// and end() to iterate through pairs of <index, stateName> and by using using lookupName() and lookupIndex().
121  /// @{
122  const RooCatType*
123  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName()")
124  lookupType(value_type index, Bool_t printError=kFALSE) const;
125  const RooCatType*
126  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupIndex()")
127  lookupType(const char* label, Bool_t printError=kFALSE) const;
128  const RooCatType*
129  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName() / lookupIndex()")
130  lookupType(const RooCatType& type, Bool_t printError=kFALSE) const;
131  TIterator*
132  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use begin(), end() or range-based for loops.")
133  typeIterator() const;
134  /// Return number of types defined (in range named rangeName if rangeName!=0)
135  Int_t numTypes(const char* /*rangeName*/=0) const {
136  return stateNames().size();
137  }
138  /// Retrieve the current index. Use getCurrentIndex() for more clarity.
139  Int_t getIndex() const { return getCurrentIndex(); }
140  /// Retrieve current label. Use getCurrentLabel() for more clarity.
141  const char* getLabel() const { return getCurrentLabel(); }
142 protected:
143  virtual Bool_t
144  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use hasIndex() or hasLabel().")
145  isValid(const RooCatType& value) const ;
146  /// \deprecated Use defineState(const std::string& label)
147  const RooCatType*
148  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
149  defineType(const char* label);
150  /// \deprecated Use defineState(const std::string& label, value_type index)
151  const RooCatType*
152  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
153  defineType(const char* label, int index);
154  /// \deprecated Use defineStateUnchecked(const std::string& label, value_type index)
155  const RooCatType*
156  R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineTypeUnchecked().")
157  defineTypeUnchecked(const char* label, value_type index);
158  /// @}
159 
160 
161 protected:
162  /// Access the map of state names to index numbers. Triggers a recomputation
163  /// if the shape is dirty.
164  const std::map<std::string, value_type>& stateNames() const {
165  if (isShapeDirty()) {
166  _legacyStates.clear();
167  const_cast<RooAbsCategory*>(this)->recomputeShape();
168  clearShapeDirty();
169  }
170 
171  return _stateNames;
172  }
173  /// \copydoc stateNames() const
174  std::map<std::string, value_type>& stateNames() {
175  if (isShapeDirty()) {
176  _legacyStates.clear();
177  recomputeShape();
178  clearShapeDirty();
179  }
180 
181  //Somebody might modify the states
182  setShapeDirty();
183 
184  return _stateNames;
185  }
186 
187  /// Evaluate the category state and return.
188  /// The returned state index should correspond to a state name that has been defined via e.g. defineType().
189  virtual value_type evaluate() const = 0;
190 
191  // Type definition management
192  virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label);
193  virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label, value_type index);
194 
195  void defineStateUnchecked(const std::string& label, value_type index);
196  void clearTypes() ;
197 
198  virtual bool isValid() const {
199  return hasIndex(_currentIndex);
200  }
201 
202  /// If a category depends on the shape of others, *i.e.*, its state numbers or names depend
203  /// on the states of other categories, this function has to be implemented to recompute
204  /// _stateNames and _insertionOrder.
205  /// If one of these two changes, setShapeDirty() has to be called to propagate this information
206  /// to possible users of this category.
207  virtual void recomputeShape() = 0;
208 
209  friend class RooVectorDataStore ;
210  virtual void syncCache(const RooArgSet* set=0) ;
211  virtual void copyCache(const RooAbsArg* source, Bool_t valueOnly=kFALSE, Bool_t setValueDirty=kTRUE) ;
212  virtual void attachToTree(TTree& t, Int_t bufSize=32000) ;
213  virtual void attachToVStore(RooVectorDataStore& vstore) ;
214  virtual void setTreeBranchStatus(TTree& t, Bool_t active) ;
215  virtual void fillTreeBranch(TTree& t) ;
216 
219 
220 
221  mutable value_type _currentIndex{std::numeric_limits<int>::min()}; /// Current category state
222  std::map<std::string, value_type> _stateNames; /// Map state names to index numbers. Make sure state names are updated in recomputeShape().
223  std::vector<std::string> _insertionOrder; /// Keeps track in which order state numbers have been inserted. Make sure this is updated in recomputeShape().
224  mutable UChar_t _byteValue{0}; //! Transient cache for byte values from tree branches
225  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.
226  bool _treeVar{false}; /// Is this category attached to a tree?
227 
228  ClassDef(RooAbsCategory, 3) // Abstract discrete variable
229 };
230 
231 #endif
RooAbsCategory::operator!=
Bool_t operator!=(value_type index)
Definition: RooAbsCategory.h:63
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:525
n
const Int_t n
Definition: legend1.C:16
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
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:217
RooAbsCategory::stateNames
std::map< std::string, value_type > & stateNames()
Access the map of state names to index numbers.
Definition: RooAbsCategory.h:174
RooAbsCategory::getCurrentIndex
virtual value_type getCurrentIndex() const
Return index number of current state.
Definition: RooAbsCategory.cxx:110
Roo1DTable
Definition: Roo1DTable.h:23
RooAbsCategory::_invalidCategory
static const std::map< std::string, RooAbsCategory::value_type >::value_type _invalidCategory
A category state to signify an invalid category. The first member is std::numeric_limits<int>::min(),...
Definition: RooAbsCategory.h:43
RooAbsCategory::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
Definition: RooAbsCategory.cxx:378
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:224
RooAbsCategory::lookupName
const std::string & lookupName(value_type index) const
Get the name corresponding to the given index.
Definition: RooAbsCategory.cxx:196
RooAbsCategory::value_type
int value_type
The type used to denote a specific category state.
Definition: RooAbsCategory.h:41
TTree
Definition: TTree.h:79
RooAbsCategory::RooAbsCategory
RooAbsCategory()
Definition: RooAbsCategory.h:46
RooAbsCategory::createTable
Roo1DTable * createTable(const char *label) const
Create a table matching the shape of this category.
Definition: RooAbsCategory.cxx:358
RooAbsArg.h
RooAbsCategory::syncCache
virtual void syncCache(const RooArgSet *set=0)
Explicitly synchronize RooAbsCategory internal cache.
Definition: RooAbsCategory.cxx:512
RooAbsCategory::isIdentical
virtual Bool_t isIdentical(const RooAbsArg &other, Bool_t assumeSameType=kFALSE) const
Definition: RooAbsCategory.cxx:170
R__SUGGEST_ALTERNATIVE
#define R__SUGGEST_ALTERNATIVE(ALTERNATIVE)
Definition: RConfig.hxx:530
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:208
RooAbsCategory::getLabel
const char * getLabel() const
Retrieve current label. Use getCurrentLabel() for more clarity.
Definition: RooAbsCategory.h:141
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:631
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:94
RooAbsCategory::setTreeBranchStatus
virtual void setTreeBranchStatus(TTree &t, Bool_t active)
(De)activate associate tree branch
Definition: RooAbsCategory.cxx:499
TString
Definition: TString.h:136
RooAbsCategory::operator!=
Bool_t operator!=(const RooAbsArg &other)
Definition: RooAbsCategory.h:67
RooAbsCategory::getCurrentLabel
virtual const char * getCurrentLabel() const
Return label string of current state.
Definition: RooAbsCategory.cxx:126
RooAbsCategory::getIndex
Int_t getIndex() const
Retrieve the current index. Use getCurrentIndex() for more clarity.
Definition: RooAbsCategory.h:139
RooAbsCategory::attachToVStore
virtual void attachToVStore(RooVectorDataStore &vstore)
Attach the category index and label to as branches to the given vector store.
Definition: RooAbsCategory.cxx:423
RooAbsCategory::numTypes
Int_t numTypes(const char *=0) const
Return number of types defined (in range named rangeName if rangeName!=0)
Definition: RooAbsCategory.h:135
bool
TIterator
Definition: TIterator.h:30
RooAbsCategory
Definition: RooAbsCategory.h:38
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:106
RooAbsCategory::hasIndex
bool hasIndex(value_type index) const
Check if a state with index index exists.
Definition: RooAbsCategory.cxx:183
RooCatType.h
RooAbsCategory::isValid
virtual bool isValid() const
WVE (08/21/01) Probably obsolete now.
Definition: RooAbsCategory.h:198
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:225
RooAbsCategory::createFundamental
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooCategory fundamental object with our properties.
Definition: RooAbsCategory.cxx:613
RooAbsCategory::nextAvailableStateIndex
value_type nextAvailableStateIndex() const
Definition: RooAbsCategory.cxx:680
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:436
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsCategory::_stateNames
std::map< std::string, value_type > _stateNames
Current category state.
Definition: RooAbsCategory.h:222
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:653
RooAbsCategory::_currentIndex
value_type _currentIndex
Definition: RooAbsCategory.h:221
RooAbsCategory::printValue
virtual void printValue(std::ostream &os) const
Print value (label name)
Definition: RooAbsCategory.cxx:388
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:102
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:302
RooAbsCategory::typeIterator
TIterator * typeIterator() const
Definition: RooAbsCategory.cxx:649
RooAbsCategory::defineTypeUnchecked
const RooCatType * defineTypeUnchecked(const char *label, value_type index)
Definition: RooAbsCategory.cxx:663
void
typedef void((*Func_t)())
RooAbsCategory::~RooAbsCategory
virtual ~RooAbsCategory()
Destructor.
Definition: RooAbsCategory.cxx:100
RooAbsCategory::fillTreeBranch
virtual void fillTreeBranch(TTree &t)
Fill tree branches associated with current object with current value.
Definition: RooAbsCategory.cxx:479
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:401
RooAbsCategory::_insertionOrder
std::vector< std::string > _insertionOrder
Map state names to index numbers. Make sure state names are updated in recomputeShape().
Definition: RooAbsCategory.h:223
RooAbsArg::clearShapeDirty
void clearShapeDirty() const
Definition: RooAbsArg.h:557
RooAbsArg::isShapeDirty
Bool_t isShapeDirty() const
Definition: RooAbsArg.h:424
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:568
RooAbsCategory::size
std::size_t size() const
Number of states defined.
Definition: RooAbsCategory.h:110
RooAbsArg::setShapeDirty
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:510
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
RooVectorDataStore
Definition: RooVectorDataStore.h:35
RooAbsCategory::recomputeShape
virtual void recomputeShape()=0
If a category depends on the shape of others, i.e.
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:71
RooAbsCategory::lookupIndex
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Definition: RooAbsCategory.cxx:278
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:226
RooAbsCategory::stateNames
const std::map< std::string, value_type > & stateNames() const
Access the map of state names to index numbers.
Definition: RooAbsCategory.h:164
RooCatType
Definition: RooCatTypeLegacy.h:23
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooAbsCategory::operator!=
Bool_t operator!=(const char *label)
Definition: RooAbsCategory.h:65
RooAbsArg
Definition: RooAbsArg.h:73
RooAbsArg::setValueDirty
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:505
RooAbsCategory::operator==
Bool_t operator==(value_type index) const
Equality operator with a integer (compares with state index number)
Definition: RooAbsCategory.cxx:140
RooSpan.h
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:265
RooAbsCategory::getCurrentOrdinalNumber
unsigned int getCurrentOrdinalNumber() const
Return ordinal number of the current state.
Definition: RooAbsCategory.cxx:588
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:33
RooArgSet
Definition: RooArgSet.h:28
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:669
int
RooAbsCategory::getValBatch
virtual RooSpan< const value_type > getValBatch(std::size_t, std::size_t) const
Retrieve a batch of category values for events in the range [begin, begin+batchSize).
Definition: RooAbsCategory.h:54
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:368