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
46
47////////////////////////////////////////////////////////////////////////////////
48
49RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, bool centr, bool takeRoot) :
50 RooAbsMoment(name, title,func,x,2,takeRoot),
51 _xf("!xf","xf",this,false,false),
52 _ixf("!ixf","ixf",this),
53 _if("!if","if",this),
54 _xfOffset(0)
55{
57
58 std::unique_ptr<RooAbsReal> XF;
59 if (centr) {
60
61 string m1name=Form("%s_moment1",GetName()) ;
62 _mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x)}) ;
63
64 string pname=Form("%s_product",name) ;
66 XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
67
68 } else {
69
70 string pname=Form("%s_product",name) ;
71 XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
72 }
73
74 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
75
76 if (func.isBinnedDistribution(x)) {
77 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
78 }
79
80 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
81 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
82 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
83 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
84
85 _xf.setArg(*XF) ;
86 _ixf.setArg(*intXF) ;
87 _if.setArg(*intF) ;
88 addOwnedComponents(std::move(XF)) ;
89 addOwnedComponents(std::move(intXF));
90 addOwnedComponents(std::move(intF));
91}
92
93////////////////////////////////////////////////////////////////////////////////
94
95RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
96 bool centr, bool takeRoot, bool intNSet) :
97 RooAbsMoment(name, title,func,x,2,takeRoot),
98 _xf("!xf","xf",this,false,false),
99 _ixf("!ixf","ixf",this),
100 _if("!if","if",this),
101 _xfOffset(0)
102{
104
105 _nset.add(nset) ;
106
107 std::unique_ptr<RooAbsReal> XF;
108 if (centr) {
109
110 string m1name=Form("%s_moment1",GetName()) ;
111 _mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x,nset)}) ;
112
113 string pname=Form("%s_product",name) ;
114 _xfOffset = _mean->getVal() ;
115 XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
116
117
118 } else {
119
120 string pname=Form("%s_product",name) ;
121 XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
122
123 }
124
125 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
126
127 if (func.isBinnedDistribution(x)) {
128 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
129 }
130 if (intNSet && !_nset.empty() && func.isBinnedDistribution(_nset)) {
131 XF->specialIntegratorConfig(true)->method2D().setLabel("RooBinIntegrator");
132 XF->specialIntegratorConfig(true)->methodND().setLabel("RooBinIntegrator");
133 }
134
135 RooArgSet intSet(x) ;
136 if (intNSet) intSet.add(_nset,true) ;
137 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
138 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
139 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
140 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
141
142 _xf.setArg(*XF) ;
143 _ixf.setArg(*intXF) ;
144 _if.setArg(*intF) ;
145 addOwnedComponents(std::move(XF)) ;
146 addOwnedComponents(std::move(intXF));
147 addOwnedComponents(std::move(intF));
148}
149
150
151
152////////////////////////////////////////////////////////////////////////////////
153
155 RooAbsMoment(other, name),
156 _xf("xf",this,other._xf),
157 _ixf("ixf",this,other._ixf),
158 _if("if",this,other._if),
159 _xfOffset(other._xfOffset)
160{
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Calculate value
165
167{
168 double ratio = _ixf / _if ;
169
170 if (_mean.absArg()) {
171 ratio -= (_mean - _xfOffset)*(_mean-_xfOffset) ;
172 }
173
174 double ret = _takeRoot ? sqrt(ratio) : ratio ;
175 return ret ;
176}
#define ClassImp(name)
Definition Rtypes.h:382
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'.
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:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooAbsMoment * mean(RooRealVar &obs)
Definition RooAbsReal.h:359
virtual bool isBinnedDistribution(const RooArgSet &) const
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Definition RooAbsReal.h:346
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:47
Double_t x[n]
Definition legend1.C:17