Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:13:03

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/L1TTrackMatch
0004 // Class:      L1TrackerEtMissEmulatorProducer
0005 //
0006 /**\class L1TrackerEtMissEmulatorProducer L1TrackerEtMissEmulatorProducer.cc
0007  L1Trigger/L1TTrackMatch/plugins/L1TrackerEtMissEmulatorProducer.cc
0008 
0009  Description: Takes L1TTTracks and performs a integer emulation of Track-based
0010  missing Et, outputting a collection of EtSum 
0011 */
0012 //
0013 // Original Author:  Christopher Brown
0014 //         Created:  Fri, 19 Feb 2021
0015 //         Updated:  Wed, 16 Jun 2021
0016 //
0017 //
0018 
0019 // system include files
0020 #include <iomanip>
0021 #include <memory>
0022 #include <numeric>
0023 #include <sstream>
0024 // user include files
0025 #include "DataFormats/L1TrackTrigger/interface/TTTrack_TrackWord.h"
0026 #include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
0027 #include "DataFormats/L1Trigger/interface/EtSum.h"
0028 #include "DataFormats/L1Trigger/interface/VertexWord.h"
0029 #include "DataFormats/Math/interface/LorentzVector.h"
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/EventSetup.h"
0032 #include "FWCore/Framework/interface/Frameworkfwd.h"
0033 #include "FWCore/Framework/interface/MakerMacros.h"
0034 #include "FWCore/Framework/interface/stream/EDProducer.h"
0035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0036 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0037 #include "L1Trigger/L1TTrackMatch/interface/Cordic.h"
0038 #include "L1Trigger/L1TTrackMatch/interface/L1TkEtMissEmuAlgo.h"
0039 
0040 using namespace l1t;
0041 
0042 class L1TrackerEtMissEmulatorProducer : public edm::stream::EDProducer<> {
0043 public:
0044   typedef TTTrack<Ref_Phase2TrackerDigi_> L1TTTrackType;
0045   typedef std::vector<L1TTTrackType> L1TTTrackCollectionType;
0046   typedef edm::RefVector<L1TTTrackCollectionType> L1TTTrackRefCollectionType;
0047 
0048   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0049 
0050   explicit L1TrackerEtMissEmulatorProducer(const edm::ParameterSet&);
0051   ~L1TrackerEtMissEmulatorProducer() override;
0052 
0053 private:
0054   virtual void beginJob();
0055   void produce(edm::Event&, const edm::EventSetup&) override;
0056   virtual void endJob();
0057 
0058   // ----------member data ---------------------------
0059 
0060   std::vector<l1tmetemu::cos_lut_fixed_t> cosLUT_;  // Cos LUT array
0061   std::vector<l1tmetemu::global_phi_t> phiQuadrants_;
0062   std::vector<l1tmetemu::global_phi_t> phiShifts_;
0063 
0064   int cordicSteps_;
0065   int debug_;
0066   bool cordicDebug_ = false;
0067 
0068   std::string L1MetCollectionName_;
0069 
0070   const edm::EDGetTokenT<L1TTTrackRefCollectionType> trackToken_;
0071   const edm::EDGetTokenT<L1TTTrackRefCollectionType> vtxAssocTrackToken_;
0072 };
0073 
0074 // constructor//
0075 L1TrackerEtMissEmulatorProducer::L1TrackerEtMissEmulatorProducer(const edm::ParameterSet& iConfig)
0076     : trackToken_(consumes<L1TTTrackRefCollectionType>(iConfig.getParameter<edm::InputTag>("L1TrackInputTag"))),
0077       vtxAssocTrackToken_(
0078           consumes<L1TTTrackRefCollectionType>(iConfig.getParameter<edm::InputTag>("L1TrackAssociatedInputTag"))) {
0079   phiQuadrants_ = l1tmetemu::generatePhiSliceLUT(l1tmetemu::kNQuadrants);
0080   phiShifts_ = l1tmetemu::generatePhiSliceLUT(l1tmetemu::kNSector);
0081 
0082   // Get Emulator config parameters
0083   cordicSteps_ = (int)iConfig.getParameter<int>("nCordicSteps");
0084   debug_ = (int)iConfig.getParameter<int>("debug");
0085 
0086   // Name of output ED Product
0087   L1MetCollectionName_ = (std::string)iConfig.getParameter<std::string>("L1MetCollectionName");
0088 
0089   if (debug_ == 5) {
0090     cordicDebug_ = true;
0091   }
0092 
0093   // To have same bin spacing between 0 and pi/2 as between original phi
0094   // granularity
0095   int cosLUTbins = std::floor(l1tmetemu::kMaxCosLUTPhi / TTTrack_TrackWord::stepPhi0);
0096 
0097   // Compute LUTs
0098   cosLUT_ = l1tmetemu::generateCosLUT(cosLUTbins);
0099 
0100   // Print LUTs
0101   if (debug_ == 1) {
0102     l1tmetemu::printLUT(phiQuadrants_, "L1TrackerEtMissEmulatorProducer", "phiQuadrants_");
0103     l1tmetemu::printLUT(phiShifts_, "L1TrackerEtMissEmulatorProducer", "phiShifts_");
0104     l1tmetemu::printLUT(cosLUT_, "L1TrackerEtMissEmulatorProducer", "cosLUT_");
0105   }
0106 
0107   produces<std::vector<EtSum>>(L1MetCollectionName_);
0108 }
0109 
0110 L1TrackerEtMissEmulatorProducer::~L1TrackerEtMissEmulatorProducer() {}
0111 
0112 void L1TrackerEtMissEmulatorProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0113   using namespace edm;
0114 
0115   std::unique_ptr<std::vector<l1t::EtSum>> METCollection(new std::vector<l1t::EtSum>(0));
0116 
0117   edm::Handle<L1TTTrackRefCollectionType> L1TTTrackHandle;
0118   iEvent.getByToken(trackToken_, L1TTTrackHandle);
0119 
0120   edm::Handle<L1TTTrackRefCollectionType> L1TTTrackAssociatedHandle;
0121   iEvent.getByToken(vtxAssocTrackToken_, L1TTTrackAssociatedHandle);
0122 
0123   // Initialize cordic class
0124   Cordic cordicSqrt(cordicSteps_, cordicDebug_);
0125 
0126   if (!L1TTTrackHandle.isValid()) {
0127     LogError("L1TrackerEtMissEmulatorProducer") << "\nWarning: L1TTTrackCollection not found in the event. Exit\n";
0128     return;
0129   }
0130 
0131   if (!L1TTTrackAssociatedHandle.isValid()) {
0132     LogError("L1TrackerEtMissEmulatorProducer")
0133         << "\nWarning: L1TTTrackAssociatedCollection not found in the event. Exit\n";
0134     return;
0135   }
0136 
0137   // Initialize sector sums, need 0 initialization in case a sector has no
0138   // tracks
0139   l1tmetemu::Et_t sumPx[l1tmetemu::kNSector * 2] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
0140   l1tmetemu::Et_t sumPy[l1tmetemu::kNSector * 2] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
0141   int link_totals[l1tmetemu::kNSector * 2] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
0142   int sector_totals[l1tmetemu::kNSector] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
0143 
0144   // Track counters
0145   int num_assoc_tracks{0};
0146 
0147   for (const auto& track : *L1TTTrackHandle) {
0148     if (std::find(L1TTTrackAssociatedHandle->begin(), L1TTTrackAssociatedHandle->end(), track) !=
0149         L1TTTrackAssociatedHandle->end()) {
0150       bool EtaSector = (track->getTanlWord() & (1 << (TTTrack_TrackWord::TrackBitWidths::kTanlSize - 1)));
0151 
0152       ap_uint<TTTrack_TrackWord::TrackBitWidths::kRinvSize - 1> ptEmulationBits = track->getTrackWord()(
0153           TTTrack_TrackWord::TrackBitLocations::kRinvMSB - 1, TTTrack_TrackWord::TrackBitLocations::kRinvLSB);
0154       ap_ufixed<TTTrack_TrackWord::TrackBitWidths::kRinvSize - 1, l1tmetemu::kPtMagSize> ptEmulation;
0155       ptEmulation.V = ptEmulationBits.range();
0156 
0157       l1tmetemu::global_phi_t globalPhi =
0158           l1tmetemu::localToGlobalPhi(track->getPhiWord(), phiShifts_[track->phiSector()]);
0159 
0160       num_assoc_tracks++;
0161       if (debug_ == 7) {
0162         edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0163             << "Track to Vertex ID: " << num_assoc_tracks << "\n"
0164             << "Phi Sector: " << track->phiSector() << " pT: " << track->getRinvWord()
0165             << " Phi: " << track->getPhiWord() << " TanL: " << track->getTanlWord() << " Z0: " << track->getZ0Word()
0166             << " Chi2rphi: " << track->getChi2RPhiWord() << " Chi2rz: " << track->getChi2RZWord()
0167             << " bendChi2: " << track->getBendChi2Word() << " Emu pT " << ptEmulation.to_double() << "\n"
0168             << "--------------------------------------------------------------\n";
0169       }
0170 
0171       if (debug_ == 2) {
0172         edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0173             << "========================Phi debug=================================\n"
0174             << "Emu pT: " << ptEmulation.to_double() << " float pT: " << track->momentum().perp() << "\n"
0175             << "Int Phi: " << globalPhi << " Float Phi: " << track->phi() << " Float Cos(Phi): " << cos(track->phi())
0176             << "  Float Sin(Phi): " << sin(track->phi())
0177             << " Float Px: " << track->momentum().perp() * cos(track->phi())
0178             << " Float Py: " << track->momentum().perp() * sin(track->phi()) << "\n";
0179       }
0180 
0181       l1tmetemu::Et_t temppx = 0;
0182       l1tmetemu::Et_t temppy = 0;
0183 
0184       // Split tracks in phi quadrants and access cosLUT_, backwards iteration
0185       // through cosLUT_ gives sin Sum sector Et -ve when cos or sin phi are -ve
0186       sector_totals[track->phiSector()] += 1;
0187       if (globalPhi >= phiQuadrants_[0] && globalPhi < phiQuadrants_[1]) {
0188         temppx = ((l1tmetemu::Et_t)ptEmulation * cosLUT_[globalPhi]);
0189         temppy = ((l1tmetemu::Et_t)ptEmulation * cosLUT_[phiQuadrants_[1] - 1 - globalPhi]);
0190 
0191         if (debug_ == 2) {
0192           edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0193               << "Sector: " << track->phiSector() << " Quadrant: " << 1 << "\n"
0194               << "Emu Phi: " << globalPhi << " Emu Cos(Phi): " << cosLUT_[globalPhi]
0195               << " Emu Sin(Phi): " << cosLUT_[phiQuadrants_[1] - 1 - globalPhi] << "\n";
0196         }
0197       } else if (globalPhi >= phiQuadrants_[1] && globalPhi < phiQuadrants_[2]) {
0198         temppx = -((l1tmetemu::Et_t)ptEmulation * cosLUT_[phiQuadrants_[2] - 1 - globalPhi]);
0199         temppy = ((l1tmetemu::Et_t)ptEmulation * cosLUT_[globalPhi - phiQuadrants_[1]]);
0200 
0201         if (debug_ == 2) {
0202           edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0203               << "Sector: " << track->phiSector() << " Quadrant: " << 2 << "\n"
0204               << "Emu Phi: " << globalPhi << " Emu Cos(Phi): -" << cosLUT_[phiQuadrants_[2] - 1 - globalPhi]
0205               << " Emu Sin(Phi): " << cosLUT_[globalPhi - phiQuadrants_[1]] << "\n";
0206         }
0207       } else if (globalPhi >= phiQuadrants_[2] && globalPhi < phiQuadrants_[3]) {
0208         temppx = -((l1tmetemu::Et_t)ptEmulation * cosLUT_[globalPhi - phiQuadrants_[2]]);
0209         temppy = -((l1tmetemu::Et_t)ptEmulation * cosLUT_[phiQuadrants_[3] - 1 - globalPhi]);
0210 
0211         if (debug_ == 2) {
0212           edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0213               << "Sector: " << track->phiSector() << " Quadrant: " << 3 << "\n"
0214               << "Emu Phi: " << globalPhi << " Emu Cos(Phi): -" << cosLUT_[globalPhi - phiQuadrants_[2]]
0215               << " Emu Sin(Phi): -" << cosLUT_[phiQuadrants_[3] - 1 - globalPhi] << "\n";
0216         }
0217 
0218       } else if (globalPhi >= phiQuadrants_[3] && globalPhi < phiQuadrants_[4]) {
0219         temppx = ((l1tmetemu::Et_t)ptEmulation * cosLUT_[phiQuadrants_[4] - 1 - globalPhi]);
0220         temppy = -((l1tmetemu::Et_t)ptEmulation * cosLUT_[globalPhi - phiQuadrants_[3]]);
0221 
0222         if (debug_ == 2) {
0223           edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0224               << "Sector: " << track->phiSector() << " Quadrant: " << 4 << "\n"
0225               << " Emu Phi: " << globalPhi << " Emu Cos(Phi): " << cosLUT_[phiQuadrants_[4] - 1 - globalPhi]
0226               << " Emu Sin(Phi): -" << cosLUT_[globalPhi - phiQuadrants_[3]] << "\n";
0227         }
0228       } else {
0229         temppx = 0;
0230         temppy = 0;
0231       }
0232 
0233       int link_number = (track->phiSector() * 2) + ((EtaSector) ? 0 : 1);
0234       link_totals[link_number] += 1;
0235       sumPx[link_number] += temppx;
0236       sumPy[link_number] += temppy;
0237 
0238       if (debug_ == 4) {
0239         edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0240             << std::setprecision(8) << "Sector: " << track->phiSector() << " Eta sector: " << EtaSector << "\n"
0241             << "Track Ref Pt: " << track->momentum().perp() << " Track Ref Px: " << track->momentum().x()
0242             << " Track Ref Py: " << track->momentum().y() << "\n"
0243             << "Track Pt: " << ptEmulation << " Track phi: " << globalPhi << " Track Px: " << temppx
0244             << " Track Py: " << temppy << "\n"
0245             << "Sector Sum Px: " << sumPx[link_number] << " Sector Sum Py: " << sumPy[link_number] << "\n";
0246       }
0247     }
0248   }  // end loop over tracks
0249 
0250   l1tmetemu::Et_t GlobalPx = 0;
0251   l1tmetemu::Et_t GlobalPy = 0;
0252 
0253   // Global Et sum as floats to emulate rounding in HW
0254   for (unsigned int i = 0; i < l1tmetemu::kNSector * 2; i++) {
0255     GlobalPx += sumPx[i];
0256     GlobalPy += sumPy[i];
0257   }
0258 
0259   // Perform cordic sqrt, take x,y and converts to polar coordinate r,phi where
0260   // r=sqrt(x**2+y**2) and phi = atan(y/x)
0261   l1tmetemu::EtMiss EtMiss = cordicSqrt.toPolar(-GlobalPx, -GlobalPy);
0262 
0263   if (debug_ == 4 || debug_ == 6) {
0264     edm::LogVerbatim("L1TrackerEtMissEmulatorProducer") << "====Sector Pt====\n";
0265 
0266     for (unsigned int i = 0; i < l1tmetemu::kNSector * 2; i++) {
0267       edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0268           << "Sector " << i << "\n"
0269           << "Px: " << sumPx[i] << " | Py: " << sumPy[i] << " | Link Totals: " << link_totals[i]
0270           << " | Sector Totals: " << sector_totals[(int)(i / 2)] << "\n";
0271     }
0272 
0273     edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0274         << "====Global Pt====\n"
0275         << "Global Px: " << GlobalPx << "| Global Py: " << GlobalPy << "\n";
0276 
0277     edm::LogVerbatim("L1TrackerEtMissEmulatorProducer")
0278         << "====MET===\n"
0279         << "MET word Et: " << EtMiss.Et.range() * l1tmetemu::kStepMETwordEt << "| MET word phi: " << EtMiss.Phi << "\n"
0280         << "MET: " << EtMiss.Et.to_double() << "| MET phi: " << EtMiss.Phi.to_double() * l1tmetemu::kStepMETwordPhi
0281         << "\n"
0282         << "Word MET: " << EtMiss.Et.to_string(2) << " | Word MET phi: " << EtMiss.Phi.to_string(2) << "\n"
0283         << "# Tracks Associated to Vertex: " << num_assoc_tracks << "\n"
0284         << "========================================================\n";
0285   }
0286 
0287   math::XYZTLorentzVector missingEt(-GlobalPx, -GlobalPy, 0, EtMiss.Et);
0288   EtSum L1EtSum(missingEt, EtSum::EtSumType::kMissingEt, EtMiss.Et.range(), 0, EtMiss.Phi, num_assoc_tracks);
0289 
0290   METCollection->push_back(L1EtSum);
0291 
0292   iEvent.put(std::move(METCollection), L1MetCollectionName_);
0293 }  // end producer
0294 
0295 void L1TrackerEtMissEmulatorProducer::beginJob() {}
0296 
0297 void L1TrackerEtMissEmulatorProducer::endJob() {}
0298 
0299 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0300 void L1TrackerEtMissEmulatorProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0301   //The following says we do not know what parameters are allowed so do no validation
0302   // Please change this to state exactly what you do use, even if it is no parameters
0303   edm::ParameterSetDescription desc;
0304   desc.setUnknown();
0305   descriptions.addDefault(desc);
0306 }
0307 
0308 // define this as a plug-in
0309 DEFINE_FWK_MODULE(L1TrackerEtMissEmulatorProducer);