Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:22

0001 // system include files
0002 #include <cmath>
0003 #include <cstdio>
0004 #include <fstream>
0005 #include <iomanip>
0006 #include <iostream>
0007 #include <string>
0008 #include <sys/time.h>
0009 
0010 // user include files
0011 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0012 #include "CondFormats/DataRecord/interface/SiPixelFedCablingMapRcd.h"
0013 #include "CondFormats/DataRecord/interface/SiPixelQualityFromDbRcd.h"
0014 #include "CondFormats/DataRecord/interface/SiPixelQualityRcd.h"
0015 #include "CondFormats/SiPixelObjects/interface/PixelFEDCabling.h"
0016 #include "CondFormats/SiPixelObjects/interface/PixelROC.h"
0017 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCabling.h"
0018 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
0019 #include "CondFormats/SiPixelObjects/interface/SiPixelQuality.h"
0020 #include "DataFormats/DetId/interface/DetId.h"
0021 #include "DataFormats/DetId/interface/DetId.h"
0022 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0023 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0024 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0025 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/EventSetup.h"
0028 #include "FWCore/Framework/interface/Frameworkfwd.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0031 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/ServiceRegistry/interface/Service.h"
0034 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0035 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0036 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0037 
0038 // ROOT includes
0039 #include "TCanvas.h"
0040 #include "TStyle.h"
0041 #include "TROOT.h"
0042 #include "TFile.h"
0043 #include "TTree.h"
0044 #include "TBranch.h"
0045 #include "TH2F.h"
0046 
0047 class SiPixelBadModuleReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0048 public:
0049   explicit SiPixelBadModuleReader(const edm::ParameterSet &);
0050   ~SiPixelBadModuleReader() override;
0051 
0052   void analyze(const edm::Event &, const edm::EventSetup &) override;
0053 
0054 private:
0055   const edm::ESGetToken<SiPixelQuality, SiPixelQualityRcd> badModuleToken;
0056   const edm::ESGetToken<SiPixelQuality, SiPixelQualityFromDbRcd> badModuleFromDBToken;
0057   const edm::ESGetToken<SiPixelFedCablingMap, SiPixelFedCablingMapRcd> siPixelFedCablingToken;
0058   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tkGeomToken;
0059   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tkTopoToken;
0060 
0061   const uint32_t printdebug_;
0062   const std::string whichRcd;
0063 
0064   TH2F *_TH2F_dead_modules_BPIX_lay1;
0065   TH2F *_TH2F_dead_modules_BPIX_lay2;
0066   TH2F *_TH2F_dead_modules_BPIX_lay3;
0067   TH2F *_TH2F_dead_modules_FPIX_minusZ_disk1;
0068   TH2F *_TH2F_dead_modules_FPIX_minusZ_disk2;
0069   TH2F *_TH2F_dead_modules_FPIX_plusZ_disk1;
0070   TH2F *_TH2F_dead_modules_FPIX_plusZ_disk2;
0071 };
0072 
0073 SiPixelBadModuleReader::SiPixelBadModuleReader(const edm::ParameterSet &iConfig)
0074     : badModuleToken(esConsumes()),
0075       badModuleFromDBToken(esConsumes()),
0076       siPixelFedCablingToken(esConsumes()),
0077       tkGeomToken(esConsumes()),
0078       tkTopoToken(esConsumes()),
0079       printdebug_(iConfig.getUntrackedParameter<uint32_t>("printDebug", 1)),
0080       whichRcd(iConfig.getUntrackedParameter<std::string>("RcdName")) {
0081   usesResource(TFileService::kSharedResource);
0082 }
0083 // txtFileName_(iConfig.getUntrackedParameter<std::string>("OutputFile","BadModuleSummary.txt")){}
0084 
0085 SiPixelBadModuleReader::~SiPixelBadModuleReader() = default;
0086 
0087 void SiPixelBadModuleReader::analyze(const edm::Event &e, const edm::EventSetup &iSetup) {
0088   const SiPixelQuality *SiPixelBadModule_ = nullptr;
0089   if (whichRcd == "SiPixelQualityRcd") {
0090     SiPixelBadModule_ = &iSetup.getData(badModuleToken);
0091   } else if (whichRcd == "SiPixelQualityFromDbRcd") {
0092     SiPixelBadModule_ = &iSetup.getData(badModuleFromDBToken);
0093   } else {
0094     throw cms::Exception("LogicalError") << "SiPixelBadModuleReader::analyze, unsupported RcdName value " << whichRcd
0095                                          << ".\n Please check the configuration." << std::endl;
0096   }
0097 
0098   edm::ESHandle<SiPixelFedCablingMap> map = iSetup.getHandle(siPixelFedCablingToken);
0099   edm::LogInfo("SiPixelBadModuleReader") << "[SiPixelBadModuleReader::analyze] End Reading SiPixelBadModule"
0100                                          << std::endl;
0101 
0102   const TrackerGeometry *geom = &iSetup.getData(tkGeomToken);
0103   const TrackerTopology &ttopo = iSetup.getData(tkTopoToken);
0104 
0105   edm::Service<TFileService> fs;
0106   _TH2F_dead_modules_BPIX_lay1 =
0107       fs->make<TH2F>("dead_modules_BPIX_lay1", "Dead modules in BPIX Layer 1", 112, -28., 28., 100, -3.2, 3.2);
0108   _TH2F_dead_modules_BPIX_lay2 =
0109       fs->make<TH2F>("dead_modules_BPIX_lay2", "Dead modules in BPIX Layer 2", 112, -28., 28., 100, -3.2, 3.2);
0110   _TH2F_dead_modules_BPIX_lay3 =
0111       fs->make<TH2F>("dead_modules_BPIX_lay3", "Dead modules in BPIX Layer 3", 112, -28., 28., 100, -3.2, 3.2);
0112   _TH2F_dead_modules_FPIX_minusZ_disk1 =
0113       fs->make<TH2F>("dead_modules_minusZ_disk1", "Dead modules in FPIX minus Z disk 1", 80, -18., 18., 80, -18., 18.);
0114   _TH2F_dead_modules_FPIX_minusZ_disk2 =
0115       fs->make<TH2F>("dead_modules_minusZ_disk2", "Dead modules in FPIX minus Z disk 2", 80, -18., 18., 80, -18., 18.);
0116   _TH2F_dead_modules_FPIX_plusZ_disk1 =
0117       fs->make<TH2F>("dead_modules_plusZ_disk1", "Dead modules in FPIX plus Z disk 1", 80, -18., 18., 80, -18., 18.);
0118   _TH2F_dead_modules_FPIX_plusZ_disk2 =
0119       fs->make<TH2F>("dead_modules_plusZ_disk2", "Dead modules in BPIX plus Z disk 2", 80, -18, 18., 80, -18., 18.);
0120 
0121   gStyle->SetPalette(1);
0122 
0123   std::vector<SiPixelQuality::disabledModuleType> disabledModules = SiPixelBadModule_->getBadComponentList();
0124 
0125   if (printdebug_) {
0126     std::ofstream debugout("BadModuleDebug.txt");
0127     debugout << "Values stored in DB, in human readable form: " << std::endl;
0128     for (size_t id = 0; id < disabledModules.size(); id++) {
0129       SiPixelQuality::disabledModuleType badmodule = disabledModules[id];
0130 
0131       //////////////////////////////////////
0132       //  errortype "whole" = int 0 in DB //
0133       //  errortype "tbmA" = int 1 in DB  //
0134       //  errortype "tbmB" = int 2 in DB  //
0135       //////////////////////////////////////
0136 
0137       std::string errorstring;
0138 
0139       if (badmodule.errorType == 0)
0140         errorstring = "whole";
0141       else if (badmodule.errorType == 1)
0142         errorstring = "tbmA";
0143       else if (badmodule.errorType == 2)
0144         errorstring = "tbmB";
0145       else if (badmodule.errorType == 3)
0146         errorstring = "none";
0147 
0148       debugout << " " << std::endl;
0149       debugout << " " << std::endl;  //to make the reading easier
0150       debugout << "DetID: " << badmodule.DetID << " and this has an error type of '" << errorstring << "'" << std::endl;
0151       debugout << "The bad ROCs are: " << std::endl;
0152       for (unsigned short n = 0; n < 16; n++) {
0153         unsigned short mask = 1 << n;  // 1 << n = 2^{n} using bitwise shift
0154         if (badmodule.BadRocs & mask)
0155           debugout << n << ", ";
0156       }
0157       debugout << std::endl;
0158       debugout << ttopo.print(badmodule.DetID) << std::endl;
0159       const auto &plane = geom->idToDet(badmodule.DetID)->surface();
0160       debugout << "phiSpan " << plane.phiSpan().first << "," << plane.phiSpan().second << std::endl;
0161       debugout << "rSpan " << plane.rSpan().first << "," << plane.rSpan().second << std::endl;
0162       debugout << "zSpan " << plane.zSpan().first << "," << plane.zSpan().second << std::endl;
0163       debugout << " " << std::endl;
0164       debugout << " " << std::endl;  //to make the reading easier
0165     }
0166   }
0167 
0168   int nmodules = 0;
0169   int nbadmodules = 0;
0170   int npartialbad = 0;
0171   for (TrackerGeometry::DetContainer::const_iterator it = geom->dets().begin(); it != geom->dets().end(); it++) {
0172     if (dynamic_cast<PixelGeomDetUnit const *>((*it)) != nullptr) {
0173       DetId detId = (*it)->geographicalId();
0174       uint32_t id = detId();
0175       nmodules++;
0176 
0177       const GeomDetUnit *geoUnit = geom->idToDetUnit(detId);
0178       const PixelGeomDetUnit *pixDet = dynamic_cast<const PixelGeomDetUnit *>(geoUnit);
0179       float detR = pixDet->surface().position().perp();
0180       float detZ = pixDet->surface().position().z();
0181       float detPhi = pixDet->surface().position().phi();
0182       //      float detEta = -1.*log(tan(atan2(detR,detZ)/2.));
0183       float detX = detR * cos(detPhi);
0184       float detY = detR * sin(detPhi);
0185 
0186       //Histograms in "colz":  those with 2 hits are totally fine.  Those with 1 are dead.  Done this way to visualize where ROCs are
0187       //fill histograms for ALL modules
0188       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel) &&
0189           PixelBarrelName(detId).layerName() == 1) {
0190         _TH2F_dead_modules_BPIX_lay1->Fill(detZ, detPhi);
0191         _TH2F_dead_modules_BPIX_lay1->SetOption("colz");
0192       }
0193 
0194       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel) &&
0195           PixelBarrelName(detId).layerName() == 2) {
0196         _TH2F_dead_modules_BPIX_lay2->Fill(detZ, detPhi);
0197         _TH2F_dead_modules_BPIX_lay2->SetOption("colz");
0198       }
0199 
0200       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel) &&
0201           PixelBarrelName(detId).layerName() == 3) {
0202         _TH2F_dead_modules_BPIX_lay3->Fill(detZ, detPhi);
0203         _TH2F_dead_modules_BPIX_lay3->SetOption("colz");
0204       }
0205       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0206           PixelEndcapName(detId).diskName() == 1 &&
0207           (PixelEndcapName(detId).halfCylinder() == 2 ||
0208            PixelEndcapName(detId).halfCylinder() == 1)) {  //mI = 2, mO = 1
0209         _TH2F_dead_modules_FPIX_minusZ_disk1->Fill(detX, detY);
0210         _TH2F_dead_modules_FPIX_minusZ_disk1->SetOption("colz");
0211       }
0212 
0213       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0214           PixelEndcapName(detId).diskName() == 2 &&
0215           (PixelEndcapName(detId).halfCylinder() == 2 ||
0216            PixelEndcapName(detId).halfCylinder() == 1)) {  //mI = 2, mO = 1
0217         _TH2F_dead_modules_FPIX_minusZ_disk2->Fill(detX, detY);
0218         _TH2F_dead_modules_FPIX_minusZ_disk2->SetOption("colz");
0219       }
0220 
0221       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0222           PixelEndcapName(detId).diskName() == 1 &&
0223           (PixelEndcapName(detId).halfCylinder() == 3 ||
0224            PixelEndcapName(detId).halfCylinder() == 4)) {  //p0 = 3, pI = 4
0225         _TH2F_dead_modules_FPIX_plusZ_disk1->Fill(detX, detY);
0226         _TH2F_dead_modules_FPIX_plusZ_disk1->SetOption("colz");
0227       }
0228 
0229       if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0230           PixelEndcapName(detId).diskName() == 2 &&
0231           (PixelEndcapName(detId).halfCylinder() == 3 ||
0232            PixelEndcapName(detId).halfCylinder() == 4)) {  //p0 = 3, pI = 4
0233         _TH2F_dead_modules_FPIX_plusZ_disk2->Fill(detX, detY);
0234         _TH2F_dead_modules_FPIX_plusZ_disk2->SetOption("colz");
0235       }
0236 
0237       //fill histograms for when all ROCs are OK
0238       if (SiPixelBadModule_->IsModuleBad(id) == false && SiPixelBadModule_->getBadRocs(id) == 0) {
0239         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel) &&
0240             PixelBarrelName(detId).layerName() == 1) {
0241           _TH2F_dead_modules_BPIX_lay1->Fill(detZ, detPhi);
0242           _TH2F_dead_modules_BPIX_lay1->SetOption("colz");
0243         }
0244 
0245         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel) &&
0246             PixelBarrelName(detId).layerName() == 2) {
0247           _TH2F_dead_modules_BPIX_lay2->Fill(detZ, detPhi);
0248           _TH2F_dead_modules_BPIX_lay2->SetOption("colz");
0249         }
0250 
0251         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel) &&
0252             PixelBarrelName(detId).layerName() == 3) {
0253           _TH2F_dead_modules_BPIX_lay3->Fill(detZ, detPhi);
0254           _TH2F_dead_modules_BPIX_lay3->SetOption("colz");
0255         }
0256         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0257             PixelEndcapName(detId).diskName() == 1 &&
0258             (PixelEndcapName(detId).halfCylinder() == 2 ||
0259              PixelEndcapName(detId).halfCylinder() == 1)) {  //mI = 2, mO = 1
0260           _TH2F_dead_modules_FPIX_minusZ_disk1->Fill(detX, detY);
0261           _TH2F_dead_modules_FPIX_minusZ_disk1->SetOption("colz");
0262         }
0263 
0264         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0265             PixelEndcapName(detId).diskName() == 2 &&
0266             (PixelEndcapName(detId).halfCylinder() == 2 ||
0267              PixelEndcapName(detId).halfCylinder() == 1)) {  //mI = 2, mO = 1
0268           _TH2F_dead_modules_FPIX_minusZ_disk2->Fill(detX, detY);
0269           _TH2F_dead_modules_FPIX_minusZ_disk2->SetOption("colz");
0270         }
0271 
0272         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0273             PixelEndcapName(detId).diskName() == 1 &&
0274             (PixelEndcapName(detId).halfCylinder() == 3 ||
0275              PixelEndcapName(detId).halfCylinder() == 4)) {  //p0 = 3, pI = 4
0276           _TH2F_dead_modules_FPIX_plusZ_disk1->Fill(detX, detY);
0277           _TH2F_dead_modules_FPIX_plusZ_disk1->SetOption("colz");
0278         }
0279 
0280         if (detId.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap) &&
0281             PixelEndcapName(detId).diskName() == 2 &&
0282             (PixelEndcapName(detId).halfCylinder() == 3 ||
0283              PixelEndcapName(detId).halfCylinder() == 4)) {  //p0 = 3, pI = 4
0284           _TH2F_dead_modules_FPIX_plusZ_disk2->Fill(detX, detY);
0285           _TH2F_dead_modules_FPIX_plusZ_disk2->SetOption("colz");
0286         }
0287       }
0288 
0289       //count number of completely dead modules
0290       if (SiPixelBadModule_->IsModuleBad(id) == true) {
0291         nbadmodules++;
0292       }
0293 
0294       //count number of partially dead modules
0295       if (SiPixelBadModule_->IsModuleBad(id) == false && SiPixelBadModule_->getBadRocs(id) != 0) {
0296         npartialbad++;
0297       }
0298     }
0299   }
0300 
0301   std::ofstream txtout("BadModuleSummary.txt");
0302   txtout << "The total number of modules is: " << nmodules << std::endl;
0303   txtout << "The total number of completely dead modules is: " << nbadmodules << std::endl;
0304   txtout << "The total number of partially dead modules is: " << npartialbad << std::endl;
0305 }
0306 DEFINE_FWK_MODULE(SiPixelBadModuleReader);