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 
19 #include "RooAbsCategoryLValue.h"
20 
21 #include <vector>
22 #include <map>
23 #include <string>
24 
26 
27 class RooCategory final : public RooAbsCategoryLValue {
28 public:
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) ;
34  RooCategory& operator=(const RooCategory&) = delete;
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 
105 protected:
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 {
110  if (hasIndex(_currentIndex))
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 
125 private:
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
RooCategory::~RooCategory
virtual ~RooCategory()
Destructor.
Definition: RooCategory.cxx:150
RooCategory::setRange
void setRange(const char *rangeName, const char *stateNameList)
Definition: RooCategory.cxx:335
RooCategory::inRange
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
RooAbsCategoryLValue::setLabel
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
RooCategory::clone
virtual TObject * clone(const char *newname) const override
Definition: RooCategory.h:36
RooCategory::defineTypes
void defineTypes(const std::map< std::string, int > &allowedStates)
Define multiple states in a single call.
Definition: RooCategory.cxx:243
RooCategory::setIndex
virtual bool setIndex(value_type index, bool printError=true)=0
Change category state by specifying the index code of the desired state.
RooCategory::RooCategory
RooCategory()
Definition: RooCategory.cxx:103
RooCategory::installLegacySharedProp
void installLegacySharedProp(const RooCategorySharedProperties *sp)
When reading old versions of the class, we get instances of shared properties.
Definition: RooCategory.cxx:479
RooAbsCategory::value_type
int value_type
The type used to denote a specific category state.
Definition: RooAbsCategory.h:40
RooCategorySharedProperties
RooCategorySharedProperties is the container for all properties that are shared between instance of R...
Definition: RooCategorySharedProperties.h:22
RooCategory::RangeMap_t
std::map< std::string, std::vector< value_type > > RangeMap_t
Definition: RooCategory.h:127
RooCategory::operator[]
value_type & operator[](const std::string &stateName)
Access a named state.
Definition: RooCategory.cxx:255
RooCategory::clearRange
void clearRange(const char *name, Bool_t silent)
Clear the named range.
Definition: RooCategory.cxx:320
RooCategory::installSharedRange
void installSharedRange(std::unique_ptr< RangeMap_t > &&rangeMap)
In current versions of the class, a map with ranges can be shared between instances.
Definition: RooCategory.cxx:514
RooCategory::evaluate
virtual value_type evaluate() const override
Evaluate the category state and return.
Definition: RooCategory.h:109
ClassDefOverride
#define ClassDefOverride(name, id)
Definition: Rtypes.h:329
RooCategory::defineType
bool defineType(const std::string &label)
Define a state with given name.
Definition: RooCategory.cxx:208
bool
RooAbsCategory::end
std::map< std::string, value_type >::const_iterator end() const
Iterator for category state names. Points to pairs of index and name.
Definition: RooAbsCategory.h:99
RooAbsCategory::hasIndex
bool hasIndex(value_type index) const
Check if a state with index index exists.
Definition: RooAbsCategory.cxx:188
RooAbsCategory::invalidCategory
static decltype(_stateNames) const ::value_type & invalidCategory()
Is this category attached to a tree?
RooAbsCategoryLValue::setIndex
virtual bool setIndex(value_type index, bool printError=true)=0
Change category state by specifying the index code of the desired state.
RooCategory::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact) const override
compact only at the moment
Definition: RooCategory.cxx:305
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooCategory::readFromStream
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE) override
Read object contents from given stream.
Definition: RooCategory.cxx:287
RooAbsCategory::_currentIndex
value_type _currentIndex
Definition: RooAbsCategory.h:214
RooCategory::states
std::map< std::string, RooAbsCategory::value_type > & states()
Return a reference to the map of state names to index states.
Definition: RooCategory.cxx:274
RooCategory::_ranges
std::shared_ptr< RangeMap_t > _ranges
Map range names to allowed category states.
Definition: RooCategory.h:130
RooCategory::hasRange
virtual bool hasRange(const char *rangeName) const override
Returns true if category has a range with given name defined.
Definition: RooCategory.h:99
RooAbsCategory::_insertionOrder
std::vector< std::string > _insertionOrder
Map state names to index numbers. Make sure state names are updated in recomputeShape().
Definition: RooAbsCategory.h:216
RooCategory::addToRange
void addToRange(const char *rangeName, RooAbsCategory::value_type stateIndex)
Add the given state to the given range.
Definition: RooCategory.cxx:347
RooCategory::_uuidToSharedRangeIOHelper
static std::map< std::string, std::weak_ptr< RangeMap_t > > _uuidToSharedRangeIOHelper
Definition: RooCategory.h:136
RooCategory::clear
void clear()
Clear all defined category states.
Definition: RooCategory.h:68
RooCategory::_rangesPointerForIO
RangeMap_t * _rangesPointerForIO
Definition: RooCategory.h:131
RooCategory
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
RooAbsCategoryLValue.h
RooCategory::isDerived
virtual Bool_t isDerived() const override
Does our value or shape depend on any other arg? Always false for RooCategory.
Definition: RooCategory.h:87
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
RooCategory::isFundamental
virtual Bool_t isFundamental() const override
Tell whether we can be stored in a dataset. Always true for RooCategory.
Definition: RooCategory.h:82
name
char name[80]
Definition: TGX11.cxx:110
RooCategory::isStateInRange
Bool_t isStateInRange(const char *rangeName, RooAbsCategory::value_type stateIndex) const
Check if the state is in the given range.
Definition: RooCategory.cxx:395
RooCategory::operator=
RooCategory & operator=(const RooCategory &)=delete
RooAbsCategory::stateNames
const std::map< std::string, value_type > & stateNames() const
Access the map of state names to index numbers.
Definition: RooAbsCategory.h:157
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooCategory::setLabel
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
RooAbsCategoryLValue
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
Definition: RooAbsCategoryLValue.h:25
RooCategory::getCurrentIndex
virtual value_type getCurrentIndex() const override final
Return current index.
Definition: RooCategory.h:39
RooCategory::recomputeShape
void recomputeShape() override
This category's shape does not depend on others, and does not need recomputing.
Definition: RooCategory.h:123
RooAbsCategory::clearTypes
void clearTypes()
Delete all currently defined states.
Definition: RooAbsCategory.cxx:270
RooCategory::_sharedRangeIOHelper
static std::map< std::string, std::weak_ptr< RangeMap_t > > _sharedRangeIOHelper
Definition: RooCategory.h:138
int