ATLAS Offline Software
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 
10 namespace TRTAlign
11 {
12 
13  class HistoChisqProjection
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  }
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
TRTAlign::HistoChisqProjection::add
void add(const CLHEP::HepMatrix &deriv, const CLHEP::HepSymMatrix &weight, const CLHEP::HepSymMatrix &variance, const CLHEP::HepVector &residual)
Definition: TRTAlignHistoChisqProjection.h:33
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
TRTAlign::HistoChisqProjection::~HistoChisqProjection
~HistoChisqProjection()
TRTAlign::HistoChisqProjection::getNormalHalfDChisqDX
CLHEP::HepVector getNormalHalfDChisqDX() const
Definition: TRTAlignHistoChisqProjection.h:48
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