CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripBadAPVAlgorithmFromClusterOccupancy.cc
Go to the documentation of this file.
2 
3 
11 
12 
14  lowoccupancy_(0),
15  highoccupancy_(100),
16  absolutelow_(0),
17  numberiterations_(2),
18  Nevents_(0),
19  occupancy_(0),
20  OutFileName_("Occupancy.root"),
21  UseInputDB_(iConfig.getUntrackedParameter<bool>("UseInputDB",false)),
22  tTopo(theTopo)
23  {
25  }
26 
28  LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")<<"[SiStripBadAPVAlgorithmFromClusterOccupancy::~SiStripBadAPVAlgorithmFromClusterOccupancy] "<<std::endl;
29 }
30 
32 
33  LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")<<"[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 
66  Apv APV;
67 
68  for (int apv=0; apv<6; apv++)
69  {
70  APV.apvMedian[apv] = 0;
71  APV.apvabsoluteOccupancy[apv] = 0;
72 
73  for (int strip=0; strip<128; strip++)
74  {
75  stripOccupancy[apv][strip] = 0;
76  stripWeight[apv][strip] = 0;
77  }
78  }
79 
80  pHisto phisto;
81  phisto._th1f = it->second.get();
82  phisto._NEntries = (int)phisto._th1f->GetEntries();
83  phisto._NBins = phisto._th1f->GetNbinsX();
84 
85  number_strips = (int)phisto._NBins;
87  APV.numberApvs = number_apvs;
88 
89  for (int apv=0; apv<number_apvs; apv++)
90  {
91  for (int strip=0; strip<128; strip++)
92  {
93  stripOccupancy[apv][strip] = phisto._th1f->GetBinContent((apv*128)+strip+1); // Remember: Bin=0 is underflow bin!
94  stripWeight[apv][strip] = 1;
95  APV.apvabsoluteOccupancy[apv] += phisto._th1f->GetBinContent((apv*128)+strip+1); // Remember: Bin=0 is underflow bin!
96  }
97  }
98 
99  for (int apv=0; apv<number_apvs; apv++)
100  {
101  APV.apvMedian[apv] = TMath::Median(128,stripOccupancy[apv],stripWeight[apv]);
102  }
103 
104  detid=it->first;
105  DetId detectorId=DetId(detid);
106 
107  if (edm::isDebugEnabled())
108  LogTrace("SiStripBadAPV") << "Analyzing detid " << detid<< std::endl;
109 
110  detrawid = detid;
111  APV.detrawId = detrawid;
112  subdetid = detectorId.subdetId();
113  switch (detectorId.subdetId())
114  {
115  case StripSubdetector::TIB :
117  disc = -1;
119  isback = -1;
121  else isexternalstring = 0;
123  else iszminusside = 0;
127 
128  if (layer_ring == 1) medianValues_TIB_Layer1.push_back(APV);
129  else if (layer_ring == 2) medianValues_TIB_Layer2.push_back(APV);
130  else if (layer_ring == 3) medianValues_TIB_Layer3.push_back(APV);
131  else if (layer_ring == 4) medianValues_TIB_Layer4.push_back(APV);
132  break;
133 
134  case StripSubdetector::TID :
138  if (tTopo->tidIsBackRing(detrawid)) isback = 1;
139  else isback = 0;
141  else iszminusside = 0;
142  isexternalstring = -1;
143  rodstringpetal = -1;
146 
147  if (iszminusside==0)
148  {
149  if (disc==1) medianValues_TIDPlus_Disc1.push_back(APV);
150  else if (disc==2) medianValues_TIDPlus_Disc2.push_back(APV);
151  else if (disc==3) medianValues_TIDPlus_Disc3.push_back(APV);
152  }
153  else if (iszminusside==1)
154  {
155  if (disc==1) medianValues_TIDMinus_Disc1.push_back(APV);
156  else if (disc==2) medianValues_TIDMinus_Disc2.push_back(APV);
157  else if (disc==3) medianValues_TIDMinus_Disc3.push_back(APV);
158  }
159  break;
160 
161  case StripSubdetector::TOB :
163  disc = -1;
165  isback = -1;
167  else iszminusside = 0;
168  isexternalstring = -1;
172 
173  if (layer_ring == 1) medianValues_TOB_Layer1.push_back(APV);
174  else if (layer_ring == 2) medianValues_TOB_Layer2.push_back(APV);
175  else if (layer_ring == 3) medianValues_TOB_Layer3.push_back(APV);
176  else if (layer_ring == 4) medianValues_TOB_Layer4.push_back(APV);
177  else if (layer_ring == 5) medianValues_TOB_Layer5.push_back(APV);
178  else if (layer_ring == 6) medianValues_TOB_Layer6.push_back(APV);
179  break;
180 
181  case StripSubdetector::TEC :
185  if (tTopo->tecIsBackPetal(detrawid)) isback = 1;
186  else isback = 0;
188  else iszminusside = 0;
189  isexternalstring = -1;
193 
194  if (iszminusside==0)
195  {
196  if (disc==1) medianValues_TECPlus_Disc1.push_back(APV);
197  else if (disc==2) medianValues_TECPlus_Disc2.push_back(APV);
198  else if (disc==3) medianValues_TECPlus_Disc3.push_back(APV);
199  else if (disc==4) medianValues_TECPlus_Disc4.push_back(APV);
200  else if (disc==5) medianValues_TECPlus_Disc5.push_back(APV);
201  else if (disc==6) medianValues_TECPlus_Disc6.push_back(APV);
202  else if (disc==7) medianValues_TECPlus_Disc7.push_back(APV);
203  else if (disc==8) medianValues_TECPlus_Disc8.push_back(APV);
204  else if (disc==9) medianValues_TECPlus_Disc9.push_back(APV);
205  }
206  else if (iszminusside==1)
207  {
208  if (disc==1) medianValues_TECMinus_Disc1.push_back(APV);
209  else if (disc==2) medianValues_TECMinus_Disc2.push_back(APV);
210  else if (disc==3) medianValues_TECMinus_Disc3.push_back(APV);
211  else if (disc==4) medianValues_TECMinus_Disc4.push_back(APV);
212  else if (disc==5) medianValues_TECMinus_Disc5.push_back(APV);
213  else if (disc==6) medianValues_TECMinus_Disc6.push_back(APV);
214  else if (disc==7) medianValues_TECMinus_Disc7.push_back(APV);
215  else if (disc==8) medianValues_TECMinus_Disc8.push_back(APV);
216  else if (disc==9) medianValues_TECMinus_Disc9.push_back(APV);
217  }
218  break;
219 
220  default :
221  std::cout << "### Detector does not belong to TIB, TID, TOB or TEC !? ###" << std::endl;
222  std::cout << "### DetRawId: " << detrawid << " ###" << std::endl;
223  }
224 
225  const StripGeomDetUnit* theStripDet = dynamic_cast<const StripGeomDetUnit*>( (TkGeom->idToDet(detectorId)) );
226  const StripTopology* theStripTopol = dynamic_cast<const StripTopology*>( &(theStripDet->specificTopology()) );
227 
228  for (int apv=0; apv<number_apvs; apv++)
229  {
230  apv_number = apv+1;
231  apvMedianOccupancy = APV.apvMedian[apv];
233 
234  LocalPoint pos_strip_local = theStripTopol->localPosition((apv*128));
235  GlobalPoint pos_strip_global = (TkGeom->idToDet(detectorId))->surface().toGlobal(pos_strip_local);
236 
237  global_position_x = pos_strip_global.x();
238  global_position_y = pos_strip_global.y();
239  global_position_z = pos_strip_global.z();
240 
241  if (WriteOutputFile_==true) apvtree->Fill();
242  }
243 
244  } // end loop on modules
245 
246  // Calculate Mean and RMS for each Layer
251 
258 
265 
275 
285 
286  pQuality=siStripQuality;
287  badStripList.clear();
288 
289  // Analyze the APV Occupancy for hot APVs
290  AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer1,MeanAndRms_TIB_Layer1,badStripList,inSiStripQuality);
291  AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer2,MeanAndRms_TIB_Layer2,badStripList,inSiStripQuality);
292  AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer3,MeanAndRms_TIB_Layer3,badStripList,inSiStripQuality);
293  AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer4,MeanAndRms_TIB_Layer4,badStripList,inSiStripQuality);
294 
295  AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer1,MeanAndRms_TOB_Layer1,badStripList,inSiStripQuality);
296  AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer2,MeanAndRms_TOB_Layer2,badStripList,inSiStripQuality);
297  AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer3,MeanAndRms_TOB_Layer3,badStripList,inSiStripQuality);
298  AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer4,MeanAndRms_TOB_Layer4,badStripList,inSiStripQuality);
299  AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer5,MeanAndRms_TOB_Layer5,badStripList,inSiStripQuality);
300  AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer6,MeanAndRms_TOB_Layer6,badStripList,inSiStripQuality);
301 
302  AnalyzeOccupancy(siStripQuality,medianValues_TIDPlus_Disc1,MeanAndRms_TIDPlus_Disc1,badStripList,inSiStripQuality);
303  AnalyzeOccupancy(siStripQuality,medianValues_TIDPlus_Disc2,MeanAndRms_TIDPlus_Disc2,badStripList,inSiStripQuality);
304  AnalyzeOccupancy(siStripQuality,medianValues_TIDPlus_Disc3,MeanAndRms_TIDPlus_Disc3,badStripList,inSiStripQuality);
305  AnalyzeOccupancy(siStripQuality,medianValues_TIDMinus_Disc1,MeanAndRms_TIDMinus_Disc1,badStripList,inSiStripQuality);
306  AnalyzeOccupancy(siStripQuality,medianValues_TIDMinus_Disc2,MeanAndRms_TIDMinus_Disc2,badStripList,inSiStripQuality);
307  AnalyzeOccupancy(siStripQuality,medianValues_TIDMinus_Disc3,MeanAndRms_TIDMinus_Disc3,badStripList,inSiStripQuality);
308 
309  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc1,MeanAndRms_TECPlus_Disc1,badStripList,inSiStripQuality);
310  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc2,MeanAndRms_TECPlus_Disc2,badStripList,inSiStripQuality);
311  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc3,MeanAndRms_TECPlus_Disc3,badStripList,inSiStripQuality);
312  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc4,MeanAndRms_TECPlus_Disc4,badStripList,inSiStripQuality);
313  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc5,MeanAndRms_TECPlus_Disc5,badStripList,inSiStripQuality);
314  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc6,MeanAndRms_TECPlus_Disc6,badStripList,inSiStripQuality);
315  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc7,MeanAndRms_TECPlus_Disc7,badStripList,inSiStripQuality);
316  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc8,MeanAndRms_TECPlus_Disc8,badStripList,inSiStripQuality);
317  AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc9,MeanAndRms_TECPlus_Disc9,badStripList,inSiStripQuality);
318 
319  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc1,MeanAndRms_TECMinus_Disc1,badStripList,inSiStripQuality);
320  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc2,MeanAndRms_TECMinus_Disc2,badStripList,inSiStripQuality);
321  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc3,MeanAndRms_TECMinus_Disc3,badStripList,inSiStripQuality);
322  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc4,MeanAndRms_TECMinus_Disc4,badStripList,inSiStripQuality);
323  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc5,MeanAndRms_TECMinus_Disc5,badStripList,inSiStripQuality);
324  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc6,MeanAndRms_TECMinus_Disc6,badStripList,inSiStripQuality);
325  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc7,MeanAndRms_TECMinus_Disc7,badStripList,inSiStripQuality);
326  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc8,MeanAndRms_TECMinus_Disc8,badStripList,inSiStripQuality);
327  AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc9,MeanAndRms_TECMinus_Disc9,badStripList,inSiStripQuality);
328 
329  siStripQuality->fillBadComponents();
330 
331  if (WriteOutputFile_==true){
332  f->cd();
333  apvtree->Write();
334  f->Close();
335  }
336 
337  LogTrace("SiStripBadAPV") << ss.str() << std::endl;
338 }
339 
340 
341 void SiStripBadAPVAlgorithmFromClusterOccupancy::CalculateMeanAndRMS(const std::vector<Apv>& a, std::pair<double,double>* MeanRMS, int number_iterations)
342 {
343  Double_t tot[7], tot2[7];
344  Double_t n[7];
345 
346  Double_t Mean[7] = {0};
347  Double_t Rms[7] = {1000,1000,1000,1000,1000,1000,1000};
348 
349  int Moduleposition;
350 
351  for (int i=0; i<number_iterations; i++)
352  {
353  for (int j=0; j<7; j++)
354  {
355  n[j] = 0;
356  tot[j] = 0;
357  tot2[j] = 0;
358  }
359 
360  for (uint32_t it=0; it<a.size(); it++)
361  {
362  Moduleposition = (a[it].modulePosition)-1;
363 
364  for (int apv=0; apv<a[it].numberApvs; apv++)
365  {
366  if (i>0)
367  {
368  if (a[it].apvMedian[apv]<(Mean[Moduleposition]-3*Rms[Moduleposition]) || (a[it].apvMedian[apv]>(Mean[Moduleposition]+5*Rms[Moduleposition])))
369  {
370  continue;
371  }
372  }
373  tot[Moduleposition] += a[it].apvMedian[apv];
374  tot2[Moduleposition] += (a[it].apvMedian[apv])*(a[it].apvMedian[apv]);
375  n[Moduleposition]++;
376  }
377  }
378 
379  for (int j=0; j<7; j++)
380  {
381  if (n[j]!=0)
382  {
383  Mean[j] = tot[j]/n[j];
384  Rms[j] = TMath::Sqrt(TMath::Abs(tot2[j]/n[j] -Mean[j]*Mean[j]));
385  }
386  }
387  }
388 
389  for (int j=0; j<7; j++)
390  {
391  MeanRMS[j] = std::make_pair(Mean[j],Rms[j]);
392  }
393 
394 }
395 
396 void SiStripBadAPVAlgorithmFromClusterOccupancy::AnalyzeOccupancy(SiStripQuality* quality, std::vector<Apv>& medianValues, std::pair<double,double>* MeanAndRms, std::vector<unsigned int>& BadStripList, edm::ESHandle<SiStripQuality>& InSiStripQuality)
397 {
398  int Moduleposition;
399  uint32_t Detid;
400 
401  for (uint32_t it=0; it<medianValues.size(); it++)
402  {
403  Moduleposition = (medianValues[it].modulePosition)-1;
404  Detid = medianValues[it].detrawId;
405 
406  for (int apv=0; apv<medianValues[it].numberApvs; apv++)
407  {
408  if(UseInputDB_)
409  {
410  if(InSiStripQuality->IsApvBad(Detid,apv) )
411  {
412  continue;//if the apv is already flagged as bad, continue.
413  }
414  }
415  if (medianValues[it].apvMedian[apv] > minNevents_)
416  {
417  if ((medianValues[it].apvMedian[apv]>(MeanAndRms[Moduleposition].first+highoccupancy_*MeanAndRms[Moduleposition].second)) && (medianValues[it].apvMedian[apv]>absolutelow_))
418  BadStripList.push_back(pQuality->encode((apv*128),128,0));
419  }
420  else if (medianValues[it].apvMedian[apv]<(MeanAndRms[Moduleposition].first-lowoccupancy_*MeanAndRms[Moduleposition].second) && (MeanAndRms[Moduleposition].first>2 || medianValues[it].apvabsoluteOccupancy[apv]==0))
421  {
422  BadStripList.push_back(pQuality->encode((apv*128),128,0));
423  std::cout << "Dead APV! DetId: " << medianValues[it].detrawId << ", APV number: " << apv+1 << ", APVMedian: " << medianValues[it].apvMedian[apv] << ", Mean: " << MeanAndRms[Moduleposition].first << ", RMS: " << MeanAndRms[Moduleposition].second << ", LowThreshold: " << lowoccupancy_ << ", Mean-Low*RMS: " << (MeanAndRms[Moduleposition].first-lowoccupancy_*MeanAndRms[Moduleposition].second) << std::endl;
424  }
425  }
426  if (BadStripList.begin()!=BadStripList.end())
427  {
428  quality->compact(Detid,BadStripList);
429  SiStripQuality::Range range(BadStripList.begin(),BadStripList.end());
430  quality->put(Detid,range);
431  }
432  BadStripList.clear();
433  }
434 }
435 
437 {
439 }
bool isDebugEnabled()
int i
Definition: DBlmapReader.cc:9
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
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
int j
Definition: DBlmapReader.cc:9
void fillBadComponents()
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
#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
void extractBadAPVs(SiStripQuality *, HistoMap &, edm::ESHandle< SiStripQuality > &)
virtual LocalPoint localPosition(float strip) const =0
unsigned int tecPetalNumber(const DetId &id) const
tuple cout
Definition: gather_cfg.py:121
volatile std::atomic< bool > shutdown_flag false
unsigned int tobRod(const DetId &id) const
bool put(const uint32_t &detID, const InputVector &vect)
for(const auto &isodef:isoDefs)
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
virtual const TrackerGeomDet * idToDet(DetId) const