Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-12 04:16:40

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/global/EDFilter.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "DataFormats/L1Trigger/interface/P2GTCandidate.h"
0005 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0006 
0007 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0008 #include "DataFormats/Common/interface/Handle.h"
0009 #include "FWCore/Utilities/interface/EDGetToken.h"
0010 #include "DataFormats/Common/interface/Ref.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 
0013 #include "L1Trigger/Phase2L1GT/interface/L1GTScales.h"
0014 #include "L1GTSingleCollectionCut.h"
0015 #include "L1GTCorrelationalCut.h"
0016 #include "L1GT3BodyCut.h"
0017 #include "L1GTSingleInOutLUT.h"
0018 
0019 #include <cinttypes>
0020 #include <memory>
0021 #include <vector>
0022 #include <set>
0023 
0024 #include <ap_int.h>
0025 
0026 using namespace l1t;
0027 
0028 class L1GTQuadObjectCond : public edm::global::EDFilter<> {
0029 public:
0030   explicit L1GTQuadObjectCond(const edm::ParameterSet&);
0031   ~L1GTQuadObjectCond() override = default;
0032 
0033   static void fillDescriptions(edm::ConfigurationDescriptions&);
0034 
0035 private:
0036   bool filter(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0037 
0038   const L1GTScales scales_;
0039 
0040   const L1GTSingleCollectionCut collection1Cuts_;
0041   const L1GTSingleCollectionCut collection2Cuts_;
0042   const L1GTSingleCollectionCut collection3Cuts_;
0043   const L1GTSingleCollectionCut collection4Cuts_;
0044 
0045   const bool enable_sanity_checks_;
0046   const bool inv_mass_checks_;
0047 
0048   const L1GTCorrelationalCut correl12Cuts_;
0049   const L1GTCorrelationalCut correl13Cuts_;
0050   const L1GTCorrelationalCut correl23Cuts_;
0051   const L1GTCorrelationalCut correl14Cuts_;
0052   const L1GTCorrelationalCut correl24Cuts_;
0053   const L1GTCorrelationalCut correl34Cuts_;
0054 
0055   const L1GT3BodyCut correl123Cuts_;
0056   const L1GT3BodyCut correl124Cuts_;
0057   const L1GT3BodyCut correl134Cuts_;
0058   const L1GT3BodyCut correl234Cuts_;
0059 
0060   const edm::EDGetTokenT<P2GTCandidateCollection> token1_;
0061   const edm::EDGetTokenT<P2GTCandidateCollection> token2_;
0062   const edm::EDGetTokenT<P2GTCandidateCollection> token3_;
0063   const edm::EDGetTokenT<P2GTCandidateCollection> token4_;
0064   const edm::EDGetTokenT<P2GTCandidateCollection> primVertToken_;
0065 };
0066 
0067 L1GTQuadObjectCond::L1GTQuadObjectCond(const edm::ParameterSet& config)
0068     : scales_(config.getParameter<edm::ParameterSet>("scales")),
0069       collection1Cuts_(config.getParameter<edm::ParameterSet>("collection1"), config, scales_),
0070       collection2Cuts_(config.getParameter<edm::ParameterSet>("collection2"), config, scales_),
0071       collection3Cuts_(config.getParameter<edm::ParameterSet>("collection3"), config, scales_),
0072       collection4Cuts_(config.getParameter<edm::ParameterSet>("collection4"), config, scales_),
0073       enable_sanity_checks_(config.getUntrackedParameter<bool>("sanity_checks")),
0074       inv_mass_checks_(config.getUntrackedParameter<bool>("inv_mass_checks")),
0075       correl12Cuts_(
0076           config.getParameter<edm::ParameterSet>("correl12"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0077       correl13Cuts_(
0078           config.getParameter<edm::ParameterSet>("correl13"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0079       correl23Cuts_(
0080           config.getParameter<edm::ParameterSet>("correl23"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0081       correl14Cuts_(
0082           config.getParameter<edm::ParameterSet>("correl14"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0083       correl24Cuts_(
0084           config.getParameter<edm::ParameterSet>("correl24"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0085       correl34Cuts_(
0086           config.getParameter<edm::ParameterSet>("correl34"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0087       correl123Cuts_(config.getParameter<edm::ParameterSet>("correl123"), config, scales_, inv_mass_checks_),
0088       correl124Cuts_(config.getParameter<edm::ParameterSet>("correl124"), config, scales_, inv_mass_checks_),
0089       correl134Cuts_(config.getParameter<edm::ParameterSet>("correl134"), config, scales_, inv_mass_checks_),
0090       correl234Cuts_(config.getParameter<edm::ParameterSet>("correl234"), config, scales_, inv_mass_checks_),
0091       token1_(consumes<P2GTCandidateCollection>(collection1Cuts_.tag())),
0092       token2_(collection1Cuts_.tag() == collection2Cuts_.tag()
0093                   ? token1_
0094                   : consumes<P2GTCandidateCollection>(collection2Cuts_.tag())),
0095       token3_(collection1Cuts_.tag() == collection3Cuts_.tag()
0096                   ? token1_
0097                   : (collection2Cuts_.tag() == collection3Cuts_.tag()
0098                          ? token2_
0099                          : consumes<P2GTCandidateCollection>(collection3Cuts_.tag()))),
0100       token4_(collection1Cuts_.tag() == collection4Cuts_.tag()
0101                   ? token1_
0102                   : (collection2Cuts_.tag() == collection4Cuts_.tag()
0103                          ? token2_
0104                          : (collection3Cuts_.tag() == collection4Cuts_.tag()
0105                                 ? token3_
0106                                 : consumes<P2GTCandidateCollection>(collection4Cuts_.tag())))),
0107       primVertToken_(consumes<P2GTCandidateCollection>(config.getParameter<edm::InputTag>("primVertTag"))) {
0108   produces<P2GTCandidateVectorRef>(collection1Cuts_.tag().instance());
0109 
0110   if (!(collection1Cuts_.tag() == collection2Cuts_.tag())) {
0111     produces<P2GTCandidateVectorRef>(collection2Cuts_.tag().instance());
0112   }
0113 
0114   if (!(collection1Cuts_.tag() == collection3Cuts_.tag()) && !(collection2Cuts_.tag() == collection3Cuts_.tag())) {
0115     produces<P2GTCandidateVectorRef>(collection3Cuts_.tag().instance());
0116   }
0117 
0118   if (!(collection1Cuts_.tag() == collection4Cuts_.tag()) && !(collection2Cuts_.tag() == collection4Cuts_.tag()) &&
0119       !(collection3Cuts_.tag() == collection4Cuts_.tag())) {
0120     produces<P2GTCandidateVectorRef>(collection4Cuts_.tag().instance());
0121   }
0122 
0123   if (inv_mass_checks_) {
0124     produces<InvariantMassErrorCollection>();
0125   }
0126 }
0127 
0128 void L1GTQuadObjectCond::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0129   edm::ParameterSetDescription desc;
0130 
0131   edm::ParameterSetDescription collection1Desc;
0132   L1GTSingleCollectionCut::fillPSetDescription(collection1Desc);
0133   desc.add<edm::ParameterSetDescription>("collection1", collection1Desc);
0134 
0135   edm::ParameterSetDescription collection2Desc;
0136   L1GTSingleCollectionCut::fillPSetDescription(collection2Desc);
0137   desc.add<edm::ParameterSetDescription>("collection2", collection2Desc);
0138 
0139   edm::ParameterSetDescription collection3Desc;
0140   L1GTSingleCollectionCut::fillPSetDescription(collection3Desc);
0141   desc.add<edm::ParameterSetDescription>("collection3", collection3Desc);
0142 
0143   edm::ParameterSetDescription collection4Desc;
0144   L1GTSingleCollectionCut::fillPSetDescription(collection4Desc);
0145   desc.add<edm::ParameterSetDescription>("collection4", collection4Desc);
0146 
0147   edm::ParameterSetDescription scalesDesc;
0148   L1GTScales::fillPSetDescription(scalesDesc);
0149   desc.add<edm::ParameterSetDescription>("scales", scalesDesc);
0150 
0151   desc.add<edm::InputTag>("primVertTag");
0152 
0153   desc.addUntracked<bool>("sanity_checks", false);
0154   desc.addUntracked<bool>("inv_mass_checks", false);
0155 
0156   edm::ParameterSetDescription correl12Desc;
0157   L1GTCorrelationalCut::fillPSetDescription(correl12Desc);
0158   desc.add<edm::ParameterSetDescription>("correl12", correl12Desc);
0159 
0160   edm::ParameterSetDescription correl13Desc;
0161   L1GTCorrelationalCut::fillPSetDescription(correl13Desc);
0162   desc.add<edm::ParameterSetDescription>("correl13", correl13Desc);
0163 
0164   edm::ParameterSetDescription correl23Desc;
0165   L1GTCorrelationalCut::fillPSetDescription(correl23Desc);
0166   desc.add<edm::ParameterSetDescription>("correl23", correl23Desc);
0167 
0168   edm::ParameterSetDescription correl14Desc;
0169   L1GTCorrelationalCut::fillPSetDescription(correl14Desc);
0170   desc.add<edm::ParameterSetDescription>("correl14", correl14Desc);
0171 
0172   edm::ParameterSetDescription correl24Desc;
0173   L1GTCorrelationalCut::fillPSetDescription(correl24Desc);
0174   desc.add<edm::ParameterSetDescription>("correl24", correl24Desc);
0175 
0176   edm::ParameterSetDescription correl34Desc;
0177   L1GTCorrelationalCut::fillPSetDescription(correl34Desc);
0178   desc.add<edm::ParameterSetDescription>("correl34", correl34Desc);
0179 
0180   edm::ParameterSetDescription correl123Desc;
0181   L1GT3BodyCut::fillPSetDescription(correl123Desc);
0182   desc.add<edm::ParameterSetDescription>("correl123", correl123Desc);
0183 
0184   edm::ParameterSetDescription correl124Desc;
0185   L1GT3BodyCut::fillPSetDescription(correl124Desc);
0186   desc.add<edm::ParameterSetDescription>("correl124", correl124Desc);
0187 
0188   edm::ParameterSetDescription correl134Desc;
0189   L1GT3BodyCut::fillPSetDescription(correl134Desc);
0190   desc.add<edm::ParameterSetDescription>("correl134", correl134Desc);
0191 
0192   edm::ParameterSetDescription correl234Desc;
0193   L1GT3BodyCut::fillPSetDescription(correl234Desc);
0194   desc.add<edm::ParameterSetDescription>("correl234", correl234Desc);
0195 
0196   L1GTCorrelationalCut::fillLUTDescriptions(desc);
0197 
0198   descriptions.addWithDefaultLabel(desc);
0199 }
0200 
0201 bool L1GTQuadObjectCond::filter(edm::StreamID, edm::Event& event, const edm::EventSetup& setup) const {
0202   edm::Handle<P2GTCandidateCollection> col1 = event.getHandle(token1_);
0203   edm::Handle<P2GTCandidateCollection> col2 = event.getHandle(token2_);
0204   edm::Handle<P2GTCandidateCollection> col3 = event.getHandle(token3_);
0205   edm::Handle<P2GTCandidateCollection> col4 = event.getHandle(token4_);
0206   edm::Handle<P2GTCandidateCollection> primVertCol = event.getHandle(primVertToken_);
0207 
0208   bool condition_result = false;
0209 
0210   std::set<std::size_t> triggeredIdcs1;
0211   std::set<std::size_t> triggeredIdcs2;
0212   std::set<std::size_t> triggeredIdcs3;
0213   std::set<std::size_t> triggeredIdcs4;
0214 
0215   InvariantMassErrorCollection massErrors;
0216 
0217   for (std::size_t idx1 = 0; idx1 < col1->size(); ++idx1) {
0218     bool single1Pass = collection1Cuts_.checkObject(col1->at(idx1));
0219     single1Pass &= collection1Cuts_.checkPrimaryVertices(col1->at(idx1), *primVertCol);
0220 
0221     for (std::size_t idx2 = 0; idx2 < col2->size(); ++idx2) {
0222       bool single2Pass = collection2Cuts_.checkObject(col2->at(idx2));
0223       single2Pass &= collection2Cuts_.checkPrimaryVertices(col2->at(idx2), *primVertCol);
0224 
0225       for (std::size_t idx3 = 0; idx3 < col3->size(); ++idx3) {
0226         bool single3Pass = collection3Cuts_.checkObject(col3->at(idx3));
0227         single3Pass &= collection3Cuts_.checkPrimaryVertices(col3->at(idx3), *primVertCol);
0228 
0229         for (std::size_t idx4 = 0; idx4 < col4->size(); ++idx4) {
0230           // If we're looking at the same collection then we shouldn't use the same object in one comparison.
0231           if (col1.product() == col2.product() && idx1 == idx2) {
0232             continue;
0233           }
0234 
0235           if (col2.product() == col3.product() && idx2 == idx3) {
0236             continue;
0237           }
0238 
0239           if (col1.product() == col3.product() && idx1 == idx3) {
0240             continue;
0241           }
0242 
0243           if (col1.product() == col4.product() && idx1 == idx4) {
0244             continue;
0245           }
0246 
0247           if (col2.product() == col4.product() && idx2 == idx4) {
0248             continue;
0249           }
0250 
0251           if (col3.product() == col4.product() && idx3 == idx4) {
0252             continue;
0253           }
0254 
0255           bool pass = single1Pass & single2Pass & single3Pass;
0256 
0257           pass &= collection4Cuts_.checkObject(col4->at(idx4));
0258           pass &= collection4Cuts_.checkPrimaryVertices(col4->at(idx4), *primVertCol);
0259           pass &= correl12Cuts_.checkObjects(col1->at(idx1), col2->at(idx2), massErrors);
0260           pass &= correl13Cuts_.checkObjects(col1->at(idx1), col3->at(idx3), massErrors);
0261           pass &= correl23Cuts_.checkObjects(col2->at(idx2), col3->at(idx3), massErrors);
0262           pass &= correl14Cuts_.checkObjects(col1->at(idx1), col4->at(idx4), massErrors);
0263           pass &= correl24Cuts_.checkObjects(col2->at(idx2), col4->at(idx4), massErrors);
0264           pass &= correl34Cuts_.checkObjects(col3->at(idx3), col4->at(idx4), massErrors);
0265           pass &= correl123Cuts_.checkObjects(col1->at(idx1), col2->at(idx2), col3->at(idx3), massErrors);
0266           pass &= correl124Cuts_.checkObjects(col1->at(idx1), col2->at(idx2), col4->at(idx4), massErrors);
0267           pass &= correl134Cuts_.checkObjects(col1->at(idx1), col3->at(idx3), col4->at(idx4), massErrors);
0268           pass &= correl234Cuts_.checkObjects(col2->at(idx2), col3->at(idx3), col4->at(idx4), massErrors);
0269 
0270           condition_result |= pass;
0271 
0272           if (pass) {
0273             triggeredIdcs1.emplace(idx1);
0274 
0275             if (col1.product() != col2.product()) {
0276               triggeredIdcs2.emplace(idx2);
0277             } else {
0278               triggeredIdcs1.emplace(idx2);
0279             }
0280 
0281             if (col1.product() != col3.product() && col2.product() != col3.product()) {
0282               triggeredIdcs3.emplace(idx3);
0283             } else if (col1.product() == col3.product()) {
0284               triggeredIdcs1.emplace(idx3);
0285             } else {
0286               triggeredIdcs2.emplace(idx3);
0287             }
0288 
0289             if (col1.product() != col4.product() && col2.product() != col4.product() &&
0290                 col3.product() != col4.product()) {
0291               triggeredIdcs4.emplace(idx4);
0292             } else if (col1.product() == col4.product()) {
0293               triggeredIdcs1.emplace(idx4);
0294             } else if (col2.product() == col4.product()) {
0295               triggeredIdcs2.emplace(idx4);
0296             } else {
0297               triggeredIdcs3.emplace(idx4);
0298             }
0299           }
0300         }
0301       }
0302     }
0303   }
0304 
0305   condition_result &= collection1Cuts_.checkCollection(*col1);
0306   condition_result &= collection2Cuts_.checkCollection(*col2);
0307   condition_result &= collection3Cuts_.checkCollection(*col3);
0308   condition_result &= collection4Cuts_.checkCollection(*col4);
0309 
0310   if (condition_result) {
0311     std::unique_ptr<P2GTCandidateVectorRef> triggerCol1 = std::make_unique<P2GTCandidateVectorRef>();
0312 
0313     for (std::size_t idx : triggeredIdcs1) {
0314       triggerCol1->push_back(P2GTCandidateRef(col1, idx));
0315     }
0316     event.put(std::move(triggerCol1), collection1Cuts_.tag().instance());
0317 
0318     if (col1.product() != col2.product()) {
0319       std::unique_ptr<P2GTCandidateVectorRef> triggerCol2 = std::make_unique<P2GTCandidateVectorRef>();
0320 
0321       for (std::size_t idx : triggeredIdcs2) {
0322         triggerCol2->push_back(P2GTCandidateRef(col2, idx));
0323       }
0324       event.put(std::move(triggerCol2), collection2Cuts_.tag().instance());
0325     }
0326 
0327     if (col1.product() != col3.product() && col2.product() != col3.product()) {
0328       std::unique_ptr<P2GTCandidateVectorRef> triggerCol3 = std::make_unique<P2GTCandidateVectorRef>();
0329 
0330       for (std::size_t idx : triggeredIdcs3) {
0331         triggerCol3->push_back(P2GTCandidateRef(col3, idx));
0332       }
0333       event.put(std::move(triggerCol3), collection3Cuts_.tag().instance());
0334     }
0335 
0336     if (col1.product() != col4.product() && col2.product() != col4.product() && col3.product() != col4.product()) {
0337       std::unique_ptr<P2GTCandidateVectorRef> triggerCol4 = std::make_unique<P2GTCandidateVectorRef>();
0338 
0339       for (std::size_t idx : triggeredIdcs4) {
0340         triggerCol4->push_back(P2GTCandidateRef(col4, idx));
0341       }
0342       event.put(std::move(triggerCol4), collection4Cuts_.tag().instance());
0343     }
0344   }
0345 
0346   if (inv_mass_checks_) {
0347     event.put(std::make_unique<InvariantMassErrorCollection>(std::move(massErrors)), "");
0348   }
0349 
0350   return condition_result;
0351 }
0352 
0353 DEFINE_FWK_MODULE(L1GTQuadObjectCond);