CMS 3D CMS Logo

SiStripBadAPVAlgorithmFromClusterOccupancy.cc
Go to the documentation of this file.
2 
9 
11  const TrackerTopology* theTopo)
12  : lowoccupancy_(0),
13  highoccupancy_(100),
14  absolutelow_(0),
15  numberiterations_(2),
16  Nevents_(0),
17  occupancy_(0),
18  OutFileName_("Occupancy.root"),
19  UseInputDB_(iConfig.getUntrackedParameter<bool>("UseInputDB", false)),
20  tTopo(theTopo) {
22 }
23 
25  LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")
26  << "[SiStripBadAPVAlgorithmFromClusterOccupancy::~SiStripBadAPVAlgorithmFromClusterOccupancy] " << std::endl;
27 }
28 
30  HistoMap& DM,
31  edm::ESHandle<SiStripQuality>& inSiStripQuality) {
32  LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")
33  << "[SiStripBadAPVAlgorithmFromClusterOccupancy::extractBadAPVs] " << std::endl;
34 
35  if (WriteOutputFile_ == true) {
36  f = new TFile(OutFileName_.c_str(), "RECREATE");
37  f->cd();
38 
39  apvtree = new TTree("moduleOccupancy", "tree");
40 
41  apvtree->Branch("DetRawId", &detrawid, "DetRawId/I");
42  apvtree->Branch("SubDetId", &subdetid, "SubDetId/I");
43  apvtree->Branch("Layer_Ring", &layer_ring, "Layer_Ring/I");
44  apvtree->Branch("Disc", &disc, "Disc/I");
45  apvtree->Branch("IsBack", &isback, "IsBack/I");
46  apvtree->Branch("IsExternalString", &isexternalstring, "IsExternalString/I");
47  apvtree->Branch("IsZMinusSide", &iszminusside, "IsZMinusSide/I");
48  apvtree->Branch("RodStringPetal", &rodstringpetal, "RodStringPetal/I");
49  apvtree->Branch("IsStereo", &isstereo, "IsStereo/I");
50  apvtree->Branch("ModuleNumber", &module_number, "ModuleNumber/I");
51  apvtree->Branch("NumberOfStrips", &number_strips, "NumberOfStrips/I");
52  apvtree->Branch("APVGlobalPositionX", &global_position_x, "APVGlobalPositionX/F");
53  apvtree->Branch("APVGlobalPositionY", &global_position_y, "APVGlobalPositionY/F");
54  apvtree->Branch("APVGlobalPositionZ", &global_position_z, "APVGlobalPositionZ/F");
55  apvtree->Branch("APVNumber", &apv_number, "APVNumber/I");
56  apvtree->Branch("APVAbsoluteOccupancy", &apvAbsoluteOccupancy, "apvAbsoluteOccupancy/D");
57  apvtree->Branch("APVMedianOccupancy", &apvMedianOccupancy, "apvMedianOccupancy/D");
58  }
59 
60  HistoMap::iterator it = DM.begin();
61  HistoMap::iterator itEnd = DM.end();
62  std::vector<unsigned int> badStripList;
63  uint32_t detid;
64  for (; it != itEnd; ++it) {
65  Apv APV;
66 
67  for (int apv = 0; apv < 6; apv++) {
68  APV.apvMedian[apv] = 0;
69  APV.apvabsoluteOccupancy[apv] = 0;
70 
71  for (int strip = 0; strip < 128; strip++) {
72  stripOccupancy[apv][strip] = 0;
73  stripWeight[apv][strip] = 0;
74  }
75  }
76 
77  pHisto phisto;
78  phisto._th1f = it->second.get();
79  phisto._NEntries = (int)phisto._th1f->GetEntries();
80  phisto._NBins = phisto._th1f->GetNbinsX();
81 
82  number_strips = (int)phisto._NBins;
83  number_apvs = number_strips / 128;
84  APV.numberApvs = number_apvs;
85 
86  for (int apv = 0; apv < number_apvs; apv++) {
87  for (int strip = 0; strip < 128; strip++) {
88  stripOccupancy[apv][strip] =
89  phisto._th1f->GetBinContent((apv * 128) + strip + 1); // Remember: Bin=0 is underflow bin!
90  stripWeight[apv][strip] = 1;
91  APV.apvabsoluteOccupancy[apv] +=
92  phisto._th1f->GetBinContent((apv * 128) + strip + 1); // Remember: Bin=0 is underflow bin!
93  }
94  }
95 
96  for (int apv = 0; apv < number_apvs; apv++) {
97  APV.apvMedian[apv] = TMath::Median(128, stripOccupancy[apv], stripWeight[apv]);
98  }
99 
100  detid = it->first;
101  DetId detectorId = DetId(detid);
102 
103  if (edm::isDebugEnabled())
104  LogTrace("SiStripBadAPV") << "Analyzing detid " << detid << std::endl;
105 
106  detrawid = detid;
107  APV.detrawId = detrawid;
108  subdetid = detectorId.subdetId();
109  switch (detectorId.subdetId()) {
112  disc = -1;
114  isback = -1;
116  isexternalstring = 1;
117  else
118  isexternalstring = 0;
120  iszminusside = 1;
121  else
122  iszminusside = 0;
126 
127  if (layer_ring == 1)
128  medianValues_TIB_Layer1.push_back(APV);
129  else if (layer_ring == 2)
130  medianValues_TIB_Layer2.push_back(APV);
131  else if (layer_ring == 3)
132  medianValues_TIB_Layer3.push_back(APV);
133  else if (layer_ring == 4)
134  medianValues_TIB_Layer4.push_back(APV);
135  break;
136 
142  isback = 1;
143  else
144  isback = 0;
146  iszminusside = 1;
147  else
148  iszminusside = 0;
149  isexternalstring = -1;
150  rodstringpetal = -1;
153 
154  if (iszminusside == 0) {
155  if (disc == 1)
156  medianValues_TIDPlus_Disc1.push_back(APV);
157  else if (disc == 2)
158  medianValues_TIDPlus_Disc2.push_back(APV);
159  else if (disc == 3)
160  medianValues_TIDPlus_Disc3.push_back(APV);
161  } else if (iszminusside == 1) {
162  if (disc == 1)
163  medianValues_TIDMinus_Disc1.push_back(APV);
164  else if (disc == 2)
165  medianValues_TIDMinus_Disc2.push_back(APV);
166  else if (disc == 3)
167  medianValues_TIDMinus_Disc3.push_back(APV);
168  }
169  break;
170 
173  disc = -1;
175  isback = -1;
177  iszminusside = 1;
178  else
179  iszminusside = 0;
180  isexternalstring = -1;
184 
185  if (layer_ring == 1)
186  medianValues_TOB_Layer1.push_back(APV);
187  else if (layer_ring == 2)
188  medianValues_TOB_Layer2.push_back(APV);
189  else if (layer_ring == 3)
190  medianValues_TOB_Layer3.push_back(APV);
191  else if (layer_ring == 4)
192  medianValues_TOB_Layer4.push_back(APV);
193  else if (layer_ring == 5)
194  medianValues_TOB_Layer5.push_back(APV);
195  else if (layer_ring == 6)
196  medianValues_TOB_Layer6.push_back(APV);
197  break;
198 
204  isback = 1;
205  else
206  isback = 0;
208  iszminusside = 1;
209  else
210  iszminusside = 0;
211  isexternalstring = -1;
215 
216  if (iszminusside == 0) {
217  if (disc == 1)
218  medianValues_TECPlus_Disc1.push_back(APV);
219  else if (disc == 2)
220  medianValues_TECPlus_Disc2.push_back(APV);
221  else if (disc == 3)
222  medianValues_TECPlus_Disc3.push_back(APV);
223  else if (disc == 4)
224  medianValues_TECPlus_Disc4.push_back(APV);
225  else if (disc == 5)
226  medianValues_TECPlus_Disc5.push_back(APV);
227  else if (disc == 6)
228  medianValues_TECPlus_Disc6.push_back(APV);
229  else if (disc == 7)
230  medianValues_TECPlus_Disc7.push_back(APV);
231  else if (disc == 8)
232  medianValues_TECPlus_Disc8.push_back(APV);
233  else if (disc == 9)
234  medianValues_TECPlus_Disc9.push_back(APV);
235  } else if (iszminusside == 1) {
236  if (disc == 1)
237  medianValues_TECMinus_Disc1.push_back(APV);
238  else if (disc == 2)
239  medianValues_TECMinus_Disc2.push_back(APV);
240  else if (disc == 3)
241  medianValues_TECMinus_Disc3.push_back(APV);
242  else if (disc == 4)
243  medianValues_TECMinus_Disc4.push_back(APV);
244  else if (disc == 5)
245  medianValues_TECMinus_Disc5.push_back(APV);
246  else if (disc == 6)
247  medianValues_TECMinus_Disc6.push_back(APV);
248  else if (disc == 7)
249  medianValues_TECMinus_Disc7.push_back(APV);
250  else if (disc == 8)
251  medianValues_TECMinus_Disc8.push_back(APV);
252  else if (disc == 9)
253  medianValues_TECMinus_Disc9.push_back(APV);
254  }
255  break;
256 
257  default:
258  std::cout << "### Detector does not belong to TIB, TID, TOB or TEC !? ###" << std::endl;
259  std::cout << "### DetRawId: " << detrawid << " ###" << std::endl;
260  }
261 
262  const StripGeomDetUnit* theStripDet = dynamic_cast<const StripGeomDetUnit*>((TkGeom->idToDet(detectorId)));
263  const StripTopology* theStripTopol = dynamic_cast<const StripTopology*>(&(theStripDet->specificTopology()));
264 
265  for (int apv = 0; apv < number_apvs; apv++) {
266  apv_number = apv + 1;
267  apvMedianOccupancy = APV.apvMedian[apv];
269 
270  LocalPoint pos_strip_local = theStripTopol->localPosition((apv * 128));
271  GlobalPoint pos_strip_global = (TkGeom->idToDet(detectorId))->surface().toGlobal(pos_strip_local);
272 
273  global_position_x = pos_strip_global.x();
274  global_position_y = pos_strip_global.y();
275  global_position_z = pos_strip_global.z();
276 
277  if (WriteOutputFile_ == true)
278  apvtree->Fill();
279  }
280 
281  } // end loop on modules
282 
283  // Calculate Mean and RMS for each Layer
288 
295 
302 
312 
322 
323  pQuality = siStripQuality;
324  badStripList.clear();
325 
326  // Analyze the APV Occupancy for hot APVs
327  AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer1, MeanAndRms_TIB_Layer1, badStripList, inSiStripQuality);
328  AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer2, MeanAndRms_TIB_Layer2, badStripList, inSiStripQuality);
329  AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer3, MeanAndRms_TIB_Layer3, badStripList, inSiStripQuality);
330  AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer4, MeanAndRms_TIB_Layer4, badStripList, inSiStripQuality);
331 
332  AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer1, MeanAndRms_TOB_Layer1, badStripList, inSiStripQuality);
333  AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer2, MeanAndRms_TOB_Layer2, badStripList, inSiStripQuality);
334  AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer3, MeanAndRms_TOB_Layer3, badStripList, inSiStripQuality);
335  AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer4, MeanAndRms_TOB_Layer4, badStripList, inSiStripQuality);
336  AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer5, MeanAndRms_TOB_Layer5, badStripList, inSiStripQuality);
337  AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer6, MeanAndRms_TOB_Layer6, badStripList, inSiStripQuality);
338 
340  siStripQuality, medianValues_TIDPlus_Disc1, MeanAndRms_TIDPlus_Disc1, badStripList, inSiStripQuality);
342  siStripQuality, medianValues_TIDPlus_Disc2, MeanAndRms_TIDPlus_Disc2, badStripList, inSiStripQuality);
344  siStripQuality, medianValues_TIDPlus_Disc3, MeanAndRms_TIDPlus_Disc3, badStripList, inSiStripQuality);
346  siStripQuality, medianValues_TIDMinus_Disc1, MeanAndRms_TIDMinus_Disc1, badStripList, inSiStripQuality);
348  siStripQuality, medianValues_TIDMinus_Disc2, MeanAndRms_TIDMinus_Disc2, badStripList, inSiStripQuality);
350  siStripQuality, medianValues_TIDMinus_Disc3, MeanAndRms_TIDMinus_Disc3, badStripList, inSiStripQuality);
351 
353  siStripQuality, medianValues_TECPlus_Disc1, MeanAndRms_TECPlus_Disc1, badStripList, inSiStripQuality);
355  siStripQuality, medianValues_TECPlus_Disc2, MeanAndRms_TECPlus_Disc2, badStripList, inSiStripQuality);
357  siStripQuality, medianValues_TECPlus_Disc3, MeanAndRms_TECPlus_Disc3, badStripList, inSiStripQuality);
359  siStripQuality, medianValues_TECPlus_Disc4, MeanAndRms_TECPlus_Disc4, badStripList, inSiStripQuality);
361  siStripQuality, medianValues_TECPlus_Disc5, MeanAndRms_TECPlus_Disc5, badStripList, inSiStripQuality);
363  siStripQuality, medianValues_TECPlus_Disc6, MeanAndRms_TECPlus_Disc6, badStripList, inSiStripQuality);
365  siStripQuality, medianValues_TECPlus_Disc7, MeanAndRms_TECPlus_Disc7, badStripList, inSiStripQuality);
367  siStripQuality, medianValues_TECPlus_Disc8, MeanAndRms_TECPlus_Disc8, badStripList, inSiStripQuality);
369  siStripQuality, medianValues_TECPlus_Disc9, MeanAndRms_TECPlus_Disc9, badStripList, inSiStripQuality);
370 
372  siStripQuality, medianValues_TECMinus_Disc1, MeanAndRms_TECMinus_Disc1, badStripList, inSiStripQuality);
374  siStripQuality, medianValues_TECMinus_Disc2, MeanAndRms_TECMinus_Disc2, badStripList, inSiStripQuality);
376  siStripQuality, medianValues_TECMinus_Disc3, MeanAndRms_TECMinus_Disc3, badStripList, inSiStripQuality);
378  siStripQuality, medianValues_TECMinus_Disc4, MeanAndRms_TECMinus_Disc4, badStripList, inSiStripQuality);
380  siStripQuality, medianValues_TECMinus_Disc5, MeanAndRms_TECMinus_Disc5, badStripList, inSiStripQuality);
382  siStripQuality, medianValues_TECMinus_Disc6, MeanAndRms_TECMinus_Disc6, badStripList, inSiStripQuality);
384  siStripQuality, medianValues_TECMinus_Disc7, MeanAndRms_TECMinus_Disc7, badStripList, inSiStripQuality);
386  siStripQuality, medianValues_TECMinus_Disc8, MeanAndRms_TECMinus_Disc8, badStripList, inSiStripQuality);
388  siStripQuality, medianValues_TECMinus_Disc9, MeanAndRms_TECMinus_Disc9, badStripList, inSiStripQuality);
389 
390  siStripQuality->fillBadComponents();
391 
392  if (WriteOutputFile_ == true) {
393  f->cd();
394  apvtree->Write();
395  f->Close();
396  }
397 
398  LogTrace("SiStripBadAPV") << ss.str() << std::endl;
399 }
400 
402  std::pair<double, double>* MeanRMS,
403  int number_iterations) {
404  Double_t tot[7], tot2[7];
405  Double_t n[7];
406 
407  Double_t Mean[7] = {0};
408  Double_t Rms[7] = {1000, 1000, 1000, 1000, 1000, 1000, 1000};
409 
410  int Moduleposition;
411 
412  for (int i = 0; i < number_iterations; i++) {
413  for (int j = 0; j < 7; j++) {
414  n[j] = 0;
415  tot[j] = 0;
416  tot2[j] = 0;
417  }
418 
419  for (uint32_t it = 0; it < a.size(); it++) {
420  Moduleposition = (a[it].modulePosition) - 1;
421 
422  for (int apv = 0; apv < a[it].numberApvs; apv++) {
423  if (i > 0) {
424  if (a[it].apvMedian[apv] < (Mean[Moduleposition] - 3 * Rms[Moduleposition]) ||
425  (a[it].apvMedian[apv] > (Mean[Moduleposition] + 5 * Rms[Moduleposition]))) {
426  continue;
427  }
428  }
429  tot[Moduleposition] += a[it].apvMedian[apv];
430  tot2[Moduleposition] += (a[it].apvMedian[apv]) * (a[it].apvMedian[apv]);
431  n[Moduleposition]++;
432  }
433  }
434 
435  for (int j = 0; j < 7; j++) {
436  if (n[j] != 0) {
437  Mean[j] = tot[j] / n[j];
438  Rms[j] = TMath::Sqrt(TMath::Abs(tot2[j] / n[j] - Mean[j] * Mean[j]));
439  }
440  }
441  }
442 
443  for (int j = 0; j < 7; j++) {
444  MeanRMS[j] = std::make_pair(Mean[j], Rms[j]);
445  }
446 }
447 
449  std::vector<Apv>& medianValues,
450  std::pair<double, double>* MeanAndRms,
451  std::vector<unsigned int>& BadStripList,
452  edm::ESHandle<SiStripQuality>& InSiStripQuality) {
453  int Moduleposition;
454  uint32_t Detid;
455 
456  for (uint32_t it = 0; it < medianValues.size(); it++) {
457  Moduleposition = (medianValues[it].modulePosition) - 1;
458  Detid = medianValues[it].detrawId;
459 
460  for (int apv = 0; apv < medianValues[it].numberApvs; apv++) {
461  if (UseInputDB_) {
462  if (InSiStripQuality->IsApvBad(Detid, apv)) {
463  continue; //if the apv is already flagged as bad, continue.
464  }
465  }
466  if (medianValues[it].apvMedian[apv] > minNevents_) {
467  if ((medianValues[it].apvMedian[apv] >
468  (MeanAndRms[Moduleposition].first + highoccupancy_ * MeanAndRms[Moduleposition].second)) &&
469  (medianValues[it].apvMedian[apv] > absolutelow_))
470  BadStripList.push_back(pQuality->encode((apv * 128), 128, 0));
471  } else if (medianValues[it].apvMedian[apv] <
472  (MeanAndRms[Moduleposition].first - lowoccupancy_ * MeanAndRms[Moduleposition].second) &&
473  (MeanAndRms[Moduleposition].first > 2 || medianValues[it].apvabsoluteOccupancy[apv] == 0)) {
474  BadStripList.push_back(pQuality->encode((apv * 128), 128, 0));
475  std::cout << "Dead APV! DetId: " << medianValues[it].detrawId << ", APV number: " << apv + 1
476  << ", APVMedian: " << medianValues[it].apvMedian[apv]
477  << ", Mean: " << MeanAndRms[Moduleposition].first << ", RMS: " << MeanAndRms[Moduleposition].second
478  << ", LowThreshold: " << lowoccupancy_ << ", Mean-Low*RMS: "
479  << (MeanAndRms[Moduleposition].first - lowoccupancy_ * MeanAndRms[Moduleposition].second)
480  << std::endl;
481  }
482  }
483  if (BadStripList.begin() != BadStripList.end()) {
484  quality->compact(Detid, BadStripList);
485  SiStripQuality::Range range(BadStripList.begin(), BadStripList.end());
486  quality->put(Detid, range);
487  }
488  BadStripList.clear();
489  }
490 }
491 
bool IsApvBad(const uint32_t &detid, const short &apvNb) const
bool isDebugEnabled()
unsigned int tibLayer(const DetId &id) const
unsigned int tibString(const DetId &id) const
unsigned int tidRing(const DetId &id) const
bool tobIsStereo(const DetId &id) const
void AnalyzeOccupancy(SiStripQuality *, std::vector< Apv > &, std::pair< double, double > *, std::vector< unsigned int > &, edm::ESHandle< SiStripQuality > &)
void CalculateMeanAndRMS(const std::vector< Apv > &, std::pair< double, double > *, int)
unsigned int tecRing(const DetId &id) const
ring id
SiStripBadAPVAlgorithmFromClusterOccupancy(const edm::ParameterSet &, const TrackerTopology *)
T y() const
Definition: PV3DBase.h:63
unsigned int tidWheel(const DetId &id) const
virtual const StripTopology & specificTopology() const
Returns a reference to the strip proxy topology.
bool tecIsZMinusSide(const DetId &id) const
bool tidIsStereo(const DetId &id) const
bool tidIsZMinusSide(const DetId &id) const
static const char occupancy_[]
U second(std::pair< T, U > const &p)
bool tecIsStereo(const DetId &id) const
bool tibIsExternalString(const DetId &id) const
bool tibIsZMinusSide(const DetId &id) const
virtual LocalPoint localPosition(float strip) const =0
unsigned int tidModule(const DetId &id) const
bool tidIsBackRing(const DetId &id) const
T z() const
Definition: PV3DBase.h:64
void compact(unsigned int &, std::vector< unsigned int > &)
T Abs(T a)
Definition: MathUtil.h:49
bool tobIsZMinusSide(const DetId &id) const
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
void fillBadComponents()
#define LogTrace(id)
unsigned int tibModule(const DetId &id) const
unsigned int tecModule(const DetId &id) const
bool tecIsBackPetal(const DetId &id) const
Definition: DetId.h:18
bool tibIsStereo(const DetId &id) const
unsigned int tobModule(const DetId &id) const
double a
Definition: hdecay.h:121
std::pair< ContainerIterator, ContainerIterator > Range
const TrackerGeomDet * idToDet(DetId) const override
void extractBadAPVs(SiStripQuality *, HistoMap &, edm::ESHandle< SiStripQuality > &)
unsigned int tecPetalNumber(const DetId &id) const
unsigned int tobRod(const DetId &id) const
bool put(const uint32_t &detID, const InputVector &vect)
T x() const
Definition: PV3DBase.h:62
unsigned int tecWheel(const DetId &id) const
unsigned int encode(const unsigned short &first, const unsigned short &NconsecutiveBadStrips, const unsigned short &flag=0)
unsigned int tobLayer(const DetId &id) const