Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:26

0001 // -*- C++ -*-
0002 //
0003 // Package:    Calibration/TkAlCaRecoProducers
0004 // Class:      TrackDistanceValueMapProducer
0005 //
0006 /**\class TrackDistanceValueMapProducer TrackDistanceValueMapProducer.cc Calibration/TkAlCaRecoProducers/plugins/TrackDistanceValueMapProducer.cc
0007 
0008  Description: creates a value map for each saved muon track with all the distances of the other track w.r.t. the muon track 
0009 
0010 */
0011 //
0012 // Original Author:  Marco Musich
0013 //         Created:  Mon, 12 Apr 2021 11:59:39 GMT
0014 //
0015 //
0016 
0017 // system include files
0018 #include <memory>
0019 
0020 // user include files
0021 #include "FWCore/Framework/interface/Frameworkfwd.h"
0022 #include "FWCore/Framework/interface/global/EDProducer.h"
0023 
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Framework/interface/MakerMacros.h"
0026 
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/Utilities/interface/StreamID.h"
0029 
0030 #include "DataFormats/TrackReco/interface/Track.h"
0031 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0032 
0033 #include "DataFormats/Math/interface/deltaR.h"
0034 //
0035 // class declaration
0036 //
0037 
0038 class TrackDistanceValueMapProducer : public edm::global::EDProducer<> {
0039 public:
0040   explicit TrackDistanceValueMapProducer(const edm::ParameterSet&);
0041   ~TrackDistanceValueMapProducer() override = default;
0042 
0043   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0044 
0045 private:
0046   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0047 
0048   // ----------member data ---------------------------
0049   // edToken
0050   edm::EDGetTokenT<edm::View<reco::Track>> muonTracksToken_;
0051   edm::EDGetTokenT<edm::View<reco::Track>> otherTracksToken_;
0052 
0053   // save in the event only up to the n-th closest
0054   unsigned int nthClosestTrack_;
0055 
0056   // putToken
0057   edm::EDPutTokenT<edm::ValueMap<std::vector<float>>> distancesPutToken_;
0058 };
0059 
0060 //
0061 // constructors and destructor
0062 //
0063 TrackDistanceValueMapProducer::TrackDistanceValueMapProducer(const edm::ParameterSet& iConfig)
0064     : muonTracksToken_(consumes<edm::View<reco::Track>>(iConfig.getParameter<edm::InputTag>("muonTracks"))),
0065       otherTracksToken_(consumes<edm::View<reco::Track>>(iConfig.getParameter<edm::InputTag>("allTracks"))),
0066       nthClosestTrack_(iConfig.getParameter<unsigned int>("saveUpToNthClosest")),
0067       distancesPutToken_(produces<edm::ValueMap<std::vector<float>>>()) {}
0068 
0069 //
0070 // member functions
0071 //
0072 
0073 // ------------ method called to produce the data  ------------
0074 void TrackDistanceValueMapProducer::produce(edm::StreamID streamID,
0075                                             edm::Event& iEvent,
0076                                             const edm::EventSetup& iSetup) const {
0077   using namespace edm;
0078 
0079   //=======================================================
0080   // Retrieve the muon Track information
0081   //=======================================================
0082 
0083   const auto& muonTrackCollectionHandle = iEvent.getHandle(muonTracksToken_);
0084   if (!muonTrackCollectionHandle.isValid())
0085     return;
0086   auto const& muonTracks = *muonTrackCollectionHandle;
0087 
0088   //=======================================================
0089   // Retrieve the general Track information
0090   //=======================================================
0091 
0092   const auto& allTrackCollectionHandle = iEvent.getHandle(otherTracksToken_);
0093   if (!allTrackCollectionHandle.isValid())
0094     return;
0095   auto const& allTracks = *allTrackCollectionHandle;
0096 
0097   //=======================================================
0098   // fill the distance vector
0099   //=======================================================
0100 
0101   // the map cannot be filled straight away, so create an intermediate vector
0102   unsigned int Nit = muonTracks.size();
0103   unsigned int Nall = allTracks.size();
0104   std::vector<std::vector<float>> v2_dR2;
0105 
0106   for (unsigned int iit = 0; iit < Nit; iit++) {
0107     const auto& muontrack = muonTracks.ptrAt(iit);
0108 
0109     std::vector<float> v_dR2;
0110     for (unsigned int iAll = 0; iAll < Nall; iAll++) {
0111       const auto& recotrack = allTracks.ptrAt(iAll);
0112       const float dR2 = ::deltaR2(*muontrack, *recotrack);
0113       if (dR2 != 0.f) {  // exclude the track itself
0114         v_dR2.push_back(dR2);
0115       }
0116     }
0117 
0118     // sort the tracks in ascending order of distance
0119     std::sort(v_dR2.begin(), v_dR2.end(), [](const float& lhs, const float& rhs) { return lhs < rhs; });
0120 
0121     // just copy the first nth
0122     std::vector<float> reduced_vdR2;
0123     std::copy(v_dR2.begin(),
0124               v_dR2.begin() + std::min(v_dR2.size(), static_cast<size_t>(nthClosestTrack_)),
0125               std::back_inserter(reduced_vdR2));
0126     v2_dR2.push_back(reduced_vdR2);
0127   }
0128 
0129   //=======================================================
0130   // Populate the event with the value map
0131   //=======================================================
0132 
0133   std::unique_ptr<edm::ValueMap<std::vector<float>>> vm_dR2(new edm::ValueMap<std::vector<float>>());
0134   edm::ValueMap<std::vector<float>>::Filler filler(*vm_dR2);
0135   filler.insert(muonTrackCollectionHandle, v2_dR2.begin(), v2_dR2.end());
0136   filler.fill();
0137   iEvent.put(distancesPutToken_, std::move(vm_dR2));
0138 }
0139 
0140 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0141 void TrackDistanceValueMapProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0142   edm::ParameterSetDescription desc;
0143   desc.setComment("Produces a value map with all the distances with the other tracks in the event");
0144   desc.add<edm::InputTag>("muonTracks", edm::InputTag("ALCARECOSiPixelCalSingleMuonTight"))
0145       ->setComment("the probe muon tracks");
0146   desc.add<edm::InputTag>("allTracks", edm::InputTag("generalTracks"))->setComment("all tracks in the event");
0147   desc.add<unsigned int>("saveUpToNthClosest", 1)->setComment("save the distance only for the nth closest tracks");
0148   descriptions.addWithDefaultLabel(desc);
0149 }
0150 
0151 //define this as a plug-in
0152 DEFINE_FWK_MODULE(TrackDistanceValueMapProducer);