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