Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 RooSecondMoment represents the first, second, or third order derivative
23 of any RooAbsReal as calculated (numerically) by the MathCore Richardson
24 derivator class.
25 **/
26 
27 
28 #include "RooFit.h"
29 
30 #include "Riostream.h"
31 #include "Riostream.h"
32 #include <math.h>
33 
34 #include "RooSecondMoment.h"
35 #include "RooAbsReal.h"
36 #include "RooAbsPdf.h"
37 #include "RooErrorHandler.h"
38 #include "RooArgSet.h"
39 #include "RooMsgService.h"
40 #include "RooRealVar.h"
41 #include "RooFunctor.h"
42 #include "RooGlobalFunc.h"
43 #include "RooConstVar.h"
44 #include "RooRealIntegral.h"
45 #include "RooNumIntConfig.h"
46 #include "RooFormulaVar.h"
47 #include "RooLinearVar.h"
48 #include "RooProduct.h"
49 #include "RooGlobalFunc.h"
50 #include <string>
51 using namespace std ;
52 
53 
54 ClassImp(RooSecondMoment);
55 ;
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Default constructor
60 
61 RooSecondMoment::RooSecondMoment()
62 {
63 }
64 
65 
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 
69 RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Bool_t centr, Bool_t takeRoot) :
70  RooAbsMoment(name, title,func,x,2,takeRoot),
71  _xf("!xf","xf",this,kFALSE,kFALSE),
72  _ixf("!ixf","ixf",this),
73  _if("!if","if",this),
74  _xfOffset(0)
75 {
76  setExpensiveObjectCache(func.expensiveObjectCache()) ;
77 
78  RooAbsReal* XF(0) ;
79  if (centr) {
80 
81  string m1name=Form("%s_moment1",GetName()) ;
82  RooAbsReal* mom1 = func.mean(x) ;
83  _mean.setArg(*mom1) ;
84 
85  string pname=Form("%s_product",name) ;
86  _xfOffset = mom1->getVal() ;
87  XF = new RooFormulaVar(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
88 
89  } else {
90 
91  string pname=Form("%s_product",name) ;
92  XF = new RooProduct(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
93  }
94 
95  XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
96 
97  if (func.isBinnedDistribution(x)) {
98  XF->specialIntegratorConfig(kTRUE)->method1D().setLabel("RooBinIntegrator");
99  }
100 
101  RooRealIntegral* intXF = (RooRealIntegral*) XF->createIntegral(x) ;
102  RooRealIntegral* intF = (RooRealIntegral*) func.createIntegral(x) ;
103  intXF->setCacheNumeric(kTRUE) ;
104  intF->setCacheNumeric(kTRUE) ;
105 
106  _xf.setArg(*XF) ;
107  _ixf.setArg(*intXF) ;
108  _if.setArg(*intF) ;
109  addOwnedComponents(RooArgSet(*XF,*intXF,*intF)) ;
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 
114 RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
115  Bool_t centr, Bool_t takeRoot, Bool_t intNSet) :
116  RooAbsMoment(name, title,func,x,2,takeRoot),
117  _xf("!xf","xf",this,kFALSE,kFALSE),
118  _ixf("!ixf","ixf",this),
119  _if("!if","if",this),
120  _xfOffset(0)
121 {
122  setExpensiveObjectCache(func.expensiveObjectCache()) ;
123 
124  _nset.add(nset) ;
125 
126  RooAbsReal* XF(0) ;
127  if (centr) {
128 
129  string m1name=Form("%s_moment1",GetName()) ;
130  RooAbsReal* mom1 = func.mean(x,nset) ;
131  _mean.setArg(*mom1) ;
132 
133  string pname=Form("%s_product",name) ;
134  _xfOffset = mom1->getVal() ;
135  XF = new RooFormulaVar(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
136 
137 
138  } else {
139 
140  string pname=Form("%s_product",name) ;
141  XF = new RooProduct(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
142 
143  }
144 
145  XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
146 
147  if (func.isBinnedDistribution(x)) {
148  XF->specialIntegratorConfig(kTRUE)->method1D().setLabel("RooBinIntegrator");
149  }
150  if (intNSet && _nset.getSize()>0 && func.isBinnedDistribution(_nset)) {
151  XF->specialIntegratorConfig(kTRUE)->method2D().setLabel("RooBinIntegrator");
152  XF->specialIntegratorConfig(kTRUE)->methodND().setLabel("RooBinIntegrator");
153  }
154 
155  RooArgSet intSet(x) ;
156  if (intNSet) intSet.add(_nset,kTRUE) ;
157  RooRealIntegral* intXF = (RooRealIntegral*) XF->createIntegral(intSet,&_nset) ;
158  RooRealIntegral* intF = (RooRealIntegral*) func.createIntegral(intSet,&_nset) ;
159  intXF->setCacheNumeric(kTRUE) ;
160  intF->setCacheNumeric(kTRUE) ;
161 
162  _xf.setArg(*XF) ;
163  _ixf.setArg(*intXF) ;
164  _if.setArg(*intF) ;
165  addOwnedComponents(RooArgSet(*XF,*intXF,*intF)) ;
166 }
167 
168 
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 
172 RooSecondMoment::RooSecondMoment(const RooSecondMoment& other, const char* name) :
173  RooAbsMoment(other, name),
174  _xf("xf",this,other._xf),
175  _ixf("ixf",this,other._ixf),
176  _if("if",this,other._if),
177  _xfOffset(other._xfOffset)
178 {
179 }
180 
181 
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// Destructor
185 
186 RooSecondMoment::~RooSecondMoment()
187 {
188 }
189 
190 
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Calculate value
194 
195 Double_t RooSecondMoment::evaluate() const
196 {
197  Double_t ratio = _ixf / _if ;
198 
199  if (_mean.absArg()) {
200  ratio -= (_mean - _xfOffset)*(_mean-_xfOffset) ;
201  }
202 
203  Double_t ret = _takeRoot ? sqrt(ratio) : ratio ;
204  return ret ;
205 }
206 
207