Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:53:55

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/stream/EDProducer.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 
0007 #include "MagneticField/Engine/interface/MagneticField.h"
0008 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0009 #include "FWCore/Framework/interface/ESWatcher.h"
0010 
0011 #include "DataFormats/L1TParticleFlow/interface/PFTrack.h"
0012 #include "L1Trigger/Phase2L1ParticleFlow/interface/L1TPFUtils.h"
0013 #include "L1Trigger/Phase2L1ParticleFlow/interface/ParametricResolution.h"
0014 
0015 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0016 
0017 namespace l1tpf {
0018   class PFTrackProducerFromL1Tracks : public edm::stream::EDProducer<> {
0019   public:
0020     explicit PFTrackProducerFromL1Tracks(const edm::ParameterSet &);
0021     ~PFTrackProducerFromL1Tracks() override {}
0022 
0023   private:
0024     edm::EDGetTokenT<std::vector<l1t::PFTrack::L1TTTrackType>> TrackTag_;
0025     edm::ESWatcher<IdealMagneticFieldRecord> BFieldWatcher_;
0026     edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> BFieldTag_;
0027     int nParam_;
0028     float fBz_;
0029     l1tpf::ParametricResolution resolCalo_, resolTrk_;
0030     std::vector<StringCutObjectSelector<l1t::PFTrack::L1TTTrackType>> qualityBitSetters_;
0031     bool redigitizeTrackWord_;
0032 
0033     void produce(edm::Event &, const edm::EventSetup &) override;
0034 
0035   };  // class
0036 }  // namespace l1tpf
0037 
0038 l1tpf::PFTrackProducerFromL1Tracks::PFTrackProducerFromL1Tracks(const edm::ParameterSet &iConfig)
0039     : TrackTag_(consumes<std::vector<l1t::PFTrack::L1TTTrackType>>(iConfig.getParameter<edm::InputTag>("L1TrackTag"))),
0040       BFieldTag_{esConsumes<MagneticField, IdealMagneticFieldRecord>()},
0041       nParam_(iConfig.getParameter<unsigned int>("nParam")),
0042       resolCalo_(iConfig.getParameter<edm::ParameterSet>("resolCalo")),
0043       resolTrk_(iConfig.getParameter<edm::ParameterSet>("resolTrack")),
0044       redigitizeTrackWord_(iConfig.getParameter<bool>("redigitizeTrackWord")) {
0045   for (const auto &cut : iConfig.getParameter<std::vector<std::string>>("qualityBits")) {
0046     qualityBitSetters_.emplace_back(cut);
0047   }
0048   produces<l1t::PFTrackCollection>();
0049 }
0050 
0051 void l1tpf::PFTrackProducerFromL1Tracks::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0052   if (BFieldWatcher_.check(iSetup)) {
0053     fBz_ = iSetup.getData(BFieldTag_).inTesla(GlobalPoint(0, 0, 0)).z();
0054   }
0055 
0056   std::unique_ptr<l1t::PFTrackCollection> out(new l1t::PFTrackCollection());
0057 
0058   // https://github.com/skinnari/cmssw/blob/80c19f1b721325c3a02ee0482f72fb974a4c3bf7/L1Trigger/TrackFindingTracklet/test/L1TrackNtupleMaker.cc
0059   edm::Handle<std::vector<l1t::PFTrack::L1TTTrackType>> htracks;
0060   iEvent.getByToken(TrackTag_, htracks);
0061   const auto &tracks = *htracks;
0062 
0063   for (unsigned int i = 0, n = tracks.size(); i < n; ++i) {
0064     const auto &tk = tracks[i];
0065 
0066     float pt = tk.momentum().perp();
0067     float eta = tk.momentum().eta();
0068     float phi = tk.momentum().phi();
0069     float z0 = tk.POCA().z();  //cm
0070     int charge = tk.rInv() > 0 ? +1 : -1;
0071 
0072     reco::Candidate::PolarLorentzVector p4p(pt, eta, phi, 0.137);  // pion mass
0073     reco::Particle::LorentzVector p4(p4p.X(), p4p.Y(), p4p.Z(), p4p.E());
0074     reco::Particle::Point vtx(tk.POCA().x(), tk.POCA().y(), z0);
0075 
0076     auto caloetaphi = l1tpf::propagateToCalo(p4, math::XYZTLorentzVector(0., 0., z0, 0.), charge, fBz_);
0077 
0078     float trkErr = resolTrk_(pt, std::abs(eta));
0079     float caloErr = resolCalo_(pt, std::abs(eta));
0080     int quality = 0;
0081     for (int i = 0, n = qualityBitSetters_.size(), bit = 1; i < n; ++i, bit <<= 1) {
0082       if (qualityBitSetters_[i](tk))
0083         quality += bit;
0084     }
0085     out->emplace_back(charge,
0086                       p4,
0087                       vtx,
0088                       l1t::PFTrack::TrackRef(htracks, i),
0089                       nParam_,
0090                       caloetaphi.first,
0091                       caloetaphi.second,
0092                       trkErr,
0093                       caloErr,
0094                       quality);
0095 
0096     if (redigitizeTrackWord_) {
0097       // Force re-digitization if an old TTrack object is read from an EDM file, and update the quaility bit for now
0098       l1t::PFTrack::L1TTTrackType trackCopy = tk;
0099       trackCopy.setTrackWordBits();  // important
0100       TTTrack_TrackWord &tw = out->back().trackWord();
0101       tw.setTrackWord(trackCopy.getValidWord(),
0102                       trackCopy.getRinvWord(),
0103                       trackCopy.getPhiWord(),
0104                       trackCopy.getTanlWord(),
0105                       trackCopy.getZ0Word(),
0106                       trackCopy.getD0Word(),
0107                       trackCopy.getChi2RPhiWord(),
0108                       trackCopy.getChi2RZWord(),
0109                       trackCopy.getBendChi2Word(),
0110                       trackCopy.getHitPatternWord(),
0111                       trackCopy.getMVAQualityWord(),
0112                       ap_uint<TTTrack_TrackWord::kMVAOtherSize>(quality));
0113     }
0114   }
0115   iEvent.put(std::move(out));
0116 }
0117 using l1tpf::PFTrackProducerFromL1Tracks;
0118 DEFINE_FWK_MODULE(PFTrackProducerFromL1Tracks);