Logo ROOT   master
Reference Guide
RooAbsCategory.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id: RooAbsCategory.h,v 1.38 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_ABS_CATEGORY
17 #define ROO_ABS_CATEGORY
18 
19 #include "RooAbsArg.h"
20 #include "RooCatType.h"
21 #include "TIterator.h"
22 #include "RooSpan.h"
23 
24 class TTree ;
25 class RooArgSet ;
26 class RooDataSet ;
27 class Roo1DTable ;
28 class RooVectorDataStore ;
29 
30 class RooAbsCategory : public RooAbsArg {
31 public:
32  /// The type used to denote a specific category state.
33  using value_type = int;
34 
35  // Constructors, assignment etc.
36  RooAbsCategory() : _byteValue(0), _treeVar(false) { };
37  RooAbsCategory(const char *name, const char *title);
38  RooAbsCategory(const RooAbsCategory& other, const char* name=0) ;
39  virtual ~RooAbsCategory();
40 
41  // Value accessors
42  virtual value_type getIndex() const ;
43  /// Retrieve a batch of category values for events in the range [begin, begin+batchSize).
44  virtual RooSpan<const value_type> getValBatch(std::size_t /*begin*/, std::size_t /*batchSize*/) const {
45  throw std::logic_error("Batch values are not implemented for RooAbsCategory.");
46  }
47  virtual const char* getLabel() const ;
48  Bool_t operator==(value_type index) const ;
49  Bool_t operator!=(value_type index) { return !operator==(index);}
50  Bool_t operator==(const char* label) const ;
51  Bool_t operator!=(const char* label) { return !operator==(label);}
52  virtual Bool_t operator==(const RooAbsArg& other) const ;
53  Bool_t operator!=(const RooAbsArg& other) { return !operator==(other);}
54  virtual Bool_t isIdentical(const RooAbsArg& other, Bool_t assumeSameType=kFALSE) const;
55 
56  Bool_t isValidIndex(value_type index) const ;
57  Bool_t isValidLabel(const char* label) const ;
58  const RooCatType* lookupType(value_type index, Bool_t printError=kFALSE) const ;
59  const RooCatType* lookupType(const char* label, Bool_t printError=kFALSE) const ;
60  const RooCatType* lookupType(const RooCatType& type, Bool_t printError=kFALSE) const ;
61  /// \deprecated Iterator over types. Use range-based for loops instead.
62  TIterator*
63  R__SUGGEST_ALTERNATIVE("Use begin(), end() or range-based for loops.")
64  typeIterator() const {
65  return new LegacyIterator(_types);
66  }
67  /// Return number of types defined (in range named rangeName if rangeName!=0)
68  Int_t numTypes(const char* /*rangeName*/=0) const {
69  return _types.size();
70  }
71  Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const ;
72 
73  Roo1DTable *createTable(const char *label) const ;
74 
75  // I/O streaming interface
76  virtual Bool_t readFromStream(std::istream& is, Bool_t compact, Bool_t verbose=kFALSE) ;
77  virtual void writeToStream(std::ostream& os, Bool_t compact) const ;
78 
79  virtual void printValue(std::ostream& os) const ;
80  virtual void printMultiline(std::ostream& os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const ;
81 
82  virtual Bool_t isIntegrationSafeLValue(const RooArgSet* /*set*/) const {
83  // Is this l-value object safe for use as integration observable
84  return kTRUE ;
85  }
86 
87  RooAbsArg *createFundamental(const char* newname=0) const;
88 
89  std::vector<RooCatType*>::const_iterator begin() const {
90  return _types.cbegin();
91  }
92 
93  std::vector<RooCatType*>::const_iterator end() const {
94  return _types.cend();
95  }
96 
97  std::size_t size() const {
98  return _types.size();
99  }
100 
101 protected:
102 
103  // Function evaluation and error tracing
104  RooCatType traceEval() const ;
105  // coverity[PASS_BY_VALUE]
106  virtual Bool_t traceEvalHook(RooCatType /*value*/) const {
107  // Hook function for trace evaluation (dummy)
108  return kFALSE ;
109  }
110  virtual RooCatType evaluate() const = 0 ;
111 
112  // Type definition management
113  const RooCatType* defineType(const char* label) ;
114  const RooCatType* defineType(const char* label, value_type index) ;
115  const RooCatType* defineTypeUnchecked(const char* label, value_type index) ;
116  const RooCatType* getOrdinal(UInt_t n, const char* rangeName=0) const;
117  void clearTypes() ;
118 
119  virtual Bool_t isValid() const ;
120  virtual Bool_t isValid(const RooCatType& value) const ;
121 
122  friend class RooVectorDataStore ;
123  virtual void syncCache(const RooArgSet* set=0) ;
124  virtual void copyCache(const RooAbsArg* source, Bool_t valueOnly=kFALSE, Bool_t setValueDirty=kTRUE) ;
125  virtual void attachToTree(TTree& t, Int_t bufSize=32000) ;
126  virtual void attachToVStore(RooVectorDataStore& vstore) ;
127  virtual void setTreeBranchStatus(TTree& t, Bool_t active) ;
128  virtual void fillTreeBranch(TTree& t) ;
129 
130  mutable UChar_t _byteValue ; //! Transient cache for byte values from tree branches
131  mutable RooCatType _value ; // Current value
132  // These need to be pointers, unfortunately, since other classes are holding pointers to the categories.
133  // That's not safe in case of reallocations.
134  std::vector<RooCatType*> _types; // Vector of allowed values.
135 
136  Bool_t _treeVar ; //! do not persist
137 
138  class LegacyIterator : public TIterator {
139  public:
140  LegacyIterator(const std::vector<RooCatType*>& vec) : _vec(&vec), index(-1) { }
141  const TCollection *GetCollection() const override {
142  return nullptr;
143  }
144  TObject* Next() override {
145  ++index;
146  return this->operator*();
147  }
148  void Reset() override {
149  index = -1;
150  }
151  TObject* operator*() const override {
152  // Need to const_cast, unfortunately because TIterator interface is too permissive
153  return 0 <= index && index < (int)_vec->size() ? const_cast<RooCatType*>((*_vec)[index]) : nullptr;
154  }
155  LegacyIterator& operator=(const LegacyIterator&) = default;
156  TIterator& operator=(const TIterator& other) override {
157  auto otherLeg = dynamic_cast<LegacyIterator*>(*other);
158  if (otherLeg)
159  return this->operator=(*otherLeg);
160 
161  throw std::logic_error("Cannot assign to category iterators from incompatible types.");
162  }
163 
164  private:
165  const std::vector<RooCatType*>* _vec;
166  int index;
167  };
168 
169  ClassDef(RooAbsCategory, 2) // Abstract discrete variable
170 };
171 
172 #endif
virtual Bool_t isIdentical(const RooAbsArg &other, Bool_t assumeSameType=kFALSE) const
Bool_t operator!=(value_type index)
virtual void copyCache(const RooAbsArg *source, Bool_t valueOnly=kFALSE, Bool_t setValueDirty=kTRUE)
Copy the cached value from given source and raise dirty flag.
A simple container to hold a batch of data values.
Definition: RooSpan.h:32
int value_type
The type used to denote a specific category state.
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from stream (dummy for now)
virtual void setTreeBranchStatus(TTree &t, Bool_t active)
(De)activate associate tree branch
Bool_t isValidLabel(const char *label) const
Check if state with given name is defined.
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to ostream.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooCatType _value
Transient cache for byte values from tree branches.
const RooCatType * getOrdinal(UInt_t n, const char *rangeName=0) const
Return state definition of ordinal nth defined state, needed by the generator mechanism.
virtual void attachToVStore(RooVectorDataStore &vstore)
Attach the category index and label to as branches to the given vector store.
virtual Bool_t isIntegrationSafeLValue(const RooArgSet *) const
void clearTypes()
Delete all currently defined states.
Roo1DTable * createTable(const char *label) const
Create a table matching the shape of this category.
Basic string class.
Definition: TString.h:131
virtual RooCatType evaluate() const =0
virtual void syncCache(const RooArgSet *set=0)
Explicitly synchronize RooAbsCategory internal cache.
virtual value_type getIndex() const
Return index number of current state.
Iterator abstract base class.
Definition: TIterator.h:30
virtual Bool_t isValid() const
Check if current value is a valid state.
Int_t numTypes(const char *=0) const
Return number of types defined (in range named rangeName if rangeName!=0)
#define ClassDef(name, id)
Definition: Rtypes.h:326
virtual void printValue(std::ostream &os) const
Print value (label name)
RooVectorDataStore is the abstract base class for data collection that use a TTree as internal storag...
Bool_t operator!=(const RooAbsArg &other)
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state...
Definition: RooCatType.h:22
TIterator & operator=(const TIterator &other) override
const RooCatType * defineType(const char *label)
Define a new state with given name.
const std::vector< RooCatType * > * _vec
Bool_t isValidIndex(value_type index) const
Check if state with given index is defined.
Bool_t isSignType(Bool_t mustHaveZero=kFALSE) const
Determine if category has 2 or 3 states with index values -1,0,1.
TIterator * R__SUGGEST_ALTERNATIVE("Use begin(), end() or range-based for loops.") typeIterator() const
const TCollection * GetCollection() const override
const RooCatType * lookupType(value_type index, Bool_t printError=kFALSE) const
Find our type corresponding to the specified index, or return 0 for no match.
virtual void attachToTree(TTree &t, Int_t bufSize=32000)
Attach the category index and label to as branches to the given TTree.
Collection abstract base class.
Definition: TCollection.h:63
unsigned int UInt_t
Definition: RtypesCore.h:42
std::size_t size() const
virtual const char * getLabel() const
Return label string of current state.
RooCatType traceEval() const
Recalculate current value and check validity of new result.
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooCategory fundamental object with our properties.
static void indent(ostringstream &buf, int indent_level)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
virtual ~RooAbsCategory()
Destructor.
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
std::vector< RooCatType * >::const_iterator begin() const
int type
Definition: TGX11.cxx:120
virtual Bool_t traceEvalHook(RooCatType) const
Bool_t operator==(value_type index) const
Equality operator with a integer (compares with state index number)
Mother of all ROOT objects.
Definition: TObject.h:37
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:465
virtual void fillTreeBranch(TTree &t)
Fill tree branches associated with current object with current value.
TObject * operator*() const override
Return current object or nullptr.
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
const RooCatType * defineTypeUnchecked(const char *label, value_type index)
Internal version of defineType that does not check if type already exists.
LegacyIterator(const std::vector< RooCatType *> &vec)
A TTree represents a columnar dataset.
Definition: TTree.h:72
unsigned char UChar_t
Definition: RtypesCore.h:34
virtual RooSpan< const value_type > getValBatch(std::size_t, std::size_t) const
Retrieve a batch of category values for events in the range [begin, begin+batchSize).
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:70
Bool_t operator!=(const char *label)
const Bool_t kTRUE
Definition: RtypesCore.h:87
const Int_t n
Definition: legend1.C:16
Roo1DTable implements a one-dimensional table.
Definition: Roo1DTable.h:23
char name[80]
Definition: TGX11.cxx:109
std::vector< RooCatType * >::const_iterator end() const
LegacyIterator & operator=(const LegacyIterator &)=default
std::vector< RooCatType * > _types