File indexing completed on 2025-12-26 11:33:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef ROO_ABS_CATEGORY
0017 #define ROO_ABS_CATEGORY
0018
0019 #include "RooAbsArg.h"
0020
0021 #include <functional>
0022 #include <limits>
0023 #include <map>
0024 #include <string>
0025 #include <vector>
0026
0027 class RooCatType;
0028 class TTree;
0029 class RooVectorDataStore;
0030 class Roo1DTable;
0031 class TIterator;
0032 struct TreeReadBuffer;
0033
0034 class RooAbsCategory : public RooAbsArg {
0035 public:
0036
0037 using value_type = int;
0038
0039
0040 RooAbsCategory();
0041 RooAbsCategory(const char *name, const char *title);
0042 RooAbsCategory(const RooAbsCategory& other, const char* name=nullptr) ;
0043 ~RooAbsCategory() override;
0044
0045
0046 virtual value_type getCurrentIndex() const ;
0047 virtual const char* getCurrentLabel() const ;
0048
0049 const std::map<std::string, value_type>::value_type& getOrdinal(unsigned int n) const;
0050 unsigned int getCurrentOrdinalNumber() const;
0051
0052 bool operator==(value_type index) const ;
0053 bool operator!=(value_type index) { return !operator==(index);}
0054 bool operator==(const char* label) const ;
0055 bool operator!=(const char* label) { return !operator==(label);}
0056 bool operator==(const RooAbsArg& other) const override ;
0057 bool operator!=(const RooAbsArg& other) { return !operator==(other);}
0058 bool isIdentical(const RooAbsArg& other, bool assumeSameType=false) const override;
0059
0060
0061 bool hasLabel(const std::string& label) const {
0062 return stateNames().find(label) != stateNames().end();
0063 }
0064
0065 bool hasIndex(value_type index) const;
0066
0067
0068
0069 const std::string& lookupName(value_type index) const;
0070 value_type lookupIndex(const std::string& stateName) const;
0071
0072
0073 bool isSignType(bool mustHaveZero=false) const ;
0074
0075 Roo1DTable *createTable(const char *label) const ;
0076
0077
0078 bool readFromStream(std::istream& is, bool compact, bool verbose=false) override ;
0079 void writeToStream(std::ostream& os, bool compact) const override ;
0080
0081 void printValue(std::ostream& os) const override ;
0082 void printMultiline(std::ostream& os, Int_t contents, bool verbose=false, TString indent="") const override ;
0083
0084 virtual bool isIntegrationSafeLValue(const RooArgSet* ) const {
0085
0086 return true ;
0087 }
0088
0089 RooFit::OwningPtr<RooAbsArg> createFundamental(const char* newname=nullptr) const override;
0090
0091
0092 std::map<std::string, value_type>::const_iterator begin() const {
0093 return stateNames().cbegin();
0094 }
0095
0096 std::map<std::string, value_type>::const_iterator end() const {
0097 return stateNames().cend();
0098 }
0099
0100 std::size_t size() const {
0101 return stateNames().size();
0102 }
0103
0104 bool empty() const {
0105 return stateNames().empty();
0106 }
0107
0108 bool isCategory() const override { return true; }
0109
0110
0111
0112
0113
0114
0115
0116
0117 const RooCatType*
0118 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName()")
0119 lookupType(value_type index, bool printError=false) const;
0120 const RooCatType*
0121 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupIndex()")
0122 lookupType(const char* label, bool printError=false) const;
0123 const RooCatType*
0124 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use lookupName() / lookupIndex()")
0125 lookupType(const RooCatType& type, bool printError=false) const;
0126 TIterator*
0127 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use begin(), end() or range-based for loops.")
0128 typeIterator() const;
0129
0130 Int_t numTypes(const char* =nullptr) const {
0131 return stateNames().size();
0132 }
0133
0134 Int_t getIndex() const { return getCurrentIndex(); }
0135
0136 const char* getLabel() const { return getCurrentLabel(); }
0137 protected:
0138 virtual bool
0139 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use hasIndex() or hasLabel().")
0140 isValid(const RooCatType& value) const ;
0141
0142 const RooCatType*
0143 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
0144 defineType(const char* label);
0145
0146 const RooCatType*
0147 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineState().")
0148 defineType(const char* label, int index);
0149
0150 const RooCatType*
0151 R__SUGGEST_ALTERNATIVE("This interface is inefficient. Use defineTypeUnchecked().")
0152 defineTypeUnchecked(const char* label, value_type index);
0153
0154
0155
0156 protected:
0157
0158
0159 const std::map<std::string, value_type>& stateNames() const {
0160 if (isShapeDirty()) {
0161 _legacyStates.clear();
0162 const_cast<RooAbsCategory*>(this)->recomputeShape();
0163 clearShapeDirty();
0164 }
0165
0166 return _stateNames;
0167 }
0168
0169 std::map<std::string, value_type>& stateNames() {
0170 if (isShapeDirty()) {
0171 _legacyStates.clear();
0172 recomputeShape();
0173 clearShapeDirty();
0174 }
0175
0176
0177 setShapeDirty();
0178
0179 return _stateNames;
0180 }
0181
0182
0183
0184 virtual value_type evaluate() const = 0;
0185
0186
0187 virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label);
0188 virtual const std::map<std::string, RooAbsCategory::value_type>::value_type& defineState(const std::string& label, value_type index);
0189
0190 void defineStateUnchecked(const std::string& label, value_type index);
0191 void clearTypes() ;
0192
0193 bool isValid() const override {
0194 return hasIndex(_currentIndex);
0195 }
0196
0197
0198
0199
0200
0201
0202 virtual void recomputeShape() = 0;
0203
0204 friend class RooVectorDataStore ;
0205 void syncCache(const RooArgSet* set=nullptr) override ;
0206 void copyCache(const RooAbsArg* source, bool valueOnly=false, bool setValueDirty=true) override ;
0207 void setCachedValue(double value, bool notifyClients = true) final;
0208 void attachToTree(TTree& t, Int_t bufSize=32000) override ;
0209 void attachToVStore(RooVectorDataStore& vstore) override ;
0210 void setTreeBranchStatus(TTree& t, bool active) override ;
0211 void fillTreeBranch(TTree& t) override ;
0212
0213 RooCatType* retrieveLegacyState(value_type index) const;
0214 value_type nextAvailableStateIndex() const;
0215
0216
0217 mutable value_type _currentIndex{std::numeric_limits<int>::min()};
0218 std::map<std::string, value_type> _stateNames;
0219 std::vector<std::string> _insertionOrder;
0220 mutable std::map<value_type, std::unique_ptr<RooCatType, std::function<void(RooCatType*)>> > _legacyStates;
0221
0222 static const decltype(_stateNames)::value_type& invalidCategory();
0223
0224 private:
0225 TreeReadBuffer *_treeReadBuffer = nullptr;
0226
0227 ClassDefOverride(RooAbsCategory, 4)
0228 };
0229
0230 #endif