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  const size_t StartingIndexV = TempVertexList_.size(); // should be zero here, but keep for consistency
137  const size_t StartingIndexT = 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  std::vector<decltype(TrackingParticleRef().index())> sourceTrackIndices;
173  std::vector<decltype(TrackingParticleRef().index())> daughterTrackIndices;
174  for (auto & vertex : TempVertexList_ ) {
175 
176  // Need to copy the indices before clearing the vectors
177  sourceTrackIndices.reserve(vertex.sourceTracks().size());
178  daughterTrackIndices.reserve(vertex.daughterTracks().size());
179  for(auto const& ref: vertex.sourceTracks()) sourceTrackIndices.push_back(ref.index());
180  for(auto const& ref: vertex.daughterTracks()) daughterTrackIndices.push_back(ref.index());
181 
182  vertex.clearParentTracks();
183  vertex.clearDaughterTracks();
184 
185  for( auto index : sourceTrackIndices ) {
186  auto newRef=TrackingParticleRef( TrackListRef_, index+StartingIndexT );
187  vertex.addParentTrack(newRef);
188  }
189 
190  // next, loop over daughter tracks, same strategy
191  for( auto index : daughterTrackIndices ) {
192  auto newRef=TrackingParticleRef( TrackListRef_, index+StartingIndexT );
193  vertex.addDaughterTrack(newRef);
194  }
195 
196  sourceTrackIndices.clear();
197  daughterTrackIndices.clear();
198  }
199 
200  // Accumulate DigiSimLinks
202  e.getByToken(StripLinkSigToken_, stripLinks);
203  if(stripLinks.isValid()) {
204  appendDetSetVector(*NewStripLinkList_, *stripLinks);
205  }
206 
208  e.getByToken(PixelLinkSigToken_, pixelLinks);
209  if(pixelLinks.isValid()) {
210  appendDetSetVector(*NewPixelLinkList_, *pixelLinks);
211  }
212 
214  e.getByToken(CSCStripLinkSigToken_, CSCstripLinks);
215  if(CSCstripLinks.isValid()) {
216  appendDetSetVector(*NewCSCStripLinkList_, *CSCstripLinks);
217  }
218 
220  e.getByToken(CSCWireLinkSigToken_, CSCwireLinks);
221  if(CSCwireLinks.isValid()) {
222  appendDetSetVector(*NewCSCWireLinkList_, *CSCwireLinks);
223  }
224 
226  e.getByToken(RPCLinkSigToken_, RPCLinks);
227  if(RPCLinks.isValid()) {
228  appendDetSetVector(*NewRPCLinkList_, *RPCLinks);
229  }
230 
232  e.getByToken(DTLinkSigToken_, DTLinks);
233  if(DTLinks.isValid()) {
234  for (DTDigiSimLinkCollection::DigiRangeIterator detUnit=DTLinks->begin(); detUnit !=DTLinks->end(); ++detUnit) {
235  const DTLayerId& layerid = (*detUnit).first;
236  const DTDigiSimLinkCollection::Range& range = (*detUnit).second;
237  NewDTLinkList_->put(range,layerid);
238  }
239  }
240 
241  } // end of addTrackingParticleSignals
242 
243 
244 
245  void DataMixingTrackingParticleWorker::addTrackingParticlePileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr,
246  ModuleCallingContext const* mcc) {
247 
248  LogDebug("DataMixingTrackingParticleWorker") <<"\n===============> adding pileups from event "<<ep->id()<<" for bunchcrossing "<<bcr;
249 
250  const size_t StartingIndexV = TempVertexList_.size(); // keep track of offsets
251  const size_t StartingIndexT = NewTrackList_->size(); // keep track of offsets
252 
253  std::shared_ptr<Wrapper<std::vector<TrackingVertex> > const> inputVPTR =
254  getProductByTag<std::vector<TrackingVertex> >(*ep, TrackingParticlePileInputTag_, mcc);
255 
256  if(inputVPTR ) {
257 
258  const std::vector<TrackingVertex> *vtxs = const_cast< std::vector<TrackingVertex> * >(inputVPTR->product());
259 
260  // grab vertices, store copy
261 
262  for (std::vector<TrackingVertex>::const_iterator vtx = vtxs->begin(); vtx != vtxs->end(); ++vtx) {
263  TempVertexList_.push_back(*vtx);
264  }
265  }
266 
267 
268  std::shared_ptr<Wrapper<std::vector<TrackingParticle> > const> inputPTR =
269  getProductByTag<std::vector<TrackingParticle> >(*ep, TrackingParticlePileInputTag_, mcc);
270 
271  if(inputPTR ) {
272 
273  const std::vector<TrackingParticle> *tracks = const_cast< std::vector<TrackingParticle> * >(inputPTR->product());
274 
275  // grab tracks, store copy
276  for (std::vector<TrackingParticle>::const_iterator track = tracks->begin(); track != tracks->end(); ++track) {
277  auto oldRef=track->parentVertex();
278  auto newRef=TrackingVertexRef( VertexListRef_, oldRef.index()+StartingIndexV );
279  NewTrackList_->push_back(*track);
280 
281  auto & Ntrack = NewTrackList_->back(); //modify copy
282 
283  Ntrack.setParentVertex( newRef );
284  Ntrack.clearDecayVertices();
285 
286  // next, loop over daughter vertices, same strategy
287 
288  for( auto const& vertexRef : track->decayVertices() ) {
289  auto newRef=TrackingVertexRef( VertexListRef_, vertexRef.index()+StartingIndexV );
290  Ntrack.addDecayVertex(newRef);
291  }
292  }
293  }
294 
295  // Now that tracks are handled, go back and put correct Refs in vertices
296  // Operate only on the added pileup vertices, and leave the already-existing vertices untouched
297  std::vector<decltype(TrackingParticleRef().index())> sourceTrackIndices;
298  std::vector<decltype(TrackingParticleRef().index())> daughterTrackIndices;
299  for(size_t iVertex = StartingIndexV; iVertex != TempVertexList_.size(); ++iVertex) {
300  auto& vertex = TempVertexList_[iVertex];
301 
302  // Need to copy the indices before clearing the vectors
303  sourceTrackIndices.reserve(vertex.sourceTracks().size());
304  daughterTrackIndices.reserve(vertex.daughterTracks().size());
305  for(auto const& ref: vertex.sourceTracks()) sourceTrackIndices.push_back(ref.index());
306  for(auto const& ref: vertex.daughterTracks()) daughterTrackIndices.push_back(ref.index());
307 
308  vertex.clearParentTracks();
309  vertex.clearDaughterTracks();
310 
311  for( auto index : sourceTrackIndices ) {
312  auto newRef=TrackingParticleRef( TrackListRef_, index+StartingIndexT );
313  vertex.addParentTrack(newRef);
314  }
315 
316  // next, loop over daughter tracks, same strategy
317  for( auto index : daughterTrackIndices ) {
318  auto newRef=TrackingParticleRef( TrackListRef_, index+StartingIndexT );
319  vertex.addDaughterTrack(newRef);
320  }
321 
322  sourceTrackIndices.clear();
323  daughterTrackIndices.clear();
324  }
325 
326 
327  // Accumulate DigiSimLinks
328  std::shared_ptr<Wrapper<edm::DetSetVector<StripDigiSimLink> > const> inputStripPtr =
329  getProductByTag<edm::DetSetVector<StripDigiSimLink> >(*ep, StripLinkPileInputTag_, mcc);
330  if(inputStripPtr) {
331  appendDetSetVector(*NewStripLinkList_, *(inputStripPtr->product()));
332  }
333 
334  std::shared_ptr<Wrapper<edm::DetSetVector<PixelDigiSimLink> > const> inputPixelPtr =
335  getProductByTag<edm::DetSetVector<PixelDigiSimLink> >(*ep, PixelLinkPileInputTag_, mcc);
336  if(inputPixelPtr) {
337  appendDetSetVector(*NewPixelLinkList_, *(inputPixelPtr->product()));
338  }
339 
340  std::shared_ptr<Wrapper<edm::DetSetVector<StripDigiSimLink> > const> CSCinputStripPtr =
341  getProductByTag<edm::DetSetVector<StripDigiSimLink> >(*ep, CSCStripLinkPileInputTag_, mcc);
342  if(CSCinputStripPtr) {
343  appendDetSetVector(*NewCSCStripLinkList_, *(CSCinputStripPtr->product()));
344  }
345 
346  std::shared_ptr<Wrapper<edm::DetSetVector<StripDigiSimLink> > const> CSCinputWirePtr =
347  getProductByTag<edm::DetSetVector<StripDigiSimLink> >(*ep, CSCWireLinkPileInputTag_, mcc);
348  if(CSCinputWirePtr) {
349  appendDetSetVector(*NewCSCWireLinkList_, *(CSCinputWirePtr->product()));
350  }
351 
352  std::shared_ptr<Wrapper<edm::DetSetVector<RPCDigiSimLink> > const> inputRPCPtr =
353  getProductByTag<edm::DetSetVector<RPCDigiSimLink> >(*ep, RPCLinkPileInputTag_, mcc);
354  if(inputRPCPtr) {
355  appendDetSetVector(*NewRPCLinkList_, *(inputRPCPtr->product()));
356  }
357 
358  std::shared_ptr<Wrapper< DTDigiSimLinkCollection > const> inputDTPtr =
359  getProductByTag< DTDigiSimLinkCollection >(*ep, DTLinkPileInputTag_, mcc);
360  if(inputDTPtr) {
361  const DTDigiSimLinkCollection* DTLinks = const_cast< DTDigiSimLinkCollection * >(inputDTPtr->product());
362  for (DTDigiSimLinkCollection::DigiRangeIterator detUnit=DTLinks->begin(); detUnit !=DTLinks->end(); ++detUnit) {
363  const DTLayerId& layerid = (*detUnit).first;
364  const DTDigiSimLinkCollection::Range& range = (*detUnit).second;
365  NewDTLinkList_->put(range,layerid);
366  }
367  }
368 
369  } // end of addPileups
370 
371 
372 
373  void DataMixingTrackingParticleWorker::putTrackingParticle(edm::Event &e) {
374 
375  // collection of Vertices to put in the event
376 
377  NewVertexList_ = std::auto_ptr<std::vector<TrackingVertex>>(new std::vector<TrackingVertex>(TempVertexList_));
378 
379  // put the collection of digis in the event
380  LogInfo("DataMixingTrackingParticleWorker") << "total # Merged Tracks: " << NewTrackList_->size() ;
381 
382  // put collections
383 
384  e.put( NewTrackList_, TrackingParticleCollectionDM_ );
385  e.put( NewVertexList_, TrackingParticleCollectionDM_ );
386 
387  e.put( std::move(NewStripLinkList_), StripLinkCollectionDM_ );
388  e.put( std::move(NewPixelLinkList_), PixelLinkCollectionDM_ );
389 
390  e.put( std::move(NewCSCStripLinkList_), CSCStripLinkCollectionDM_ );
391  e.put( std::move(NewCSCWireLinkList_), CSCWireLinkCollectionDM_ );
392  e.put( std::move(NewRPCLinkList_), RPCLinkCollectionDM_ );
393  e.put( std::move(NewDTLinkList_), DTLinkCollectionDM_ );
394 
395 
396  // clear local storage for this event
397  //NewTrackList_.clear();
398  TempVertexList_.clear();
399  }
400 
401 } //edm
#define LogDebug(id)
T getParameter(std::string const &) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
EventID const & id() const
reference find_or_insert(det_id_type id)
Definition: DetSetVector.h:254
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
def move
Definition: eostools.py:510
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:43
edm::Ref< TrackingParticleCollection > TrackingParticleRef
DigiRangeIterator begin() const