Logo ROOT  
Reference Guide
RooCategory.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooCategory.h,v 1.27 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_CATEGORY
17#define ROO_CATEGORY
18
20
22
23class RooCategory final : public RooAbsCategoryLValue {
24public:
25 // Constructor, assignment etc.
26 RooCategory() ;
27 RooCategory(const char *name, const char *title);
28 RooCategory(const char* name, const char* title, const std::map<std::string, int>& allowedStates);
29 RooCategory(const RooCategory& other, const char* name=0) ;
31 virtual ~RooCategory();
32 virtual TObject* clone(const char* newname) const override { return new RooCategory(*this,newname); }
33
34 /// Return current index.
35 virtual value_type getCurrentIndex() const override final {
36 return RooCategory::evaluate();
37 }
38
39 virtual Bool_t setIndex(Int_t index, bool printError = true) override;
41 virtual Bool_t setLabel(const char* label, bool printError = true) override;
43
44 // I/O streaming interface (machine readable)
45 virtual Bool_t readFromStream(std::istream& is, Bool_t compact, Bool_t verbose=kFALSE) override;
46 virtual void writeToStream(std::ostream& os, Bool_t compact) const override ;
47
48 bool defineType(const std::string& label);
49 bool defineType(const std::string& label, Int_t index);
50 void defineTypes(const std::map<std::string, int>& allowedStates);
51 value_type& operator[](const std::string& stateName);
52 std::map<std::string, RooAbsCategory::value_type>& states();
53
54 /// \cond LEGACY
55 bool defineType(const char* label) {
56 return defineType(std::string(label));
57 }
58 bool defineType(const char* label, Int_t index) {
59 return defineType(std::string(label), index);
60 }
61 /// \endcond
62
63 /// Clear all defined category states.
64 void clear() {
65 clearTypes();
66 }
67
68 void clearRange(const char* name, Bool_t silent) ;
69 void setRange(const char* rangeName, const char* stateNameList) ;
70 void addToRange(const char* rangeName, RooAbsCategory::value_type stateIndex);
71 void addToRange(const char* rangeName, const char* stateNameList) ;
72
73
74 /// \name RooFit interface
75 /// @{
76
77 /// Tell whether we can be stored in a dataset. Always true for RooCategory.
78 inline virtual Bool_t isFundamental() const override {
79 return true;
80 }
81
82 /// Does our value or shape depend on any other arg? Always false for RooCategory.
83 virtual Bool_t isDerived() const override {
84 return false;
85 }
86
87 virtual RooSpan<const value_type> getValBatch(std::size_t /*begin*/, std::size_t /*batchSize*/) const override {
88 throw std::logic_error("Not implemented yet.");
89 }
90
91 Bool_t isStateInRange(const char* rangeName, RooAbsCategory::value_type stateIndex) const ;
92 Bool_t isStateInRange(const char* rangeName, const char* stateName) const ;
93 /// Check if the currently defined category state is in the range with the given name.
94 /// If no ranges are defined, the state counts as being in range.
95 virtual Bool_t inRange(const char* rangeName) const override {
96 return isStateInRange(rangeName, RooCategory::evaluate());
97 }
98 /// Returns true if category has a range with given name defined.
99 virtual bool hasRange(const char* rangeName) const override {
100 return _ranges->find(rangeName) != _ranges->end();
101 }
102
103 ///@}
104
105protected:
106 /// \copydoc RooAbsCategory::evaluate() const
107 /// Returns the currently set state index. If this is invalid,
108 /// returns the first-set index.
109 virtual value_type evaluate() const override {
111 return _currentIndex;
112
113 if (_insertionOrder.empty()) {
114 return invalidCategory().second;
115 } else {
116 auto item = stateNames().find(_insertionOrder.front());
117 assert(item != stateNames().end());
118 return item->second;
119 }
120 }
121
122 /// This category's shape does not depend on others, and does not need recomputing.
123 void recomputeShape() override { };
124
125private:
126
127 using RangeMap_t = std::map<std::string, std::vector<value_type>>;
128 /// Map range names to allowed category states. Note that this must be shared between copies,
129 /// so categories in datasets have the same ranges as their counterparts outside of the dataset.
130 std::shared_ptr<RangeMap_t> _ranges{new RangeMap_t()}; //!
131 RangeMap_t* _rangesPointerForIO{nullptr}; // Pointer to the same object as _ranges, but not shared for I/O.
132
134 void installSharedRange(std::unique_ptr<RangeMap_t>&& rangeMap);
135 // Helper for restoring shared ranges from old versions of this class read from files. Maps TUUID names to shared ranges.
136 static std::map<std::string, std::weak_ptr<RangeMap_t>> _uuidToSharedRangeIOHelper;
137 // Helper for restoring shared ranges from current versions of this class read from files. Maps category names to shared ranges.
138 static std::map<std::string, std::weak_ptr<RangeMap_t>> _sharedRangeIOHelper;
139
140 ClassDefOverride(RooCategory, 3) // Discrete valued variable type
141};
142
143#endif
const Bool_t kFALSE
Definition: RtypesCore.h:90
#define ClassDefOverride(name, id)
Definition: Rtypes.h:326
char name[80]
Definition: TGX11.cxx:109
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
virtual bool setIndex(value_type index, bool printError=true)=0
Change category state by specifying the index code of the desired state.
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
value_type _currentIndex
std::map< std::string, value_type >::const_iterator end() const
Iterator for category state names. Points to pairs of index and name.
int value_type
The type used to denote a specific category state.
bool hasIndex(value_type index) const
Check if a state with index index exists.
std::vector< std::string > _insertionOrder
Map state names to index numbers. Make sure state names are updated in recomputeShape().
const std::map< std::string, value_type > & stateNames() const
Access the map of state names to index numbers.
static const decltype(_stateNames) ::value_type & invalidCategory()
Is this category attached to a tree?
void clearTypes()
Delete all currently defined states.
RooCategorySharedProperties is the container for all properties that are shared between instance of R...
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:23
virtual Bool_t isFundamental() const override
Tell whether we can be stored in a dataset. Always true for RooCategory.
Definition: RooCategory.h:78
virtual RooSpan< const value_type > getValBatch(std::size_t, std::size_t) const override
Retrieve a batch of category values for events in the range [begin, begin+batchSize).
Definition: RooCategory.h:87
RangeMap_t * _rangesPointerForIO
Definition: RooCategory.h:131
void addToRange(const char *rangeName, RooAbsCategory::value_type stateIndex)
Add the given state to the given range.
virtual void writeToStream(std::ostream &os, Bool_t compact) const override
compact only at the moment
void setRange(const char *rangeName, const char *stateNameList)
void defineTypes(const std::map< std::string, int > &allowedStates)
Define multiple states in a single call.
void recomputeShape() override
This category's shape does not depend on others, and does not need recomputing.
Definition: RooCategory.h:123
static std::map< std::string, std::weak_ptr< RangeMap_t > > _sharedRangeIOHelper
Definition: RooCategory.h:138
void installSharedRange(std::unique_ptr< RangeMap_t > &&rangeMap)
In current versions of the class, a map with ranges can be shared between instances.
virtual value_type evaluate() const override
Evaluate the category state and return.
Definition: RooCategory.h:109
bool defineType(const std::string &label)
Define a state with given name.
virtual ~RooCategory()
Destructor.
std::shared_ptr< RangeMap_t > _ranges
Map range names to allowed category states.
Definition: RooCategory.h:130
std::map< std::string, std::vector< value_type > > RangeMap_t
Definition: RooCategory.h:127
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE) override
Read object contents from given stream.
virtual Bool_t setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
virtual Bool_t inRange(const char *rangeName) const override
Check if the currently defined category state is in the range with the given name.
Definition: RooCategory.h:95
virtual value_type getCurrentIndex() const override final
Return current index.
Definition: RooCategory.h:35
value_type & operator[](const std::string &stateName)
Access a named state.
void clear()
Clear all defined category states.
Definition: RooCategory.h:64
static std::map< std::string, std::weak_ptr< RangeMap_t > > _uuidToSharedRangeIOHelper
Definition: RooCategory.h:136
virtual Bool_t setIndex(Int_t index, bool printError=true) override
Set value by specifying the index code of the desired state.
virtual bool hasRange(const char *rangeName) const override
Returns true if category has a range with given name defined.
Definition: RooCategory.h:99
virtual Bool_t isDerived() const override
Does our value or shape depend on any other arg? Always false for RooCategory.
Definition: RooCategory.h:83
void clearRange(const char *name, Bool_t silent)
Clear the named range.
virtual TObject * clone(const char *newname) const override
Definition: RooCategory.h:32
std::map< std::string, RooAbsCategory::value_type > & states()
Return a reference to the map of state names to index states.
RooCategory & operator=(const RooCategory &)=delete
Bool_t isStateInRange(const char *rangeName, RooAbsCategory::value_type stateIndex) const
Check if the state is in the given range.
void installLegacySharedProp(const RooCategorySharedProperties *sp)
When reading old versions of the class, we get instances of shared properties.
A simple container to hold a batch of data values.
Definition: RooSpan.h:32
Mother of all ROOT objects.
Definition: TObject.h:37