CMS 3D CMS Logo

MuonReducedTrackExtraProducer.cc
Go to the documentation of this file.
5 
7  : muonToken_(consumes<edm::View<reco::Muon>>(pset.getParameter<edm::InputTag>("muonTag"))),
8  outputClusters_(pset.getParameter<bool>("outputClusters")),
9  selector_(pset.getParameter<std::string>("cut")),
10  trackExtraOutToken_(produces<reco::TrackExtraCollection>()),
11  trackingRecHitsOutToken_(produces<TrackingRecHitCollection>()),
12  associationOutToken_(produces<edm::Association<reco::TrackExtraCollection>>()) {
13  std::vector<edm::InputTag> trackExtraTags = pset.getParameter<std::vector<edm::InputTag>>("trackExtraTags");
14  for (edm::InputTag const& tag : trackExtraTags) {
15  trackExtraTokens_.push_back(consumes<reco::TrackExtraCollection>(tag));
16  }
17 
18  std::vector<edm::InputTag> trackExtraAssocTags = pset.getParameter<std::vector<edm::InputTag>>("trackExtraAssocs");
19  for (edm::InputTag const& tag : trackExtraAssocTags) {
21  }
22 
23  if (outputClusters_) {
25  consumes<edmNew::DetSetVector<SiPixelCluster>>(pset.getParameter<edm::InputTag>("pixelClusterTag"));
27  consumes<edmNew::DetSetVector<SiStripCluster>>(pset.getParameter<edm::InputTag>("stripClusterTag"));
28 
29  pixelClusterOutToken_ = produces<edmNew::DetSetVector<SiPixelCluster>>();
30  stripClusterOutToken_ = produces<edmNew::DetSetVector<SiStripCluster>>();
31  }
32 }
33 
36  desc.setComment(
37  "Produces reduced set of TrackExtras and corresponding TrackingRecHits and (optionally) Pixe/Strip clusters "
38  "associated to a muon track.");
39  desc.add<edm::InputTag>("muonTag", edm::InputTag("muons"));
40  desc.add<std::vector<edm::InputTag>>("trackExtraTags",
41  {edm::InputTag("generalTracks"),
42  edm::InputTag("globalMuons"),
43  edm::InputTag("tevMuons", "firstHit"),
44  edm::InputTag("tevMuons", "picky"),
45  edm::InputTag("tevMuons", "dyt")});
46  desc.add<std::vector<edm::InputTag>>("trackExtraAssocs", {});
47  desc.add<edm::InputTag>("pixelClusterTag", edm::InputTag("siPixelClusters"));
48  desc.add<edm::InputTag>("stripClusterTag", edm::InputTag("siStripClusters"));
49  desc.add<std::string>("cut", "pt > 3.0");
50  desc.add<bool>("outputClusters", true);
51  descriptions.add("muonReducedTrackExtras", desc);
52 }
53 
55  auto muons = event.getHandle(muonToken_);
56 
57  std::vector<edm::Handle<reco::TrackExtraCollection>> trackExtrasV(trackExtraTokens_.size());
58  for (unsigned int i = 0; i < trackExtraTokens_.size(); ++i) {
59  event.getByToken(trackExtraTokens_[i], trackExtrasV[i]);
60  }
61 
62  std::vector<edm::Handle<edm::Association<reco::TrackExtraCollection>>> trackExtraAssocs(trackExtraAssocs_.size());
63  for (unsigned int i = 0; i < trackExtraAssocs_.size(); ++i) {
64  event.getByToken(trackExtraAssocs_[i], trackExtraAssocs[i]);
65  }
66 
67  std::map<edm::ProductID, std::vector<bool>> idxstokeep;
68  for (auto const& trackExtras : trackExtrasV) {
69  idxstokeep[trackExtras.id()].resize(trackExtras->size(), false);
70  }
71 
72  //loop over muons and mark track extras to keep
73  for (auto const& muon : *muons) {
74  if (!selector_(muon)) {
75  continue;
76  }
77  reco::TrackExtraRef trackExtra = muon.bestTrack()->extra();
78  // check recursively through association maps if present
79  for (auto const& assoc : trackExtraAssocs) {
80  if (!assoc->contains(trackExtra.id())) {
81  continue;
82  }
83  reco::TrackExtraRef const& trackExtraOut = (*assoc)[trackExtra];
84  if (trackExtraOut.isNonnull()) {
85  trackExtra = trackExtraOut;
86  }
87  }
88  auto idxs = idxstokeep.find(trackExtra.id());
89  if (idxs != idxstokeep.end()) {
90  idxs->second[trackExtra.key()] = true;
91  }
92  }
93 
94  //output collections for TrackExtras and TrackingRecHits and the association map
95  reco::TrackExtraCollection trackExtrasOut;
96  TrackingRecHitCollection trackingRecHitsOut;
99 
100  //refprod for the output TrackExtraCollection
101  edm::RefProd<reco::TrackExtraCollection> trackExtraRefProd = event.getRefBeforePut(trackExtraOutToken_);
102  //refprod for the output TrackingRecHitCollection
103  edm::RefProd<TrackingRecHitCollection> hitRefProd = event.getRefBeforePut(trackingRecHitsOutToken_);
104 
105  association.setRef(trackExtraRefProd);
106 
109 
110  //indexes of pixel/strip clusters to keep
111  std::vector<bool> pixelClustersToKeep;
112  std::vector<bool> stripClustersToKeep;
113 
114  if (outputClusters_) {
115  event.getByToken(pixelClusterToken_, pixelClusters);
116  event.getByToken(stripClusterToken_, stripClusters);
117 
118  pixelClustersToKeep.resize(pixelClusters->dataSize(), false);
119  stripClustersToKeep.resize(stripClusters->dataSize(), false);
120  }
121 
124 
125  //loop over track extras and fill output together with TrackingRechits
126  //as well as marking pixel and strip clusters to keep
127  for (auto const& trackExtras : trackExtrasV) {
128  const std::vector<bool>& idxs = idxstokeep.at(trackExtras.id());
129  //indices for association (-1 for null association)
130  std::vector<int> associdxs(trackExtras->size(), -1);
131  for (unsigned int i = 0; i < trackExtras->size(); ++i) {
132  if (!idxs[i]) {
133  continue;
134  }
135  const reco::TrackExtra& trackExtra = (*trackExtras)[i];
136 
137  //fill association idx
138  associdxs[i] = trackExtrasOut.size();
139 
140  //fill TrackExtra
141  trackExtrasOut.emplace_back(trackExtra.outerPosition(),
142  trackExtra.outerMomentum(),
143  trackExtra.outerOk(),
144  trackExtra.innerPosition(),
145  trackExtra.innerMomentum(),
146  trackExtra.innerOk(),
147  trackExtra.outerStateCovariance(),
148  trackExtra.outerDetId(),
149  trackExtra.innerStateCovariance(),
150  trackExtra.innerDetId(),
151  trackExtra.seedDirection(),
152  trackExtra.seedRef());
153 
154  //rekey refs to TrackingRecHits
155  reco::TrackExtra& trackExtraOut = trackExtrasOut.back();
156  trackExtraOut.setHits(hitRefProd, trackingRecHitsOut.size(), trackExtra.recHitsSize());
157  for (auto const& hit : trackExtra.recHits()) {
158  if (outputClusters_) {
159  //mark clusters to keep
160  TrackerSingleRecHit const* singleHit = dynamic_cast<TrackerSingleRecHit const*>(&*hit);
161  if (singleHit != nullptr) {
162  SiPixelClusterRef const& pixelRef = singleHit->cluster_pixel();
163  if (pixelRef.isNonnull() && pixelRef.id() == pixelClusters.id()) {
164  pixelClustersToKeep[pixelRef.key()] = true;
165  }
166  SiStripClusterRef const& stripRef = singleHit->cluster_strip();
167  if (stripRef.isNonnull() && stripRef.id() == stripClusters.id()) {
168  stripClustersToKeep[stripRef.key()] = true;
169  }
170  SiStripMatchedRecHit2D const* matched2DHit = dynamic_cast<SiStripMatchedRecHit2D const*>(singleHit);
171  if (matched2DHit != nullptr) {
172  SiStripClusterRef const& monoRef = matched2DHit->monoClusterRef().cluster_strip();
173  SiStripClusterRef const& stereoRef = matched2DHit->stereoClusterRef().cluster_strip();
174  if (monoRef.isNonnull() && monoRef.id() == stripClusters.id()) {
175  stripClustersToKeep[monoRef.key()] = true;
176  }
177  if (stereoRef.isNonnull() && stereoRef.id() == stripClusters.id()) {
178  stripClustersToKeep[stereoRef.key()] = true;
179  }
180  }
181  }
182  }
183  //fill output hit
184  trackingRecHitsOut.push_back(hit->clone());
185  }
186  }
187  assocfiller.insert(trackExtras, associdxs.begin(), associdxs.end());
188  }
189 
190  assocfiller.fill();
191 
192  if (outputClusters_) {
193  //output collections for clusters
194  edmNew::DetSetVector<SiPixelCluster> pixelClustersOut;
195  edmNew::DetSetVector<SiStripCluster> stripClustersOut;
196  //mapping of indices from input to output collections
197  std::unordered_map<unsigned int, unsigned int> pixelClusterIdxMap;
198  std::unordered_map<unsigned int, unsigned int> stripClusterIdxMap;
199 
200  //fill output clusters
201  //this indexes the internal data array of the DetSetVector
202  unsigned int iIndex = 0;
203  //loop over edmNew:::DetSet<T>
204  for (auto setIter = pixelClusters->begin(), setIterEnd = pixelClusters->end(); setIter != setIterEnd; ++setIter) {
205  //fill items from this DetSet
206  typename edmNew::DetSetVector<SiPixelCluster>::FastFiller ff(pixelClustersOut, setIter->detId());
207  for (auto iter = setIter->begin(), iterEnd = setIter->end(); iter != iterEnd; ++iter, ++iIndex) {
208  if (pixelClustersToKeep[iIndex]) {
209  ff.push_back(*iter);
210  const unsigned int oIndex = pixelClusterIdxMap.size();
211  pixelClusterIdxMap[iIndex] = oIndex;
212  }
213  }
214  }
215 
216  iIndex = 0;
217  for (auto setIter = stripClusters->begin(), setIterEnd = stripClusters->end(); setIter != setIterEnd; ++setIter) {
218  //fill items from this DetSet
219  typename edmNew::DetSetVector<SiStripCluster>::FastFiller ff(stripClustersOut, setIter->detId());
220  for (auto iter = setIter->begin(), iterEnd = setIter->end(); iter != iterEnd; ++iter, ++iIndex) {
221  if (stripClustersToKeep[iIndex]) {
222  ff.push_back(*iter);
223  const unsigned int oIndex = stripClusterIdxMap.size();
224  stripClusterIdxMap[iIndex] = oIndex;
225  }
226  }
227  }
228 
230  event.emplace(pixelClusterOutToken_, std::move(pixelClustersOut));
232  event.emplace(stripClusterOutToken_, std::move(stripClustersOut));
233 
234  //rekey cluster references in output hit collection
235  for (auto& hit : trackingRecHitsOut) {
236  TrackerSingleRecHit* singleHit = dynamic_cast<TrackerSingleRecHit*>(&hit);
237  if (singleHit != nullptr) {
238  SiPixelClusterRef const& pixelRef = singleHit->cluster_pixel();
239  if (pixelRef.isNonnull() && pixelRef.id() == pixelClusters.id()) {
240  SiPixelClusterRef const pixelRefOut(pixelClustersOutH, pixelClusterIdxMap.at(pixelRef.key()));
241  singleHit->setClusterPixelRef(pixelRefOut);
242  }
243  SiStripClusterRef const& stripRef = singleHit->cluster_strip();
244  if (stripRef.isNonnull() && stripRef.id() == stripClusters.id()) {
245  SiStripClusterRef const stripRefOut(stripClustersOutH, stripClusterIdxMap.at(stripRef.key()));
246  singleHit->setClusterStripRef(stripRefOut);
247  }
248  SiStripMatchedRecHit2D* matched2DHit = dynamic_cast<SiStripMatchedRecHit2D*>(singleHit);
249  if (matched2DHit != nullptr) {
250  SiStripClusterRef const& monoRef = matched2DHit->monoClusterRef().cluster_strip();
251  SiStripClusterRef const& stereoRef = matched2DHit->stereoClusterRef().cluster_strip();
252  if (monoRef.isNonnull() && monoRef.id() == stripClusters.id()) {
253  SiStripClusterRef const monoRefOut(stripClustersOutH, stripClusterIdxMap.at(monoRef.key()));
254  matched2DHit->monoClusterRef() = OmniClusterRef(monoRefOut);
255  }
256  if (stereoRef.isNonnull() && stereoRef.id() == stripClusters.id()) {
257  SiStripClusterRef const stereoRefOut(stripClustersOutH, stripClusterIdxMap.at(stereoRef.key()));
258  matched2DHit->stereoClusterRef() = OmniClusterRef(stereoRefOut);
259  }
260  }
261  }
262  }
263  }
264 
265  event.emplace(trackExtraOutToken_, std::move(trackExtrasOut));
266  event.emplace(trackingRecHitsOutToken_, std::move(trackingRecHitsOut));
267  event.emplace(associationOutToken_, std::move(association));
268 }
bool outerOk() const
returns true if the outermost hit is valid
Definition: TrackExtra.h:77
void setClusterStripRef(ClusterStripRef const &ref)
ProductID id() const
Accessor for product ID.
Definition: Ref.h:244
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: TrackExtra.cc:41
const edm::EDPutTokenT< reco::TrackExtraCollection > trackExtraOutToken_
const StringCutObjectSelector< reco::Muon > selector_
std::vector< edm::EDGetTokenT< reco::TrackExtraCollection > > trackExtraTokens_
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
unsigned int recHitsSize() const
number of RecHits
MuonReducedTrackExtraProducer(const edm::ParameterSet &)
const edm::EDPutTokenT< edm::Association< reco::TrackExtraCollection > > associationOutToken_
const PropagationDirection & seedDirection() const
Definition: TrackExtra.h:122
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
OmniClusterRef const & stereoClusterRef() const
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const Vector & innerMomentum() const
momentum vector at innermost hit position
Definition: TrackExtra.h:81
edm::EDPutTokenT< edmNew::DetSetVector< SiPixelCluster > > pixelClusterOutToken_
OmniClusterRef const & monoClusterRef() const
key_type key() const
Accessor for product key.
Definition: Ref.h:250
void produce(edm::Event &, const edm::EventSetup &) override
const edm::RefToBase< TrajectorySeed > & seedRef() const
Definition: TrackExtra.h:129
edm::EDGetTokenT< edmNew::DetSetVector< SiPixelCluster > > pixelClusterToken_
void push_back(D *&d)
Definition: OwnVector.h:326
std::vector< edm::EDGetTokenT< edm::Association< reco::TrackExtraCollection > > > trackExtraAssocs_
auto recHits() const
accessor to RecHits
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
Definition: Muon.py:1
const Point & outerPosition() const
outermost hit position
Definition: TrackExtra.h:73
const edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
size_type size() const
Definition: OwnVector.h:300
const Point & innerPosition() const
innermost hit position
Definition: TrackExtra.h:79
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: TrackExtra.h:118
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: TrackExtra.h:120
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:10
ClusterPixelRef cluster_pixel() const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
ClusterStripRef cluster_strip() const
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: TrackExtra.cc:47
edm::EDPutTokenT< edmNew::DetSetVector< SiStripCluster > > stripClusterOutToken_
Pixel cluster – collection of neighboring pixels above threshold.
fixed size matrix
HLT enums.
const Vector & outerMomentum() const
momentum vector at outermost hit position
Definition: TrackExtra.h:75
const edm::EDPutTokenT< TrackingRecHitCollection > trackingRecHitsOutToken_
ClusterStripRef cluster_strip() const
edm::EDGetTokenT< edmNew::DetSetVector< SiStripCluster > > stripClusterToken_
void setClusterPixelRef(ClusterPixelRef const &ref)
def move(src, dest)
Definition: eostools.py:511
bool innerOk() const
returns true if the innermost hit is valid
Definition: TrackExtra.h:83
Definition: event.py:1