ATLAS Offline Software
AbsShape.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 
6 
7 #include "LArCafJobs/AbsShape.h"
8 
9 #include "LArCafJobs/ShapeInfo.h"
10 #include "LArCafJobs/SimpleShape.h"
11 #include "TH1D.h"
12 #include "TGraphErrors.h"
13 #include "TMath.h"
14 #include "TVector.h"
15 #include "TMatrixD.h"
16 
17 #include <iostream>
18 using std::cout;
19 using std::endl;
20 
21 using namespace LArSamples;
22 
23 
24 double AbsShape::error(unsigned int i) const
25 {
26  return TMath::Sqrt(covariance(i, i));
27 }
28 
29 
30 double AbsShape::maxValue(bool withErrors) const
31 {
32  double maxValue = -DBL_MAX;
33  for (unsigned int i = 0; i < nPoints(); i++) {
34  double val = value(i) + (withErrors ? error(i) : 0);
35  if (val > maxValue) maxValue = val;
36  }
37  return maxValue;
38 }
39 
40 
41 double AbsShape::minValue(bool withErrors) const
42 {
43  double minValue = DBL_MAX;
44  for (unsigned int i = 0; i < nPoints(); i++) {
45  double val = value(i) - (withErrors ? error(i) : 0);
46  if (val < minValue) minValue = val;
47  }
48  return minValue;
49 }
50 
51 
53 {
54  int maxPosition = -1;
55  double maxValue = -1;
56  for (unsigned int i = 0; i < nPoints(); i++)
57  if (value(i) > maxValue) { maxPosition = i; maxValue = value(i); }
58  return maxPosition;
59 }
60 
62 {
63  int minPosition = -1;
64  double minValue = Definitions::none;
65  for (unsigned int i = 0; i < nPoints(); i++)
66  if (value(i) < minValue) { minPosition = i; minValue = value(i); }
67  return minPosition;
68 }
69 
70 
71 int AbsShape::findTimeInterval(double t) const
72 {
73  if (t < time(0)) return -1;
74  if (t > time(nPoints() - 1)) return int(nPoints()) - 1;
75 
76  for (unsigned int i = 0; i < nPoints() - 1; i++)
77  if (time(i) <= t && t < time(i+1)) return i;
78 
79  return nPoints() - 1;
80 }
81 
82 
83 int AbsShape::interpolate(double t, double& val, double& err) const
84 {
85  if (nPoints() < 2) return -1;
86 
87  int status = 0;
88  int i1 = findTimeInterval(t);
89 
90  if (i1 < 0) {
91  status = -1;
92  i1 = 0;
93  }
94 
95  if (i1 > int(nPoints()) - 2) {
96  status = 1;
97  i1 = nPoints() - 2;
98  }
99 
100  double delta = (t - time(i1))/(time(i1+1) - time(i1));
101  val = value(i1) + delta*(value(i1 + 1) - value(i1));
102  err = error(i1) + delta*(error(i1 + 1) - error(i1));
103  //cout << t << " " << i1 << " " << time(i1) << " " << delta << " " << value(i1) << " " << value(i1+1) << " " << val << " " << status << endl;
104  return status;
105 }
106 
107 
108 int AbsShape::interpolateDiff(double t, double& diff) const
109 {
110  int status = 0;
111  int i1 = findTimeInterval(t);
112  if (i1 < 0) {
113  i1 = 0;
114  status = -1;
115  }
116  if (i1 > int(nPoints()) - 1) {
117  i1 = nPoints() - 1;
118  status = 1;
119  }
120  if (i1 > 1 && i1 < int(nPoints()) - 2)
121  diff = (value(i1 - 2) - 8*value(i1 - 1) + 8*value(i1 + 1) - value(i1 + 2))/(time(i1 + 2) - time(i1 - 2))/3;
122  else
123  if (i1 > 0)
124  if (i1 < int(nPoints()) - 1)
125  diff = (value(i1 + 1) - value(i1 - 1))/(time(i1 + 1) - time(i1 - 1));
126  else
127  diff = (value(i1) - value(i1 - 1))/(time(i1) - time(i1 - 1));
128  else
129  diff = (value(i1 + 1) - value(i1))/(time(i1 + 1) - time(i1));
130 
131  return status;
132 }
133 
134 
135 TVectorD AbsShape::values(int lwb, int upb) const
136 {
137  if (lwb < 0) lwb = 0;
138  if (upb < 0) upb = (int)nPoints() - 1;
139  if (upb >= (int)nPoints()) upb = (int)nPoints() - 1;
140  TVectorD vals(lwb, upb);
141  for (int i = lwb; i <= upb; i++)
142  vals(i) = value(i);
143  return vals;
144 }
145 
146 
147 bool AbsShape::interpolate(const AbsShape& other, TVectorD& values, CovMatrix& errors, int lwb, int upb) const
148 {
149  if (lwb < 0) lwb = 0;
150  if (upb < 0) upb = other.nPoints() - 1;
151  values.ResizeTo(lwb, upb);
152  errors.ResizeTo(lwb, upb, lwb, upb);
153  int actualLwb = -1, actualUpb = upb;
154  for (int i = lwb; i <= upb; i++) {
155  double val, err;
156  int stat = interpolate(other.time(i), val, err);
157  if (stat == 0 && actualLwb < 0) actualLwb = i;
158  if (stat == +1) { actualUpb = i - 1; break; }
159  values(i) = val;
160  errors(i, i) = err*err;
161  }
162  values.ResizeTo(actualLwb, actualUpb);
163  errors.ResizeTo(actualLwb, actualUpb, actualLwb, actualUpb);
164  return (actualLwb >= 0);
165 }
166 
167 
168 bool AbsShape::interpolateDiff(const AbsShape& other, TVectorD& diffs, int lwb, int upb) const
169 {
170  if (lwb < 0) lwb = 0;
171  if (upb < 0) upb = other.nPoints() - 1;
172  diffs.ResizeTo(lwb, upb);
173  int actualLwb = -1, actualUpb = upb;
174  for (int i = lwb; i <= upb; i++) {
175  double diff;
176  int stat = interpolateDiff(other.time(i), diff);
177  if (stat == 0 && actualLwb < 0) actualLwb = i;
178  if (stat == +1) { actualUpb = i - 1; break; }
179  diffs(i) = diff;
180  }
181  diffs.ResizeTo(actualLwb, actualUpb);
182  return (actualLwb >= 0);
183 }
184 
185 
186 TGraphErrors* AbsShape::graph(bool timeInUnitOfSamples) const
187 {
188  TGraphErrors* graph = new TGraphErrors(nPoints());
189  double unit = (timeInUnitOfSamples ? Definitions::samplingInterval : 1);
190  for (unsigned int i = 0; i < nPoints(); i++) {
191  graph->SetPoint(i, time(i)/unit, value(i));
192  graph->SetPointError(i, 0, error(i));
193  }
194  return graph;
195 }
196 
197 
199  const CovMatrix& refErr,
200  bool withCorrs) const
201 {
202  if (lwb < 0 || upb < 0) { lwb = 0; upb = nPoints() - 1; }
203  CovMatrix c(lwb, upb);
204  for (int i = lwb; i <= upb; i++) {
205  for (int j = lwb; j <= upb; j++) {
206  double cov = (withCorrs || i == j ? covariance(i, j) : 0);
207  if (refErr.GetNrows() > 0) cov += refErr(i, j);
208  c(i, j) = cov;
209  }
210  }
211  return c;
212 }
213 
214 
216  const CovMatrix& refErr,
217  bool withCorrs) const
218 {
219  return covarianceMatrix(lwb, upb, refErr, withCorrs).Invert();
220 }
221 
222 
224  bool withCorrs) const
225 {
226  return covarianceMatrix(0, n - 1, CovMatrix(), withCorrs);
227 }
228 
229 
231  bool withCorrs) const
232 {
233  return covarianceMatrix(n, withCorrs).Invert();
234 }
235 
236 
237 SimpleShape* AbsShape::resample(unsigned int nPts) const
238 {
239  std::vector<double> values, errors;
240  double t0 = time(0);
241  double t1 = time(nPoints() - 1);
242  double dT = (t1 - t0)/nPts;
243  double t = t0;
244  for (unsigned int i = 0; i < nPts; i++, t += dT) {
245  double val, err;
246  int inRange = interpolate(t, val, err);
247  if (inRange != 0) return nullptr;
248  values.push_back(val);
249  errors.push_back(err);
250  }
251 
252  return new SimpleShape(values, errors, dT, t0);
253 }
254 
LArSamples::AbsShape::covariance
virtual double covariance(unsigned int i, unsigned int j) const =0
AbsShape.h
SimpleShape.h
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
LArSamples::CovMatrix
TMatrixTSym< double > CovMatrix
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:11
LArSamples::AbsShape::value
virtual double value(unsigned int i) const =0
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:201
LArSamples::SimpleShape
Definition: SimpleShape.h:25
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
LArSamples::AbsShape::minValue
double minValue(bool withErrors=false) const
Definition: AbsShape.cxx:41
LArSamples
Definition: AbsShape.h:24
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
LArSamples::AbsShape::interpolate
int interpolate(double time, double &value, double &error) const
Definition: AbsShape.cxx:83
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
ShapeInfo.h
LArSamples::AbsShape::error
virtual double error(unsigned int i) const
Definition: AbsShape.cxx:24
LArSamples::AbsShape::graph
TGraphErrors * graph(bool timeInUnitOfSamples=false) const
Definition: AbsShape.cxx:186
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArSamples::AbsShape::invCovarianceMatrix
CovMatrix invCovarianceMatrix(int lwb=-1, int upb=-1, const CovMatrix &refErr=CovMatrix(), bool withCorrs=true) const
Definition: AbsShape.cxx:215
beamspotman.n
n
Definition: beamspotman.py:731
LArSamples::Definitions::none
static const double none
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:17
LArSamples::AbsShape::findTimeInterval
int findTimeInterval(double time) const
Definition: AbsShape.cxx:71
LArSamples::AbsShape::resample
SimpleShape * resample(unsigned int nPts) const
Definition: AbsShape.cxx:237
beamspotman.stat
stat
Definition: beamspotman.py:266
inRange
bool inRange(const double *boundaries, const double value, const double tolerance=0.02)
Definition: LArSCIdVsIdTest.cxx:5
mergePhysValFiles.errors
list errors
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:43
LArSamples::AbsShape::maxValue
double maxValue(bool withErrors=false) const
Definition: AbsShape.cxx:30
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
LArSamples::AbsShape
Definition: AbsShape.h:28
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
unit
const PlainObject unit() const
This is a plugin that makes Eigen look like CLHEP & defines some convenience methods.
Definition: AmgMatrixBasePlugin.h:21
merge.status
status
Definition: merge.py:17
LArSamples::AbsShape::maxPosition
int maxPosition() const
Definition: AbsShape.cxx:52
LArSamples::AbsShape::covarianceMatrix
CovMatrix covarianceMatrix(int lwb=-1, int upb=-1, const CovMatrix &refErr=CovMatrix(), bool withCorrs=true) const
Definition: AbsShape.cxx:198
LArSamples::Definitions::samplingInterval
static const unsigned int samplingInterval
Definition: LArCalorimeter/LArCafJobs/LArCafJobs/Definitions.h:15
LArSamples::AbsShape::time
virtual double time(unsigned int i) const =0
LArSamples::AbsShape::minPosition
int minPosition() const
Definition: AbsShape.cxx:61
python.compressB64.c
def c
Definition: compressB64.py:93
LArSamples::AbsShape::values
TVectorD values(int lwb, int upb) const
Definition: AbsShape.cxx:135
LArSamples::AbsShape::interpolateDiff
int interpolateDiff(double time, double &diff) const
Definition: AbsShape.cxx:108
LArSamples::AbsShape::nPoints
virtual unsigned int nPoints() const =0
PlotCalibFromCool.vals
vals
Definition: PlotCalibFromCool.py:474