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
21#include <vector>
22#include <map>
23#include <string>
24
26
27class RooCategory final : public RooAbsCategoryLValue {
28public:
29 // Constructor, assignment etc.
30 RooCategory() ;
31 RooCategory(const char *name, const char *title);
32 RooCategory(const char* name, const char* title, const std::map<std::string, int>& allowedStates);
33 RooCategory(const RooCategory& other, const char* name=0) ;
35 virtual ~RooCategory();
36 virtual TObject* clone(const char* newname) const override { return new RooCategory(*this,newname); }
37
38 /// Return current index.
39 virtual value_type getCurrentIndex() const override final {
40 return RooCategory::evaluate();
41 }
42
43 virtual Bool_t setIndex(Int_t index, bool printError = true) override;
45 virtual Bool_t setLabel(const char* label, bool printError = true) override;
47
48 // I/O streaming interface (machine readable)
49 virtual Bool_t readFromStream(std::istream& is, Bool_t compact, Bool_t verbose=kFALSE) override;
50 virtual void writeToStream(std::ostream& os, Bool_t compact) const override ;
51
52 bool defineType(const std::string& label);
53 bool defineType(const std::string& label, Int_t index);
54 void defineTypes(const std::map<std::string, int>& allowedStates);
55 value_type& operator[](const std::string& stateName);
56 std::map<std::string, RooAbsCategory::value_type>& states();
57
58 /// \cond LEGACY
59 bool defineType(const char* label) {
60 return defineType(std::string(label));
61 }
62 bool defineType(const char* label, Int_t index) {
63 return defineType(std::string(label), index);
64 }
65 /// \endcond
66
67 /// Clear all defined category states.
68 void clear() {
69 clearTypes();
70 }
71
72 void clearRange(const char* name, Bool_t silent) ;
73 void setRange(const char* rangeName, const char* stateNameList) ;
74 void addToRange(const char* rangeName, RooAbsCategory::value_type stateIndex);
75 void addToRange(const char* rangeName, const char* stateNameList) ;
76
77
78 /// \name RooFit interface
79 /// @{
80
81 /// Tell whether we can be stored in a dataset. Always true for RooCategory.
82 inline virtual Bool_t isFundamental() const override {
83 return true;
84 }
85
86 /// Does our value or shape depend on any other arg? Always false for RooCategory.
87 virtual Bool_t isDerived() const override {
88 return false;
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
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
#define ClassDefOverride(name, id)
Definition: Rtypes.h:329
char name[80]
Definition: TGX11.cxx:110
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.
static const decltype(_stateNames) ::value_type & invalidCategory()
Is this category attached to a tree?
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.
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:27
virtual Bool_t isFundamental() const override
Tell whether we can be stored in a dataset. Always true for RooCategory.
Definition: RooCategory.h:82
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:39
value_type & operator[](const std::string &stateName)
Access a named state.
void clear()
Clear all defined category states.
Definition: RooCategory.h:68
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:87
void clearRange(const char *name, Bool_t silent)
Clear the named range.
virtual TObject * clone(const char *newname) const override
Definition: RooCategory.h:36
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.
Mother of all ROOT objects.
Definition: TObject.h:37