CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalNoiseMonitor.cc
Go to the documentation of this file.
1 #include <cmath>
2 #include <fstream>
3 #include <algorithm>
4 
6 
11 
14 
19 
22 
24 
26 {
27  Online_ = ps.getUntrackedParameter<bool>("online",false);
28  mergeRuns_ = ps.getUntrackedParameter<bool>("mergeRuns",false);
29  enableCleanup_ = ps.getUntrackedParameter<bool>("enableCleanup",false);
30  debug_ = ps.getUntrackedParameter<int>("debug",0);
31  prefixME_ = ps.getUntrackedParameter<std::string>("subSystemFolder","Hcal/");
32  if(prefixME_.substr(prefixME_.size()-1,prefixME_.size())!="/")
33  prefixME_.append("/");
34  subdir_ = ps.getUntrackedParameter<std::string>("TaskFolder","NoiseMonitor_Hcal");
35  if(subdir_.size()>0 && subdir_.substr(subdir_.size()-1,subdir_.size())!="/")
36  subdir_.append("/");
37  subdir_=prefixME_+subdir_;
38  AllowedCalibTypes_ = ps.getUntrackedParameter<std::vector<int> > ("AllowedCalibTypes");
39  skipOutOfOrderLS_ = ps.getUntrackedParameter<bool>("skipOutOfOrderLS",false);
40  NLumiBlocks_ = ps.getUntrackedParameter<int>("NLumiBlocks",4000);
41  makeDiagnostics_ = ps.getUntrackedParameter<bool>("makeDiagnostics",false);
42 
43  triggers_=ps.getUntrackedParameter<std::vector<std::string> >("nzsHLTnames");
44  //["HLT_HcalPhiSym","HLT_HcalNoise_8E29]
45  period_=ps.getUntrackedParameter<int>("NoiseeventPeriod",4096); //4096
47  hltresultsLabel_ = ps.getUntrackedParameter<edm::InputTag>("HLTResultsLabel");
48  tok_hbhe_ = consumes<HBHEDigiCollection>(ps.getUntrackedParameter<edm::InputTag>("hbheDigiLabel"));
49  tok_hbherec_ = consumes<HBHERecHitCollection>(ps.getUntrackedParameter<edm::InputTag>("hbheRechitLabel"));
50  tok_noise_ = consumes<reco::HcalNoiseRBXCollection>(ps.getUntrackedParameter<edm::InputTag>("noiseLabel"));
51 
52  mTrianglePeakTS = 4; // for now...
53 
54  mE2E10MinEnergy = ps.getUntrackedParameter<double>("E2E10MinEnergy");
55  mMinE2E10 = ps.getUntrackedParameter<double>("MinE2E10");
56  mMaxE2E10 = ps.getUntrackedParameter<double>("MaxE2E10");
57  mMaxHPDHitCount = ps.getUntrackedParameter<int>("MaxHPDHitCount");
58  mMaxHPDNoOtherHitCount = ps.getUntrackedParameter<int>("MaxHPDNoOtherHitCount");
59  mMaxADCZeros = ps.getUntrackedParameter<int>("MaxADCZeros");
60  mTotalZeroMinEnergy = ps.getUntrackedParameter<double>("TotalZeroMinEnergy");
61  setupDone_ = false;
62 }
63 
65 
67 {
68 }
69 
71 {
72  if(dbe_)
73  {
76  }
77 }
78 
80 {
81  if(debug_ > 1)
82  std::cout <<"HcalNoiseMonitor::beginRun"<< std::endl;
83 
85 
86  if(tevt_ == 0)
87  setup();
88 
89  if(mergeRuns_ == false)
90  reset();
91 
92  return;
93 }
94 
95 
97 {
98  if (setupDone_)
99  return;
100  setupDone_ = true;
102 
103  if(debug_ > 1)
104  std::cout << "<HcalNoiseMonitor::setup> Creating histograms" << std::endl;
105 
106  if(dbe_)
107  {
109 
110  // Fit-based
111  dbe_->setCurrentFolder(subdir_ + "DoubleChi2/");
112 
113  hNominalChi2 = dbe_->book1D("Nominal_fit_chi2", "Nominal fit chi2, total charge > 20 fC", 100, 0, 200);
114  hNominalChi2->setAxisTitle("Nominal fit #chi^{2}", 1);
115 
116  hLinearChi2 = dbe_->book1D("Linear_fit_chi2", "Linear fit chi2, total charge > 20 fC", 100, 0, 200);
117  hLinearChi2->setAxisTitle("Linear fit #chi^{2}", 1);
118 
119  hLinearTestStatistics = dbe_->book1D("Lambda_linear", "#Lambda_{linear}, total charge > 20 fC", 100, -10, 10);
120  hLinearTestStatistics->setAxisTitle("#Lambda_{linear}", 1);
121 
122  hRMS8OverMax = dbe_->book1D("RMS8_over_Max", "RMS8/max, total charge > 20 fC", 100, 0, 2);
123  hRMS8OverMax->setAxisTitle("RMS8/max", 1);
124 
125  hRMS8OverMaxTestStatistics = dbe_->book1D("Lambda_RMS8_over_max", "#Lambda_{RMS8/Max}, total charge > 20 fC",
126  100, -30, 10);
127  hRMS8OverMaxTestStatistics->setAxisTitle("#Lambda_{RMS8/Max}", 1);
128 
129  hLambdaLinearVsTotalCharge = dbe_->book2D("Lambda_linear_vs_total_charge", "#Lambda_{Linear}",
130  50, -5, 5, 25, 0, 500);
131  hLambdaLinearVsTotalCharge->setAxisTitle("#Lambda_{linear}", 1);
132  hLambdaLinearVsTotalCharge->setAxisTitle("Total charge", 2);
133 
134  hLambdaRMS8MaxVsTotalCharge = dbe_->book2D("Lambda_RMS8Max_vs_total_charge", "#Lambda_{RMS8/Max}",
135  50, -15, 5, 25, 0, 500);
136  hLambdaRMS8MaxVsTotalCharge->setAxisTitle("#Lambda_{RMS8/Max}", 1);
137  hLambdaRMS8MaxVsTotalCharge->setAxisTitle("Total charge", 2);
138 
139  hTriangleLeftSlopeVsTS4 = dbe_->book2D("Triangle_fit_left_slope",
140  "Triangle fit left distance vs. TS4", 50, 0, 10, 25, 0, 500);
141  hTriangleLeftSlopeVsTS4->setAxisTitle("Left slope", 1);
142  hTriangleLeftSlopeVsTS4->setAxisTitle("Peak time slice", 2);
143 
144  hTriangleRightSlopeVsTS4 = dbe_->book2D("Triangle_fit_right_slope",
145  "Triangle fit right distance vs. peak time slice", 50, 0, 10, 25, 0, 500);
146  hTriangleRightSlopeVsTS4->setAxisTitle("Left slope", 1);
147  hTriangleRightSlopeVsTS4->setAxisTitle("Peak time slice", 2);
148 
149  SetupEtaPhiHists(hFailLinearEtaPhi, "Fail_linear_Eta_Phi_Map", "");
150  SetupEtaPhiHists(hFailRMSMaxEtaPhi, "Fail_RMS8Max_Eta_Phi_Map", "");
151  SetupEtaPhiHists(hFailTriangleEtaPhi, "Fail_triangle_Eta_Phi_Map", "");
152 
153  // High-level isolation filter
154  dbe_->setCurrentFolder(subdir_ + "IsolationVariable/");
155 
156  SetupEtaPhiHists(hFailIsolationEtaPhi, "Fail_isolation_Eta_Phi_Map", "");
157 
158  // TS4 vs. TS5 variable
159  dbe_->setCurrentFolder(subdir_ + "TS4TS5Variable/");
160 
161  hTS4TS5RelativeDifference = dbe_->book1D("TS4_TS5_relative_difference",
162  "(TS4-TS5)/(TS4+TS5), total charge > 20 fC", 100, -1, 1);
163  hTS4TS5RelativeDifference->setAxisTitle("(TS4 - TS5) / (TS4 + TS5)", 1);
164 
165  hTS4TS5RelativeDifferenceVsCharge = dbe_->book2D("TS4_TS5_relative_difference_charge",
166  "(TS4-TS5)/(TS4+TS5) vs. Charge", 25, 0, 400, 75, -1, 1);
168  hTS4TS5RelativeDifferenceVsCharge->setAxisTitle("(TS4 - TS5) / (TS4 + TS5)", 2);
169 
170  // Noise summary object
171  dbe_->setCurrentFolder(subdir_ + "NoiseMonitoring/");
172 
173  hMaxZeros = dbe_->book1D("Max_Zeros", "Max zeros", 15, -0.5, 14.5);
174 
175  hTotalZeros = dbe_->book1D("Total_Zeros", "Total zeros", 15, -0.5, 14.5);
176 
177  hE2OverE10Digi = dbe_->book1D("E2OverE10Digi", "E2/E10 of the highest digi in an HPD", 100, 0, 2);
178 
179  hE2OverE10Digi5 = dbe_->book1D("E2OverE10Digi5", "E2/E10 of the highest 5 digi in an HPD", 100, 0, 2);
180 
181  hE2OverE10RBX = dbe_->book1D("E2OverE10RBX", "E2/E10 of RBX", 100, 0, 2);
182 
183  hHPDHitCount = dbe_->book1D("HPDHitCount", "HPD hit count (1.5 GeV)", 19, -0.5, 18.5);
184 
185  hRBXHitCount = dbe_->book1D("RBXHitCount", "Number of hits in RBX", 74, -0.5, 73.5);
186 
187  hHcalNoiseCategory = dbe_->book1D("Hcal_noise_category", "Hcal noise category", 10, 0.5, 10.5);
188  hHcalNoiseCategory->setBinLabel(1, "RBX noise", 1);
189  hHcalNoiseCategory->setBinLabel(2, "RBX pedestal flatter", 1);
190  hHcalNoiseCategory->setBinLabel(3, "RBX pedestal sharper", 1);
191  hHcalNoiseCategory->setBinLabel(4, "RBX flash large hit count", 1);
192  hHcalNoiseCategory->setBinLabel(5, "RBX flash small hit count", 1);
193  hHcalNoiseCategory->setBinLabel(7, "HPD discharge", 1);
194  hHcalNoiseCategory->setBinLabel(8, "HPD ion feedback", 1);
195 
196  hBadZeroRBX = dbe_->book1D("BadZeroRBX", "RBX with bad ADC zero counts", 72, 0.5, 72.5);
197  hBadCountHPD = dbe_->book1D("BadCountHPD", "HPD with bad hit counts", 72 * 4, 0.5, 72 * 4 + 0.5);
198  hBadNoOtherCountHPD = dbe_->book1D("BadNoOtherCountHPD", "HPD with bad \"no other\" hit counts", 72 * 4, 0.5, 72 * 4 + 0.5);
199  hBadE2E10RBX = dbe_->book1D("BadE2E10RBX", "RBX with bad E2/E10 value", 72, 0.5, 72.5);
200  }
201 
202  ReadHcalPulse();
203 
204  return;
205 }
206 
207 
209 {
211  iEvent.getByToken(tok_hbhe_,hHBHEDigis);
212 
213  edm::ESHandle<HcalDbService> hConditions;
214  iSetup.get<HcalDbRecord>().get(hConditions);
215 
217  iEvent.getByToken(tok_hbherec_, hRecHits);
218 
220  iEvent.getByToken(tok_noise_,hRBXCollection);
221 
222  HcalBaseDQMonitor::analyze(iEvent, iSetup);
223 
224  if(dbe_ == NULL)
225  {
226  if(debug_ > 0)
227  std::cout << "HcalNoiseMonitor::processEvent DQMStore not instantiated!!!"<< std::endl;
228  return;
229  }
230 
231  // loop over digis
232  for(HBHEDigiCollection::const_iterator iter = hHBHEDigis->begin(); iter != hHBHEDigis->end(); iter++)
233  {
234  HcalDetId id = iter->id();
235  const HcalCalibrations &Calibrations = hConditions->getHcalCalibrations(id);
236  const HcalQIECoder *ChannelCoder = hConditions->getHcalCoder(id);
237  const HcalQIEShape *Shape = hConditions->getHcalShape(ChannelCoder);
238  HcalCoderDb Coder(*ChannelCoder, *Shape);
239  CaloSamples Tool;
240  Coder.adc2fC(*iter, Tool);
241 
242  // int ieta = id.ieta();
243  // int iphi = id.iphi();
244  // int depth = id.depth();
245 
246  double Charge[10] = {0};
247  for(int i = 0; i < iter->size(); i++)
248  Charge[i] = Tool[i] - Calibrations.pedestal(iter->sample(i).capid());
249 
250  double TotalCharge = 0;
251  for(int i = 0; i < 10; i++)
252  TotalCharge = TotalCharge + Charge[i];
253 
254  if(TotalCharge > 20)
255  {
256  double NominalChi2 = 10000000;
257  NominalChi2 = PerformNominalFit(Charge);
258 
259  double LinearChi2 = PerformLinearFit(Charge);
260  double RMS8Max = CalculateRMS8Max(Charge);
261  TriangleFitResult TriangleResult = PerformTriangleFit(Charge);
262 
263  double TS4LeftSlope = 100000;
264  double TS4RightSlope = 100000;
265 
266  if(TriangleResult.LeftSlope > 1e-5)
267  TS4LeftSlope = Charge[4] / fabs(TriangleResult.LeftSlope);
268  if(TriangleResult.RightSlope < -1e-5)
269  TS4RightSlope = Charge[4] / fabs(TriangleResult.RightSlope);
270 
271  if(TS4LeftSlope < -1000 || TS4LeftSlope > 1000)
272  TS4LeftSlope = 1000;
273  if(TS4RightSlope < -1000 || TS4RightSlope > 1000)
274  TS4RightSlope = 1000;
275 
276  hNominalChi2->Fill(NominalChi2);
277  hLinearChi2->Fill(LinearChi2);
278  hLinearTestStatistics->Fill(log(LinearChi2) - log(NominalChi2));
279  hRMS8OverMax->Fill(RMS8Max);
280  hRMS8OverMaxTestStatistics->Fill(log(RMS8Max) - log(NominalChi2));
281 
282  hLambdaLinearVsTotalCharge->Fill(log(LinearChi2) - log(NominalChi2), TotalCharge);
283  hLambdaRMS8MaxVsTotalCharge->Fill(log(RMS8Max) - log(NominalChi2), TotalCharge);
284  hTriangleLeftSlopeVsTS4->Fill(TS4LeftSlope, Charge[4]);
285  hTriangleRightSlopeVsTS4->Fill(TS4RightSlope, Charge[4]);
286  }
287 
288  if(Charge[4] + Charge[5] > 1e-5)
289  {
290  hTS4TS5RelativeDifference->Fill((Charge[4] - Charge[5]) / (Charge[4] + Charge[5]));
291  hTS4TS5RelativeDifferenceVsCharge->Fill(TotalCharge, (Charge[4] - Charge[5]) / (Charge[4] + Charge[5]));
292  }
293  }
294 
295  // loop over rechits - noise bits (fit-based, isolation)
296  for(HBHERecHitCollection::const_iterator iter = hRecHits->begin(); iter != hRecHits->end(); iter++)
297  {
298  HcalDetId id = iter->id();
299 
300  int ieta = id.ieta();
301  int iphi = id.iphi();
302  int depth = id.depth();
303 
304  if(iter->flagField(HcalCaloFlagLabels::HBHEFlatNoise) == 1)
305  hFailLinearEtaPhi.depth[depth-1]->Fill(ieta, iphi);
306 
307  if(iter->flagField(HcalCaloFlagLabels::HBHESpikeNoise) == 1)
308  hFailRMSMaxEtaPhi.depth[depth-1]->Fill(ieta, iphi);
309 
310  if(iter->flagField(HcalCaloFlagLabels::HBHETriangleNoise) == 1)
311  hFailTriangleEtaPhi.depth[depth-1]->Fill(ieta, iphi);
312 
313  if(iter->flagField(HcalCaloFlagLabels::HBHEIsolatedNoise) == 1)
314  hFailIsolationEtaPhi.depth[depth-1]->Fill(ieta, iphi);
315  }
316 
317  // Code analagous to Yifei's
318  for(reco::HcalNoiseRBXCollection::const_iterator rbx = hRBXCollection->begin();
319  rbx != hRBXCollection->end(); rbx++)
320  {
321  const reco::HcalNoiseRBX RBX = *rbx;
322 
323  int NumberRBXHits = RBX.numRecHits(1.5);
324  double RBXEnergy = RBX.recHitEnergy(1.5);
325  double RBXE2 = RBX.allChargeHighest2TS();
326  double RBXE10 = RBX.allChargeTotal();
327 
328  std::vector<reco::HcalNoiseHPD> HPDs = RBX.HPDs();
329 
330  int RBXID = RBX.idnumber();
331 
332  if(RBXEnergy > mTotalZeroMinEnergy && RBX.totalZeros() >= mMaxADCZeros)
333  hBadZeroRBX->Fill(RBXID);
334  if(RBXEnergy > mE2E10MinEnergy && RBXE10 > 1e-5 && (RBXE2 / RBXE10 > mMaxE2E10 || RBXE2 / RBXE10 < mMinE2E10))
335  hBadE2E10RBX->Fill(RBXID);
336  for(std::vector<reco::HcalNoiseHPD>::const_iterator hpd = HPDs.begin(); hpd != HPDs.end(); hpd++)
337  {
338  reco::HcalNoiseHPD HPD = *hpd;
339  int HPDHitCount = HPD.numRecHits(1.5);
340  if(HPDHitCount >= mMaxHPDHitCount)
341  hBadCountHPD->Fill(HPD.idnumber());
342  if(HPDHitCount == NumberRBXHits && HPDHitCount >= mMaxHPDNoOtherHitCount)
344  }
345 
346  if(NumberRBXHits == 0 || RBXEnergy <= 10)
347  continue;
348 
349  hRBXHitCount->Fill(NumberRBXHits);
350 
351  hMaxZeros->Fill(RBX.maxZeros());
352  hTotalZeros->Fill(RBX.totalZeros());
353 
354  double HighestHPDEnergy = 0;
355  int HighestHPDHits = 0;
356 
357  for(std::vector<reco::HcalNoiseHPD>::const_iterator hpd = HPDs.begin(); hpd != HPDs.end(); hpd++)
358  {
359  reco::HcalNoiseHPD HPD = *hpd;
360 
361  if(HPD.recHitEnergy(1.5) > HighestHPDEnergy)
362  {
363  HighestHPDEnergy = HPD.recHitEnergy(1.5);
364  HighestHPDHits = HPD.numRecHits(1.5);
365  }
366 
367  if(HPD.numRecHits(5) < 1)
368  continue;
369 
370  if(HPD.bigChargeTotal() > 1e-5)
372  if(HPD.big5ChargeTotal() > 1e-5)
374 
375  hHPDHitCount->Fill(HPD.numRecHits(1.5));
376  }
377 
378  int NoiseCategory = 0;
379  bool IsRBXNoise = false;
380  // bool IsHPDNoise = false;
381  // bool IsHPDIonFeedback = false;
382  // bool IsHPDDischarge = false;
383 
384  if(RBXEnergy > 1e-5 && HighestHPDEnergy / RBXEnergy > 0.98)
385  {
386  // IsHPDNoise = true;
387 
388  if(HighestHPDHits >= 9)
389  {
390  // IsHPDDischarge = true;
391  NoiseCategory = 7;
392  }
393  else
394  {
395  // IsHPDIonFeedback = true;
396  NoiseCategory = 8;
397  }
398  }
399  else
400  {
401  IsRBXNoise = true;
402  NoiseCategory = 1;
403 
404  if(RBXE10 > 1e-5)
405  {
406  if(RBXE2 / RBXE10 < 0.33)
407  NoiseCategory = 2;
408  else if(RBXE2 / RBXE10 < 0.8)
409  NoiseCategory = 3;
410  else if(RBXE2 / RBXE10 > 0.8 && NumberRBXHits > 10)
411  NoiseCategory = 4;
412  else if(RBXE2 / RBXE10 > 0.8 && NumberRBXHits < 10) // [hic]
413  NoiseCategory = 5;
414  }
415  }
416 
417  hHcalNoiseCategory->Fill(NoiseCategory);
418 
419  if(IsRBXNoise == true && RBXE10 > 1e-5)
420  hE2OverE10RBX->Fill(RBXE2 / RBXE10);
421  }
422 
423  return;
424 }
425 
426 double HcalNoiseMonitor::PerformNominalFit(double Charge[10])
427 {
428  //
429  // Performs a fit to the ideal pulse shape. Returns best chi2
430  //
431  // A scan over different timing offset (for the ideal pulse) is carried out,
432  // and for each offset setting a one-parameter fit is performed
433  //
434 
435  int DigiSize = 10;
436 
437  double MinimumChi2 = 100000;
438 
439  double F = 0;
440 
441  double SumF2 = 0;
442  double SumTF = 0;
443  double SumT2 = 0;
444 
445  for(int i = 0; i + 250 < (int)CumulativeIdealPulse.size(); i++)
446  {
448  continue;
449 
450  SumF2 = 0;
451  SumTF = 0;
452  SumT2 = 0;
453 
454  for(int j = 0; j < DigiSize; j++)
455  {
456  // get ideal pulse component for this time slice....
457  F = CumulativeIdealPulse[i+j*25+25] - CumulativeIdealPulse[i+j*25];
458 
459  double Error2 = Charge[j];
460  if(Error2 < 1)
461  Error2 = 1;
462 
463  // ...and increment various summations
464  SumF2 += F * F / Error2;
465  SumTF += F * Charge[j] / Error2;
466  SumT2 += Charge[j] * Charge[j] / Error2;
467  }
468 
469  /* chi2= sum((Charge[j]-aF)^2/|Charge[j]|
470  ( |Charge[j]| = assumed sigma^2 for Charge[j]; a bit wonky for Charge[j]<1 )
471  chi2 = sum(|Charge[j]|) - 2*sum(aF*Charge[j]/|Charge[j]|) +sum( a^2*F^2/|Charge[j]|)
472  chi2 minimimized when d(chi2)/da = 0:
473  a = sum(F*Charge[j])/sum(F^2)
474  ...
475  chi2= sum(|Q[j]|) - sum(Q[j]/|Q[j]|*F)*sum(Q[j]/|Q[j]|*F)/sum(F^2/|Q[j]|), where Q = Charge
476  chi2 = SumT2 - SumTF*SumTF/SumF2
477  */
478 
479  double Chi2 = SumT2 - SumTF * SumTF / SumF2;
480 
481  if(Chi2 < MinimumChi2)
482  MinimumChi2 = Chi2;
483  }
484 
485  // safety protection in case of perfect fit - don't want the log(...) to explode
486  if(MinimumChi2 < 1e-5)
487  MinimumChi2 = 1e-5;
488 
489  return MinimumChi2;
490 }
491 
493 {
494  //
495  // Perform dual nominal fit and returns the chi2
496  //
497  // In this function we do a scan over possible "distance" (number of time slices between two components)
498  // and overall offset for the two components; first coarse, then finer
499  // All the fitting is done in the DualNominalFitSingleTry function
500  //
501 
502  double OverallMinimumChi2 = 1000000;
503 
504  int AvailableDistance[] = {-100, -75, -50, 50, 75, 100};
505 
506  // loop over possible pulse distances between two components
507  for(int k = 0; k < 6; k++)
508  {
509  double SingleMinimumChi2 = 1000000;
510  int MinOffset = 0;
511 
512  // scan coarsely through different offsets and find the minimum
513  for(int i = 0; i + 250 < (int)CumulativeIdealPulse.size(); i += 10)
514  {
515  double Chi2 = DualNominalFitSingleTry(Charge, i, AvailableDistance[k]);
516 
517  if(Chi2 < SingleMinimumChi2)
518  {
519  SingleMinimumChi2 = Chi2;
520  MinOffset = i;
521  }
522  }
523 
524  // around the minimum, scan finer for better a better minimum
525  for(int i = MinOffset - 15; i + 250 < (int)CumulativeIdealPulse.size() && i < MinOffset + 15; i++)
526  {
527  double Chi2 = DualNominalFitSingleTry(Charge, i, AvailableDistance[k]);
528  if(Chi2 < SingleMinimumChi2)
529  SingleMinimumChi2 = Chi2;
530  }
531 
532  // update overall minimum chi2
533  if(SingleMinimumChi2 < OverallMinimumChi2)
534  OverallMinimumChi2 = SingleMinimumChi2;
535  }
536 
537  return OverallMinimumChi2;
538 }
539 
540 double HcalNoiseMonitor::DualNominalFitSingleTry(double Charge[10], int Offset, int Distance)
541 {
542  //
543  // Does a fit to dual signal pulse hypothesis given offset and distance of the two target pulses
544  //
545  // The only parameters to fit here are the two pulse heights of in-time and out-of-time components
546  // since offset is given
547  // The calculation here is based from writing down the Chi2 formula and minimize against the two parameters,
548  // ie., Chi2 = Sum{((T[i] - a1 * F1[i] - a2 * F2[i]) / (Sigma[i]))^2}, where T[i] is the input pulse shape,
549  // and F1[i], F2[i] are the two ideal pulse components
550  //
551 
552  int DigiSize = 10;
553 
554  if(Offset < 0 || Offset + 250 >= (int)CumulativeIdealPulse.size())
555  return 1000000;
556  if(CumulativeIdealPulse[Offset+250] - CumulativeIdealPulse[Offset] < 1e-5)
557  return 1000000;
558 
559  static std::vector<double> F1;
560  static std::vector<double> F2;
561 
562  F1.resize(DigiSize);
563  F2.resize(DigiSize);
564 
565  double SumF1F1 = 0;
566  double SumF1F2 = 0;
567  double SumF2F2 = 0;
568  double SumTF1 = 0;
569  double SumTF2 = 0;
570 
571  double Error = 0;
572 
573  for(int j = 0; j < DigiSize; j++)
574  {
575  // this is the TS value for in-time component - no problem we can do a subtraction directly
576  F1[j] = CumulativeIdealPulse[Offset+j*25+25] - CumulativeIdealPulse[Offset+j*25];
577 
578  // However for the out-of-time component the index might go out-of-bound.
579  // Let's protect against this.
580 
581  int OffsetTemp = Offset + j * 25 + Distance;
582 
583  double C1 = 0; // lower-indexed value in the cumulative pulse shape
584  double C2 = 0; // higher-indexed value in the cumulative pulse shape
585 
586  if(OffsetTemp + 25 < (int)CumulativeIdealPulse.size() && OffsetTemp + 25 >= 0)
587  C1 = CumulativeIdealPulse[OffsetTemp+25];
588  if(OffsetTemp + 25 >= (int)CumulativeIdealPulse.size())
589  C1 = CumulativeIdealPulse[CumulativeIdealPulse.size()-1];
590  if(OffsetTemp < (int)CumulativeIdealPulse.size() && OffsetTemp >= 0)
591  C2 = CumulativeIdealPulse[OffsetTemp];
592  if(OffsetTemp >= (int)CumulativeIdealPulse.size())
593  C2 = CumulativeIdealPulse[CumulativeIdealPulse.size()-1];
594  F2[j] = C1 - C2;
595 
596  Error = Charge[j];
597  if(Error < 1)
598  Error = 1;
599 
600  SumF1F1 += F1[j] * F1[j] / Error;
601  SumF1F2 += F1[j] * F2[j] / Error;
602  SumF2F2 += F2[j] * F2[j] / Error;
603  SumTF1 += F1[j] * Charge[j] / Error;
604  SumTF2 += F2[j] * Charge[j] / Error;
605  }
606 
607  double Height = (SumF1F2 * SumTF2 - SumF2F2 * SumTF1) / (SumF1F2 * SumF1F2 - SumF1F1 * SumF2F2);
608  double Height2 = (SumF1F2 * SumTF1 - SumF1F1 * SumTF2) / (SumF1F2 * SumF1F2 - SumF1F1 * SumF2F2);
609 
610  double Chi2 = 0;
611  for(int j = 0; j < DigiSize; j++)
612  {
613  double Error = Charge[j];
614  if(Error < 1)
615  Error = 1;
616 
617  double Residual = Height * F1[j] + Height2 * F2[j] - Charge[j];
618  Chi2 += Residual * Residual / Error;
619  }
620 
621  // Safety protection in case of zero
622  if(Chi2 < 1e-5)
623  Chi2 = 1e-5;
624 
625  return Chi2;
626 }
627 
628 double HcalNoiseMonitor::PerformLinearFit(double Charge[10])
629 {
630  //
631  // Performs a straight-line fit over all time slices, and returns the chi2 value
632  //
633  // The calculation here is based from writing down the formula for chi2 and minimize
634  // with respect to the parameters in the fit, ie., slope and intercept of the straight line
635  // By doing two differentiation, we will get two equations, and the best parameters are determined by these
636  //
637 
638  int DigiSize = 10;
639 
640  double SumTS2OverTi = 0;
641  double SumTSOverTi = 0;
642  double SumOverTi = 0;
643  double SumTiTS = 0;
644  double SumTi = 0;
645 
646  double Error2 = 0;
647  for(int i = 0; i < DigiSize; i++)
648  {
649  Error2 = Charge[i];
650  if(Charge[i] < 1)
651  Error2 = 1;
652 
653  SumTS2OverTi += 1.* i * i / Error2;
654  SumTSOverTi += 1.* i / Error2;
655  SumOverTi += 1. / Error2;
656  SumTiTS += Charge[i] * i / Error2;
657  SumTi += Charge[i] / Error2;
658  }
659 
660  double CM1 = SumTS2OverTi; // Coefficient in front of slope in equation 1
661  double CM2 = SumTSOverTi; // Coefficient in front of slope in equation 2
662  double CD1 = SumTSOverTi; // Coefficient in front of intercept in equation 1
663  double CD2 = SumOverTi; // Coefficient in front of intercept in equation 2
664  double C1 = SumTiTS; // Constant coefficient in equation 1
665  double C2 = SumTi; // Constant coefficient in equation 2
666 
667  double Slope = (C1 * CD2 - C2 * CD1) / (CM1 * CD2 - CM2 * CD1);
668  double Intercept = (C1 * CM2 - C2 * CM1) / (CD1 * CM2 - CD2 * CM1);
669 
670  // now that the best parameters are found, calculate chi2 from those
671  double Chi2 = 0;
672  for(int i = 0; i < DigiSize; i++)
673  {
674  double Deviation = Slope * i + Intercept - Charge[i];
675  double Error2 = Charge[i];
676  if(Charge[i] < 1)
677  Error2 = 1;
678  Chi2 += Deviation * Deviation / Error2;
679  }
680 
681  // safety protection in case of perfect fit
682  if(Chi2 < 1e-5)
683  Chi2 = 1e-5;
684 
685  return Chi2;
686 }
687 
688 double HcalNoiseMonitor::CalculateRMS8Max(double Charge[10])
689 {
690  //
691  // CalculateRMS8Max
692  //
693  // returns "RMS" divided by the largest charge in the time slices
694  // "RMS" is calculated using all but the two largest time slices.
695  // The "RMS" is not quite the actual RMS (see note below), but the value is only
696  // used for determining max values, and is not quoted as the actual RMS anywhere.
697  //
698 
699  int DigiSize = 10;
700 
701  // Copy Charge vector again, since we are passing references around
702  std::vector<double> TempCharge(Charge, Charge + 10);
703 
704  // Sort TempCharge vector from smallest to largest charge
705  sort(TempCharge.begin(), TempCharge.end());
706 
707  double Total = 0;
708  double Total2 = 0;
709  for(int i = 0; i < DigiSize - 2; i++)
710  {
711  Total = Total + TempCharge[i];
712  Total2 = Total2 + TempCharge[i] * TempCharge[i];
713  }
714 
715  // This isn't quite the RMS (both Total2 and Total*Total need to be
716  // divided by an extra (DigiSize-2) within the sqrt to get the RMS.)
717  // We're only using this value for relative comparisons, though; we
718  // aren't explicitly interpreting it as the RMS. It might be nice
719  // to either change the calculation or rename the variable in the future, though.
720 
721  double RMS = sqrt(Total2 - Total * Total / (DigiSize - 2));
722 
723  double RMS8Max = 99999;
724  if(TempCharge[DigiSize-1] > 1e-5)
725  RMS8Max = RMS / TempCharge[DigiSize-1];
726  if(RMS8Max < 1e-5) // protection against zero
727  RMS8Max = 1e-5;
728 
729  return RMS8Max;
730 }
731 
733 {
734  //
735  // Perform a "triangle fit", and extract the slopes
736  //
737  // Left-hand side and right-hand side are not correlated to each other - do them separately
738  //
739 
741  result.Chi2 = 0;
742  result.LeftSlope = 0;
743  result.RightSlope = 0;
744 
745  int DigiSize = 10;
746 
747  // right side, starting from TS4
748  double MinimumRightChi2 = 1000000;
749  double Numerator = 0;
750  double Denominator = 0;
751 
752  for(int iTS = mTrianglePeakTS + 2; iTS <= DigiSize; iTS++) // the place where first TS center in flat line
753  {
754  // fit a straight line to the triangle part
755  Numerator = 0;
756  Denominator = 0;
757 
758  for(int i = mTrianglePeakTS + 1; i < iTS; i++)
759  {
760  Numerator += (i - mTrianglePeakTS) * (Charge[i] - Charge[mTrianglePeakTS]);
761  Denominator += (i - mTrianglePeakTS) * (i - mTrianglePeakTS);
762  }
763 
764  double BestSlope = Numerator / Denominator;
765  if(BestSlope > 0)
766  BestSlope = 0;
767 
768  // check if the slope is reasonable
769  if(iTS != DigiSize)
770  {
771  if(BestSlope > -1 * Charge[mTrianglePeakTS] / (iTS - mTrianglePeakTS))
772  BestSlope = -1 * Charge[mTrianglePeakTS] / (iTS - mTrianglePeakTS);
773  if(BestSlope < -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS))
774  BestSlope = -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS);
775  }
776  else
777  {
778  if(BestSlope < -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS))
779  BestSlope = -1 * Charge[mTrianglePeakTS] / (iTS - 1 - mTrianglePeakTS);
780  }
781 
782  // calculate partial chi2
783 
784  // The shape I'm fitting is more like a tent than a triangle.
785  // After the end of triangle edge assume a flat line
786 
787  double Chi2 = 0;
788  for(int i = mTrianglePeakTS + 1; i < iTS; i++)
789  Chi2 += (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope)
790  * (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope);
791  for(int i = iTS; i < DigiSize; i++) // Assumes fit line = 0 for iTS > fit
792  Chi2 += Charge[i] * Charge[i];
793 
794  if(Chi2 < MinimumRightChi2)
795  {
796  MinimumRightChi2 = Chi2;
797  result.RightSlope = BestSlope;
798  }
799  } // end of right-hand side loop
800 
801  // left side
802  double MinimumLeftChi2 = 1000000;
803 
804  for(int iTS = 0; iTS < (int)mTrianglePeakTS; iTS++) // the first time after linear fit ends
805  {
806  // fit a straight line to the triangle part
807  Numerator = 0;
808  Denominator = 0;
809  for(int i = iTS; i < (int)mTrianglePeakTS; i++)
810  {
811  Numerator = Numerator + (i - mTrianglePeakTS) * (Charge[i] - Charge[mTrianglePeakTS]);
812  Denominator = Denominator + (i - mTrianglePeakTS) * (i - mTrianglePeakTS);
813  }
814 
815  double BestSlope = Numerator / Denominator;
816  if(BestSlope < 0)
817  BestSlope = 0;
818 
819  // check slope
820  if(iTS != 0)
821  {
822  if(BestSlope > Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS))
823  BestSlope = Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS);
824  if(BestSlope < Charge[mTrianglePeakTS] / (mTrianglePeakTS + 1 - iTS))
825  BestSlope = Charge[mTrianglePeakTS] / (mTrianglePeakTS + 1 - iTS);
826  }
827  else
828  {
829  if(BestSlope > Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS))
830  BestSlope = Charge[mTrianglePeakTS] / (mTrianglePeakTS - iTS);
831  }
832 
833  // calculate minimum chi2
834  double Chi2 = 0;
835  for(int i = 0; i < iTS; i++)
836  Chi2 += Charge[i] * Charge[i];
837  for(int i = iTS; i < (int)mTrianglePeakTS; i++)
838  Chi2 += (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope)
839  * (Charge[mTrianglePeakTS] - Charge[i] + (i - mTrianglePeakTS) * BestSlope);
840 
841  if(MinimumLeftChi2 > Chi2)
842  {
843  MinimumLeftChi2 = Chi2;
844  result.LeftSlope = BestSlope;
845  }
846  } // end of left-hand side loop
847 
848  result.Chi2 = MinimumLeftChi2 + MinimumRightChi2;
849 
850  return result;
851 }
852 
854 {
855  std::vector<double> PulseShape;
856 
857  HcalPulseShapes Shapes;
858  HcalPulseShapes::Shape HPDShape = Shapes.hbShape();
859 
860  PulseShape.reserve(350);
861  for(int i = 0; i < 200; i++)
862  PulseShape.push_back(HPDShape.at(i));
863  PulseShape.insert(PulseShape.begin(), 150, 0); // Safety margin of a lot of zeros in the beginning
864 
865  CumulativeIdealPulse.reserve(350);
866  CumulativeIdealPulse.clear();
867  CumulativeIdealPulse.push_back(0);
868  for(unsigned int i = 1; i < PulseShape.size(); i++)
869  CumulativeIdealPulse.push_back(CumulativeIdealPulse[i-1] + PulseShape[i]);
870 }
871 
873 
MonitorElement * hBadCountHPD
MonitorElement * hE2OverE10Digi
T getUntrackedParameter(std::string const &, T const &) const
MonitorElement * hBadNoOtherCountHPD
MonitorElement * hTS4TS5RelativeDifferenceVsCharge
int i
Definition: DBlmapReader.cc:9
double PerformNominalFit(double Charge[10])
MonitorElement * hE2OverE10RBX
float allChargeHighest2TS(unsigned int firstts=4) const
Definition: HcalNoiseRBX.cc:65
int idnumber(void) const
Definition: HcalNoiseHPD.cc:32
MonitorElement * hMaxZeros
virtual void analyze(const edm::Event &e, const edm::EventSetup &c)
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:872
int maxZeros(void) const
Definition: HcalNoiseRBX.cc:90
float at(double time) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< reco::HcalNoiseRBXCollection > tok_noise_
std::vector< int > AllowedCalibTypes_
TriangleFitResult PerformTriangleFit(double Charge[10])
MonitorElement * hLambdaRMS8MaxVsTotalCharge
std::vector< HBHEDataFrame >::const_iterator const_iterator
double CalculateRMS8Max(double Charge[10])
void setBinLabel(int bin, const std::string &label, int axis=1)
set bin label for x, y or z axis (axis=1, 2, 3 respectively)
int numRecHits(double threshold=1.5) const
MonitorElement * hHcalNoiseCategory
#define NULL
Definition: scimark2.h:8
double pedestal(int fCapId) const
get pedestal for capid=0..3
MonitorElement * hNominalChi2
edm::EDGetTokenT< HBHEDigiCollection > tok_hbhe_
EtaPhiHists hFailRMSMaxEtaPhi
void Fill(long long x)
MonitorElement * hTotalZeros
MonitorElement * hLinearChi2
MonitorElement * hLinearTestStatistics
edm::InputTag hltresultsLabel_
int iEvent
Definition: GenABIO.cc:243
std::vector< MonitorElement * > depth
MonitorElement * hBadE2E10RBX
virtual void beginRun(const edm::Run &run, const edm::EventSetup &c)
void removeContents(void)
erase all monitoring elements in current directory (not including subfolders);
Definition: DQMStore.cc:2764
double recHitEnergy(double theshold=1.5) const
Definition: HcalNoiseRBX.cc:99
float allChargeTotal(void) const
Definition: HcalNoiseRBX.cc:57
EtaPhiHists hFailTriangleEtaPhi
float big5ChargeHighest2TS(unsigned int firstts=4) const
Definition: HcalNoiseHPD.cc:81
virtual void adc2fC(const HBHEDataFrame &df, CaloSamples &lf) const
Definition: HcalCoderDb.cc:44
T sqrt(T t)
Definition: SSEVec.h:48
int numRecHits(float threshold=1.5) const
MonitorElement * hBadZeroRBX
void beginRun(const edm::Run &run, const edm::EventSetup &c)
tuple result
Definition: query.py:137
MonitorElement * hTriangleRightSlopeVsTS4
float bigChargeHighest2TS(unsigned int firstts=4) const
Definition: HcalNoiseHPD.cc:51
int ieta() const
get the cell ieta
Definition: HcalDetId.h:36
MonitorElement * hRMS8OverMaxTestStatistics
int j
Definition: DBlmapReader.cc:9
MonitorElement * hTS4TS5RelativeDifference
double PerformDualNominalFit(double Charge[10])
float big5ChargeTotal(void) const
Definition: HcalNoiseHPD.cc:72
double DualNominalFitSingleTry(double Charge[10], int Offset, int Distance)
int k[5][pyjets_maxn]
MonitorElement * hRBXHitCount
MonitorElement * hE2OverE10Digi5
int totalZeros(void) const
Definition: HcalNoiseRBX.cc:82
const T & get() const
Definition: EventSetup.h:55
MonitorElement * hRMS8OverMax
MonitorElement * hTriangleLeftSlopeVsTS4
const std::vector< HcalNoiseHPD > HPDs(void) const
Definition: HcalNoiseRBX.cc:33
void SetupEtaPhiHists(EtaPhiHists &hh, std::string Name, std::string Units)
std::vector< double > CumulativeIdealPulse
const Shape & hbShape() const
float recHitEnergy(float threshold=1.5) const
void analyze(edm::Event const &e, edm::EventSetup const &s)
int idnumber(void) const
Definition: HcalNoiseRBX.cc:28
tuple cout
Definition: gather_cfg.py:121
MonitorElement * hHPDHitCount
EtaPhiHists hFailIsolationEtaPhi
std::vector< std::string > triggers_
edm::EDGetTokenT< HBHERecHitCollection > tok_hbherec_
edm::InputTag rawdataLabel_
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
Definition: Chi2.h:17
MonitorElement * book2D(const char *name, const char *title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
Book 2D histogram.
Definition: DQMStore.cc:1000
virtual void setup(void)
HcalNoiseMonitor(const edm::ParameterSet &ps)
MonitorElement * hLambdaLinearVsTotalCharge
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
float bigChargeTotal(void) const
Definition: HcalNoiseHPD.cc:42
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:584
Definition: Run.h:41
double PerformLinearFit(double Charge[10])
EtaPhiHists hFailLinearEtaPhi
Abstract Class of shape.
Definition: Shape.h:14