Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:22:48

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0008 
0009 #include "DataFormats/Common/interface/Ref.h"
0010 #include "DataFormats/L1Trigger/interface/BXVector.h"
0011 #include "DataFormats/L1Trigger/interface/L1TObjComparison.h"
0012 
0013 #include <algorithm>
0014 
0015 template <typename T>
0016 class L1TStage2ObjectComparison : public edm::stream::EDProducer<> {
0017 public:
0018   L1TStage2ObjectComparison(const edm::ParameterSet& ps);
0019   ~L1TStage2ObjectComparison() override = default;
0020   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0021 
0022 protected:
0023   void produce(edm::Event&, const edm::EventSetup&) override;
0024 
0025 private:
0026   edm::EDGetTokenT<BXVector<T>> token1_;
0027   edm::EDGetTokenT<BXVector<T>> token2_;
0028   const bool checkBxRange_;
0029   const bool checkCollSizePerBx_;
0030   const bool checkObject_;
0031 };
0032 
0033 template <typename T>
0034 L1TStage2ObjectComparison<T>::L1TStage2ObjectComparison(const edm::ParameterSet& ps)
0035     : token1_(consumes<BXVector<T>>(ps.getParameter<edm::InputTag>("collection1"))),
0036       token2_(consumes<BXVector<T>>(ps.getParameter<edm::InputTag>("collection2"))),
0037       checkBxRange_(ps.getParameter<bool>("checkBxRange")),
0038       checkCollSizePerBx_(ps.getParameter<bool>("checkCollSizePerBx")),
0039       checkObject_(ps.getParameter<bool>("checkObject")) {
0040   if (checkBxRange_ || checkCollSizePerBx_) {
0041     produces<l1t::ObjectRefBxCollection<T>>("collection1ExcessObjects");
0042     produces<l1t::ObjectRefBxCollection<T>>("collection2ExcessObjects");
0043   }
0044   if (checkObject_) {
0045     produces<l1t::ObjectRefPairBxCollection<T>>("objectMatches");
0046     produces<l1t::ObjectRefPairBxCollection<T>>("objectMismatches");
0047   }
0048 }
0049 
0050 template <typename T>
0051 void L1TStage2ObjectComparison<T>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0052   edm::ParameterSetDescription desc;
0053   desc.add<edm::InputTag>("collection1", edm::InputTag("collection1"))->setComment("L1T object collection 1");
0054   desc.add<edm::InputTag>("collection2", edm::InputTag("collection2"))->setComment("L1T object collection 2");
0055   desc.add<bool>("checkBxRange", true)->setComment("Check if BX ranges match");
0056   desc.add<bool>("checkCollSizePerBx", true)->setComment("Check if collection sizes within one BX match");
0057   desc.add<bool>("checkObject", true)->setComment("Check if objects match");
0058   descriptions.addWithDefaultLabel(desc);
0059 }
0060 
0061 template <typename T>
0062 void L1TStage2ObjectComparison<T>::produce(edm::Event& e, const edm::EventSetup& c) {
0063   auto excessObjRefsColl1 = std::make_unique<l1t::ObjectRefBxCollection<T>>();
0064   auto excessObjRefsColl2 = std::make_unique<l1t::ObjectRefBxCollection<T>>();
0065   auto matchRefPairs = std::make_unique<l1t::ObjectRefPairBxCollection<T>>();
0066   auto mismatchRefPairs = std::make_unique<l1t::ObjectRefPairBxCollection<T>>();
0067 
0068   edm::Handle<BXVector<T>> bxColl1;
0069   edm::Handle<BXVector<T>> bxColl2;
0070   e.getByToken(token1_, bxColl1);
0071   e.getByToken(token2_, bxColl2);
0072 
0073   // Set the BX ranges like the input collection BX ranges
0074   excessObjRefsColl1->setBXRange(bxColl1->getFirstBX(), bxColl1->getLastBX());
0075   excessObjRefsColl2->setBXRange(bxColl2->getFirstBX(), bxColl2->getLastBX());
0076   // Set the BX range to the intersection of the two input collection BX ranges
0077   matchRefPairs->setBXRange(std::max(bxColl1->getFirstBX(), bxColl2->getFirstBX()),
0078                             std::min(bxColl1->getLastBX(), bxColl2->getLastBX()));
0079   mismatchRefPairs->setBXRange(std::max(bxColl1->getFirstBX(), bxColl2->getFirstBX()),
0080                                std::min(bxColl1->getLastBX(), bxColl2->getLastBX()));
0081 
0082   if (checkBxRange_) {
0083     // Store references to objects in BX that do not exist in the other collection
0084     typename BXVector<T>::const_iterator it;
0085     // BX range of collection 1 > collection 2
0086     for (auto iBx = bxColl1->getFirstBX(); iBx < bxColl2->getFirstBX(); ++iBx) {
0087       for (it = bxColl1->begin(iBx); it != bxColl1->end(iBx); ++it) {
0088         edm::Ref<BXVector<T>> ref{bxColl1, bxColl1->key(it)};
0089         excessObjRefsColl1->push_back(iBx, ref);
0090       }
0091     }
0092     for (auto iBx = bxColl1->getLastBX(); iBx > bxColl2->getLastBX(); --iBx) {
0093       for (it = bxColl1->begin(iBx); it != bxColl1->end(iBx); ++it) {
0094         edm::Ref<BXVector<T>> ref{bxColl1, bxColl1->key(it)};
0095         excessObjRefsColl1->push_back(iBx, ref);
0096       }
0097     }
0098     // BX range of collection 2 > collection 1
0099     for (auto iBx = bxColl2->getFirstBX(); iBx < bxColl1->getFirstBX(); ++iBx) {
0100       for (it = bxColl2->begin(iBx); it != bxColl2->end(iBx); ++it) {
0101         edm::Ref<BXVector<T>> ref{bxColl2, bxColl2->key(it)};
0102         excessObjRefsColl2->push_back(iBx, ref);
0103       }
0104     }
0105     for (auto iBx = bxColl2->getLastBX(); iBx > bxColl1->getLastBX(); --iBx) {
0106       for (it = bxColl2->begin(iBx); it != bxColl2->end(iBx); ++it) {
0107         edm::Ref<BXVector<T>> ref{bxColl2, bxColl2->key(it)};
0108         excessObjRefsColl2->push_back(iBx, ref);
0109       }
0110     }
0111   }
0112 
0113   // Loop over all BX that exist in both collections
0114   for (int iBx = matchRefPairs->getFirstBX(); iBx <= matchRefPairs->getLastBX(); ++iBx) {
0115     auto it1 = bxColl1->begin(iBx);
0116     auto it2 = bxColl2->begin(iBx);
0117     while (it1 != bxColl1->end(iBx) && it2 != bxColl2->end(iBx)) {
0118       if (checkObject_) {
0119         // Store reference pairs for matching and mismatching objects
0120         edm::Ref<BXVector<T>> ref1{bxColl1, bxColl1->key(it1)};
0121         edm::Ref<BXVector<T>> ref2{bxColl2, bxColl2->key(it2)};
0122         if (*it1 == *it2) {
0123           matchRefPairs->push_back(iBx, std::make_pair(ref1, ref2));
0124         } else {
0125           mismatchRefPairs->push_back(iBx, std::make_pair(ref1, ref2));
0126         }
0127       }
0128       ++it1;
0129       ++it2;
0130     }
0131     if (checkCollSizePerBx_) {
0132       // Store references to excess objects if there are more objects in one collection (per BX)
0133       while (it1 != bxColl1->end(iBx)) {
0134         edm::Ref<BXVector<T>> ref{bxColl1, bxColl1->key(it1)};
0135         excessObjRefsColl1->push_back(iBx, ref);
0136         ++it1;
0137       }
0138       while (it2 != bxColl2->end(iBx)) {
0139         edm::Ref<BXVector<T>> ref{bxColl2, bxColl2->key(it2)};
0140         excessObjRefsColl2->push_back(iBx, ref);
0141         ++it2;
0142       }
0143     }
0144   }
0145 
0146   // Put data in the event
0147   if (checkBxRange_ || checkCollSizePerBx_) {
0148     e.put(std::move(excessObjRefsColl1), "collection1ExcessObjects");
0149     e.put(std::move(excessObjRefsColl2), "collection2ExcessObjects");
0150   }
0151   if (checkObject_) {
0152     e.put(std::move(matchRefPairs), "objectMatches");
0153     e.put(std::move(mismatchRefPairs), "objectMismatches");
0154   }
0155 }
0156 
0157 //define plugins for different L1T objects
0158 #include "DataFormats/L1TGlobal/interface/GlobalAlgBlk.h"
0159 #include "DataFormats/L1Trigger/interface/EGamma.h"
0160 #include "DataFormats/L1Trigger/interface/Tau.h"
0161 #include "DataFormats/L1Trigger/interface/Jet.h"
0162 #include "DataFormats/L1Trigger/interface/EtSum.h"
0163 #include "DataFormats/L1Trigger/interface/Muon.h"
0164 #include "DataFormats/L1TMuon/interface/RegionalMuonCand.h"
0165 #include "DataFormats/L1TMuon/interface/RegionalMuonCandFwd.h"
0166 typedef L1TStage2ObjectComparison<GlobalAlgBlk> L1TStage2GlobalAlgBlkComparison;
0167 typedef L1TStage2ObjectComparison<l1t::EGamma> L1TStage2EGammaComparison;
0168 typedef L1TStage2ObjectComparison<l1t::Tau> L1TStage2TauComparison;
0169 typedef L1TStage2ObjectComparison<l1t::Jet> L1TStage2JetComparison;
0170 typedef L1TStage2ObjectComparison<l1t::EtSum> L1TStage2EtSumComparison;
0171 typedef L1TStage2ObjectComparison<l1t::Muon> L1TStage2MuonComparison;
0172 typedef L1TStage2ObjectComparison<l1t::RegionalMuonCand> L1TStage2RegionalMuonCandComparison;
0173 DEFINE_FWK_MODULE(L1TStage2GlobalAlgBlkComparison);
0174 DEFINE_FWK_MODULE(L1TStage2EGammaComparison);
0175 DEFINE_FWK_MODULE(L1TStage2TauComparison);
0176 DEFINE_FWK_MODULE(L1TStage2JetComparison);
0177 DEFINE_FWK_MODULE(L1TStage2EtSumComparison);
0178 DEFINE_FWK_MODULE(L1TStage2MuonComparison);
0179 DEFINE_FWK_MODULE(L1TStage2RegionalMuonCandComparison);