Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:48

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