ATLAS Offline Software
Chi2Calc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "LArSamplesMon/OFC.h"
10 #include "LArCafJobs/Definitions.h"
11 #include "TMath.h"
12 #include <iostream>
13 using std::cout;
14 using std::endl;
15 
16 using namespace LArSamples;
17 
18 
19 double Chi2Calc::scalarProduct(const TVectorD& values1, const TVectorD& values2,
20  const CovMatrix& invCovMat) const
21 {
22  if (values1.GetLwb() != invCovMat.GetRowLwb() || values1.GetUpb() != invCovMat.GetRowUpb()) return -1;
23  if (values2.GetLwb() != invCovMat.GetColLwb() || values2.GetUpb() != invCovMat.GetColUpb()) return -1;
24 
25  double chi2Val = 0;
26 
27  for (int i = values1.GetLwb(); i <= values1.GetUpb(); i++) {
28  for (int j = values2.GetLwb(); j <= values2.GetUpb(); j++) {
29  chi2Val += invCovMat(i, j)*values1(i)*values2(j);
30  }
31  }
32  return chi2Val;
33 }
34 
35 
37  const ScaledErrorData* shapeError, int lw, int up, bool noDataError)
38 {
39  TVectorD refVals;
40  if (!reference.interpolate(data, refVals, errors, lw, up)) {
41  cout << "Chi2Calc::deltas : Could not find overlap region between data and reference in [" << lw << ", " << up << "]." << endl;
42  return TVectorD();
43  }
44  m_lwb = refVals.GetLwb();
45  m_upb = refVals.GetUpb();
46 
47  TVectorD deltas = data.values(lwb(), upb());
48  deltas -= refVals;
49  if (shapeError && shapeError->providesRange(*this)) {
50  TVectorD offsets = shapeError->offsets(lwb(), upb());
51  deltas -= offsets;
52  errors += shapeError->errors(lwb(), upb());
53  }
54 
55  if (!noDataError) errors = data.covarianceMatrix(lwb(), upb(), errors, useCorrs());
56  return deltas;
57 }
58 
59 
60 double Chi2Calc::chi2(const AbsShape& data, const AbsShape& reference, const ScaledErrorData* shapeError,
61  int lw, int up)
62 {
63  double chi2Val = 0;
65  TVectorD dv = deltas(data, reference, errors, shapeError, lw, up, true);
66  if (dv.GetNrows() == 0) return -1;
67 
68  // OFC version (gives correct error terms for individual samplings after OFC fit is performed
69  if (m_pars & OFCChi2) {
70  OFC ofc(reference, data, errors, lwb(), upb(), shapeError, useCorrs());
71  return scalarProduct(dv, dv, ofc.invGamma());
72  }
73  // Basic version --- fast, simple
74  if (m_pars & BasicChi2) {
75  for (int k = lwb(); k < upb(); k++)
76  chi2Val += dv(k)*dv(k)/(TMath::Power(data.error(k),2) + errors(k,k));
77  return chi2Val;
78  }
79  // Default version :
80  CovMatrix invCovMat = data.invCovarianceMatrix(lwb(), upb(), errors, useCorrs());
81 /* dv.Print();
82  data.covarianceMatrix(lwb(), upb()).Print();
83  data.covarianceMatrix(lwb(), upb(), errors, useCorrs()).Print();*/
84  return scalarProduct(dv, dv, invCovMat);
85 }
86 
87 
88 bool Chi2Calc::bestRescale(const AbsShape& data, const AbsShape& reference, double& k,
89  double& chi2, double deltaT, const ScaledErrorData* sed, unsigned int minNDof) const
90 {
91  k = 1;
92  chi2 = -1;
93  ScaledShiftedShape shiftedData(data, 1, -deltaT);
94  TVectorD r;
96  bool result = reference.interpolate(shiftedData, r, errors, -1, -1); // lwb and upb will come from data
97  if (!result || r.GetNrows() < (int)minNDof) {
98  k = 0;
99  chi2 = 1E99;
100  return true;
101  }
102 
103  if (sed) {
104  ScaledErrorData sed2(*sed, sed->scale(), sed->time() + deltaT);
105  if (sed2.providesRange(r)) r += sed2.offsets(r.GetLwb(), r.GetUpb());
106  }
107 
108  TVectorD v = data.values(r.GetLwb(), r.GetUpb());
109  CovMatrix invCovMat = data.invCovarianceMatrix(r.GetLwb(), r.GetUpb(), errors, useCorrs());
110  double sumR2 = scalarProduct(r, r, invCovMat);
111  double sumRV = scalarProduct(r, v, invCovMat);
112  double sumV2 = scalarProduct(v, v, invCovMat);
113  k = sumRV/sumR2; // optimal K-factor
114  chi2 = sumV2 - 2*k*sumRV + k*k*sumR2; // chi2
115 
116  return true;
117 }
beamspotman.r
def r
Definition: beamspotman.py:676
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Chi2Calc.h
LArSamples::ScaledShiftedShape
Definition: ScaledShiftedShape.h:17
LArSamples::Chi2Calc::deltas
TVectorD deltas(const AbsShape &data, const AbsShape &reference, CovMatrix &errors, const ScaledErrorData *shapeError=0, int lwb=-1, int upb=-1, bool noDataError=false)
Definition: Chi2Calc.cxx:36
get_generator_info.result
result
Definition: get_generator_info.py:21
PlotCalibFromCool.dv
dv
Definition: PlotCalibFromCool.py:762
ScaledErrorData.h
LArSamples::Chi2Calc::m_pars
int m_pars
Definition: Chi2Calc.h:56
LArSamples::CovMatrix
TMatrixTSym< double > CovMatrix
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:11
python.LumiCalcWorking.lw
lw
Definition: LumiCalcWorking.py:112
LArSamples::Chi2Calc::lwb
int lwb() const
Definition: Chi2Calc.h:45
OFC.h
LArSamples
Definition: AbsShape.h:24
reference
Definition: hcg.cxx:437
LArSamples::Chi2Calc::scalarProduct
double scalarProduct(const TVectorD &values1, const TVectorD &values2, const CovMatrix &invCovMat) const
Definition: Chi2Calc.cxx:19
LArSamples::ScaledErrorData
Definition: ScaledErrorData.h:17
LArSamples::Chi2Calc::bestRescale
bool bestRescale(const AbsShape &data, const AbsShape &reference, double &k, double &chi2, double deltaT=0, const ScaledErrorData *sed=0, unsigned int minNDof=0) const
Definition: Chi2Calc.cxx:88
lumiFormat.i
int i
Definition: lumiFormat.py:85
CalibCoolCompareRT.up
up
Definition: CalibCoolCompareRT.py:109
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:523
LArSamples::ScaledErrorData::scale
double scale() const
Definition: ScaledErrorData.h:30
LArSamples::Chi2Calc::m_lwb
int m_lwb
Definition: Chi2Calc.h:58
LArSamples::OFC
Definition: OFC.h:27
mergePhysValFiles.errors
list errors
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:43
LArSamples::OFCChi2
@ OFCChi2
Definition: Chi2Calc.h:24
python.PyAthena.v
v
Definition: PyAthena.py:154
LArSamples::BasicChi2
@ BasicChi2
Definition: Chi2Calc.h:24
LArSamples::FitterData::minNDof
unsigned int minNDof
Definition: ShapeFitter.cxx:27
LArSamples::AbsShape
Definition: AbsShape.h:28
Definitions.h
LArSamples::Chi2Calc::useCorrs
bool useCorrs() const
Definition: Chi2Calc.h:54
LArSamples::IndexRange::providesRange
bool providesRange(int lw, int up) const
Definition: IndexRange.h:33
LArSamples::FitterData::sed
const ScaledErrorData * sed
Definition: ShapeFitter.cxx:26
LArSamples::ScaledErrorData::errors
const CovMatrix errors(int first=-1, int last=-1) const
Definition: ScaledErrorData.cxx:31
ScaledShiftedShape.h
ReadOfcFromCool.ofc
ofc
Definition: ReadOfcFromCool.py:110
LArSamples::ScaledErrorData::time
double time() const
Definition: ScaledErrorData.h:31
LArSamples::Chi2Calc::upb
int upb() const
Definition: Chi2Calc.h:46
LArSamples::ScaledErrorData::offsets
const TVectorD offsets(int first=-1, int last=-1) const
Definition: ScaledErrorData.cxx:14
fitman.k
k
Definition: fitman.py:528
LArSamples::Chi2Calc::m_upb
int m_upb
Definition: Chi2Calc.h:58
LArSamples::Chi2Calc::chi2
double chi2(const AbsShape &data, const AbsShape &reference, const ScaledErrorData *shapeError=0, int lwb=-1, int upb=-1)
Definition: Chi2Calc.cxx:60