CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ClusterTPAssociationProducer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <vector>
3 #include <utility>
4 
13 
23 
32 
33 namespace {
34 
35  template <typename T>
36  void getSimTrackId(std::vector<UniqueSimTrackId>& simTrkId,
37  const edm::Handle<edm::DetSetVector<T> >& simLinks,
38  const DetId& detId,
39  uint32_t channel) {
40  auto isearch = simLinks->find(detId);
41  if (isearch != simLinks->end()) {
42  // Loop over DigiSimLink in this det unit
43  edm::DetSet<T> link_detset = (*isearch);
44  for (typename edm::DetSet<T>::const_iterator it = link_detset.data.begin(); it != link_detset.data.end(); ++it) {
45  if (channel == it->channel()) {
46  simTrkId.emplace_back(it->SimTrackId(), it->eventId());
47  }
48  }
49  }
50  }
51 
52 } // namespace
53 
55 public:
56  using OmniClusterCollection = std::vector<OmniClusterRef>;
57 
59  ~ClusterTPAssociationProducer() override = default;
60 
61  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
62 
63 private:
64  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
65 
74 };
75 
77  : sipixelSimLinksToken_(
78  consumes<edm::DetSetVector<PixelDigiSimLink> >(cfg.getParameter<edm::InputTag>("pixelSimLinkSrc"))),
79  sistripSimLinksToken_(
80  consumes<edm::DetSetVector<StripDigiSimLink> >(cfg.getParameter<edm::InputTag>("stripSimLinkSrc"))),
81  siphase2OTSimLinksToken_(
82  consumes<edm::DetSetVector<PixelDigiSimLink> >(cfg.getParameter<edm::InputTag>("phase2OTSimLinkSrc"))),
83  pixelClustersToken_(
84  consumes<edmNew::DetSetVector<SiPixelCluster> >(cfg.getParameter<edm::InputTag>("pixelClusterSrc"))),
85  stripClustersToken_(
86  consumes<edmNew::DetSetVector<SiStripCluster> >(cfg.getParameter<edm::InputTag>("stripClusterSrc"))),
87  phase2OTClustersToken_(consumes<edmNew::DetSetVector<Phase2TrackerCluster1D> >(
88  cfg.getParameter<edm::InputTag>("phase2OTClusterSrc"))),
89  trackingParticleToken_(
90  consumes<TrackingParticleCollection>(cfg.getParameter<edm::InputTag>("trackingParticleSrc"))),
91  throwOnMissingCollections_(cfg.getParameter<bool>("throwOnMissingCollections")) {
92  produces<ClusterTPAssociation>();
93 }
94 
97  desc.add<edm::InputTag>("simTrackSrc", edm::InputTag("g4SimHits"));
98  desc.add<edm::InputTag>("pixelSimLinkSrc", edm::InputTag("simSiPixelDigis"));
99  desc.add<edm::InputTag>("stripSimLinkSrc", edm::InputTag("simSiStripDigis"));
100  desc.add<edm::InputTag>("phase2OTSimLinkSrc", edm::InputTag("simSiPixelDigis", "Tracker"));
101  desc.add<edm::InputTag>("pixelClusterSrc", edm::InputTag("siPixelClusters"));
102  desc.add<edm::InputTag>("stripClusterSrc", edm::InputTag("siStripClusters"));
103  desc.add<edm::InputTag>("phase2OTClusterSrc", edm::InputTag("siPhase2Clusters"));
104  desc.add<edm::InputTag>("trackingParticleSrc", edm::InputTag("mix", "MergedTrackTruth"));
105  desc.add<bool>("throwOnMissingCollections", true);
106  descriptions.add("tpClusterProducerDefault", desc);
107 }
108 
110  // Pixel Cluster
112  bool foundPixelClusters = iEvent.getByToken(pixelClustersToken_, pixelClusters);
113 
114  // Strip Cluster
116  bool foundStripClusters = iEvent.getByToken(stripClustersToken_, stripClusters);
117 
118  // Phase2 Cluster
120  bool foundPhase2OTClusters = iEvent.getByToken(phase2OTClustersToken_, phase2OTClusters);
121 
122  // Pixel DigiSimLink
124  // iEvent.getByLabel(_pixelSimLinkSrc, sipixelSimLinks);
125  auto pixelSimLinksFound = iEvent.getByToken(sipixelSimLinksToken_, sipixelSimLinks);
126  if (not throwOnMissingCollections_ and foundPixelClusters and not pixelSimLinksFound) {
127  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
128  iEvent.put(std::move(clusterTPList));
129  return;
130  }
131 
132  // SiStrip DigiSimLink
134  auto stripSimLinksFound = iEvent.getByToken(sistripSimLinksToken_, sistripSimLinks);
135  if (not throwOnMissingCollections_ and foundStripClusters and not stripSimLinksFound) {
136  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
137  iEvent.put(std::move(clusterTPList));
138  return;
139  }
140 
141  // Phase2 OT DigiSimLink
143  auto phase2OTSimLinksFound = iEvent.getByToken(siphase2OTSimLinksToken_, siphase2OTSimLinks);
144  if (not throwOnMissingCollections_ and foundPhase2OTClusters and not phase2OTSimLinksFound) {
145  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
146  iEvent.put(std::move(clusterTPList));
147  return;
148  }
149 
150  // TrackingParticle
152  auto tpFound = iEvent.getByToken(trackingParticleToken_, TPCollectionH);
153  if (not throwOnMissingCollections_ and not tpFound) {
154  auto clusterTPList = std::make_unique<ClusterTPAssociation>();
155  iEvent.put(std::move(clusterTPList));
156  return;
157  }
158 
159  auto clusterTPList = std::make_unique<ClusterTPAssociation>(TPCollectionH);
160 
161  // prepare temporary map between SimTrackId and TrackingParticle index
162  std::unordered_map<UniqueSimTrackId, TrackingParticleRef, UniqueSimTrackIdHash> mapping;
163  auto const& tpColl = *TPCollectionH.product();
164  for (TrackingParticleCollection::size_type itp = 0; itp < tpColl.size(); ++itp) {
165  TrackingParticleRef trackingParticleRef(TPCollectionH, itp);
166  auto const& trackingParticle = tpColl[itp];
167  // SimTracks inside TrackingParticle
168  EncodedEventId eid(trackingParticle.eventId());
169  //size_t index = 0;
170  for (auto const& trk : trackingParticle.g4Tracks()) {
171  UniqueSimTrackId trkid(trk.trackId(), eid);
172  //std::cout << "creating map for id: " << trkid.first << " with tp: " << trackingParticle.key() << std::endl;
173  mapping.insert(std::make_pair(trkid, trackingParticleRef));
174  }
175  }
176 
177  std::unordered_set<UniqueSimTrackId, UniqueSimTrackIdHash> simTkIds;
178  std::vector<UniqueSimTrackId> trkid;
179  if (foundPixelClusters) {
180  // Pixel Clusters
181  clusterTPList->addKeyID(pixelClusters.id());
182  for (edmNew::DetSetVector<SiPixelCluster>::const_iterator iter = pixelClusters->begin();
183  iter != pixelClusters->end();
184  ++iter) {
185  uint32_t detid = iter->id();
186  DetId detId(detid);
187  edmNew::DetSet<SiPixelCluster> link_pixel = (*iter);
188  for (edmNew::DetSet<SiPixelCluster>::const_iterator di = link_pixel.begin(); di != link_pixel.end(); ++di) {
189  const SiPixelCluster& cluster = (*di);
191 
192  simTkIds.clear();
193  for (int irow = cluster.minPixelRow(); irow <= cluster.maxPixelRow(); ++irow) {
194  for (int icol = cluster.minPixelCol(); icol <= cluster.maxPixelCol(); ++icol) {
195  uint32_t channel = PixelChannelIdentifier::pixelToChannel(irow, icol);
196  trkid.clear();
197  getSimTrackId<PixelDigiSimLink>(trkid, sipixelSimLinks, detId, channel);
198  simTkIds.insert(trkid.begin(), trkid.end());
199  }
200  }
201  for (auto iset = simTkIds.begin(); iset != simTkIds.end(); iset++) {
202  auto ipos = mapping.find(*iset);
203  if (ipos != mapping.end()) {
204  //std::cout << "cluster in detid: " << detid << " from tp: " << ipos->second.key() << " " << iset->first << std::endl;
205  clusterTPList->emplace_back(OmniClusterRef(c_ref), ipos->second);
206  }
207  }
208  }
209  }
210  }
211 
212  if (foundStripClusters) {
213  // Strip Clusters
214  clusterTPList->addKeyID(stripClusters.id());
215  for (edmNew::DetSetVector<SiStripCluster>::const_iterator iter = stripClusters->begin(false),
216  eter = stripClusters->end(false);
217  iter != eter;
218  ++iter) {
219  if (!(*iter).isValid())
220  continue;
221  uint32_t detid = iter->id();
222  DetId detId(detid);
223  edmNew::DetSet<SiStripCluster> link_strip = (*iter);
224  for (edmNew::DetSet<SiStripCluster>::const_iterator di = link_strip.begin(); di != link_strip.end(); di++) {
225  const SiStripCluster& cluster = (*di);
227 
228  simTkIds.clear();
229  int first = cluster.firstStrip();
230  int last = first + cluster.amplitudes().size();
231 
232  for (int istr = first; istr < last; ++istr) {
233  trkid.clear();
234  getSimTrackId<StripDigiSimLink>(trkid, sistripSimLinks, detId, istr);
235  simTkIds.insert(trkid.begin(), trkid.end());
236  }
237  for (auto iset = simTkIds.begin(); iset != simTkIds.end(); iset++) {
238  auto ipos = mapping.find(*iset);
239  if (ipos != mapping.end()) {
240  //std::cout << "cluster in detid: " << detid << " from tp: " << ipos->second.key() << " " << iset->first << std::endl;
241  clusterTPList->emplace_back(OmniClusterRef(c_ref), ipos->second);
242  }
243  }
244  }
245  }
246  }
247 
248  if (foundPhase2OTClusters) {
249  // Phase2 Clusters
250  clusterTPList->addKeyID(phase2OTClusters.id());
251  if (phase2OTClusters.isValid()) {
252  for (edmNew::DetSetVector<Phase2TrackerCluster1D>::const_iterator iter = phase2OTClusters->begin(false),
253  eter = phase2OTClusters->end(false);
254  iter != eter;
255  ++iter) {
256  if (!(*iter).isValid())
257  continue;
258  uint32_t detid = iter->id();
259  DetId detId(detid);
260  edmNew::DetSet<Phase2TrackerCluster1D> link_phase2 = (*iter);
261  for (edmNew::DetSet<Phase2TrackerCluster1D>::const_iterator di = link_phase2.begin(); di != link_phase2.end();
262  di++) {
263  const Phase2TrackerCluster1D& cluster = (*di);
265  edmNew::makeRefTo(phase2OTClusters, di);
266 
267  simTkIds.clear();
268 
269  for (unsigned int istr(0); istr < cluster.size(); ++istr) {
270  uint32_t channel = Phase2TrackerDigi::pixelToChannel(cluster.firstRow() + istr, cluster.column());
271  trkid.clear();
272  getSimTrackId<PixelDigiSimLink>(trkid, siphase2OTSimLinks, detId, channel);
273  simTkIds.insert(trkid.begin(), trkid.end());
274  }
275 
276  for (auto iset = simTkIds.begin(); iset != simTkIds.end(); iset++) {
277  auto ipos = mapping.find(*iset);
278  if (ipos != mapping.end()) {
279  clusterTPList->emplace_back(OmniClusterRef(c_ref), ipos->second);
280  }
281  }
282  }
283  }
284  }
285  }
286  clusterTPList->sortAndUnique();
287  iEvent.put(std::move(clusterTPList));
288 }
289 
292 
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
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
tuple cfg
Definition: looper.py:296
std::pair< uint32_t, EncodedEventId > UniqueSimTrackId
std::vector< OmniClusterRef > OmniClusterCollection
ProductID id() const
Definition: HandleBase.cc:29
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< edmNew::DetSetVector< Phase2TrackerCluster1D > > phase2OTClustersToken_
SiStripCluster const & amplitudes() const
uint16_t firstStrip() const
data_type const * const_iterator
Definition: DetSetNew.h:31
uint16_t size_type
edm::EDGetTokenT< edm::DetSetVector< PixelDigiSimLink > > siphase2OTSimLinksToken_
edm::EDGetTokenT< edmNew::DetSetVector< SiPixelCluster > > pixelClustersToken_
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
int minPixelRow() const
auto size() const
decltype(auto) emplace_back(Args &&...args)
Definition: DetSet.h:68
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
ClusterTPAssociationProducer(const edm::ParameterSet &)
unsigned int column() const
static PackedDigiType pixelToChannel(unsigned int row, unsigned int col)
def move
Definition: eostools.py:511
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
edm::EDGetTokenT< TrackingParticleCollection > trackingParticleToken_
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetId.h:17
T const * product() const
Definition: Handle.h:70
edm::EDGetTokenT< edm::DetSetVector< PixelDigiSimLink > > sipixelSimLinksToken_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
int maxPixelCol() const
~ClusterTPAssociationProducer() override=default
collection_type data
Definition: DetSet.h:80
Pixel cluster – collection of neighboring pixels above threshold.
iterator end()
Definition: DetSetNew.h:56
static int pixelToChannel(int row, int col)
std::vector< TrackingParticle > TrackingParticleCollection
tuple last
Definition: dqmdumpme.py:56
collection_type::const_iterator const_iterator
Definition: DetSet.h:31
edm::EDGetTokenT< edm::DetSetVector< StripDigiSimLink > > sistripSimLinksToken_
iterator begin()
Definition: DetSetNew.h:54