Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:46:10

0001 #include "DataFormats/Common/interface/ValueMap.h"
0002 #include "DataFormats/EgammaReco/interface/ElectronSeed.h"
0003 #include "DataFormats/ParticleFlowReco/interface/PreId.h"
0004 #include "DataFormats/TrackReco/interface/Track.h"
0005 #include "FastSimulation/Tracking/plugins/ElectronSeedTrackRefFix.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/MakerMacros.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 
0010 namespace {
0011 
0012   template <typename VMType, typename HandleType>
0013   const edm::OrphanHandle<edm::ValueMap<VMType>> addVMToEvent(edm::Event& event,
0014                                                               const edm::Handle<HandleType>& handle,
0015                                                               std::vector<VMType> values,
0016                                                               const std::string& label) {
0017     auto vMap = std::make_unique<edm::ValueMap<VMType>>();
0018     typename edm::ValueMap<VMType>::Filler mapFiller(*vMap);
0019     mapFiller.insert(handle, values.begin(), values.end());
0020     mapFiller.fill();
0021     return event.put(std::move(vMap), label);
0022   }
0023 
0024   template <typename T>
0025   edm::Handle<T> getHandle(const edm::Event& event, const edm::EDGetTokenT<T>& token) {
0026     edm::Handle<T> handle;
0027     event.getByToken(token, handle);
0028     return handle;
0029   }
0030 
0031 }  // namespace
0032 
0033 ElectronSeedTrackRefFix::ElectronSeedTrackRefFix(const edm::ParameterSet& iConfig) {
0034   // read parameters
0035   preidgsfLabel_ = iConfig.getParameter<std::string>("PreGsfLabel");
0036   preidLabel_ = iConfig.getParameter<std::vector<std::string>>("PreIdLabel");
0037   oldTracksTag_ = iConfig.getParameter<edm::InputTag>("oldTrackCollection");
0038   newTracksTag_ = iConfig.getParameter<edm::InputTag>("newTrackCollection");
0039   seedsTag_ = iConfig.getParameter<edm::InputTag>("seedCollection");
0040   idsTag_ = iConfig.getParameter<std::vector<edm::InputTag>>("idCollection");
0041 
0042   //register your products
0043   produces<reco::ElectronSeedCollection>(preidgsfLabel_);
0044   for (const auto& idLabel : preidLabel_) {
0045     produces<reco::PreIdCollection>(idLabel);
0046     produces<edm::ValueMap<reco::PreIdRef>>(idLabel);
0047   }
0048 
0049   //create tokens
0050   oldTracksToken_ = consumes<reco::TrackCollection>(oldTracksTag_);
0051   newTracksToken_ = consumes<reco::TrackCollection>(newTracksTag_);
0052   seedsToken_ = consumes<reco::ElectronSeedCollection>(seedsTag_);
0053   for (const auto& idTag : idsTag_) {
0054     idsToken_.emplace_back(consumes<reco::PreIdCollection>(idTag));
0055     idMapToken_.emplace_back(consumes<edm::ValueMap<reco::PreIdRef>>(idTag));
0056   }
0057 }
0058 
0059 ElectronSeedTrackRefFix::~ElectronSeedTrackRefFix() {
0060   // do anything here that needs to be done at desctruction time
0061   // (e.g. close files, deallocate resources etc.)
0062 }
0063 
0064 // ------------ method called to produce the data  ------------
0065 void ElectronSeedTrackRefFix::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0066   auto oldTracks = getHandle(iEvent, oldTracksToken_);
0067   auto newTracks = getHandle(iEvent, newTracksToken_);
0068   auto iSeeds = getHandle(iEvent, seedsToken_);
0069 
0070   auto oSeeds = std::make_unique<reco::ElectronSeedCollection>();
0071   for (unsigned int s = 0; s < iSeeds->size(); ++s) {
0072     oSeeds->push_back(iSeeds->at(s));
0073     reco::TrackRef newTrackRef(newTracks, oSeeds->back().ctfTrack().index());
0074     oSeeds->back().setCtfTrack(newTrackRef);
0075   }
0076   iEvent.put(std::move(oSeeds), preidgsfLabel_);
0077 
0078   for (size_t idNr = 0; idNr < idsTag_.size(); idNr++) {
0079     auto iIds = getHandle(iEvent, idsToken_[idNr]);
0080     auto oIds = std::make_unique<reco::PreIdCollection>();
0081     for (unsigned int i = 0; i < iIds->size(); ++i) {
0082       oIds->push_back(iIds->at(i));
0083       reco::TrackRef newTrackRef(newTracks, oIds->back().trackRef().index());
0084       oIds->back().setTrack(newTrackRef);
0085     }
0086     const edm::OrphanHandle<reco::PreIdCollection> preIdProd = iEvent.put(std::move(oIds), preidLabel_[idNr]);
0087 
0088     auto iIdMap = getHandle(iEvent, idMapToken_[idNr]);
0089     std::vector<reco::PreIdRef> values;
0090     for (size_t trkNr = 0; trkNr < newTracks->size(); ++trkNr) {
0091       //low pt electron seeds do not make the idMaps so this is now optional to fill in a useful way
0092       if (trkNr < oldTracks->size() && iIdMap.isValid()) {
0093         reco::TrackRef oldTrackRef(oldTracks, trkNr);
0094         values.push_back(reco::PreIdRef(preIdProd, (*(iIdMap.product()))[oldTrackRef].index()));
0095       } else
0096         values.push_back(reco::PreIdRef());
0097     }
0098     addVMToEvent(iEvent, newTracks, std::move(values), preidLabel_[idNr]);
0099   }  //end loop over ids
0100 }
0101 
0102 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0103 void ElectronSeedTrackRefFix::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0104   //The following says we do not know what parameters are allowed so do no validation
0105   // Please change this to state exactly what you do use, even if it is no parameters
0106   edm::ParameterSetDescription desc;
0107   desc.setUnknown();
0108   descriptions.addDefault(desc);
0109 }
0110 
0111 #include "FWCore/Framework/interface/MakerMacros.h"
0112 DEFINE_FWK_MODULE(ElectronSeedTrackRefFix);