Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:00:05

0001 #include <iostream>
0002 #include <stdexcept>
0003 #include <sstream>
0004 #include "TString.h"
0005 #include "PhysicsTools/FWLite/interface/CommandLineParser.h"
0006 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalLutManager.h"
0007 #include "FWCore/Utilities/interface/FileInPath.h"
0008 
0009 using namespace std;
0010 
0011 void mergeLUTs(const char *flist, const char *out) {
0012   LutXml xmls;
0013   stringstream ss(flist);
0014   while (ss.good()) {
0015     string file;
0016     ss >> file;
0017     xmls += LutXml(file);
0018   }
0019   xmls.write(out);
0020 }
0021 
0022 void dumpLutDiff(LutXml &xmls1, LutXml &xmls2, bool testFormat = true, int detail = 0) {
0023   const int ndet = 5;
0024   const char *DET[ndet] = {"HB", "HE", "HO", "HF", "HT"};
0025   const int dtype[ndet] = {0, 1, 2, 3, 4};
0026   const int HBandHE_fgBits = 0xFC00;
0027 
0028   const int nvar = 5;
0029   enum vtype { total, extra, zeros, match, fgMatch };
0030 
0031   std::array<int, nvar> n[ndet];
0032 
0033   for (auto &d : n) {
0034     for (auto &v : d) {
0035       v = 0;
0036     }
0037   }
0038 
0039   for (auto &x1 : xmls1) {
0040     HcalGenericDetId id(x1.first);
0041     auto x2 = xmls2.find(id.rawId());
0042     auto subdet = id.genericSubdet();
0043     if (subdet == 0 or subdet == 6)
0044       continue;  //'empty' or 'other'
0045 
0046     auto &m = n[subdet - 1];
0047 
0048     m[total]++;
0049     if (x2 == xmls2.end()) {
0050       m[extra]++;
0051       if (testFormat)
0052         cout << "Extra detId: " << id << endl;
0053       else
0054         continue;
0055     }
0056 
0057     const auto &lut1 = x1.second;
0058     size_t size = lut1.size();
0059 
0060     bool zero = true;
0061     for (auto &i : lut1) {
0062       if (i > 0) {
0063         zero = false;
0064         break;
0065       }
0066     }
0067     if (zero) {
0068       m[zeros]++;
0069       if (detail == 1 and testFormat) {
0070         cout << "Zero LUT: " << id << endl;
0071       }
0072     }
0073 
0074     if (testFormat)
0075       continue;
0076 
0077     const auto &lut2 = x2->second;
0078     bool good = size == lut2.size();
0079     bool fgGood = size == lut2.size();
0080     for (size_t i = 0; i < size and (good or fgGood); ++i) {
0081       if (lut1[i] != lut2[i]) {
0082         good = false;
0083         //Only check fine grain bits in HB and HE
0084         if (subdet == 1 || subdet == 2) {
0085           if ((lut1[i] & HBandHE_fgBits) != (lut2[i] & HBandHE_fgBits))
0086             fgGood = false;
0087         }
0088         if (detail == 2) {
0089           cout << Form("Mismatach in index=%3d, %4d!=%4d, ", int(i), lut1[i], lut2[i]) << id << endl;
0090         }
0091       }
0092     }
0093     if (good)
0094       m[match]++;
0095     if (fgGood)
0096       m[fgMatch]++;
0097   }
0098 
0099   if (testFormat) {
0100     cout << Form("%3s:  %8s  %8s  %8s", "Det", "total", "zeroes", "extra") << endl;
0101     for (auto i : dtype)
0102       cout << Form("%3s:  %8d  %8d  %8d", DET[i], n[i][total], n[i][zeros], n[i][extra]) << endl;
0103     cout << "--------------------------------------------" << endl;
0104   } else {
0105     bool good = true;
0106     for (auto &d : n) {
0107       if (d[total] != d[match]) {
0108         good = false;
0109       }
0110     }
0111     cout << Form("%3s:  %8s  %8s  %8s  %8s  %8s", "Det", "total", "match", "mismatch", "FG match", "FG mismatch")
0112          << endl;
0113     for (auto i : dtype)
0114       cout << Form("%3s:  %8d  %8d  %8d  %8d  %8d",
0115                    DET[i],
0116                    n[i][total],
0117                    n[i][match],
0118                    n[i][total] - n[i][match],
0119                    n[i][fgMatch],
0120                    n[i][total] - n[i][fgMatch])
0121            << endl;
0122     cout << "--------------------------------------------" << endl;
0123     cout << (good ? "PASS!" : "FAIL!") << endl;
0124   }
0125 }
0126 
0127 int main(int argc, char **argv) {
0128   optutl::CommandLineParser parser("runTestParameters");
0129   parser.parseArguments(argc, argv, true);
0130   if (argc < 2) {
0131     std::cerr << "runTest: missing input command" << std::endl;
0132   } else if (strcmp(argv[1], "merge") == 0) {
0133     std::string flist_ = parser.stringValue("storePrepend");
0134     std::string out_ = parser.stringValue("outputFile");
0135     mergeLUTs(flist_.c_str(), out_.c_str());
0136   } else if (strcmp(argv[1], "diff") == 0) {
0137     auto files = parser.stringVector("inputFiles");
0138     auto detail = parser.integerValue("section");
0139 
0140     LutXml xmls1(edm::FileInPath(files[0]).fullPath());
0141     LutXml xmls2(edm::FileInPath(files[1]).fullPath());
0142 
0143     xmls1.create_lut_map();
0144     xmls2.create_lut_map();
0145 
0146     cout << files[0] << endl;
0147     dumpLutDiff(xmls1, xmls2, true, detail);
0148 
0149     cout << files[1] << endl;
0150     dumpLutDiff(xmls2, xmls1, true, detail);
0151 
0152     cout << "Comparison" << endl;
0153     dumpLutDiff(xmls1, xmls2, false, detail);
0154   } else if (strcmp(argv[1], "create-lut-loader") == 0) {
0155     std::string _file_list = parser.stringValue("outputFile");
0156     std::string _tag = parser.stringValue("tag");
0157     std::string _comment = parser.stringValue("storePrepend");
0158     const std::string &_prefix = _tag;
0159     std::string _version = "1";
0160     int _subversion = 0;
0161     HcalLutManager manager;
0162     manager.create_lut_loader(_file_list, _prefix, _tag, _comment, _tag, _subversion);
0163   } else {
0164     throw std::invalid_argument(Form("Unknown command: %s", argv[1]));
0165   }
0166 
0167   return 0;
0168 }