BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
ChisqConsistency.cxx
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2// File and Version Information:
3// $Id: ChisqConsistency.cxx,v 1.1.1.1 2005/04/21 01:17:17 zhangy Exp $
4//
5// Description:
6//
7// Environment:
8// Software developed for the BaBar Detector at the SLAC B-Factory.
9//
10// Author List:
11// Bob Jacobsen, Ed Iskander
12//
13// Copyright Information:
14// Copyright (C) 1996 Lawrence Berkeley Laboratory
15//
16//------------------------------------------------------------------------
17
18//-----------------------
19// This Class's Header --
20//-----------------------
21// #include "BaBar/BaBar.hh"
22#include "ProbTools/ChisqConsistency.h"
23
24#include <float.h>
25#include <iostream>
26#include <math.h>
27
28#include "ProbTools/NumRecipes.h"
29
30// #include "ErrLogger/ErrLog.hh"
31
32// prototype the cernlib function
33extern "C" {
34float chisin_( const float&, const int& );
35double dgausn_( double& arg );
36}
37
39
40ChisqConsistency::ChisqConsistency( double chisq, double nDof )
41 : _chisq( chisq ), _nDof( nDof ) {
42 double z2 = 0.5 * _chisq;
43 double n2 = 0.5 * _nDof;
44
45 if ( n2 <= 0 || z2 < 0 )
46 {
47 std::cout << "ErrMsg(warning)"
48 << " Got unphysical values: chisq = " << chisq << " #dof = " << nDof
49 << std::endl;
50 _value = 0;
51 _likelihood = 0;
53 return;
54 }
56
57 // given that n2>0 && z2>=0, gammq will NOT abort
59
60 if ( _chisq == 0 ) { _likelihood = 1; }
61 else
62 {
63 double loglike = ( n2 - 1 ) * log( z2 ) - z2 - NumRecipes::gammln( n2 );
64 if ( loglike < DBL_MIN_EXP )
65 {
66 _likelihood = 0;
68 }
69 else { _likelihood = 0.5 * exp( loglike ); }
70 }
71}
72
73ChisqConsistency::ChisqConsistency( unsigned nDof, double prob ) : _nDof( nDof ) {
74 if ( prob >= 0.0 || prob <= 1.0 || nDof < 0 ) _value = prob;
75 else
76 {
77 std::cout << "ErrMsg(warning)"
78 << " Got unphysical values: prob = " << prob << " #dof = " << nDof << std::endl;
79 _value = 0;
80 _likelihood = 0;
82 return;
83 }
85 if ( prob != 1.0 )
86 {
87 // use the cernlib function to get chisq. Note the funny convention on prob!!
88 float value = 1.0 - float( _value );
89 int ndof = nDof;
90 if ( value < 1.0 ) _chisq = chisin_( value, ndof );
91 else _chisq = log( double( FLT_MAX ) );
92 // use the same algorithm as above to get loglikelihood
93 double z2 = 0.5 * _chisq;
94 double n2 = 0.5 * _nDof;
95 if ( _chisq == 0 ) { _likelihood = 1; }
96 else
97 {
98 double loglike = ( n2 - 1 ) * log( z2 ) - z2 - NumRecipes::gammln( n2 );
99 if ( loglike < DBL_MIN_EXP )
100 {
101 _likelihood = 0;
103 }
104 else { _likelihood = 0.5 * exp( loglike ); }
105 }
106 }
107}
108
110 : Consistency( other ), _chisq( other._chisq ), _nDof( other._nDof ) {}
111
113 if ( this != &other )
114 {
115 Consistency::operator=( other );
116 _chisq = other._chisq;
117 _nDof = other._nDof;
118 }
119 return *this;
120}
double dgausn_(double &arg)
float chisin_(const float &, const int &)
EvtComplex exp(const EvtComplex &c)
double arg(const EvtComplex &c)
int n2
Definition SD0Tag.cxx:59
ChisqConsistency & operator=(const ChisqConsistency &)
Consistency & operator=(const Consistency &rhs)
static double gammq(double a, double x)
static double gammln(double x)