File indexing completed on 2024-04-06 11:59:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <memory>
0019
0020
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
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
0049
0050 edm::EDGetTokenT<edm::View<reco::Track>> muonTracksToken_;
0051 edm::EDGetTokenT<edm::View<reco::Track>> otherTracksToken_;
0052
0053
0054 unsigned int nthClosestTrack_;
0055
0056
0057 edm::EDPutTokenT<edm::ValueMap<std::vector<float>>> distancesPutToken_;
0058 };
0059
0060
0061
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
0071
0072
0073
0074 void TrackDistanceValueMapProducer::produce(edm::StreamID streamID,
0075 edm::Event& iEvent,
0076 const edm::EventSetup& iSetup) const {
0077 using namespace edm;
0078
0079
0080
0081
0082
0083 const auto& muonTrackCollectionHandle = iEvent.getHandle(muonTracksToken_);
0084 if (!muonTrackCollectionHandle.isValid())
0085 return;
0086 auto const& muonTracks = *muonTrackCollectionHandle;
0087
0088
0089
0090
0091
0092 const auto& allTrackCollectionHandle = iEvent.getHandle(otherTracksToken_);
0093 if (!allTrackCollectionHandle.isValid())
0094 return;
0095 auto const& allTracks = *allTrackCollectionHandle;
0096
0097
0098
0099
0100
0101
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) {
0114 v_dR2.push_back(dR2);
0115 }
0116 }
0117
0118
0119 std::sort(v_dR2.begin(), v_dR2.end(), [](const float& lhs, const float& rhs) { return lhs < rhs; });
0120
0121
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
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
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
0152 DEFINE_FWK_MODULE(TrackDistanceValueMapProducer);