CMS 3D CMS Logo

TrackDistanceValueMapProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Calibration/TkAlCaRecoProducers
4 // Class: TrackDistanceValueMapProducer
5 //
11 //
12 // Original Author: Marco Musich
13 // Created: Mon, 12 Apr 2021 11:59:39 GMT
14 //
15 //
16 
17 // system include files
18 #include <memory>
19 
20 // user include files
23 
26 
29 
32 
34 //
35 // class declaration
36 //
37 
39 public:
41  ~TrackDistanceValueMapProducer() override = default;
42 
43  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
44 
45 private:
46  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
47 
48  // ----------member data ---------------------------
49  // edToken
52 
53  // save in the event only up to the n-th closest
54  unsigned int nthClosestTrack_;
55 
56  // putToken
58 };
59 
60 //
61 // constructors and destructor
62 //
64  : muonTracksToken_(consumes<edm::View<reco::Track>>(iConfig.getParameter<edm::InputTag>("muonTracks"))),
65  otherTracksToken_(consumes<edm::View<reco::Track>>(iConfig.getParameter<edm::InputTag>("allTracks"))),
66  nthClosestTrack_(iConfig.getParameter<unsigned int>("saveUpToNthClosest")),
67  distancesPutToken_(produces<edm::ValueMap<std::vector<float>>>()) {}
68 
69 //
70 // member functions
71 //
72 
73 // ------------ method called to produce the data ------------
76  const edm::EventSetup& iSetup) const {
77  using namespace edm;
78 
79  //=======================================================
80  // Retrieve the muon Track information
81  //=======================================================
82 
83  const auto& muonTrackCollectionHandle = iEvent.getHandle(muonTracksToken_);
84  if (!muonTrackCollectionHandle.isValid())
85  return;
86  auto const& muonTracks = *muonTrackCollectionHandle;
87 
88  //=======================================================
89  // Retrieve the general Track information
90  //=======================================================
91 
92  const auto& allTrackCollectionHandle = iEvent.getHandle(otherTracksToken_);
93  if (!allTrackCollectionHandle.isValid())
94  return;
95  auto const& allTracks = *allTrackCollectionHandle;
96 
97  //=======================================================
98  // fill the distance vector
99  //=======================================================
100 
101  // the map cannot be filled straight away, so create an intermediate vector
102  unsigned int Nit = muonTracks.size();
103  unsigned int Nall = allTracks.size();
104  std::vector<std::vector<float>> v2_dR2;
105 
106  for (unsigned int iit = 0; iit < Nit; iit++) {
107  const auto& muontrack = muonTracks.ptrAt(iit);
108 
109  std::vector<float> v_dR2;
110  for (unsigned int iAll = 0; iAll < Nall; iAll++) {
111  const auto& recotrack = allTracks.ptrAt(iAll);
112  const float dR2 = ::deltaR2(*muontrack, *recotrack);
113  if (dR2 != 0.f) { // exclude the track itself
114  v_dR2.push_back(dR2);
115  }
116  }
117 
118  // sort the tracks in ascending order of distance
119  std::sort(v_dR2.begin(), v_dR2.end(), [](const float& lhs, const float& rhs) { return lhs < rhs; });
120 
121  // just copy the first nth
122  std::vector<float> reduced_vdR2;
123  std::copy(v_dR2.begin(),
124  v_dR2.begin() + std::min(v_dR2.size(), static_cast<size_t>(nthClosestTrack_)),
125  std::back_inserter(reduced_vdR2));
126  v2_dR2.push_back(reduced_vdR2);
127  }
128 
129  //=======================================================
130  // Populate the event with the value map
131  //=======================================================
132 
133  std::unique_ptr<edm::ValueMap<std::vector<float>>> vm_dR2(new edm::ValueMap<std::vector<float>>());
134  edm::ValueMap<std::vector<float>>::Filler filler(*vm_dR2);
135  filler.insert(muonTrackCollectionHandle, v2_dR2.begin(), v2_dR2.end());
136  filler.fill();
137  iEvent.put(distancesPutToken_, std::move(vm_dR2));
138 }
139 
140 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
143  desc.setComment("Produces a value map with all the distances with the other tracks in the event");
144  desc.add<edm::InputTag>("muonTracks", edm::InputTag("ALCARECOSiPixelCalSingleMuonTight"))
145  ->setComment("the probe muon tracks");
146  desc.add<edm::InputTag>("allTracks", edm::InputTag("generalTracks"))->setComment("all tracks in the event");
147  desc.add<unsigned int>("saveUpToNthClosest", 1)->setComment("save the distance only for the nth closest tracks");
148  descriptions.addWithDefaultLabel(desc);
149 }
150 
151 //define this as a plug-in
edm::EDGetTokenT< edm::View< reco::Track > > otherTracksToken_
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TrackDistanceValueMapProducer(const edm::ParameterSet &)
~TrackDistanceValueMapProducer() override=default
int iEvent
Definition: GenABIO.cc:224
double f[11][100]
edm::EDGetTokenT< edm::View< reco::Track > > muonTracksToken_
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
fixed size matrix
HLT enums.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDPutTokenT< edm::ValueMap< std::vector< float > > > distancesPutToken_
def move(src, dest)
Definition: eostools.py:511