Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22RooAbsCategoryLValue is the common abstract base class for objects that represent a
23discrete value that can be set from the outside, *i.e.* that may appear on the left
24hand side of an assignment ("*lvalue*").
25
26Each implementation must provide the functions setIndex()/setLabel() to allow direct modification
27of the value. RooAbsCategoryLValue may be derived, but its functional relation
28to other RooAbsArgs must be invertible.
29**/
30
32
33#include "RooFit.h"
34#include "RooArgSet.h"
35#include "RooRandom.h"
36#include "RooMsgService.h"
37
38#include "TString.h"
39
40using namespace std;
41
43
44
45
46////////////////////////////////////////////////////////////////////////////////
47/// Constructor
48
49RooAbsCategoryLValue::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;
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
137void 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.
150void 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
177void 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 && getBinningPtr(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 )
203Int_t RooAbsCategoryLValue::numBins(const char* rangeName) const
204{
205 return numTypes(rangeName) ;
206}
#define coutF(a)
#define coutE(a)
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition RooAbsArg.h:510
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition RooAbsArg.h:505
virtual Bool_t inRange(const char *) const
Definition RooAbsArg.h:393
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.
void copyCache(const RooAbsArg *source, Bool_t valueOnly=kFALSE, Bool_t setValDirty=kTRUE)
Copy the cached value from given source and raise dirty flag.
virtual void randomize(const char *rangeName=0)
Randomize current value.
virtual const RooAbsBinning * getBinningPtr(const char *) const
virtual void setBin(Int_t ibin, const char *rangeName=0)
Set category to i-th fit bin, which is the i-th registered state.
bool setOrdinal(unsigned int index)
Set our state to our nth defined type.
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
virtual ~RooAbsCategoryLValue()
Destructor.
RooAbsArg & operator=(int index)
Assignment operator from integer index number.
virtual Int_t numBins(const char *rangeName=nullptr) const
Return the number of fit bins ( = number of types )
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
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.
virtual bool isValid() const
WVE (08/21/01) Probably obsolete now.
virtual const char * getCurrentLabel() const
Return label string of current state.
value_type _currentIndex
const std::map< std::string, value_type >::value_type & getOrdinal(unsigned int n) const
Return name and index of the nth defined state.
Int_t numTypes(const char *=0) const
Return number of types defined (in range named rangeName if rangeName!=0)
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.
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Abstract base class for objects that are lvalues, i.e.
static UInt_t integer(UInt_t max, TRandom *generator=randomGenerator())
Return an integer uniformly distributed from [0,n-1].
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
const Int_t n
Definition legend1.C:16