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 "RooArgSet.h"
34#include "RooRandom.h"
35#include "RooMsgService.h"
36
37using namespace std;
38
40
41
42
43////////////////////////////////////////////////////////////////////////////////
44/// Constructor
45
46RooAbsCategoryLValue::RooAbsCategoryLValue(const char *name, const char *title) :
47 RooAbsCategory(name,title)
48{
51}
52
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Copy constructor
57
59 RooAbsCategory(other, name), RooAbsLValue(other)
60{
61}
62
63
64
65////////////////////////////////////////////////////////////////////////////////
66/// Destructor
67
69{
70}
71
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// Assignment operator from integer index number
76
78{
79 setIndex(index,true) ;
80 return *this ;
81}
82
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// Assignment operator from string pointer
87
89{
90 setLabel(label) ;
91 return *this ;
92}
93
94
95
96////////////////////////////////////////////////////////////////////////////////
97/// Assignment from another RooAbsCategory. This will use the *state name*
98/// of the other object to set the corresponding state. This is less efficient
99/// then directly assigning the state index.
101{
102 if (&other==this) return *this ;
103
104 const auto index = lookupIndex(other.getCurrentLabel());
105 if (index == std::numeric_limits<value_type>::min()) {
106 coutE(ObjectHandling) << "Trying to assign the label '" << other.getCurrentLabel() << "' to category'"
107 << GetName() << "', but such a label is not defined." << std::endl;
108 return *this;
109 }
110
113
114 return *this;
115}
116
117
118////////////////////////////////////////////////////////////////////////////////
119/// Set our state to our `n`th defined type.
120/// \return true in case of an error.
122{
123 return setIndex(getOrdinal(n).second, true);
124}
125
126
127
128////////////////////////////////////////////////////////////////////////////////
129/// Copy the cached value from given source and raise dirty flag.
130/// It is the callers responsability to ensure that the sources
131/// cache is clean(valid) before this function is called, e.g. by
132/// calling syncCache() on the source.
133
134void RooAbsCategoryLValue::copyCache(const RooAbsArg* source, bool valueOnly, bool setValDirty)
135{
136 RooAbsCategory::copyCache(source,valueOnly,setValDirty) ;
137
138 if (isValid()) {
139 setIndex(_currentIndex); // force back-propagation
140 }
141}
142
143
144////////////////////////////////////////////////////////////////////////////////
145/// Randomize current value.
146/// If the result is not in the range, the randomisation is repeated.
147void RooAbsCategoryLValue::randomize(const char* rangeName)
148{
149 const auto& theStateNames = stateNames();
150
151 if (_insertionOrder.size() == theStateNames.size()) {
152 // If users didn't manipulate the state map directly, the order of insertion has to be respected to
153 // ensure backward compatibility.
154 // This heavily uses strings, though.
155 do {
156 const UInt_t ordinal = RooRandom::integer(theStateNames.size());
157 const auto item = theStateNames.find(_insertionOrder[ordinal]);
158 setIndex(item->second);
159 } while (!inRange(rangeName));
160 } else {
161 // When not having to respect the insertion order, can just advance the iterator
162 do {
163 const UInt_t ordinal = RooRandom::integer(theStateNames.size());
164 const auto it = std::next(theStateNames.begin(), ordinal);
165 setIndex(it->second);
166 } while (!inRange(rangeName));
167 }
168}
169
170
171////////////////////////////////////////////////////////////////////////////////
172/// Set category to i-th fit bin, which is the i-th registered state.
173
174void RooAbsCategoryLValue::setBin(Int_t ibin, const char* rangeName)
175{
176 // Check validity of ibin
177 if (ibin<0 || ibin>=numBins(rangeName)) {
178 coutE(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: bin index " << ibin
179 << " is out of range (0," << numBins(rangeName)-1 << ")" << endl ;
180 return ;
181 }
182
183 if (rangeName) {
184 coutF(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: ranges not implemented"
185 " for setting bins in categories." << std::endl;
186 throw std::logic_error("Ranges not implemented for setting bins in categories.");
187 }
188
189 // Retrieve state corresponding to bin
190 const auto& type = getOrdinal(ibin);
191 assert(type.second != std::numeric_limits<value_type>::min());
192
193 // Set value to requested state
194 setIndex(type.second);
195}
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Return the number of fit bins ( = number of types )
200Int_t RooAbsCategoryLValue::numBins(const char* rangeName) const
201{
202 return numTypes(rangeName) ;
203}
#define coutF(a)
Definition: RooMsgService.h:38
#define coutE(a)
Definition: RooMsgService.h:37
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition: TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:498
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:493
virtual bool inRange(const char *) const
Definition: RooAbsArg.h:381
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
void randomize(const char *rangeName=nullptr) override
Randomize current value.
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 valueOnly=false, bool setValDirty=true) override
Copy the cached value from given source and raise dirty flag.
Int_t numBins(const char *rangeName=nullptr) const override
Return the number of fit bins ( = number of types )
bool setOrdinal(unsigned int index)
Set our state to our nth defined type.
RooAbsArg & operator=(int index)
Assignment operator from integer index number.
void setBin(Int_t ibin, const char *rangeName=nullptr) override
Set category to i-th fit bin, which is the i-th registered state.
~RooAbsCategoryLValue() override
Destructor.
virtual bool setLabel(const char *label, bool printError=true)=0
Change category state by specifying a state name.
A space to attach TBranches.
virtual const char * getCurrentLabel() const
Return label string of current state.
void copyCache(const RooAbsArg *source, bool valueOnly=false, bool setValueDirty=true) override
Copy the cached value from given source and raise dirty flag.
value_type _currentIndex
Current category state.
Int_t numTypes(const char *=nullptr) const
Return number of types defined (in range named rangeName if rangeName!=nullptr)
const std::map< std::string, value_type >::value_type & getOrdinal(unsigned int n) const
Return name and index of the nth defined state.
std::vector< std::string > _insertionOrder
Keeps track in which order state numbers have been inserted. Make sure this is updated in recomputeSh...
bool isValid() const override
WVE (08/21/01) Probably obsolete now.
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:99
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const Int_t n
Definition: legend1.C:16
@ InputArguments
Definition: RooGlobalFunc.h:63
@ ObjectHandling
Definition: RooGlobalFunc.h:63
static constexpr double second