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