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