CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DataMixingTrackingParticleWorker.cc
Go to the documentation of this file.
1 // File: DataMixingTrackingParticleWorker.cc
2 // Description: see DataMixingTrackingParticleWorker.h
3 // Author: Mike Hildreth, University of Notre Dame
4 //
5 //--------------------------------------------
6 
7 #include <map>
8 #include <memory>
13 
17 //
18 //
20 
21 using namespace std;
22 
23 namespace {
24  template <typename T>
25  void appendDetSetVector(edm::DetSetVector<T>& target, const edm::DetSetVector<T>& source) {
26  for(auto& detsetSource: source) {
27  auto& detsetTarget = target.find_or_insert(detsetSource.detId());
28  std::copy(detsetSource.begin(), detsetSource.end(), std::back_inserter(detsetTarget));
29  }
30  }
31 }
32 
33 namespace edm
34 {
35 
36  // Virtual constructor
37 
38  DataMixingTrackingParticleWorker::DataMixingTrackingParticleWorker() { }
39 
40  // Constructor
41  DataMixingTrackingParticleWorker::DataMixingTrackingParticleWorker(const edm::ParameterSet& ps, edm::ConsumesCollector && iC)
42  {
43 
44  // get the subdetector names
45  // this->getSubdetectorNames(); //something like this may be useful to check what we are supposed to do...
46 
47  // declare the products to produce
48 
49  TrackingParticleLabelSig_ = ps.getParameter<edm::InputTag>("TrackingParticleLabelSig");
50 
51  TrackingParticlePileInputTag_ = ps.getParameter<edm::InputTag>("TrackingParticlePileInputTag");
52 
53  TrackingParticleCollectionDM_ = ps.getParameter<std::string>("TrackingParticleCollectionDM");
54 
55  TrackSigToken_ = iC.consumes<std::vector<TrackingParticle> >(TrackingParticleLabelSig_);
56  TrackPileToken_ = iC.consumes<std::vector<TrackingParticle> >(TrackingParticlePileInputTag_);
57 
58  VtxSigToken_ = iC.consumes<std::vector<TrackingVertex> >(TrackingParticleLabelSig_);
59  VtxPileToken_ = iC.consumes<std::vector<TrackingVertex> >(TrackingParticlePileInputTag_);
60 
61  // Pixel and Strip DigiSimlinks
62 
63  StripLinkPileInputTag_ = ps.getParameter<edm::InputTag>("StripDigiSimLinkPileInputTag");
64  PixelLinkPileInputTag_ = ps.getParameter<edm::InputTag>("PixelDigiSimLinkPileInputTag");
65  StripLinkCollectionDM_ = ps.getParameter<std::string>("StripDigiSimLinkCollectionDM");
66  PixelLinkCollectionDM_ = ps.getParameter<std::string>("PixelDigiSimLinkCollectionDM");
67 
68  StripLinkSigToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(ps.getParameter<edm::InputTag>("StripDigiSimLinkLabelSig"));
69  StripLinkPileToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(StripLinkPileInputTag_);
70  PixelLinkSigToken_ = iC.consumes<edm::DetSetVector<PixelDigiSimLink> >(ps.getParameter<edm::InputTag>("PixelDigiSimLinkLabelSig"));
71  PixelLinkPileToken_ = iC.consumes<edm::DetSetVector<PixelDigiSimLink> >(PixelLinkPileInputTag_);
72 
73  // Muon DigiSimLinks
74 
75  DTLinkPileInputTag_ = ps.getParameter<edm::InputTag>("DTDigiSimLinkPileInputTag");
76  RPCLinkPileInputTag_ = ps.getParameter<edm::InputTag>("RPCDigiSimLinkPileInputTag");
77  CSCWireLinkPileInputTag_ = ps.getParameter<edm::InputTag>("CSCWireDigiSimLinkPileInputTag");
78  CSCStripLinkPileInputTag_ = ps.getParameter<edm::InputTag>("CSCStripDigiSimLinkPileInputTag");
79 
80  DTLinkCollectionDM_ = ps.getParameter<std::string>("DTDigiSimLinkDM");
81  RPCLinkCollectionDM_ = ps.getParameter<std::string>("RPCDigiSimLinkDM");
82  CSCWireLinkCollectionDM_ = ps.getParameter<std::string>("CSCWireDigiSimLinkDM");
83  CSCStripLinkCollectionDM_ = ps.getParameter<std::string>("CSCStripDigiSimLinkDM");
84 
85  CSCWireLinkSigToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(ps.getParameter<edm::InputTag>("CSCWireDigiSimLinkLabelSig"));
86  CSCWireLinkPileToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(CSCWireLinkPileInputTag_);
87  CSCStripLinkSigToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(ps.getParameter<edm::InputTag>("CSCStripDigiSimLinkLabelSig"));
88  CSCStripLinkPileToken_ = iC.consumes<edm::DetSetVector<StripDigiSimLink> >(CSCStripLinkPileInputTag_);
89  DTLinkSigToken_ = iC.consumes< MuonDigiCollection<DTLayerId, DTDigiSimLink> >(ps.getParameter<edm::InputTag>("DTDigiSimLinkLabelSig"));
90  DTLinkPileToken_ = iC.consumes< MuonDigiCollection<DTLayerId, DTDigiSimLink> >(DTLinkPileInputTag_);
91  RPCLinkSigToken_ = iC.consumes<edm::DetSetVector<RPCDigiSimLink> >(ps.getParameter<edm::InputTag>("RPCDigiSimLinkLabelSig"));
92  RPCLinkPileToken_ = iC.consumes<edm::DetSetVector<RPCDigiSimLink> >(RPCLinkPileInputTag_);
93 
94  }
95 
96 
97  // Virtual destructor needed.
98  DataMixingTrackingParticleWorker::~DataMixingTrackingParticleWorker() {
99  }
100 
101  // Need an event initialization
102 
103  void DataMixingTrackingParticleWorker::initializeEvent(edm::Event const& e, edm::EventSetup const& iSetup) {
104 
105  // Create new track/vertex lists, getting references, too, so that we can cross-link everything
106 
107  NewTrackList_ = std::auto_ptr<std::vector<TrackingParticle>>(new std::vector<TrackingParticle>());
108  //NewVertexList_ = std::auto_ptr<std::vector<TrackingVertex>>(new std::vector<TrackingVertex>());
109  TempVertexList_ = std::vector<TrackingVertex>();
110 
111  TrackListRef_ =const_cast<edm::Event&>( e ).getRefBeforePut< std::vector<TrackingParticle> >(TrackingParticleCollectionDM_);
112  VertexListRef_ =const_cast<edm::Event&>( e ).getRefBeforePut< std::vector<TrackingVertex> >(TrackingParticleCollectionDM_);
113 
114  // tracker
115 
116  NewStripLinkList_ = std::make_unique<edm::DetSetVector<StripDigiSimLink> >();
117  NewPixelLinkList_ = std::make_unique<edm::DetSetVector<PixelDigiSimLink> >();
118 
119  // muons
120 
121  NewCSCStripLinkList_ = std::make_unique<edm::DetSetVector<StripDigiSimLink> >();
122  NewCSCWireLinkList_ = std::make_unique<edm::DetSetVector<StripDigiSimLink> >();
123  NewRPCLinkList_ = std::make_unique<edm::DetSetVector<RPCDigiSimLink> >();
124  NewDTLinkList_ = std::make_unique< MuonDigiCollection<DTLayerId, DTDigiSimLink> >();
125 
126  }
127 
128 
129  void DataMixingTrackingParticleWorker::addTrackingParticleSignals(const edm::Event &e) {
130 
131  // grab Vertices, store copy, preserving indices. Easier to loop over vertices first - fewer links
132 
134  e.getByToken(VtxSigToken_, vtxs);
135 
136  int StartingIndexV = int(TempVertexList_.size()); // should be zero here, but keep for consistency
137  int StartingIndexT = int(NewTrackList_->size()); // should be zero here, but keep for consistency
138 
139  if (vtxs.isValid()) {
140 
141  for (std::vector<TrackingVertex>::const_iterator vtx = vtxs->begin(); vtx != vtxs->end(); ++vtx) {
142  TempVertexList_.push_back(*vtx);
143  }
144  }
145 
146  // grab tracks, store copy
147 
149  e.getByToken(TrackSigToken_, tracks);
150 
151  if (tracks.isValid()) {
152  for (std::vector<TrackingParticle>::const_iterator track = tracks->begin(); track != tracks->end(); ++track) {
153  auto oldRef=track->parentVertex();
154  auto newRef=TrackingVertexRef( VertexListRef_, oldRef.index()+StartingIndexV );
155  NewTrackList_->push_back(*track);
156 
157  auto & Ntrack = NewTrackList_->back(); //modify copy
158 
159  Ntrack.setParentVertex( newRef );
160  Ntrack.clearDecayVertices();
161 
162  // next, loop over daughter vertices, same strategy
163 
164  for( auto const& vertexRef : track->decayVertices() ) {
165  auto newRef=TrackingVertexRef( VertexListRef_, vertexRef.index()+StartingIndexV );
166  Ntrack.addDecayVertex(newRef);
167  }
168  }
169  }
170 
171  // Now that tracks are handled, go back and put correct Refs in vertices
172 
173  for (auto & vertex : TempVertexList_ ) {
174 
175  vertex.clearParentTracks();
176  vertex.clearDaughterTracks();
177 
178  for( auto const& trackRef : vertex.sourceTracks() ) {
179  auto newRef=TrackingParticleRef( TrackListRef_, trackRef.index()+StartingIndexT );
180  vertex.addParentTrack(newRef);
181  }
182 
183  // next, loop over daughter tracks, same strategy
184  for( auto const& trackRef : vertex.daughterTracks() ) {
185  auto newRef=TrackingParticleRef( TrackListRef_, trackRef.index()+StartingIndexT );
186  vertex.addDaughterTrack(newRef);
187  }
188  }
189 
190  // Accumulate DigiSimLinks
192  e.getByToken(StripLinkSigToken_, stripLinks);
193  if(stripLinks.isValid()) {
194  appendDetSetVector(*NewStripLinkList_, *stripLinks);
195  }
196 
198  e.getByToken(PixelLinkSigToken_, pixelLinks);
199  if(pixelLinks.isValid()) {
200  appendDetSetVector(*NewPixelLinkList_, *pixelLinks);
201  }
202 
204  e.getByToken(CSCStripLinkSigToken_, CSCstripLinks);
205  if(CSCstripLinks.isValid()) {
206  appendDetSetVector(*NewCSCStripLinkList_, *CSCstripLinks);
207  }
208 
210  e.getByToken(CSCWireLinkSigToken_, CSCwireLinks);
211  if(CSCwireLinks.isValid()) {
212  appendDetSetVector(*NewCSCWireLinkList_, *CSCwireLinks);
213  }
214 
216  e.getByToken(RPCLinkSigToken_, RPCLinks);
217  if(RPCLinks.isValid()) {
218  appendDetSetVector(*NewRPCLinkList_, *RPCLinks);
219  }
220 
222  e.getByToken(DTLinkSigToken_, DTLinks);
223  if(DTLinks.isValid()) {
224  for (DTDigiSimLinkCollection::DigiRangeIterator detUnit=DTLinks->begin(); detUnit !=DTLinks->end(); ++detUnit) {
225  const DTLayerId& layerid = (*detUnit).first;
226  const DTDigiSimLinkCollection::Range& range = (*detUnit).second;
227  NewDTLinkList_->put(range,layerid);
228  }
229  }
230 
231  } // end of addTrackingParticleSignals
232 
233 
234 
235  void DataMixingTrackingParticleWorker::addTrackingParticlePileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr,
236  ModuleCallingContext const* mcc) {
237 
238  LogDebug("DataMixingTrackingParticleWorker") <<"\n===============> adding pileups from event "<<ep->id()<<" for bunchcrossing "<<bcr;
239 
240  int StartingIndexV = int(TempVertexList_.size()); // keep track of offsets
241  int StartingIndexT = int(NewTrackList_->size()); // keep track of offsets
242 
243  std::shared_ptr<Wrapper<std::vector<TrackingVertex> > const> inputVPTR =
244  getProductByTag<std::vector<TrackingVertex> >(*ep, TrackingParticlePileInputTag_, mcc);
245 
246  if(inputVPTR ) {
247 
248  const std::vector<TrackingVertex> *vtxs = const_cast< std::vector<TrackingVertex> * >(inputVPTR->product());
249 
250  // grab vertices, store copy
251 
252  for (std::vector<TrackingVertex>::const_iterator vtx = vtxs->begin(); vtx != vtxs->end(); ++vtx) {
253  TempVertexList_.push_back(*vtx);
254  }
255  }
256 
257 
258  std::shared_ptr<Wrapper<std::vector<TrackingParticle> > const> inputPTR =
259  getProductByTag<std::vector<TrackingParticle> >(*ep, TrackingParticlePileInputTag_, mcc);
260 
261  if(inputPTR ) {
262 
263  const std::vector<TrackingParticle> *tracks = const_cast< std::vector<TrackingParticle> * >(inputPTR->product());
264 
265  // grab tracks, store copy
266  for (std::vector<TrackingParticle>::const_iterator track = tracks->begin(); track != tracks->end(); ++track) {
267  auto oldRef=track->parentVertex();
268  auto newRef=TrackingVertexRef( VertexListRef_, oldRef.index()+StartingIndexV );
269  NewTrackList_->push_back(*track);
270 
271  auto & Ntrack = NewTrackList_->back(); //modify copy
272 
273  Ntrack.setParentVertex( newRef );
274  Ntrack.clearDecayVertices();
275 
276  // next, loop over daughter vertices, same strategy
277 
278  for( auto const& vertexRef : track->decayVertices() ) {
279  auto newRef=TrackingVertexRef( VertexListRef_, vertexRef.index()+StartingIndexV );
280  Ntrack.addDecayVertex(newRef);
281  }
282  }
283  }
284 
285  // Now that tracks are handled, go back and put correct Refs in vertices
286 
287  for (auto & vertex : TempVertexList_ ) {
288 
289  vertex.clearParentTracks();
290  vertex.clearDaughterTracks();
291 
292  for( auto const& trackRef : vertex.sourceTracks() ) {
293  auto newRef=TrackingParticleRef( TrackListRef_, trackRef.index()+StartingIndexT );
294  vertex.addParentTrack(newRef);
295  }
296 
297  // next, loop over daughter tracks, same strategy
298  for( auto const& trackRef : vertex.daughterTracks() ) {
299  auto newRef=TrackingParticleRef( TrackListRef_, trackRef.index()+StartingIndexT );
300  vertex.addDaughterTrack(newRef);
301  }
302  }
303 
304 
305  // Accumulate DigiSimLinks
306  std::shared_ptr<Wrapper<edm::DetSetVector<StripDigiSimLink> > const> inputStripPtr =
307  getProductByTag<edm::DetSetVector<StripDigiSimLink> >(*ep, StripLinkPileInputTag_, mcc);
308  if(inputStripPtr) {
309  appendDetSetVector(*NewStripLinkList_, *(inputStripPtr->product()));
310  }
311 
312  std::shared_ptr<Wrapper<edm::DetSetVector<PixelDigiSimLink> > const> inputPixelPtr =
313  getProductByTag<edm::DetSetVector<PixelDigiSimLink> >(*ep, PixelLinkPileInputTag_, mcc);
314  if(inputPixelPtr) {
315  appendDetSetVector(*NewPixelLinkList_, *(inputPixelPtr->product()));
316  }
317 
318  std::shared_ptr<Wrapper<edm::DetSetVector<StripDigiSimLink> > const> CSCinputStripPtr =
319  getProductByTag<edm::DetSetVector<StripDigiSimLink> >(*ep, CSCStripLinkPileInputTag_, mcc);
320  if(CSCinputStripPtr) {
321  appendDetSetVector(*NewCSCStripLinkList_, *(CSCinputStripPtr->product()));
322  }
323 
324  std::shared_ptr<Wrapper<edm::DetSetVector<StripDigiSimLink> > const> CSCinputWirePtr =
325  getProductByTag<edm::DetSetVector<StripDigiSimLink> >(*ep, CSCWireLinkPileInputTag_, mcc);
326  if(CSCinputWirePtr) {
327  appendDetSetVector(*NewCSCWireLinkList_, *(CSCinputWirePtr->product()));
328  }
329 
330  std::shared_ptr<Wrapper<edm::DetSetVector<RPCDigiSimLink> > const> inputRPCPtr =
331  getProductByTag<edm::DetSetVector<RPCDigiSimLink> >(*ep, RPCLinkPileInputTag_, mcc);
332  if(inputRPCPtr) {
333  appendDetSetVector(*NewRPCLinkList_, *(inputRPCPtr->product()));
334  }
335 
336  std::shared_ptr<Wrapper< DTDigiSimLinkCollection > const> inputDTPtr =
337  getProductByTag< DTDigiSimLinkCollection >(*ep, DTLinkPileInputTag_, mcc);
338  if(inputDTPtr) {
339  const DTDigiSimLinkCollection* DTLinks = const_cast< DTDigiSimLinkCollection * >(inputDTPtr->product());
340  for (DTDigiSimLinkCollection::DigiRangeIterator detUnit=DTLinks->begin(); detUnit !=DTLinks->end(); ++detUnit) {
341  const DTLayerId& layerid = (*detUnit).first;
342  const DTDigiSimLinkCollection::Range& range = (*detUnit).second;
343  NewDTLinkList_->put(range,layerid);
344  }
345  }
346 
347  } // end of addPileups
348 
349 
350 
351  void DataMixingTrackingParticleWorker::putTrackingParticle(edm::Event &e) {
352 
353  // collection of Vertices to put in the event
354 
355  NewVertexList_ = std::auto_ptr<std::vector<TrackingVertex>>(new std::vector<TrackingVertex>(TempVertexList_));
356 
357  // put the collection of digis in the event
358  LogInfo("DataMixingTrackingParticleWorker") << "total # Merged Tracks: " << NewTrackList_->size() ;
359 
360  // put collections
361 
362  e.put( NewTrackList_, TrackingParticleCollectionDM_ );
363  e.put( NewVertexList_, TrackingParticleCollectionDM_ );
364 
365  e.put( std::move(NewStripLinkList_), StripLinkCollectionDM_ );
366  e.put( std::move(NewPixelLinkList_), PixelLinkCollectionDM_ );
367 
368  e.put( std::move(NewCSCStripLinkList_), CSCStripLinkCollectionDM_ );
369  e.put( std::move(NewCSCWireLinkList_), CSCWireLinkCollectionDM_ );
370  e.put( std::move(NewRPCLinkList_), RPCLinkCollectionDM_ );
371  e.put( std::move(NewDTLinkList_), DTLinkCollectionDM_ );
372 
373 
374  // clear local storage for this event
375  //NewTrackList_.clear();
376  TempVertexList_.clear();
377  }
378 
379 } //edm
#define LogDebug(id)
T getParameter(std::string const &) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
EventID const & id() const
reference find_or_insert(det_id_type id)
Definition: DetSetVector.h:258
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
bool isValid() const
Definition: HandleBase.h:75
edm::Ref< TrackingVertexCollection > TrackingVertexRef
tuple tracks
Definition: testEve_cfg.py:39
std::pair< const_iterator, const_iterator > Range
DigiRangeIterator end() const
static std::string const source
Definition: EdmProvDump.cc:42
edm::Ref< TrackingParticleCollection > TrackingParticleRef
DigiRangeIterator begin() const