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