ATLAS Offline Software
Averager.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "TMath.h"
8 #include <iostream>
9 using std::cout;
10 using std::endl;
11 
12 using namespace LArSamples;
13 
14 
15 Averager::Averager(unsigned int n)
16  : m_n(0), m_wTot(0), m_lwb(0)
17 {
18  for (unsigned int i = 0; i < n; i++) add();
19 }
20 
21 
22 Averager::Averager(unsigned int lwb, unsigned int upb)
23  : m_n(0), m_wTot(0), m_lwb(lwb)
24 {
25  for (unsigned int i = lwb; i <= upb; i++) add();
26 }
27 
28 
30 {
31  m_n = other.m_n;
32  m_wTot = other.m_wTot;
33  m_lwb = other.lwb();
34  m_xMin = other.m_xMin;
35  m_xMax = other.m_xMax;
36  prepare();
37  m_sum1 = other.sum1();
38  m_sum2 = other.sum2();
39  m_sum11Matrix = other.sum11Matrix();
40  m_sum21Matrix = other.sum21Matrix();
41  m_sum22Matrix = other.sum22Matrix();
42  return *this;
43 }
44 
45 
46 bool Averager::add(double xMin, double xMax)
47 {
48  if (m_n > 0) {
49  cout << "Cannot add more variables to the Averager, data filling has started already." << endl;
50  return false;
51  }
52  m_xMin.push_back(xMin);
53  m_xMax.push_back(xMax);
54  return true;
55 }
56 
57 
59 {
60  m_sum1.ResizeTo(lwb(), upb());
61  m_sum2.ResizeTo(lwb(), upb());
62  m_sum11Matrix.ResizeTo(lwb(), upb(), lwb(), upb());
63  m_sum21Matrix.ResizeTo(lwb(), upb(), lwb(), upb());
64  m_sum22Matrix.ResizeTo(lwb(), upb(), lwb(), upb());
65  return true;
66 }
67 
68 
69 bool Averager::fill(const TVectorD& values, double w)
70 {
71  if (!hasSameRange(values)) {
72  cout << "Filling with wrong indices!" << endl;
73  return false;
74  }
75  if (m_n == 0 && !prepare()) return false;
76 
77  for (int i1 = lwb(); i1 <= upb(); i1++) {
78  double val1 = values(i1);
79  m_sum1(i1) += val1*w;
80  m_sum2(i1) += val1*val1*w;
81  for (int i2 = lwb(); i2 <= upb(); i2++) {
82  double val2 = values(i2);
83  m_sum11Matrix(i1, i2) += val1*val2*w;
84  m_sum21Matrix(i1, i2) += val1*val1*val2*w;
85  m_sum22Matrix(i1, i2) += val1*val1*val2*val2*w;
86  }
87  }
88  m_n++;
89  m_wTot += w;
90  return true;
91 }
92 
93 
95 {
96  if (nEntries() == 0) {
97  *this = other;
98  return true;
99  }
100  if (!hasSameRange(other)) return false;
101  m_sum1 += other.sum1();
102  m_sum2 += other.sum2();
103  m_sum11Matrix += other.sum11Matrix();
104  m_sum21Matrix += other.sum21Matrix();
105  m_sum22Matrix += other.sum22Matrix();
106  m_n += other.nEntries();
107  m_wTot += other.totalWeight();
108  return true;
109 }
110 
111 
112 TVectorD Averager::means() const
113 {
114  TVectorD m(lwb(), upb());
115  if (m_n == 0) {
116  cout << "No entries filled yet!" << endl;
117  return m;
118  }
119  for (int i = lwb(); i <= upb(); i++) m(i) = m_sum1(i)/m_wTot;
120  return m;
121 }
122 
123 
124 TVectorD Averager::meanErrors() const
125 {
126  TVectorD dm(lwb(), upb());
127  if (m_n == 0) {
128  cout << "No entries filled yet!" << endl;
129  return dm;
130  }
132  for (int i1 = lwb(); i1 <= upb(); i1++)
133  dm(i1) = safeSqrt(cm(i1, i1)/m_wTot);
134  return dm;
135 }
136 
137 
139 {
140  CovMatrix errs(lwb(), upb());
141  if (m_n == 0) {
142  cout << "No entries filled yet!" << endl;
143  return errs;
144  }
146  for (int i1 = lwb(); i1 <= upb(); i1++)
147  for (int i2 = lwb(); i2 <= upb(); i2++)
148  errs(i1, i2) = cm(i1, i2)/m_wTot;
149  return errs;
150 }
151 
152 
154 {
155  CovMatrix cm(lwb(), upb());
156  if (m_n == 0) {
157  cout << "No entries filled yet!" << endl;
158  return cm;
159  }
160  for (int i1 = lwb(); i1 <= upb(); i1++)
161  for (int i2 = lwb(); i2 <= upb(); i2++)
162  cm(i1, i2) = (m_sum11Matrix(i1, i2)/m_wTot) - (m_sum1(i1)/m_wTot)*(m_sum1(i2)/m_wTot);
163  return cm;
164 }
165 
166 
168 {
169  CovMatrix dcm(lwb(), upb());
170  if (m_n == 0) {
171  cout << "No entries filled yet!" << endl;
172  return dcm;
173  }
174 
175  for (int i1 = lwb(); i1 <= upb(); i1++)
176  for (int i2 = lwb(); i2 <= upb(); i2++) {
177  dcm(i1, i2) = safeSqrt((
178  (m_sum22Matrix(i1, i2)/m_wTot)
179  - 2*(m_sum21Matrix(i1, i2)/m_wTot)*(m_sum1(i2)/m_wTot) - 2*(m_sum21Matrix(i2, i1)/m_wTot)*(m_sum1(i1)/m_wTot)
180  + (m_sum11Matrix(i1, i1)/m_wTot)*(m_sum1(i2)/m_wTot)*(m_sum1(i2)/m_wTot) + (m_sum11Matrix(i2, i2)/m_wTot)*(m_sum1(i1)/m_wTot)*(m_sum1(i1)/m_wTot)
181  - (m_sum11Matrix(i1, i2)/m_wTot)*(m_sum11Matrix(i1, i2)/m_wTot)
182  + 6*(m_sum11Matrix(i1, i2)/m_wTot)*(m_sum1(i1)/m_wTot)*(m_sum1(i2)/m_wTot)
183  - 4*(m_sum1(i1)/m_wTot)*(m_sum1(i1)/m_wTot)*(m_sum1(i2)/m_wTot)*(m_sum1(i2)/m_wTot)
184  )/m_wTot);
185  }
186 
187  return dcm;
188 }
189 
190 
191 double Averager::mean(unsigned int i) const
192 {
193  if (!isInRange(i)) {
194  cout << "Index " << i << " out of range" << endl;
195  return 0;
196  }
197  return means()(i);
198 }
199 
200 
201 double Averager::meanError(unsigned int i) const
202 {
203  if (!isInRange(i)) {
204  cout << "Index " << i << " out of range" << endl;
205  return 0;
206  }
207  return meanErrors()(i);
208 }
209 
210 
211 double Averager::rms(unsigned int i) const
212 {
213  if (!isInRange(i)) {
214  cout << "Index " << i << " out of range" << endl;
215  return 0;
216  }
217  return safeSqrt(covarianceMatrix()(i, i));
218 }
219 
220 
221 double Averager::covariance(unsigned int i, unsigned j) const
222 {
223  if (!isInRange(i) || !isInRange(j)) {
224  cout << "Indices (" << i << ", " << j << ") out of range" << endl;
225  return 0;
226  }
227  return covarianceMatrix()(i, j);
228 }
229 
230 
231 double Averager::covarianceError(unsigned int i, unsigned j) const
232 {
233  if (!isInRange(i) || !isInRange(j)) {
234  cout << "Indices (" << i << ", " << j << ") out of range" << endl;
235  return 0;
236  }
237  return covarianceMatrixErrors()(i, j);
238 }
239 
240 
241 double Averager::safeSqrt(double x)
242 {
243  if (x < 0 && x > -1E-10) return 0;
244  return TMath::Sqrt(x);
245 }
LArSamples::Averager::lwb
int lwb() const
Definition: Averager.h:54
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
LArSamples::Averager::meanError
double meanError(unsigned int i) const
Definition: Averager.cxx:201
LArSamples::CovMatrix
TMatrixTSym< double > CovMatrix
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:11
LArSamples::IndexRange::hasSameRange
bool hasSameRange(int lw, int up) const
Definition: IndexRange.h:29
LArSamples::Averager::append
bool append(const Averager &other)
Definition: Averager.cxx:94
LArSamples::Averager::meanErrors
TVectorD meanErrors() const
Definition: Averager.cxx:124
LArSamples::Averager::fill
bool fill(const TVectorD &values, double w=1)
Definition: Averager.cxx:69
LArSamples::Averager::mean
double mean(unsigned int i) const
Definition: Averager.cxx:191
LArSamples::Averager::operator=
Averager & operator=(const Averager &other)
Definition: Averager.cxx:29
LArSamples
Definition: AbsShape.h:24
LArSamples::Averager::m_sum11Matrix
TMatrixD m_sum11Matrix
Definition: Averager.h:79
LArSamples::Averager::covarianceError
double covarianceError(unsigned int i, unsigned j) const
Definition: Averager.cxx:231
LArSamples::Averager
storage of the time histories of all the cells
Definition: Averager.h:23
LArSamples::Averager::covariance
double covariance(unsigned int i, unsigned j) const
Definition: Averager.cxx:221
x
#define x
Averager.h
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
LArSamples::Averager::means
TVectorD means() const
Definition: Averager.cxx:112
LArSamples::Averager::m_sum21Matrix
TMatrixD m_sum21Matrix
Definition: Averager.h:79
LArSamples::Averager::meanErrorMatrix
CovMatrix meanErrorMatrix() const
Definition: Averager.cxx:138
cm
const double cm
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloSimParametrization/tools/FCAL_ChannelMap.cxx:25
LArSamples::Averager::upb
int upb() const
Definition: Averager.h:55
LArSamples::IndexRange::isInRange
bool isInRange(int i) const
Definition: IndexRange.h:27
LArSamples::Averager::covarianceMatrixErrors
CovMatrix covarianceMatrixErrors() const
Definition: Averager.cxx:167
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
LArSamples::Averager::covarianceMatrix
CovMatrix covarianceMatrix() const
Definition: Averager.cxx:153
LArSamples::Averager::safeSqrt
static double safeSqrt(double x)
Definition: Averager.cxx:241
LArSamples::Averager::nEntries
unsigned int nEntries() const
Definition: Averager.h:57
LArSamples::Averager::m_xMin
std::vector< double > m_xMin
Definition: Averager.h:77
LArSamples::Averager::m_sum22Matrix
TMatrixD m_sum22Matrix
Definition: Averager.h:79
ReadCellNoiseFromCool.dm
dm
Definition: ReadCellNoiseFromCool.py:235
LArSamples::Averager::m_sum2
TVectorD m_sum2
Definition: Averager.h:78
LArSamples::Averager::prepare
bool prepare()
Definition: Averager.cxx:58
LArSamples::Averager::m_wTot
double m_wTot
Definition: Averager.h:74
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
LArSamples::Averager::m_n
unsigned int m_n
Definition: Averager.h:73
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
LArSamples::Averager::m_sum1
TVectorD m_sum1
Definition: Averager.h:78
LArSamples::Averager::add
bool add(double xMin=-DBL_MAX, double xMax=DBL_MAX)
Definition: Averager.cxx:46
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
LArSamples::Averager::m_xMax
std::vector< double > m_xMax
Definition: Averager.h:77
LArSamples::Averager::m_lwb
int m_lwb
Definition: Averager.h:75
LArSamples::Averager::Averager
Averager(unsigned int n=0)
Constructor
Definition: Averager.cxx:15
LArSamples::Averager::rms
double rms(unsigned int i) const
Definition: Averager.cxx:211