Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:29:40

0001 #include "CalibTracker/SiStripQuality/interface/SiStripBadAPVAlgorithmFromClusterOccupancy.h"
0002 
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "DataFormats/DetId/interface/DetId.h"
0005 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0006 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
0007 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
0008 #include "Geometry/CommonTopologies/interface/StripTopology.h"
0009 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0010 
0011 SiStripBadAPVAlgorithmFromClusterOccupancy::SiStripBadAPVAlgorithmFromClusterOccupancy(const edm::ParameterSet& iConfig,
0012                                                                                        const TrackerTopology* theTopo)
0013     : lowoccupancy_(0),
0014       highoccupancy_(100),
0015       absolutelow_(0),
0016       numberiterations_(2),
0017       Nevents_(0),
0018       occupancy_(0),
0019       OutFileName_("Occupancy.root"),
0020       UseInputDB_(iConfig.getUntrackedParameter<bool>("UseInputDB", false)),
0021       tTopo(theTopo) {
0022   minNevents_ = Nevents_ * occupancy_;
0023 }
0024 
0025 SiStripBadAPVAlgorithmFromClusterOccupancy::~SiStripBadAPVAlgorithmFromClusterOccupancy() {
0026   LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")
0027       << "[SiStripBadAPVAlgorithmFromClusterOccupancy::~SiStripBadAPVAlgorithmFromClusterOccupancy] " << std::endl;
0028 }
0029 
0030 void SiStripBadAPVAlgorithmFromClusterOccupancy::extractBadAPVs(SiStripQuality* siStripQuality,
0031                                                                 HistoMap& DM,
0032                                                                 const SiStripQuality* inSiStripQuality) {
0033   LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")
0034       << "[SiStripBadAPVAlgorithmFromClusterOccupancy::extractBadAPVs] " << std::endl;
0035 
0036   if (WriteOutputFile_ == true) {
0037     f = new TFile(OutFileName_.c_str(), "RECREATE");
0038     f->cd();
0039 
0040     apvtree = new TTree("moduleOccupancy", "tree");
0041 
0042     apvtree->Branch("DetRawId", &detrawid, "DetRawId/I");
0043     apvtree->Branch("SubDetId", &subdetid, "SubDetId/I");
0044     apvtree->Branch("Layer_Ring", &layer_ring, "Layer_Ring/I");
0045     apvtree->Branch("Disc", &disc, "Disc/I");
0046     apvtree->Branch("IsBack", &isback, "IsBack/I");
0047     apvtree->Branch("IsExternalString", &isexternalstring, "IsExternalString/I");
0048     apvtree->Branch("IsZMinusSide", &iszminusside, "IsZMinusSide/I");
0049     apvtree->Branch("RodStringPetal", &rodstringpetal, "RodStringPetal/I");
0050     apvtree->Branch("IsStereo", &isstereo, "IsStereo/I");
0051     apvtree->Branch("ModuleNumber", &module_number, "ModuleNumber/I");
0052     apvtree->Branch("NumberOfStrips", &number_strips, "NumberOfStrips/I");
0053     apvtree->Branch("APVGlobalPositionX", &global_position_x, "APVGlobalPositionX/F");
0054     apvtree->Branch("APVGlobalPositionY", &global_position_y, "APVGlobalPositionY/F");
0055     apvtree->Branch("APVGlobalPositionZ", &global_position_z, "APVGlobalPositionZ/F");
0056     apvtree->Branch("APVNumber", &apv_number, "APVNumber/I");
0057     apvtree->Branch("APVAbsoluteOccupancy", &apvAbsoluteOccupancy, "apvAbsoluteOccupancy/D");
0058     apvtree->Branch("APVMedianOccupancy", &apvMedianOccupancy, "apvMedianOccupancy/D");
0059   }
0060 
0061   HistoMap::iterator it = DM.begin();
0062   HistoMap::iterator itEnd = DM.end();
0063   std::vector<unsigned int> badStripList;
0064   uint32_t detid;
0065   for (; it != itEnd; ++it) {
0066     Apv APV;
0067 
0068     for (int apv = 0; apv < 6; apv++) {
0069       APV.apvMedian[apv] = 0;
0070       APV.apvabsoluteOccupancy[apv] = 0;
0071 
0072       for (int strip = 0; strip < 128; strip++) {
0073         stripOccupancy[apv][strip] = 0;
0074         stripWeight[apv][strip] = 0;
0075       }
0076     }
0077 
0078     pHisto phisto;
0079     phisto._th1f = it->second.get();
0080     phisto._NEntries = (int)phisto._th1f->GetEntries();
0081     phisto._NBins = phisto._th1f->GetNbinsX();
0082 
0083     number_strips = (int)phisto._NBins;
0084     number_apvs = number_strips / 128;
0085     APV.numberApvs = number_apvs;
0086 
0087     for (int apv = 0; apv < number_apvs; apv++) {
0088       for (int strip = 0; strip < 128; strip++) {
0089         stripOccupancy[apv][strip] =
0090             phisto._th1f->GetBinContent((apv * 128) + strip + 1);  // Remember: Bin=0 is underflow bin!
0091         stripWeight[apv][strip] = 1;
0092         APV.apvabsoluteOccupancy[apv] +=
0093             phisto._th1f->GetBinContent((apv * 128) + strip + 1);  // Remember: Bin=0 is underflow bin!
0094       }
0095     }
0096 
0097     for (int apv = 0; apv < number_apvs; apv++) {
0098       APV.apvMedian[apv] = TMath::Median(128, stripOccupancy[apv], stripWeight[apv]);
0099     }
0100 
0101     detid = it->first;
0102     DetId detectorId = DetId(detid);
0103 
0104     if (edm::isDebugEnabled())
0105       LogTrace("SiStripBadAPV") << "Analyzing detid " << detid << std::endl;
0106 
0107     detrawid = detid;
0108     APV.detrawId = detrawid;
0109     subdetid = detectorId.subdetId();
0110     switch (detectorId.subdetId()) {
0111       case StripSubdetector::TIB:
0112         layer_ring = tTopo->tibLayer(detrawid);
0113         disc = -1;
0114         isstereo = tTopo->tibIsStereo(detrawid);
0115         isback = -1;
0116         if (tTopo->tibIsExternalString(detrawid))
0117           isexternalstring = 1;
0118         else
0119           isexternalstring = 0;
0120         if (tTopo->tibIsZMinusSide(detrawid))
0121           iszminusside = 1;
0122         else
0123           iszminusside = 0;
0124         rodstringpetal = tTopo->tibString(detrawid);
0125         module_number = tTopo->tibModule(detrawid);
0126         APV.modulePosition = module_number;
0127 
0128         if (layer_ring == 1)
0129           medianValues_TIB_Layer1.push_back(APV);
0130         else if (layer_ring == 2)
0131           medianValues_TIB_Layer2.push_back(APV);
0132         else if (layer_ring == 3)
0133           medianValues_TIB_Layer3.push_back(APV);
0134         else if (layer_ring == 4)
0135           medianValues_TIB_Layer4.push_back(APV);
0136         break;
0137 
0138       case StripSubdetector::TID:
0139         layer_ring = tTopo->tidRing(detrawid);
0140         disc = tTopo->tidWheel(detrawid);
0141         isstereo = tTopo->tidIsStereo(detrawid);
0142         if (tTopo->tidIsBackRing(detrawid))
0143           isback = 1;
0144         else
0145           isback = 0;
0146         if (tTopo->tidIsZMinusSide(detrawid))
0147           iszminusside = 1;
0148         else
0149           iszminusside = 0;
0150         isexternalstring = -1;
0151         rodstringpetal = -1;
0152         module_number = tTopo->tidModule(detrawid);
0153         APV.modulePosition = layer_ring;
0154 
0155         if (iszminusside == 0) {
0156           if (disc == 1)
0157             medianValues_TIDPlus_Disc1.push_back(APV);
0158           else if (disc == 2)
0159             medianValues_TIDPlus_Disc2.push_back(APV);
0160           else if (disc == 3)
0161             medianValues_TIDPlus_Disc3.push_back(APV);
0162         } else if (iszminusside == 1) {
0163           if (disc == 1)
0164             medianValues_TIDMinus_Disc1.push_back(APV);
0165           else if (disc == 2)
0166             medianValues_TIDMinus_Disc2.push_back(APV);
0167           else if (disc == 3)
0168             medianValues_TIDMinus_Disc3.push_back(APV);
0169         }
0170         break;
0171 
0172       case StripSubdetector::TOB:
0173         layer_ring = tTopo->tobLayer(detrawid);
0174         disc = -1;
0175         isstereo = tTopo->tobIsStereo(detrawid);
0176         isback = -1;
0177         if (tTopo->tobIsZMinusSide(detrawid))
0178           iszminusside = 1;
0179         else
0180           iszminusside = 0;
0181         isexternalstring = -1;
0182         rodstringpetal = tTopo->tobRod(detrawid);
0183         module_number = tTopo->tobModule(detrawid);
0184         APV.modulePosition = module_number;
0185 
0186         if (layer_ring == 1)
0187           medianValues_TOB_Layer1.push_back(APV);
0188         else if (layer_ring == 2)
0189           medianValues_TOB_Layer2.push_back(APV);
0190         else if (layer_ring == 3)
0191           medianValues_TOB_Layer3.push_back(APV);
0192         else if (layer_ring == 4)
0193           medianValues_TOB_Layer4.push_back(APV);
0194         else if (layer_ring == 5)
0195           medianValues_TOB_Layer5.push_back(APV);
0196         else if (layer_ring == 6)
0197           medianValues_TOB_Layer6.push_back(APV);
0198         break;
0199 
0200       case StripSubdetector::TEC:
0201         layer_ring = tTopo->tecRing(detrawid);
0202         disc = tTopo->tecWheel(detrawid);
0203         isstereo = tTopo->tecIsStereo(detrawid);
0204         if (tTopo->tecIsBackPetal(detrawid))
0205           isback = 1;
0206         else
0207           isback = 0;
0208         if (tTopo->tecIsZMinusSide(detrawid))
0209           iszminusside = 1;
0210         else
0211           iszminusside = 0;
0212         isexternalstring = -1;
0213         rodstringpetal = tTopo->tecPetalNumber(detrawid);
0214         module_number = tTopo->tecModule(detrawid);
0215         APV.modulePosition = layer_ring;
0216 
0217         if (iszminusside == 0) {
0218           if (disc == 1)
0219             medianValues_TECPlus_Disc1.push_back(APV);
0220           else if (disc == 2)
0221             medianValues_TECPlus_Disc2.push_back(APV);
0222           else if (disc == 3)
0223             medianValues_TECPlus_Disc3.push_back(APV);
0224           else if (disc == 4)
0225             medianValues_TECPlus_Disc4.push_back(APV);
0226           else if (disc == 5)
0227             medianValues_TECPlus_Disc5.push_back(APV);
0228           else if (disc == 6)
0229             medianValues_TECPlus_Disc6.push_back(APV);
0230           else if (disc == 7)
0231             medianValues_TECPlus_Disc7.push_back(APV);
0232           else if (disc == 8)
0233             medianValues_TECPlus_Disc8.push_back(APV);
0234           else if (disc == 9)
0235             medianValues_TECPlus_Disc9.push_back(APV);
0236         } else if (iszminusside == 1) {
0237           if (disc == 1)
0238             medianValues_TECMinus_Disc1.push_back(APV);
0239           else if (disc == 2)
0240             medianValues_TECMinus_Disc2.push_back(APV);
0241           else if (disc == 3)
0242             medianValues_TECMinus_Disc3.push_back(APV);
0243           else if (disc == 4)
0244             medianValues_TECMinus_Disc4.push_back(APV);
0245           else if (disc == 5)
0246             medianValues_TECMinus_Disc5.push_back(APV);
0247           else if (disc == 6)
0248             medianValues_TECMinus_Disc6.push_back(APV);
0249           else if (disc == 7)
0250             medianValues_TECMinus_Disc7.push_back(APV);
0251           else if (disc == 8)
0252             medianValues_TECMinus_Disc8.push_back(APV);
0253           else if (disc == 9)
0254             medianValues_TECMinus_Disc9.push_back(APV);
0255         }
0256         break;
0257 
0258       default:
0259         std::cout << "### Detector does not belong to TIB, TID, TOB or TEC !? ###" << std::endl;
0260         std::cout << "### DetRawId: " << detrawid << " ###" << std::endl;
0261     }
0262 
0263     const StripGeomDetUnit* theStripDet = dynamic_cast<const StripGeomDetUnit*>((TkGeom->idToDet(detectorId)));
0264     const StripTopology* theStripTopol = dynamic_cast<const StripTopology*>(&(theStripDet->specificTopology()));
0265 
0266     for (int apv = 0; apv < number_apvs; apv++) {
0267       apv_number = apv + 1;
0268       apvMedianOccupancy = APV.apvMedian[apv];
0269       apvAbsoluteOccupancy = APV.apvabsoluteOccupancy[apv];
0270 
0271       LocalPoint pos_strip_local = theStripTopol->localPosition((apv * 128));
0272       GlobalPoint pos_strip_global = (TkGeom->idToDet(detectorId))->surface().toGlobal(pos_strip_local);
0273 
0274       global_position_x = pos_strip_global.x();
0275       global_position_y = pos_strip_global.y();
0276       global_position_z = pos_strip_global.z();
0277 
0278       if (WriteOutputFile_ == true)
0279         apvtree->Fill();
0280     }
0281 
0282   }  // end loop on modules
0283 
0284   // Calculate Mean and RMS for each Layer
0285   CalculateMeanAndRMS(medianValues_TIB_Layer1, MeanAndRms_TIB_Layer1, numberiterations_);
0286   CalculateMeanAndRMS(medianValues_TIB_Layer2, MeanAndRms_TIB_Layer2, numberiterations_);
0287   CalculateMeanAndRMS(medianValues_TIB_Layer3, MeanAndRms_TIB_Layer3, numberiterations_);
0288   CalculateMeanAndRMS(medianValues_TIB_Layer4, MeanAndRms_TIB_Layer4, numberiterations_);
0289 
0290   CalculateMeanAndRMS(medianValues_TOB_Layer1, MeanAndRms_TOB_Layer1, numberiterations_);
0291   CalculateMeanAndRMS(medianValues_TOB_Layer2, MeanAndRms_TOB_Layer2, numberiterations_);
0292   CalculateMeanAndRMS(medianValues_TOB_Layer3, MeanAndRms_TOB_Layer3, numberiterations_);
0293   CalculateMeanAndRMS(medianValues_TOB_Layer4, MeanAndRms_TOB_Layer4, numberiterations_);
0294   CalculateMeanAndRMS(medianValues_TOB_Layer5, MeanAndRms_TOB_Layer5, numberiterations_);
0295   CalculateMeanAndRMS(medianValues_TOB_Layer6, MeanAndRms_TOB_Layer6, numberiterations_);
0296 
0297   CalculateMeanAndRMS(medianValues_TIDPlus_Disc1, MeanAndRms_TIDPlus_Disc1, numberiterations_);
0298   CalculateMeanAndRMS(medianValues_TIDPlus_Disc2, MeanAndRms_TIDPlus_Disc2, numberiterations_);
0299   CalculateMeanAndRMS(medianValues_TIDPlus_Disc3, MeanAndRms_TIDPlus_Disc3, numberiterations_);
0300   CalculateMeanAndRMS(medianValues_TIDMinus_Disc1, MeanAndRms_TIDMinus_Disc1, numberiterations_);
0301   CalculateMeanAndRMS(medianValues_TIDMinus_Disc2, MeanAndRms_TIDMinus_Disc2, numberiterations_);
0302   CalculateMeanAndRMS(medianValues_TIDMinus_Disc3, MeanAndRms_TIDMinus_Disc3, numberiterations_);
0303 
0304   CalculateMeanAndRMS(medianValues_TECPlus_Disc1, MeanAndRms_TECPlus_Disc1, numberiterations_);
0305   CalculateMeanAndRMS(medianValues_TECPlus_Disc2, MeanAndRms_TECPlus_Disc2, numberiterations_);
0306   CalculateMeanAndRMS(medianValues_TECPlus_Disc3, MeanAndRms_TECPlus_Disc3, numberiterations_);
0307   CalculateMeanAndRMS(medianValues_TECPlus_Disc4, MeanAndRms_TECPlus_Disc4, numberiterations_);
0308   CalculateMeanAndRMS(medianValues_TECPlus_Disc5, MeanAndRms_TECPlus_Disc5, numberiterations_);
0309   CalculateMeanAndRMS(medianValues_TECPlus_Disc6, MeanAndRms_TECPlus_Disc6, numberiterations_);
0310   CalculateMeanAndRMS(medianValues_TECPlus_Disc7, MeanAndRms_TECPlus_Disc7, numberiterations_);
0311   CalculateMeanAndRMS(medianValues_TECPlus_Disc8, MeanAndRms_TECPlus_Disc8, numberiterations_);
0312   CalculateMeanAndRMS(medianValues_TECPlus_Disc9, MeanAndRms_TECPlus_Disc9, numberiterations_);
0313 
0314   CalculateMeanAndRMS(medianValues_TECMinus_Disc1, MeanAndRms_TECMinus_Disc1, numberiterations_);
0315   CalculateMeanAndRMS(medianValues_TECMinus_Disc2, MeanAndRms_TECMinus_Disc2, numberiterations_);
0316   CalculateMeanAndRMS(medianValues_TECMinus_Disc3, MeanAndRms_TECMinus_Disc3, numberiterations_);
0317   CalculateMeanAndRMS(medianValues_TECMinus_Disc4, MeanAndRms_TECMinus_Disc4, numberiterations_);
0318   CalculateMeanAndRMS(medianValues_TECMinus_Disc5, MeanAndRms_TECMinus_Disc5, numberiterations_);
0319   CalculateMeanAndRMS(medianValues_TECMinus_Disc6, MeanAndRms_TECMinus_Disc6, numberiterations_);
0320   CalculateMeanAndRMS(medianValues_TECMinus_Disc7, MeanAndRms_TECMinus_Disc7, numberiterations_);
0321   CalculateMeanAndRMS(medianValues_TECMinus_Disc8, MeanAndRms_TECMinus_Disc8, numberiterations_);
0322   CalculateMeanAndRMS(medianValues_TECMinus_Disc9, MeanAndRms_TECMinus_Disc9, numberiterations_);
0323 
0324   pQuality = siStripQuality;
0325   badStripList.clear();
0326 
0327   // Analyze the APV Occupancy for hot APVs
0328   AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer1, MeanAndRms_TIB_Layer1, badStripList, inSiStripQuality);
0329   AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer2, MeanAndRms_TIB_Layer2, badStripList, inSiStripQuality);
0330   AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer3, MeanAndRms_TIB_Layer3, badStripList, inSiStripQuality);
0331   AnalyzeOccupancy(siStripQuality, medianValues_TIB_Layer4, MeanAndRms_TIB_Layer4, badStripList, inSiStripQuality);
0332 
0333   AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer1, MeanAndRms_TOB_Layer1, badStripList, inSiStripQuality);
0334   AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer2, MeanAndRms_TOB_Layer2, badStripList, inSiStripQuality);
0335   AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer3, MeanAndRms_TOB_Layer3, badStripList, inSiStripQuality);
0336   AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer4, MeanAndRms_TOB_Layer4, badStripList, inSiStripQuality);
0337   AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer5, MeanAndRms_TOB_Layer5, badStripList, inSiStripQuality);
0338   AnalyzeOccupancy(siStripQuality, medianValues_TOB_Layer6, MeanAndRms_TOB_Layer6, badStripList, inSiStripQuality);
0339 
0340   AnalyzeOccupancy(
0341       siStripQuality, medianValues_TIDPlus_Disc1, MeanAndRms_TIDPlus_Disc1, badStripList, inSiStripQuality);
0342   AnalyzeOccupancy(
0343       siStripQuality, medianValues_TIDPlus_Disc2, MeanAndRms_TIDPlus_Disc2, badStripList, inSiStripQuality);
0344   AnalyzeOccupancy(
0345       siStripQuality, medianValues_TIDPlus_Disc3, MeanAndRms_TIDPlus_Disc3, badStripList, inSiStripQuality);
0346   AnalyzeOccupancy(
0347       siStripQuality, medianValues_TIDMinus_Disc1, MeanAndRms_TIDMinus_Disc1, badStripList, inSiStripQuality);
0348   AnalyzeOccupancy(
0349       siStripQuality, medianValues_TIDMinus_Disc2, MeanAndRms_TIDMinus_Disc2, badStripList, inSiStripQuality);
0350   AnalyzeOccupancy(
0351       siStripQuality, medianValues_TIDMinus_Disc3, MeanAndRms_TIDMinus_Disc3, badStripList, inSiStripQuality);
0352 
0353   AnalyzeOccupancy(
0354       siStripQuality, medianValues_TECPlus_Disc1, MeanAndRms_TECPlus_Disc1, badStripList, inSiStripQuality);
0355   AnalyzeOccupancy(
0356       siStripQuality, medianValues_TECPlus_Disc2, MeanAndRms_TECPlus_Disc2, badStripList, inSiStripQuality);
0357   AnalyzeOccupancy(
0358       siStripQuality, medianValues_TECPlus_Disc3, MeanAndRms_TECPlus_Disc3, badStripList, inSiStripQuality);
0359   AnalyzeOccupancy(
0360       siStripQuality, medianValues_TECPlus_Disc4, MeanAndRms_TECPlus_Disc4, badStripList, inSiStripQuality);
0361   AnalyzeOccupancy(
0362       siStripQuality, medianValues_TECPlus_Disc5, MeanAndRms_TECPlus_Disc5, badStripList, inSiStripQuality);
0363   AnalyzeOccupancy(
0364       siStripQuality, medianValues_TECPlus_Disc6, MeanAndRms_TECPlus_Disc6, badStripList, inSiStripQuality);
0365   AnalyzeOccupancy(
0366       siStripQuality, medianValues_TECPlus_Disc7, MeanAndRms_TECPlus_Disc7, badStripList, inSiStripQuality);
0367   AnalyzeOccupancy(
0368       siStripQuality, medianValues_TECPlus_Disc8, MeanAndRms_TECPlus_Disc8, badStripList, inSiStripQuality);
0369   AnalyzeOccupancy(
0370       siStripQuality, medianValues_TECPlus_Disc9, MeanAndRms_TECPlus_Disc9, badStripList, inSiStripQuality);
0371 
0372   AnalyzeOccupancy(
0373       siStripQuality, medianValues_TECMinus_Disc1, MeanAndRms_TECMinus_Disc1, badStripList, inSiStripQuality);
0374   AnalyzeOccupancy(
0375       siStripQuality, medianValues_TECMinus_Disc2, MeanAndRms_TECMinus_Disc2, badStripList, inSiStripQuality);
0376   AnalyzeOccupancy(
0377       siStripQuality, medianValues_TECMinus_Disc3, MeanAndRms_TECMinus_Disc3, badStripList, inSiStripQuality);
0378   AnalyzeOccupancy(
0379       siStripQuality, medianValues_TECMinus_Disc4, MeanAndRms_TECMinus_Disc4, badStripList, inSiStripQuality);
0380   AnalyzeOccupancy(
0381       siStripQuality, medianValues_TECMinus_Disc5, MeanAndRms_TECMinus_Disc5, badStripList, inSiStripQuality);
0382   AnalyzeOccupancy(
0383       siStripQuality, medianValues_TECMinus_Disc6, MeanAndRms_TECMinus_Disc6, badStripList, inSiStripQuality);
0384   AnalyzeOccupancy(
0385       siStripQuality, medianValues_TECMinus_Disc7, MeanAndRms_TECMinus_Disc7, badStripList, inSiStripQuality);
0386   AnalyzeOccupancy(
0387       siStripQuality, medianValues_TECMinus_Disc8, MeanAndRms_TECMinus_Disc8, badStripList, inSiStripQuality);
0388   AnalyzeOccupancy(
0389       siStripQuality, medianValues_TECMinus_Disc9, MeanAndRms_TECMinus_Disc9, badStripList, inSiStripQuality);
0390 
0391   siStripQuality->fillBadComponents();
0392 
0393   if (WriteOutputFile_ == true) {
0394     f->cd();
0395     apvtree->Write();
0396     f->Close();
0397   }
0398 
0399   LogTrace("SiStripBadAPV") << ss.str() << std::endl;
0400 }
0401 
0402 void SiStripBadAPVAlgorithmFromClusterOccupancy::CalculateMeanAndRMS(const std::vector<Apv>& a,
0403                                                                      std::pair<double, double>* MeanRMS,
0404                                                                      int number_iterations) {
0405   Double_t tot[7], tot2[7];
0406   Double_t n[7];
0407 
0408   Double_t Mean[7] = {0};
0409   Double_t Rms[7] = {1000, 1000, 1000, 1000, 1000, 1000, 1000};
0410 
0411   int Moduleposition;
0412 
0413   for (int i = 0; i < number_iterations; i++) {
0414     for (int j = 0; j < 7; j++) {
0415       n[j] = 0;
0416       tot[j] = 0;
0417       tot2[j] = 0;
0418     }
0419 
0420     for (uint32_t it = 0; it < a.size(); it++) {
0421       Moduleposition = (a[it].modulePosition) - 1;
0422 
0423       for (int apv = 0; apv < a[it].numberApvs; apv++) {
0424         if (i > 0) {
0425           if (a[it].apvMedian[apv] < (Mean[Moduleposition] - 3 * Rms[Moduleposition]) ||
0426               (a[it].apvMedian[apv] > (Mean[Moduleposition] + 5 * Rms[Moduleposition]))) {
0427             continue;
0428           }
0429         }
0430         tot[Moduleposition] += a[it].apvMedian[apv];
0431         tot2[Moduleposition] += (a[it].apvMedian[apv]) * (a[it].apvMedian[apv]);
0432         n[Moduleposition]++;
0433       }
0434     }
0435 
0436     for (int j = 0; j < 7; j++) {
0437       if (n[j] != 0) {
0438         Mean[j] = tot[j] / n[j];
0439         Rms[j] = TMath::Sqrt(TMath::Abs(tot2[j] / n[j] - Mean[j] * Mean[j]));
0440       }
0441     }
0442   }
0443 
0444   for (int j = 0; j < 7; j++) {
0445     MeanRMS[j] = std::make_pair(Mean[j], Rms[j]);
0446   }
0447 }
0448 
0449 void SiStripBadAPVAlgorithmFromClusterOccupancy::AnalyzeOccupancy(SiStripQuality* quality,
0450                                                                   std::vector<Apv>& medianValues,
0451                                                                   std::pair<double, double>* MeanAndRms,
0452                                                                   std::vector<unsigned int>& BadStripList,
0453                                                                   const SiStripQuality* InSiStripQuality) {
0454   int Moduleposition;
0455   uint32_t Detid;
0456 
0457   for (uint32_t it = 0; it < medianValues.size(); it++) {
0458     Moduleposition = (medianValues[it].modulePosition) - 1;
0459     Detid = medianValues[it].detrawId;
0460 
0461     for (int apv = 0; apv < medianValues[it].numberApvs; apv++) {
0462       if (UseInputDB_) {
0463         if (InSiStripQuality->IsApvBad(Detid, apv)) {
0464           continue;  //if the apv is already flagged as bad, continue.
0465         }
0466       }
0467       if (medianValues[it].apvMedian[apv] > minNevents_) {
0468         if ((medianValues[it].apvMedian[apv] >
0469              (MeanAndRms[Moduleposition].first + highoccupancy_ * MeanAndRms[Moduleposition].second)) &&
0470             (medianValues[it].apvMedian[apv] > absolutelow_))
0471           BadStripList.push_back(pQuality->encode((apv * 128), 128, 0));
0472       } else if (medianValues[it].apvMedian[apv] <
0473                      (MeanAndRms[Moduleposition].first - lowoccupancy_ * MeanAndRms[Moduleposition].second) &&
0474                  (MeanAndRms[Moduleposition].first > 2 || medianValues[it].apvabsoluteOccupancy[apv] == 0)) {
0475         BadStripList.push_back(pQuality->encode((apv * 128), 128, 0));
0476         std::cout << "Dead APV! DetId: " << medianValues[it].detrawId << ", APV number: " << apv + 1
0477                   << ", APVMedian: " << medianValues[it].apvMedian[apv]
0478                   << ", Mean: " << MeanAndRms[Moduleposition].first << ", RMS: " << MeanAndRms[Moduleposition].second
0479                   << ", LowThreshold: " << lowoccupancy_ << ", Mean-Low*RMS: "
0480                   << (MeanAndRms[Moduleposition].first - lowoccupancy_ * MeanAndRms[Moduleposition].second)
0481                   << std::endl;
0482       }
0483     }
0484     if (BadStripList.begin() != BadStripList.end()) {
0485       quality->compact(Detid, BadStripList);
0486       SiStripQuality::Range range(BadStripList.begin(), BadStripList.end());
0487       quality->put(Detid, range);
0488     }
0489     BadStripList.clear();
0490   }
0491 }
0492 
0493 void SiStripBadAPVAlgorithmFromClusterOccupancy::setMinNumOfEvents() { minNevents_ = occupancy_ * Nevents_; }