Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-27 03:17:28

0001 // -*- C++ -*-
0002 //
0003 // Package:    Test/HcalLutComparer
0004 // Class:      HcalLutComparer
0005 //
0006 /**\class HcalLutComparer HcalLutComparer.cc Test/HcalLutComparer/plugins/HcalLutComparer.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Joshua C. Hiltbrand
0015 //         Created:  Tue, 12 Nov 2024 05:57:40 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 #include <iostream>
0022 #include <fstream>
0023 
0024 // user include files
0025 #include "FWCore/Framework/interface/Frameworkfwd.h"
0026 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Framework/interface/EventSetup.h"
0031 
0032 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
0033 #include "CalibCalorimetry/HcalTPGAlgos/interface/LutXml.h"
0034 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0035 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0036 #include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
0037 #include "Geometry/CaloTopology/interface/HcalTopology.h"
0038 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0039 #include "Geometry/Records/interface/HcalRecNumberingRecord.h"
0040 #include "CondFormats/DataRecord/interface/HcalElectronicsMapRcd.h"
0041 
0042 class HcalLutComparer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0043 public:
0044   explicit HcalLutComparer(const edm::ParameterSet &);
0045   ~HcalLutComparer() override {}
0046   void dumpLutDiff(LutXml &xmls1, LutXml &xmls2, bool testFormat);
0047   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0048 
0049 private:
0050   void analyze(const edm::Event &, const edm::EventSetup &) override;
0051 
0052   edm::ESGetToken<HcalTopology, HcalRecNumberingRecord> tok_htopo_;
0053   edm::ESGetToken<HcalElectronicsMap, HcalElectronicsMapRcd> tok_emap_;
0054 
0055   std::string lutXML1_;
0056   std::string lutXML2_;
0057   unsigned int verbosity_;
0058 };
0059 
0060 HcalLutComparer::HcalLutComparer(const edm::ParameterSet &iConfig) {
0061   lutXML1_ = iConfig.getParameter<std::string>("lutXML1");
0062   lutXML2_ = iConfig.getParameter<std::string>("lutXML2");
0063   verbosity_ = iConfig.getParameter<unsigned int>("verbosity");
0064 
0065   tok_htopo_ = esConsumes<HcalTopology, HcalRecNumberingRecord>();
0066   tok_emap_ = esConsumes<HcalElectronicsMap, HcalElectronicsMapRcd>();
0067 }
0068 
0069 void HcalLutComparer::dumpLutDiff(LutXml &xmls1, LutXml &xmls2, bool testFormat = true) {
0070   std::vector<int> detCodes = {1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 9, -9, 10, -10, 11, -11, 12, -12};
0071   std::vector<std::string> detNames = {"HBP",
0072                                        "HBM",
0073                                        "HEP",
0074                                        "HEM",
0075                                        "HOP",
0076                                        "HOM",
0077                                        "HFP",
0078                                        "HFM",
0079                                        "HTP",
0080                                        "HTM",
0081                                        "ZDCP_EM",
0082                                        "ZDCM_EM",
0083                                        "ZDCP_HAD",
0084                                        "ZDCM_HAD",
0085                                        "ZDCP_LUM",
0086                                        "ZDCM_LUM",
0087                                        "ZDCP_RPD",
0088                                        "ZDCM_RPD"};
0089 
0090   const int HBandHE_fgBits = 0xF000;
0091   const int HF_fgBits = 0x3000;
0092 
0093   unsigned int nvars = 5;
0094   enum vtype { total, extra, zeros, match, fgMatch };
0095 
0096   std::map<int, std::vector<int>> n;
0097 
0098   for (const auto &detCode : detCodes) {
0099     n[detCode] = std::vector<int>{};
0100     for (unsigned int j = 0; j < nvars; j++) {
0101       n[detCode].push_back(0);
0102     }
0103   }
0104 
0105   for (auto &x1 : xmls1) {
0106     auto x2 = xmls2.find(x1.first);
0107 
0108     HcalGenericDetId id = HcalGenericDetId(x1.first);
0109     int subdet = id.genericSubdet();
0110     if (subdet == 0 or subdet == 6)
0111       continue;  //'empty' or 'other'
0112 
0113     int side = 1;
0114     int section = 0;
0115     if (id.isHcalDetId()) {
0116       HcalDetId hdetId = HcalDetId(x1.first);
0117       side = hdetId.zside();
0118     } else if (id.isHcalTrigTowerDetId()) {
0119       HcalTrigTowerDetId htdetId = HcalTrigTowerDetId(x1.first);
0120       side = htdetId.zside();
0121     } else if (id.isHcalZDCDetId()) {
0122       HcalZDCDetId zdetId = HcalZDCDetId(x1.first);
0123       side = zdetId.zside();
0124       section = zdetId.section();
0125     }
0126 
0127     int detCode = side * (subdet + section);
0128 
0129     auto &m = n[detCode];
0130 
0131     m[total]++;
0132     if (x2 == xmls2.end()) {
0133       m[extra]++;
0134       if (testFormat)
0135         std::cout << "Extra detId: " << id << std::endl;
0136       else
0137         continue;
0138     }
0139 
0140     const auto &lut1 = x1.second;
0141     size_t size = lut1.size();
0142 
0143     bool zero = true;
0144     for (auto &i : lut1) {
0145       if (i > 0) {
0146         zero = false;
0147         break;
0148       }
0149     }
0150     if (zero) {
0151       m[zeros]++;
0152       if (verbosity_ == 1 and testFormat) {
0153         std::cout << "Zero LUT: " << id << std::endl;
0154       }
0155     }
0156 
0157     if (testFormat)
0158       continue;
0159 
0160     const auto &lut2 = x2->second;
0161     bool good = size == lut2.size();
0162     bool fgGood = size == lut2.size();
0163     for (size_t i = 0; i < size and (good or fgGood); ++i) {
0164       if (lut1[i] != lut2[i]) {
0165         good = false;
0166         if (subdet == 1 || subdet == 2) {
0167           if ((lut1[i] & HBandHE_fgBits) != (lut2[i] & HBandHE_fgBits))
0168             fgGood = false;
0169         } else if (subdet == 4) {
0170           if ((lut1[i] & HF_fgBits) != (lut2[i] & HF_fgBits))
0171             fgGood = false;
0172         }
0173 
0174         if (verbosity_ == 2) {
0175           std::cout << Form("Mismatach in index=%3d, %4d!=%4d, ", int(i), lut1[i], lut2[i]) << id << std::endl;
0176         }
0177       }
0178     }
0179     if (good)
0180       m[match]++;
0181     if (fgGood)
0182       m[fgMatch]++;
0183   }
0184 
0185   if (testFormat) {
0186     std::cout << Form("%9s  %6s  %6s  %6s", "Det", "total", "zeroes", "extra") << std::endl;
0187     for (unsigned int i = 0; i < detCodes.size(); i++) {
0188       int detCode = detCodes.at(i);
0189       std::string detName = detNames.at(i);
0190       std::cout << Form("%9s  %6d  %6d  %6d", detName.c_str(), n[detCode][total], n[detCode][zeros], n[detCode][extra])
0191                 << std::endl;
0192       if (detCode < 0) {
0193         std::cout << Form("%9s  %6d  %6d  %6d",
0194                           " ",
0195                           n[detCode][total] + n[-1 * detCode][total],
0196                           n[detCode][zeros] + n[-1 * detCode][zeros],
0197                           n[detCode][extra] + n[-1 * detCode][extra])
0198                   << std::endl;
0199         std::cout << std::endl;
0200       }
0201     }
0202     std::cout << "--------------------------------------------" << std::endl;
0203   } else {
0204     bool good = true;
0205     for (const auto &it : n) {
0206       if (it.second[total] != it.second[match]) {
0207         good = false;
0208       }
0209     }
0210     std::cout << Form("%9s  %6s  %6s  %8s  %8s  %11s", "Det", "total", "match", "mismatch", "FG match", "FG mismatch")
0211               << std::endl;
0212     for (unsigned int i = 0; i < detCodes.size(); i++) {
0213       int detCode = detCodes.at(i);
0214       std::string detName = detNames.at(i);
0215       std::cout << Form("%9s  %6d  %6d  %8d  %8d  %11d",
0216                         detName.c_str(),
0217                         n[detCode][total],
0218                         n[detCode][match],
0219                         n[detCode][total] - n[detCode][match],
0220                         n[detCode][fgMatch],
0221                         n[detCode][total] - n[detCode][fgMatch])
0222                 << std::endl;
0223       if (detCode < 0) {
0224         std::cout << Form("%9s  %6d  %6d  %8d  %8d  %11d",
0225                           " ",
0226                           n[detCode][total] + n[-1 * detCode][total],
0227                           n[detCode][match] + n[-1 * detCode][match],
0228                           n[detCode][total] - n[detCode][match] + n[-1 * detCode][total] - n[-1 * detCode][match],
0229                           n[detCode][fgMatch] + n[-1 * detCode][fgMatch],
0230                           n[detCode][total] - n[detCode][fgMatch] + n[-1 * detCode][total] - n[-1 * detCode][fgMatch])
0231                   << std::endl;
0232         std::cout << std::endl;
0233       }
0234     }
0235     std::cout << "--------------------------------------------" << std::endl;
0236     std::cout << (good ? "PASS!" : "FAIL!") << std::endl;
0237   }
0238 }
0239 
0240 void HcalLutComparer::analyze(const edm::Event &, const edm::EventSetup &iSetup) {
0241   const HcalElectronicsMap *electronicsMap = &iSetup.getData(tok_emap_);
0242 
0243   LutXml xmls1(edm::FileInPath(lutXML1_).fullPath());
0244   LutXml xmls2(edm::FileInPath(lutXML2_).fullPath());
0245 
0246   xmls1.create_lut_map(electronicsMap);
0247   xmls2.create_lut_map(electronicsMap);
0248 
0249   std::cout << lutXML1_ << std::endl;
0250   dumpLutDiff(xmls1, xmls2, true);
0251 
0252   std::cout << lutXML2_ << std::endl;
0253   dumpLutDiff(xmls2, xmls1, true);
0254 
0255   std::cout << "Comparison" << std::endl;
0256   dumpLutDiff(xmls1, xmls2, false);
0257 }
0258 
0259 void HcalLutComparer::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0260   edm::ParameterSetDescription desc;
0261   desc.setUnknown();
0262   descriptions.addDefault(desc);
0263 }
0264 
0265 DEFINE_FWK_MODULE(HcalLutComparer);