CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HBHEPulseShapeFlag.cc
Go to the documentation of this file.
1 //---------------------------------------------------------------------------
2 #include <string>
3 #include <vector>
4 #include <iostream>
5 #include <algorithm>
6 #include <fstream>
7 #include <cmath>
8 
9 //---------------------------------------------------------------------------
12 
14 
18 
22 //---------------------------------------------------------------------------
24 {
25  //
26  // Argumentless constructor; should not be used
27  //
28  // If arguments not properly specified for the constructor, I don't think
29  // we'd trust the flagging algorithm.
30  // Set the minimum charge threshold large enough so that nothing will be flagged.
31  //
32 
33  mMinimumChargeThreshold = 99999999;
34  mTS4TS5ChargeThreshold = 99999999;
35 }
36 //---------------------------------------------------------------------------
38  double TS4TS5ChargeThreshold,
39  unsigned int TrianglePeakTS,
40  const std::vector<double>& LinearThreshold,
41  const std::vector<double>& LinearCut,
42  const std::vector<double>& RMS8MaxThreshold,
43  const std::vector<double>& RMS8MaxCut,
44  const std::vector<double>& LeftSlopeThreshold,
45  const std::vector<double>& LeftSlopeCut,
46  const std::vector<double>& RightSlopeThreshold,
47  const std::vector<double>& RightSlopeCut,
48  const std::vector<double>& RightSlopeSmallThreshold,
49  const std::vector<double>& RightSlopeSmallCut,
50  const std::vector<double>& TS4TS5LowerThreshold,
51  const std::vector<double>& TS4TS5LowerCut,
52  const std::vector<double>& TS4TS5UpperThreshold,
53  const std::vector<double>& TS4TS5UpperCut,
54  bool UseDualFit,
55  bool TriangleIgnoreSlow)
56 {
57  //
58  // The constructor that should be used
59  //
60  // Copies various thresholds and limits and parameters to the class for future use.
61  // Also calls the Initialize() function
62  //
63 
64  mMinimumChargeThreshold = MinimumChargeThreshold;
65  mTS4TS5ChargeThreshold = TS4TS5ChargeThreshold;
66  mTrianglePeakTS = TrianglePeakTS;
67  mTriangleIgnoreSlow = TriangleIgnoreSlow;
68 
69  for(std::vector<double>::size_type i = 0; i < LinearThreshold.size() && i < LinearCut.size(); i++)
70  mLambdaLinearCut.push_back(std::pair<double, double>(LinearThreshold[i], LinearCut[i]));
71  sort(mLambdaLinearCut.begin(), mLambdaLinearCut.end());
72 
73  for(std::vector<double>::size_type i = 0; i < RMS8MaxThreshold.size() && i < RMS8MaxCut.size(); i++)
74  mLambdaRMS8MaxCut.push_back(std::pair<double, double>(RMS8MaxThreshold[i], RMS8MaxCut[i]));
76 
77  for(std::vector<double>::size_type i = 0; i < LeftSlopeThreshold.size() && i < LeftSlopeCut.size(); i++)
78  mLeftSlopeCut.push_back(std::pair<double, double>(LeftSlopeThreshold[i], LeftSlopeCut[i]));
79  sort(mLeftSlopeCut.begin(), mLeftSlopeCut.end());
80 
81  for(std::vector<double>::size_type i = 0; i < RightSlopeThreshold.size() && i < RightSlopeCut.size(); i++)
82  mRightSlopeCut.push_back(std::pair<double, double>(RightSlopeThreshold[i], RightSlopeCut[i]));
83  sort(mRightSlopeCut.begin(), mRightSlopeCut.end());
84 
85  for(std::vector<double>::size_type i = 0; i < RightSlopeSmallThreshold.size() && i < RightSlopeSmallCut.size(); i++)
86  mRightSlopeSmallCut.push_back(std::pair<double, double>(RightSlopeSmallThreshold[i], RightSlopeSmallCut[i]));
88 
89  for(std::vector<double>::size_type i = 0; i < TS4TS5UpperThreshold.size() && i < TS4TS5UpperCut.size(); i++)
90  mTS4TS5UpperCut.push_back(std::pair<double, double>(TS4TS5UpperThreshold[i], TS4TS5UpperCut[i]));
91  sort(mTS4TS5UpperCut.begin(), mTS4TS5UpperCut.end());
92 
93  for(std::vector<double>::size_type i = 0; i < TS4TS5LowerThreshold.size() && i < TS4TS5LowerCut.size(); i++)
94  mTS4TS5LowerCut.push_back(std::pair<double, double>(TS4TS5LowerThreshold[i], TS4TS5LowerCut[i]));
95  sort(mTS4TS5LowerCut.begin(), mTS4TS5LowerCut.end());
96 
97  mUseDualFit = UseDualFit;
98 
99  Initialize();
100 }
101 //---------------------------------------------------------------------------
103 {
104  // Dummy destructor - there's nothing to destruct by hand here
105 }
106 //---------------------------------------------------------------------------
108 {
109  // Dummy function in case something needs to be cleaned....but none right now
110 }
111 //---------------------------------------------------------------------------
113  const HBHEDataFrame &digi,
114  const HcalCoder &coder,
115  const HcalCalibrations &calib)
116 {
117  //
118  // Decide if a digi/pulse is good or bad using fit-based discriminants
119  //
120  // SetPulseShapeFlags determines the total charge in the digi.
121  // If the charge is above the minimum threshold, the code then
122  // runs the flag-setting algorithms to determine whether the
123  // flags should be set.
124  //
125 
126  // hack to exclude ieta=28/29 for the moment...
127  int abseta = hbhe.id().ietaAbs();
128  if(abseta == 28 || abseta == 29) return;
129 
130  CaloSamples Tool;
131  coder.adc2fC(digi, Tool);
132 
133  mCharge.clear(); // mCharge is a vector of (pedestal-subtracted) Charge values vs. time slice
134  mCharge.resize(digi.size());
135 
136  double TotalCharge = 0;
137 
138  for(int i = 0; i < digi.size(); ++i)
139  {
140  mCharge[i] = Tool[i] - calib.pedestal(digi.sample(i).capid());
141  TotalCharge += mCharge[i];
142  }
143 
144  // No flagging if TotalCharge is less than threshold
145  if(TotalCharge < mMinimumChargeThreshold)
146  return;
147 
148  double NominalChi2 = 0;
149  if (mUseDualFit == true)
150  NominalChi2=PerformDualNominalFit(mCharge);
151  else
152  NominalChi2=PerformNominalFit(mCharge);
153 
154  double LinearChi2 = PerformLinearFit(mCharge);
155 
156  double RMS8Max = CalculateRMS8Max(mCharge);
157  TriangleFitResult TriangleResult = PerformTriangleFit(mCharge);
158 
159  // Set the HBHEFlatNoise and HBHESpikeNoise flags
160  if(CheckPassFilter(TotalCharge, log(LinearChi2) - log(NominalChi2), mLambdaLinearCut, -1) == false)
162  if(CheckPassFilter(TotalCharge, log(RMS8Max) * 2 - log(NominalChi2), mLambdaRMS8MaxCut, -1) == false)
164 
165  // Set the HBHETriangleNoise flag
166  if ((int)mCharge.size() >= mTrianglePeakTS) // can't compute flag if peak TS isn't present; revise this at some point?
167  {
168  // initial values
169  double TS4Left = 1000;
170  double TS4Right = 1000;
171 
172  // Use 'if' statements to protect against slopes that are either 0 or very small
173  if (TriangleResult.LeftSlope > 1e-5)
174  TS4Left = mCharge[mTrianglePeakTS] / TriangleResult.LeftSlope;
175  if (TriangleResult.RightSlope < -1e-5)
176  TS4Right = mCharge[mTrianglePeakTS] / -TriangleResult.RightSlope;
177 
178  if(TS4Left > 1000 || TS4Left < -1000)
179  TS4Left = 1000;
180  if(TS4Right > 1000 || TS4Right < -1000)
181  TS4Right = 1000;
182 
183  if(mTriangleIgnoreSlow == false) // the slow-rising and slow-dropping edges won't be useful in 50ns/75ns
184  {
185  if(CheckPassFilter(mCharge[mTrianglePeakTS], TS4Left, mLeftSlopeCut, 1) == false)
187  else if(CheckPassFilter(mCharge[mTrianglePeakTS], TS4Right, mRightSlopeCut, 1) == false)
189  }
190 
191  // fast-dropping ones should be checked in any case
192  if(CheckPassFilter(mCharge[mTrianglePeakTS], TS4Right, mRightSlopeSmallCut, -1) == false)
194  }
195 
196  if(mCharge[4] + mCharge[5] > mTS4TS5ChargeThreshold && mTS4TS5ChargeThreshold>0) // silly protection against negative charge values
197  {
198  double TS4TS5 = (mCharge[4] - mCharge[5]) / (mCharge[4] + mCharge[5]);
199  if(CheckPassFilter(mCharge[4] + mCharge[5], TS4TS5, mTS4TS5UpperCut, 1) == false)
201  if(CheckPassFilter(mCharge[4] + mCharge[5], TS4TS5, mTS4TS5LowerCut, -1) == false)
203  }
204 }
205 //---------------------------------------------------------------------------
207 {
208  //
209  // Initialization: whatever preprocess is needed
210  //
211  // 1. Get the ideal pulse shape from CMSSW
212  //
213  // Since the HcalPulseShapes class stores the ideal pulse shape in terms of 256 numbers,
214  // each representing 1ns integral of the ideal pulse, here I'm taking out the vector
215  // by calling at() function.
216  //
217  // A cumulative distribution is formed and stored to save some time doing integration to TS later on
218  //
219  // 2. Reserve space for vector
220  //
221 
222  std::vector<double> PulseShape;
223 
224  HcalPulseShapes Shapes;
225  HcalPulseShapes::Shape HPDShape = Shapes.hbShape();
226 
227  PulseShape.reserve(350);
228  for(int i = 0; i < 200; i++)
229  PulseShape.push_back(HPDShape.at(i));
230  PulseShape.insert(PulseShape.begin(), 150, 0); // Safety margin of a lot of zeros in the beginning
231 
232  CumulativeIdealPulse.reserve(350);
233  CumulativeIdealPulse.clear();
234  CumulativeIdealPulse.push_back(0);
235  for(unsigned int i = 1; i < PulseShape.size(); i++)
236  CumulativeIdealPulse.push_back(CumulativeIdealPulse[i-1] + PulseShape[i]);
237 
238  // reserve space for vector
239  mCharge.reserve(10);
240 }
241 //---------------------------------------------------------------------------
243 {
244  //
245  // Perform a "triangle fit", and extract the slopes
246  //
247  // Left-hand side and right-hand side are not correlated to each other - do them separately
248  //
249 
251  result.Chi2 = 0;
252  result.LeftSlope = 0;
253  result.RightSlope = 0;
254 
255  int DigiSize = Charge.size();
256 
257  // right side, starting from TS4
258  double MinimumRightChi2 = 1000000;
259  double Numerator = 0;
260  double Denominator = 0;
261 
262  for(int iTS = mTrianglePeakTS + 2; iTS <= DigiSize; iTS++) // the place where first TS center in flat line
263  {
264  // fit a straight line to the triangle part
265  Numerator = 0;
266  Denominator = 0;
267 
268  for(int i = mTrianglePeakTS + 1; i < iTS; i++)
269  {
270  Numerator += (i - mTrianglePeakTS) * (Charge[i] - Charge[mTrianglePeakTS]);
271  Denominator += (i - mTrianglePeakTS) * (i - mTrianglePeakTS);
272  }
273 
274  double BestSlope = 0;
275  if (Denominator!=0) BestSlope = Numerator / Denominator;
276  if(BestSlope > 0)
277  BestSlope = 0;
278 
279  // check if the slope is reasonable
280  if(iTS != DigiSize)
281  {
282  // iTS starts at mTrianglePeak+2; denominator never zero
283  if(BestSlope > -1 * Charge[mTrianglePeakTS] / (iTS - mTrianglePeakTS))
284  BestSlope = -1 * Charge[mTrianglePeakTS] / (iTS - mTrianglePeakTS);
285  if(BestSlope < -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS))
286  BestSlope = -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS);
287  }
288  else
289  {
290  // iTS starts at mTrianglePeak+2; denominator never zero
291  if(BestSlope < -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS))
292  BestSlope = -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS);
293  }
294 
295  // calculate partial chi2
296 
297  // The shape I'm fitting is more like a tent than a triangle.
298  // After the end of triangle edge assume a flat line
299 
300  double Chi2 = 0;
301  for(int i = mTrianglePeakTS + 1; i < iTS; i++)
302  Chi2 += (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope)
303  * (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope);
304  for(int i = iTS; i < DigiSize; i++) // Assumes fit line = 0 for iTS > fit
305  Chi2 += Charge[i] * Charge[i];
306 
307  if(Chi2 < MinimumRightChi2)
308  {
309  MinimumRightChi2 = Chi2;
310  result.RightSlope = BestSlope;
311  }
312  } // end of right-hand side loop
313 
314  // left side
315  double MinimumLeftChi2 = 1000000;
316 
317  for(int iTS = 0; iTS < (int)mTrianglePeakTS; iTS++) // the first time after linear fit ends
318  {
319  // fit a straight line to the triangle part
320  Numerator = 0;
321  Denominator = 0;
322  for(int i = iTS; i < (int)mTrianglePeakTS; i++)
323  {
324  Numerator = Numerator + (i - mTrianglePeakTS) * (Charge[i] - Charge[mTrianglePeakTS]);
325  Denominator = Denominator + (i - mTrianglePeakTS) * (i - mTrianglePeakTS);
326  }
327 
328  double BestSlope = 0;
329  if (Denominator!=0) BestSlope = Numerator / Denominator;
330  if (BestSlope < 0)
331  BestSlope = 0;
332 
333  // check slope
334  if(iTS != 0)
335  {
336  // iTS must be >0 and < mTrianglePeakTS; slope never 0
337  if(BestSlope > Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS))
338  BestSlope = Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS);
339  if(BestSlope < Charge[mTrianglePeakTS] / (mTrianglePeakTS + 1 - iTS))
340  BestSlope = Charge[mTrianglePeakTS] / (mTrianglePeakTS + 1 - iTS);
341  }
342  else
343  {
344  if(BestSlope > Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS))
345  BestSlope = Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS);
346  }
347 
348  // calculate minimum chi2
349  double Chi2 = 0;
350  for(int i = 0; i < iTS; i++)
351  Chi2 += Charge[i] * Charge[i];
352  for(int i = iTS; i < (int)mTrianglePeakTS; i++)
353  Chi2 += (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope)
354  * (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope);
355 
356  if(MinimumLeftChi2 > Chi2)
357  {
358  MinimumLeftChi2 = Chi2;
359  result.LeftSlope = BestSlope;
360  }
361  } // end of left-hand side loop
362 
363  result.Chi2 = MinimumLeftChi2 + MinimumRightChi2;
364 
365  return result;
366 }
367 //---------------------------------------------------------------------------
368 double HBHEPulseShapeFlagSetter::PerformNominalFit(const std::vector<double> &Charge)
369 {
370  //
371  // Performs a fit to the ideal pulse shape. Returns best chi2
372  //
373  // A scan over different timing offset (for the ideal pulse) is carried out,
374  // and for each offset setting a one-parameter fit is performed
375  //
376 
377  int DigiSize = Charge.size();
378 
379  double MinimumChi2 = 100000;
380 
381  double F = 0;
382 
383  double SumF2 = 0;
384  double SumTF = 0;
385  double SumT2 = 0;
386 
387  for(int i = 0; i + 250 < (int)CumulativeIdealPulse.size(); i++)
388  {
390  continue;
391 
392  SumF2 = 0;
393  SumTF = 0;
394  SumT2 = 0;
395 
396  double ErrorTemp=0;
397  for(int j = 0; j < DigiSize; j++)
398  {
399  // get ideal pulse component for this time slice....
400  F = CumulativeIdealPulse[i+j*25+25] - CumulativeIdealPulse[i+j*25];
401 
402  ErrorTemp=Charge[j];
403  if (ErrorTemp<1) // protection against small charges
404  ErrorTemp=1;
405  // ...and increment various summations
406  SumF2 += F * F / ErrorTemp;
407  SumTF += F * Charge[j] / ErrorTemp;
408  SumT2 += fabs(Charge[j]);
409  }
410 
411  /*
412  chi2= sum((Charge[j]-aF)^2/|Charge[j]|
413  ( |Charge[j]| = assumed sigma^2 for Charge[j]; a bit wonky for Charge[j]<1 )
414  chi2 = sum(|Charge[j]|) - 2*sum(aF*Charge[j]/|Charge[j]|) +sum( a^2*F^2/|Charge[j]|)
415  chi2 minimimized when d(chi2)/da = 0:
416  a = sum(F*Charge[j])/sum(F^2)
417  ...
418  chi2= sum(|Q[j]|) - sum(Q[j]/|Q[j]|*F)*sum(Q[j]/|Q[j]|*F)/sum(F^2/|Q[j]|), where Q = Charge
419  chi2 = SumT2 - SumTF*SumTF/SumF2
420  */
421 
422  double Chi2 = SumT2 - SumTF * SumTF / SumF2;
423 
424  if(Chi2 < MinimumChi2)
425  MinimumChi2 = Chi2;
426  }
427 
428  // safety protection in case of perfect fit - don't want the log(...) to explode
429  if(MinimumChi2 < 1e-5)
430  MinimumChi2 = 1e-5;
431 
432  return MinimumChi2;
433 }
434 //---------------------------------------------------------------------------
435 double HBHEPulseShapeFlagSetter::PerformDualNominalFit(const std::vector<double> &Charge)
436 {
437  //
438  // Perform dual nominal fit and returns the chi2
439  //
440  // In this function we do a scan over possible "distance" (number of time slices between two components)
441  // and overall offset for the two components; first coarse, then finer
442  // All the fitting is done in the DualNominalFitSingleTry function
443  //
444 
445  double OverallMinimumChi2 = 1000000;
446 
447  int AvailableDistance[] = {-100, -75, -50, 50, 75, 100};
448 
449  // loop over possible pulse distances between two components
450  for(int k = 0; k < 6; k++)
451  {
452  double SingleMinimumChi2 = 1000000;
453  int MinOffset = 0;
454 
455  // scan coarsely through different offsets and find the minimum
456  for(int i = 0; i + 250 < (int)CumulativeIdealPulse.size(); i += 10)
457  {
458  double Chi2 = DualNominalFitSingleTry(Charge, i, AvailableDistance[k]);
459 
460  if(Chi2 < SingleMinimumChi2)
461  {
462  SingleMinimumChi2 = Chi2;
463  MinOffset = i;
464  }
465  }
466 
467  // around the minimum, scan finer for better a better minimum
468  for(int i = MinOffset - 15; i + 250 < (int)CumulativeIdealPulse.size() && i < MinOffset + 15; i++)
469  {
470  double Chi2 = DualNominalFitSingleTry(Charge, i, AvailableDistance[k]);
471  if(Chi2 < SingleMinimumChi2)
472  SingleMinimumChi2 = Chi2;
473  }
474 
475  // update overall minimum chi2
476  if(SingleMinimumChi2 < OverallMinimumChi2)
477  OverallMinimumChi2 = SingleMinimumChi2;
478  }
479 
480  return OverallMinimumChi2;
481 }
482 //---------------------------------------------------------------------------
483 double HBHEPulseShapeFlagSetter::DualNominalFitSingleTry(const std::vector<double> &Charge, int Offset, int Distance)
484 {
485  //
486  // Does a fit to dual signal pulse hypothesis given offset and distance of the two target pulses
487  //
488  // The only parameters to fit here are the two pulse heights of in-time and out-of-time components
489  // since offset is given
490  // The calculation here is based from writing down the Chi2 formula and minimize against the two parameters,
491  // ie., Chi2 = Sum{((T[i] - a1 * F1[i] - a2 * F2[i]) / (Sigma[i]))^2}, where T[i] is the input pulse shape,
492  // and F1[i], F2[i] are the two ideal pulse components
493  //
494 
495  int DigiSize = Charge.size();
496 
497  if(Offset < 0 || Offset + 250 >= (int)CumulativeIdealPulse.size())
498  return 1000000;
499  if(CumulativeIdealPulse[Offset+250] - CumulativeIdealPulse[Offset] < 1e-5)
500  return 1000000;
501 
502  static std::vector<double> F1;
503  static std::vector<double> F2;
504 
505  F1.resize(DigiSize);
506  F2.resize(DigiSize);
507 
508  double SumF1F1 = 0;
509  double SumF1F2 = 0;
510  double SumF2F2 = 0;
511  double SumTF1 = 0;
512  double SumTF2 = 0;
513 
514  double Error = 0;
515 
516  for(int j = 0; j < DigiSize; j++)
517  {
518  // this is the TS value for in-time component - no problem we can do a subtraction directly
519  F1[j] = CumulativeIdealPulse[Offset+j*25+25] - CumulativeIdealPulse[Offset+j*25];
520 
521  // However for the out-of-time component the index might go out-of-bound.
522  // Let's protect against this.
523 
524  int OffsetTemp = Offset + j * 25 + Distance;
525 
526  double C1 = 0; // lower-indexed value in the cumulative pulse shape
527  double C2 = 0; // higher-indexed value in the cumulative pulse shape
528 
529  if(OffsetTemp + 25 < (int)CumulativeIdealPulse.size() && OffsetTemp + 25 >= 0)
530  C1 = CumulativeIdealPulse[OffsetTemp+25];
531  if(OffsetTemp + 25 >= (int)CumulativeIdealPulse.size())
532  C1 = CumulativeIdealPulse[CumulativeIdealPulse.size()-1];
533  if(OffsetTemp < (int)CumulativeIdealPulse.size() && OffsetTemp >= 0)
534  C2 = CumulativeIdealPulse[OffsetTemp];
535  if(OffsetTemp >= (int)CumulativeIdealPulse.size())
536  C2 = CumulativeIdealPulse[CumulativeIdealPulse.size()-1];
537  F2[j] = C1 - C2;
538 
539  Error = Charge[j];
540  if(Error < 1)
541  Error = 1;
542 
543  SumF1F1 += F1[j] * F1[j] / Error;
544  SumF1F2 += F1[j] * F2[j] / Error;
545  SumF2F2 += F2[j] * F2[j] / Error;
546  SumTF1 += F1[j] * Charge[j] / Error;
547  SumTF2 += F2[j] * Charge[j] / Error;
548  }
549 
550  double Height = 0;
551  double Height2 = 0;
552  if (fabs(SumF1F2*SumF1F2-SumF1F1*SumF2F2)>1e-5)
553  {
554  Height = (SumF1F2 * SumTF2 - SumF2F2 * SumTF1) / (SumF1F2 * SumF1F2 - SumF1F1 * SumF2F2);
555  Height2 = (SumF1F2 * SumTF1 - SumF1F1 * SumTF2) / (SumF1F2 * SumF1F2 - SumF1F1 * SumF2F2);
556  }
557 
558  double Chi2 = 0;
559  for(int j = 0; j < DigiSize; j++)
560  {
561  double Error = Charge[j];
562  if(Error < 1)
563  Error = 1;
564 
565  double Residual = Height * F1[j] + Height2 * F2[j] - Charge[j];
566  Chi2 += Residual * Residual / Error;
567  }
568 
569  // Safety protection in case of zero
570  if(Chi2 < 1e-5)
571  Chi2 = 1e-5;
572 
573  return Chi2;
574 }
575 //---------------------------------------------------------------------------
576 double HBHEPulseShapeFlagSetter::CalculateRMS8Max(const std::vector<double> &Charge)
577 {
578  //
579  // CalculateRMS8Max
580  //
581  // returns "RMS" divided by the largest charge in the time slices
582  // "RMS" is calculated using all but the two largest time slices.
583  // The "RMS" is not quite the actual RMS (see note below), but the value is only
584  // used for determining max values, and is not quoted as the actual RMS anywhere.
585  //
586 
587  int DigiSize = Charge.size();
588 
589  if (DigiSize<=2) return 1e-5; // default statement when DigiSize is too small for useful RMS calculation
590  // Copy Charge vector again, since we are passing references around
591  std::vector<double> TempCharge = Charge;
592 
593  // Sort TempCharge vector from smallest to largest charge
594  sort(TempCharge.begin(), TempCharge.end());
595 
596  double Total = 0;
597  double Total2 = 0;
598  for(int i = 0; i < DigiSize - 2; i++)
599  {
600  Total = Total + TempCharge[i];
601  Total2 = Total2 + TempCharge[i] * TempCharge[i];
602  }
603 
604  // This isn't quite the RMS (both Total2 and Total*Total need to be
605  // divided by an extra (DigiSize-2) within the sqrt to get the RMS.)
606  // We're only using this value for relative comparisons, though; we
607  // aren't explicitly interpreting it as the RMS. It might be nice
608  // to either change the calculation or rename the variable in the future, though.
609 
610  double RMS = sqrt(Total2 - Total * Total / (DigiSize - 2));
611 
612  double RMS8Max = RMS / TempCharge[DigiSize-1];
613  if(RMS8Max < 1e-5) // protection against zero
614  RMS8Max = 1e-5;
615 
616  return RMS / TempCharge[DigiSize-1];
617 }
618 //---------------------------------------------------------------------------
619 double HBHEPulseShapeFlagSetter::PerformLinearFit(const std::vector<double> &Charge)
620 {
621  //
622  // Performs a straight-line fit over all time slices, and returns the chi2 value
623  //
624  // The calculation here is based from writing down the formula for chi2 and minimize
625  // with respect to the parameters in the fit, ie., slope and intercept of the straight line
626  // By doing two differentiation, we will get two equations, and the best parameters are determined by these
627  //
628 
629  int DigiSize = Charge.size();
630 
631  double SumTS2OverTi = 0;
632  double SumTSOverTi = 0;
633  double SumOverTi = 0;
634  double SumTiTS = 0;
635  double SumTi = 0;
636 
637  double Error2 = 0;
638  for(int i = 0; i < DigiSize; i++)
639  {
640  Error2 = Charge[i];
641  if(Charge[i] < 1)
642  Error2 = 1;
643 
644  SumTS2OverTi += 1.* i * i / Error2;
645  SumTSOverTi += 1.* i / Error2;
646  SumOverTi += 1. / Error2;
647  SumTiTS += Charge[i] * i / Error2;
648  SumTi += Charge[i] / Error2;
649  }
650 
651  double CM1 = SumTS2OverTi; // Coefficient in front of slope in equation 1
652  double CM2 = SumTSOverTi; // Coefficient in front of slope in equation 2
653  double CD1 = SumTSOverTi; // Coefficient in front of intercept in equation 1
654  double CD2 = SumOverTi; // Coefficient in front of intercept in equation 2
655  double C1 = SumTiTS; // Constant coefficient in equation 1
656  double C2 = SumTi; // Constant coefficient in equation 2
657 
658  // Denominators always non-zero by construction
659  double Slope = (C1 * CD2 - C2 * CD1) / (CM1 * CD2 - CM2 * CD1);
660  double Intercept = (C1 * CM2 - C2 * CM1) / (CD1 * CM2 - CD2 * CM1);
661 
662  // now that the best parameters are found, calculate chi2 from those
663  double Chi2 = 0;
664  for(int i = 0; i < DigiSize; i++)
665  {
666  double Deviation = Slope * i + Intercept - Charge[i];
667  double Error2 = Charge[i];
668  if(Charge[i] < 1)
669  Error2 = 1;
670  Chi2 += Deviation * Deviation / Error2;
671  }
672 
673  // safety protection in case of perfect fit
674  if(Chi2 < 1e-5)
675  Chi2 = 1e-5;
676 
677  return Chi2;
678 }
679 //---------------------------------------------------------------------------
681  double Discriminant,
682  std::vector<std::pair<double, double> > &Cuts,
683  int Side)
684 {
685  //
686  // Checks whether Discriminant value passes Cuts for the specified Charge. True if pulse is good.
687  //
688  // The "Cuts" pairs are assumed to be sorted in terms of size from small to large,
689  // where each "pair" = (Charge, Discriminant)
690  // "Side" is either positive or negative, which determines whether to discard the pulse if discriminant
691  // is greater or smaller than the cut value
692  //
693 
694  if(Cuts.size() == 0) // safety check that there are some cuts defined
695  return true;
696 
697  if(Charge <= Cuts[0].first) // too small to cut on
698  return true;
699 
700  int IndexLargerThanCharge = -1; // find the range it is falling in
701  for(int i = 1; i < (int)Cuts.size(); i++)
702  {
703  if(Cuts[i].first > Charge)
704  {
705  IndexLargerThanCharge = i;
706  break;
707  }
708  }
709 
710  double limit = 1000000;
711 
712  if(IndexLargerThanCharge == -1) // if charge is greater than the last entry, assume flat line
713  limit = Cuts[Cuts.size()-1].second;
714  else // otherwise, do a linear interpolation to find the cut position
715  {
716  double C1 = Cuts[IndexLargerThanCharge].first;
717  double C2 = Cuts[IndexLargerThanCharge-1].first;
718  double L1 = Cuts[IndexLargerThanCharge].second;
719  double L2 = Cuts[IndexLargerThanCharge-1].second;
720 
721  limit = (Charge - C1) / (C2 - C1) * (L2 - L1) + L1;
722  }
723 
724  if(Side > 0 && Discriminant > limit)
725  return false;
726  if(Side < 0 && Discriminant < limit)
727  return false;
728 
729  return true;
730 }
731 //---------------------------------------------------------------------------
732 
733 
int i
Definition: DBlmapReader.cc:9
std::vector< std::pair< double, double > > mLambdaLinearCut
float at(double time) const
int size() const
total number of samples in the digi
Definition: HBHEDataFrame.h:26
HcalDetId id() const
Definition: HBHERecHit.h:23
void setFlagField(uint32_t value, int base, int width=1)
Definition: CaloRecHit.cc:20
double pedestal(int fCapId) const
get pedestal for capid=0..3
double CalculateRMS8Max(const std::vector< double > &Charge)
std::vector< std::pair< double, double > > mTS4TS5LowerCut
double PerformNominalFit(const std::vector< double > &Charge)
double PerformDualNominalFit(const std::vector< double > &Charge)
uint16_t size_type
double PerformLinearFit(const std::vector< double > &Charge)
std::vector< std::pair< double, double > > mLeftSlopeCut
TriangleFitResult PerformTriangleFit(const std::vector< double > &Charge)
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
T sqrt(T t)
Definition: SSEVec.h:48
std::vector< std::pair< double, double > > mRightSlopeCut
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
bool first
Definition: L1TdeRCT.cc:79
bool CheckPassFilter(double Charge, double Discriminant, std::vector< std::pair< double, double > > &Cuts, int Side)
int k[5][pyjets_maxn]
int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.h:34
std::vector< double > mCharge
std::vector< std::pair< double, double > > mRightSlopeSmallCut
int capid() const
get the Capacitor id
Definition: HcalQIESample.h:26
const HcalQIESample & sample(int i) const
access a sample
Definition: HBHEDataFrame.h:39
double DualNominalFitSingleTry(const std::vector< double > &Charge, int Offset, int Distance)
const Shape & hbShape() const
virtual void adc2fC(const HBHEDataFrame &df, CaloSamples &lf) const =0
void SetPulseShapeFlags(HBHERecHit &hbhe, const HBHEDataFrame &digi, const HcalCoder &coder, const HcalCalibrations &calib)
std::vector< double > CumulativeIdealPulse
std::vector< std::pair< double, double > > mLambdaRMS8MaxCut
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
Definition: Chi2.h:17
std::vector< std::pair< double, double > > mTS4TS5UpperCut