ATLAS Offline Software
Loading...
Searching...
No Matches
TRTAlignHistoChisqProjection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7#include <TH2F.h>
8#include <TF1.h>
9#include <CLHEP/Matrix/Matrix.h>
10
11namespace TRTAlign
12{
13
14 HistoChisqProjection::HistoChisqProjection(const char* name, int dim, int nbins, float xmin, float xmax)
15 : m_dim(dim), m_integrals(dim,0),m_halfdChisqdX(dim,0)
16 {
17 m_h2 = new TH2F("h2",name,dim,-0.5,dim-0.5,nbins,xmin,xmax) ;
18 m_unweightedh2 = new TH2F("unweightedh2",name,dim,-0.5,dim-0.5,nbins,xmin,xmax) ;
19 m_h2->Sumw2() ;
20 } ;
21
22 void HistoChisqProjection::addfirst(const CLHEP::HepMatrix& deriv, const CLHEP::HepSymMatrix& weight,
23 const CLHEP::HepSymMatrix& variance, const CLHEP::HepVector& residual )
24 {
25 int dimr = weight.num_row() ;
26 CLHEP::HepMatrix derivtimesweight(deriv*weight) ;
27
28 for(int irow=1; irow<=dimr; ++irow) {
29 double thissigma = sqrt( variance.fast(irow,irow) ) ;
30
31 for(int ipar=0; ipar<m_dim; ++ipar) {
32 double thisweight = derivtimesweight(ipar+1, irow ) * thissigma ;
33 double thispull = residual(irow) / thissigma ;
34 if( thisweight< 0 ) {
35 thisweight*=-1 ;
36 thispull*=-1 ;
37 }
38 m_h2->Fill( double(ipar), thispull, thisweight ) ;
39 m_unweightedh2->Fill( double(ipar), thispull ) ;
40 m_integrals[ipar] += thisweight ;
41 m_halfdChisqdX(ipar+1) += thisweight*thispull ;
42 }
43 }
44 }
45
46 void HistoChisqProjection::addsecond(const CLHEP::HepMatrix& deriv, const CLHEP::HepSymMatrix& weight )
47 {
48 m_halfd2ChisqdX2 += weight.similarity(deriv) ;
49 }
50
51 double binwidth(1) ;
52 double gausfunc(double* x, double* par)
53 {
54 double integral = par[0] ;
55 double mean = par[1] ;
56 double sigma = par[2] ;
57
58 double xi = (x[0] - mean)/sigma ;
59 static double sqrt2pi = sqrt(2*M_PI) ;
60 double norm = integral * binwidth / ( sigma*sqrt2pi ) ;
61
62 return norm * exp(-0.5*xi*xi) ;
63 } ;
64
65
66 CLHEP::HepVector
68 {
69 CLHEP::HepVector halfDChisqDX(m_dim,0) ;
70 // perform a fit to each hist
71 TF1 f1("mygaus",gausfunc,m_h2->GetYaxis()->GetXmin(),m_h2->GetYaxis()->GetXmax(),3) ;
72 for(int ipar=1; ipar<=m_dim; ++ipar) {
73 char hisname[256] ;
74 sprintf(hisname,"tmph1_%d",ipar) ;
75 TH1* tmph1 = m_h2->ProjectionY(hisname,ipar,ipar,"e") ; // the 'e' triggers the error calculation (quite important!)
76 binwidth = tmph1->GetXaxis()->GetBinWidth(1) ;
77 tmph1->SetDirectory(m_h2->GetDirectory()) ;
78 f1.SetParameter(0,tmph1->Integral());
79 f1.SetParLimits(0,0.5*tmph1->Integral(),2*tmph1->Integral()) ;
80 f1.SetParameter(1,0) ;
81 f1.SetParameter(2,tmph1->GetRMS()) ;
82 tmph1->Fit(&f1,"Q0I") ;
83 tmph1->GetListOfFunctions()->Add(f1.Clone()) ;
84 halfDChisqDX(ipar) = f1.GetParameter(1) * m_integrals[ipar-1] ;
85 os << "fitresult: " << f1.GetParameter(1) << " weight: " << m_integrals[ipar-1] << " integrated mean: "
86 << m_halfdChisqdX(ipar) / m_integrals[ipar-1] << std::endl ;
87 }
88 return halfDChisqDX ;
89 }
90
92 {
93 m_h2->SetDirectory(dir) ;
94 m_unweightedh2->SetDirectory(dir) ;
95 }
96}
#define M_PI
#define x
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)
double binwidth(1)