Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:50

0001 #include "FastSimulation/Tracking/plugins/RecoTrackAccumulator.h"
0002 
0003 #include <memory>
0004 
0005 #include "DataFormats/Common/interface/ValueMap.h"
0006 #include "DataFormats/TrackReco/interface/Track.h"
0007 #include "FWCore/Framework/interface/ConsumesCollector.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 RecoTrackAccumulator::RecoTrackAccumulator(const edm::ParameterSet& conf,
0011                                            edm::ProducesCollector producesCollector,
0012                                            edm::ConsumesCollector& iC)
0013     : signalTracksTag(conf.getParameter<edm::InputTag>("signalTracks")),
0014       pileUpTracksTag(conf.getParameter<edm::InputTag>("pileUpTracks")),
0015       outputLabel(conf.getParameter<std::string>("outputLabel")) {
0016   producesCollector.produces<reco::TrackCollection>(outputLabel);
0017   producesCollector.produces<TrackingRecHitCollection>(outputLabel);
0018   producesCollector.produces<reco::TrackExtraCollection>(outputLabel);
0019 
0020   iC.consumes<reco::TrackCollection>(signalTracksTag);
0021   iC.consumes<TrackingRecHitCollection>(signalTracksTag);
0022   iC.consumes<reco::TrackExtraCollection>(signalTracksTag);
0023 }
0024 
0025 RecoTrackAccumulator::~RecoTrackAccumulator() {}
0026 
0027 void RecoTrackAccumulator::initializeEvent(edm::Event const& e, edm::EventSetup const& iSetup) {
0028   newTracks_ = std::make_unique<reco::TrackCollection>();
0029   newHits_ = std::make_unique<TrackingRecHitCollection>();
0030   newTrackExtras_ = std::make_unique<reco::TrackExtraCollection>();
0031 
0032   // this is needed to get the ProductId of the TrackExtra and TrackingRecHit and Track collections
0033   rNewTracks = const_cast<edm::Event&>(e).getRefBeforePut<reco::TrackCollection>(outputLabel);
0034   rNewTrackExtras = const_cast<edm::Event&>(e).getRefBeforePut<reco::TrackExtraCollection>(outputLabel);
0035   rNewHits = const_cast<edm::Event&>(e).getRefBeforePut<TrackingRecHitCollection>(outputLabel);
0036 }
0037 
0038 void RecoTrackAccumulator::accumulate(edm::Event const& e, edm::EventSetup const& iSetup) {
0039   accumulateEvent(e, iSetup, signalTracksTag);
0040 }
0041 
0042 void RecoTrackAccumulator::accumulate(PileUpEventPrincipal const& e,
0043                                       edm::EventSetup const& iSetup,
0044                                       edm::StreamID const&) {
0045   if (e.bunchCrossing() == 0) {
0046     accumulateEvent(e, iSetup, pileUpTracksTag);
0047   }
0048 }
0049 
0050 void RecoTrackAccumulator::finalizeEvent(edm::Event& e, const edm::EventSetup& iSetup) {
0051   e.put(std::move(newTracks_), outputLabel);
0052   e.put(std::move(newHits_), outputLabel);
0053   e.put(std::move(newTrackExtras_), outputLabel);
0054 }
0055 
0056 template <class T>
0057 void RecoTrackAccumulator::accumulateEvent(const T& e, edm::EventSetup const& iSetup, const edm::InputTag& label) {
0058   edm::Handle<reco::TrackCollection> tracks;
0059   edm::Handle<TrackingRecHitCollection> hits;
0060   edm::Handle<reco::TrackExtraCollection> trackExtras;
0061   e.getByLabel(label, tracks);
0062   e.getByLabel(label, hits);
0063   e.getByLabel(label, trackExtras);
0064 
0065   if (!tracks.isValid()) {
0066     throw cms::Exception("RecoTrackAccumulator")
0067         << "Failed to find track collections with inputTag " << label << std::endl;
0068   }
0069   if (!hits.isValid()) {
0070     throw cms::Exception("RecoTrackAccumulator")
0071         << "Failed to find hit collections with inputTag " << label << std::endl;
0072   }
0073   if (!trackExtras.isValid()) {
0074     throw cms::Exception("RecoTrackAccumulator")
0075         << "Failed to find trackExtra collections with inputTag " << label << std::endl;
0076   }
0077 
0078   for (size_t t = 0; t < tracks->size(); ++t) {
0079     const reco::Track& track = (*tracks)[t];
0080     newTracks_->push_back(track);
0081     // track extras:
0082     auto const& extra = trackExtras->at(track.extra().key());
0083     newTrackExtras_->emplace_back(extra.outerPosition(),
0084                                   extra.outerMomentum(),
0085                                   extra.outerOk(),
0086                                   extra.innerPosition(),
0087                                   extra.innerMomentum(),
0088                                   extra.innerOk(),
0089                                   extra.outerStateCovariance(),
0090                                   extra.outerDetId(),
0091                                   extra.innerStateCovariance(),
0092                                   extra.innerDetId(),
0093                                   extra.seedDirection(),
0094                                   //If TrajectorySeeds are needed, then their list must be gotten from the
0095                                   // secondary event directly and looked up similarly to TrackExtras.
0096                                   //We can't use a default constructed RefToBase due to a bug in RefToBase
0097                                   // which causes an seg fault when calling isAvailable on a default constructed one.
0098                                   edm::RefToBase<TrajectorySeed>{edm::Ref<std::vector<TrajectorySeed>>{}});
0099     newTracks_->back().setExtra(reco::TrackExtraRef(rNewTrackExtras, newTracks_->size() - 1));
0100     // rechits:
0101     // note: extra.recHit(i) does not work for pileup events
0102     // probably the Ref does not know its product id applies on a pileup event
0103     auto& newExtra = newTrackExtras_->back();
0104     auto const firstTrackIndex = newHits_->size();
0105     for (unsigned int i = 0; i < extra.recHitsSize(); i++) {
0106       newHits_->push_back((*hits)[extra.recHit(i).key()]);
0107     }
0108     newExtra.setHits(rNewHits, firstTrackIndex, newHits_->size() - firstTrackIndex);
0109     newExtra.setTrajParams(extra.trajParams(), extra.chi2sX5());
0110     assert(newExtra.recHitsSize() == newExtra.trajParams().size());
0111   }
0112 }
0113 
0114 #include "SimGeneral/MixingModule/interface/DigiAccumulatorMixModFactory.h"
0115 DEFINE_DIGI_ACCUMULATOR(RecoTrackAccumulator);