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
27class RooCatType;
28#else
29#include "RooCatType.h"
30#endif
31
32class TTree;
34class Roo1DTable;
35class TIterator;
36struct TreeReadBuffer; /// A space to attach TBranches
37
38class RooAbsCategory : public RooAbsArg {
39public:
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=nullptr) ;
47 ~RooAbsCategory() override;
48
49 // Value accessors
50 virtual value_type getCurrentIndex() const ;
51 virtual const char* getCurrentLabel() const ;
52
53 const std::map<std::string, value_type>::value_type& getOrdinal(unsigned int n) const;
54 unsigned int getCurrentOrdinalNumber() const;
55
56 bool operator==(value_type index) const ;
58 bool operator==(const char* label) const ;
59 bool operator!=(const char* label) { return !operator==(label);}
60 bool operator==(const RooAbsArg& other) const override ;
61 bool operator!=(const RooAbsArg& other) { return !operator==(other);}
62 bool isIdentical(const RooAbsArg& other, bool assumeSameType=false) const override;
63
64 /// Check if a state with name `label` exists.
65 bool hasLabel(const std::string& label) const {
66 return stateNames().find(label) != stateNames().end();
67 }
68 /// Check if a state with index `index` exists.
69 bool hasIndex(value_type index) const;
70
71 /// Get the name corresponding to the given index.
72 /// \return Name or empty string if index is invalid.
73 const std::string& lookupName(value_type index) const;
74 value_type lookupIndex(const std::string& stateName) const;
75
76
77 bool isSignType(bool mustHaveZero=false) const ;
78
79 Roo1DTable *createTable(const char *label) const ;
80
81 // I/O streaming interface
82 bool readFromStream(std::istream& is, bool compact, bool verbose=false) override ;
83 void writeToStream(std::ostream& os, bool compact) const override ;
84
85 void printValue(std::ostream& os) const override ;
86 void printMultiline(std::ostream& os, Int_t contents, bool verbose=false, TString indent="") const override ;
87
88 virtual bool isIntegrationSafeLValue(const RooArgSet* /*set*/) const {
89 // Is this l-value object safe for use as integration observable
90 return true ;
91 }
92
93 RooAbsArg *createFundamental(const char* newname=nullptr) const override;
94
95 /// Iterator for category state names. Points to pairs of index and name.
96 std::map<std::string, value_type>::const_iterator begin() const {
97 return stateNames().cbegin();
98 }
99 /// Iterator for category state names. Points to pairs of index and name.
100 std::map<std::string, value_type>::const_iterator end() const {
101 return stateNames().cend();
102 }
103 /// Number of states defined.
104 std::size_t size() const {
105 return stateNames().size();
106 }
107 /// If there are no states defined
108 bool empty() const {
109 return stateNames().empty();
110 }
111
112 bool isCategory() const override { return true; }
113
114 /// \name Legacy interface
115 /// Previous versions of RooAbsCategory were based on RooCatType, a class containing a state and a label.
116 /// It has been replaced by integers, which use less space and allow for faster access. The following part of the interface
117 /// should not be used if possible.
118 /// Since RooCatType in essence is only an index and a state name, equivalent functionality can be achieved using begin()
119 /// and end() to iterate through pairs of <index, stateName> and by using using lookupName() and lookupIndex().
120 /// @{
121 const RooCatType*
122 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName()")
123 lookupType(value_type index, bool printError=false) const;
124 const RooCatType*
125 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupIndex()")
126 lookupType(const char* label, bool printError=false) const;
127 const RooCatType*
128 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName() / lookupIndex()")
129 lookupType(const RooCatType& type, bool printError=false) const;
130 TIterator*
131 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use begin(), end() or range-based for loops.")
132 typeIterator() const;
133 /// Return number of types defined (in range named rangeName if rangeName!=nullptr)
134 Int_t numTypes(const char* /*rangeName*/=nullptr) const {
135 return stateNames().size();
136 }
137 /// Retrieve the current index. Use getCurrentIndex() for more clarity.
138 Int_t getIndex() const { return getCurrentIndex(); }
139 /// Retrieve current label. Use getCurrentLabel() for more clarity.
140 const char* getLabel() const { return getCurrentLabel(); }
141protected:
142 virtual bool
143 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use hasIndex() or hasLabel().")
144 isValid(const RooCatType& value) const ;
145 /// \deprecated Use defineState(const std::string& label)
146 const RooCatType*
147 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
148 defineType(const char* label);
149 /// \deprecated Use defineState(const std::string& label, value_type index)
150 const RooCatType*
151 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
152 defineType(const char* label, int index);
153 /// \deprecated Use defineStateUnchecked(const std::string& label, value_type index)
154 const RooCatType*
155 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineTypeUnchecked().")
156 defineTypeUnchecked(const char* label, value_type index);
157 /// @}
158
159
160protected:
161 /// Access the map of state names to index numbers. Triggers a recomputation
162 /// if the shape is dirty.
163 const std::map<std::string, value_type>& stateNames() const {
164 if (isShapeDirty()) {
165 _legacyStates.clear();
166 const_cast<RooAbsCategory*>(this)->recomputeShape();
168 }
169
170 return _stateNames;
171 }
172 /// \copydoc stateNames() const
173 std::map<std::string, value_type>& stateNames() {
174 if (isShapeDirty()) {
175 _legacyStates.clear();
178 }
179
180 //Somebody might modify the states
182
183 return _stateNames;
184 }
185
186 /// Evaluate the category state and return.
187 /// The returned state index should correspond to a state name that has been defined via e.g. defineType().
188 virtual value_type evaluate() const = 0;
189
190 // Type definition management
191 virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label);
192 virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label, value_type index);
193
194 void defineStateUnchecked(const std::string& label, value_type index);
195 void clearTypes() ;
196
197 bool isValid() const override {
198 return hasIndex(_currentIndex);
199 }
200
201 /// If a category depends on the shape of others, i.e.\ its state numbers or names depend
202 /// on the states of other categories, this function has to be implemented to recompute
203 /// _stateNames and _insertionOrder.
204 /// If one of these two changes, setShapeDirty() has to be called to propagate this information
205 /// to possible users of this category.
206 virtual void recomputeShape() = 0;
207
208 friend class RooVectorDataStore ;
209 void syncCache(const RooArgSet* set=nullptr) override ;
210 void copyCache(const RooAbsArg* source, bool valueOnly=false, bool setValueDirty=true) override ;
211 void setCachedValue(double value, bool notifyClients = true) final;
212 void attachToTree(TTree& t, Int_t bufSize=32000) override ;
213 void attachToVStore(RooVectorDataStore& vstore) override ;
214 void setTreeBranchStatus(TTree& t, bool active) override ;
215 void fillTreeBranch(TTree& t) override ;
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 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.
225
226 static const decltype(_stateNames)::value_type& invalidCategory();
227
228private:
229 std::unique_ptr<TreeReadBuffer> _treeReadBuffer; //! A buffer for reading values from trees
230
231 ClassDefOverride(RooAbsCategory, 4) // Abstract discrete variable
232};
233
234#endif
#define R__SUGGEST_ALTERNATIVE(ALTERNATIVE)
Definition: RConfig.hxx:524
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
static void indent(ostringstream &buf, int indent_level)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition: TGX11.cxx:110
Roo1DTable implements a one-dimensional table.
Definition: Roo1DTable.h:23
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:71
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:495
bool isShapeDirty() const
Definition: RooAbsArg.h:416
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:490
void clearShapeDirty() const
Definition: RooAbsArg.h:604
A space to attach TBranches.
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...
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.
void setCachedValue(double value, bool notifyClients=true) final
Overwrite the value stored in this object's cache.
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.
virtual bool isIntegrationSafeLValue(const RooArgSet *) const
const std::string & lookupName(value_type index) const
Get the name corresponding to the given index.
TIterator * typeIterator() const
bool operator==(value_type index) const
Equality operator with a integer (compares with state index number)
RooCatType * retrieveLegacyState(value_type index) const
Return the legacy RooCatType corresponding to index. If it doesn't exist, create one.
void copyCache(const RooAbsArg *source, bool valueOnly=false, bool setValueDirty=true) override
Copy the cached value from given source and raise dirty flag.
value_type _currentIndex
Current category state.
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.
const RooCatType * defineTypeUnchecked(const char *label, value_type index)
void setTreeBranchStatus(TTree &t, bool active) override
(De)activate associate tree branch
const RooCatType * lookupType(value_type index, bool printError=false) const
Find our type corresponding to the specified index, or return nullptr for no match.
void writeToStream(std::ostream &os, bool compact) const override
Write object contents to ostream.
Int_t getIndex() const
Retrieve the current index. Use getCurrentIndex() for more clarity.
value_type nextAvailableStateIndex() const
bool operator!=(const char *label)
const char * getLabel() const
Retrieve current label. Use getCurrentLabel() for more clarity.
bool readFromStream(std::istream &is, bool compact, bool verbose=false) override
Read object contents from stream (dummy for now)
bool isSignType(bool mustHaveZero=false) const
Determine if category has 2 or 3 states with index values -1,0,1.
std::map< std::string, value_type >::const_iterator end() const
Iterator for category state names. Points to pairs of index and name.
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 value_type evaluate() const =0
Evaluate the category state and return.
int value_type
The type used to denote a specific category state.
bool operator!=(const RooAbsArg &other)
Int_t numTypes(const char *=nullptr) const
Return number of types defined (in range named rangeName if rangeName!=nullptr)
void syncCache(const RooArgSet *set=nullptr) override
Explicitly synchronize RooAbsCategory internal cache.
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.
RooAbsArg * createFundamental(const char *newname=nullptr) const override
Create a RooCategory fundamental object with our properties.
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Print info about this object to the specified stream.
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.
bool isIdentical(const RooAbsArg &other, bool assumeSameType=false) const override
~RooAbsCategory() override
Destructor.
std::vector< std::string > _insertionOrder
Keeps track in which order state numbers have been inserted. Make sure this is updated in recomputeSh...
std::map< std::string, value_type > _stateNames
Map state names to index numbers. Make sure state names are updated in recomputeShape().
bool isValid() const override
WVE (08/21/01) Probably obsolete now.
const RooCatType * defineType(const char *label)
const std::map< std::string, value_type > & stateNames() const
Access the map of state names to index numbers.
bool hasIndex(value_type index) const
Check if a state with index index exists.
void attachToVStore(RooVectorDataStore &vstore) override
Attach the category index and label to as branches to the given vector store.
static const decltype(_stateNames) ::value_type & invalidCategory()
A category state to signify an invalid category.
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
std::unique_ptr< TreeReadBuffer > _treeReadBuffer
void attachToTree(TTree &t, Int_t bufSize=32000) override
Attach the category index and label as branches to the given TTree.
void fillTreeBranch(TTree &t) override
Fill tree branches associated with current object with current value.
bool empty() const
If there are no states defined.
bool isCategory() const override
void clearTypes()
Delete all currently defined states.
void printValue(std::ostream &os) const override
Print value (label name)
bool operator!=(value_type index)
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
RooVectorDataStore uses std::vectors to store data columns.
Iterator abstract base class.
Definition: TIterator.h:30
Basic string class.
Definition: TString.h:136
A TTree represents a columnar dataset.
Definition: TTree.h:79
const Int_t n
Definition: legend1.C:16
for(Int_t i=0;i< n;i++)
Definition: legend1.C:18
void(off) SmallVectorTemplateBase< T
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:167