Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooLinearVar.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/// \class RooLinearVar
19/// RooLinearVar is the most general form of a derived real-valued object that can
20/// be used by RooRealIntegral to integrate over. The requirements for this are
21/// * Can be modified directly (i.e. invertible formula)
22/// * Jacobian term in integral is constant (but not necessarily 1)
23///
24/// This class implements the most general form that satisfies these requirements
25/// \f[
26/// RLV = \mathrm{slope} \cdot x + \mathrm{offset}
27/// \f]
28/// \f$ x \f$ is required to be a RooRealVar to meet the invertibility criterium,
29/// `slope` and `offset` are RooAbsReals, but cannot overlap with \f$ x \f$,
30/// *i.e.*, \f$ x \f$ may not be a server of `slope` and `offset`.
31///
32/// In the context of a dataset, `slope` may not contain any real-valued dependents
33/// (to satisfyt the constant Jacobian requirement). This check cannot be enforced at
34/// construction time, but can be performed at run time through the isJacobianOK(depList)
35/// member function.
36///
37///
38
39#include <cmath>
40
41#include "TClass.h"
42#include "RooLinearVar.h"
43#include "RooStreamParser.h"
44#include "RooArgSet.h"
45#include "RooRealVar.h"
46#include "RooNumber.h"
47#include "RooBinning.h"
48#include "RooMsgService.h"
49
50
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Constructor with RooAbsRealLValue variable and RooAbsReal slope and offset
55
56RooLinearVar::RooLinearVar(const char *name, const char *title, RooAbsRealLValue& variable,
57 const RooAbsReal& slope, const RooAbsReal& offs, const char *unit) :
58 RooAbsRealLValue(name, title, unit),
59 _binning(variable.getBinning(),slope.getVal(),offs.getVal()),
60 _var("var","variable",this,variable,true,true),
61 _slope("slope","slope",this,(RooAbsReal&)slope),
62 _offset("offset","offset",this,(RooAbsReal&)offs)
63{
64 // Slope and offset may not depend on variable
65 if (slope.dependsOnValue(variable) || offs.dependsOnValue(variable)) {
66 std::stringstream ss;
67 ss << "RooLinearVar::RooLinearVar(" << GetName()
68 << "): ERROR, slope(" << slope.GetName() << ") and offset("
69 << offs.GetName() << ") may not depend on variable("
70 << variable.GetName() << ")";
71 const std::string errMsg = ss.str();
72 coutE(InputArguments) << errMsg << std::endl;
73 throw std::invalid_argument(errMsg);
74 }
75
76 // Initial plot range and number of bins from dependent variable
77// setPlotRange(variable.getPlotMin()*_slope + _offset,
78// variable.getPlotMax()*_slope + _offset) ;
79// setPlotBins(variable.getPlotBins()) ;
80
81}
82
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// Copy constructor
87
90 _binning(other._binning),
91 _var("var",this,other._var),
92 _slope("slope",this,other._slope),
93 _offset("offset",this,other._offset)
94{
95}
96
97
98
99////////////////////////////////////////////////////////////////////////////////
100/// Destructor
101
106
107
108
109////////////////////////////////////////////////////////////////////////////////
110/// Calculate current value of this object
111
113{
114 return _offset + _var * _slope ;
115}
116
117
118
119////////////////////////////////////////////////////////////////////////////////
120/// Assign given value to linear transformation: sets input variable to (value-offset)/slope
121/// If slope is zerom an error message is printed and no assignment is made
122
124{
125 //cout << "RooLinearVar::setVal(" << GetName() << "): new value = " << value << std::endl ;
126
127 // Prevent DIV0 problems
128 if (_slope == 0.) {
129 coutE(Eval) << "RooLinearVar::setVal(" << GetName() << "): ERROR: slope is zero, cannot invert relation" << std::endl ;
130 return ;
131 }
132
133 // Invert formula 'value = offset + slope*var'
134 _var->setVal((value - _offset) / _slope) ;
135
136}
137
138
139
140////////////////////////////////////////////////////////////////////////////////
141/// Returns true if Jacobian term associated with current
142/// expression tree is indeed constant.
143
145{
146 if (!_var->isJacobianOK(depList)) {
147 return false ;
148 }
149
150 // Check if jacobian has no real-valued dependents
151 for(RooAbsArg* arg : depList) {
152 if (arg->IsA()->InheritsFrom(RooAbsReal::Class())) {
153 if (_slope->dependsOnValue(*arg)) {
154// std::cout << "RooLinearVar::isJacobianOK(" << GetName() << ") return false because slope depends on value of " << arg->GetName() << std::endl ;
155 return false ;
156 }
157 }
158 }
159 // std::cout << "RooLinearVar::isJacobianOK(" << GetName() << ") return true" << std::endl ;
160 return true ;
161}
162
163
164
165////////////////////////////////////////////////////////////////////////////////
166/// Return value of Jacobian associated with the transformation
167
169{
170 return _slope*_var->jacobian() ;
171}
172
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// Read object contents from stream
177
178bool RooLinearVar::readFromStream(std::istream& /*is*/, bool /*compact*/, bool /*verbose*/)
179{
180 return true ;
181}
182
183
184
185////////////////////////////////////////////////////////////////////////////////
186/// Write object contents to stream
187
188void RooLinearVar::writeToStream(std::ostream& os, bool compact) const
189{
190 if (compact) {
191 os << getVal() ;
192 } else {
193 os << _slope->GetName() << " * " << _var->GetName() << " + " << _offset->GetName() ;
194 }
195}
196
197
198
199////////////////////////////////////////////////////////////////////////////////
200/// Retrieve binning of this linear transformation. A RooLinearVar does not have its own
201/// binnings but uses linearly transformed binnings of the input variable. If a given
202/// binning exists on the input variable, it will also exist on this linear transformation,
203/// and a binning adaptor object is created on the fly.
204
206{
207 // Normalization binning
208 if (name==nullptr) {
210 return _binning ;
211 }
212
213 // Alternative named range binnings, look for existing translator binning first
215 if (altBinning) {
216 altBinning->updateInput(_var->getBinning(name,verbose),_slope,_offset) ;
217 return *altBinning ;
218 }
219
220 // If binning is not found return default binning, if creation is not requested
221 if (!_var->hasRange(name) && !createOnTheFly) {
222 return _binning ;
223 }
224
225 // Create translator binning on the fly
229
230 return *transBinning ;
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Const version of getBinning()
236
237const RooAbsBinning& RooLinearVar::getBinning(const char* name, bool verbose, bool createOnTheFly) const
238{
239 return const_cast<RooLinearVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
240}
241
242////////////////////////////////////////////////////////////////////////////////
243/// Get a list of all binning names. An empty name implies the default binning.
244/// A 0 pointer should be passed to getBinning in this case.
245
246std::list<std::string> RooLinearVar::getBinningNames() const
247{
248 std::list<std::string> binningNames(1, "");
249
250 for (TObject const* binning : _altBinning) {
251 binningNames.push_back(binning->GetName());
252 }
253
254 return binningNames;
255}
256
257////////////////////////////////////////////////////////////////////////////////
258/// Returns true if binning with given name exists.If a given binning
259/// exists on the input variable, it will also exists on this linear
260/// transformation.
261
262bool RooLinearVar::hasBinning(const char* name) const
263{
264 return _var->hasBinning(name) ;
265}
#define coutE(a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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:77
bool dependsOnValue(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr) const
Check whether this object depends on values from an element in the serverList.
Definition RooAbsArg.h:106
Abstract base class for RooRealVar binning definitions.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual double jacobian() const
virtual const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false) const =0
Retrieve binning configuration with given name or default binning.
virtual bool isJacobianOK(const RooArgSet &depList) const
virtual void setVal(double value)=0
Set the current value of the object. Needs to be overridden by implementations.
virtual bool hasBinning(const char *name) const =0
Check if binning with given name has been defined.
bool hasRange(const char *name) const override
Check if variable has a binning with given name.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
static TClass * Class()
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Special binning implementation for RooLinearVar that transforms the binning of the RooLinearVar input...
void updateInput(const RooAbsBinning &input, double slope=1.0, double offset=0.0)
Update the slope and offset parameters and the pointer to the input binning.
RooLinearVar is the most general form of a derived real-valued object that can be used by RooRealInte...
RooRealProxy _slope
Slope of transformation.
bool hasBinning(const char *name) const override
Returns true if binning with given name exists.If a given binning exists on the input variable,...
RooLinkedList _altBinning
!
std::list< std::string > getBinningNames() const override
Get a list of all binning names.
RooLinTransBinning _binning
double jacobian() const override
Return value of Jacobian associated with the transformation.
double evaluate() const override
Calculate current value of this object.
void writeToStream(std::ostream &os, bool compact) const override
Write object contents to stream.
bool isJacobianOK(const RooArgSet &depList) const override
Returns true if Jacobian term associated with current expression tree is indeed constant.
void setVal(double value) override
Assign given value to linear transformation: sets input variable to (value-offset)/slope If slope is ...
RooRealProxy _offset
Offset of transformation.
bool readFromStream(std::istream &is, bool compact, bool verbose=false) override
Read object contents from stream.
RooTemplateProxy< RooAbsRealLValue > _var
Input observable.
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false) const override
Const version of getBinning()
~RooLinearVar() override
Destructor.
void Delete(Option_t *o=nullptr) override
Remove all elements in collection and delete all elements NB: Collection does not own elements,...
virtual void Add(TObject *arg)
TObject * FindObject(const char *name) const override
Return pointer to object with given name.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:41