Logo ROOT  
Reference Guide
RooAbsCategoryLValue.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
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 
17 /**
18 \file RooAbsCategoryLValue.cxx
19 \class RooAbsCategoryLValue
20 \ingroup Roofitcore
21 
22 RooAbsCategoryLValue is the common abstract base class for objects that represent a
23 discrete value that can be set from the outside, *i.e.* that may appear on the left
24 hand side of an assignment ("*lvalue*").
25 
26 Each implementation must provide the functions setIndex()/setLabel() to allow direct modification
27 of the value. RooAbsCategoryLValue may be derived, but its functional relation
28 to other RooAbsArgs must be invertible.
29 **/
30 
31 #include "RooAbsCategoryLValue.h"
32 
33 #include "RooFit.h"
34 #include "RooArgSet.h"
35 #include "RooRandom.h"
36 #include "RooMsgService.h"
37 
38 #include "TString.h"
39 
40 using namespace std;
41 
43 
44 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Constructor
48 
49 RooAbsCategoryLValue::RooAbsCategoryLValue(const char *name, const char *title) :
50  RooAbsCategory(name,title)
51 {
52  setValueDirty() ;
53  setShapeDirty() ;
54 }
55 
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Copy constructor
60 
62  RooAbsCategory(other, name), RooAbsLValue(other)
63 {
64 }
65 
66 
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Destructor
70 
72 {
73 }
74 
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Assignment operator from integer index number
79 
81 {
82  setIndex(index,kTRUE) ;
83  return *this ;
84 }
85 
86 
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Assignment operator from string pointer
90 
92 {
93  setLabel(label) ;
94  return *this ;
95 }
96 
97 
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// Assignment from another RooAbsCategory. This will use the *state name*
101 /// of the other object to set the corresponding state. This is less efficient
102 /// then directly assigning the state index.
104 {
105  if (&other==this) return *this ;
106 
107  const auto index = lookupIndex(other.getCurrentLabel());
108  if (index == std::numeric_limits<value_type>::min()) {
109  coutE(ObjectHandling) << "Trying to assign the label '" << other.getCurrentLabel() << "' to category'"
110  << GetName() << "', but such a label is not defined." << std::endl;
111  return *this;
112  }
113 
114  _currentIndex = index;
115  setValueDirty();
116 
117  return *this;
118 }
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Set our state to our `n`th defined type.
123 /// \return true in case of an error.
125 {
126  return setIndex(getOrdinal(n).second, true);
127 }
128 
129 
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Copy the cached value from given source and raise dirty flag.
133 /// It is the callers responsability to ensure that the sources
134 /// cache is clean(valid) before this function is called, e.g. by
135 /// calling syncCache() on the source.
136 
137 void RooAbsCategoryLValue::copyCache(const RooAbsArg* source, Bool_t valueOnly, Bool_t setValDirty)
138 {
139  RooAbsCategory::copyCache(source,valueOnly,setValDirty) ;
140 
141  if (isValid()) {
142  setIndex(_currentIndex); // force back-propagation
143  }
144 }
145 
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Randomize current value.
149 /// If the result is not in the range, the randomisation is repeated.
150 void RooAbsCategoryLValue::randomize(const char* rangeName)
151 {
152  const auto& theStateNames = stateNames();
153 
154  if (_insertionOrder.size() == theStateNames.size()) {
155  // If users didn't manipulate the state map directly, the order of insertion has to be respected to
156  // ensure backward compatibility.
157  // This heavily uses strings, though.
158  do {
159  const UInt_t ordinal = RooRandom::integer(theStateNames.size());
160  const auto item = theStateNames.find(_insertionOrder[ordinal]);
161  setIndex(item->second);
162  } while (!inRange(rangeName));
163  } else {
164  // When not having to respect the insertion order, can just advance the iterator
165  do {
166  const UInt_t ordinal = RooRandom::integer(theStateNames.size());
167  const auto it = std::next(theStateNames.begin(), ordinal);
168  setIndex(it->second);
169  } while (!inRange(rangeName));
170  }
171 }
172 
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Set category to i-th fit bin, which is the i-th registered state.
176 
177 void RooAbsCategoryLValue::setBin(Int_t ibin, const char* rangeName)
178 {
179  // Check validity of ibin
180  if (ibin<0 || ibin>=numBins(rangeName)) {
181  coutE(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: bin index " << ibin
182  << " is out of range (0," << numBins(rangeName)-1 << ")" << endl ;
183  return ;
184  }
185 
186  if (rangeName) {
187  coutF(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: ranges not implemented"
188  " for setting bins in categories." << std::endl;
189  throw std::logic_error("Ranges not implemented for setting bins in categories.");
190  }
191 
192  // Retrieve state corresponding to bin
193  const auto& type = getOrdinal(ibin);
194  assert(type.second != std::numeric_limits<value_type>::min());
195 
196  // Set value to requested state
197  setIndex(type.second);
198 }
199 
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// Return the number of fit bins ( = number of types )
203 Int_t RooAbsCategoryLValue::numBins(const char* rangeName) const
204 {
205  return numTypes(rangeName) ;
206 }
RooAbsCategoryLValue::copyCache
void copyCache(const RooAbsArg *source, Bool_t valueOnly=kFALSE, Bool_t setValDirty=kTRUE)
Copy the cached value from given source and raise dirty flag.
Definition: RooAbsCategoryLValue.cxx:137
RooAbsCategory::copyCache
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.
Definition: RooAbsCategory.cxx:530
RooAbsCategoryLValue::setBin
virtual void setBin(Int_t ibin, const char *rangeName=0)
Set category to i-th fit bin, which is the i-th registered state.
Definition: RooAbsCategoryLValue.cxx:177
n
const Int_t n
Definition: legend1.C:16
RooAbsCategoryLValue::randomize
virtual void randomize(const char *rangeName=0)
Randomize current value.
Definition: RooAbsCategoryLValue.cxx:150
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooMsgService.h
RooAbsCategoryLValue::setOrdinal
bool setOrdinal(unsigned int index)
Set our state to our nth defined type.
Definition: RooAbsCategoryLValue.cxx:124
RooFit.h
RooAbsCategoryLValue::setLabel
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooArgSet.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooRandom::integer
static UInt_t integer(UInt_t max, TRandom *generator=randomGenerator())
Return an integer uniformly distributed from [0,n-1].
Definition: RooRandom.cxx:101
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooAbsCategory::getCurrentLabel
virtual const char * getCurrentLabel() const
Return label string of current state.
Definition: RooAbsCategory.cxx:130
TString.h
RooAbsCategory::numTypes
Int_t numTypes(const char *=0) const
Return number of types defined (in range named rangeName if rangeName!=0)
Definition: RooAbsCategory.h:129
bool
RooAbsCategory
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
Definition: RooAbsCategory.h:38
RooAbsCategoryLValue::numBins
virtual Int_t numBins(const char *rangeName) const
Return the number of fit bins ( = number of types )
Definition: RooAbsCategoryLValue.cxx:203
RooAbsCategory::isValid
virtual bool isValid() const
WVE (08/21/01) Probably obsolete now.
Definition: RooAbsCategory.h:192
RooAbsCategoryLValue::setIndex
virtual bool setIndex(value_type index, bool printError=true)=0
Change category state by specifying the index code of the desired state.
RooAbsCategoryLValue::operator=
RooAbsArg & operator=(int index)
Assignment operator from integer index number.
Definition: RooAbsCategoryLValue.cxx:80
RooRandom.h
RooAbsCategory::_currentIndex
value_type _currentIndex
Definition: RooAbsCategory.h:215
RooAbsCategoryLValue::~RooAbsCategoryLValue
virtual ~RooAbsCategoryLValue()
Destructor.
Definition: RooAbsCategoryLValue.cxx:71
RooFit::ObjectHandling
@ ObjectHandling
Definition: RooGlobalFunc.h:68
RooAbsCategoryLValue::RooAbsCategoryLValue
RooAbsCategoryLValue()
Definition: RooAbsCategoryLValue.h:28
unsigned int
RooAbsCategory::_insertionOrder
std::vector< std::string > _insertionOrder
Map state names to index numbers. Make sure state names are updated in recomputeShape().
Definition: RooAbsCategory.h:217
RooAbsCategory::getOrdinal
const std::map< std::string, value_type >::value_type & getOrdinal(unsigned int n) const
Return name and index of the nth defined state.
Definition: RooAbsCategory.cxx:573
RooAbsArg::setShapeDirty
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:493
RooAbsCategoryLValue.h
coutF
#define coutF(a)
Definition: RooMsgService.h:34
RooAbsCategory::lookupIndex
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Definition: RooAbsCategory.cxx:283
name
char name[80]
Definition: TGX11.cxx:110
RooAbsCategory::stateNames
const std::map< std::string, value_type > & stateNames() const
Access the map of state names to index numbers.
Definition: RooAbsCategory.h:158
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooAbsArg::setValueDirty
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:488
RooAbsLValue
Abstract base class for objects that are lvalues, i.e.
Definition: RooAbsLValue.h:26
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
type
int type
Definition: TGX11.cxx:121
RooAbsCategoryLValue
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
Definition: RooAbsCategoryLValue.h:25
TGeant4Unit::second
static constexpr double second
Definition: TGeant4SystemOfUnits.h:151
RooAbsArg::inRange
virtual Bool_t inRange(const char *) const
Definition: RooAbsArg.h:376
int