Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:20:50

0001 // Originally written by James Jackson
0002 // modified by Peter Wittich
0003 
0004 // user include files
0005 #include "FWCore/Common/interface/TriggerNames.h"
0006 #include "HLTriggerOffline/Common/interface/HltComparator.h"
0007 //#include "FWCore/Utilities/interface/Exception.h"
0008 
0009 //#include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0011 #include "FWCore/ServiceRegistry/interface/Service.h"
0012 
0013 #include <TH1.h>
0014 #include <iostream>
0015 #include <string>
0016 #include <vector>
0017 
0018 typedef std::vector<std::string> StringCollection;
0019 
0020 // types of outcomes possible.
0021 // only some are errors
0022 enum {
0023   kOnOffPass = 0,
0024   kOnOffFail,
0025   kOnPassOffFail,
0026   kOnFailOffPass,
0027   kOnOffError,
0028   kOnRunOffError,
0029   kOnErrorOffRun,
0030   kOnRunOffNot,
0031   kOnNotOffRun,
0032   kOnOffNot
0033 };
0034 
0035 // Analyser constructor
0036 HltComparator::HltComparator(const edm::ParameterSet &iConfig)
0037     : hltOnlineResults_(consumes<edm::TriggerResults>(iConfig.getParameter<edm::InputTag>("OnlineResults"))),
0038       hltOfflineResults_(consumes<edm::TriggerResults>(iConfig.getParameter<edm::InputTag>("OfflineResults"))),
0039       init_(false),
0040       verbose_(iConfig.getUntrackedParameter<bool>("verbose")),
0041       skipPathList_(iConfig.getUntrackedParameter<std::vector<std::string>>("skipPaths")),
0042       usePathList_(iConfig.getUntrackedParameter<std::vector<std::string>>("usePaths")) {
0043   // std::cout << " HERE I AM " << std::endl;
0044   produces<StringCollection>("failedTriggerDescription");
0045   // std::cout << " HERE I GO " << std::endl;
0046 }
0047 
0048 HltComparator::~HltComparator() {}
0049 
0050 // Initialises online --> offline trigger bit mappings and histograms
0051 void HltComparator::initialise(const edm::TriggerResults &onlineResults,
0052                                const edm::TriggerResults &offlineResults,
0053                                edm::Event &e) {
0054   init_ = true;
0055 
0056   // Get trigger names
0057   const edm::TriggerNames &onlineTriggerNames = e.triggerNames(onlineResults);
0058   const edm::TriggerNames &offlineTriggerNames = e.triggerNames(offlineResults);
0059   onlineActualNames_ = onlineTriggerNames.triggerNames();
0060   offlineActualNames_ = offlineTriggerNames.triggerNames();
0061   numTriggers_ = onlineActualNames_.size();
0062 
0063   // do we need to throw? I guess the whole job is crap if this happens.
0064   // sort of assumes we're the only game in town.
0065   if (numTriggers_ != offlineActualNames_.size()) {
0066     throw cms::Exception("IncorrectTriggers") << "Online had " << numTriggers_ << "triggers, "
0067                                               << "Offline had " << offlineActualNames_.size() << "triggers";
0068   }
0069 
0070   // Create bit mappings
0071   std::map<std::string, unsigned int> offlineNameBitMap;
0072   for (unsigned int i = 0; i < numTriggers_; ++i) {
0073     offlineNameBitMap[offlineActualNames_[i]] = i;
0074   }
0075   for (unsigned int i = 0; i < numTriggers_; ++i) {
0076     // Find offline position for fixed online bit
0077     std::map<std::string, unsigned int>::iterator it = offlineNameBitMap.find(onlineActualNames_[i]);
0078     if (it != offlineNameBitMap.end()) {
0079       onlineToOfflineBitMappings_.push_back(it->second);
0080     } else {
0081       throw cms::Exception("IncorrectTriggers") << "Online trigger path " << onlineActualNames_[i]
0082                                                 << " not found in Offline "
0083                                                    "processing";
0084     }
0085   }
0086 
0087   // Create histograms
0088   edm::Service<TFileService> fs;
0089   for (std::vector<std::string>::iterator it = onlineActualNames_.begin(); it != onlineActualNames_.end(); ++it) {
0090     // Bin descriptions: OnOfPass, OnOffFail, OnPassOffFail, OnFailOffPass,
0091     // OnOffError, OnRunOffError, OnErrorOffRun, OnRunOffNot OnNotOffRun
0092     // OnNotOffNot
0093     TH1F *h = fs->make<TH1F>(it->c_str(), it->c_str(), 10, 0, 10);
0094     TAxis *a = h->GetXaxis();
0095     a->SetBinLabel(1, "OnPass_OffPass");
0096     a->SetBinLabel(2, "OnFail_OffFail");
0097     a->SetBinLabel(3, "OnPass_OffFail");
0098     a->SetBinLabel(4, "OnFail_OffPass");
0099     a->SetBinLabel(5, "OnError_OffError");
0100     a->SetBinLabel(6, "OnRun_OffError");
0101     a->SetBinLabel(7, "OnError_OffRun");
0102     a->SetBinLabel(8, "OnRun_OffNotRun");
0103     a->SetBinLabel(9, "OnNotRun_OffRun");
0104     a->SetBinLabel(10, "OnNotRun_OffNotRun");
0105     comparisonHists_.push_back(h);
0106   }
0107 }
0108 
0109 // Format a comparison result
0110 std::string HltComparator::formatResult(const unsigned int i) {
0111   switch (i) {
0112     case 0:
0113       return std::string("OnPass_OffPass");
0114       break;
0115     case 1:
0116       return std::string("OnFail_OffFail");
0117       break;
0118     case 2:
0119       return std::string("OnPass_OffFail");
0120       break;
0121     case 3:
0122       return std::string("OnFail_OffPass");
0123       break;
0124     case 4:
0125       return std::string("OnError_OffError");
0126       break;
0127     case 5:
0128       return std::string("OnRun_OffError");
0129       break;
0130     case 6:
0131       return std::string("OnError_OffRun");
0132       break;
0133     case 7:
0134       return std::string("OnRun_OffNotRun");
0135       break;
0136     case 8:
0137       return std::string("OnNotRun_OffRun");
0138       break;
0139     case 9:
0140       return std::string("OnNotRun_OffNotRun");
0141       break;
0142   }
0143   return std::string("CODE NOT KNOWN");
0144 }
0145 
0146 bool HltComparator::filter(edm::Event &event, const edm::EventSetup &iSetup) {
0147   // std::cout << "top of the filter " << std::endl;
0148   // Get trigger results
0149   edm::Handle<edm::TriggerResults> onlineResults;
0150   edm::Handle<edm::TriggerResults> offlineResults;
0151   event.getByToken(hltOnlineResults_, onlineResults);
0152   event.getByToken(hltOfflineResults_, offlineResults);
0153 
0154   std::unique_ptr<StringCollection> resultDescription(new StringCollection);
0155 
0156   // Initialise comparator if required
0157   if (!init_) {
0158     initialise(*onlineResults, *offlineResults, event);
0159   }
0160 
0161   // Perform trigger checks
0162   bool hasDisagreement = false;
0163   for (unsigned int i = 0; i < numTriggers_; ++i) {
0164     unsigned int offlineTriggerBit = onlineToOfflineBitMappings_[i];
0165 
0166     bool onRun = onlineResults->wasrun(i);
0167     bool offRun = offlineResults->wasrun(offlineTriggerBit);
0168     bool onAccept = onlineResults->accept(i);
0169     bool offAccept = offlineResults->accept(offlineTriggerBit);
0170     bool onError = onlineResults->error(i);
0171     bool offError = offlineResults->error(offlineTriggerBit);
0172 
0173     int result = -1;
0174     if (onError || offError) {
0175       if (onError && offError) {
0176         result = 4;
0177       } else if (onError) {
0178         result = 6;
0179       } else {
0180         result = 5;
0181       }
0182     } else if ((!onRun) || (!offRun)) {
0183       if ((!onRun) && (!offRun)) {
0184         result = 9;
0185       } else if (!onRun) {
0186         result = 8;
0187       } else {
0188         result = 7;
0189       }
0190     } else {
0191       if (onAccept && offAccept) {
0192         result = 0;
0193       } else if ((!onAccept) && (!offAccept)) {
0194         result = 1;
0195       } else if (onAccept) {
0196         result = 2;
0197       } else {
0198         result = 3;
0199       }
0200     }
0201 
0202     // Fill the results histogram
0203     comparisonHists_[i]->Fill(result);
0204 
0205     // if the online-offline comparison results in a failure, we
0206     // want to send the result to a special stream. Hence we _pass_ the filter.
0207     // If it all worked as expected the filter fails and the event doesn't go
0208     // to the output stream.
0209     if ((result == kOnPassOffFail) || (result == kOnFailOffPass) || (result == kOnRunOffError) ||
0210         (result == kOnErrorOffRun) || (result == kOnRunOffNot) || (result == kOnNotOffRun)) {
0211       // is this one we should ignore? check the skip list
0212       if (verbose()) {
0213         std::cout << "Found disagreemenet " << result << ", name is " << onlineActualNames_[i] << std::endl;
0214       }
0215       std::ostringstream desc;
0216       desc << onlineActualNames_[i] << ":" << formatResult(result);
0217       resultDescription->push_back(desc.str());
0218       if (std::find(skipPathList_.begin(), skipPathList_.end(), onlineActualNames_[i]) == skipPathList_.end()) {
0219         if (!usePathList_.empty()) {
0220           // only use specified paths to debug
0221           if (std::find(usePathList_.begin(), usePathList_.end(), onlineActualNames_[i]) != usePathList_.end())
0222             hasDisagreement = true;
0223         } else
0224           hasDisagreement = true;
0225       }
0226     }
0227 
0228     // Record the trigger error code
0229     // I think this should be result > 2? (pw)
0230     if (verbose() && (result > 1)) {
0231       std::cout << "HLT-Compare: Event " << event.id().event() << " Path " << onlineActualNames_[i] << " "
0232                 << formatResult(result) << std::endl;
0233 #ifdef NOTDEF
0234       triggerComparisonErrors_[event.id().event()][onlineActualNames_[i]] = result;
0235 #endif  // NOTDEF
0236     }
0237   }
0238 
0239   // std::cout << " HERE I STAY " << std::endl;
0240   event.put(std::move(resultDescription), "failedTriggerDescription");
0241   // std::cout << " HERE I WENT " << std::endl;
0242 
0243   if (hasDisagreement)
0244     return true;
0245   else
0246     return false;
0247 }
0248 
0249 void HltComparator::beginJob() {}
0250 
0251 // Print the trigger results
0252 void HltComparator::endJob() {
0253 #ifdef NOTDEF
0254   std::cout << "HLT-Compare ---------- Trigger Comparison Summary ----------" << std::endl;
0255   std::cout << "HLT-Compare  The following events had trigger mismatches:" << std::endl;
0256   std::map<unsigned int, std::map<std::string, unsigned int>>::iterator it;
0257   for (it = triggerComparisonErrors_.begin(); it != triggerComparisonErrors_.end(); ++it) {
0258     std::cout << "HLT-Compare  Event: " << it->first << std::endl;
0259     std::map<std::string, unsigned int>::iterator jt;
0260     for (jt = it->second.begin(); jt != it->second.end(); ++jt) {
0261       std::cout << "HLT-Compare    Path: " << jt->first << " : " << formatResult(jt->second) << std::endl;
0262     }
0263   }
0264   std::cout << "HLT-Compare ------------ End Trigger Comparison ------------" << std::endl;
0265 #endif  // NOTDEF
0266 }