Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:55:51

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 "L1GTDeltaCut.h"
0016 #include "L1GTSingleInOutLUT.h"
0017 
0018 #include <cinttypes>
0019 #include <memory>
0020 #include <vector>
0021 #include <set>
0022 
0023 #include <ap_int.h>
0024 
0025 using namespace l1t;
0026 
0027 class L1GTQuadObjectCond : public edm::global::EDFilter<> {
0028 public:
0029   explicit L1GTQuadObjectCond(const edm::ParameterSet&);
0030   ~L1GTQuadObjectCond() override = default;
0031 
0032   static void fillDescriptions(edm::ConfigurationDescriptions&);
0033 
0034 private:
0035   bool filter(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0036 
0037   const L1GTScales scales_;
0038 
0039   const L1GTSingleCollectionCut collection1Cuts_;
0040   const L1GTSingleCollectionCut collection2Cuts_;
0041   const L1GTSingleCollectionCut collection3Cuts_;
0042   const L1GTSingleCollectionCut collection4Cuts_;
0043 
0044   const bool enable_sanity_checks_;
0045   const bool inv_mass_checks_;
0046 
0047   const L1GTDeltaCut delta12Cuts_;
0048   const L1GTDeltaCut delta13Cuts_;
0049   const L1GTDeltaCut delta23Cuts_;
0050   const L1GTDeltaCut delta14Cuts_;
0051   const L1GTDeltaCut delta24Cuts_;
0052   const L1GTDeltaCut delta34Cuts_;
0053 
0054   const edm::EDGetTokenT<P2GTCandidateCollection> token1_;
0055   const edm::EDGetTokenT<P2GTCandidateCollection> token2_;
0056   const edm::EDGetTokenT<P2GTCandidateCollection> token3_;
0057   const edm::EDGetTokenT<P2GTCandidateCollection> token4_;
0058 };
0059 
0060 L1GTQuadObjectCond::L1GTQuadObjectCond(const edm::ParameterSet& config)
0061     : scales_(config.getParameter<edm::ParameterSet>("scales")),
0062       collection1Cuts_(config.getParameter<edm::ParameterSet>("collection1"), config, scales_),
0063       collection2Cuts_(config.getParameter<edm::ParameterSet>("collection2"), config, scales_),
0064       collection3Cuts_(config.getParameter<edm::ParameterSet>("collection3"), config, scales_),
0065       collection4Cuts_(config.getParameter<edm::ParameterSet>("collection4"), config, scales_),
0066       enable_sanity_checks_(config.getUntrackedParameter<bool>("sanity_checks")),
0067       inv_mass_checks_(config.getUntrackedParameter<bool>("inv_mass_checks")),
0068       delta12Cuts_(
0069           config.getParameter<edm::ParameterSet>("delta12"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0070       delta13Cuts_(
0071           config.getParameter<edm::ParameterSet>("delta13"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0072       delta23Cuts_(
0073           config.getParameter<edm::ParameterSet>("delta23"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0074       delta14Cuts_(
0075           config.getParameter<edm::ParameterSet>("delta14"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0076       delta24Cuts_(
0077           config.getParameter<edm::ParameterSet>("delta24"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0078       delta34Cuts_(
0079           config.getParameter<edm::ParameterSet>("delta34"), config, scales_, enable_sanity_checks_, inv_mass_checks_),
0080       token1_(consumes<P2GTCandidateCollection>(collection1Cuts_.tag())),
0081       token2_(collection1Cuts_.tag() == collection2Cuts_.tag()
0082                   ? token1_
0083                   : consumes<P2GTCandidateCollection>(collection2Cuts_.tag())),
0084       token3_(collection1Cuts_.tag() == collection3Cuts_.tag()
0085                   ? token1_
0086                   : (collection2Cuts_.tag() == collection3Cuts_.tag()
0087                          ? token2_
0088                          : consumes<P2GTCandidateCollection>(collection3Cuts_.tag()))),
0089       token4_(collection1Cuts_.tag() == collection4Cuts_.tag()
0090                   ? token1_
0091                   : (collection2Cuts_.tag() == collection4Cuts_.tag()
0092                          ? token2_
0093                          : (collection3Cuts_.tag() == collection4Cuts_.tag()
0094                                 ? token3_
0095                                 : consumes<P2GTCandidateCollection>(collection4Cuts_.tag())))) {
0096   produces<P2GTCandidateVectorRef>(collection1Cuts_.tag().instance());
0097 
0098   if (!(collection1Cuts_.tag() == collection2Cuts_.tag())) {
0099     produces<P2GTCandidateVectorRef>(collection2Cuts_.tag().instance());
0100   }
0101 
0102   if (!(collection1Cuts_.tag() == collection3Cuts_.tag()) && !(collection2Cuts_.tag() == collection3Cuts_.tag())) {
0103     produces<P2GTCandidateVectorRef>(collection3Cuts_.tag().instance());
0104   }
0105 
0106   if (!(collection1Cuts_.tag() == collection4Cuts_.tag()) && !(collection2Cuts_.tag() == collection4Cuts_.tag()) &&
0107       !(collection3Cuts_.tag() == collection4Cuts_.tag())) {
0108     produces<P2GTCandidateVectorRef>(collection4Cuts_.tag().instance());
0109   }
0110 
0111   if (inv_mass_checks_) {
0112     produces<InvariantMassErrorCollection>();
0113   }
0114 }
0115 
0116 void L1GTQuadObjectCond::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0117   edm::ParameterSetDescription desc;
0118 
0119   edm::ParameterSetDescription collection1Desc;
0120   L1GTSingleCollectionCut::fillPSetDescription(collection1Desc);
0121   desc.add<edm::ParameterSetDescription>("collection1", collection1Desc);
0122 
0123   edm::ParameterSetDescription collection2Desc;
0124   L1GTSingleCollectionCut::fillPSetDescription(collection2Desc);
0125   desc.add<edm::ParameterSetDescription>("collection2", collection2Desc);
0126 
0127   edm::ParameterSetDescription collection3Desc;
0128   L1GTSingleCollectionCut::fillPSetDescription(collection3Desc);
0129   desc.add<edm::ParameterSetDescription>("collection3", collection3Desc);
0130 
0131   edm::ParameterSetDescription collection4Desc;
0132   L1GTSingleCollectionCut::fillPSetDescription(collection4Desc);
0133   desc.add<edm::ParameterSetDescription>("collection4", collection4Desc);
0134 
0135   edm::ParameterSetDescription scalesDesc;
0136   L1GTScales::fillPSetDescription(scalesDesc);
0137   desc.add<edm::ParameterSetDescription>("scales", scalesDesc);
0138 
0139   desc.addUntracked<bool>("sanity_checks", false);
0140   desc.addUntracked<bool>("inv_mass_checks", false);
0141 
0142   edm::ParameterSetDescription delta12Desc;
0143   L1GTDeltaCut::fillPSetDescription(delta12Desc);
0144   desc.add<edm::ParameterSetDescription>("delta12", delta12Desc);
0145 
0146   edm::ParameterSetDescription delta13Desc;
0147   L1GTDeltaCut::fillPSetDescription(delta13Desc);
0148   desc.add<edm::ParameterSetDescription>("delta13", delta13Desc);
0149 
0150   edm::ParameterSetDescription delta23Desc;
0151   L1GTDeltaCut::fillPSetDescription(delta23Desc);
0152   desc.add<edm::ParameterSetDescription>("delta23", delta23Desc);
0153 
0154   edm::ParameterSetDescription delta14Desc;
0155   L1GTDeltaCut::fillPSetDescription(delta14Desc);
0156   desc.add<edm::ParameterSetDescription>("delta14", delta14Desc);
0157 
0158   edm::ParameterSetDescription delta24Desc;
0159   L1GTDeltaCut::fillPSetDescription(delta24Desc);
0160   desc.add<edm::ParameterSetDescription>("delta24", delta24Desc);
0161 
0162   edm::ParameterSetDescription delta34Desc;
0163   L1GTDeltaCut::fillPSetDescription(delta34Desc);
0164   desc.add<edm::ParameterSetDescription>("delta34", delta34Desc);
0165 
0166   L1GTDeltaCut::fillLUTDescriptions(desc);
0167 
0168   descriptions.addWithDefaultLabel(desc);
0169 }
0170 
0171 bool L1GTQuadObjectCond::filter(edm::StreamID, edm::Event& event, const edm::EventSetup& setup) const {
0172   edm::Handle<P2GTCandidateCollection> col1 = event.getHandle(token1_);
0173   edm::Handle<P2GTCandidateCollection> col2 = event.getHandle(token2_);
0174   edm::Handle<P2GTCandidateCollection> col3 = event.getHandle(token3_);
0175   edm::Handle<P2GTCandidateCollection> col4 = event.getHandle(token4_);
0176 
0177   bool condition_result = false;
0178 
0179   std::set<std::size_t> triggeredIdcs1;
0180   std::set<std::size_t> triggeredIdcs2;
0181   std::set<std::size_t> triggeredIdcs3;
0182   std::set<std::size_t> triggeredIdcs4;
0183 
0184   InvariantMassErrorCollection massErrors;
0185 
0186   for (std::size_t idx1 = 0; idx1 < col1->size(); ++idx1) {
0187     for (std::size_t idx2 = 0; idx2 < col2->size(); ++idx2) {
0188       for (std::size_t idx3 = 0; idx3 < col3->size(); ++idx3) {
0189         for (std::size_t idx4 = 0; idx4 < col4->size(); ++idx4) {
0190           // If we're looking at the same collection then we shouldn't use the same object in one comparison.
0191           if (col1.product() == col2.product() && idx1 == idx2) {
0192             continue;
0193           }
0194 
0195           if (col2.product() == col3.product() && idx2 == idx3) {
0196             continue;
0197           }
0198 
0199           if (col1.product() == col3.product() && idx1 == idx3) {
0200             continue;
0201           }
0202 
0203           if (col1.product() == col4.product() && idx1 == idx4) {
0204             continue;
0205           }
0206 
0207           if (col2.product() == col4.product() && idx2 == idx4) {
0208             continue;
0209           }
0210 
0211           if (col3.product() == col4.product() && idx3 == idx4) {
0212             continue;
0213           }
0214 
0215           bool pass = true;
0216           pass &= collection1Cuts_.checkObject(col1->at(idx1));
0217           pass &= collection2Cuts_.checkObject(col2->at(idx2));
0218           pass &= collection3Cuts_.checkObject(col3->at(idx3));
0219           pass &= collection4Cuts_.checkObject(col4->at(idx4));
0220           pass &= delta12Cuts_.checkObjects(col1->at(idx1), col2->at(idx2), massErrors);
0221           pass &= delta13Cuts_.checkObjects(col1->at(idx1), col3->at(idx3), massErrors);
0222           pass &= delta23Cuts_.checkObjects(col2->at(idx2), col3->at(idx3), massErrors);
0223           pass &= delta14Cuts_.checkObjects(col1->at(idx1), col4->at(idx4), massErrors);
0224           pass &= delta24Cuts_.checkObjects(col2->at(idx2), col4->at(idx4), massErrors);
0225           pass &= delta34Cuts_.checkObjects(col3->at(idx3), col4->at(idx4), massErrors);
0226           condition_result |= pass;
0227 
0228           if (pass) {
0229             triggeredIdcs1.emplace(idx1);
0230 
0231             if (col1.product() != col2.product()) {
0232               triggeredIdcs2.emplace(idx2);
0233             } else {
0234               triggeredIdcs1.emplace(idx2);
0235             }
0236 
0237             if (col1.product() != col3.product() && col2.product() != col3.product()) {
0238               triggeredIdcs3.emplace(idx3);
0239             } else if (col1.product() == col3.product()) {
0240               triggeredIdcs1.emplace(idx3);
0241             } else {
0242               triggeredIdcs2.emplace(idx3);
0243             }
0244 
0245             if (col1.product() != col4.product() && col2.product() != col4.product() &&
0246                 col3.product() != col4.product()) {
0247               triggeredIdcs4.emplace(idx4);
0248             } else if (col1.product() == col4.product()) {
0249               triggeredIdcs1.emplace(idx4);
0250             } else if (col2.product() == col4.product()) {
0251               triggeredIdcs2.emplace(idx4);
0252             } else {
0253               triggeredIdcs3.emplace(idx4);
0254             }
0255           }
0256         }
0257       }
0258     }
0259   }
0260 
0261   if (condition_result) {
0262     std::unique_ptr<P2GTCandidateVectorRef> triggerCol1 = std::make_unique<P2GTCandidateVectorRef>();
0263 
0264     for (std::size_t idx : triggeredIdcs1) {
0265       triggerCol1->push_back(P2GTCandidateRef(col1, idx));
0266     }
0267     event.put(std::move(triggerCol1), collection1Cuts_.tag().instance());
0268 
0269     if (col1.product() != col2.product()) {
0270       std::unique_ptr<P2GTCandidateVectorRef> triggerCol2 = std::make_unique<P2GTCandidateVectorRef>();
0271 
0272       for (std::size_t idx : triggeredIdcs2) {
0273         triggerCol2->push_back(P2GTCandidateRef(col2, idx));
0274       }
0275       event.put(std::move(triggerCol2), collection2Cuts_.tag().instance());
0276     }
0277 
0278     if (col1.product() != col3.product() && col2.product() != col3.product()) {
0279       std::unique_ptr<P2GTCandidateVectorRef> triggerCol3 = std::make_unique<P2GTCandidateVectorRef>();
0280 
0281       for (std::size_t idx : triggeredIdcs3) {
0282         triggerCol3->push_back(P2GTCandidateRef(col3, idx));
0283       }
0284       event.put(std::move(triggerCol3), collection3Cuts_.tag().instance());
0285     }
0286 
0287     if (col1.product() != col4.product() && col2.product() != col4.product() && col3.product() != col4.product()) {
0288       std::unique_ptr<P2GTCandidateVectorRef> triggerCol4 = std::make_unique<P2GTCandidateVectorRef>();
0289 
0290       for (std::size_t idx : triggeredIdcs4) {
0291         triggerCol4->push_back(P2GTCandidateRef(col4, idx));
0292       }
0293       event.put(std::move(triggerCol4), collection4Cuts_.tag().instance());
0294     }
0295   }
0296 
0297   if (inv_mass_checks_) {
0298     event.put(std::make_unique<InvariantMassErrorCollection>(std::move(massErrors)), "");
0299   }
0300 
0301   return condition_result;
0302 }
0303 
0304 DEFINE_FWK_MODULE(L1GTQuadObjectCond);