ATLAS Offline Software
Loading...
Searching...
No Matches
TRTAlignHistogrammedChisqProjection.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6// this class keeps track of chisquare derivatives. it makes
7// histograms of the projections to the derivatives, which is kine of
8// useful, sometimes.
9
10namespace TRTAlign
11{
12
14 {
15 public:
16 HistoChisqProjection(const char* name, int dim, int nbins, float xmin, float xmax) ;
18
19 // use this if the weight matrix for first and second derivative are equal
20 void add( const HepMatrix& deriv, const HepSymMatrix& weight,
21 const HepSymMatrix& variance, const HepVector& residual ) {
22 addfirst( deriv, weight, variance, residual ) ;
23 addsecond( deriv, weight ) ;
24 }
25
26 // use this if they are not (like in global chisquare method)
27 void add( const HepMatrix& deriv, const HepSymMatrix& weight,
28 const HepSymMatrix& variance, const HepVector& residual, const HepSymMatrix& secondweight) {
29 addfirst( deriv, weight, variance, residual ) ;
30 addsecond( deriv, secondweight ) ;
31 }
32
33 HepVector getFittedHalfDChisqDX(std::ostream& os=std::cout) const ;
34 HepVector getNormalHalfDChisqDX() const { return m_halfdChisqdX ; }
35
36 // set the TDirectory where the histograms are kept
37 void setDirectory(TDirectory* dir) { m_h2->SetDirectory(dir) ; m_unweightedh2->SetDirectory(dir) ; }
38
39 private:
40 void addfirst( const HepMatrix& deriv, const HepSymMatrix& weight, const HepSymMatrix& variance, const HepVector& residual ) ;
41 void addsecond( const HepMatrix& deriv, const HepSymMatrix& weight) ;
42
43 private:
44 int m_dim ;
45 TH2* m_h2 ;
46 TH2* m_unweightedh2 ;
47 std::vector<double> m_integrals ;
48 HepVector m_halfdChisqdX ;
49 HepSymMatrix m_halfd2ChisqdX2 ;
50 } ;
51
52 HistoChisqProjection::HistoChisqProjection(const char* name, int dim, int nbins, float xmin, float xmax)
53 : m_dim(dim), m_integrals(dim,0),m_halfdChisqdX(dim,0)
54 {
55 m_h2 = new TH2F("h2",name,dim,-0.5,dim-0.5,nbins,xmin,xmax) ;
56 m_unweightedh2 = new TH2F("unweightedh2",name,dim,-0.5,dim-0.5,nbins,xmin,xmax) ;
57 m_h2->Sumw2() ;
58 } ;
59
60 void HistoChisqProjection::addfirst(const HepMatrix& deriv, const HepSymMatrix& weight,
61 const HepSymMatrix& variance, const HepVector& residual )
62 {
63 int dimr = weight.num_row() ;
64 HepMatrix derivtimesweight(deriv*weight) ;
65
66 for(int irow=1; irow<=dimr; ++irow) {
67 double thissigma = sqrt( variance.fast(irow,irow) ) ;
68
69 for(int ipar=0; ipar<m_dim; ++ipar) {
70 double thisweight = derivtimesweight(ipar+1, irow ) * thissigma ;
71 double thispull = residual(irow) / thissigma ;
72 if( thisweight< 0 ) {
73 thisweight*=-1 ;
74 thispull*=-1 ;
75 }
76 m_h2->Fill( double(ipar), thispull, thisweight ) ;
77 m_unweightedh2->Fill( double(ipar), thispull ) ;
78 m_integrals[ipar] += thisweight ;
79 m_halfdChisqdX(ipar+1) += thisweight*thispull ;
80 }
81 }
82 }
83
84 void HistoChisqProjection::addsecond(const HepMatrix& deriv, const HepSymMatrix& weight )
85 {
86 m_halfd2ChisqdX2 += weight.similarity(deriv) ;
87 }
88
89 double binwidth(1) ;
90 double gausfunc(double* x, double* par)
91 {
92 double integral = par[0] ;
93 double mean = par[1] ;
94 double sigma = par[2] ;
95
96 double xi = (x[0] - mean)/sigma ;
97 static double sqrt2pi = sqrt(2*M_PI) ;
98 double norm = integral * binwidth / ( sigma*sqrt2pi ) ;
99
100 return norm * exp(-0.5*xi*xi) ;
101 } ;
102
103
104 HepVector
105 HistoChisqProjection::getFittedHalfDChisqDX(std::ostream& os) const {
106 HepVector halfDChisqDX(m_dim,0) ;
107 // perform a fit to each hist
108 TF1 f1("mygaus",gausfunc,m_h2->GetYaxis()->GetXmin(),m_h2->GetYaxis()->GetXmax(),3) ;
109 for(int ipar=1; ipar<=m_dim; ++ipar) {
110 char hisname[256] ;
111 sprintf(hisname,"tmph1_%d",ipar) ;
112 TH1* tmph1 = m_h2->ProjectionY(hisname,ipar,ipar,"e") ; // the 'e' triggers the error calculation (quite important!)
113 binwidth = tmph1->GetXaxis()->GetBinWidth(1) ;
114 tmph1->SetDirectory(m_h2->GetDirectory()) ;
115 f1.SetParameter(0,tmph1->Integral());
116 f1.SetParLimits(0,0.5*tmph1->Integral(),2*tmph1->Integral()) ;
117 f1.SetParameter(1,0) ;
118 f1.SetParameter(2,tmph1->GetRMS()) ;
119 tmph1->Fit(&f1,"Q0I") ;
120 tmph1->GetListOfFunctions()->Add(f1.Clone()) ;
121 halfDChisqDX(ipar) = f1.GetParameter(1) * m_integrals[ipar-1] ;
122 os << "fitresult: " << f1.GetParameter(1) << " weight: " << m_integrals[ipar-1] << " integrated mean: "
123 << m_halfdChisqdX(ipar) / m_integrals[ipar-1] << std::endl ;
124 }
125 return halfDChisqDX ;
126 }
#define M_PI
#define x
void add(const CLHEP::HepMatrix &deriv, const CLHEP::HepSymMatrix &weight, const CLHEP::HepSymMatrix &variance, const CLHEP::HepVector &residual)
void addfirst(const CLHEP::HepMatrix &derivative, const CLHEP::HepSymMatrix &weight, const CLHEP::HepSymMatrix &variance, const CLHEP::HepVector &residual)
HistoChisqProjection(const char *name, int dim, int nbins, float xmin, float xmax)
void addsecond(const CLHEP::HepMatrix &derivative, const CLHEP::HepSymMatrix &weight)
CLHEP::HepVector getFittedHalfDChisqDX(std::ostream &os=std::cout) const
double integral(TH1 *h)
Definition computils.cxx:59
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
double xmax
Definition listroot.cxx:61
double xmin
Definition listroot.cxx:60
bool binwidth
Definition listroot.cxx:58
double gausfunc(double *x, double *par)