Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooSecondMoment.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 RooSecondMoment.cxx
19\class RooSecondMoment
20\ingroup Roofitcore
21**/
22
23#include "RooSecondMoment.h"
24#include "RooAbsReal.h"
25#include "RooAbsPdf.h"
26#include "RooArgSet.h"
27#include "RooRealVar.h"
28#include "RooGlobalFunc.h"
29#include "RooConstVar.h"
30#include "RooRealIntegral.h"
31#include "RooNumIntConfig.h"
32#include "RooFormulaVar.h"
33#include "RooLinearVar.h"
34#include "RooProduct.h"
35
36#include <string>
37#include <cmath>
38#include <ostream>
39
40using std::string;
41
42////////////////////////////////////////////////////////////////////////////////
43
44RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, bool centr, bool takeRoot) :
45 RooAbsMoment(name, title,func,x,2,takeRoot),
46 _xf("!xf","xf",this,false,false),
47 _ixf("!ixf","ixf",this),
48 _if("!if","if",this),
49 _xfOffset(0)
50{
52
53 std::unique_ptr<RooAbsReal> XF;
54 if (centr) {
55
56 _mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x)});
57
58 std::string pname = std::string{name} + "_product";
60 std::stringstream formula;
61 formula << "std::pow((@0-" << _xfOffset << "),2) * @1";
62 XF = std::make_unique<RooFormulaVar>(pname.c_str(), formula.str().c_str(), RooArgList(x, func));
63
64 } else {
65
66 std::string pname = std::string{name} + "_product";
67 XF = std::make_unique<RooProduct>(pname.c_str(), pname.c_str(), RooArgList(x, x, func));
68 }
69
70 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
71
72 if (func.isBinnedDistribution(x)) {
73 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
74 }
75
76 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
77 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
78 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
79 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
80
81 _xf.setArg(*XF) ;
82 _ixf.setArg(*intXF) ;
83 _if.setArg(*intF) ;
84 addOwnedComponents(std::move(XF)) ;
85 addOwnedComponents(std::move(intXF));
86 addOwnedComponents(std::move(intF));
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
91RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
92 bool centr, bool takeRoot, bool intNSet) :
93 RooAbsMoment(name, title,func,x,2,takeRoot),
94 _xf("!xf","xf",this,false,false),
95 _ixf("!ixf","ixf",this),
96 _if("!if","if",this),
97 _xfOffset(0)
98{
100
101 _nset.add(nset) ;
102
103 std::unique_ptr<RooAbsReal> XF;
104 if (centr) {
105
106 _mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x, nset)});
107
108 std::string pname = std::string{name} + "_product";
110 std::stringstream formula;
111 formula << "std::pow((@0-" << _xfOffset << "),2) * @1";
112 XF = std::make_unique<RooFormulaVar>(pname.c_str(), formula.str().c_str(), RooArgList(x, func));
113
114 } else {
115
116 std::string pname = std::string{name} + "_product";
117 XF = std::make_unique<RooProduct>(pname.c_str(), pname.c_str(), RooArgList(x, x, func));
118 }
119
120 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
121
122 if (func.isBinnedDistribution(x)) {
123 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
124 }
125 if (intNSet && !_nset.empty() && func.isBinnedDistribution(_nset)) {
126 XF->specialIntegratorConfig(true)->method2D().setLabel("RooBinIntegrator");
127 XF->specialIntegratorConfig(true)->methodND().setLabel("RooBinIntegrator");
128 }
129
130 RooArgSet intSet(x) ;
131 if (intNSet) intSet.add(_nset,true) ;
132 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
133 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
134 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
135 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
136
137 _xf.setArg(*XF) ;
138 _ixf.setArg(*intXF) ;
139 _if.setArg(*intF) ;
140 addOwnedComponents(std::move(XF)) ;
141 addOwnedComponents(std::move(intXF));
142 addOwnedComponents(std::move(intF));
143}
144
145
146
147////////////////////////////////////////////////////////////////////////////////
148
150 RooAbsMoment(other, name),
151 _xf("xf",this,other._xf),
152 _ixf("ixf",this,other._ixf),
153 _if("if",this,other._if),
154 _xfOffset(other._xfOffset)
155{
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Calculate value
160
162{
163 double ratio = _ixf / _if ;
164
165 if (_mean.absArg()) {
166 ratio -= (_mean - _xfOffset)*(_mean-_xfOffset) ;
167 }
168
169 double ret = _takeRoot ? sqrt(ratio) : ratio ;
170 return ret ;
171}
char name[80]
Definition TGX11.cxx:142
RooExpensiveObjectCache & expensiveObjectCache() const
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:420
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooRealProxy _mean
Mean (if calculated for central moment)
Int_t _takeRoot
Return n-order root of moment.
RooSetProxy _nset
Normalization set (optional)
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
RooAbsMoment * mean(RooRealVar &obs)
Definition RooAbsReal.h:356
virtual bool isBinnedDistribution(const RooArgSet &) const
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Definition RooAbsReal.h:343
RooFit::OwningPtr< RooAbsReal > createIntegral(const RooArgSet &iset, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Create an object that represents the integral of the function over one or more observables listed in ...
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * absArg() const
Return pointer to contained argument.
Definition RooArgProxy.h:46
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
Performs hybrid numerical/analytical integrals of RooAbsReal objects.
void setCacheNumeric(bool flag)
Variable that can be changed from the outside.
Definition RooRealVar.h:37
RooRealProxy _ixf
Int((X-offset)*F(X))dx ;.
RooSecondMoment()=default
RooRealProxy _if
Int(F(x))dx ;.
double evaluate() const override
Calculate value.
double _xfOffset
offset
RooRealProxy _xf
(X-offset)*F
U & putOwnedArg(std::unique_ptr< U > ownedArg)
Move a new object held and owned by proxy.
bool setArg(T &newRef)
Change object held in proxy into newRef.
Double_t x[n]
Definition legend1.C:17