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::StreamID
Definition: StreamID.h:30
TrackDistanceValueMapProducer::otherTracksToken_
edm::EDGetTokenT< edm::View< reco::Track > > otherTracksToken_
Definition: TrackDistanceValueMapProducer.cc:51
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
sistrip::View
View
Definition: ConstantsForView.h:26
min
T min(T a, T b)
Definition: MathUtil.h:58
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::EDPutTokenT
Definition: EDPutToken.h:33
TrackDistanceValueMapProducer::~TrackDistanceValueMapProducer
~TrackDistanceValueMapProducer() override=default
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89301
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
muonTagProbeFilters_cff.allTracks
allTracks
Definition: muonTagProbeFilters_cff.py:22
TrackDistanceValueMapProducer
Definition: TrackDistanceValueMapProducer.cc:38
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:46
TrackDistanceValueMapProducer::TrackDistanceValueMapProducer
TrackDistanceValueMapProducer(const edm::ParameterSet &)
Definition: TrackDistanceValueMapProducer.cc:63
MakerMacros.h
Track.h
TrackFwd.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::global::EDProducer
Definition: EDProducer.h:32
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
TrackDistanceValueMapProducer::muonTracksToken_
edm::EDGetTokenT< edm::View< reco::Track > > muonTracksToken_
Definition: TrackDistanceValueMapProducer.cc:50
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
TrackDistanceValueMapProducer::produce
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: TrackDistanceValueMapProducer.cc:74
edm::EventSetup
Definition: EventSetup.h:58
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
HLTMuonOfflineAnalyzer_cfi.deltaR2
deltaR2
Definition: HLTMuonOfflineAnalyzer_cfi.py:105
Frameworkfwd.h
edm::ValueMap
Definition: ValueMap.h:107
TrackDistanceValueMapProducer::distancesPutToken_
edm::EDPutTokenT< edm::ValueMap< std::vector< float > > > distancesPutToken_
Definition: TrackDistanceValueMapProducer.cc:57
TrackDistanceValueMapProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: TrackDistanceValueMapProducer.cc:141
ParameterSet.h
EDProducer.h
edm::Event
Definition: Event.h:73
StreamID.h
edm::InputTag
Definition: InputTag.h:15
edm::ConfigurationDescriptions::addWithDefaultLabel
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:87
TrackDistanceValueMapProducer::nthClosestTrack_
unsigned int nthClosestTrack_
Definition: TrackDistanceValueMapProducer.cc:54