CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/CalibTracker/SiStripQuality/src/SiStripBadAPVAlgorithmFromClusterOccupancy.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiStripQuality/interface/SiStripBadAPVAlgorithmFromClusterOccupancy.h"
00002 
00003 
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "DataFormats/DetId/interface/DetId.h"
00006 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00007 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00008 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00009 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00010 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
00011 #include "Geometry/CommonTopologies/interface/StripTopology.h"
00012 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
00013 
00014 
00015 SiStripBadAPVAlgorithmFromClusterOccupancy::SiStripBadAPVAlgorithmFromClusterOccupancy(const edm::ParameterSet& iConfig):
00016   lowoccupancy_(0),
00017   highoccupancy_(100),
00018   absolutelow_(0),
00019   numberiterations_(2),
00020   Nevents_(0),
00021   occupancy_(0),
00022   OutFileName_("Occupancy.root"),
00023   UseInputDB_(iConfig.getUntrackedParameter<bool>("UseInputDB",false))
00024   {
00025     minNevents_=Nevents_*occupancy_;
00026   }
00027 
00028 SiStripBadAPVAlgorithmFromClusterOccupancy::~SiStripBadAPVAlgorithmFromClusterOccupancy(){
00029   LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")<<"[SiStripBadAPVAlgorithmFromClusterOccupancy::~SiStripBadAPVAlgorithmFromClusterOccupancy] "<<std::endl;
00030 }
00031 
00032 void SiStripBadAPVAlgorithmFromClusterOccupancy::extractBadAPVs(SiStripQuality* siStripQuality,HistoMap& DM, edm::ESHandle<SiStripQuality>& inSiStripQuality){
00033 
00034   LogTrace("SiStripBadAPVAlgorithmFromClusterOccupancy")<<"[SiStripBadAPVAlgorithmFromClusterOccupancy::extractBadAPVs] "<<std::endl;
00035 
00036   if (WriteOutputFile_==true){
00037   f = new TFile(OutFileName_.c_str(),"RECREATE");
00038   f->cd();
00039 
00040   apvtree = new TTree("moduleOccupancy","tree");
00041 
00042   apvtree->Branch("DetRawId",                &detrawid,                "DetRawId/I");
00043   apvtree->Branch("SubDetId",                &subdetid,                "SubDetId/I");
00044   apvtree->Branch("Layer_Ring",              &layer_ring,              "Layer_Ring/I");
00045   apvtree->Branch("Disc",                    &disc,                    "Disc/I");
00046   apvtree->Branch("IsBack",                  &isback,                  "IsBack/I");
00047   apvtree->Branch("IsExternalString",        &isexternalstring,        "IsExternalString/I");
00048   apvtree->Branch("IsZMinusSide",            &iszminusside,            "IsZMinusSide/I");
00049   apvtree->Branch("RodStringPetal",          &rodstringpetal,          "RodStringPetal/I");
00050   apvtree->Branch("IsStereo",                &isstereo,                "IsStereo/I");
00051   apvtree->Branch("ModuleNumber",            &module_number,           "ModuleNumber/I");
00052   apvtree->Branch("NumberOfStrips",          &number_strips,           "NumberOfStrips/I");
00053   apvtree->Branch("APVGlobalPositionX",      &global_position_x,       "APVGlobalPositionX/F");
00054   apvtree->Branch("APVGlobalPositionY",      &global_position_y,       "APVGlobalPositionY/F");
00055   apvtree->Branch("APVGlobalPositionZ",      &global_position_z,       "APVGlobalPositionZ/F");
00056   apvtree->Branch("APVNumber",               &apv_number,              "APVNumber/I");
00057   apvtree->Branch("APVAbsoluteOccupancy",    &apvAbsoluteOccupancy,    "apvAbsoluteOccupancy/D");
00058   apvtree->Branch("APVMedianOccupancy",      &apvMedianOccupancy,      "apvMedianOccupancy/D");
00059   }
00060 
00061   HistoMap::iterator it=DM.begin();
00062   HistoMap::iterator itEnd=DM.end();
00063   std::vector<unsigned int> badStripList;
00064   uint32_t detid;
00065   for (;it!=itEnd;++it){
00066 
00067     Apv APV;
00068 
00069     for (int apv=0; apv<6; apv++)
00070       {
00071         APV.apvMedian[apv]            = 0;
00072         APV.apvabsoluteOccupancy[apv] = 0;
00073 
00074         for (int strip=0; strip<128; strip++)
00075           {
00076             stripOccupancy[apv][strip] = 0;
00077             stripWeight[apv][strip]    = 0;
00078           }
00079       }
00080 
00081     pHisto phisto;
00082     phisto._th1f = it->second.get();
00083     phisto._NEntries = (int)phisto._th1f->GetEntries();
00084     phisto._NBins = phisto._th1f->GetNbinsX();
00085 
00086     number_strips  = (int)phisto._NBins;
00087     number_apvs    = number_strips/128;
00088     APV.numberApvs = number_apvs;
00089 
00090     for (int apv=0; apv<number_apvs; apv++)
00091       {
00092         for (int strip=0; strip<128; strip++)
00093           {
00094             stripOccupancy[apv][strip]     = phisto._th1f->GetBinContent((apv*128)+strip+1); // Remember: Bin=0 is underflow bin!
00095             stripWeight[apv][strip]        = 1;
00096             APV.apvabsoluteOccupancy[apv] += phisto._th1f->GetBinContent((apv*128)+strip+1); // Remember: Bin=0 is underflow bin!
00097           }
00098       }
00099 
00100     for (int apv=0; apv<number_apvs; apv++)
00101       {
00102         APV.apvMedian[apv] = TMath::Median(128,stripOccupancy[apv],stripWeight[apv]);
00103       }
00104 
00105     detid=it->first;
00106     DetId detectorId=DetId(detid);
00107 
00108     if (edm::isDebugEnabled())
00109       LogTrace("SiStripBadAPV") << "Analyzing detid " << detid<< std::endl;
00110 
00111     detrawid     = detid;
00112     APV.detrawId = detrawid;
00113     subdetid     = detectorId.subdetId();
00114     if (SiStripDetId(detrawid).stereo() !=0 ) isstereo = 1; // It's a stereo module
00115     else                                      isstereo = 0; // It's an rphi module
00116     switch (detectorId.subdetId())
00117       {
00118       case StripSubdetector::TIB :
00119         layer_ring = TIBDetId(detrawid).layer();
00120         disc       = -1;
00121         isback     = -1;
00122         if (TIBDetId(detrawid).isExternalString()) isexternalstring = 1;
00123         else                                       isexternalstring = 0;
00124         if (TIBDetId(detrawid).isZMinusSide()) iszminusside = 1;
00125         else                                   iszminusside = 0;
00126         rodstringpetal     = TIBDetId(detrawid).stringNumber();
00127         module_number      = TIBDetId(detrawid).moduleNumber();
00128         APV.modulePosition = module_number;
00129 
00130         if      (layer_ring == 1) medianValues_TIB_Layer1.push_back(APV);
00131         else if (layer_ring == 2) medianValues_TIB_Layer2.push_back(APV);
00132         else if (layer_ring == 3) medianValues_TIB_Layer3.push_back(APV);
00133         else if (layer_ring == 4) medianValues_TIB_Layer4.push_back(APV);
00134         break;
00135 
00136       case StripSubdetector::TID :
00137         layer_ring = TIDDetId(detrawid).ring();
00138         disc       = TIDDetId(detrawid).wheel();
00139         if (TIDDetId(detrawid).isBackRing()) isback = 1;
00140         else                                 isback = 0;
00141         if (TIDDetId(detrawid).isZMinusSide()) iszminusside = 1;
00142         else                                   iszminusside = 0;
00143         isexternalstring   = -1;
00144         rodstringpetal     = -1;
00145         module_number      = TIDDetId(detrawid).moduleNumber();
00146         APV.modulePosition = layer_ring;
00147 
00148         if (iszminusside==0)
00149           {
00150             if      (disc==1) medianValues_TIDPlus_Disc1.push_back(APV);
00151             else if (disc==2) medianValues_TIDPlus_Disc2.push_back(APV);
00152             else if (disc==3) medianValues_TIDPlus_Disc3.push_back(APV);
00153           }
00154         else if (iszminusside==1)
00155           {
00156             if      (disc==1) medianValues_TIDMinus_Disc1.push_back(APV);
00157             else if (disc==2) medianValues_TIDMinus_Disc2.push_back(APV);
00158             else if (disc==3) medianValues_TIDMinus_Disc3.push_back(APV);
00159           }
00160         break;
00161 
00162       case StripSubdetector::TOB :
00163         layer_ring = TOBDetId(detrawid).layer();
00164         disc       = -1;
00165         isback     = -1;
00166         if (TOBDetId(detrawid).isZMinusSide()) iszminusside = 1;
00167         else                                   iszminusside = 0;
00168         isexternalstring   = -1;
00169         rodstringpetal     = TOBDetId(detrawid).rodNumber();
00170         module_number      = TOBDetId(detrawid).moduleNumber();
00171         APV.modulePosition = module_number;
00172 
00173         if      (layer_ring == 1) medianValues_TOB_Layer1.push_back(APV);
00174         else if (layer_ring == 2) medianValues_TOB_Layer2.push_back(APV);
00175         else if (layer_ring == 3) medianValues_TOB_Layer3.push_back(APV);
00176         else if (layer_ring == 4) medianValues_TOB_Layer4.push_back(APV);
00177         else if (layer_ring == 5) medianValues_TOB_Layer5.push_back(APV);
00178         else if (layer_ring == 6) medianValues_TOB_Layer6.push_back(APV);
00179         break;
00180 
00181       case StripSubdetector::TEC :
00182         layer_ring = TECDetId(detrawid).ring();
00183         disc       = TECDetId(detrawid).wheel();
00184         if (TECDetId(detrawid).isBackPetal()) isback = 1;
00185         else                                  isback = 0;
00186         if (TECDetId(detrawid).isZMinusSide()) iszminusside = 1;
00187         else                                   iszminusside = 0;
00188         isexternalstring   = -1;
00189         rodstringpetal     = TECDetId(detrawid).petalNumber();
00190         module_number      = TECDetId(detrawid).moduleNumber();
00191         APV.modulePosition = layer_ring;
00192 
00193         if (iszminusside==0)
00194           {
00195             if      (disc==1) medianValues_TECPlus_Disc1.push_back(APV);
00196             else if (disc==2) medianValues_TECPlus_Disc2.push_back(APV);
00197             else if (disc==3) medianValues_TECPlus_Disc3.push_back(APV);
00198             else if (disc==4) medianValues_TECPlus_Disc4.push_back(APV);
00199             else if (disc==5) medianValues_TECPlus_Disc5.push_back(APV);
00200             else if (disc==6) medianValues_TECPlus_Disc6.push_back(APV);
00201             else if (disc==7) medianValues_TECPlus_Disc7.push_back(APV);
00202             else if (disc==8) medianValues_TECPlus_Disc8.push_back(APV);
00203             else if (disc==9) medianValues_TECPlus_Disc9.push_back(APV);
00204           }
00205         else if (iszminusside==1)
00206           {
00207             if      (disc==1) medianValues_TECMinus_Disc1.push_back(APV);
00208             else if (disc==2) medianValues_TECMinus_Disc2.push_back(APV);
00209             else if (disc==3) medianValues_TECMinus_Disc3.push_back(APV);
00210             else if (disc==4) medianValues_TECMinus_Disc4.push_back(APV);
00211             else if (disc==5) medianValues_TECMinus_Disc5.push_back(APV);
00212             else if (disc==6) medianValues_TECMinus_Disc6.push_back(APV);
00213             else if (disc==7) medianValues_TECMinus_Disc7.push_back(APV);
00214             else if (disc==8) medianValues_TECMinus_Disc8.push_back(APV);
00215             else if (disc==9) medianValues_TECMinus_Disc9.push_back(APV);
00216           }
00217         break;
00218 
00219       default :
00220         std::cout << "### Detector does not belong to TIB, TID, TOB or TEC !? ###" << std::endl;
00221         std::cout << "### DetRawId: " << detrawid << " ###" << std::endl;
00222       }
00223 
00224     const StripGeomDetUnit*  theStripDet = dynamic_cast<const StripGeomDetUnit*>( (TkGeom->idToDet(detectorId)) );
00225     const StripTopology* theStripTopol   = dynamic_cast<const StripTopology*>( &(theStripDet->specificTopology()) );
00226 
00227     for (int apv=0; apv<number_apvs; apv++)
00228       {
00229         apv_number           = apv+1;
00230         apvMedianOccupancy   = APV.apvMedian[apv];
00231         apvAbsoluteOccupancy = APV.apvabsoluteOccupancy[apv];
00232 
00233         LocalPoint  pos_strip_local  = theStripTopol->localPosition((apv*128));
00234         GlobalPoint pos_strip_global = (TkGeom->idToDet(detectorId))->surface().toGlobal(pos_strip_local);
00235 
00236         global_position_x = pos_strip_global.x();
00237         global_position_y = pos_strip_global.y();
00238         global_position_z = pos_strip_global.z();
00239 
00240         if (WriteOutputFile_==true) apvtree->Fill();
00241       }
00242 
00243   } // end loop on modules
00244 
00245   // Calculate Mean and RMS for each Layer
00246   CalculateMeanAndRMS(medianValues_TIB_Layer1,MeanAndRms_TIB_Layer1,numberiterations_);
00247   CalculateMeanAndRMS(medianValues_TIB_Layer2,MeanAndRms_TIB_Layer2,numberiterations_);
00248   CalculateMeanAndRMS(medianValues_TIB_Layer3,MeanAndRms_TIB_Layer3,numberiterations_);
00249   CalculateMeanAndRMS(medianValues_TIB_Layer4,MeanAndRms_TIB_Layer4,numberiterations_);
00250 
00251   CalculateMeanAndRMS(medianValues_TOB_Layer1,MeanAndRms_TOB_Layer1,numberiterations_);
00252   CalculateMeanAndRMS(medianValues_TOB_Layer2,MeanAndRms_TOB_Layer2,numberiterations_);
00253   CalculateMeanAndRMS(medianValues_TOB_Layer3,MeanAndRms_TOB_Layer3,numberiterations_);
00254   CalculateMeanAndRMS(medianValues_TOB_Layer4,MeanAndRms_TOB_Layer4,numberiterations_);
00255   CalculateMeanAndRMS(medianValues_TOB_Layer5,MeanAndRms_TOB_Layer5,numberiterations_);
00256   CalculateMeanAndRMS(medianValues_TOB_Layer6,MeanAndRms_TOB_Layer6,numberiterations_);
00257 
00258   CalculateMeanAndRMS(medianValues_TIDPlus_Disc1,MeanAndRms_TIDPlus_Disc1,numberiterations_);
00259   CalculateMeanAndRMS(medianValues_TIDPlus_Disc2,MeanAndRms_TIDPlus_Disc2,numberiterations_);
00260   CalculateMeanAndRMS(medianValues_TIDPlus_Disc3,MeanAndRms_TIDPlus_Disc3,numberiterations_);
00261   CalculateMeanAndRMS(medianValues_TIDMinus_Disc1,MeanAndRms_TIDMinus_Disc1,numberiterations_);
00262   CalculateMeanAndRMS(medianValues_TIDMinus_Disc2,MeanAndRms_TIDMinus_Disc2,numberiterations_);
00263   CalculateMeanAndRMS(medianValues_TIDMinus_Disc3,MeanAndRms_TIDMinus_Disc3,numberiterations_);
00264 
00265   CalculateMeanAndRMS(medianValues_TECPlus_Disc1,MeanAndRms_TECPlus_Disc1,numberiterations_);
00266   CalculateMeanAndRMS(medianValues_TECPlus_Disc2,MeanAndRms_TECPlus_Disc2,numberiterations_);
00267   CalculateMeanAndRMS(medianValues_TECPlus_Disc3,MeanAndRms_TECPlus_Disc3,numberiterations_);
00268   CalculateMeanAndRMS(medianValues_TECPlus_Disc4,MeanAndRms_TECPlus_Disc4,numberiterations_);
00269   CalculateMeanAndRMS(medianValues_TECPlus_Disc5,MeanAndRms_TECPlus_Disc5,numberiterations_);
00270   CalculateMeanAndRMS(medianValues_TECPlus_Disc6,MeanAndRms_TECPlus_Disc6,numberiterations_);
00271   CalculateMeanAndRMS(medianValues_TECPlus_Disc7,MeanAndRms_TECPlus_Disc7,numberiterations_);
00272   CalculateMeanAndRMS(medianValues_TECPlus_Disc8,MeanAndRms_TECPlus_Disc8,numberiterations_);
00273   CalculateMeanAndRMS(medianValues_TECPlus_Disc9,MeanAndRms_TECPlus_Disc9,numberiterations_);
00274 
00275   CalculateMeanAndRMS(medianValues_TECMinus_Disc1,MeanAndRms_TECMinus_Disc1,numberiterations_);
00276   CalculateMeanAndRMS(medianValues_TECMinus_Disc2,MeanAndRms_TECMinus_Disc2,numberiterations_);
00277   CalculateMeanAndRMS(medianValues_TECMinus_Disc3,MeanAndRms_TECMinus_Disc3,numberiterations_);
00278   CalculateMeanAndRMS(medianValues_TECMinus_Disc4,MeanAndRms_TECMinus_Disc4,numberiterations_);
00279   CalculateMeanAndRMS(medianValues_TECMinus_Disc5,MeanAndRms_TECMinus_Disc5,numberiterations_);
00280   CalculateMeanAndRMS(medianValues_TECMinus_Disc6,MeanAndRms_TECMinus_Disc6,numberiterations_);
00281   CalculateMeanAndRMS(medianValues_TECMinus_Disc7,MeanAndRms_TECMinus_Disc7,numberiterations_);
00282   CalculateMeanAndRMS(medianValues_TECMinus_Disc8,MeanAndRms_TECMinus_Disc8,numberiterations_);
00283   CalculateMeanAndRMS(medianValues_TECMinus_Disc9,MeanAndRms_TECMinus_Disc9,numberiterations_);
00284 
00285   pQuality=siStripQuality;
00286   badStripList.clear();
00287 
00288   // Analyze the APV Occupancy for hot APVs
00289   AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer1,MeanAndRms_TIB_Layer1,badStripList,inSiStripQuality);
00290   AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer2,MeanAndRms_TIB_Layer2,badStripList,inSiStripQuality);
00291   AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer3,MeanAndRms_TIB_Layer3,badStripList,inSiStripQuality);
00292   AnalyzeOccupancy(siStripQuality,medianValues_TIB_Layer4,MeanAndRms_TIB_Layer4,badStripList,inSiStripQuality);
00293 
00294   AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer1,MeanAndRms_TOB_Layer1,badStripList,inSiStripQuality);
00295   AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer2,MeanAndRms_TOB_Layer2,badStripList,inSiStripQuality);
00296   AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer3,MeanAndRms_TOB_Layer3,badStripList,inSiStripQuality);
00297   AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer4,MeanAndRms_TOB_Layer4,badStripList,inSiStripQuality);
00298   AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer5,MeanAndRms_TOB_Layer5,badStripList,inSiStripQuality);
00299   AnalyzeOccupancy(siStripQuality,medianValues_TOB_Layer6,MeanAndRms_TOB_Layer6,badStripList,inSiStripQuality);
00300 
00301   AnalyzeOccupancy(siStripQuality,medianValues_TIDPlus_Disc1,MeanAndRms_TIDPlus_Disc1,badStripList,inSiStripQuality);
00302   AnalyzeOccupancy(siStripQuality,medianValues_TIDPlus_Disc2,MeanAndRms_TIDPlus_Disc2,badStripList,inSiStripQuality);
00303   AnalyzeOccupancy(siStripQuality,medianValues_TIDPlus_Disc3,MeanAndRms_TIDPlus_Disc3,badStripList,inSiStripQuality);
00304   AnalyzeOccupancy(siStripQuality,medianValues_TIDMinus_Disc1,MeanAndRms_TIDMinus_Disc1,badStripList,inSiStripQuality);
00305   AnalyzeOccupancy(siStripQuality,medianValues_TIDMinus_Disc2,MeanAndRms_TIDMinus_Disc2,badStripList,inSiStripQuality);
00306   AnalyzeOccupancy(siStripQuality,medianValues_TIDMinus_Disc3,MeanAndRms_TIDMinus_Disc3,badStripList,inSiStripQuality);
00307 
00308   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc1,MeanAndRms_TECPlus_Disc1,badStripList,inSiStripQuality);
00309   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc2,MeanAndRms_TECPlus_Disc2,badStripList,inSiStripQuality);
00310   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc3,MeanAndRms_TECPlus_Disc3,badStripList,inSiStripQuality);
00311   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc4,MeanAndRms_TECPlus_Disc4,badStripList,inSiStripQuality);
00312   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc5,MeanAndRms_TECPlus_Disc5,badStripList,inSiStripQuality);
00313   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc6,MeanAndRms_TECPlus_Disc6,badStripList,inSiStripQuality);
00314   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc7,MeanAndRms_TECPlus_Disc7,badStripList,inSiStripQuality);
00315   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc8,MeanAndRms_TECPlus_Disc8,badStripList,inSiStripQuality);
00316   AnalyzeOccupancy(siStripQuality,medianValues_TECPlus_Disc9,MeanAndRms_TECPlus_Disc9,badStripList,inSiStripQuality);
00317 
00318   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc1,MeanAndRms_TECMinus_Disc1,badStripList,inSiStripQuality);
00319   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc2,MeanAndRms_TECMinus_Disc2,badStripList,inSiStripQuality);
00320   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc3,MeanAndRms_TECMinus_Disc3,badStripList,inSiStripQuality);
00321   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc4,MeanAndRms_TECMinus_Disc4,badStripList,inSiStripQuality);
00322   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc5,MeanAndRms_TECMinus_Disc5,badStripList,inSiStripQuality);
00323   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc6,MeanAndRms_TECMinus_Disc6,badStripList,inSiStripQuality);
00324   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc7,MeanAndRms_TECMinus_Disc7,badStripList,inSiStripQuality);
00325   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc8,MeanAndRms_TECMinus_Disc8,badStripList,inSiStripQuality);
00326   AnalyzeOccupancy(siStripQuality,medianValues_TECMinus_Disc9,MeanAndRms_TECMinus_Disc9,badStripList,inSiStripQuality);
00327 
00328   siStripQuality->fillBadComponents();
00329 
00330   if (WriteOutputFile_==true){
00331   f->cd();
00332   apvtree->Write();
00333   f->Close();
00334   }
00335 
00336   LogTrace("SiStripBadAPV") << ss.str() << std::endl;
00337 }
00338 
00339 
00340 void SiStripBadAPVAlgorithmFromClusterOccupancy::CalculateMeanAndRMS(std::vector<Apv> a, std::pair<double,double>* MeanRMS, int number_iterations)
00341 {
00342   Double_t tot[7], tot2[7];
00343   Double_t n[7];
00344 
00345   Double_t Mean[7] = {0};
00346   Double_t Rms[7]  = {1000,1000,1000,1000,1000,1000,1000};
00347 
00348   int Moduleposition;
00349 
00350   for (int i=0; i<number_iterations; i++)
00351     {
00352       for (int j=0; j<7; j++)
00353         {
00354           n[j]    = 0;
00355           tot[j]  = 0;
00356           tot2[j] = 0;
00357         }
00358 
00359       for (uint32_t it=0; it<a.size(); it++)
00360         {
00361           Moduleposition = (a[it].modulePosition)-1;
00362 
00363           for (int apv=0; apv<a[it].numberApvs; apv++)
00364             {
00365               if (i>0)
00366                 {
00367                   if (a[it].apvMedian[apv]<(Mean[Moduleposition]-3*Rms[Moduleposition]) || (a[it].apvMedian[apv]>(Mean[Moduleposition]+5*Rms[Moduleposition])))
00368                     {
00369                       continue;
00370                     }
00371                 }
00372               tot[Moduleposition]  += a[it].apvMedian[apv];
00373               tot2[Moduleposition] += (a[it].apvMedian[apv])*(a[it].apvMedian[apv]);
00374               n[Moduleposition]++;
00375             }
00376         }
00377 
00378       for (int j=0; j<7; j++)
00379         {
00380           if (n[j]!=0)
00381             {
00382               Mean[j] = tot[j]/n[j];
00383               Rms[j]  = TMath::Sqrt(TMath::Abs(tot2[j]/n[j] -Mean[j]*Mean[j]));
00384             }
00385         }
00386     }
00387 
00388   for (int j=0; j<7; j++)
00389     {
00390       MeanRMS[j] = std::make_pair(Mean[j],Rms[j]);
00391     }
00392 
00393 }
00394 
00395 void SiStripBadAPVAlgorithmFromClusterOccupancy::AnalyzeOccupancy(SiStripQuality* quality, std::vector<Apv>& medianValues, std::pair<double,double>* MeanAndRms, std::vector<unsigned int>& BadStripList, edm::ESHandle<SiStripQuality>& InSiStripQuality)
00396 {
00397   int Moduleposition;
00398   uint32_t Detid;
00399 
00400   for (uint32_t it=0; it<medianValues.size(); it++)
00401     {
00402       Moduleposition = (medianValues[it].modulePosition)-1;
00403       Detid          = medianValues[it].detrawId;
00404 
00405       for (int apv=0; apv<medianValues[it].numberApvs; apv++)
00406         {
00407           if(UseInputDB_)
00408             {
00409               if(InSiStripQuality->IsApvBad(Detid,apv) )
00410                 {
00411                   continue;//if the apv is already flagged as bad, continue.
00412                 }
00413             }
00414           if (medianValues[it].apvMedian[apv] > minNevents_)
00415             {
00416               if ((medianValues[it].apvMedian[apv]>(MeanAndRms[Moduleposition].first+highoccupancy_*MeanAndRms[Moduleposition].second)) && (medianValues[it].apvMedian[apv]>absolutelow_))
00417                 BadStripList.push_back(pQuality->encode((apv*128),128,0));
00418             }
00419           else if (medianValues[it].apvMedian[apv]<(MeanAndRms[Moduleposition].first-lowoccupancy_*MeanAndRms[Moduleposition].second) && (MeanAndRms[Moduleposition].first>2 || medianValues[it].apvabsoluteOccupancy[apv]==0))
00420             {
00421               BadStripList.push_back(pQuality->encode((apv*128),128,0));
00422               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;
00423             }
00424         }
00425       if (BadStripList.begin()!=BadStripList.end())
00426         {
00427           quality->compact(Detid,BadStripList);
00428           SiStripQuality::Range range(BadStripList.begin(),BadStripList.end());
00429           quality->put(Detid,range);
00430         }
00431       BadStripList.clear();
00432     }
00433 }
00434 
00435 void SiStripBadAPVAlgorithmFromClusterOccupancy::setMinNumOfEvents()
00436 {
00437   minNevents_=occupancy_*Nevents_;
00438 }