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
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 "TTree.h"
39#include "TString.h"
40#include "TH1.h"
41
42using namespace std;
43
45
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Constructor
50
51RooAbsCategoryLValue::RooAbsCategoryLValue(const char *name, const char *title) :
52 RooAbsCategory(name,title)
53{
54 setValueDirty() ;
55 setShapeDirty() ;
56}
57
58
59
60////////////////////////////////////////////////////////////////////////////////
61/// Copy constructor
62
64 RooAbsCategory(other, name), RooAbsLValue(other)
65{
66}
67
68
69
70////////////////////////////////////////////////////////////////////////////////
71/// Destructor
72
74{
75}
76
77
78
79////////////////////////////////////////////////////////////////////////////////
80/// Assignment operator from integer index number
81
83{
84 setIndex(index,kTRUE) ;
85 return *this ;
86}
87
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// Assignment operator from string pointer
92
94{
95 setLabel(label) ;
96 return *this ;
97}
98
99
100
101////////////////////////////////////////////////////////////////////////////////
102/// Assignment from another RooAbsCategory. This will use the *state name*
103/// of the other object to set the corresponding state. This is less efficient
104/// then directly assigning the state index.
106{
107 if (&other==this) return *this ;
108
109 const auto index = lookupIndex(other.getCurrentLabel());
110 if (index == std::numeric_limits<value_type>::min()) {
111 coutE(ObjectHandling) << "Trying to assign the label '" << other.getCurrentLabel() << "' to category'"
112 << GetName() << "', but such a label is not defined." << std::endl;
113 return *this;
114 }
115
116 _currentIndex = index;
118
119 return *this;
120}
121
122
123////////////////////////////////////////////////////////////////////////////////
124/// Set our state to our `n`th defined type.
125/// \return true in case of an error.
127{
128 return setIndex(getOrdinal(n).second, true);
129}
130
131
132
133////////////////////////////////////////////////////////////////////////////////
134/// Copy the cached value from given source and raise dirty flag.
135/// It is the callers responsability to ensure that the sources
136/// cache is clean(valid) before this function is called, e.g. by
137/// calling syncCache() on the source.
138
139void RooAbsCategoryLValue::copyCache(const RooAbsArg* source, Bool_t valueOnly, Bool_t setValDirty)
140{
141 RooAbsCategory::copyCache(source,valueOnly,setValDirty) ;
142
143 if (isValid()) {
144 setIndex(_currentIndex); // force back-propagation
145 }
146}
147
148
149////////////////////////////////////////////////////////////////////////////////
150/// Randomize current value.
151/// If the result is not in the range, the randomisation is repeated.
152void RooAbsCategoryLValue::randomize(const char* rangeName)
153{
154 const auto& theStateNames = stateNames();
155
156 if (_insertionOrder.size() == theStateNames.size()) {
157 // If users didn't manipulate the state map directly, the order of insertion has to be respected to
158 // ensure backward compatibility.
159 // This heavily uses strings, though.
160 do {
161 const UInt_t ordinal = RooRandom::integer(theStateNames.size());
162 const auto item = theStateNames.find(_insertionOrder[ordinal]);
163 setIndex(item->second);
164 } while (!inRange(rangeName));
165 } else {
166 // When not having to respect the insertion order, can just advance the iterator
167 do {
168 const UInt_t ordinal = RooRandom::integer(theStateNames.size());
169 const auto it = std::next(theStateNames.begin(), ordinal);
170 setIndex(it->second);
171 } while (!inRange(rangeName));
172 }
173}
174
175
176////////////////////////////////////////////////////////////////////////////////
177/// Set category to i-th fit bin, which is the i-th registered state.
178
179void RooAbsCategoryLValue::setBin(Int_t ibin, const char* rangeName)
180{
181 // Check validity of ibin
182 if (ibin<0 || ibin>=numBins(rangeName)) {
183 coutE(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: bin index " << ibin
184 << " is out of range (0," << numBins(rangeName)-1 << ")" << endl ;
185 return ;
186 }
187
188 if (rangeName) {
189 coutF(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: ranges not implemented"
190 " for setting bins in categories." << std::endl;
191 throw std::logic_error("Ranges not implemented for setting bins in categories.");
192 }
193
194 // Retrieve state corresponding to bin
195 const auto& type = getOrdinal(ibin);
196 assert(type.second != std::numeric_limits<value_type>::min());
197
198 // Set value to requested state
199 setIndex(type.second);
200}
201
202
203////////////////////////////////////////////////////////////////////////////////
204/// Return the number of fit bins ( = number of types )
205Int_t RooAbsCategoryLValue::numBins(const char* rangeName) const
206{
207 return numTypes(rangeName) ;
208}
#define coutF(a)
Definition: RooMsgService.h:34
#define coutE(a)
Definition: RooMsgService.h:33
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:492
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:487
virtual Bool_t inRange(const char *) const
Definition: RooAbsArg.h:364
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 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) 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.
Definition: RooAbsLValue.h:26
static UInt_t integer(UInt_t max, TRandom *generator=randomGenerator())
Return an integer uniformly distributed from [0,n-1].
Definition: RooRandom.cxx:101
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
const Int_t n
Definition: legend1.C:16
@ InputArguments
Definition: RooGlobalFunc.h:68
@ ObjectHandling
Definition: RooGlobalFunc.h:68
static constexpr double second