CMS 3D CMS Logo

SiStripHotStripAlgorithmFromClusterOccupancy.cc
Go to the documentation of this file.
3 
5  const edm::ParameterSet& iConfig, const TrackerTopology* theTopo)
6  : prob_(1.E-7),
7  ratio_(1.5),
8  MinNumEntries_(0),
9  MinNumEntriesPerStrip_(0),
10  Nevents_(0),
11  occupancy_(0),
12  OutFileName_("Occupancy.root"),
13  tTopo(theTopo),
14  UseInputDB_(iConfig.getUntrackedParameter<bool>("UseInputDB", false)) {
16 }
17 
19  LogTrace("SiStripHotStripAlgorithmFromClusterOccupancy")
20  << "[SiStripHotStripAlgorithmFromClusterOccupancy::~SiStripHotStripAlgorithmFromClusterOccupancy] " << std::endl;
21 }
22 
24  HistoMap& DM,
25  edm::ESHandle<SiStripQuality>& InSiStripQuality) {
26  LogTrace("SiStripHotStripAlgorithmFromClusterOccupancy")
27  << "[SiStripHotStripAlgorithmFromClusterOccupancy::extractBadStrips] " << std::endl;
28 
29  if (WriteOutputFile_ == true) {
30  f = new TFile(OutFileName_.c_str(), "RECREATE");
31  f->cd();
32 
33  striptree = new TTree("stripOccupancy", "tree");
34 
35  striptree->Branch("DetRawId", &detrawid, "DetRawId/I");
36  striptree->Branch("SubDetId", &subdetid, "SubDetId/I");
37  striptree->Branch("Layer_Ring", &layer_ring, "Layer_Ring/I");
38  striptree->Branch("Disc", &disc, "Disc/I");
39  striptree->Branch("IsBack", &isback, "IsBack/I");
40  striptree->Branch("IsExternalString", &isexternalstring, "IsExternalString/I");
41  striptree->Branch("IsZMinusSide", &iszminusside, "IsZMinusSide/I");
42  striptree->Branch("RodStringPetal", &rodstringpetal, "RodStringPetal/I");
43  striptree->Branch("IsStereo", &isstereo, "IsStereo/I");
44  striptree->Branch("ModulePosition", &module_position, "ModulePosition/I");
45  striptree->Branch("NumberOfStrips", &number_strips, "NumberOfStrips/I");
46  striptree->Branch("StripNumber", &strip_number, "StripNumber/I");
47  striptree->Branch("APVChannel", &apv_channel, "APVChannel/I");
48  striptree->Branch("StripGlobalPositionX", &global_position_x, "StripGlobalPositionX/F");
49  striptree->Branch("StripGlobalPositionY", &global_position_y, "StripGlobalPositionY/F");
50  striptree->Branch("StripGlobalPositionZ", &global_position_z, "StripGlobalPositionZ/F");
51  striptree->Branch("IsHot", &isHot, "IsHot/I");
52  striptree->Branch("HotStripsPerAPV", &hotStripsPerAPV, "HotStripsPerAPV/I");
53  striptree->Branch("HotStripsPerModule", &hotStripsPerModule, "HotStripsPerModule/I");
54  striptree->Branch("StripOccupancy", &stripOccupancy, "StripOccupancy/D");
55  striptree->Branch("StripHits", &stripHits, "StripHits/I");
56  striptree->Branch("PoissonProb", &poissonProb, "PoissonProb/D");
57  striptree->Branch("MedianAPVHits", &medianAPVHits, "MedianAPVHits/D");
58  striptree->Branch("AvgAPVHits", &avgAPVHits, "AvgAPVHits/D");
59  }
60 
61  HistoMap::iterator it = DM.begin();
62  HistoMap::iterator itEnd = DM.end();
63  std::vector<unsigned int> badStripList;
64  uint32_t detid;
65  for (; it != itEnd; ++it) {
66  pHisto phisto;
67  detid = it->first;
68 
69  DetId detectorId = DetId(detid);
70  phisto._SubdetId = detectorId.subdetId();
71 
72  if (edm::isDebugEnabled())
73  LogTrace("SiStripHotStrip") << "Analyzing detid " << detid << std::endl;
74 
75  int numberAPVs = (int)(it->second.get())->GetNbinsX() / 128;
76 
77  // Set the values for the tree:
78 
79  detrawid = detid;
80  subdetid = detectorId.subdetId();
81  number_strips = (int)(it->second.get())->GetNbinsX();
82  switch (detectorId.subdetId()) {
85  disc = -1;
87  isback = -1;
89  isexternalstring = 1;
90  else
91  isexternalstring = 0;
93  iszminusside = 1;
94  else
95  iszminusside = 0;
98  break;
99 
105  isback = 1;
106  else
107  isback = 0;
109  iszminusside = 1;
110  else
111  iszminusside = 0;
112  isexternalstring = -1;
113  rodstringpetal = -1;
115  break;
116 
119  disc = -1;
121  isback = -1;
123  iszminusside = 1;
124  else
125  iszminusside = 0;
126  isexternalstring = -1;
129  break;
130 
136  isback = 1;
137  else
138  isback = 0;
140  iszminusside = 1;
141  else
142  iszminusside = 0;
143  isexternalstring = -1;
146  break;
147 
148  default:
149  std::cout << "### Detector does not belong to TIB, TID, TOB or TEC !? ###" << std::endl;
150  std::cout << "### DetRawId: " << detrawid << " ###" << std::endl;
151  }
152 
153  // End: Set the values for the tree.
154 
155  pQuality = OutSiStripQuality;
156  badStripList.clear();
157 
158  for (int i = 0; i < 768; i++) {
159  ishot[i] = 0;
160  stripoccupancy[i] = 0;
161  striphits[i] = 0;
162  poissonprob[i] = 0;
163  hotstripsperapv[i / 128] = 0;
164  }
165 
166  hotstripspermodule = 0;
167 
168  for (int apv = 0; apv < numberAPVs; apv++) {
169  if (UseInputDB_) {
170  if (InSiStripQuality->IsApvBad(detid, apv)) {
171  if (edm::isDebugEnabled())
172  LogTrace("SiStripHotStrip") << "(Module and Apv number) " << detid << " , " << apv
173  << " excluded by input ESetup." << std::endl;
174  continue; //if the apv is already flagged as bad, continue.
175  } else {
176  if (edm::isDebugEnabled())
177  LogTrace("SiStripHotStrip") << "(Module and Apv number) " << detid << " , " << apv
178  << " good by input ESetup." << std::endl;
179  }
180  }
181 
182  phisto._th1f = new TH1F("tmp", "tmp", 128, 0.5, 128.5);
183  int NumberEntriesPerAPV = 0;
184 
185  for (int strip = 0; strip < 128; strip++) {
186  phisto._th1f->SetBinContent(strip + 1, (it->second.get())->GetBinContent((apv * 128) + strip + 1));
187  NumberEntriesPerAPV += (int)(it->second.get())->GetBinContent((apv * 128) + strip + 1);
188  }
189 
190  phisto._th1f->SetEntries(NumberEntriesPerAPV);
191  phisto._NEntries = (int)phisto._th1f->GetEntries();
192  phisto._NEmptyBins = 0;
193 
194  LogTrace("SiStripHotStrip") << "Number of clusters in APV " << apv << ": " << NumberEntriesPerAPV << std::endl;
195 
196  iterativeSearch(phisto, badStripList, apv);
197 
198  delete phisto._th1f;
199  }
200 
201  const StripGeomDetUnit* theStripDet = dynamic_cast<const StripGeomDetUnit*>((TkGeom->idToDet(detectorId)));
202  const StripTopology* theStripTopol = dynamic_cast<const StripTopology*>(&(theStripDet->specificTopology()));
203 
204  for (int strip = 0; strip < number_strips; strip++) {
205  strip_number = strip + 1;
206  apv_channel = (strip % 128) + 1;
207  isHot = ishot[strip];
212  avgAPVHits = avgapvhits[strip / 128];
213 
216 
217  LocalPoint pos_strip_local = theStripTopol->localPosition(strip);
218  GlobalPoint pos_strip_global = (TkGeom->idToDet(detectorId))->surface().toGlobal(pos_strip_local);
219 
220  global_position_x = pos_strip_global.x();
221  global_position_y = pos_strip_global.y();
222  global_position_z = pos_strip_global.z();
223 
224  if (WriteOutputFile_ == true)
225  striptree->Fill();
226  }
227 
228  if (badStripList.begin() == badStripList.end())
229  continue;
230 
231  OutSiStripQuality->compact(detid, badStripList);
232 
233  SiStripQuality::Range range(badStripList.begin(), badStripList.end());
234  if (!OutSiStripQuality->put(detid, range))
235  edm::LogError("SiStripHotStrip")
236  << "[SiStripHotStripAlgorithmFromClusterOccupancy::extractBadStrips] detid already exists" << std::endl;
237  }
238  OutSiStripQuality->fillBadComponents();
239 
240  if (WriteOutputFile_ == true) {
241  f->cd();
242  striptree->Write();
243  f->Close();
244  }
245 
246  LogTrace("SiStripHotStrip") << ss.str() << std::endl;
247 }
248 
250  std::vector<unsigned int>& vect,
251  int apv) {
252  if (!histo._NEntries || histo._NEntries <= MinNumEntries_ || histo._NEntries <= minNevents_)
253  return;
254 
255  size_t startingSize = vect.size();
256  long double diff = 1. - prob_;
257 
258  size_t Nbins = histo._th1f->GetNbinsX();
259  size_t ibinStart = 1;
260  size_t ibinStop = Nbins + 1;
261  int MaxEntry = (int)histo._th1f->GetMaximum();
262 
263  std::vector<long double> vPoissonProbs(MaxEntry + 1, 0);
264  long double meanVal = 1. * histo._NEntries / (1. * Nbins - histo._NEmptyBins);
265  evaluatePoissonian(vPoissonProbs, meanVal);
266 
267  // Find median occupancy, taking into account only good strips
268  unsigned int goodstripentries[128];
269  int nGoodStrips = 0;
270  for (size_t i = ibinStart; i < ibinStop; ++i) {
271  if (ishot[(apv * 128) + i - 1] == 0) {
272  goodstripentries[nGoodStrips] = (unsigned int)histo._th1f->GetBinContent(i);
273  nGoodStrips++;
274  }
275  }
276  double median = TMath::Median(nGoodStrips, goodstripentries);
277 
278  for (size_t i = ibinStart; i < ibinStop; ++i) {
279  unsigned int entries = (unsigned int)histo._th1f->GetBinContent(i);
280 
281  if (ishot[(apv * 128) + i - 1] == 0) {
282  stripoccupancy[(apv * 128) + i - 1] = entries / (double)Nevents_;
283  striphits[(apv * 128) + i - 1] = entries;
284  poissonprob[(apv * 128) + i - 1] = 1 - vPoissonProbs[entries];
285  medianapvhits[apv] = median;
286  avgapvhits[apv] = meanVal;
287  }
288  if (entries <= MinNumEntriesPerStrip_ || entries <= minNevents_ || entries / median < ratio_)
289  continue;
290 
291  if (diff < vPoissonProbs[entries]) {
292  ishot[(apv * 128) + i - 1] = 1;
294  hotstripsperapv[apv]++;
295  histo._th1f->SetBinContent(i, 0.);
296  histo._NEntries -= entries;
297  histo._NEmptyBins++;
298  if (edm::isDebugEnabled())
299  LogTrace("SiStripHotStrip") << " rejecting strip " << (apv * 128) + i - 1 << " value " << entries << " diff "
300  << diff << " prob " << vPoissonProbs[entries] << std::endl;
301  vect.push_back(pQuality->encode((apv * 128) + i - 1, 1, 0));
302  }
303  }
304  if (edm::isDebugEnabled())
305  LogTrace("SiStripHotStrip") << " [SiStripHotStripAlgorithmFromClusterOccupancy::iterativeSearch] Nbins=" << Nbins
306  << " MaxEntry=" << MaxEntry << " meanVal=" << meanVal
307  << " NEmptyBins=" << histo._NEmptyBins << " NEntries=" << histo._NEntries
308  << " thEntries " << histo._th1f->GetEntries() << " startingSize " << startingSize
309  << " vector.size " << vect.size() << std::endl;
310 
311  if (vect.size() != startingSize)
312  iterativeSearch(histo, vect, apv);
313 }
314 
315 void SiStripHotStripAlgorithmFromClusterOccupancy::evaluatePoissonian(std::vector<long double>& vPoissonProbs,
316  long double& meanVal) {
317  for (size_t i = 0; i < vPoissonProbs.size(); ++i) {
318  vPoissonProbs[i] = (i == 0) ? TMath::Poisson(i, meanVal) : vPoissonProbs[i - 1] + TMath::Poisson(i, meanVal);
319  }
320 }
321 
323  Nevents_ = Nevents;
325  if (edm::isDebugEnabled())
326  LogTrace("SiStripHotStrip") << " [SiStripHotStripAlgorithmFromClusterOccupancy::setNumberOfEvents] minNumber of "
327  "Events per strip used to consider a strip bad"
328  << minNevents_ << " for occupancy " << occupancy_ << std::endl;
329 }
bool IsApvBad(const uint32_t &detid, const short &apvNb) const
bool isDebugEnabled()
SiStripHotStripAlgorithmFromClusterOccupancy(const edm::ParameterSet &, const TrackerTopology *)
void extractBadStrips(SiStripQuality *, HistoMap &, edm::ESHandle< SiStripQuality > &)
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
unsigned int tecRing(const DetId &id) const
ring id
T y() const
Definition: PV3DBase.h:63
void evaluatePoissonian(std::vector< long double > &, long double &meanVal)
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_[]
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 > &)
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
void iterativeSearch(pHisto &, std::vector< unsigned int > &, int)
bool tibIsStereo(const DetId &id) const
unsigned int tobModule(const DetId &id) const
std::pair< ContainerIterator, ContainerIterator > Range
const TrackerGeomDet * idToDet(DetId) const override
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