Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-10 02:53:41

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 
0027   const int nvar = 4;
0028   enum vtype { total, extra, zeros, match };
0029 
0030   std::array<int, nvar> n[ndet];
0031 
0032   for (auto &d : n) {
0033     for (auto &v : d) {
0034       v = 0;
0035     }
0036   }
0037 
0038   for (auto &x1 : xmls1) {
0039     HcalGenericDetId id(x1.first);
0040     auto x2 = xmls2.find(id.rawId());
0041     auto subdet = id.genericSubdet();
0042     if (subdet == 0 or subdet == 6)
0043       continue;  //'empty' or 'other'
0044 
0045     auto &m = n[subdet - 1];
0046 
0047     m[total]++;
0048     if (x2 == xmls2.end()) {
0049       m[extra]++;
0050       if (testFormat)
0051         cout << "Extra detId: " << id << endl;
0052       else
0053         continue;
0054     }
0055 
0056     const auto &lut1 = x1.second;
0057     size_t size = lut1.size();
0058 
0059     bool zero = true;
0060     for (auto &i : lut1) {
0061       if (i > 0) {
0062         zero = false;
0063         break;
0064       }
0065     }
0066     if (zero) {
0067       m[zeros]++;
0068       if (detail == 1 and testFormat) {
0069         cout << "Zero LUT: " << id << endl;
0070       }
0071     }
0072 
0073     if (testFormat)
0074       continue;
0075 
0076     const auto &lut2 = x2->second;
0077     bool good = size == lut2.size();
0078     for (size_t i = 0; i < size and good; ++i) {
0079       if (lut1[i] != lut2[i]) {
0080         good = false;
0081         if (detail == 2) {
0082           cout << Form("Mismatach in index=%3d, %4d!=%4d, ", int(i), lut1[i], lut2[i]) << id << endl;
0083         }
0084       }
0085     }
0086     if (good)
0087       m[match]++;
0088   }
0089 
0090   if (testFormat) {
0091     cout << Form("%3s:  %8s  %8s  %8s", "Det", "total", "zeroes", "extra") << endl;
0092     for (auto i : dtype)
0093       cout << Form("%3s:  %8d  %8d  %8d", DET[i], n[i][total], n[i][zeros], n[i][extra]) << endl;
0094     cout << "--------------------------------------------" << endl;
0095   } else {
0096     bool good = true;
0097     for (auto &d : n) {
0098       if (d[total] != d[match]) {
0099         good = false;
0100       }
0101     }
0102     cout << Form("%3s:  %8s  %8s  %8s", "Det", "total", "match", "mismatch") << endl;
0103     for (auto i : dtype)
0104       cout << Form("%3s:  %8d  %8d  %8d", DET[i], n[i][total], n[i][match], n[i][total] - n[i][match]) << endl;
0105     cout << "--------------------------------------------" << endl;
0106     cout << (good ? "PASS!" : "FAIL!") << endl;
0107   }
0108 }
0109 
0110 int main(int argc, char **argv) {
0111   optutl::CommandLineParser parser("runTestParameters");
0112   parser.parseArguments(argc, argv, true);
0113   if (argc < 2) {
0114     std::cerr << "runTest: missing input command" << std::endl;
0115   } else if (strcmp(argv[1], "merge") == 0) {
0116     std::string flist_ = parser.stringValue("storePrepend");
0117     std::string out_ = parser.stringValue("outputFile");
0118     mergeLUTs(flist_.c_str(), out_.c_str());
0119   } else if (strcmp(argv[1], "diff") == 0) {
0120     auto files = parser.stringVector("inputFiles");
0121     auto detail = parser.integerValue("section");
0122 
0123     LutXml xmls1(edm::FileInPath(files[0]).fullPath());
0124     LutXml xmls2(edm::FileInPath(files[1]).fullPath());
0125 
0126     xmls1.create_lut_map();
0127     xmls2.create_lut_map();
0128 
0129     cout << files[0] << endl;
0130     dumpLutDiff(xmls1, xmls2, true, detail);
0131 
0132     cout << files[1] << endl;
0133     dumpLutDiff(xmls2, xmls1, true, detail);
0134 
0135     cout << "Comparison" << endl;
0136     dumpLutDiff(xmls1, xmls2, false, detail);
0137   } else if (strcmp(argv[1], "create-lut-loader") == 0) {
0138     std::string _file_list = parser.stringValue("outputFile");
0139     std::string _tag = parser.stringValue("tag");
0140     std::string _comment = parser.stringValue("storePrepend");
0141     const std::string &_prefix = _tag;
0142     std::string _version = "1";
0143     int _subversion = 0;
0144     HcalLutManager manager;
0145     manager.create_lut_loader(_file_list, _prefix, _tag, _comment, _tag, _subversion);
0146   } else {
0147     throw std::invalid_argument(Form("Unknown command: %s", argv[1]));
0148   }
0149 
0150   return 0;
0151 }