TMath.cxx

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: TMath.cxx 36817 2010-11-21 08:36:19Z brun $
00002 // Authors: Rene Brun, Anna Kreshuk, Eddy Offermann, Fons Rademakers   29/07/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TMath                                                                //
00015 //                                                                      //
00016 // Encapsulate math routines.                                           //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #include "TMath.h"
00021 #include "TError.h"
00022 #include <math.h>
00023 #include <string.h>
00024 #include <algorithm>
00025 #include "Riostream.h"
00026 #include "TString.h"
00027 
00028 #include <Math/SpecFuncMathCore.h>
00029 #include <Math/PdfFuncMathCore.h>
00030 #include <Math/ProbFuncMathCore.h>
00031 
00032 //const Double_t
00033 //   TMath::Pi = 3.14159265358979323846,
00034 //   TMath::E  = 2.7182818284590452354;
00035 
00036 
00037 // Without this macro the THtml doc for TMath can not be generated
00038 #if !defined(R__ALPHA) && !defined(R__SOLARIS) && !defined(R__ACC) && !defined(R__FBSD)
00039 NamespaceImp(TMath)
00040 #endif
00041 
00042 namespace TMath {
00043 
00044    Double_t GamCf(Double_t a,Double_t x);
00045    Double_t GamSer(Double_t a,Double_t x);
00046    Double_t VavilovDenEval(Double_t rlam, Double_t *AC, Double_t *HC, Int_t itype);
00047    void VavilovSet(Double_t rkappa, Double_t beta2, Bool_t mode, Double_t *WCM, Double_t *AC, Double_t *HC, Int_t &itype, Int_t &npt);
00048 
00049 }
00050 
00051 //______________________________________________________________________________
00052 Long_t TMath::Hypot(Long_t x, Long_t y)
00053 {
00054    return (Long_t) (hypot((Double_t)x, (Double_t)y) + 0.5);
00055 }
00056 
00057 //______________________________________________________________________________
00058 Double_t TMath::Hypot(Double_t x, Double_t y)
00059 {
00060    return hypot(x, y);
00061 }
00062 
00063 //______________________________________________________________________________
00064 Double_t TMath::ASinH(Double_t x)
00065 {
00066 #if defined(WIN32)
00067    if(x==0.0) return 0.0;
00068    Double_t ax = Abs(x);
00069    return log(x+ax*sqrt(1.+1./(ax*ax)));
00070 #else
00071    return asinh(x);
00072 #endif
00073 }
00074 
00075 //______________________________________________________________________________
00076 Double_t TMath::ACosH(Double_t x)
00077 {
00078 #if defined(WIN32)
00079    if(x==0.0) return 0.0;
00080    Double_t ax = Abs(x);
00081    return log(x+ax*sqrt(1.-1./(ax*ax)));
00082 #else
00083    return acosh(x);
00084 #endif
00085 }
00086 
00087 //______________________________________________________________________________
00088 Double_t TMath::ATanH(Double_t x)
00089 {
00090 #if defined(WIN32)
00091    return log((1+x)/(1-x))/2;
00092 #else
00093    return atanh(x);
00094 #endif
00095 }
00096 
00097 //______________________________________________________________________________
00098 Double_t TMath::Log2(Double_t x)
00099 {
00100    return log(x)/log(2.0);
00101 }
00102 
00103 //______________________________________________________________________________
00104 Int_t TMath::Nint(Float_t x)
00105 {
00106    // Round to nearest integer. Rounds half integers to the nearest
00107    // even integer.
00108 
00109    int i;
00110    if (x >= 0) {
00111       i = int(x + 0.5);
00112       if (x + 0.5 == Float_t(i) && i & 1) i--;
00113    } else {
00114       i = int(x - 0.5);
00115       if (x - 0.5 == Float_t(i) && i & 1) i++;
00116 
00117    }
00118    return i;
00119 }
00120 
00121 //______________________________________________________________________________
00122 Int_t TMath::Nint(Double_t x)
00123 {
00124    // Round to nearest integer. Rounds half integers to the nearest
00125    // even integer.
00126 
00127    int i;
00128    if (x >= 0) {
00129       i = int(x + 0.5);
00130       if (x + 0.5 == Double_t(i) && i & 1) i--;
00131    } else {
00132       i = int(x - 0.5);
00133       if (x - 0.5 == Double_t(i) && i & 1) i++;
00134 
00135    }
00136    return i;
00137 }
00138 
00139 //______________________________________________________________________________
00140 Double_t TMath::DiLog(Double_t x)
00141 {
00142    // The DiLogarithm function
00143    // Code translated by R.Brun from CERNLIB DILOG function C332
00144 
00145    const Double_t hf  = 0.5;
00146    const Double_t pi  = TMath::Pi();
00147    const Double_t pi2 = pi*pi;
00148    const Double_t pi3 = pi2/3;
00149    const Double_t pi6 = pi2/6;
00150    const Double_t pi12 = pi2/12;
00151    const Double_t c[20] = {0.42996693560813697, 0.40975987533077105,
00152      -0.01858843665014592, 0.00145751084062268,-0.00014304184442340,
00153       0.00001588415541880,-0.00000190784959387, 0.00000024195180854,
00154      -0.00000003193341274, 0.00000000434545063,-0.00000000060578480,
00155       0.00000000008612098,-0.00000000001244332, 0.00000000000182256,
00156      -0.00000000000027007, 0.00000000000004042,-0.00000000000000610,
00157       0.00000000000000093,-0.00000000000000014, 0.00000000000000002};
00158 
00159    Double_t t,h,y,s,a,alfa,b1,b2,b0;
00160 
00161    if (x == 1) {
00162       h = pi6;
00163    } else if (x == -1) {
00164       h = -pi12;
00165    } else {
00166       t = -x;
00167       if (t <= -2) {
00168          y = -1/(1+t);
00169          s = 1;
00170          b1= TMath::Log(-t);
00171          b2= TMath::Log(1+1/t);
00172          a = -pi3+hf*(b1*b1-b2*b2);
00173       } else if (t < -1) {
00174          y = -1-t;
00175          s = -1;
00176          a = TMath::Log(-t);
00177          a = -pi6+a*(a+TMath::Log(1+1/t));
00178       } else if (t <= -0.5) {
00179          y = -(1+t)/t;
00180          s = 1;
00181          a = TMath::Log(-t);
00182          a = -pi6+a*(-hf*a+TMath::Log(1+t));
00183       } else if (t < 0) {
00184          y = -t/(1+t);
00185          s = -1;
00186          b1= TMath::Log(1+t);
00187          a = hf*b1*b1;
00188       } else if (t <= 1) {
00189          y = t;
00190          s = 1;
00191          a = 0;
00192       } else {
00193          y = 1/t;
00194          s = -1;
00195          b1= TMath::Log(t);
00196          a = pi6+hf*b1*b1;
00197       }
00198       h    = y+y-1;
00199       alfa = h+h;
00200       b1   = 0;
00201       b2   = 0;
00202       for (Int_t i=19;i>=0;i--){
00203          b0 = c[i] + alfa*b1-b2;
00204          b2 = b1;
00205          b1 = b0;
00206       }
00207       h = -(s*(b0-h*b2)+a);
00208    }
00209    return h;
00210 }
00211 
00212 //______________________________________________________________________________
00213 Double_t TMath::Erf(Double_t x)
00214 {
00215    // Computation of the error function erf(x).
00216    // Erf(x) = (2/sqrt(pi)) Integral(exp(-t^2))dt between 0 and x
00217 
00218    return ::ROOT::Math::erf(x);
00219 }
00220 
00221 //______________________________________________________________________________
00222 Double_t TMath::Erfc(Double_t x)
00223 {
00224    // Compute the complementary error function erfc(x).
00225    // Erfc(x) = (2/sqrt(pi)) Integral(exp(-t^2))dt between x and infinity
00226    //
00227 
00228    return ::ROOT::Math::erfc(x);
00229 }
00230 
00231 //______________________________________________________________________________
00232 Double_t TMath::ErfInverse(Double_t x)
00233 {
00234    // returns  the inverse error function
00235    // x must be  <-1<x<1
00236 
00237    Int_t kMaxit    = 50;
00238    Double_t kEps   = 1e-14;
00239    Double_t kConst = 0.8862269254527579;     // sqrt(pi)/2.0
00240 
00241    if(TMath::Abs(x) <= kEps) return kConst*x;
00242 
00243    // Newton iterations
00244    Double_t erfi, derfi, y0,y1,dy0,dy1;
00245    if(TMath::Abs(x) < 1.0) {
00246       erfi  = kConst*TMath::Abs(x);
00247       y0    = TMath::Erf(0.9*erfi);
00248       derfi = 0.1*erfi;
00249       for (Int_t iter=0; iter<kMaxit; iter++) {
00250          y1  = 1. - TMath::Erfc(erfi);
00251          dy1 = TMath::Abs(x) - y1;
00252          if (TMath::Abs(dy1) < kEps)  {if (x < 0) return -erfi; else return erfi;}
00253          dy0    = y1 - y0;
00254          derfi *= dy1/dy0;
00255          y0     = y1;
00256          erfi  += derfi;
00257          if(TMath::Abs(derfi/erfi) < kEps) {if (x < 0) return -erfi; else return erfi;}
00258       }
00259    }
00260    return 0; //did not converge
00261 }
00262 Double_t TMath::ErfcInverse(Double_t x)
00263 {
00264    // returns  the inverse of the complementary error function
00265    // x must be  0<x<2
00266    // implement using  the quantile of the normal distribution
00267    // instead of ErfInverse for better numerical precision for large x 
00268    
00269    // erfc-1(x) = - 1/sqrt(2) * normal_quantile( 0.5 * x)
00270    return - 0.70710678118654752440 * TMath::NormQuantile( 0.5 * x); 
00271 }
00272    
00273 
00274 
00275 
00276 //______________________________________________________________________________
00277 Double_t TMath::Factorial(Int_t n)
00278 {
00279    // Compute factorial(n).
00280 
00281    if (n <= 0) return 1.;
00282    Double_t x = 1;
00283    Int_t b = 0;
00284    do {
00285       b++;
00286       x *= b;
00287    } while (b != n);
00288    return x;
00289 }
00290 
00291 //______________________________________________________________________________
00292 Double_t TMath::Freq(Double_t x)
00293 {
00294    // Computation of the normal frequency function freq(x).
00295    // Freq(x) = (1/sqrt(2pi)) Integral(exp(-t^2/2))dt between -infinity and x.
00296    //
00297    // Translated from CERNLIB C300 by Rene Brun.
00298 
00299    const Double_t c1 = 0.56418958354775629;
00300    const Double_t w2 = 1.41421356237309505;
00301 
00302    const Double_t p10 = 2.4266795523053175e+2,  q10 = 2.1505887586986120e+2,
00303                   p11 = 2.1979261618294152e+1,  q11 = 9.1164905404514901e+1,
00304                   p12 = 6.9963834886191355e+0,  q12 = 1.5082797630407787e+1,
00305                   p13 =-3.5609843701815385e-2,  q13 = 1;
00306 
00307    const Double_t p20 = 3.00459261020161601e+2, q20 = 3.00459260956983293e+2,
00308                   p21 = 4.51918953711872942e+2, q21 = 7.90950925327898027e+2,
00309                   p22 = 3.39320816734343687e+2, q22 = 9.31354094850609621e+2,
00310                   p23 = 1.52989285046940404e+2, q23 = 6.38980264465631167e+2,
00311                   p24 = 4.31622272220567353e+1, q24 = 2.77585444743987643e+2,
00312                   p25 = 7.21175825088309366e+0, q25 = 7.70001529352294730e+1,
00313                   p26 = 5.64195517478973971e-1, q26 = 1.27827273196294235e+1,
00314                   p27 =-1.36864857382716707e-7, q27 = 1;
00315 
00316    const Double_t p30 =-2.99610707703542174e-3, q30 = 1.06209230528467918e-2,
00317                   p31 =-4.94730910623250734e-2, q31 = 1.91308926107829841e-1,
00318                   p32 =-2.26956593539686930e-1, q32 = 1.05167510706793207e+0,
00319                   p33 =-2.78661308609647788e-1, q33 = 1.98733201817135256e+0,
00320                   p34 =-2.23192459734184686e-2, q34 = 1;
00321 
00322    Double_t v  = TMath::Abs(x)/w2;
00323    Double_t vv = v*v;
00324    Double_t ap, aq, h, hc, y;
00325    if (v < 0.5) {
00326       y=vv;
00327       ap=p13;
00328       aq=q13;
00329       ap = p12 +y*ap;
00330       ap = p11 +y*ap;
00331       ap = p10 +y*ap;
00332       aq = q12 +y*aq;
00333       aq = q11 +y*aq;
00334       aq = q10 +y*aq;
00335       h  = v*ap/aq;
00336       hc = 1-h;
00337    } else if (v < 4) {
00338       ap = p27;
00339       aq = q27;
00340       ap = p26 +v*ap;
00341       ap = p25 +v*ap;
00342       ap = p24 +v*ap;
00343       ap = p23 +v*ap;
00344       ap = p22 +v*ap;
00345       ap = p21 +v*ap;
00346       ap = p20 +v*ap;
00347       aq = q26 +v*aq;
00348       aq = q25 +v*aq;
00349       aq = q24 +v*aq;
00350       aq = q23 +v*aq;
00351       aq = q22 +v*aq;
00352       aq = q21 +v*aq;
00353       aq = q20 +v*aq;
00354       hc = TMath::Exp(-vv)*ap/aq;
00355       h  = 1-hc;
00356    } else {
00357       y  = 1/vv;
00358       ap = p34;
00359       aq = q34;
00360       ap = p33 +y*ap;
00361       ap = p32 +y*ap;
00362       ap = p31 +y*ap;
00363       ap = p30 +y*ap;
00364       aq = q33 +y*aq;
00365       aq = q32 +y*aq;
00366       aq = q31 +y*aq;
00367       aq = q30 +y*aq;
00368       hc = TMath::Exp(-vv)*(c1+y*ap/aq)/v;
00369       h  = 1-hc;
00370    }
00371    if (x > 0) return 0.5 +0.5*h;
00372    else return 0.5*hc;
00373 }
00374 
00375 //______________________________________________________________________________
00376 Double_t TMath::Gamma(Double_t z)
00377 {
00378    // Computation of gamma(z) for all z.
00379    //
00380    // C.Lanczos, SIAM Journal of Numerical Analysis B1 (1964), 86.
00381    //
00382 
00383    return ::ROOT::Math::tgamma(z);
00384 }
00385 
00386 //______________________________________________________________________________
00387 Double_t TMath::Gamma(Double_t a,Double_t x)
00388 {
00389    // Computation of the normalized lower incomplete gamma function P(a,x) as defined in the
00390    // Handbook of Mathematical Functions by Abramowitz and Stegun, formula 6.5.1 on page 260 .
00391    // Its normalization is such that TMath::Gamma(a,+infinity) = 1 .
00392    //
00393    //  Begin_Latex
00394    //  P(a, x) = #frac{1}{#Gamma(a) } #int_{0}^{x} t^{a-1} e^{-t} dt
00395    //   End_Latex
00396    //
00397    //
00398    //--- Nve 14-nov-1998 UU-SAP Utrecht
00399 
00400    return ::ROOT::Math::inc_gamma(a, x);
00401 }
00402 
00403 //______________________________________________________________________________
00404 Double_t TMath::GamCf(Double_t a,Double_t x)
00405 {
00406    // Computation of the incomplete gamma function P(a,x)
00407    // via its continued fraction representation.
00408    //
00409    //--- Nve 14-nov-1998 UU-SAP Utrecht
00410 
00411    Int_t itmax    = 100;      // Maximum number of iterations
00412    Double_t eps   = 3.e-14;   // Relative accuracy
00413    Double_t fpmin = 1.e-30;   // Smallest Double_t value allowed here
00414 
00415    if (a <= 0 || x <= 0) return 0;
00416 
00417    Double_t gln = LnGamma(a);
00418    Double_t b   = x+1-a;
00419    Double_t c   = 1/fpmin;
00420    Double_t d   = 1/b;
00421    Double_t h   = d;
00422    Double_t an,del;
00423    for (Int_t i=1; i<=itmax; i++) {
00424       an = Double_t(-i)*(Double_t(i)-a);
00425       b += 2;
00426       d  = an*d+b;
00427       if (Abs(d) < fpmin) d = fpmin;
00428       c = b+an/c;
00429       if (Abs(c) < fpmin) c = fpmin;
00430       d   = 1/d;
00431       del = d*c;
00432       h   = h*del;
00433       if (Abs(del-1) < eps) break;
00434       //if (i==itmax) cout << "*GamCf(a,x)* a too large or itmax too small" << endl;
00435    }
00436    Double_t v = Exp(-x+a*Log(x)-gln)*h;
00437    return (1-v);
00438 }
00439 
00440 //______________________________________________________________________________
00441 Double_t TMath::GamSer(Double_t a,Double_t x)
00442 {
00443    // Computation of the incomplete gamma function P(a,x)
00444    // via its series representation.
00445    //
00446    //--- Nve 14-nov-1998 UU-SAP Utrecht
00447 
00448    Int_t itmax  = 100;    // Maximum number of iterations
00449    Double_t eps = 3.e-14; // Relative accuracy
00450 
00451    if (a <= 0 || x <= 0) return 0;
00452 
00453    Double_t gln = LnGamma(a);
00454    Double_t ap  = a;
00455    Double_t sum = 1/a;
00456    Double_t del = sum;
00457    for (Int_t n=1; n<=itmax; n++) {
00458       ap  += 1;
00459       del  = del*x/ap;
00460       sum += del;
00461       if (TMath::Abs(del) < Abs(sum*eps)) break;
00462       //if (n==itmax) cout << "*GamSer(a,x)* a too large or itmax too small" << endl;
00463    }
00464    Double_t v = sum*Exp(-x+a*Log(x)-gln);
00465    return v;
00466 }
00467 
00468 //______________________________________________________________________________
00469 Double_t TMath::BreitWigner(Double_t x, Double_t mean, Double_t gamma)
00470 {
00471    // Calculate a Breit Wigner function with mean and gamma.
00472 
00473    Double_t bw = gamma/((x-mean)*(x-mean) + gamma*gamma/4);
00474    return bw/(2*Pi());
00475 }
00476 
00477 //______________________________________________________________________________
00478 Double_t TMath::Gaus(Double_t x, Double_t mean, Double_t sigma, Bool_t norm)
00479 {
00480    // Calculate a gaussian function with mean and sigma.
00481    // If norm=kTRUE (default is kFALSE) the result is divided
00482    // by sqrt(2*Pi)*sigma.
00483 
00484    if (sigma == 0) return 1.e30;
00485    Double_t arg = (x-mean)/sigma;
00486    Double_t res = TMath::Exp(-0.5*arg*arg);
00487    if (!norm) return res;
00488    return res/(2.50662827463100024*sigma); //sqrt(2*Pi)=2.50662827463100024
00489 }
00490 
00491 //______________________________________________________________________________
00492 Double_t TMath::Landau(Double_t x, Double_t mpv, Double_t sigma, Bool_t norm)
00493 {
00494    // The LANDAU function. 
00495    // mpv is a location parameter and correspond approximatly to the most probable value
00496    // and sigma is a scale parameter (not the sigma of the full distribution which is not defined)
00497    // Note that for mpv=0 and sigma=1 (default values) the exact location of the maximum of the distribution (most proble value) is at 
00498    // x = -0.22278
00499    // This function has been adapted from the CERNLIB routine G110 denlan.
00500    // If norm=kTRUE (default is kFALSE) the result is divided by sigma
00501 
00502    if (sigma <= 0) return 0; 
00503    Double_t den = ::ROOT::Math::landau_pdf( (x-mpv)/sigma ); 
00504    if (!norm) return den;
00505    return den/sigma;
00506 }
00507 
00508 //______________________________________________________________________________
00509 Double_t TMath::LnGamma(Double_t z)
00510 {
00511    // Computation of ln[gamma(z)] for all z.
00512    //
00513    // C.Lanczos, SIAM Journal of Numerical Analysis B1 (1964), 86.
00514    //
00515    // The accuracy of the result is better than 2e-10.
00516    //
00517    //--- Nve 14-nov-1998 UU-SAP Utrecht
00518 
00519    return ::ROOT::Math::lgamma(z);
00520 }
00521 
00522 //______________________________________________________________________________
00523 Float_t TMath::Normalize(Float_t v[3])
00524 {
00525    // Normalize a vector v in place.
00526    // Returns the norm of the original vector.
00527 
00528    Float_t d = Sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
00529    if (d != 0) {
00530       v[0] /= d;
00531       v[1] /= d;
00532       v[2] /= d;
00533    }
00534    return d;
00535 }
00536 
00537 //______________________________________________________________________________
00538 Double_t TMath::Normalize(Double_t v[3])
00539 {
00540    // Normalize a vector v in place.
00541    // Returns the norm of the original vector.
00542    // This implementation (thanks Kevin Lynch <krlynch@bu.edu>) is protected
00543    // against possible overflows.
00544 
00545    // Find the largest element, and divide that one out.
00546 
00547    Double_t av0 = Abs(v[0]), av1 = Abs(v[1]), av2 = Abs(v[2]);
00548 
00549    Double_t amax, foo, bar;
00550    // 0 >= {1, 2}
00551    if( av0 >= av1 && av0 >= av2 ) {
00552       amax = av0;
00553       foo = av1;
00554       bar = av2;
00555    }
00556    // 1 >= {0, 2}
00557    else if (av1 >= av0 && av1 >= av2) {
00558       amax = av1;
00559       foo = av0;
00560       bar = av2;
00561    }
00562    // 2 >= {0, 1}
00563    else {
00564       amax = av2;
00565       foo = av0;
00566       bar = av1;
00567    }
00568 
00569    if (amax == 0.0)
00570       return 0.;
00571 
00572    Double_t foofrac = foo/amax, barfrac = bar/amax;
00573    Double_t d = amax * Sqrt(1.+foofrac*foofrac+barfrac*barfrac);
00574 
00575    v[0] /= d;
00576    v[1] /= d;
00577    v[2] /= d;
00578    return d;
00579 }
00580 
00581 //______________________________________________________________________________
00582 Double_t TMath::Poisson(Double_t x, Double_t par)
00583 {
00584   // compute the Poisson distribution function for (x,par)
00585   // The Poisson PDF is implemented by means of Euler's Gamma-function
00586   // (for the factorial), so for all integer arguments it is correct.
00587   // BUT for non-integer values it IS NOT equal to the Poisson distribution.
00588   // see TMath::PoissonI to get a non-smooth function.
00589   // Note that for large values of par, it is better to call
00590   //     TMath::Gaus(x,par,sqrt(par),kTRUE)
00591 //Begin_Html
00592 /*
00593 <img src="gif/Poisson.gif">
00594 */
00595 //End_Html
00596 
00597    if (x<0)
00598       return 0;
00599    else if (x == 0.0)
00600       return 1./Exp(par);
00601    else {
00602       Double_t lnpoisson = x*log(par)-par-LnGamma(x+1.);
00603       return Exp(lnpoisson);
00604    }
00605    // An alternative strategy is to transition to a Gaussian approximation for
00606    // large values of par ...
00607    //   else {
00608    //     return Gaus(x,par,Sqrt(par),kTRUE);
00609    //   }
00610 }
00611 
00612 //______________________________________________________________________________
00613 Double_t TMath::PoissonI(Double_t x, Double_t par)
00614 {
00615   // compute the Poisson distribution function for (x,par)
00616   // This is a non-smooth function.  
00617   // This function is equivalent to ROOT::Math::poisson_pdf
00618 //Begin_Html
00619 /*
00620 <img src="gif/PoissonI.gif">
00621 */
00622 //End_Html
00623 
00624    Int_t ix = Int_t(x);
00625    return Poisson(ix,par); 
00626 }
00627 
00628 //______________________________________________________________________________
00629 Double_t TMath::Prob(Double_t chi2,Int_t ndf)
00630 {
00631    // Computation of the probability for a certain Chi-squared (chi2)
00632    // and number of degrees of freedom (ndf).
00633    //
00634    // Calculations are based on the incomplete gamma function P(a,x),
00635    // where a=ndf/2 and x=chi2/2.
00636    //
00637    // P(a,x) represents the probability that the observed Chi-squared
00638    // for a correct model should be less than the value chi2.
00639    //
00640    // The returned probability corresponds to 1-P(a,x),
00641    // which denotes the probability that an observed Chi-squared exceeds
00642    // the value chi2 by chance, even for a correct model.
00643    //
00644    //--- NvE 14-nov-1998 UU-SAP Utrecht
00645 
00646    if (ndf <= 0) return 0; // Set CL to zero in case ndf<=0
00647 
00648    if (chi2 <= 0) {
00649       if (chi2 < 0) return 0;
00650       else          return 1;
00651    }
00652 
00653    return ::ROOT::Math::chisquared_cdf_c(chi2,ndf); 
00654 }
00655 
00656 //______________________________________________________________________________
00657 Double_t TMath::KolmogorovProb(Double_t z)
00658 {
00659    // Calculates the Kolmogorov distribution function,
00660    //Begin_Html
00661    /*
00662    <img src="gif/kolmogorov.gif">
00663    */
00664    //End_Html
00665    // which gives the probability that Kolmogorov's test statistic will exceed
00666    // the value z assuming the null hypothesis. This gives a very powerful
00667    // test for comparing two one-dimensional distributions.
00668    // see, for example, Eadie et al, "statistocal Methods in Experimental
00669    // Physics', pp 269-270).
00670    //
00671    // This function returns the confidence level for the null hypothesis, where:
00672    //   z = dn*sqrt(n), and
00673    //   dn  is the maximum deviation between a hypothetical distribution
00674    //       function and an experimental distribution with
00675    //   n    events
00676    //
00677    // NOTE: To compare two experimental distributions with m and n events,
00678    //       use z = sqrt(m*n/(m+n))*dn
00679    //
00680    // Accuracy: The function is far too accurate for any imaginable application.
00681    //           Probabilities less than 10^-15 are returned as zero.
00682    //           However, remember that the formula is only valid for "large" n.
00683    // Theta function inversion formula is used for z <= 1
00684    //
00685    // This function was translated by Rene Brun from PROBKL in CERNLIB.
00686 
00687    Double_t fj[4] = {-2,-8,-18,-32}, r[4];
00688    const Double_t w = 2.50662827;
00689    // c1 - -pi**2/8, c2 = 9*c1, c3 = 25*c1
00690    const Double_t c1 = -1.2337005501361697;
00691    const Double_t c2 = -11.103304951225528;
00692    const Double_t c3 = -30.842513753404244;
00693 
00694    Double_t u = TMath::Abs(z);
00695    Double_t p;
00696    if (u < 0.2) {
00697       p = 1;
00698    } else if (u < 0.755) {
00699       Double_t v = 1./(u*u);
00700       p = 1 - w*(TMath::Exp(c1*v) + TMath::Exp(c2*v) + TMath::Exp(c3*v))/u;
00701    } else if (u < 6.8116) {
00702       r[1] = 0;
00703       r[2] = 0;
00704       r[3] = 0;
00705       Double_t v = u*u;
00706       Int_t maxj = TMath::Max(1,TMath::Nint(3./u));
00707       for (Int_t j=0; j<maxj;j++) {
00708          r[j] = TMath::Exp(fj[j]*v);
00709       }
00710       p = 2*(r[0] - r[1] +r[2] - r[3]);
00711    } else {
00712       p = 0;
00713    }
00714    return p;
00715    }
00716 
00717 //______________________________________________________________________________
00718 Double_t TMath::KolmogorovTest(Int_t na, const Double_t *a, Int_t nb, const Double_t *b, Option_t *option)
00719 {
00720 //  Statistical test whether two one-dimensional sets of points are compatible
00721 //  with coming from the same parent distribution, using the Kolmogorov test.
00722 //  That is, it is used to compare two experimental distributions of unbinned data.
00723 //
00724 //  Input:
00725 //  a,b: One-dimensional arrays of length na, nb, respectively.
00726 //       The elements of a and b must be given in ascending order.
00727 //  option is a character string to specify options
00728 //         "D" Put out a line of "Debug" printout
00729 //         "M" Return the Maximum Kolmogorov distance instead of prob
00730 //
00731 //  Output:
00732 // The returned value prob is a calculated confidence level which gives a
00733 // statistical test for compatibility of a and b.
00734 // Values of prob close to zero are taken as indicating a small probability
00735 // of compatibility. For two point sets drawn randomly from the same parent
00736 // distribution, the value of prob should be uniformly distributed between
00737 // zero and one.
00738 //   in case of error the function return -1
00739 //   If the 2 sets have a different number of points, the minimum of
00740 //   the two sets is used.
00741 //
00742 // Method:
00743 // The Kolmogorov test is used. The test statistic is the maximum deviation
00744 // between the two integrated distribution functions, multiplied by the
00745 // normalizing factor (rdmax*sqrt(na*nb/(na+nb)).
00746 //
00747 //  Code adapted by Rene Brun from CERNLIB routine TKOLMO (Fred James)
00748 //   (W.T. Eadie, D. Drijard, F.E. James, M. Roos and B. Sadoulet,
00749 //      Statistical Methods in Experimental Physics, (North-Holland,
00750 //      Amsterdam 1971) 269-271)
00751 //
00752 //  Method Improvement by Jason A Detwiler (JADetwiler@lbl.gov)
00753 //  -----------------------------------------------------------
00754 //   The nuts-and-bolts of the TMath::KolmogorovTest() algorithm is a for-loop
00755 //   over the two sorted arrays a and b representing empirical distribution
00756 //   functions. The for-loop handles 3 cases: when the next points to be
00757 //   evaluated satisfy a>b, a<b, or a=b:
00758 //
00759 //      for (Int_t i=0;i<na+nb;i++) {
00760 //         if (a[ia-1] < b[ib-1]) {
00761 //            rdiff -= sa;
00762 //            ia++;
00763 //            if (ia > na) {ok = kTRUE; break;}
00764 //         } else if (a[ia-1] > b[ib-1]) {
00765 //            rdiff += sb;
00766 //            ib++;
00767 //            if (ib > nb) {ok = kTRUE; break;}
00768 //         } else {
00769 //            rdiff += sb - sa;
00770 //            ia++;
00771 //            ib++;
00772 //            if (ia > na) {ok = kTRUE; break;}
00773 //            if (ib > nb) {ok = kTRUE; break;}
00774 //        }
00775 //         rdmax = TMath::Max(rdmax,TMath::Abs(rdiff));
00776 //      }
00777 //
00778 //   For the last case, a=b, the algorithm advances each array by one index in an
00779 //   attempt to move through the equality. However, this is incorrect when one or
00780 //   the other of a or b (or both) have a repeated value, call it x. For the KS
00781 //   statistic to be computed properly, rdiff needs to be calculated after all of
00782 //   the a and b at x have been tallied (this is due to the definition of the
00783 //   empirical distribution function; another way to convince yourself that the
00784 //   old CERNLIB method is wrong is that it implies that the function defined as the
00785 //   difference between a and b is multi-valued at x -- besides being ugly, this
00786 //   would invalidate Kolmogorov's theorem).
00787 //
00788 //   The solution is to just add while-loops into the equality-case handling to
00789 //   perform the tally:
00790 //
00791 //         } else {
00792 //            double x = a[ia-1];
00793 //            while(a[ia-1] == x && ia <= na) {
00794 //              rdiff -= sa;
00795 //              ia++;
00796 //            }
00797 //            while(b[ib-1] == x && ib <= nb) {
00798 //              rdiff += sb;
00799 //              ib++;
00800 //            }
00801 //            if (ia > na) {ok = kTRUE; break;}
00802 //            if (ib > nb) {ok = kTRUE; break;}
00803 //         }
00804 //
00805 //
00806 //  NOTE1
00807 //  A good description of the Kolmogorov test can be seen at:
00808 //    http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
00809 
00810 //  LM: Nov 2010: clean up and returns now a zero distance when vectors are the same 
00811 
00812    TString opt = option;
00813    opt.ToUpper();
00814 
00815    Double_t prob = -1;
00816 //      Require at least two points in each graph
00817    if (!a || !b || na <= 2 || nb <= 2) {
00818       Error("KolmogorovTest","Sets must have more than 2 points");
00819       return prob;
00820    }
00821 //     Constants needed
00822    Double_t rna = na;
00823    Double_t rnb = nb;
00824    Double_t sa  = 1./rna;
00825    Double_t sb  = 1./rnb;
00826    Double_t rdiff = 0;
00827    Double_t rdmax = 0;
00828    Int_t ia = 0;
00829    Int_t ib = 0;
00830 
00831 //    Main loop over point sets to find max distance
00832 //    rdiff is the running difference, and rdmax the max.
00833    Bool_t ok = kFALSE;
00834    for (Int_t i=0;i<na+nb;i++) {
00835       if (a[ia] < b[ib]) {
00836          rdiff -= sa;
00837          ia++;
00838          if (ia >= na) {ok = kTRUE; break;}
00839       } else if (a[ia] > b[ib]) {
00840          rdiff += sb;
00841          ib++;
00842          if (ib >= nb) {ok = kTRUE; break;}
00843       } else {
00844          // special cases for the ties 
00845          double x = a[ia];
00846          while(a[ia] == x && ia < na) {
00847             rdiff -= sa;
00848             ia++;
00849          }
00850          while(b[ib] == x && ib < nb) {
00851             rdiff += sb;
00852             ib++;
00853          }
00854          if (ia >= na) {ok = kTRUE; break;}
00855          if (ib >= nb) {ok = kTRUE; break;}
00856       }
00857       rdmax = TMath::Max(rdmax,TMath::Abs(rdiff));
00858    }
00859    //    Should never terminate this loop with ok = kFALSE!
00860    R__ASSERT(ok);
00861 
00862    if (ok) {
00863       rdmax = TMath::Max(rdmax,TMath::Abs(rdiff));
00864       Double_t z = rdmax * TMath::Sqrt(rna*rnb/(rna+rnb));
00865       prob = TMath::KolmogorovProb(z);
00866    }
00867       // debug printout
00868    if (opt.Contains("D")) {
00869       printf(" Kolmogorov Probability = %g, Max Dist = %g\n",prob,rdmax);
00870    }
00871    if(opt.Contains("M")) return rdmax;
00872    else                  return prob;
00873 }
00874 
00875 
00876 //______________________________________________________________________________
00877 Double_t TMath::Voigt(Double_t xx, Double_t sigma, Double_t lg, Int_t r)
00878 {
00879    // Computation of Voigt function (normalised).
00880    // Voigt is a convolution of 
00881    // gauss(xx) = 1/(sqrt(2*pi)*sigma) * exp(xx*xx/(2*sigma*sigma)
00882    // and
00883    // lorentz(xx) = (1/pi) * (lg/2) / (xx*xx + lg*lg/4)
00884    // functions.
00885    //
00886    // The Voigt function is known to be the real part of Faddeeva function also
00887    // called complex error function [2].
00888    //
00889    // The algoritm was developed by J. Humlicek [1].
00890    // This code is based on fortran code presented by R. J. Wells [2].
00891    // Translated and adapted by Miha D. Puc
00892    //
00893    // To calculate the Faddeeva function with relative error less than 10^(-r).
00894    // r can be set by the the user subject to the constraints 2 <= r <= 5.
00895    //
00896    // [1] J. Humlicek, JQSRT, 21, 437 (1982).
00897    // [2] R.J. Wells "Rapid Approximation to the Voigt/Faddeeva Function and its
00898    // Derivatives" JQSRT 62 (1999), pp 29-48.
00899    // http://www-atm.physics.ox.ac.uk/user/wells/voigt.html
00900 
00901    if ((sigma < 0 || lg < 0) || (sigma==0 && lg==0)) {
00902       return 0;  // Not meant to be for those who want to be thinner than 0
00903    }
00904 
00905    if (sigma == 0) {
00906       return lg * 0.159154943  / (xx*xx + lg*lg /4); //pure Lorentz
00907    }
00908 
00909    if (lg == 0) {   //pure gauss
00910       return 0.39894228 / sigma * TMath::Exp(-xx*xx / (2*sigma*sigma));
00911    }
00912 
00913    Double_t x, y, k;
00914    x = xx / sigma / 1.41421356;
00915    y = lg / 2 / sigma / 1.41421356;
00916 
00917    Double_t r0, r1;
00918 
00919    if (r < 2) r = 2;
00920    if (r > 5) r = 5;
00921 
00922    r0=1.51 * exp(1.144 * (Double_t)r);
00923    r1=1.60 * exp(0.554 * (Double_t)r);
00924 
00925    // Constants
00926 
00927    const Double_t rrtpi = 0.56418958;  // 1/SQRT(pi)
00928 
00929    Double_t y0, y0py0, y0q;                      // for CPF12 algorithm
00930    y0 = 1.5;
00931    y0py0 = y0 + y0;
00932    y0q = y0 * y0;
00933 
00934    Double_t c[6] = { 1.0117281, -0.75197147, 0.012557727, 0.010022008, -0.00024206814, 0.00000050084806};
00935    Double_t s[6] = { 1.393237, 0.23115241, -0.15535147, 0.0062183662, 0.000091908299, -0.00000062752596};
00936    Double_t t[6] = { 0.31424038, 0.94778839, 1.5976826, 2.2795071, 3.0206370, 3.8897249};
00937 
00938    // Local variables
00939 
00940    int j;                                        // Loop variables
00941    int rg1, rg2, rg3;                            // y polynomial flags
00942    Double_t abx, xq, yq, yrrtpi;                 // --x--, x^2, y^2, y/SQRT(pi)
00943    Double_t xlim0, xlim1, xlim2, xlim3, xlim4;   // --x-- on region boundaries
00944    Double_t a0=0, d0=0, d2=0, e0=0, e2=0, e4=0, h0=0, h2=0, h4=0, h6=0;// W4 temporary variables
00945    Double_t p0=0, p2=0, p4=0, p6=0, p8=0, z0=0, z2=0, z4=0, z6=0, z8=0;
00946    Double_t xp[6], xm[6], yp[6], ym[6];          // CPF12 temporary values
00947    Double_t mq[6], pq[6], mf[6], pf[6];
00948    Double_t d, yf, ypy0, ypy0q;
00949 
00950    //***** Start of executable code *****************************************
00951 
00952    rg1 = 1;  // Set flags
00953    rg2 = 1;
00954    rg3 = 1;
00955    yq = y * y;  // y^2
00956    yrrtpi = y * rrtpi;  // y/SQRT(pi)
00957 
00958    // Region boundaries when both k and L are required or when R<>4
00959 
00960    xlim0 = r0 - y;
00961    xlim1 = r1 - y;
00962    xlim3 = 3.097 * y - 0.45;
00963    xlim2 = 6.8 - y;
00964    xlim4 = 18.1 * y + 1.65;
00965    if ( y <= 1e-6 ) {                      // When y<10^-6 avoid W4 algorithm
00966       xlim1 = xlim0;
00967       xlim2 = xlim0;
00968    }
00969 
00970    abx = fabs(x);                                // |x|
00971    xq = abx * abx;                               // x^2
00972    if ( abx > xlim0 ) {                          // Region 0 algorithm
00973       k = yrrtpi / (xq + yq);
00974    } else if ( abx > xlim1 ) {                   // Humlicek W4 Region 1
00975       if ( rg1 != 0 ) {                          // First point in Region 1
00976          rg1 = 0;
00977          a0 = yq + 0.5;                          // Region 1 y-dependents
00978          d0 = a0*a0;
00979          d2 = yq + yq - 1.0;
00980       }
00981       d = rrtpi / (d0 + xq*(d2 + xq));
00982       k = d * y * (a0 + xq);
00983    } else if ( abx > xlim2 ) {                   // Humlicek W4 Region 2
00984       if ( rg2 != 0 ) {                          // First point in Region 2
00985          rg2 = 0;
00986          h0 = 0.5625 + yq * (4.5 + yq * (10.5 + yq * (6.0 + yq)));
00987                                                  // Region 2 y-dependents
00988          h2 = -4.5 + yq * (9.0 + yq * ( 6.0 + yq * 4.0));
00989          h4 = 10.5 - yq * (6.0 - yq * 6.0);
00990          h6 = -6.0 + yq * 4.0;
00991          e0 = 1.875 + yq * (8.25 + yq * (5.5 + yq));
00992          e2 = 5.25 + yq * (1.0 + yq * 3.0);
00993          e4 = 0.75 * h6;
00994       }
00995       d = rrtpi / (h0 + xq * (h2 + xq * (h4 + xq * (h6 + xq))));
00996       k = d * y * (e0 + xq * (e2 + xq * (e4 + xq)));
00997    } else if ( abx < xlim3 ) {                   // Humlicek W4 Region 3
00998       if ( rg3 != 0 ) {                          // First point in Region 3
00999          rg3 = 0;
01000          z0 = 272.1014 + y * (1280.829 + y *
01001                               (2802.870 + y *
01002                                (3764.966 + y *
01003                                 (3447.629 + y *
01004                                  (2256.981 + y *
01005                                   (1074.409 + y *
01006                                    (369.1989  + y *
01007                                     (88.26741 + y *
01008                                      (13.39880 + y)
01009                                      ))))))));   // Region 3 y-dependents
01010          z2 = 211.678 + y * (902.3066 + y *
01011                              (1758.336 + y *
01012                               (2037.310 + y *
01013                                (1549.675 + y *
01014                                 (793.4273 + y *
01015                                  (266.2987 + y *
01016                                   (53.59518 + y * 5.0)
01017                                   ))))));
01018          z4 = 78.86585 + y * (308.1852 + y *
01019                               (497.3014 + y *
01020                                (479.2576 + y *
01021                                 (269.2916 + y *
01022                                  (80.39278 + y * 10.0)
01023                                  ))));
01024          z6 = 22.03523 + y * (55.02933 + y *
01025                               (92.75679 + y *
01026                                (53.59518 + y * 10.0)
01027                                ));
01028          z8 = 1.496460 + y * (13.39880 + y * 5.0);
01029          p0 = 153.5168 + y * (549.3954 + y *
01030                               (919.4955 + y *
01031                                (946.8970 + y *
01032                                 (662.8097 + y *
01033                                  (328.2151 + y *
01034                                   (115.3772 + y *
01035                                    (27.93941 + y *
01036                                     (4.264678 + y * 0.3183291)
01037                                     )))))));
01038          p2 = -34.16955 + y * (-1.322256+ y *
01039                                (124.5975 + y *
01040                                 (189.7730 + y *
01041                                  (139.4665 + y *
01042                                   (56.81652 + y *
01043                                    (12.79458 + y * 1.2733163)
01044                                    )))));
01045          p4 = 2.584042 + y * (10.46332 + y *
01046                               (24.01655 + y *
01047                                (29.81482 + y *
01048                                 (12.79568 + y * 1.9099744)
01049                                 )));
01050          p6 = -0.07272979 + y * (0.9377051 + y *
01051                                  (4.266322 + y * 1.273316));
01052          p8 = 0.0005480304 + y * 0.3183291;
01053       }
01054       d = 1.7724538 / (z0 + xq * (z2 + xq * (z4 + xq * (z6 + xq * (z8 + xq)))));
01055       k = d * (p0 + xq * (p2 + xq * (p4 + xq * (p6 + xq * p8))));
01056    } else {                             // Humlicek CPF12 algorithm
01057       ypy0 = y + y0;
01058       ypy0q = ypy0 * ypy0;
01059       k = 0.0;
01060       for (j = 0; j <= 5; j++) {
01061          d = x - t[j];
01062          mq[j] = d * d;
01063          mf[j] = 1.0 / (mq[j] + ypy0q);
01064          xm[j] = mf[j] * d;
01065          ym[j] = mf[j] * ypy0;
01066          d = x + t[j];
01067          pq[j] = d * d;
01068          pf[j] = 1.0 / (pq[j] + ypy0q);
01069          xp[j] = pf[j] * d;
01070          yp[j] = pf[j] * ypy0;
01071       }
01072       if ( abx <= xlim4 ) {                      // Humlicek CPF12 Region I
01073          for (j = 0; j <= 5; j++) {
01074             k = k + c[j]*(ym[j]+yp[j]) - s[j]*(xm[j]-xp[j]) ;
01075          }
01076       } else {                                   // Humlicek CPF12 Region II
01077          yf = y + y0py0;
01078          for ( j = 0; j <= 5; j++) {
01079             k = k + (c[j] *
01080                  (mq[j] * mf[j] - y0 * ym[j])
01081                     + s[j] * yf * xm[j]) / (mq[j]+y0q)
01082                  + (c[j] * (pq[j] * pf[j] - y0 * yp[j])
01083                    - s[j] * yf * xp[j]) / (pq[j]+y0q);
01084          }
01085          k = y * k + exp( -xq );
01086       }
01087    }
01088    return k / 2.506628 / sigma; // Normalize by dividing by sqrt(2*pi)*sigma.
01089 }
01090 
01091 //______________________________________________________________________________
01092 Bool_t TMath::RootsCubic(const Double_t coef[4],Double_t &a, Double_t &b, Double_t &c)
01093 {
01094    // Calculates roots of polynomial of 3rd order a*x^3 + b*x^2 + c*x + d, where
01095    // a == coef[3], b == coef[2], c == coef[1], d == coef[0]
01096    //coef[3] must be different from 0
01097    // If the boolean returned by the method is false:
01098    //    ==> there are 3 real roots a,b,c
01099    // If the boolean returned by the method is true:
01100    //    ==> there is one real root a and 2 complex conjugates roots (b+i*c,b-i*c)
01101    // Author: Francois-Xavier Gentit
01102 
01103    Bool_t complex = kFALSE;
01104    Double_t r,s,t,p,q,d,ps3,ps33,qs2,u,v,tmp,lnu,lnv,su,sv,y1,y2,y3;
01105    a    = 0;
01106    b    = 0;
01107    c    = 0;
01108    if (coef[3] == 0) return complex;
01109    r    = coef[2]/coef[3];
01110    s    = coef[1]/coef[3];
01111    t    = coef[0]/coef[3];
01112    p    = s - (r*r)/3;
01113    ps3  = p/3;
01114    q    = (2*r*r*r)/27.0 - (r*s)/3 + t;
01115    qs2  = q/2;
01116    ps33 = ps3*ps3*ps3;
01117    d    = ps33 + qs2*qs2;
01118    if (d>=0) {
01119       complex = kTRUE;
01120       d   = TMath::Sqrt(d);
01121       u   = -qs2 + d;
01122       v   = -qs2 - d;
01123       tmp = 1./3.;
01124       lnu = TMath::Log(TMath::Abs(u));
01125       lnv = TMath::Log(TMath::Abs(v));
01126       su  = TMath::Sign(1.,u);
01127       sv  = TMath::Sign(1.,v);
01128       u   = su*TMath::Exp(tmp*lnu);
01129       v   = sv*TMath::Exp(tmp*lnv);
01130       y1  = u + v;
01131       y2  = -y1/2;
01132       y3  = ((u-v)*TMath::Sqrt(3.))/2;
01133       tmp = r/3;
01134       a   = y1 - tmp;
01135       b   = y2 - tmp;
01136       c   = y3;
01137    } else {
01138       Double_t phi,cphi,phis3,c1,c2,c3,pis3;
01139       ps3   = -ps3;
01140       ps33  = -ps33;
01141       cphi  = -qs2/TMath::Sqrt(ps33);
01142       phi   = TMath::ACos(cphi);
01143       phis3 = phi/3;
01144       pis3  = TMath::Pi()/3;
01145       c1    = TMath::Cos(phis3);
01146       c2    = TMath::Cos(pis3 + phis3);
01147       c3    = TMath::Cos(pis3 - phis3);
01148       tmp   = TMath::Sqrt(ps3);
01149       y1    = 2*tmp*c1;
01150       y2    = -2*tmp*c2;
01151       y3    = -2*tmp*c3;
01152       tmp = r/3;
01153       a   = y1 - tmp;
01154       b   = y2 - tmp;
01155       c   = y3 - tmp;
01156    }
01157    return complex;
01158 }
01159 
01160 //______________________________________________________________________________
01161 void TMath::Quantiles(Int_t n, Int_t nprob, Double_t *x, Double_t *quantiles, Double_t *prob, Bool_t isSorted, Int_t *index, Int_t type)
01162 {
01163    //Computes sample quantiles, corresponding to the given probabilities
01164    //Parameters:
01165    //  x -the data sample
01166    //  n - its size
01167    //  quantiles - computed quantiles are returned in there
01168    //  prob - probabilities where to compute quantiles
01169    //  nprob - size of prob array
01170    //  isSorted - is the input array x sorted?
01171    //  NOTE, that when the input is not sorted, an array of integers of size n needs
01172    //        to be allocated. It can be passed by the user in parameter index,
01173    //        or, if not passed, it will be allocated inside the function
01174    //
01175    //  type - method to compute (from 1 to 9). Following types are provided:
01176    //  Discontinuous:
01177    //    type=1 - inverse of the empirical distribution function
01178    //    type=2 - like type 1, but with averaging at discontinuities
01179    //    type=3 - SAS definition: nearest even order statistic
01180    //  Piecwise linear continuous:
01181    //    In this case, sample quantiles can be obtained by linear interpolation
01182    //    between the k-th order statistic and p(k).
01183    //    type=4 - linear interpolation of empirical cdf, p(k)=k/n;
01184    //    type=5 - a very popular definition, p(k) = (k-0.5)/n;
01185    //    type=6 - used by Minitab and SPSS, p(k) = k/(n+1);
01186    //    type=7 - used by S-Plus and R, p(k) = (k-1)/(n-1);
01187    //    type=8 - resulting sample quantiles are approximately median unbiased
01188    //             regardless of the distribution of x. p(k) = (k-1/3)/(n+1/3);
01189    //    type=9 - resulting sample quantiles are approximately unbiased, when
01190    //             the sample comes from Normal distribution. p(k)=(k-3/8)/(n+1/4);
01191    //
01192    //    default type = 7
01193    //
01194    // References:
01195    // 1) Hyndman, R.J and Fan, Y, (1996) "Sample quantiles in statistical packages"
01196    //                                     American Statistician, 50, 361-365
01197    // 2) R Project documentation for the function quantile of package {stats}
01198 
01199    if (type<1 || type>9){
01200       printf("illegal value of type\n");
01201       return;
01202    }
01203    Double_t g, npm, np, xj, xjj;
01204    Int_t j, intnpm;
01205    Int_t *ind = 0;
01206    Bool_t isAllocated = kFALSE;
01207    if (!isSorted){
01208       if (index) ind = index;
01209       else {
01210          ind = new Int_t[n];
01211          isAllocated = kTRUE;
01212       }
01213    }
01214    npm=0;
01215    //Discontinuous functions
01216    if (type<4){
01217       for (Int_t i=0; i<nprob; i++){
01218          npm = n*prob[i];
01219          if (npm < 1){
01220             if(isSorted)
01221                quantiles[i] = x[0];
01222             else
01223                quantiles[i] = TMath::KOrdStat(n, x, 0, ind);
01224          } else {
01225             j=TMath::Max(TMath::FloorNint(npm)-1, 0);
01226             if (npm - j -1 > 1e-14){
01227                if (isSorted)
01228                   quantiles[i] = x[j+1];
01229                else
01230                   quantiles[i] = TMath::KOrdStat(n, x, j+1, ind);
01231             } else {
01232                if (isSorted) xj = x[j];
01233                else xj = TMath::KOrdStat(n, x, j, ind);
01234                if (type==1) quantiles[i] = xj;
01235                if (type==2) {
01236                   if (isSorted) xjj = x[j+1];
01237                   else xjj = TMath::KOrdStat(n, x, j+1, ind);
01238                   quantiles[i] = 0.5*(xj + xjj);
01239                }
01240                if (type==3) {
01241                   if (!TMath::Even(j-1)){
01242                      if (isSorted) xjj = x[j+1];
01243                      else xjj = TMath::KOrdStat(n, x, j+1, ind);
01244                      quantiles[i] = xjj;
01245                   } else
01246                      quantiles[i] = xj;
01247                }
01248             }
01249          }
01250       }
01251    }
01252    if (type>3){
01253       for (Int_t i=0; i<nprob; i++){
01254          np=n*prob[i];
01255          if (np<1 && type!=7 && type!=4)
01256             quantiles[i] = TMath::KOrdStat(n, x, 0, ind);
01257          else {
01258             if (type==4) npm = np;
01259             if (type==5) npm = np + 0.5;
01260             if (type==6) npm = np + prob[i];
01261             if (type==7) npm = np - prob[i] +1;
01262             if (type==8) npm = np+(1./3.)*(1+prob[i]);
01263             if (type==9) npm = np + 0.25*prob[i] + 0.375;
01264             intnpm = TMath::FloorNint(npm);
01265             j = TMath::Max(intnpm - 1, 0);
01266             g = npm - intnpm;
01267             if (isSorted){
01268                xj = x[j];
01269                xjj = x[j+1];
01270             } else {
01271                xj = TMath::KOrdStat(n, x, j, ind);
01272                xjj = TMath::KOrdStat(n, x, j+1, ind);
01273             }
01274             quantiles[i] = (1-g)*xj + g*xjj;
01275          }
01276       }
01277    }
01278 
01279    if (isAllocated)
01280       delete [] ind;
01281 }
01282 
01283 //______________________________________________________________________________
01284 void TMath::BubbleHigh(Int_t Narr, Double_t *arr1, Int_t *arr2)
01285 {
01286    // Bubble sort variant to obtain the order of an array's elements into
01287    // an index in order to do more useful things than the standard built
01288    // in functions.
01289    // *arr1 is unchanged;
01290    // *arr2 is the array of indicies corresponding to the decending value
01291    // of arr1 with arr2[0] corresponding to the largest arr1 value and
01292    // arr2[Narr] the smallest.
01293    //
01294    //  Author: Adrian Bevan (bevan@slac.stanford.edu)
01295 
01296    if (Narr <= 0) return;
01297    double *localArr1 = new double[Narr];
01298    int    *localArr2 = new int[Narr];
01299    int iEl;
01300    int iEl2;
01301 
01302    for(iEl = 0; iEl < Narr; iEl++) {
01303       localArr1[iEl] = arr1[iEl];
01304       localArr2[iEl] = iEl;
01305    }
01306 
01307    for (iEl = 0; iEl < Narr; iEl++) {
01308       for (iEl2 = Narr-1; iEl2 > iEl; --iEl2) {
01309          if (localArr1[iEl2-1] < localArr1[iEl2]) {
01310             double tmp        = localArr1[iEl2-1];
01311             localArr1[iEl2-1] = localArr1[iEl2];
01312             localArr1[iEl2]   = tmp;
01313 
01314             int    tmp2       = localArr2[iEl2-1];
01315             localArr2[iEl2-1] = localArr2[iEl2];
01316             localArr2[iEl2]   = tmp2;
01317          }
01318       }
01319    }
01320 
01321    for (iEl = 0; iEl < Narr; iEl++) {
01322       arr2[iEl] = localArr2[iEl];
01323    }
01324    delete [] localArr2;
01325    delete [] localArr1;
01326 }
01327 
01328 //______________________________________________________________________________
01329 void TMath::BubbleLow(Int_t Narr, Double_t *arr1, Int_t *arr2)
01330 {
01331    // Opposite ordering of the array arr2[] to that of BubbleHigh.
01332    //
01333    //  Author: Adrian Bevan (bevan@slac.stanford.edu)
01334 
01335    if (Narr <= 0) return;
01336    double *localArr1 = new double[Narr];
01337    int    *localArr2 = new int[Narr];
01338    int iEl;
01339    int iEl2;
01340 
01341    for (iEl = 0; iEl < Narr; iEl++) {
01342       localArr1[iEl] = arr1[iEl];
01343       localArr2[iEl] = iEl;
01344    }
01345 
01346    for (iEl = 0; iEl < Narr; iEl++) {
01347       for (iEl2 = Narr-1; iEl2 > iEl; --iEl2) {
01348          if (localArr1[iEl2-1] > localArr1[iEl2]) {
01349             double tmp        = localArr1[iEl2-1];
01350             localArr1[iEl2-1] = localArr1[iEl2];
01351             localArr1[iEl2]   = tmp;
01352 
01353             int    tmp2       = localArr2[iEl2-1];
01354             localArr2[iEl2-1] = localArr2[iEl2];
01355             localArr2[iEl2]   = tmp2;
01356          }
01357       }
01358    }
01359 
01360    for (iEl = 0; iEl < Narr; iEl++) {
01361       arr2[iEl] = localArr2[iEl];
01362    }
01363    delete [] localArr2;
01364    delete [] localArr1;
01365 }
01366 
01367 
01368 //______________________________________________________________________________
01369 ULong_t TMath::Hash(const void *txt, Int_t ntxt)
01370 {
01371    // Calculates hash index from any char string.
01372    // Based on precalculated table of 256 specially selected numbers.
01373    // These numbers are selected in such a way, that for string
01374    // length == 4 (integer number) the hash is unambigous, i.e.
01375    // from hash value we can recalculate input (no degeneration).
01376    //
01377    // The quality of hash method is good enough, that
01378    // "random" numbers made as R = Hash(1), Hash(2), ...Hash(N)
01379    // tested by <R>, <R*R>, <Ri*Ri+1> gives the same result
01380    // as for libc rand().
01381    //
01382    // For string:  i = TMath::Hash(string,nstring);
01383    // For int:     i = TMath::Hash(&intword,sizeof(int));
01384    // For pointer: i = TMath::Hash(&pointer,sizeof(void*));
01385    //
01386    //              V.Perev
01387    // This function is kept for back compatibility. The code previously in this function
01388    // has been moved to the static function TString::Hash
01389 
01390    return TString::Hash(txt,ntxt);
01391 }
01392 
01393 
01394 //______________________________________________________________________________
01395 ULong_t TMath::Hash(const char *txt)
01396 {
01397    return Hash(txt, Int_t(strlen(txt)));
01398 }
01399 
01400 //______________________________________________________________________________
01401 Double_t TMath::BesselI0(Double_t x)
01402 {
01403    // Compute the modified Bessel function I_0(x) for any real x.
01404    //
01405    //--- NvE 12-mar-2000 UU-SAP Utrecht
01406 
01407    // Parameters of the polynomial approximation
01408    const Double_t p1=1.0,          p2=3.5156229,    p3=3.0899424,
01409                   p4=1.2067492,    p5=0.2659732,    p6=3.60768e-2,  p7=4.5813e-3;
01410 
01411    const Double_t q1= 0.39894228,  q2= 1.328592e-2, q3= 2.25319e-3,
01412                   q4=-1.57565e-3,  q5= 9.16281e-3,  q6=-2.057706e-2,
01413                   q7= 2.635537e-2, q8=-1.647633e-2, q9= 3.92377e-3;
01414 
01415    const Double_t k1 = 3.75;
01416    Double_t ax = TMath::Abs(x);
01417 
01418    Double_t y=0, result=0;
01419 
01420    if (ax < k1) {
01421       Double_t xx = x/k1;
01422       y = xx*xx;
01423       result = p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))));
01424    } else {
01425       y = k1/ax;
01426       result = (TMath::Exp(ax)/TMath::Sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9))))))));
01427    }
01428    return result;
01429 }
01430 
01431 //______________________________________________________________________________
01432 Double_t TMath::BesselK0(Double_t x)
01433 {
01434    // Compute the modified Bessel function K_0(x) for positive real x.
01435    //
01436    //  M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
01437    //     Applied Mathematics Series vol. 55 (1964), Washington.
01438    //
01439    //--- NvE 12-mar-2000 UU-SAP Utrecht
01440 
01441    // Parameters of the polynomial approximation
01442    const Double_t p1=-0.57721566,  p2=0.42278420,   p3=0.23069756,
01443                   p4= 3.488590e-2, p5=2.62698e-3,   p6=1.0750e-4,    p7=7.4e-6;
01444 
01445    const Double_t q1= 1.25331414,  q2=-7.832358e-2, q3= 2.189568e-2,
01446                   q4=-1.062446e-2, q5= 5.87872e-3,  q6=-2.51540e-3,  q7=5.3208e-4;
01447 
01448    if (x <= 0) {
01449       Error("TMath::BesselK0", "*K0* Invalid argument x = %g\n",x);
01450       return 0;
01451    }
01452 
01453    Double_t y=0, result=0;
01454 
01455    if (x <= 2) {
01456       y = x*x/4;
01457       result = (-log(x/2.)*TMath::BesselI0(x))+(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))));
01458    } else {
01459       y = 2/x;
01460       result = (exp(-x)/sqrt(x))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*q7))))));
01461    }
01462    return result;
01463 }
01464 
01465 //______________________________________________________________________________
01466 Double_t TMath::BesselI1(Double_t x)
01467 {
01468    // Compute the modified Bessel function I_1(x) for any real x.
01469    //
01470    //  M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
01471    //     Applied Mathematics Series vol. 55 (1964), Washington.
01472    //
01473    //--- NvE 12-mar-2000 UU-SAP Utrecht
01474 
01475    // Parameters of the polynomial approximation
01476    const Double_t p1=0.5,          p2=0.87890594,   p3=0.51498869,
01477                   p4=0.15084934,   p5=2.658733e-2,  p6=3.01532e-3,  p7=3.2411e-4;
01478 
01479    const Double_t q1= 0.39894228,  q2=-3.988024e-2, q3=-3.62018e-3,
01480                   q4= 1.63801e-3,  q5=-1.031555e-2, q6= 2.282967e-2,
01481                   q7=-2.895312e-2, q8= 1.787654e-2, q9=-4.20059e-3;
01482 
01483    const Double_t k1 = 3.75;
01484    Double_t ax = TMath::Abs(x);
01485 
01486    Double_t y=0, result=0;
01487 
01488    if (ax < k1) {
01489       Double_t xx = x/k1;
01490       y = xx*xx;
01491       result = x*(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))));
01492    } else {
01493       y = k1/ax;
01494       result = (exp(ax)/sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9))))))));
01495       if (x < 0) result = -result;
01496    }
01497    return result;
01498 }
01499 
01500 //______________________________________________________________________________
01501 Double_t TMath::BesselK1(Double_t x)
01502 {
01503    // Compute the modified Bessel function K_1(x) for positive real x.
01504    //
01505    //  M.Abramowitz and I.A.Stegun, Handbook of Mathematical Functions,
01506    //     Applied Mathematics Series vol. 55 (1964), Washington.
01507    //
01508    //--- NvE 12-mar-2000 UU-SAP Utrecht
01509 
01510    // Parameters of the polynomial approximation
01511    const Double_t p1= 1.,          p2= 0.15443144,  p3=-0.67278579,
01512                   p4=-0.18156897,  p5=-1.919402e-2, p6=-1.10404e-3,  p7=-4.686e-5;
01513 
01514    const Double_t q1= 1.25331414,  q2= 0.23498619,  q3=-3.655620e-2,
01515                   q4= 1.504268e-2, q5=-7.80353e-3,  q6= 3.25614e-3,  q7=-6.8245e-4;
01516 
01517    if (x <= 0) {
01518       Error("TMath::BesselK1", "*K1* Invalid argument x = %g\n",x);
01519       return 0;
01520    }
01521 
01522    Double_t y=0,result=0;
01523 
01524    if (x <= 2) {
01525       y = x*x/4;
01526       result = (log(x/2.)*TMath::BesselI1(x))+(1./x)*(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))));
01527    } else {
01528       y = 2/x;
01529       result = (exp(-x)/sqrt(x))*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*q7))))));
01530    }
01531    return result;
01532 }
01533 
01534 //______________________________________________________________________________
01535 Double_t TMath::BesselK(Int_t n,Double_t x)
01536 {
01537    // Compute the Integer Order Modified Bessel function K_n(x)
01538    // for n=0,1,2,... and positive real x.
01539    //
01540    //--- NvE 12-mar-2000 UU-SAP Utrecht
01541 
01542    if (x <= 0 || n < 0) {
01543       Error("TMath::BesselK", "*K* Invalid argument(s) (n,x) = (%d, %g)\n",n,x);
01544       return 0;
01545    }
01546 
01547    if (n==0) return TMath::BesselK0(x);
01548    if (n==1) return TMath::BesselK1(x);
01549 
01550    // Perform upward recurrence for all x
01551    Double_t tox = 2/x;
01552    Double_t bkm = TMath::BesselK0(x);
01553    Double_t bk  = TMath::BesselK1(x);
01554    Double_t bkp = 0;
01555    for (Int_t j=1; j<n; j++) {
01556       bkp = bkm+Double_t(j)*tox*bk;
01557       bkm = bk;
01558       bk  = bkp;
01559    }
01560    return bk;
01561 }
01562 
01563 //______________________________________________________________________________
01564 Double_t TMath::BesselI(Int_t n,Double_t x)
01565 {
01566    // Compute the Integer Order Modified Bessel function I_n(x)
01567    // for n=0,1,2,... and any real x.
01568    //
01569    //--- NvE 12-mar-2000 UU-SAP Utrecht
01570 
01571    Int_t iacc = 40; // Increase to enhance accuracy
01572    const Double_t kBigPositive = 1.e10;
01573    const Double_t kBigNegative = 1.e-10;
01574 
01575    if (n < 0) {
01576       Error("TMath::BesselI", "*I* Invalid argument (n,x) = (%d, %g)\n",n,x);
01577       return 0;
01578    }
01579 
01580    if (n==0) return TMath::BesselI0(x);
01581    if (n==1) return TMath::BesselI1(x);
01582 
01583    if (x == 0) return 0;
01584    if (TMath::Abs(x) > kBigPositive) return 0;
01585 
01586    Double_t tox = 2/TMath::Abs(x);
01587    Double_t bip = 0, bim = 0;
01588    Double_t bi  = 1;
01589    Double_t result = 0;
01590    Int_t m = 2*((n+Int_t(sqrt(Float_t(iacc*n)))));
01591    for (Int_t j=m; j>=1; j--) {
01592       bim = bip+Double_t(j)*tox*bi;
01593       bip = bi;
01594       bi  = bim;
01595       // Renormalise to prevent overflows
01596       if (TMath::Abs(bi) > kBigPositive)  {
01597          result *= kBigNegative;
01598          bi     *= kBigNegative;
01599          bip    *= kBigNegative;
01600       }
01601       if (j==n) result=bip;
01602    }
01603 
01604    result *= TMath::BesselI0(x)/bi; // Normalise with BesselI0(x)
01605    if ((x < 0) && (n%2 == 1)) result = -result;
01606 
01607    return result;
01608 }
01609 
01610 //______________________________________________________________________________
01611 Double_t TMath::BesselJ0(Double_t x)
01612 {
01613    // Returns the Bessel function J0(x) for any real x.
01614 
01615    Double_t ax,z;
01616    Double_t xx,y,result,result1,result2;
01617    const Double_t p1  = 57568490574.0, p2  = -13362590354.0, p3 = 651619640.7;
01618    const Double_t p4  = -11214424.18,  p5  = 77392.33017,    p6 = -184.9052456;
01619    const Double_t p7  = 57568490411.0, p8  = 1029532985.0,   p9 = 9494680.718;
01620    const Double_t p10 = 59272.64853,   p11 = 267.8532712;
01621 
01622    const Double_t q1  = 0.785398164;
01623    const Double_t q2  = -0.1098628627e-2,  q3  = 0.2734510407e-4;
01624    const Double_t q4  = -0.2073370639e-5,  q5  = 0.2093887211e-6;
01625    const Double_t q6  = -0.1562499995e-1,  q7  = 0.1430488765e-3;
01626    const Double_t q8  = -0.6911147651e-5,  q9  = 0.7621095161e-6;
01627    const Double_t q10 =  0.934935152e-7,   q11 = 0.636619772;
01628 
01629    if ((ax=fabs(x)) < 8) {
01630       y=x*x;
01631       result1 = p1 + y*(p2 + y*(p3 + y*(p4  + y*(p5  + y*p6))));
01632       result2 = p7 + y*(p8 + y*(p9 + y*(p10 + y*(p11 + y))));
01633       result  = result1/result2;
01634    } else {
01635       z  = 8/ax;
01636       y  = z*z;
01637       xx = ax-q1;
01638       result1 = 1  + y*(q2 + y*(q3 + y*(q4 + y*q5)));
01639       result2 = q6 + y*(q7 + y*(q8 + y*(q9 - y*q10)));
01640       result  = sqrt(q11/ax)*(cos(xx)*result1-z*sin(xx)*result2);
01641    }
01642    return result;
01643 }
01644 
01645 //______________________________________________________________________________
01646 Double_t TMath::BesselJ1(Double_t x)
01647 {
01648    // Returns the Bessel function J1(x) for any real x.
01649 
01650    Double_t ax,z;
01651    Double_t xx,y,result,result1,result2;
01652    const Double_t p1  = 72362614232.0,  p2  = -7895059235.0, p3 = 242396853.1;
01653    const Double_t p4  = -2972611.439,   p5  = 15704.48260,   p6 = -30.16036606;
01654    const Double_t p7  = 144725228442.0, p8  = 2300535178.0,  p9 = 18583304.74;
01655    const Double_t p10 = 99447.43394,    p11 = 376.9991397;
01656 
01657    const Double_t q1  = 2.356194491;
01658    const Double_t q2  = 0.183105e-2,     q3  = -0.3516396496e-4;
01659    const Double_t q4  = 0.2457520174e-5, q5  = -0.240337019e-6;
01660    const Double_t q6  = 0.04687499995,   q7  = -0.2002690873e-3;
01661    const Double_t q8  = 0.8449199096e-5, q9  = -0.88228987e-6;
01662    const Double_t q10 = 0.105787412e-6,  q11 = 0.636619772;
01663 
01664    if ((ax=fabs(x)) < 8) {
01665       y=x*x;
01666       result1 = x*(p1 + y*(p2 + y*(p3 + y*(p4  + y*(p5  + y*p6)))));
01667       result2 = p7    + y*(p8 + y*(p9 + y*(p10 + y*(p11 + y))));
01668       result  = result1/result2;
01669    } else {
01670       z  = 8/ax;
01671       y  = z*z;
01672       xx = ax-q1;
01673       result1 = 1  + y*(q2 + y*(q3 + y*(q4 + y*q5)));
01674       result2 = q6 + y*(q7 + y*(q8 + y*(q9 + y*q10)));
01675       result  = sqrt(q11/ax)*(cos(xx)*result1-z*sin(xx)*result2);
01676       if (x < 0) result = -result;
01677    }
01678    return result;
01679 }
01680 
01681 //______________________________________________________________________________
01682 Double_t TMath::BesselY0(Double_t x)
01683 {
01684    // Returns the Bessel function Y0(x) for positive x.
01685 
01686    Double_t z,xx,y,result,result1,result2;
01687    const Double_t p1  = -2957821389.,  p2  = 7062834065.0, p3  = -512359803.6;
01688    const Double_t p4  = 10879881.29,   p5  = -86327.92757, p6  = 228.4622733;
01689    const Double_t p7  = 40076544269.,  p8  = 745249964.8,  p9  = 7189466.438;
01690    const Double_t p10 = 47447.26470,   p11 = 226.1030244,  p12 = 0.636619772;
01691 
01692    const Double_t q1  =  0.785398164;
01693    const Double_t q2  = -0.1098628627e-2, q3  = 0.2734510407e-4;
01694    const Double_t q4  = -0.2073370639e-5, q5  = 0.2093887211e-6;
01695    const Double_t q6  = -0.1562499995e-1, q7  = 0.1430488765e-3;
01696    const Double_t q8  = -0.6911147651e-5, q9  = 0.7621095161e-6;
01697    const Double_t q10 = -0.934945152e-7,  q11 = 0.636619772;
01698 
01699    if (x < 8) {
01700       y = x*x;
01701       result1 = p1 + y*(p2 + y*(p3 + y*(p4  + y*(p5  + y*p6))));
01702       result2 = p7 + y*(p8 + y*(p9 + y*(p10 + y*(p11 + y))));
01703       result  = (result1/result2) + p12*TMath::BesselJ0(x)*log(x);
01704    } else {
01705       z  = 8/x;
01706       y  = z*z;
01707       xx = x-q1;
01708       result1 = 1  + y*(q2 + y*(q3 + y*(q4 + y*q5)));
01709       result2 = q6 + y*(q7 + y*(q8 + y*(q9 + y*q10)));
01710       result  = sqrt(q11/x)*(sin(xx)*result1+z*cos(xx)*result2);
01711    }
01712    return result;
01713 }
01714 
01715 //______________________________________________________________________________
01716 Double_t TMath::BesselY1(Double_t x)
01717 {
01718    // Returns the Bessel function Y1(x) for positive x.
01719 
01720    Double_t z,xx,y,result,result1,result2;
01721    const Double_t p1  = -0.4900604943e13, p2  = 0.1275274390e13;
01722    const Double_t p3  = -0.5153438139e11, p4  = 0.7349264551e9;
01723    const Double_t p5  = -0.4237922726e7,  p6  = 0.8511937935e4;
01724    const Double_t p7  =  0.2499580570e14, p8  = 0.4244419664e12;
01725    const Double_t p9  =  0.3733650367e10, p10 = 0.2245904002e8;
01726    const Double_t p11 =  0.1020426050e6,  p12 = 0.3549632885e3;
01727    const Double_t p13 =  0.636619772;
01728    const Double_t q1  =  2.356194491;
01729    const Double_t q2  =  0.183105e-2,     q3  = -0.3516396496e-4;
01730    const Double_t q4  =  0.2457520174e-5, q5  = -0.240337019e-6;
01731    const Double_t q6  =  0.04687499995,   q7  = -0.2002690873e-3;
01732    const Double_t q8  =  0.8449199096e-5, q9  = -0.88228987e-6;
01733    const Double_t q10 =  0.105787412e-6,  q11 =  0.636619772;
01734 
01735    if (x < 8) {
01736       y=x*x;
01737       result1 = x*(p1 + y*(p2 + y*(p3 + y*(p4 + y*(p5  + y*p6)))));
01738       result2 = p7 + y*(p8 + y*(p9 + y*(p10 + y*(p11  + y*(p12+y)))));
01739       result  = (result1/result2) + p13*(TMath::BesselJ1(x)*log(x)-1/x);
01740    } else {
01741       z  = 8/x;
01742       y  = z*z;
01743       xx = x-q1;
01744       result1 = 1  + y*(q2 + y*(q3 + y*(q4 + y*q5)));
01745       result2 = q6 + y*(q7 + y*(q8 + y*(q9 + y*q10)));
01746       result  = sqrt(q11/x)*(sin(xx)*result1+z*cos(xx)*result2);
01747    }
01748    return result;
01749 }
01750 
01751 //______________________________________________________________________________
01752 Double_t TMath::StruveH0(Double_t x)
01753 {
01754    // Struve Functions of Order 0
01755    //
01756    // Converted from CERNLIB M342 by Rene Brun.
01757 
01758    const Int_t n1 = 15;
01759    const Int_t n2 = 25;
01760    const Double_t c1[16] = { 1.00215845609911981, -1.63969292681309147,
01761                              1.50236939618292819, -.72485115302121872,
01762                               .18955327371093136, -.03067052022988,
01763                               .00337561447375194, -2.6965014312602e-4,
01764                              1.637461692612e-5,   -7.8244408508e-7,
01765                              3.021593188e-8,      -9.6326645e-10,
01766                              2.579337e-11,        -5.8854e-13,
01767                              1.158e-14,           -2e-16 };
01768    const Double_t c2[26] = {  .99283727576423943, -.00696891281138625,
01769                              1.8205103787037e-4,  -1.063258252844e-5,
01770                              9.8198294287e-7,     -1.2250645445e-7,
01771                              1.894083312e-8,      -3.44358226e-9,
01772                              7.1119102e-10,       -1.6288744e-10,
01773                              4.065681e-11,        -1.091505e-11,
01774                              3.12005e-12,         -9.4202e-13,
01775                              2.9848e-13,          -9.872e-14,
01776                              3.394e-14,           -1.208e-14,
01777                              4.44e-15,            -1.68e-15,
01778                              6.5e-16,             -2.6e-16,
01779                              1.1e-16,             -4e-17,
01780                              2e-17,               -1e-17 };
01781 
01782    const Double_t c0  = 2/TMath::Pi();
01783 
01784    Int_t i;
01785    Double_t alfa, h, r, y, b0, b1, b2;
01786    Double_t v = TMath::Abs(x);
01787 
01788    v = TMath::Abs(x);
01789    if (v < 8) {
01790       y = v/8;
01791       h = 2*y*y -1;
01792       alfa = h + h;
01793       b0 = 0;
01794       b1 = 0;
01795       b2 = 0;
01796       for (i = n1; i >= 0; --i) {
01797          b0 = c1[i] + alfa*b1 - b2;
01798          b2 = b1;
01799          b1 = b0;
01800       }
01801       h = y*(b0 - h*b2);
01802    } else {
01803       r = 1/v;
01804       h = 128*r*r -1;
01805       alfa = h + h;
01806       b0 = 0;
01807       b1 = 0;
01808       b2 = 0;
01809       for (i = n2; i >= 0; --i) {
01810          b0 = c2[i] + alfa*b1 - b2;
01811          b2 = b1;
01812          b1 = b0;
01813       }
01814       h = TMath::BesselY0(v) + r*c0*(b0 - h*b2);
01815    }
01816    if (x < 0)  h = -h;
01817    return h;
01818 }
01819 
01820 //______________________________________________________________________________
01821 Double_t TMath::StruveH1(Double_t x)
01822 {
01823    // Struve Functions of Order 1
01824    //
01825    // Converted from CERNLIB M342 by Rene Brun.
01826 
01827    const Int_t n3 = 16;
01828    const Int_t n4 = 22;
01829    const Double_t c3[17] = { .5578891446481605,   -.11188325726569816,
01830                             -.16337958125200939,   .32256932072405902,
01831                             -.14581632367244242,   .03292677399374035,
01832                             -.00460372142093573,  4.434706163314e-4,
01833                             -3.142099529341e-5,   1.7123719938e-6,
01834                             -7.416987005e-8,      2.61837671e-9,
01835                             -7.685839e-11,        1.9067e-12,
01836                             -4.052e-14,           7.5e-16,
01837                             -1e-17 };
01838    const Double_t c4[23] = { 1.00757647293865641,  .00750316051248257,
01839                             -7.043933264519e-5,   2.66205393382e-6,
01840                             -1.8841157753e-7,     1.949014958e-8,
01841                             -2.6126199e-9,        4.236269e-10,
01842                             -7.955156e-11,        1.679973e-11,
01843                             -3.9072e-12,          9.8543e-13,
01844                             -2.6636e-13,          7.645e-14,
01845                             -2.313e-14,           7.33e-15,
01846                             -2.42e-15,            8.3e-16,
01847                             -3e-16,               1.1e-16,
01848                             -4e-17,               2e-17,-1e-17 };
01849 
01850    const Double_t c0  = 2/TMath::Pi();
01851    const Double_t cc  = 2/(3*TMath::Pi());
01852 
01853    Int_t i, i1;
01854    Double_t alfa, h, r, y, b0, b1, b2;
01855    Double_t v = TMath::Abs(x);
01856 
01857    if (v == 0) {
01858       h = 0;
01859    } else if (v <= 0.3) {
01860       y = v*v;
01861       r = 1;
01862       h = 1;
01863       i1 = (Int_t)(-8. / TMath::Log10(v));
01864       for (i = 1; i <= i1; ++i) {
01865          h = -h*y / ((2*i+ 1)*(2*i + 3));
01866          r += h;
01867       }
01868       h = cc*y*r;
01869    } else if (v < 8) {
01870       h = v*v/32 -1;
01871       alfa = h + h;
01872       b0 = 0;
01873       b1 = 0;
01874       b2 = 0;
01875       for (i = n3; i >= 0; --i) {
01876          b0 = c3[i] + alfa*b1 - b2;
01877          b2 = b1;
01878          b1 = b0;
01879       }
01880       h = b0 - h*b2;
01881    } else {
01882       h = 128/(v*v) -1;
01883       alfa = h + h;
01884       b0 = 0;
01885       b1 = 0;
01886       b2 = 0;
01887       for (i = n4; i >= 0; --i) {
01888          b0 = c4[i] + alfa*b1 - b2;
01889          b2 = b1;
01890          b1 = b0;
01891       }
01892       h = TMath::BesselY1(v) + c0*(b0 - h*b2);
01893    }
01894    return h;
01895 }
01896 
01897 
01898 //______________________________________________________________________________
01899 Double_t TMath::StruveL0(Double_t x)
01900 {
01901    // Modified Struve Function of Order 0.
01902    // By Kirill Filimonov.
01903 
01904    const Double_t pi=TMath::Pi();
01905 
01906    Double_t s=1.0;
01907    Double_t r=1.0;
01908 
01909    Double_t a0,sl0,a1,bi0;
01910 
01911    Int_t km,i;
01912 
01913    if (x<=20.) {
01914       a0=2.0*x/pi;
01915       for (i=1; i<=60;i++) {
01916          r*=(x/(2*i+1))*(x/(2*i+1));
01917          s+=r;
01918          if(TMath::Abs(r/s)<1.e-12) break;
01919       }
01920       sl0=a0*s;
01921    } else {
01922       km=int(5*(x+1.0));
01923       if(x>=50.0)km=25;
01924       for (i=1; i<=km; i++) {
01925          r*=(2*i-1)*(2*i-1)/x/x;
01926          s+=r;
01927          if(TMath::Abs(r/s)<1.0e-12) break;
01928       }
01929       a1=TMath::Exp(x)/TMath::Sqrt(2*pi*x);
01930       r=1.0;
01931       bi0=1.0;
01932       for (i=1; i<=16; i++) {
01933          r=0.125*r*(2.0*i-1.0)*(2.0*i-1.0)/(i*x);
01934          bi0+=r;
01935          if(TMath::Abs(r/bi0)<1.0e-12) break;
01936       }
01937 
01938       bi0=a1*bi0;
01939       sl0=-2.0/(pi*x)*s+bi0;
01940    }
01941    return sl0;
01942 }
01943 
01944 //______________________________________________________________________________
01945 Double_t TMath::StruveL1(Double_t x)
01946 {
01947    // Modified Struve Function of Order 1.
01948    // By Kirill Filimonov.
01949 
01950    const Double_t pi=TMath::Pi();
01951    Double_t a1,sl1,bi1,s;
01952    Double_t r=1.0;
01953    Int_t km,i;
01954 
01955    if (x<=20.) {
01956       s=0.0;
01957       for (i=1; i<=60;i++) {
01958          r*=x*x/(4.0*i*i-1.0);
01959          s+=r;
01960          if(TMath::Abs(r)<TMath::Abs(s)*1.e-12) break;
01961       }
01962       sl1=2.0/pi*s;
01963    } else {
01964       s=1.0;
01965       km=int(0.5*x);
01966       if(x>50.0)km=25;
01967       for (i=1; i<=km; i++) {
01968          r*=(2*i+3)*(2*i+1)/x/x;
01969          s+=r;
01970          if(TMath::Abs(r/s)<1.0e-12) break;
01971       }
01972       sl1=2.0/pi*(-1.0+1.0/(x*x)+3.0*s/(x*x*x*x));
01973       a1=TMath::Exp(x)/TMath::Sqrt(2*pi*x);
01974       r=1.0;
01975       bi1=1.0;
01976       for (i=1; i<=16; i++) {
01977          r=-0.125*r*(4.0-(2.0*i-1.0)*(2.0*i-1.0))/(i*x);
01978          bi1+=r;
01979          if(TMath::Abs(r/bi1)<1.0e-12) break;
01980       }
01981       sl1+=a1*bi1;
01982    }
01983    return sl1;
01984 }
01985 
01986 //______________________________________________________________________________
01987 Double_t TMath::Beta(Double_t p, Double_t q)
01988 {
01989    // Calculates Beta-function Gamma(p)*Gamma(q)/Gamma(p+q).
01990 
01991    return ::ROOT::Math::beta(p, q);
01992 }
01993 
01994 //______________________________________________________________________________
01995 Double_t TMath::BetaCf(Double_t x, Double_t a, Double_t b)
01996 {
01997    // Continued fraction evaluation by modified Lentz's method
01998    // used in calculation of incomplete Beta function.
01999 
02000    Int_t itmax = 500;
02001    Double_t eps = 3.e-14;
02002    Double_t fpmin = 1.e-30;
02003 
02004    Int_t m, m2;
02005    Double_t aa, c, d, del, qab, qam, qap;
02006    Double_t h;
02007    qab = a+b;
02008    qap = a+1.0;
02009    qam = a-1.0;
02010    c = 1.0;
02011    d = 1.0 - qab*x/qap;
02012    if (TMath::Abs(d)<fpmin) d=fpmin;
02013    d=1.0/d;
02014    h=d;
02015    for (m=1; m<=itmax; m++) {
02016       m2=m*2;
02017       aa = m*(b-m)*x/((qam+ m2)*(a+m2));
02018       d = 1.0 +aa*d;
02019       if(TMath::Abs(d)<fpmin) d = fpmin;
02020       c = 1 +aa/c;
02021       if (TMath::Abs(c)<fpmin) c = fpmin;
02022       d=1.0/d;
02023       h*=d*c;
02024       aa = -(a+m)*(qab +m)*x/((a+m2)*(qap+m2));
02025       d=1.0+aa*d;
02026       if(TMath::Abs(d)<fpmin) d = fpmin;
02027       c = 1.0 +aa/c;
02028       if (TMath::Abs(c)<fpmin) c = fpmin;
02029       d=1.0/d;
02030       del = d*c;
02031       h*=del;
02032       if (TMath::Abs(del-1)<=eps) break;
02033    }
02034    if (m>itmax) {
02035       Info("TMath::BetaCf", "a or b too big, or itmax too small, a=%g, b=%g, x=%g, h=%g, itmax=%d",
02036            a,b,x,h,itmax);
02037    }
02038    return h;
02039 }
02040 
02041 //______________________________________________________________________________
02042 Double_t TMath::BetaDist(Double_t x, Double_t p, Double_t q)
02043 {
02044    // Computes the probability density function of the Beta distribution
02045    // (the distribution function is computed in BetaDistI).
02046    // The first argument is the point, where the function will be
02047    // computed, second and third are the function parameters.
02048    // Since the Beta distribution is bounded on both sides, it's often
02049    // used to represent processes with natural lower and upper limits.
02050 
02051    if ((x<0) || (x>1) || (p<=0) || (q<=0)){
02052       Error("TMath::BetaDist", "parameter value outside allowed range");
02053       return 0;
02054    }
02055    Double_t beta = TMath::Beta(p, q);
02056    Double_t r = TMath::Power(x, p-1)*TMath::Power(1-x, q-1)/beta;
02057    return r;
02058 }
02059 
02060 //______________________________________________________________________________
02061 Double_t TMath::BetaDistI(Double_t x, Double_t p, Double_t q)
02062 {
02063    // Computes the distribution function of the Beta distribution.
02064    // The first argument is the point, where the function will be
02065    // computed, second and third are the function parameters.
02066    // Since the Beta distribution is bounded on both sides, it's often
02067    // used to represent processes with natural lower and upper limits.
02068 
02069    if ((x<0) || (x>1) || (p<=0) || (q<=0)){
02070       Error("TMath::BetaDistI", "parameter value outside allowed range");
02071       return 0;
02072    }
02073    Double_t betai = TMath::BetaIncomplete(x, p, q);
02074    return betai;
02075 }
02076 
02077 //______________________________________________________________________________
02078 Double_t TMath::BetaIncomplete(Double_t x, Double_t a, Double_t b)
02079 {
02080    // Calculates the incomplete Beta-function.
02081 
02082    return ::ROOT::Math::inc_beta(x, a, b);
02083 }
02084 
02085 //______________________________________________________________________________
02086 Double_t TMath::Binomial(Int_t n,Int_t k)
02087 {
02088    // Calculate the binomial coefficient n over k.
02089 
02090    if (k==0 || n==k) return 1;
02091    if (n<=0 || k<0 || n<k) return 0;
02092 
02093    Int_t k1=TMath::Min(k,n-k);
02094    Int_t k2=n-k1;
02095    Double_t fact=k2+1;
02096    for (Double_t i=k1;i>1.;--i)
02097       fact *= (k2+i)/i;
02098    return fact;
02099 }
02100 
02101 //______________________________________________________________________________
02102 Double_t TMath::BinomialI(Double_t p, Int_t n, Int_t k)
02103 {
02104    // Suppose an event occurs with probability _p_ per trial
02105    // Then the probability P of its occuring _k_ or more times
02106    // in _n_ trials is termed a cumulative binomial probability
02107    // the formula is P = sum_from_j=k_to_n(TMath::Binomial(n, j)*
02108    // *TMath::Power(p, j)*TMath::Power(1-p, n-j)
02109    // For _n_ larger than 12 BetaIncomplete is a much better way
02110    // to evaluate the sum than would be the straightforward sum calculation
02111    // for _n_ smaller than 12 either method is acceptable
02112    // ("Numerical Recipes")
02113    //     --implementation by Anna Kreshuk
02114 
02115    if(k <= 0) return 1.0;
02116    if(k > n) return 0.0;
02117    if(k == n) return TMath::Power(p, n);
02118 
02119    return BetaIncomplete(p, Double_t(k), Double_t(n-k+1));
02120 }
02121 
02122 //______________________________________________________________________________
02123 Double_t TMath::CauchyDist(Double_t x, Double_t t, Double_t s)
02124 {
02125    // Computes the density of Cauchy distribution at point x
02126    // by default, standard Cauchy distribution is used (t=0, s=1)
02127    //    t is the location parameter
02128    //    s is the scale parameter
02129    // The Cauchy distribution, also called Lorentzian distribution,
02130    // is a continuous distribution describing resonance behavior
02131    // The mean and standard deviation of the Cauchy distribution are undefined.
02132    // The practical meaning of this is that collecting 1,000 data points gives
02133    // no more accurate an estimate of the mean and standard deviation than
02134    // does a single point.
02135    // The formula was taken from "Engineering Statistics Handbook" on site
02136    // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3663.htm
02137    // Implementation by Anna Kreshuk.
02138    // Example:
02139    //    TF1* fc = new TF1("fc", "TMath::CauchyDist(x, [0], [1])", -5, 5);
02140    //    fc->SetParameters(0, 1);
02141    //    fc->Draw();
02142 
02143    Double_t temp= (x-t)*(x-t)/(s*s);
02144    Double_t result = 1/(s*TMath::Pi()*(1+temp));
02145    return result;
02146 }
02147 
02148 //______________________________________________________________________________
02149 Double_t TMath::ChisquareQuantile(Double_t p, Double_t ndf)
02150 {
02151    // Evaluate the quantiles of the chi-squared probability distribution function.
02152    // Algorithm AS 91   Appl. Statist. (1975) Vol.24, P.35
02153    // implemented by Anna Kreshuk.
02154    // Incorporates the suggested changes in AS R85 (vol.40(1), pp.233-5, 1991)
02155    // Parameters:
02156    //   p   - the probability value, at which the quantile is computed
02157    //   ndf - number of degrees of freedom
02158 
02159    Double_t c[]={0, 0.01, 0.222222, 0.32, 0.4, 1.24, 2.2,
02160                  4.67, 6.66, 6.73, 13.32, 60.0, 70.0,
02161                  84.0, 105.0, 120.0, 127.0, 140.0, 175.0,
02162                  210.0, 252.0, 264.0, 294.0, 346.0, 420.0,
02163                  462.0, 606.0, 672.0, 707.0, 735.0, 889.0,
02164                  932.0, 966.0, 1141.0, 1182.0, 1278.0, 1740.0,
02165                  2520.0, 5040.0};
02166    Double_t e = 5e-7;
02167    Double_t aa = 0.6931471806;
02168    Int_t maxit = 20;
02169    Double_t ch, p1, p2, q, t, a, b, x;
02170    Double_t s1, s2, s3, s4, s5, s6;
02171 
02172    if (ndf <= 0) return 0;
02173 
02174    Double_t g = TMath::LnGamma(0.5*ndf);
02175 
02176    Double_t xx = 0.5 * ndf;
02177    Double_t cp = xx - 1;
02178    if (ndf >= TMath::Log(p)*(-c[5])){
02179    //starting approximation for ndf less than or equal to 0.32
02180       if (ndf > c[3]) {
02181          x = TMath::NormQuantile(p);
02182          //starting approximation using Wilson and Hilferty estimate
02183          p1=c[2]/ndf;
02184          ch = ndf*TMath::Power((x*TMath::Sqrt(p1) + 1 - p1), 3);
02185          if (ch > c[6]*ndf + 6)
02186             ch = -2 * (TMath::Log(1-p) - cp * TMath::Log(0.5 * ch) + g);
02187       } else {
02188          ch = c[4];
02189          a = TMath::Log(1-p);
02190          do{
02191             q = ch;
02192             p1 = 1 + ch * (c[7]+ch);
02193             p2 = ch * (c[9] + ch * (c[8] + ch));
02194             t = -0.5 + (c[7] + 2 * ch) / p1 - (c[9] + ch * (c[10] + 3 * ch)) / p2;
02195             ch = ch - (1 - TMath::Exp(a + g + 0.5 * ch + cp * aa) *p2 / p1) / t;
02196          }while (TMath::Abs(q/ch - 1) > c[1]);
02197       }
02198    } else {
02199       ch = TMath::Power((p * xx * TMath::Exp(g + xx * aa)),(1./xx));
02200       if (ch < e) return ch;
02201    }
02202 //call to algorithm AS 239 and calculation of seven term  Taylor series
02203    for (Int_t i=0; i<maxit; i++){
02204       q = ch;
02205       p1 = 0.5 * ch;
02206       p2 = p - TMath::Gamma(xx, p1);
02207 
02208       t = p2 * TMath::Exp(xx * aa + g + p1 - cp * TMath::Log(ch));
02209       b = t / ch;
02210       a = 0.5 * t - b * cp;
02211       s1 = (c[19] + a * (c[17] + a * (c[14] + a * (c[13] + a * (c[12] +c[11] * a))))) / c[24];
02212       s2 = (c[24] + a * (c[29] + a * (c[32] + a * (c[33] + c[35] * a)))) / c[37];
02213       s3 = (c[19] + a * (c[25] + a * (c[28] + c[31] * a))) / c[37];
02214       s4 = (c[20] + a * (c[27] + c[34] * a) + cp * (c[22] + a * (c[30] + c[36] * a))) / c[38];
02215       s5 = (c[13] + c[21] * a + cp * (c[18] + c[26] * a)) / c[37];
02216       s6 = (c[15] + cp * (c[23] + c[16] * cp)) / c[38];
02217       ch = ch + t * (1 + 0.5 * t * s1 - b * cp * (s1 - b * (s2 - b * (s3 - b * (s4 - b * (s5 - b * s6))))));
02218       if (TMath::Abs(q / ch - 1) > e) break;
02219    }
02220    return ch;
02221 }
02222 
02223 //______________________________________________________________________________
02224 Double_t TMath::FDist(Double_t F, Double_t N, Double_t M)
02225 {
02226    // Computes the density function of F-distribution
02227    // (probability function, integral of density, is computed in FDistI).
02228    //
02229    // Parameters N and M stand for degrees of freedom of chi-squares
02230    // mentioned above parameter F is the actual variable x of the
02231    // density function p(x) and the point at which the density function
02232    // is calculated.
02233    //
02234    // About F distribution:
02235    // F-distribution arises in testing whether two random samples
02236    // have the same variance. It is the ratio of two chi-square
02237    // distributions, with N and M degrees of freedom respectively,
02238    // where each chi-square is first divided by it's number of degrees
02239    // of freedom.
02240    // Implementation by Anna Kreshuk.
02241    
02242    return ::ROOT::Math::fdistribution_pdf(F,N,M);
02243 }
02244 
02245 //______________________________________________________________________________
02246 Double_t TMath::FDistI(Double_t F, Double_t N, Double_t M)
02247 {
02248    // Calculates the cumulative distribution function of F-distribution,
02249    // this function occurs in the statistical test of whether two observed
02250    // samples have the same variance. For this test a certain statistic F,
02251    // the ratio of observed dispersion of the first sample to that of the
02252    // second sample, is calculated. N and M stand for numbers of degrees
02253    // of freedom in the samples 1-FDistI() is the significance level at
02254    // which the hypothesis "1 has smaller variance than 2" can be rejected.
02255    // A small numerical value of 1 - FDistI() implies a very significant
02256    // rejection, in turn implying high confidence in the hypothesis
02257    // "1 has variance greater than 2".
02258    // Implementation by Anna Kreshuk.
02259 
02260    Double_t fi = ::ROOT::Math::fdistribution_cdf(F,N,M);
02261    return fi;
02262 }
02263 
02264 //______________________________________________________________________________
02265 Double_t TMath::GammaDist(Double_t x, Double_t gamma, Double_t mu, Double_t beta)
02266 {
02267    // Computes the density function of Gamma distribution at point x.
02268    //   gamma - shape parameter
02269    //   mu    - location parameter
02270    //   beta  - scale parameter
02271    //
02272    // The definition can be found in "Engineering Statistics Handbook" on site
02273    // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366b.htm
02274    // use now implementation in ROOT::Math::gamma_pdf 
02275    //Begin_Html
02276    /*
02277    <img src="gif/gammadist.gif">
02278    */
02279    //End_Html
02280 
02281    if ((x<mu) || (gamma<=0) || (beta <=0)) {
02282       Error("TMath::GammaDist", "illegal parameter values");
02283       return 0;
02284    }
02285    return ::ROOT::Math::gamma_pdf(x, gamma, beta, mu); 
02286 }
02287 
02288 //______________________________________________________________________________
02289 Double_t TMath::LaplaceDist(Double_t x, Double_t alpha, Double_t beta)
02290 {
02291    // Computes the probability density function of Laplace distribution
02292    // at point x, with location parameter alpha and shape parameter beta.
02293    // By default, alpha=0, beta=1
02294    // This distribution is known under different names, most common is
02295    // double exponential distribution, but it also appears as
02296    // the two-tailed exponential or the bilateral exponential distribution
02297    Double_t temp;
02298    temp  = TMath::Exp(-TMath::Abs((x-alpha)/beta));
02299    temp /= (2.*beta);
02300    return temp;
02301 }
02302 
02303 //______________________________________________________________________________
02304 Double_t TMath::LaplaceDistI(Double_t x, Double_t alpha, Double_t beta)
02305 {
02306    // Computes the distribution function of Laplace distribution
02307    // at point x, with location parameter alpha and shape parameter beta.
02308    // By default, alpha=0, beta=1
02309    // This distribution is known under different names, most common is
02310    // double exponential distribution, but it also appears as
02311    // the two-tailed exponential or the bilateral exponential distribution
02312 
02313    Double_t temp;
02314    if (x<=alpha){
02315       temp = 0.5*TMath::Exp(-TMath::Abs((x-alpha)/beta));
02316    } else {
02317       temp = 1-0.5*TMath::Exp(-TMath::Abs((x-alpha)/beta));
02318    }
02319    return temp;
02320 }
02321 
02322 //______________________________________________________________________________
02323 Double_t TMath::LogNormal(Double_t x, Double_t sigma, Double_t theta, Double_t m)
02324 {
02325    // Computes the density of LogNormal distribution at point x.
02326    // Variable X has lognormal distribution if Y=Ln(X) has normal distribution
02327    // sigma is the shape parameter
02328    // theta is the location parameter
02329    // m is the scale parameter
02330    // The formula was taken from "Engineering Statistics Handbook" on site
02331    // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3669.htm
02332    // Implementation using ROOT::Math::lognormal_pdf
02333    //Begin_Html
02334    /*
02335    <img src="gif/lognormal.gif">
02336    */
02337    //End_Html
02338 
02339    if ((x<theta) || (sigma<=0) || (m<=0)) {
02340       Error("TMath::Lognormal", "illegal parameter values");
02341       return 0;
02342    }
02343    // lognormal_pdf uses same definition of http://en.wikipedia.org/wiki/Log-normal_distribution
02344    // where mu = log(m)
02345    
02346    return ::ROOT::Math::lognormal_pdf(x, TMath::Log(m), sigma, theta); 
02347 
02348 }
02349 
02350 //______________________________________________________________________________
02351 Double_t TMath::NormQuantile(Double_t p)
02352 {
02353    // Computes quantiles for standard normal distribution N(0, 1)
02354    // at probability p
02355    // ALGORITHM AS241  APPL. STATIST. (1988) VOL. 37, NO. 3, 477-484.
02356 
02357    if ((p<=0)||(p>=1)) {
02358       Error("TMath::NormQuantile", "probability outside (0, 1)");
02359       return 0;
02360    }
02361 
02362    Double_t  a0 = 3.3871328727963666080e0;
02363    Double_t  a1 = 1.3314166789178437745e+2;
02364    Double_t  a2 = 1.9715909503065514427e+3;
02365    Double_t  a3 = 1.3731693765509461125e+4;
02366    Double_t  a4 = 4.5921953931549871457e+4;
02367    Double_t  a5 = 6.7265770927008700853e+4;
02368    Double_t  a6 = 3.3430575583588128105e+4;
02369    Double_t  a7 = 2.5090809287301226727e+3;
02370    Double_t  b1 = 4.2313330701600911252e+1;
02371    Double_t  b2 = 6.8718700749205790830e+2;
02372    Double_t  b3 = 5.3941960214247511077e+3;
02373    Double_t  b4 = 2.1213794301586595867e+4;
02374    Double_t  b5 = 3.9307895800092710610e+4;
02375    Double_t  b6 = 2.8729085735721942674e+4;
02376    Double_t  b7 = 5.2264952788528545610e+3;
02377    Double_t  c0 = 1.42343711074968357734e0;
02378    Double_t  c1 = 4.63033784615654529590e0;
02379    Double_t  c2 = 5.76949722146069140550e0;
02380    Double_t  c3 = 3.64784832476320460504e0;
02381    Double_t  c4 = 1.27045825245236838258e0;
02382    Double_t  c5 = 2.41780725177450611770e-1;
02383    Double_t  c6 = 2.27238449892691845833e-2;
02384    Double_t  c7 = 7.74545014278341407640e-4;
02385    Double_t  d1 = 2.05319162663775882187e0;
02386    Double_t  d2 = 1.67638483018380384940e0;
02387    Double_t  d3 = 6.89767334985100004550e-1;
02388    Double_t  d4 = 1.48103976427480074590e-1;
02389    Double_t  d5 = 1.51986665636164571966e-2;
02390    Double_t  d6 = 5.47593808499534494600e-4;
02391    Double_t  d7 = 1.05075007164441684324e-9;
02392    Double_t  e0 = 6.65790464350110377720e0;
02393    Double_t  e1 = 5.46378491116411436990e0;
02394    Double_t  e2 = 1.78482653991729133580e0;
02395    Double_t  e3 = 2.96560571828504891230e-1;
02396    Double_t  e4 = 2.65321895265761230930e-2;
02397    Double_t  e5 = 1.24266094738807843860e-3;
02398    Double_t  e6 = 2.71155556874348757815e-5;
02399    Double_t  e7 = 2.01033439929228813265e-7;
02400    Double_t  f1 = 5.99832206555887937690e-1;
02401    Double_t  f2 = 1.36929880922735805310e-1;
02402    Double_t  f3 = 1.48753612908506148525e-2;
02403    Double_t  f4 = 7.86869131145613259100e-4;
02404    Double_t  f5 = 1.84631831751005468180e-5;
02405    Double_t  f6 = 1.42151175831644588870e-7;
02406    Double_t  f7 = 2.04426310338993978564e-15;
02407 
02408    Double_t split1 = 0.425;
02409    Double_t split2=5.;
02410    Double_t konst1=0.180625;
02411    Double_t konst2=1.6;
02412 
02413    Double_t q, r, quantile;
02414    q=p-0.5;
02415    if (TMath::Abs(q)<split1) {
02416       r=konst1-q*q;
02417       quantile = q* (((((((a7 * r + a6) * r + a5) * r + a4) * r + a3)
02418                  * r + a2) * r + a1) * r + a0) /
02419                  (((((((b7 * r + b6) * r + b5) * r + b4) * r + b3)
02420                  * r + b2) * r + b1) * r + 1.);
02421    } else {
02422       if(q<0) r=p;
02423       else    r=1-p;
02424       //error case
02425       if (r<=0)
02426          quantile=0;
02427       else {
02428          r=TMath::Sqrt(-TMath::Log(r));
02429          if (r<=split2) {
02430             r=r-konst2;
02431             quantile=(((((((c7 * r + c6) * r + c5) * r + c4) * r + c3)
02432                      * r + c2) * r + c1) * r + c0) /
02433                      (((((((d7 * r + d6) * r + d5) * r + d4) * r + d3)
02434                      * r + d2) * r + d1) * r + 1);
02435          } else{
02436             r=r-split2;
02437             quantile=(((((((e7 * r + e6) * r + e5) * r + e4) * r + e3)
02438                      * r + e2) * r + e1) * r + e0) /
02439                      (((((((f7 * r + f6) * r + f5) * r + f4) * r + f3)
02440                      * r + f2) * r + f1) * r + 1);
02441          }
02442          if (q<0) quantile=-quantile;
02443       }
02444    }
02445    return quantile;
02446 }
02447 
02448 //______________________________________________________________________________
02449 Bool_t TMath::Permute(Int_t n, Int_t *a)
02450 {
02451    // Simple recursive algorithm to find the permutations of
02452    // n natural numbers, not necessarily all distinct
02453    // adapted from CERNLIB routine PERMU.
02454    // The input array has to be initialised with a non descending
02455    // sequence. The method returns kFALSE when
02456    // all combinations are exhausted.
02457 
02458    Int_t i,itmp;
02459    Int_t i1=-1;
02460 
02461    // find rightmost upward transition
02462    for(i=n-2; i>-1; i--) {
02463       if(a[i]<a[i+1]) {
02464          i1=i;
02465          break;
02466       }
02467    }
02468    // no more upward transitions, end of the story
02469    if(i1==-1) return kFALSE;
02470    else {
02471       // find lower right element higher than the lower
02472       // element of the upward transition
02473       for(i=n-1;i>i1;i--) {
02474          if(a[i] > a[i1]) {
02475             // swap the two
02476             itmp=a[i1];
02477             a[i1]=a[i];
02478             a[i]=itmp;
02479             break;
02480          }
02481       }
02482       // order the rest, in fact just invert, as there
02483       // are only downward transitions from here on
02484       for(i=0;i<(n-i1-1)/2;i++) {
02485          itmp=a[i1+i+1];
02486          a[i1+i+1]=a[n-i-1];
02487          a[n-i-1]=itmp;
02488       }
02489    }
02490    return kTRUE;
02491 }
02492 
02493 //______________________________________________________________________________
02494 Double_t TMath::Student(Double_t T, Double_t ndf)
02495 {
02496    // Computes density function for Student's t- distribution
02497    // (the probability function (integral of density) is computed in StudentI).
02498    //
02499    // First parameter stands for x - the actual variable of the
02500    // density function p(x) and the point at which the density is calculated.
02501    // Second parameter stands for number of degrees of freedom.
02502    //
02503    // About Student distribution:
02504    // Student's t-distribution is used for many significance tests, for example,
02505    // for the Student's t-tests for the statistical significance of difference
02506    // between two sample means and for confidence intervals for the difference
02507    // between two population means.
02508    //
02509    // Example: suppose we have a random sample of size n drawn from normal
02510    // distribution with mean Mu and st.deviation Sigma. Then the variable
02511    //
02512    //   t = (sample_mean - Mu)/(sample_deviation / sqrt(n))
02513    //
02514    // has Student's t-distribution with n-1 degrees of freedom.
02515    //
02516    // NOTE that this function's second argument is number of degrees of freedom,
02517    // not the sample size.
02518    //
02519    // As the number of degrees of freedom grows, t-distribution approaches
02520    // Normal(0,1) distribution.
02521    // Implementation by Anna Kreshuk.
02522 
02523    if (ndf < 1) {
02524       return 0;
02525    }
02526 
02527    Double_t r   = ndf;
02528    Double_t rh  = 0.5*r;
02529    Double_t rh1 = rh + 0.5;
02530    Double_t denom = TMath::Sqrt(r*TMath::Pi())*TMath::Gamma(rh)*TMath::Power(1+T*T/r, rh1);
02531    return TMath::Gamma(rh1)/denom;
02532 }
02533 
02534 //______________________________________________________________________________
02535 Double_t TMath::StudentI(Double_t T, Double_t ndf)
02536 {
02537    // Calculates the cumulative distribution function of Student's
02538    // t-distribution second parameter stands for number of degrees of freedom,
02539    // not for the number of samples
02540    // if x has Student's t-distribution, the function returns the probability of
02541    // x being less than T.
02542    // Implementation by Anna Kreshuk.
02543 
02544    Double_t r = ndf;
02545 
02546    Double_t si = (T>0) ?
02547                  (1 - 0.5*BetaIncomplete((r/(r + T*T)), r*0.5, 0.5)) :
02548                  0.5*BetaIncomplete((r/(r + T*T)), r*0.5, 0.5);
02549    return si;
02550 }
02551 
02552 //______________________________________________________________________________
02553 Double_t TMath::StudentQuantile(Double_t p, Double_t ndf, Bool_t lower_tail)
02554 {
02555    // Computes quantiles of the Student's t-distribution
02556    // 1st argument is the probability, at which the quantile is computed
02557    // 2nd argument - the number of degrees of freedom of the
02558    // Student distribution
02559    // When the 3rd argument lower_tail is kTRUE (default)-
02560    // the algorithm returns such x0, that
02561    //   P(x < x0)=p
02562    // upper tail (lower_tail is kFALSE)- the algorithm returns such x0, that
02563    //   P(x > x0)=p
02564    // the algorithm was taken from
02565    //   G.W.Hill, "Algorithm 396, Student's t-quantiles"
02566    //             "Communications of the ACM", 13(10), October 1970
02567 
02568    Double_t quantile;
02569    Double_t temp;
02570    Bool_t neg;
02571    Double_t q;
02572    if (ndf<1 || p>=1 || p<=0) {
02573       Error("TMath::StudentQuantile", "illegal parameter values");
02574       return 0;
02575    }
02576    if ((lower_tail && p>0.5)||(!lower_tail && p<0.5)){
02577       neg=kFALSE;
02578       q=2*(lower_tail ? (1-p) : p);
02579    } else {
02580       neg=kTRUE;
02581       q=2*(lower_tail? p : (1-p));
02582    }
02583 
02584    if ((ndf-1)<1e-8) {
02585       temp=TMath::PiOver2()*q;
02586       quantile = TMath::Cos(temp)/TMath::Sin(temp);
02587    } else {
02588       if ((ndf-2)<1e-8) {
02589          quantile = TMath::Sqrt(2./(q*(2-q))-2);
02590       } else {
02591          Double_t a=1./(ndf-0.5);
02592          Double_t b=48./(a*a);
02593          Double_t c=((20700*a/b -98)*a-16)*a+96.36;
02594          Double_t d=((94.5/(b+c)-3.)/b+1)*TMath::Sqrt(a*TMath::PiOver2())*ndf;
02595          Double_t x=q*d;
02596          Double_t y=TMath::Power(x, (2./ndf));
02597          if (y>0.05+a){
02598             //asymptotic inverse expansion about normal
02599             x=TMath::NormQuantile(q*0.5);
02600             y=x*x;
02601             if (ndf<5) c+=0.3*(ndf-4.5)*(x+0.6);
02602             c+=(((0.05*d*x-5.)*x-7.)*x-2.)*x +b;
02603             y=(((((0.4*y+6.3)*y+36.)*y+94.5)/c - y-3.)/b+1)*x;
02604             y=a*y*y;
02605             if(y>0.002) y  = TMath::Exp(y)-1;
02606             else        y += 0.5*y*y;
02607          } else {
02608             y=((1./(((ndf+6.)/(ndf*y)-0.089*d-0.822)*(ndf+2.)*3)+0.5/(ndf+4.))*y-1.)*
02609               (ndf+1.)/(ndf+2.)+1/y;
02610          }
02611          quantile = TMath::Sqrt(ndf*y);
02612       }
02613    }
02614    if(neg) quantile=-quantile;
02615    return quantile;
02616 }
02617 
02618 //______________________________________________________________________________
02619 Double_t TMath::Vavilov(Double_t x, Double_t kappa, Double_t beta2)
02620 {
02621    //Returns the value of the Vavilov density function
02622    //Parameters: 1st - the point were the density function is evaluated
02623    //            2nd - value of kappa (distribution parameter)
02624    //            3rd - value of beta2 (distribution parameter)
02625    //The algorithm was taken from the CernLib function vavden(G115)
02626    //Reference: A.Rotondi and P.Montagna, Fast Calculation of Vavilov distribution
02627    //Nucl.Instr. and Meth. B47(1990), 215-224
02628    //Accuracy: quote from the reference above:
02629    //"The resuls of our code have been compared with the values of the Vavilov
02630    //density function computed numerically in an accurate way: our approximation
02631    //shows a difference of less than 3% around the peak of the density function, slowly
02632    //increasing going towards the extreme tails to the right and to the left"
02633 //Begin_Html
02634 /*
02635 <img src="gif/Vavilov.gif">
02636 */
02637 //End_Html
02638 
02639    Double_t *ac = new Double_t[14];
02640    Double_t *hc = new Double_t[9];
02641 
02642    Int_t itype;
02643    Int_t npt;
02644    TMath::VavilovSet(kappa, beta2, 0, 0, ac, hc, itype, npt);
02645    Double_t v =  TMath::VavilovDenEval(x, ac, hc, itype);
02646    delete [] ac;
02647    delete [] hc;
02648    return v;
02649 }
02650 
02651 //______________________________________________________________________________
02652 Double_t TMath::VavilovI(Double_t x, Double_t kappa, Double_t beta2)
02653 {
02654    //Returns the value of the Vavilov distribution function
02655    //Parameters: 1st - the point were the density function is evaluated
02656    //            2nd - value of kappa (distribution parameter)
02657    //            3rd - value of beta2 (distribution parameter)
02658    //The algorithm was taken from the CernLib function vavden(G115)
02659    //Reference: A.Rotondi and P.Montagna, Fast Calculation of Vavilov distribution
02660    //Nucl.Instr. and Meth. B47(1990), 215-224
02661    //Accuracy: quote from the reference above:
02662    //"The resuls of our code have been compared with the values of the Vavilov
02663    //density function computed numerically in an accurate way: our approximation
02664    //shows a difference of less than 3% around the peak of the density function, slowly
02665    //increasing going towards the extreme tails to the right and to the left"
02666 
02667    Double_t *ac = new Double_t[14];
02668    Double_t *hc = new Double_t[9];
02669    Double_t *wcm = new Double_t[201];
02670    Int_t itype;
02671    Int_t npt;
02672    Int_t k;
02673    Double_t xx, v;
02674    TMath::VavilovSet(kappa, beta2, 1, wcm, ac, hc, itype, npt);
02675    if (x < ac[0]) v = 0;
02676    else if (x >=ac[8]) v = 1;
02677    else {
02678       xx = x - ac[0];
02679       k = Int_t(xx*ac[10]);
02680       v = TMath::Min(wcm[k] + (xx - k*ac[9])*(wcm[k+1]-wcm[k])*ac[10], 1.);
02681    }
02682    delete [] ac;
02683    delete [] hc;
02684    delete [] wcm;
02685    return v;
02686 }
02687 
02688 //______________________________________________________________________________
02689 Double_t TMath::LandauI(Double_t x)
02690 {
02691    //Returns the value of the Landau distribution function at point x.
02692    //The algorithm was taken from the Cernlib function dislan(G110)
02693    //Reference: K.S.Kolbig and B.Schorr, "A program package for the Landau
02694    //distribution", Computer Phys.Comm., 31(1984), 97-111
02695    
02696    return ::ROOT::Math::landau_cdf(x);
02697 }
02698 
02699 
02700 //______________________________________________________________________________
02701 void TMath::VavilovSet(Double_t rkappa, Double_t beta2, Bool_t mode, Double_t *WCM, Double_t *AC, Double_t *HC, Int_t &itype, Int_t &npt)
02702 {
02703    //Internal function, called by Vavilov and VavilovI
02704 
02705 
02706    Double_t BKMNX1 = 0.02, BKMNY1 = 0.05, BKMNX2 = 0.12, BKMNY2 = 0.05,
02707       BKMNX3 = 0.22, BKMNY3 = 0.05, BKMXX1 = 0.1 , BKMXY1 = 1,
02708       BKMXX2 = 0.2 , BKMXY2 = 1   , BKMXX3 = 0.3 , BKMXY3 = 1;
02709 
02710    Double_t FBKX1 = 2/(BKMXX1-BKMNX1), FBKX2 = 2/(BKMXX2-BKMNX2),
02711       FBKX3 = 2/(BKMXX3-BKMNX3), FBKY1 = 2/(BKMXY1-BKMNY1),
02712       FBKY2 = 2/(BKMXY2-BKMNY2), FBKY3 = 2/(BKMXY3-BKMNY3);
02713 
02714    Double_t FNINV[] = {0, 1, 0.5, 0.33333333, 0.25, 0.2};
02715 
02716    Double_t EDGEC[]= {0, 0, 0.16666667e+0, 0.41666667e-1, 0.83333333e-2,
02717                       0.13888889e-1, 0.69444444e-2, 0.77160493e-3};
02718 
02719    Double_t U1[] = {0, 0.25850868e+0,  0.32477982e-1, -0.59020496e-2,
02720                     0.            , 0.24880692e-1,  0.47404356e-2,
02721                     -0.74445130e-3,  0.73225731e-2,  0.           ,
02722                     0.11668284e-2,  0.           , -0.15727318e-2,-0.11210142e-2};
02723 
02724    Double_t U2[] = {0, 0.43142611e+0,  0.40797543e-1, -0.91490215e-2,
02725                     0.           ,  0.42127077e-1,  0.73167928e-2,
02726                     -0.14026047e-2,  0.16195241e-1,  0.24714789e-2,
02727                     0.20751278e-2,  0.           , -0.25141668e-2,-0.14064022e-2};
02728 
02729    Double_t U3[] = {0,  0.25225955e+0,  0.64820468e-1, -0.23615759e-1,
02730                     0.           ,  0.23834176e-1,  0.21624675e-2,
02731                     -0.26865597e-2, -0.54891384e-2,  0.39800522e-2,
02732                     0.48447456e-2, -0.89439554e-2, -0.62756944e-2,-0.24655436e-2};
02733 
02734    Double_t U4[] = {0, 0.12593231e+1, -0.20374501e+0,  0.95055662e-1,
02735                     -0.20771531e-1, -0.46865180e-1, -0.77222986e-2,
02736                     0.32241039e-2,  0.89882920e-2, -0.67167236e-2,
02737                     -0.13049241e-1,  0.18786468e-1,  0.14484097e-1};
02738 
02739    Double_t U5[] = {0, -0.24864376e-1, -0.10368495e-2,  0.14330117e-2,
02740                     0.20052730e-3,  0.18751903e-2,  0.12668869e-2,
02741                     0.48736023e-3,  0.34850854e-2,  0.           ,
02742                     -0.36597173e-3,  0.19372124e-2,  0.70761825e-3, 0.46898375e-3};
02743 
02744    Double_t U6[] = {0,  0.35855696e-1, -0.27542114e-1,  0.12631023e-1,
02745                     -0.30188807e-2, -0.84479939e-3,  0.           ,
02746                     0.45675843e-3, -0.69836141e-2,  0.39876546e-2,
02747                     -0.36055679e-2,  0.           ,  0.15298434e-2, 0.19247256e-2};
02748 
02749    Double_t U7[] = {0, 0.10234691e+2, -0.35619655e+1,  0.69387764e+0,
02750                     -0.14047599e+0, -0.19952390e+1, -0.45679694e+0,
02751                     0.           ,  0.50505298e+0};
02752    Double_t U8[] = {0,  0.21487518e+2, -0.11825253e+2,  0.43133087e+1,
02753                     -0.14500543e+1, -0.34343169e+1, -0.11063164e+1,
02754                     -0.21000819e+0,  0.17891643e+1, -0.89601916e+0,
02755                     0.39120793e+0,  0.73410606e+0,  0.           ,-0.32454506e+0};
02756 
02757    Double_t V1[] = {0, 0.27827257e+0, -0.14227603e-2,  0.24848327e-2,
02758                     0.           ,  0.45091424e-1,  0.80559636e-2,
02759                     -0.38974523e-2,  0.           , -0.30634124e-2,
02760                     0.75633702e-3,  0.54730726e-2,  0.19792507e-2};
02761 
02762    Double_t V2[] = {0, 0.41421789e+0, -0.30061649e-1,  0.52249697e-2,
02763                     0.           ,  0.12693873e+0,  0.22999801e-1,
02764                     -0.86792801e-2,  0.31875584e-1, -0.61757928e-2,
02765                     0.           ,  0.19716857e-1,  0.32596742e-2};
02766 
02767    Double_t V3[] = {0, 0.20191056e+0, -0.46831422e-1,  0.96777473e-2,
02768                     -0.17995317e-2,  0.53921588e-1,  0.35068740e-2,
02769                     -0.12621494e-1, -0.54996531e-2, -0.90029985e-2,
02770                     0.34958743e-2,  0.18513506e-1,  0.68332334e-2,-0.12940502e-2};
02771 
02772    Double_t V4[] = {0, 0.13206081e+1,  0.10036618e+0, -0.22015201e-1,
02773                     0.61667091e-2, -0.14986093e+0, -0.12720568e-1,
02774                     0.24972042e-1, -0.97751962e-2,  0.26087455e-1,
02775                     -0.11399062e-1, -0.48282515e-1, -0.98552378e-2};
02776 
02777    Double_t V5[] = {0, 0.16435243e-1,  0.36051400e-1,  0.23036520e-2,
02778                     -0.61666343e-3, -0.10775802e-1,  0.51476061e-2,
02779                     0.56856517e-2, -0.13438433e-1,  0.           ,
02780                     0.           , -0.25421507e-2,  0.20169108e-2,-0.15144931e-2};
02781 
02782    Double_t V6[] = {0, 0.33432405e-1,  0.60583916e-2, -0.23381379e-2,
02783                     0.83846081e-3, -0.13346861e-1, -0.17402116e-2,
02784                     0.21052496e-2,  0.15528195e-2,  0.21900670e-2,
02785                     -0.13202847e-2, -0.45124157e-2, -0.15629454e-2, 0.22499176e-3};
02786 
02787    Double_t V7[] = {0, 0.54529572e+1, -0.90906096e+0,  0.86122438e-1,
02788                     0.           , -0.12218009e+1, -0.32324120e+0,
02789                     -0.27373591e-1,  0.12173464e+0,  0.           ,
02790                     0.           ,  0.40917471e-1};
02791 
02792    Double_t V8[] = {0, 0.93841352e+1, -0.16276904e+1,  0.16571423e+0,
02793                     0.           , -0.18160479e+1, -0.50919193e+0,
02794                     -0.51384654e-1,  0.21413992e+0,  0.           ,
02795                     0.           ,  0.66596366e-1};
02796 
02797    Double_t W1[] = {0, 0.29712951e+0,  0.97572934e-2,  0.           ,
02798                     -0.15291686e-2,  0.35707399e-1,  0.96221631e-2,
02799                     -0.18402821e-2, -0.49821585e-2,  0.18831112e-2,
02800                     0.43541673e-2,  0.20301312e-2, -0.18723311e-2,-0.73403108e-3};
02801 
02802    Double_t W2[] = {0, 0.40882635e+0,  0.14474912e-1,  0.25023704e-2,
02803                     -0.37707379e-2,  0.18719727e+0,  0.56954987e-1,
02804                     0.           ,  0.23020158e-1,  0.50574313e-2,
02805                     0.94550140e-2,  0.19300232e-1};
02806 
02807    Double_t W3[] = {0, 0.16861629e+0,  0.           ,  0.36317285e-2,
02808                     -0.43657818e-2,  0.30144338e-1,  0.13891826e-1,
02809                     -0.58030495e-2, -0.38717547e-2,  0.85359607e-2,
02810                     0.14507659e-1,  0.82387775e-2, -0.10116105e-1,-0.55135670e-2};
02811 
02812    Double_t W4[] = {0, 0.13493891e+1, -0.26863185e-2, -0.35216040e-2,
02813                     0.24434909e-1, -0.83447911e-1, -0.48061360e-1,
02814                     0.76473951e-2,  0.24494430e-1, -0.16209200e-1,
02815                     -0.37768479e-1, -0.47890063e-1,  0.17778596e-1, 0.13179324e-1};
02816 
02817    Double_t W5[] = {0,  0.10264945e+0,  0.32738857e-1,  0.           ,
02818                     0.43608779e-2, -0.43097757e-1, -0.22647176e-2,
02819                     0.94531290e-2, -0.12442571e-1, -0.32283517e-2,
02820                     -0.75640352e-2, -0.88293329e-2,  0.52537299e-2, 0.13340546e-2};
02821 
02822    Double_t W6[] = {0, 0.29568177e-1, -0.16300060e-2, -0.21119745e-3,
02823                     0.23599053e-2, -0.48515387e-2, -0.40797531e-2,
02824                     0.40403265e-3,  0.18200105e-2, -0.14346306e-2,
02825                     -0.39165276e-2, -0.37432073e-2,  0.19950380e-2, 0.12222675e-2};
02826 
02827    Double_t W8[] = {0,  0.66184645e+1, -0.73866379e+0,  0.44693973e-1,
02828                     0.           , -0.14540925e+1, -0.39529833e+0,
02829                     -0.44293243e-1,  0.88741049e-1};
02830 
02831    itype = 0;
02832    if (rkappa <0.01 || rkappa >12) {
02833       Error("Vavilov distribution", "illegal value of kappa");
02834       return;
02835    }
02836 
02837    Double_t DRK[6];
02838    Double_t DSIGM[6];
02839    Double_t ALFA[8];
02840    Int_t j;
02841    Double_t x, y, xx, yy, x2, x3, y2, y3, xy, p2, p3, q2, q3, pq;
02842    if (rkappa >= 0.29) {
02843       itype = 1;
02844       npt = 100;
02845       Double_t wk = 1./TMath::Sqrt(rkappa);
02846 
02847       AC[0] = (-0.032227*beta2-0.074275)*rkappa + (0.24533*beta2+0.070152)*wk + (-0.55610*beta2-3.1579);
02848       AC[8] = (-0.013483*beta2-0.048801)*rkappa + (-1.6921*beta2+8.3656)*wk + (-0.73275*beta2-3.5226);
02849       DRK[1] = wk*wk;
02850       DSIGM[1] = TMath::Sqrt(rkappa/(1-0.5*beta2));
02851       for (j=1; j<=4; j++) {
02852          DRK[j+1] = DRK[1]*DRK[j];
02853          DSIGM[j+1] = DSIGM[1]*DSIGM[j];
02854          ALFA[j+1] = (FNINV[j]-beta2*FNINV[j+1])*DRK[j];
02855       }
02856       HC[0]=TMath::Log(rkappa)+beta2+0.42278434;
02857       HC[1]=DSIGM[1];
02858       HC[2]=ALFA[3]*DSIGM[3];
02859       HC[3]=(3*ALFA[2]*ALFA[2] + ALFA[4])*DSIGM[4]-3;
02860       HC[4]=(10*ALFA[2]*ALFA[3]+ALFA[5])*DSIGM[5]-10*HC[2];
02861       HC[5]=HC[2]*HC[2];
02862       HC[6]=HC[2]*HC[3];
02863       HC[7]=HC[2]*HC[5];
02864       for (j=2; j<=7; j++)
02865          HC[j]*=EDGEC[j];
02866       HC[8]=0.39894228*HC[1];
02867    }
02868    else if (rkappa >=0.22) {
02869       itype = 2;
02870       npt = 150;
02871       x = 1+(rkappa-BKMXX3)*FBKX3;
02872       y = 1+(TMath::Sqrt(beta2)-BKMXY3)*FBKY3;
02873       xx = 2*x;
02874       yy = 2*y;
02875       x2 = xx*x-1;
02876       x3 = xx*x2-x;
02877       y2 = yy*y-1;
02878       y3 = yy*y2-y;
02879       xy = x*y;
02880       p2 = x2*y;
02881       p3 = x3*y;
02882       q2 = y2*x;
02883       q3 = y3*x;
02884       pq = x2*y2;
02885       AC[1] = W1[1] + W1[2]*x + W1[4]*x3 + W1[5]*y + W1[6]*y2 + W1[7]*y3 +
02886          W1[8]*xy + W1[9]*p2 + W1[10]*p3 + W1[11]*q2 + W1[12]*q3 + W1[13]*pq;
02887       AC[2] = W2[1] + W2[2]*x + W2[3]*x2 + W2[4]*x3 + W2[5]*y + W2[6]*y2 +
02888          W2[8]*xy + W2[9]*p2 + W2[10]*p3 + W2[11]*q2;
02889       AC[3] = W3[1] + W3[3]*x2 + W3[4]*x3 + W3[5]*y + W3[6]*y2 + W3[7]*y3 +
02890          W3[8]*xy + W3[9]*p2 + W3[10]*p3 + W3[11]*q2 + W3[12]*q3 + W3[13]*pq;
02891       AC[4] = W4[1] + W4[2]*x + W4[3]*x2 + W4[4]*x3 + W4[5]*y + W4[6]*y2 + W4[7]*y3 +
02892          W4[8]*xy + W4[9]*p2 + W4[10]*p3 + W4[11]*q2 + W4[12]*q3 + W4[13]*pq;
02893       AC[5] = W5[1] + W5[2]*x + W5[4]*x3 + W5[5]*y + W5[6]*y2 + W5[7]*y3 +
02894          W5[8]*xy + W5[9]*p2 + W5[10]*p3 + W5[11]*q2 + W5[12]*q3 + W5[13]*pq;
02895       AC[6] = W6[1] + W6[2]*x + W6[3]*x2 + W6[4]*x3 + W6[5]*y + W6[6]*y2 + W6[7]*y3 +
02896          W6[8]*xy + W6[9]*p2 + W6[10]*p3 + W6[11]*q2 + W6[12]*q3 + W6[13]*pq;
02897       AC[8] = W8[1] + W8[2]*x + W8[3]*x2 + W8[5]*y + W8[6]*y2 + W8[7]*y3 + W8[8]*xy;
02898       AC[0] = -3.05;
02899    } else if (rkappa >= 0.12) {
02900       itype = 3;
02901       npt = 200;
02902       x = 1 + (rkappa-BKMXX2)*FBKX2;
02903       y = 1 + (TMath::Sqrt(beta2)-BKMXY2)*FBKY2;
02904       xx = 2*x;
02905       yy = 2*y;
02906       x2 = xx*x-1;
02907       x3 = xx*x2-x;
02908       y2 = yy*y-1;
02909       y3 = yy*y2-y;
02910       xy = x*y;
02911       p2 = x2*y;
02912       p3 = x3*y;
02913       q2 = y2*x;
02914       q3 = y3*x;
02915       pq = x2*y2;
02916       AC[1] = V1[1] + V1[2]*x + V1[3]*x2 + V1[5]*y + V1[6]*y2 + V1[7]*y3 +
02917          V1[9]*p2 + V1[10]*p3 + V1[11]*q2 + V1[12]*q3;
02918       AC[2] = V2[1] + V2[2]*x + V2[3]*x2 + V2[5]*y + V2[6]*y2 + V2[7]*y3 +
02919          V2[8]*xy + V2[9]*p2 + V2[11]*q2 + V2[12]*q3;
02920       AC[3] = V3[1] + V3[2]*x + V3[3]*x2 + V3[4]*x3 + V3[5]*y + V3[6]*y2 + V3[7]*y3 +
02921          V3[8]*xy + V3[9]*p2 + V3[10]*p3 + V3[11]*q2 + V3[12]*q3 + V3[13]*pq;
02922       AC[4] = V4[1] + V4[2]*x + V4[3]*x2 + V4[4]*x3 + V4[5]*y + V4[6]*y2 + V4[7]*y3 +
02923          V4[8]*xy + V4[9]*p2 + V4[10]*p3 + V4[11]*q2 + V4[12]*q3;
02924       AC[5] = V5[1] + V5[2]*x + V5[3]*x2 + V5[4]*x3 + V5[5]*y + V5[6]*y2 + V5[7]*y3 +
02925          V5[8]*xy + V5[11]*q2 + V5[12]*q3 + V5[13]*pq;
02926       AC[6] = V6[1] + V6[2]*x + V6[3]*x2 + V6[4]*x3 + V6[5]*y + V6[6]*y2 + V6[7]*y3 +
02927          V6[8]*xy + V6[9]*p2 + V6[10]*p3 + V6[11]*q2 + V6[12]*q3 + V6[13]*pq;
02928       AC[7] = V7[1] + V7[2]*x + V7[3]*x2 + V7[5]*y + V7[6]*y2 + V7[7]*y3 +
02929          V7[8]*xy + V7[11]*q2;
02930       AC[8] = V8[1] + V8[2]*x + V8[3]*x2 + V8[5]*y + V8[6]*y2 + V8[7]*y3 +
02931          V8[8]*xy + V8[11]*q2;
02932       AC[0] = -3.04;
02933    } else {
02934       itype = 4;
02935       if (rkappa >=0.02) itype = 3;
02936       npt = 200;
02937       x = 1+(rkappa-BKMXX1)*FBKX1;
02938       y = 1+(TMath::Sqrt(beta2)-BKMXY1)*FBKY1;
02939       xx = 2*x;
02940       yy = 2*y;
02941       x2 = xx*x-1;
02942       x3 = xx*x2-x;
02943       y2 = yy*y-1;
02944       y3 = yy*y2-y;
02945       xy = x*y;
02946       p2 = x2*y;
02947       p3 = x3*y;
02948       q2 = y2*x;
02949       q3 = y3*x;
02950       pq = x2*y2;
02951       if (itype==3){
02952          AC[1] = U1[1] + U1[2]*x + U1[3]*x2 + U1[5]*y + U1[6]*y2 + U1[7]*y3 +
02953             U1[8]*xy + U1[10]*p3 + U1[12]*q3 + U1[13]*pq;
02954          AC[2] = U2[1] + U2[2]*x + U2[3]*x2 + U2[5]*y + U2[6]*y2 + U2[7]*y3 +
02955             U2[8]*xy + U2[9]*p2 + U2[10]*p3 + U2[12]*q3 + U2[13]*pq;
02956          AC[3] = U3[1] + U3[2]*x + U3[3]*x2 + U3[5]*y + U3[6]*y2 + U3[7]*y3 +
02957             U3[8]*xy + U3[9]*p2 + U3[10]*p3 + U3[11]*q2 + U3[12]*q3 + U3[13]*pq;
02958          AC[4] = U4[1] + U4[2]*x + U4[3]*x2 + U4[4]*x3 + U4[5]*y + U4[6]*y2 + U4[7]*y3 +
02959             U4[8]*xy + U4[9]*p2 + U4[10]*p3 + U4[11]*q2 + U4[12]*q3;
02960          AC[5] = U5[1] + U5[2]*x + U5[3]*x2 + U5[4]*x3 + U5[5]*y + U5[6]*y2 + U5[7]*y3 +
02961             U5[8]*xy + U5[10]*p3 + U5[11]*q2 + U5[12]*q3 + U5[13]*pq;
02962          AC[6] = U6[1] + U6[2]*x + U6[3]*x2 + U6[4]*x3 + U6[5]*y + U6[7]*y3 +
02963             U6[8]*xy + U6[9]*p2 + U6[10]*p3 + U6[12]*q3 + U6[13]*pq;
02964          AC[7] = U7[1] + U7[2]*x + U7[3]*x2 + U7[4]*x3 + U7[5]*y + U7[6]*y2 + U7[8]*xy;
02965       }
02966       AC[8] = U8[1] + U8[2]*x + U8[3]*x2 + U8[4]*x3 + U8[5]*y + U8[6]*y2 + U8[7]*y3 +
02967          U8[8]*xy + U8[9]*p2 + U8[10]*p3 + U8[11]*q2 + U8[13]*pq;
02968       AC[0] = -3.03;
02969    }
02970 
02971    AC[9] = (AC[8] - AC[0])/npt;
02972    AC[10] = 1./AC[9];
02973    if (itype == 3) {
02974       x = (AC[7]-AC[8])/(AC[7]*AC[8]);
02975       y = 1./TMath::Log(AC[8]/AC[7]);
02976       p2 = AC[7]*AC[7];
02977       AC[11] = p2*(AC[1]*TMath::Exp(-AC[2]*(AC[7]+AC[5]*p2)-
02978                                     AC[3]*TMath::Exp(-AC[4]*(AC[7]+AC[6]*p2)))-0.045*y/AC[7])/(1+x*y*AC[7]);
02979       AC[12] = (0.045+x*AC[11])*y;
02980    }
02981    if (itype == 4) AC[13] = 0.995/LandauI(AC[8]);
02982 
02983    if (mode==0) return;
02984    //
02985    x = AC[0];
02986    WCM[0] = 0;
02987    Double_t fl, fu;
02988    Int_t k;
02989    fl = TMath::VavilovDenEval(x, AC, HC, itype);
02990    for (k=1; k<=npt; k++) {
02991       x += AC[9];
02992       fu = TMath::VavilovDenEval(x, AC, HC, itype);
02993       WCM[k] = WCM[k-1] + fl + fu;
02994       fl = fu;
02995    }
02996    x = 0.5*AC[9];
02997    for (k=1; k<=npt; k++)
02998       WCM[k]*=x;
02999 }
03000 
03001 //______________________________________________________________________________
03002 Double_t TMath::VavilovDenEval(Double_t rlam, Double_t *AC, Double_t *HC, Int_t itype)
03003 {
03004    //Internal function, called by Vavilov and VavilovSet
03005 
03006    Double_t v = 0;
03007    if (rlam < AC[0] || rlam > AC[8])
03008       return 0;
03009    Int_t k;
03010    Double_t x, fn, s;
03011    Double_t h[10];
03012    if (itype ==1 ) {
03013       fn = 1;
03014       x = (rlam + HC[0])*HC[1];
03015       h[1] = x;
03016       h[2] = x*x -1;
03017       for (k=2; k<=8; k++) {
03018          fn++;
03019          h[k+1] = x*h[k]-fn*h[k-1];
03020       }
03021       s = 1 + HC[7]*h[9];
03022       for (k=2; k<=6; k++)
03023          s+=HC[k]*h[k+1];
03024       v = HC[8]*TMath::Exp(-0.5*x*x)*TMath::Max(s, 0.);
03025    }
03026    else if (itype == 2) {
03027       x = rlam*rlam;
03028       v = AC[1]*TMath::Exp(-AC[2]*(rlam+AC[5]*x) - AC[3]*TMath::Exp(-AC[4]*(rlam+AC[6]*x)));
03029    }
03030    else if (itype == 3) {
03031       if (rlam < AC[7]) {
03032          x = rlam*rlam;
03033          v = AC[1]*TMath::Exp(-AC[2]*(rlam+AC[5]*x)-AC[3]*TMath::Exp(-AC[4]*(rlam+AC[6]*x)));
03034       } else {
03035          x = 1./rlam;
03036          v = (AC[11]*x + AC[12])*x;
03037       }
03038    }
03039    else if (itype == 4) {
03040       v = AC[13]*TMath::Landau(rlam);
03041    }
03042    return v;
03043 }

Generated on Tue Jul 5 14:34:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1