Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // system include files
0002 #include <cstdio>
0003 #include <iostream>
0004 #include <sys/time.h>
0005 
0006 // user include files
0007 #include "CondFormats/DataRecord/interface/SiPixelDynamicInefficiencyRcd.h"
0008 #include "CondFormats/SiPixelObjects/interface/SiPixelDynamicInefficiency.h"
0009 #include "DataFormats/DetId/interface/DetId.h"
0010 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0011 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0012 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0013 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/ServiceRegistry/interface/Service.h"
0022 #include "FWCore/ServiceRegistry/interface/Service.h"
0023 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0024 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0025 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0026 
0027 //
0028 //
0029 // class decleration
0030 //
0031 class SiPixelDynamicInefficiencyReader : public edm::one::EDAnalyzer<> {
0032 public:
0033   explicit SiPixelDynamicInefficiencyReader(const edm::ParameterSet&);
0034   ~SiPixelDynamicInefficiencyReader() override;
0035 
0036   void analyze(const edm::Event&, const edm::EventSetup&) override;
0037 
0038 private:
0039   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tkGeomToken;
0040   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tkTopoToken;
0041   const edm::ESGetToken<SiPixelDynamicInefficiency, SiPixelDynamicInefficiencyRcd> dynIneffToken;
0042 
0043   const bool printdebug_;
0044   double thePixelEfficiency[20];                     // Single pixel effciency
0045   double thePixelColEfficiency[20];                  // Column effciency
0046   double thePixelChipEfficiency[20];                 // ROC efficiency
0047   std::vector<double> theLadderEfficiency_BPix[20];  // Ladder efficiency
0048   std::vector<double> theModuleEfficiency_BPix[20];  // Module efficiency
0049   std::vector<double> thePUEfficiency[20];           // Instlumi dependent efficiency
0050   double theInnerEfficiency_FPix[20];                // Fpix inner module efficiency
0051   double theOuterEfficiency_FPix[20];                // Fpix outer module efficiency
0052 };
0053 
0054 using namespace cms;
0055 
0056 SiPixelDynamicInefficiencyReader::SiPixelDynamicInefficiencyReader(const edm::ParameterSet& iConfig)
0057     : tkGeomToken(esConsumes()),
0058       tkTopoToken(esConsumes()),
0059       dynIneffToken(esConsumes()),
0060       printdebug_(iConfig.getUntrackedParameter<bool>("printDebug", false)) {
0061   //Load factors from config file, copied from SimTracker/SiPixelDigitizer/plugins/SiPixelDigitizerAlgorithm.cc
0062   int i = 0;
0063   thePixelColEfficiency[i++] = iConfig.getParameter<double>("thePixelColEfficiency_BPix1");
0064   thePixelColEfficiency[i++] = iConfig.getParameter<double>("thePixelColEfficiency_BPix2");
0065   thePixelColEfficiency[i++] = iConfig.getParameter<double>("thePixelColEfficiency_BPix3");
0066   i = 0;
0067   thePixelEfficiency[i++] = iConfig.getParameter<double>("thePixelEfficiency_BPix1");
0068   thePixelEfficiency[i++] = iConfig.getParameter<double>("thePixelEfficiency_BPix2");
0069   thePixelEfficiency[i++] = iConfig.getParameter<double>("thePixelEfficiency_BPix3");
0070   i = 0;
0071   thePixelChipEfficiency[i++] = iConfig.getParameter<double>("thePixelChipEfficiency_BPix1");
0072   thePixelChipEfficiency[i++] = iConfig.getParameter<double>("thePixelChipEfficiency_BPix2");
0073   thePixelChipEfficiency[i++] = iConfig.getParameter<double>("thePixelChipEfficiency_BPix3");
0074   i = 0;
0075   theLadderEfficiency_BPix[i++] = iConfig.getParameter<std::vector<double> >("theLadderEfficiency_BPix1");
0076   theLadderEfficiency_BPix[i++] = iConfig.getParameter<std::vector<double> >("theLadderEfficiency_BPix2");
0077   theLadderEfficiency_BPix[i++] = iConfig.getParameter<std::vector<double> >("theLadderEfficiency_BPix3");
0078   if ((theLadderEfficiency_BPix[0].size() != 20) || (theLadderEfficiency_BPix[1].size() != 32) ||
0079       (theLadderEfficiency_BPix[2].size() != 44))
0080     throw cms::Exception("Configuration") << "Wrong ladder number in efficiency config!";
0081   //
0082   i = 0;
0083   theModuleEfficiency_BPix[i++] = iConfig.getParameter<std::vector<double> >("theModuleEfficiency_BPix1");
0084   theModuleEfficiency_BPix[i++] = iConfig.getParameter<std::vector<double> >("theModuleEfficiency_BPix2");
0085   theModuleEfficiency_BPix[i++] = iConfig.getParameter<std::vector<double> >("theModuleEfficiency_BPix3");
0086   if ((theModuleEfficiency_BPix[0].size() != 4) || (theModuleEfficiency_BPix[1].size() != 4) ||
0087       (theModuleEfficiency_BPix[2].size() != 4))
0088     throw cms::Exception("Configuration") << "Wrong module number in efficiency config!";
0089   //
0090   i = 0;
0091   thePUEfficiency[i++] = iConfig.getParameter<std::vector<double> >("thePUEfficiency_BPix1");
0092   thePUEfficiency[i++] = iConfig.getParameter<std::vector<double> >("thePUEfficiency_BPix2");
0093   thePUEfficiency[i++] = iConfig.getParameter<std::vector<double> >("thePUEfficiency_BPix3");
0094   i = 3;
0095   thePixelColEfficiency[i++] = iConfig.getParameter<double>("thePixelColEfficiency_FPix1");
0096   thePixelColEfficiency[i++] = iConfig.getParameter<double>("thePixelColEfficiency_FPix2");
0097   i = 3;
0098   thePixelEfficiency[i++] = iConfig.getParameter<double>("thePixelEfficiency_FPix1");
0099   thePixelEfficiency[i++] = iConfig.getParameter<double>("thePixelEfficiency_FPix2");
0100   i = 3;
0101   thePixelChipEfficiency[i++] = iConfig.getParameter<double>("thePixelChipEfficiency_FPix1");
0102   thePixelChipEfficiency[i++] = iConfig.getParameter<double>("thePixelChipEfficiency_FPix2");
0103   //FPix Dynamic Inefficiency
0104   i = 3;
0105   theInnerEfficiency_FPix[i++] = iConfig.getParameter<double>("theInnerEfficiency_FPix1");
0106   theInnerEfficiency_FPix[i++] = iConfig.getParameter<double>("theInnerEfficiency_FPix2");
0107   i = 3;
0108   theOuterEfficiency_FPix[i++] = iConfig.getParameter<double>("theOuterEfficiency_FPix1");
0109   theOuterEfficiency_FPix[i++] = iConfig.getParameter<double>("theOuterEfficiency_FPix2");
0110   i = 3;
0111   thePUEfficiency[i++] = iConfig.getParameter<std::vector<double> >("thePUEfficiency_FPix_Inner");
0112   thePUEfficiency[i++] = iConfig.getParameter<std::vector<double> >("thePUEfficiency_FPix_Outer");
0113 }
0114 
0115 SiPixelDynamicInefficiencyReader::~SiPixelDynamicInefficiencyReader() = default;
0116 
0117 void SiPixelDynamicInefficiencyReader::analyze(const edm::Event& e, const edm::EventSetup& iSetup) {
0118   const SiPixelDynamicInefficiency* SiPixelDynamicInefficiency_ = &iSetup.getData(dynIneffToken);
0119   edm::LogInfo("SiPixelDynamicInefficiencyReader")
0120       << "[SiPixelDynamicInefficiencyReader::analyze] End Reading SiPixelDynamicInefficiency" << std::endl;
0121 
0122   std::map<unsigned int, double> map_pixelgeomfactor = SiPixelDynamicInefficiency_->getPixelGeomFactors();
0123   std::map<unsigned int, double> map_colgeomfactor = SiPixelDynamicInefficiency_->getColGeomFactors();
0124   std::map<unsigned int, double> map_chipgeomfactor = SiPixelDynamicInefficiency_->getChipGeomFactors();
0125   std::map<unsigned int, std::vector<double> > map_pufactor = SiPixelDynamicInefficiency_->getPUFactors();
0126   std::map<unsigned int, double>::const_iterator it_pixelgeom;
0127   std::map<unsigned int, double>::const_iterator it_colgeom;
0128   std::map<unsigned int, double>::const_iterator it_chipgeom;
0129   std::map<unsigned int, std::vector<double> >::const_iterator it_pu;
0130 
0131   edm::LogPrint("SiPixelDynamicInefficiencyReader") << "Printing out DB content:" << std::endl;
0132   for (it_pixelgeom = map_pixelgeomfactor.begin(); it_pixelgeom != map_pixelgeomfactor.end(); it_pixelgeom++) {
0133     printf("pixelgeom detid %x\tfactor %f\n", it_pixelgeom->first, it_pixelgeom->second);
0134   }
0135   for (it_colgeom = map_colgeomfactor.begin(); it_colgeom != map_colgeomfactor.end(); it_colgeom++) {
0136     printf("colgeom detid %x\tfactor %f\n", it_colgeom->first, it_colgeom->second);
0137   }
0138   for (it_chipgeom = map_chipgeomfactor.begin(); it_chipgeom != map_chipgeomfactor.end(); it_chipgeom++) {
0139     printf("chipgeom detid %x\tfactor %f\n", it_chipgeom->first, it_chipgeom->second);
0140   }
0141   for (it_pu = map_pufactor.begin(); it_pu != map_pufactor.end(); it_pu++) {
0142     printf("pu detid %x\t", it_pu->first);
0143     edm::LogPrint("SiPixelDynamicInefficiencyReader") << " Size of vector " << it_pu->second.size() << " elements:";
0144     if (it_pu->second.size() > 1) {
0145       for (unsigned int i = 0; i < it_pu->second.size(); i++) {
0146         edm::LogPrint("SiPixelDynamicInefficiencyReader") << " " << it_pu->second.at(i);
0147       }
0148       edm::LogPrint("SiPixelDynamicInefficiencyReader") << std::endl;
0149     } else {
0150       edm::LogPrint("SiPixelDynamicInefficiencyReader") << " " << it_pu->second.at(0) << std::endl;
0151     }
0152   }
0153   std::vector<uint32_t> detIdmasks = SiPixelDynamicInefficiency_->getDetIdmasks();
0154   for (unsigned int i = 0; i < detIdmasks.size(); i++)
0155     printf("DetId Mask: %x\t\n", detIdmasks.at(i));
0156   double theInstLumiScaleFactor = SiPixelDynamicInefficiency_->gettheInstLumiScaleFactor_();
0157   edm::LogPrint("SiPixelDynamicInefficiencyReader") << "theInstLumiScaleFactor " << theInstLumiScaleFactor << std::endl;
0158 
0159   //Comparing DB factors to config factors
0160   edm::LogPrint("SiPixelDynamicInefficiencyReader")
0161       << "\nCalculating factors/module and comparing it to config file factors...\n"
0162       << std::endl;
0163 
0164   const TrackerTopology* const tTopo = &iSetup.getData(tkTopoToken);
0165   const TrackerGeometry* pDD = &iSetup.getData(tkGeomToken);
0166   edm::LogInfo("SiPixelDynamicInefficiency (old)")
0167       << " There are " << pDD->detUnits().size() << " detectors (old)" << std::endl;
0168 
0169   const size_t pu_det = map_pufactor.size();
0170   double _pu_scale[pu_det];
0171   double _pu_scale_conf[pu_det];
0172   unsigned int match = 0, mismatch = 0, pu_match = 0, pu_mismatch = 0;
0173 
0174   for (const auto& it : pDD->detUnits()) {
0175     if (dynamic_cast<PixelGeomDetUnit const*>(it) == nullptr)
0176       continue;
0177     const DetId detid = it->geographicalId();
0178     double scale_db = 1;
0179 
0180     //Geom DB factor calculation
0181     for (it_colgeom = map_colgeomfactor.begin(); it_colgeom != map_colgeomfactor.end(); ++it_colgeom) {
0182       const DetId mapid = DetId(it_colgeom->first);
0183       if (mapid.subdetId() != detid.subdetId())
0184         continue;
0185       size_t __i = 0;
0186       for (; __i < detIdmasks.size(); __i++) {
0187         DetId maskid = DetId(detIdmasks.at(__i));
0188         if (maskid.subdetId() != mapid.subdetId())
0189           continue;
0190         if ((detid.rawId() & maskid.rawId()) != (mapid.rawId() & maskid.rawId()) &&
0191             (mapid.rawId() & maskid.rawId()) != DetId(mapid.det(), mapid.subdetId()).rawId())
0192           break;
0193       }
0194       if (__i != detIdmasks.size())
0195         continue;
0196       scale_db *= it_colgeom->second;
0197     }
0198 
0199     //DB PU factor calculation
0200     unsigned int pu_iterator = 0;
0201     for (it_pu = map_pufactor.begin(); it_pu != map_pufactor.end(); it_pu++, pu_iterator++) {
0202       const DetId mapid = DetId(it_pu->first);
0203       if (mapid.subdetId() != detid.subdetId())
0204         continue;
0205       size_t __i = 0;
0206       for (; __i < detIdmasks.size(); __i++) {
0207         DetId maskid = DetId(detIdmasks.at(__i));
0208         if (maskid.subdetId() != mapid.subdetId())
0209           continue;
0210         if ((detid.rawId() & maskid.rawId()) != (mapid.rawId() & maskid.rawId()) &&
0211             (mapid.rawId() & maskid.rawId()) != DetId(mapid.det(), mapid.subdetId()).rawId())
0212           break;
0213       }
0214       if (__i != detIdmasks.size())
0215         continue;
0216       double instlumi = 30 * theInstLumiScaleFactor;
0217       double instlumi_pow = 1.;
0218       _pu_scale[pu_iterator] = 0;
0219       for (size_t j = 0; j < it_pu->second.size(); j++) {
0220         _pu_scale[pu_iterator] += instlumi_pow * it_pu->second[j];
0221         instlumi_pow *= instlumi;
0222       }
0223     }
0224 
0225     //Config PU factor calculation
0226     for (size_t i = 0; i < pu_det; i++) {
0227       double instlumi = 30 * theInstLumiScaleFactor;
0228       double instlumi_pow = 1.;
0229       _pu_scale_conf[i] = 0;
0230       for (size_t j = 0; j < thePUEfficiency[i].size(); j++) {
0231         _pu_scale_conf[i] += instlumi_pow * thePUEfficiency[i][j];
0232         instlumi_pow *= instlumi;
0233       }
0234     }
0235 
0236     //Config geom factor calculation
0237     double columnEfficiency = 1;
0238     if (detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) {
0239       int layerIndex = tTopo->pxbLayer(detid.rawId());
0240       columnEfficiency = thePixelColEfficiency[layerIndex - 1];
0241       int ladder = tTopo->pxbLadder(detid.rawId());
0242       int module = tTopo->pxbModule(detid.rawId());
0243       if (module <= 4)
0244         module = 5 - module;
0245       else
0246         module -= 4;
0247 
0248       columnEfficiency *=
0249           theLadderEfficiency_BPix[layerIndex - 1][ladder - 1] * theModuleEfficiency_BPix[layerIndex - 1][module - 1];
0250     }
0251 
0252     if (detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) {
0253       unsigned int diskIndex = tTopo->layer(detid) + 3;  // Use diskIndex-1 later to stay consistent with BPix
0254       unsigned int panelIndex = tTopo->pxfPanel(detid);
0255       unsigned int moduleIndex = tTopo->pxfModule(detid);
0256       columnEfficiency = thePixelColEfficiency[diskIndex - 1];
0257       if ((panelIndex == 1 && (moduleIndex == 1 || moduleIndex == 2)) ||
0258           (panelIndex == 2 && moduleIndex == 1)) {  //inner modules
0259         columnEfficiency *= theInnerEfficiency_FPix[diskIndex - 1];
0260       } else {  //outer modules
0261         columnEfficiency *= theOuterEfficiency_FPix[diskIndex - 1];
0262       }
0263     }
0264 
0265     if (scale_db == columnEfficiency) {
0266       //printf("Config match, detid %x\tfactor %f\n",detid.rawId(),columnEfficiency);
0267       match++;
0268     } else {
0269       //printf("Config mismatch! detid %x\t db_geom_factor %f\tconf_geom_factor %f\n",detid.rawId(),scale_db,columnEfficiency);
0270       mismatch++;
0271     }
0272     for (unsigned int i = 0; i < pu_det; i++) {
0273       if (_pu_scale[i] != 0 && _pu_scale_conf[i] != 0 && _pu_scale[i] == _pu_scale_conf[i]) {
0274         //printf("Config match! detid %x\t db_pu_scale %f\tconf_pu_scale %f\n",detid.rawId(),_pu_scale[i],_pu_scale_conf[i]);
0275         pu_match++;
0276         break;
0277       }
0278       if (_pu_scale[i] != 0 && _pu_scale_conf[i] != 0 && _pu_scale[i] != _pu_scale_conf[i]) {
0279         //printf("Config mismatch! detid %x\t db_pu_scale %f\tconf_pu_scale %f\n",detid.rawId(),_pu_scale[i],_pu_scale_conf[i]);
0280         pu_mismatch++;
0281         continue;
0282       }
0283     }
0284   }
0285   edm::LogPrint("SiPixelDynamicInefficiencyReader")
0286       << match << " geom factors and " << pu_match << " pu factors matched to config file factors!\n"
0287       << std::endl;
0288   if (mismatch != 0)
0289     edm::LogPrint("SiPixelDynamicInefficiencyReader")
0290         << "ERROR! " << mismatch
0291         << " geom factors mismatched to config file factors! Please change config and/or DB content!" << std::endl;
0292   if (pu_mismatch != 0)
0293     edm::LogPrint("SiPixelDynamicInefficiencyReader")
0294         << "ERROR! " << pu_mismatch
0295         << " pu_factors mismatched to config file pu_factors! Please change config and/or DB content!" << std::endl;
0296 }
0297 DEFINE_FWK_MODULE(SiPixelDynamicInefficiencyReader);