ATLAS Offline Software
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 
11 namespace 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 }
PlotCalibFromCool.norm
norm
Definition: PlotCalibFromCool.py:100
pdg_comparison.sigma
sigma
Definition: pdg_comparison.py:324
mean
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="")
Definition: dependence.cxx:254
yodamerge_tmp.dim
dim
Definition: yodamerge_tmp.py:239
TRTAlign::HistoChisqProjection::m_h2
TH2 * m_h2
Definition: TRTAlignHistoChisqProjection.h:60
integral
double integral(TH1 *h)
Definition: computils.cxx:58
ClusterSeg::residual
@ residual
Definition: ClusterNtuple.h:20
TRTAlign::HistoChisqProjection::getFittedHalfDChisqDX
CLHEP::HepVector getFittedHalfDChisqDX(std::ostream &os=std::cout) const
Definition: TRTAlignHistoChisqProjection.cxx:67
M_PI
#define M_PI
Definition: ActiveFraction.h:11
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
x
#define x
TRTAlign::HistoChisqProjection::m_halfd2ChisqdX2
CLHEP::HepSymMatrix m_halfd2ChisqdX2
Definition: TRTAlignHistoChisqProjection.h:64
TRTAlign::HistoChisqProjection::setDirectory
void setDirectory(TDirectory *dir)
Definition: TRTAlignHistoChisqProjection.cxx:91
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
xmin
double xmin
Definition: listroot.cxx:60
TRTAlign::HistoChisqProjection::m_dim
int m_dim
Definition: TRTAlignHistoChisqProjection.h:59
TRTAlign::HistoChisqProjection::m_halfdChisqdX
CLHEP::HepVector m_halfdChisqdX
Definition: TRTAlignHistoChisqProjection.h:63
TRTAlign::HistoChisqProjection::HistoChisqProjection
HistoChisqProjection(const char *name, int dim, int nbins, float xmin, float xmax)
Definition: TRTAlignHistoChisqProjection.cxx:14
TRTAlign::binwidth
double binwidth(1)
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
beamspotman.dir
string dir
Definition: beamspotman.py:623
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
TRTAlign::HistoChisqProjection::addfirst
void addfirst(const CLHEP::HepMatrix &derivative, const CLHEP::HepSymMatrix &weight, const CLHEP::HepSymMatrix &variance, const CLHEP::HepVector &residual)
Definition: TRTAlignHistoChisqProjection.cxx:22
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
TRTAlign::gausfunc
double gausfunc(double *x, double *par)
Definition: TRTAlignHistoChisqProjection.cxx:52
xmax
double xmax
Definition: listroot.cxx:61
TRTAlign::HistoChisqProjection::addsecond
void addsecond(const CLHEP::HepMatrix &derivative, const CLHEP::HepSymMatrix &weight)
Definition: TRTAlignHistoChisqProjection.cxx:46
TRTAlign::HistoChisqProjection::m_integrals
std::vector< double > m_integrals
Definition: TRTAlignHistoChisqProjection.h:62
TRTAlignHistoChisqProjection.h
read_hist_ntuple.f1
f1
Definition: read_hist_ntuple.py:4
TRTAlign::HistoChisqProjection::m_unweightedh2
TH2 * m_unweightedh2
Definition: TRTAlignHistoChisqProjection.h:61
TRTAlign
Definition: TRTAlignHistoChisqProjection.cxx:12