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