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 "Riostream.h"
24#include <cmath>
25
26#include "RooSecondMoment.h"
27#include "RooAbsReal.h"
28#include "RooAbsPdf.h"
29#include "RooErrorHandler.h"
30#include "RooArgSet.h"
31#include "RooMsgService.h"
32#include "RooRealVar.h"
33#include "RooFunctor.h"
34#include "RooGlobalFunc.h"
35#include "RooConstVar.h"
36#include "RooRealIntegral.h"
37#include "RooNumIntConfig.h"
38#include "RooFormulaVar.h"
39#include "RooLinearVar.h"
40#include "RooProduct.h"
41#include <string>
42using std::string;
43
44
45
46////////////////////////////////////////////////////////////////////////////////
47
48RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, bool centr, bool takeRoot) :
49 RooAbsMoment(name, title,func,x,2,takeRoot),
50 _xf("!xf","xf",this,false,false),
51 _ixf("!ixf","ixf",this),
52 _if("!if","if",this),
53 _xfOffset(0)
54{
56
57 std::unique_ptr<RooAbsReal> XF;
58 if (centr) {
59
60 string m1name=Form("%s_moment1",GetName()) ;
61 _mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x)}) ;
62
63 string pname=Form("%s_product",name) ;
65 XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
66
67 } else {
68
69 string pname=Form("%s_product",name) ;
70 XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
71 }
72
73 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
74
75 if (func.isBinnedDistribution(x)) {
76 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
77 }
78
79 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
80 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
81 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
82 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
83
84 _xf.setArg(*XF) ;
85 _ixf.setArg(*intXF) ;
86 _if.setArg(*intF) ;
87 addOwnedComponents(std::move(XF)) ;
88 addOwnedComponents(std::move(intXF));
89 addOwnedComponents(std::move(intF));
90}
91
92////////////////////////////////////////////////////////////////////////////////
93
94RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
95 bool centr, bool takeRoot, bool intNSet) :
96 RooAbsMoment(name, title,func,x,2,takeRoot),
97 _xf("!xf","xf",this,false,false),
98 _ixf("!ixf","ixf",this),
99 _if("!if","if",this),
100 _xfOffset(0)
101{
103
104 _nset.add(nset) ;
105
106 std::unique_ptr<RooAbsReal> XF;
107 if (centr) {
108
109 string m1name=Form("%s_moment1",GetName()) ;
110 _mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x,nset)}) ;
111
112 string pname=Form("%s_product",name) ;
113 _xfOffset = _mean->getVal() ;
114 XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
115
116
117 } else {
118
119 string pname=Form("%s_product",name) ;
120 XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
121
122 }
123
124 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
125
126 if (func.isBinnedDistribution(x)) {
127 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
128 }
129 if (intNSet && !_nset.empty() && func.isBinnedDistribution(_nset)) {
130 XF->specialIntegratorConfig(true)->method2D().setLabel("RooBinIntegrator");
131 XF->specialIntegratorConfig(true)->methodND().setLabel("RooBinIntegrator");
132 }
133
135 if (intNSet) intSet.add(_nset,true) ;
136 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
137 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
138 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
139 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
140
141 _xf.setArg(*XF) ;
142 _ixf.setArg(*intXF) ;
143 _if.setArg(*intF) ;
144 addOwnedComponents(std::move(XF)) ;
145 addOwnedComponents(std::move(intXF));
146 addOwnedComponents(std::move(intF));
147}
148
149
150
151////////////////////////////////////////////////////////////////////////////////
152
155 _xf("xf",this,other._xf),
156 _ixf("ixf",this,other._ixf),
157 _if("if",this,other._if),
158 _xfOffset(other._xfOffset)
159{
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Calculate value
164
166{
167 double ratio = _ixf / _if ;
168
169 if (_mean.absArg()) {
170 ratio -= (_mean - _xfOffset)*(_mean-_xfOffset) ;
171 }
172
173 double ret = _takeRoot ? sqrt(ratio) : ratio ;
174 return ret ;
175}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
RooExpensiveObjectCache & expensiveObjectCache() const
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:444
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
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:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooAbsMoment * mean(RooRealVar &obs)
Definition RooAbsReal.h:361
virtual bool isBinnedDistribution(const RooArgSet &) const
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Definition RooAbsReal.h:348
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.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Double_t x[n]
Definition legend1.C:17