CMS 3D CMS Logo

ClusterTPAssociationProducer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <vector>
3 #include <utility>
4 
13 
23 
31 
33 public:
34  typedef std::vector<OmniClusterRef> OmniClusterCollection;
35 
38 
39  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
40 
41 private:
42  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
43 
44  template <typename T>
45  std::vector<std::pair<uint32_t, EncodedEventId> > getSimTrackId(const edm::Handle<edm::DetSetVector<T> >& simLinks,
46  const DetId& detId,
47  uint32_t channel) const;
48 
57 };
58 
61  consumes<edm::DetSetVector<PixelDigiSimLink> >(cfg.getParameter<edm::InputTag>("pixelSimLinkSrc"))),
63  consumes<edm::DetSetVector<StripDigiSimLink> >(cfg.getParameter<edm::InputTag>("stripSimLinkSrc"))),
65  consumes<edm::DetSetVector<PixelDigiSimLink> >(cfg.getParameter<edm::InputTag>("phase2OTSimLinkSrc"))),
67  consumes<edmNew::DetSetVector<SiPixelCluster> >(cfg.getParameter<edm::InputTag>("pixelClusterSrc"))),
69  consumes<edmNew::DetSetVector<SiStripCluster> >(cfg.getParameter<edm::InputTag>("stripClusterSrc"))),
71  cfg.getParameter<edm::InputTag>("phase2OTClusterSrc"))),
73  consumes<TrackingParticleCollection>(cfg.getParameter<edm::InputTag>("trackingParticleSrc"))),
74  throwOnMissingCollections_(cfg.getParameter<bool>("throwOnMissingCollections")) {
75  produces<ClusterTPAssociation>();
76 }
77 
79 
82  desc.add<edm::InputTag>("simTrackSrc", edm::InputTag("g4SimHits"));
83  desc.add<edm::InputTag>("pixelSimLinkSrc", edm::InputTag("simSiPixelDigis"));
84  desc.add<edm::InputTag>("stripSimLinkSrc", edm::InputTag("simSiStripDigis"));
85  desc.add<edm::InputTag>("phase2OTSimLinkSrc", edm::InputTag("simSiPixelDigis", "Tracker"));
86  desc.add<edm::InputTag>("pixelClusterSrc", edm::InputTag("siPixelClusters"));
87  desc.add<edm::InputTag>("stripClusterSrc", edm::InputTag("siStripClusters"));
88  desc.add<edm::InputTag>("phase2OTClusterSrc", edm::InputTag("siPhase2Clusters"));
89  desc.add<edm::InputTag>("trackingParticleSrc", edm::InputTag("mix", "MergedTrackTruth"));
90  desc.add<bool>("throwOnMissingCollections", true);
91  descriptions.add("tpClusterProducerDefault", desc);
92 }
93 
95  // Pixel Cluster
97  bool foundPixelClusters = iEvent.getByToken(pixelClustersToken_, pixelClusters);
98 
99  // Strip Cluster
101  bool foundStripClusters = iEvent.getByToken(stripClustersToken_, stripClusters);
102 
103  // Phase2 Cluster
105  bool foundPhase2OTClusters = iEvent.getByToken(phase2OTClustersToken_, phase2OTClusters);
106 
107  // Pixel DigiSimLink
109  // iEvent.getByLabel(_pixelSimLinkSrc, sipixelSimLinks);
110  auto pixelSimLinksFound = iEvent.getByToken(sipixelSimLinksToken_, sipixelSimLinks);
111  if (not throwOnMissingCollections_ and foundPixelClusters and not pixelSimLinksFound) {
112  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
113  iEvent.put(std::move(clusterTPList));
114  return;
115  }
116 
117  // SiStrip DigiSimLink
119  auto stripSimLinksFound = iEvent.getByToken(sistripSimLinksToken_, sistripSimLinks);
120  if (not throwOnMissingCollections_ and foundStripClusters and not stripSimLinksFound) {
121  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
122  iEvent.put(std::move(clusterTPList));
123  return;
124  }
125 
126  // Phase2 OT DigiSimLink
128  auto phase2OTSimLinksFound = iEvent.getByToken(siphase2OTSimLinksToken_, siphase2OTSimLinks);
129  if (not throwOnMissingCollections_ and foundPhase2OTClusters and not phase2OTSimLinksFound) {
130  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
131  iEvent.put(std::move(clusterTPList));
132  return;
133  }
134 
135  // TrackingParticle
137  auto tpFound = iEvent.getByToken(trackingParticleToken_, TPCollectionH);
138  if (not throwOnMissingCollections_ and not tpFound) {
139  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
140  iEvent.put(std::move(clusterTPList));
141  return;
142  }
143 
144  auto clusterTPList = std::make_unique<ClusterTPAssociation>(TPCollectionH);
145 
146  // prepare temporary map between SimTrackId and TrackingParticle index
147  std::map<std::pair<size_t, EncodedEventId>, TrackingParticleRef> mapping;
148  for (TrackingParticleCollection::size_type itp = 0; itp < TPCollectionH.product()->size(); ++itp) {
149  TrackingParticleRef trackingParticle(TPCollectionH, itp);
150 
151  // SimTracks inside TrackingParticle
152  EncodedEventId eid(trackingParticle->eventId());
153  //size_t index = 0;
154  for (std::vector<SimTrack>::const_iterator itrk = trackingParticle->g4Track_begin();
155  itrk != trackingParticle->g4Track_end();
156  ++itrk) {
157  std::pair<uint32_t, EncodedEventId> trkid(itrk->trackId(), eid);
158  //std::cout << "creating map for id: " << trkid.first << " with tp: " << trackingParticle.key() << std::endl;
159  mapping.insert(std::make_pair(trkid, trackingParticle));
160  }
161  }
162 
163  if (foundPixelClusters) {
164  // Pixel Clusters
165  clusterTPList->addKeyID(pixelClusters.id());
166  for (edmNew::DetSetVector<SiPixelCluster>::const_iterator iter = pixelClusters->begin();
167  iter != pixelClusters->end();
168  ++iter) {
169  uint32_t detid = iter->id();
170  DetId detId(detid);
171  edmNew::DetSet<SiPixelCluster> link_pixel = (*iter);
172  for (edmNew::DetSet<SiPixelCluster>::const_iterator di = link_pixel.begin(); di != link_pixel.end(); ++di) {
173  const SiPixelCluster& cluster = (*di);
175 
176  std::set<std::pair<uint32_t, EncodedEventId> > simTkIds;
177  for (int irow = cluster.minPixelRow(); irow <= cluster.maxPixelRow(); ++irow) {
178  for (int icol = cluster.minPixelCol(); icol <= cluster.maxPixelCol(); ++icol) {
179  uint32_t channel = PixelChannelIdentifier::pixelToChannel(irow, icol);
180  std::vector<std::pair<uint32_t, EncodedEventId> > trkid(
181  getSimTrackId<PixelDigiSimLink>(sipixelSimLinks, detId, channel));
182  if (trkid.empty())
183  continue;
184  simTkIds.insert(trkid.begin(), trkid.end());
185  }
186  }
187  for (std::set<std::pair<uint32_t, EncodedEventId> >::const_iterator iset = simTkIds.begin();
188  iset != simTkIds.end();
189  iset++) {
190  auto ipos = mapping.find(*iset);
191  if (ipos != mapping.end()) {
192  //std::cout << "cluster in detid: " << detid << " from tp: " << ipos->second.key() << " " << iset->first << std::endl;
193  clusterTPList->emplace_back(OmniClusterRef(c_ref), ipos->second);
194  }
195  }
196  }
197  }
198  }
199 
200  if (foundStripClusters) {
201  // Strip Clusters
202  clusterTPList->addKeyID(stripClusters.id());
203  for (edmNew::DetSetVector<SiStripCluster>::const_iterator iter = stripClusters->begin(false),
204  eter = stripClusters->end(false);
205  iter != eter;
206  ++iter) {
207  if (!(*iter).isValid())
208  continue;
209  uint32_t detid = iter->id();
210  DetId detId(detid);
211  edmNew::DetSet<SiStripCluster> link_strip = (*iter);
212  for (edmNew::DetSet<SiStripCluster>::const_iterator di = link_strip.begin(); di != link_strip.end(); di++) {
213  const SiStripCluster& cluster = (*di);
215 
216  std::set<std::pair<uint32_t, EncodedEventId> > simTkIds;
217  int first = cluster.firstStrip();
218  int last = first + cluster.amplitudes().size();
219 
220  for (int istr = first; istr < last; ++istr) {
221  std::vector<std::pair<uint32_t, EncodedEventId> > trkid(
222  getSimTrackId<StripDigiSimLink>(sistripSimLinks, detId, istr));
223  if (trkid.empty())
224  continue;
225  simTkIds.insert(trkid.begin(), trkid.end());
226  }
227  for (std::set<std::pair<uint32_t, EncodedEventId> >::const_iterator iset = simTkIds.begin();
228  iset != simTkIds.end();
229  iset++) {
230  auto ipos = mapping.find(*iset);
231  if (ipos != mapping.end()) {
232  //std::cout << "cluster in detid: " << detid << " from tp: " << ipos->second.key() << " " << iset->first << std::endl;
233  clusterTPList->emplace_back(OmniClusterRef(c_ref), ipos->second);
234  }
235  }
236  }
237  }
238  }
239 
240  if (foundPhase2OTClusters) {
241  // Phase2 Clusters
242  clusterTPList->addKeyID(phase2OTClusters.id());
243  if (phase2OTClusters.isValid()) {
244  for (edmNew::DetSetVector<Phase2TrackerCluster1D>::const_iterator iter = phase2OTClusters->begin(false),
245  eter = phase2OTClusters->end(false);
246  iter != eter;
247  ++iter) {
248  if (!(*iter).isValid())
249  continue;
250  uint32_t detid = iter->id();
251  DetId detId(detid);
252  edmNew::DetSet<Phase2TrackerCluster1D> link_phase2 = (*iter);
253  for (edmNew::DetSet<Phase2TrackerCluster1D>::const_iterator di = link_phase2.begin(); di != link_phase2.end();
254  di++) {
255  const Phase2TrackerCluster1D& cluster = (*di);
257  edmNew::makeRefTo(phase2OTClusters, di);
258 
259  std::set<std::pair<uint32_t, EncodedEventId> > simTkIds;
260 
261  for (unsigned int istr(0); istr < cluster.size(); ++istr) {
262  uint32_t channel = Phase2TrackerDigi::pixelToChannel(cluster.firstRow() + istr, cluster.column());
263  std::vector<std::pair<uint32_t, EncodedEventId> > trkid(
264  getSimTrackId<PixelDigiSimLink>(siphase2OTSimLinks, detId, channel));
265  if (trkid.empty())
266  continue;
267  simTkIds.insert(trkid.begin(), trkid.end());
268  }
269 
270  for (std::set<std::pair<uint32_t, EncodedEventId> >::const_iterator iset = simTkIds.begin();
271  iset != simTkIds.end();
272  iset++) {
273  auto ipos = mapping.find(*iset);
274  if (ipos != mapping.end()) {
275  clusterTPList->emplace_back(OmniClusterRef(c_ref), ipos->second);
276  }
277  }
278  }
279  }
280  }
281  }
282  clusterTPList->sortAndUnique();
283  iEvent.put(std::move(clusterTPList));
284 }
285 
286 template <typename T>
287 std::vector<std::pair<uint32_t, EncodedEventId> >
288 //std::pair<uint32_t, EncodedEventId>
290  const DetId& detId,
291  uint32_t channel) const {
292  //std::pair<uint32_t, EncodedEventId> simTrkId;
293  std::vector<std::pair<uint32_t, EncodedEventId> > simTrkId;
294  auto isearch = simLinks->find(detId);
295  if (isearch != simLinks->end()) {
296  // Loop over DigiSimLink in this det unit
297  edm::DetSet<T> link_detset = (*isearch);
298  for (typename edm::DetSet<T>::const_iterator it = link_detset.data.begin(); it != link_detset.data.end(); ++it) {
299  if (channel == it->channel()) {
300  simTrkId.push_back(std::make_pair(it->SimTrackId(), it->eventId()));
301  }
302  }
303  }
304  return simTrkId;
305 }
308 
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
edm::Ref< typename HandleT::element_type, typename HandleT::element_type::value_type::value_type > makeRefTo(const HandleT &iHandle, typename HandleT::element_type::value_type::const_iterator itIter)
int minPixelCol() const
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
void push_back(const T &t)
Definition: DetSet.h:68
std::vector< TrackingParticle > TrackingParticleCollection
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< edmNew::DetSetVector< Phase2TrackerCluster1D > > phase2OTClustersToken_
uint16_t firstStrip() const
data_type const * const_iterator
Definition: DetSetNew.h:30
uint16_t size_type
edm::EDGetTokenT< edm::DetSetVector< PixelDigiSimLink > > siphase2OTSimLinksToken_
edm::EDGetTokenT< edmNew::DetSetVector< SiPixelCluster > > pixelClustersToken_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
edm::EDGetTokenT< edmNew::DetSetVector< SiStripCluster > > stripClustersToken_
int maxPixelRow() const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
unsigned int firstRow() const
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int minPixelRow() const
ClusterTPAssociationProducer(const edm::ParameterSet &)
std::vector< OmniClusterRef > OmniClusterCollection
unsigned int column() const
static PackedDigiType pixelToChannel(unsigned int row, unsigned int col)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< TrackingParticleCollection > trackingParticleToken_
Definition: DetId.h:18
T const * product() const
Definition: Handle.h:74
edm::EDGetTokenT< edm::DetSetVector< PixelDigiSimLink > > sipixelSimLinksToken_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
int maxPixelCol() const
collection_type data
Definition: DetSet.h:80
Pixel cluster – collection of neighboring pixels above threshold.
HLT enums.
iterator end()
Definition: DetSetNew.h:70
static int pixelToChannel(int row, int col)
std::vector< std::pair< uint32_t, EncodedEventId > > getSimTrackId(const edm::Handle< edm::DetSetVector< T > > &simLinks, const DetId &detId, uint32_t channel) const
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
edm::EDGetTokenT< edm::DetSetVector< StripDigiSimLink > > sistripSimLinksToken_
const std::vector< uint8_t > & amplitudes() const
def move(src, dest)
Definition: eostools.py:511
iterator begin()
Definition: DetSetNew.h:67