ROOT   Reference Guide
Searching...
No Matches
RooLegendre.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitModels *
4 * File: $Id$
5 * Authors: *
6 * GR, Gerhard Raven, Nikhef & VU, Gerhard.Raven@nikhef.nl
7 * *
9 * *
10 * Redistribution and use in source and binary forms, *
11 * with or without modification, are permitted according to the terms *
13 *****************************************************************************/
14
15/** \class RooLegendre
16 \ingroup Roofit
17
18 Compute the associated Legendre polynomials using ROOT::Math::assoc_legendre().
19
20 Since the Legendre polynomials have a value range of [-1, 1], these cannot be implemented as a PDF.
21 They can be used in sums, though, for example using a RooRealSumFunc of RooLegendre plus an offset.
22**/
23
24#include "RooLegendre.h"
25#include "RooAbsReal.h"
26
27#include "Math/SpecFunc.h"
28#include "TMath.h"
29
30#include <cmath>
31#include <algorithm>
32
34
35////////////////////////////////////////////////////////////////////////////////
36
37namespace {
38 inline double a(int p, int l, int m) {
40 r /= pow(2.,m+2*p);
41 return p%2==0 ? r : -r ;
42 }
43
44 void checkCoeffs(int m1, int l1, int m2, int l2) {
45 if (m1 < 0 || m2 < 0) {
46 throw std::invalid_argument("RooLegendre: m coefficients need to be >= 0.");
47 }
48 if (l1 < m1 || l2 < m2) {
49 throw std::invalid_argument("RooLegendre: m coefficients need to be smaller than corresponding l.");
50 }
51 }
52}
53
54////////////////////////////////////////////////////////////////////////////////
55
57 _l1(1),_m1(1),_l2(0),_m2(0)
58{
59}
60
61////////////////////////////////////////////////////////////////////////////////
62///TODO: for now, we assume that ctheta has a range [-1,1]
63/// should map the ctheta range onto this interval, and adjust integrals...
64
65RooLegendre::RooLegendre(const char* name, const char* title, RooAbsReal& ctheta, int l, int m)
66 : RooAbsReal(name, title)
67 , _ctheta("ctheta", "ctheta", this, ctheta)
68 , _l1(l),_m1(m),_l2(0),_m2(0)
69{
70 checkCoeffs(_m1, _l1, _m2, _l2);
71}
72
73////////////////////////////////////////////////////////////////////////////////
74
75RooLegendre::RooLegendre(const char* name, const char* title, RooAbsReal& ctheta, int l1, int m1, int l2, int m2)
76 : RooAbsReal(name, title)
77 , _ctheta("ctheta", "ctheta", this, ctheta)
78 , _l1(l1),_m1(m1),_l2(l2),_m2(m2)
79{
80 checkCoeffs(_m1, _l1, _m2, _l2);
81}
82
83////////////////////////////////////////////////////////////////////////////////
84
85RooLegendre::RooLegendre(const RooLegendre& other, const char* name)
86 : RooAbsReal(other, name)
87 , _ctheta("ctheta", this, other._ctheta)
88 , _l1(other._l1), _m1(other._m1)
89 , _l2(other._l2), _m2(other._m2)
90{
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Note: P_0^0 = 1, so P_l^m = P_l^m P_0^0
95
97{
98 double r = 1;
99 double ctheta = std::max(-1., std::min((double)_ctheta, +1.));
100 if (_l1!=0||_m1!=0) r *= ROOT::Math::assoc_legendre(_l1,_m1,ctheta);
101 if (_l2!=0||_m2!=0) r *= ROOT::Math::assoc_legendre(_l2,_m2,ctheta);
102 if ((_m1+_m2)%2==1) r = -r;
103 return r;
104}
105
106
107////////////////////////////////////////////////////////////////////////////////
108
109namespace {
110//Author: Emmanouil Michalainas, CERN 26 August 2019
111
112void compute( size_t batchSize, const int l1, const int m1, const int l2, const int m2,
113 double * __restrict output,
114 double const * __restrict TH)
115{
116 double legendre1 = 1.0;
117 double legendreMinus1 = 1.0;
118 if (l1+m1 > 0) {
119 legendre1 = ROOT::Math::internal::legendre(l1,m1,1.0);
120 legendreMinus1 = ROOT::Math::internal::legendre(l1,m1,-1.0);
121 }
122 if (l2+m2 > 0) {
123 legendre1 *= ROOT::Math::internal::legendre(l2,m2,1.0);
124 legendreMinus1 *= ROOT::Math::internal::legendre(l2,m2,-1.0);
125 }
126
127 for (size_t i=0; i<batchSize; i++) {
128 if (TH[i] <= -1.0) {
129 output[i] = legendreMinus1;
130 } else if (TH[i] >= 1.0) {
131 output[i] = legendre1;
132 }
133 else {
134 output[i] = 1.0;
135 if (l1+m1 > 0) {
136 output[i] *= ROOT::Math::internal::legendre(l1,m1,TH[i]);
137 }
138 if (l2+m2 > 0) {
139 output[i] *= ROOT::Math::internal::legendre(l2,m2,TH[i]);
140 }
141 }
142 }
143}
144};
145
147{
148 compute(ctx.output().size(), _l1, _m1, _l2, _m2, ctx.output().data(), ctx.at(_ctheta).data());
149}
150
151
152////////////////////////////////////////////////////////////////////////////////
153
154namespace {
155 bool fullRange(const RooRealProxy& x ,const char* range)
156 {
157 return range == nullptr || strlen(range) == 0
158 ? std::abs(x.min() + 1.) < 1.e-8 && std::abs(x.max() - 1.) < 1.e-8
159 : std::abs(x.min(range) + 1.) < 1.e-8 && std::abs(x.max(range) - 1.) < 1.e-8;
160 }
161}
162
163////////////////////////////////////////////////////////////////////////////////
164
165Int_t RooLegendre::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* rangeName) const
166{
167 // don't support indefinite integrals...
168 if (fullRange(_ctheta,rangeName) && matchArgs(allVars, analVars, _ctheta)) return 1;
169 return 0;
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// this was verified to match mathematica for
174/// l1 in [0,2], m1 in [0,l1], l2 in [l1,4], m2 in [0,l2]
175
176double RooLegendre::analyticalIntegral(Int_t code, const char* ) const
177{
178 R__ASSERT(code==1) ;
179 if ( _m1==_m2 ) return ( _l1 == _l2) ? TMath::Factorial(_l1+_m2)/TMath::Factorial(_l1-_m1)*double(2)/(2*_l1+1) : 0.;
180 if ( (_l1+_l2-_m1-_m2)%2 != 0 ) return 0; // these combinations are odd under x -> -x
181
182 // from B.R. Wong, "On the overlap integral of associated Legendre Polynomials" 1998 J. Phys. A: Math. Gen. 31 1101
183 // TODO: update to the result of
184 // H. A. Mavromatis
185 // "A single-sum expression for the overlap integral of two associated Legendre polynomials"
186 // 1999 J. Phys. A: Math. Gen. 32 2601
187 // http://iopscience.iop.org/0305-4470/32/13/011/pdf/0305-4470_32_13_011.pdf
188 // For that we need Wigner 3-j, which Lorenzo has added for Root 5.28... (note: check Condon-Shortly convention in this paper!)
189 double r=0;
190 for (int p1=0; 2*p1 <= _l1-_m1 ;++p1) {
191 double a1 = a(p1,_l1,_m1);
192 for (int p2=0; 2*p2 <= _l2-_m2 ; ++p2) {
193 double a2 = a(p2,_l2,_m2);
194 r+= a1*a2*TMath::Gamma( double(_l1+_l2-_m1-_m2-2*p1-2*p2+1)/2 )*TMath::Gamma( double(_m1+_m2+2*p1+2*p2+2)/2 );
195 }
196 }
197 r /= TMath::Gamma( double(_l1+_l2+3)/2 );
198
199 if ((_m1+_m2)%2==1) r = -r;
200 return r;
201}
202
203////////////////////////////////////////////////////////////////////////////////
204
205Int_t RooLegendre::getMaxVal( const RooArgSet& /*vars*/) const {
206 if (_m1==0&&_m2==0) return 1;
207 // does anyone know the analytical expression for the max values in case m!=0??
208 if (_l1<3&&_l2<3) return 1;
209 return 0;
210}
211
212namespace {
213 inline double maxSingle(int i, int j) {
214 R__ASSERT(j<=i);
215 // x0 : 1 (ordinary Legendre)
216 if (j==0) return 1;
217 R__ASSERT(i<3);
218 // 11: 1
219 if (i<2) return 1;
220 // 21: 3 22: 3
221 static const double m2[3] = { 3,3 };
222 return m2[j-1];
223 }
224}
225double RooLegendre::maxVal( Int_t /*code*/) const {
226 return maxSingle(_l1,_m1)*maxSingle(_l2,_m2);
227}
#define a(i)
Definition RSha256.hxx:99
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char name[80]
Definition TGX11.cxx:110
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
bool matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
Compute the associated Legendre polynomials using ROOT::Math::assoc_legendre().
Definition RooLegendre.h:20
double evaluate() const override
Note: P_0^0 = 1, so P_l^m = P_l^m P_0^0.
Int_t getMaxVal(const RooArgSet &vars) const override
Advertise capability to determine maximum value of function for given set of observables.
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
this was verified to match mathematica for l1 in [0,2], m1 in [0,l1], l2 in [l1,4],...
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
RooRealProxy _ctheta
Definition RooLegendre.h:39
void doEval(RooFit::EvalContext &) const override
Base function for computing multiple values of a RooAbsReal.
double maxVal(Int_t code) const override
Return maximum value for set of observables identified by code assigned in getMaxVal.
double assoc_legendre(unsigned l, unsigned m, double x)
Computes the associated Legendre polynomials.
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Definition RVec.hxx:1846
Double_t x[n]
Definition legend1.C:17
double legendre(unsigned l, unsigned m, double x)
Double_t Factorial(Int_t i)
Computes factorial(n).
Definition TMath.cxx:252
Double_t Gamma(Double_t z)
Computation of gamma(z) for all z.
Definition TMath.cxx:353
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
static void output()