Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include "RooSharedProperties.h"
21
22#include <vector>
23#include <map>
24#include <string>
25
27
28class RooCategory final : public RooAbsCategoryLValue {
29public:
30 // Constructor, assignment etc.
31 RooCategory() ;
32 RooCategory(const char *name, const char *title);
33 RooCategory(const char* name, const char* title, const std::map<std::string, int>& allowedStates);
34 RooCategory(const RooCategory& other, const char* name=nullptr) ;
36 ~RooCategory() override;
37 TObject* clone(const char* newname) const override { return new RooCategory(*this,newname); }
38
39 /// Return current index.
41 return RooCategory::evaluate();
42 }
43
44 bool setIndex(Int_t index, bool printError = true) override;
46 bool setLabel(const char* label, bool printError = true) override;
48
49 // I/O streaming interface (machine readable)
50 bool readFromStream(std::istream& is, bool compact, bool verbose=false) override;
51 void writeToStream(std::ostream& os, bool compact) const override ;
52
53 bool defineType(const std::string& label);
54 bool defineType(const std::string& label, Int_t index);
55 void defineTypes(const std::map<std::string, int>& allowedStates);
56 value_type& operator[](const std::string& stateName);
57 std::map<std::string, RooAbsCategory::value_type>& states();
58
59 /// \cond LEGACY
60 bool defineType(const char* label) {
61 return defineType(std::string(label));
62 }
63 bool defineType(const char* label, Int_t index) {
64 return defineType(std::string(label), index);
65 }
66 /// \endcond
67
68 /// Clear all defined category states.
69 void clear() {
70 clearTypes();
71 }
72
73 void clearRange(const char* name, bool silent) ;
74 void setRange(const char* rangeName, const char* stateNameList) ;
75 void addToRange(const char* rangeName, RooAbsCategory::value_type stateIndex);
76 void addToRange(const char* rangeName, const char* stateNameList) ;
77
78
79 /// \name RooFit interface
80 /// @{
81
82 /// Tell whether we can be stored in a dataset. Always true for RooCategory.
83 inline bool isFundamental() const override {
84 return true;
85 }
86
87 /// Does our value or shape depend on any other arg? Always false for RooCategory.
88 bool isDerived() const override {
89 return false;
90 }
91
92 bool isStateInRange(const char* rangeName, RooAbsCategory::value_type stateIndex) const ;
93 bool isStateInRange(const char* rangeName, const char* stateName) const ;
94 /// Check if the currently defined category state is in the range with the given name.
95 /// If no ranges are defined, the state counts as being in range.
96 bool inRange(const char* rangeName) const override {
97 return isStateInRange(rangeName, RooCategory::evaluate());
98 }
99 /// Returns true if category has a range with given name defined.
100 bool hasRange(const char* rangeName) const override {
101 return _ranges->find(rangeName) != _ranges->end();
102 }
103
104 ///@}
105
106protected:
107 /// \copydoc RooAbsCategory::evaluate() const
108 /// Returns the currently set state index. If this is invalid,
109 /// returns the first-set index.
110 value_type evaluate() const override {
112 return _currentIndex;
113
114 if (_insertionOrder.empty()) {
115 return invalidCategory().second;
116 } else {
117 auto item = stateNames().find(_insertionOrder.front());
118 assert(item != stateNames().end());
119 return item->second;
120 }
121 }
122
123 /// This category's shape does not depend on others, and does not need recomputing.
124 void recomputeShape() override { };
125
126private:
127
128 using RangeMap_t = std::map<std::string, std::vector<value_type>>;
129 /// Map range names to allowed category states. Note that this must be shared between copies,
130 /// so categories in datasets have the same ranges as their counterparts outside of the dataset.
131 std::shared_ptr<RangeMap_t> _ranges{new RangeMap_t()}; //!
132 RangeMap_t* _rangesPointerForIO{nullptr}; ///< Pointer to the same object as _ranges, but not shared for I/O.
133
135 void installSharedRange(std::unique_ptr<RangeMap_t>&& rangeMap);
136 /// Helper for restoring shared ranges from old versions of this class read from files. Maps TUUID names to shared ranges.
137 static std::map<RooSharedProperties::UUID, std::weak_ptr<RangeMap_t>> _uuidToSharedRangeIOHelper;
138 /// Helper for restoring shared ranges from current versions of this class read from files. Maps category names to shared ranges.
139 static std::map<std::string, std::weak_ptr<RangeMap_t>> _sharedRangeIOHelper;
140
141 ClassDefOverride(RooCategory, 3) // Discrete valued variable type
142};
143
144#endif
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
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
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 printError=true)=0
Change category state by specifying a state name.
value_type _currentIndex
Current category state.
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.
std::vector< std::string > _insertionOrder
Keeps track in which order state numbers have been inserted. Make sure this is updated in recomputeSh...
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.
static const decltype(_stateNames) ::value_type & invalidCategory()
A category state to signify an invalid category.
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:28
RangeMap_t * _rangesPointerForIO
Pointer to the same object as _ranges, but not shared for I/O.
void addToRange(const char *rangeName, RooAbsCategory::value_type stateIndex)
Add the given state to the given range.
bool setIndex(Int_t index, bool printError=true) override
Set value by specifying the index code of the desired state.
bool hasRange(const char *rangeName) const override
Returns true if category has a range with given name defined.
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 writeToStream(std::ostream &os, bool compact) const override
compact only at the moment
void recomputeShape() override
This category's shape does not depend on others, and does not need recomputing.
static std::map< std::string, std::weak_ptr< RangeMap_t > > _sharedRangeIOHelper
Helper for restoring shared ranges from current versions of this class read from files....
bool isDerived() const override
Does our value or shape depend on any other arg? Always false for RooCategory.
Definition RooCategory.h:88
void installSharedRange(std::unique_ptr< RangeMap_t > &&rangeMap)
In current versions of the class, a map with ranges can be shared between instances.
bool defineType(const std::string &label)
Define a state with given name.
void clearRange(const char *name, bool silent)
Clear the named range.
std::shared_ptr< RangeMap_t > _ranges
Map range names to allowed category states.
std::map< std::string, std::vector< value_type > > RangeMap_t
value_type evaluate() const override
Evaluate the category state and return.
static std::map< RooSharedProperties::UUID, std::weak_ptr< RangeMap_t > > _uuidToSharedRangeIOHelper
Helper for restoring shared ranges from old versions of this class read from files....
value_type & operator[](const std::string &stateName)
Access a named state.
void clear()
Clear all defined category states.
Definition RooCategory.h:69
bool isFundamental() const override
Tell whether we can be stored in a dataset. Always true for RooCategory.
Definition RooCategory.h:83
TObject * clone(const char *newname) const override
Definition RooCategory.h:37
bool readFromStream(std::istream &is, bool compact, bool verbose=false) override
Read object contents from given stream.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
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 isStateInRange(const char *rangeName, RooAbsCategory::value_type stateIndex) const
Check if the state is in the given range.
value_type getCurrentIndex() const final
Return current index.
Definition RooCategory.h:40
void installLegacySharedProp(const RooCategorySharedProperties *sp)
When reading old versions of the class, we get instances of shared properties.
bool inRange(const char *rangeName) const override
Check if the currently defined category state is in the range with the given name.
Definition RooCategory.h:96
~RooCategory() override
Destructor.
Mother of all ROOT objects.
Definition TObject.h:41