Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:30:21

0001 // L1TGlobalSummary:  Use L1TGlobalUtils to print summary of L1TGlobal output
0002 //
0003 // author: Brian Winer Ohio State
0004 //
0005 
0006 #include <fstream>
0007 #include <iomanip>
0008 
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0013 #include "FWCore/Utilities/interface/EDGetToken.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 
0018 #include "DataFormats/L1TGlobal/interface/GlobalAlgBlk.h"
0019 #include "DataFormats/L1TGlobal/interface/GlobalExtBlk.h"
0020 
0021 #include "L1Trigger/L1TGlobal/interface/L1TGlobalUtil.h"
0022 
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 
0025 using namespace edm;
0026 using namespace std;
0027 using namespace l1t;
0028 
0029 // class declaration
0030 class L1TGlobalSummary : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0031 public:
0032   explicit L1TGlobalSummary(const edm::ParameterSet&);
0033   ~L1TGlobalSummary() override{};
0034   void analyze(const edm::Event&, const edm::EventSetup&) override;
0035   void beginRun(Run const&, EventSetup const&) override;
0036   void endRun(Run const&, EventSetup const&) override;
0037   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0038 
0039 private:
0040   InputTag algInputTag_;
0041   InputTag extInputTag_;
0042   EDGetToken algToken_;
0043   EDGetToken extToken_;
0044   bool dumpRecord_;
0045   bool dumpTriggerResults_;
0046   bool dumpTriggerSummary_;
0047   bool readPrescalesFromFile_;
0048   int minBx_;
0049   int maxBx_;
0050   L1TGlobalUtil* gtUtil_;
0051 
0052   std::vector<int> decisionCount_;
0053   std::vector<int> intermCount_;
0054   std::vector<int> finalCount_;
0055   int finalOrCount;
0056 };
0057 
0058 L1TGlobalSummary::L1TGlobalSummary(const edm::ParameterSet& iConfig) {
0059   algInputTag_ = iConfig.getParameter<InputTag>("AlgInputTag");
0060   extInputTag_ = iConfig.getParameter<InputTag>("ExtInputTag");
0061   algToken_ = consumes<BXVector<GlobalAlgBlk>>(algInputTag_);
0062   extToken_ = consumes<BXVector<GlobalExtBlk>>(extInputTag_);
0063   dumpRecord_ = iConfig.getParameter<bool>("DumpRecord");
0064   dumpTriggerResults_ = iConfig.getParameter<bool>("DumpTrigResults");
0065   dumpTriggerSummary_ = iConfig.getParameter<bool>("DumpTrigSummary");
0066   readPrescalesFromFile_ = iConfig.getParameter<bool>("ReadPrescalesFromFile");
0067   minBx_ = iConfig.getParameter<int>("MinBx");
0068   maxBx_ = iConfig.getParameter<int>("MaxBx");
0069   l1t::UseEventSetupIn useEventSetupIn = l1t::UseEventSetupIn::Run;
0070   if (dumpTriggerResults_ || dumpTriggerSummary_) {
0071     useEventSetupIn = l1t::UseEventSetupIn::RunAndEvent;
0072   }
0073   gtUtil_ = new L1TGlobalUtil(iConfig, consumesCollector(), *this, algInputTag_, extInputTag_, useEventSetupIn);
0074   finalOrCount = 0;
0075 
0076   if (readPrescalesFromFile_) {
0077     std::string preScaleFileName = iConfig.getParameter<std::string>("psFileName");
0078     unsigned int preScColumn = iConfig.getParameter<int>("psColumn");
0079     gtUtil_->OverridePrescalesAndMasks(preScaleFileName, preScColumn);
0080   }
0081 }
0082 
0083 void L1TGlobalSummary::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0084   edm::ParameterSetDescription desc;
0085   // These parameters are part of the L1T/HLT interface, avoid changing if possible::
0086   desc.add<edm::InputTag>("AlgInputTag", edm::InputTag(""))
0087       ->setComment("InputTag for uGT Algorithm Block (required parameter:  default value is invalid)");
0088   desc.add<edm::InputTag>("ExtInputTag", edm::InputTag(""))
0089       ->setComment("InputTag for uGT External Block (required parameter:  default value is invalid)");
0090   // These parameters have well defined  default values and are not currently
0091   // part of the L1T/HLT interface.  They can be cleaned up or updated at will:
0092   desc.add<int>("MinBx", 0);
0093   desc.add<int>("MaxBx", 0);
0094   desc.add<bool>("DumpTrigResults", false);
0095   desc.add<bool>("DumpRecord", false);
0096   desc.add<bool>("DumpTrigSummary", true);
0097   desc.add<bool>("ReadPrescalesFromFile", false);
0098   desc.add<std::string>("psFileName", "prescale_L1TGlobal.csv")
0099       ->setComment("File should be located in directory: L1Trigger/L1TGlobal/data/Luminosity/startup");
0100   desc.add<int>("psColumn", 0);
0101   descriptions.add("L1TGlobalSummary", desc);
0102 }
0103 
0104 void L1TGlobalSummary::beginRun(Run const&, EventSetup const& evSetup) {
0105   decisionCount_.clear();
0106   intermCount_.clear();
0107   finalCount_.clear();
0108 
0109   finalOrCount = 0;
0110   gtUtil_->retrieveL1Setup(evSetup);
0111 
0112   int size = gtUtil_->decisionsInitial().size();
0113   decisionCount_.resize(size);
0114   intermCount_.resize(size);
0115   finalCount_.resize(size);
0116   std::fill(decisionCount_.begin(), decisionCount_.end(), 0);
0117   std::fill(intermCount_.begin(), intermCount_.end(), 0);
0118   std::fill(finalCount_.begin(), finalCount_.end(), 0);
0119 }
0120 
0121 void L1TGlobalSummary::endRun(Run const&, EventSetup const&) {
0122   if (dumpTriggerSummary_) {
0123     LogVerbatim out("L1TGlobalSummary");
0124     if (gtUtil_->valid()) {
0125       out << "==================  L1 Trigger Report  "
0126              "=====================================================================\n";
0127       out << '\n';
0128       out << " L1T menu Name   : " << gtUtil_->gtTriggerMenuName() << '\n';
0129       out << " L1T menu Version: " << gtUtil_->gtTriggerMenuVersion() << '\n';
0130       out << " L1T menu Comment: " << gtUtil_->gtTriggerMenuComment() << '\n';
0131       out << '\n';
0132       out << "    Bit                  Algorithm Name                  Init    PScd  Final   PS Factor     Num Bx "
0133              "Masked\n";
0134       out << "========================================================================================================="
0135              "===\n";
0136       auto const& prescales = gtUtil_->prescales();
0137       auto const& masks = gtUtil_->masks();
0138       for (unsigned int i = 0; i < prescales.size(); i++) {
0139         // get the prescale and mask (needs some error checking here)
0140         int resultInit = decisionCount_[i];
0141         int resultPre = intermCount_[i];
0142         int resultFin = finalCount_[i];
0143 
0144         auto const& name = prescales.at(i).first;
0145         if (name != "NULL") {
0146           double prescale = prescales.at(i).second;
0147           auto const& mask = masks.at(i).second;
0148           out << std::dec << setfill(' ') << "   " << setw(5) << i << "   " << setw(40) << name << "   " << setw(7)
0149               << resultInit << setw(7) << resultPre << setw(7) << resultFin << setw(10) << prescale << setw(11)
0150               << mask.size() << '\n';
0151         }
0152       }
0153       out << "                                                      Final OR Count = " << finalOrCount << '\n';
0154       out << "========================================================================================================="
0155              "===\n";
0156     } else {
0157       out << "==================  No Level-1 Trigger menu  "
0158              "===============================================================\n";
0159     }
0160   }
0161 }
0162 
0163 // loop over events
0164 void L1TGlobalSummary::analyze(const edm::Event& iEvent, const edm::EventSetup& evSetup) {
0165   Handle<BXVector<GlobalAlgBlk>> alg;
0166   iEvent.getByToken(algToken_, alg);
0167 
0168   Handle<BXVector<GlobalExtBlk>> ext;
0169   iEvent.getByToken(extToken_, ext);
0170 
0171   LogDebug("l1t|Global") << "retrieved L1 GT data blocks" << endl;
0172 
0173   if (dumpTriggerResults_ || dumpTriggerSummary_) {
0174     //Fill the L1 result maps
0175     gtUtil_->retrieveL1(iEvent, evSetup, algToken_);
0176 
0177     LogDebug("l1t|Global") << "retrieved L1 data from GT Util" << endl;
0178 
0179     // grab the map for the final decisions
0180     const std::vector<std::pair<std::string, bool>> initialDecisions = gtUtil_->decisionsInitial();
0181     const std::vector<std::pair<std::string, bool>> intermDecisions = gtUtil_->decisionsInterm();
0182     const std::vector<std::pair<std::string, bool>> finalDecisions = gtUtil_->decisionsFinal();
0183     const std::vector<std::pair<std::string, double>> prescales = gtUtil_->prescales();
0184     const std::vector<std::pair<std::string, std::vector<int>>> masks = gtUtil_->masks();
0185 
0186     if ((decisionCount_.size() != gtUtil_->decisionsInitial().size()) ||
0187         (intermCount_.size() != gtUtil_->decisionsInterm().size()) ||
0188         (finalCount_.size() != gtUtil_->decisionsFinal().size())) {
0189       LogError("l1t|Global") << "gtUtil sizes inconsistent across run." << endl;
0190       return;
0191     }
0192 
0193     if (dumpTriggerResults_) {
0194       cout << "\n===================================== Trigger Results for BX=0 "
0195               "=============================================\n"
0196            << endl;
0197       cout << "    Bit                  Algorithm Name                  Init    aBXM  Final   PS Factor     Num Bx "
0198               "Masked"
0199            << endl;
0200       cout << "========================================================================================================"
0201               "===="
0202            << endl;
0203     }
0204     for (unsigned int i = 0; i < initialDecisions.size(); i++) {
0205       // get the name and trigger result
0206       std::string name = (initialDecisions.at(i)).first;
0207       if (name == "NULL")
0208         continue;
0209 
0210       bool resultInit = (initialDecisions.at(i)).second;
0211 
0212       // get prescaled and final results (need some error checking here)
0213       bool resultInterm = (intermDecisions.at(i)).second;
0214       bool resultFin = (finalDecisions.at(i)).second;
0215 
0216       // get the prescale and mask (needs some error checking here)
0217       double prescale = (prescales.at(i)).second;
0218       std::vector<int> mask = (masks.at(i)).second;
0219 
0220       if (resultInit)
0221         decisionCount_[i]++;
0222       if (resultInterm)
0223         intermCount_[i]++;
0224       if (resultFin)
0225         finalCount_[i]++;
0226 
0227       //cout << i << " " << decisionCount_[i] << "\n";
0228 
0229       if (dumpTriggerResults_) {
0230         cout << std::dec << setfill(' ') << "   " << setw(5) << i << "   " << setw(40) << name.c_str() << "   "
0231              << setw(7) << resultInit << setw(7) << resultInterm << setw(7) << resultFin << setw(10) << prescale
0232              << setw(11) << mask.size() << endl;
0233       }
0234     }
0235     bool finOR = gtUtil_->getFinalOR();
0236     if (finOR)
0237       finalOrCount++;
0238     if (dumpTriggerResults_) {
0239       cout << "                                                                FinalOR = " << finOR << endl;
0240       cout << "========================================================================================================"
0241               "==="
0242            << endl;
0243     }
0244   }
0245 
0246   if (dumpRecord_) {
0247     //int i = 0; // now now just printing BX=0...
0248     for (int i = minBx_; i <= maxBx_; i++) {
0249       // Dump the coutput record
0250       cout << " ------ Bx= " << i << " ext ----------" << endl;
0251       if (ext.isValid()) {
0252         if (i >= ext->getFirstBX() && i <= ext->getLastBX()) {
0253           for (std::vector<GlobalExtBlk>::const_iterator extBlk = ext->begin(i); extBlk != ext->end(i); ++extBlk) {
0254             extBlk->print(cout);
0255             cout << std::dec;
0256           }
0257         } else {
0258           cout << "No Ext Conditions stored for this bx " << i << endl;
0259         }
0260       } else {
0261         LogError("L1TGlobalSummary") << "No ext Data in this event " << endl;
0262       }
0263 
0264       // Dump the coutput record
0265       cout << " ------ Bx= " << i << " alg ----------" << endl;
0266       if (alg.isValid()) {
0267         if (i >= alg->getFirstBX() && i <= alg->getLastBX()) {
0268           for (std::vector<GlobalAlgBlk>::const_iterator algBlk = alg->begin(i); algBlk != alg->end(i); ++algBlk) {
0269             algBlk->print(cout);
0270             cout << std::dec;
0271           }
0272         } else {
0273           cout << "No Alg Decisions stored for this bx " << i << endl;
0274         }
0275       } else {
0276         LogError("L1TGlobalSummary") << "No alg Data in this event " << endl;
0277       }
0278     }
0279   }
0280 }
0281 
0282 #include "FWCore/Framework/interface/MakerMacros.h"
0283 DEFINE_FWK_MODULE(L1TGlobalSummary);