Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:31:07

0001 #include "TauAnalysis/MCEmbeddingTools/plugins/TrackMergeremb.h"
0002 
0003 #include <memory>
0004 
0005 #include "DataFormats/TrackReco/interface/TrackExtra.h"
0006 
0007 #include "DataFormats/GsfTrackReco/interface/GsfTrackExtra.h"
0008 #include "DataFormats/GsfTrackReco/interface/GsfTrackExtraFwd.h"
0009 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0010 #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
0011 
0012 #include "DataFormats/EgammaTrackReco/interface/ConversionTrack.h"
0013 #include "DataFormats/EgammaTrackReco/interface/ConversionTrackFwd.h"
0014 
0015 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0016 #include "DataFormats/MuonReco/interface/MuonTrackLinks.h"
0017 
0018 #include "DataFormats/MuonReco/interface/MuonQuality.h"
0019 #include "DataFormats/Common/interface/ValueMap.h"
0020 
0021 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0022 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0023 
0024 #include "DataFormats/MuonReco/interface/Muon.h"
0025 #include "DataFormats/MuonReco/interface/MuonTrackLinks.h"
0026 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0027 #include "RecoMuon/MuonIdentification/interface/MuonTimingFiller.h"
0028 #include "DataFormats/MuonReco/interface/MuonCocktails.h"
0029 #include "DataFormats/MuonReco/interface/MuonTime.h"
0030 #include "DataFormats/MuonReco/interface/MuonTimeExtra.h"
0031 #include "DataFormats/MuonReco/interface/MuonTimeExtraMap.h"
0032 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
0033 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
0034 #include "DataFormats/MuonReco/interface/CaloMuon.h"
0035 
0036 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0037 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
0038 
0039 #include "DataFormats/ParticleFlowReco/interface/PFRecTrackFwd.h"
0040 #include "DataFormats/ParticleFlowReco/interface/PFRecTrack.h"
0041 
0042 typedef TrackMergeremb<reco::TrackCollection> TrackColMerger;
0043 typedef TrackMergeremb<reco::MuonCollection> MuonColMerger;
0044 typedef TrackMergeremb<reco::GsfElectronCollection> GsfElectronColMerger;
0045 typedef TrackMergeremb<reco::PhotonCollection> PhotonColMerger;
0046 typedef TrackMergeremb<reco::PFCandidateCollection> PFColMerger;
0047 
0048 // Here some overloaded functions, which are needed such that the right merger function is called for the indivudal Collections
0049 template <typename T1>
0050 void TrackMergeremb<T1>::willproduce(std::string instance, std::string alias) {
0051   produces<TrackCollectionemb>(instance);
0052 }
0053 
0054 template <typename T1>
0055 void TrackMergeremb<T1>::willconsume(const edm::ParameterSet& iConfig) {}
0056 
0057 template <typename T1>
0058 void TrackMergeremb<T1>::merg_and_put(edm::Event& iEvent,
0059                                       std::string instance,
0060                                       std::vector<edm::EDGetTokenT<TrackCollectionemb> >& to_merge) {
0061   std::unique_ptr<TrackCollectionemb> outTracks = std::unique_ptr<TrackCollectionemb>(new TrackCollectionemb);
0062 
0063   for (auto akt_collection : to_merge) {
0064     edm::Handle<TrackCollectionemb> track_col_in;
0065     iEvent.getByToken(akt_collection, track_col_in);
0066 
0067     size_t sedref_it = 0;
0068     for (typename TrackCollectionemb::const_iterator it = track_col_in->begin(); it != track_col_in->end();
0069          ++it, ++sedref_it) {
0070       outTracks->push_back(typename T1::value_type(*it));
0071     }
0072 
0073   }  // end merge
0074 
0075   iEvent.put(std::move(outTracks), instance);
0076 }
0077 
0078 template <>
0079 void TrackMergeremb<reco::TrackCollection>::willproduce(std::string instance, std::string alias) {
0080   produces<reco::TrackCollection>(instance).setBranchAlias(alias + "Tracks");
0081   produces<reco::TrackExtraCollection>(instance).setBranchAlias(alias + "TrackExtras");
0082   produces<TrackingRecHitCollection>(instance).setBranchAlias(alias + "RecHits");
0083   produces<TrackToTrackMapnew>();
0084 }
0085 
0086 template <>
0087 void TrackMergeremb<reco::TrackCollection>::merg_and_put(
0088     edm::Event& iEvent, std::string instance, std::vector<edm::EDGetTokenT<reco::TrackCollection> >& to_merge) {
0089   std::unique_ptr<reco::TrackCollection> outTracks = std::make_unique<reco::TrackCollection>();
0090   std::unique_ptr<reco::TrackExtraCollection> outTracks_ex = std::make_unique<reco::TrackExtraCollection>();
0091   std::unique_ptr<TrackingRecHitCollection> outTracks_rh = std::make_unique<TrackingRecHitCollection>();
0092   std::unique_ptr<TrackToTrackMapnew> outTracks_refs = std::make_unique<TrackToTrackMapnew>();
0093 
0094   auto rTrackExtras = iEvent.getRefBeforePut<reco::TrackExtraCollection>();
0095   // auto rHits = iEvent.getRefBeforePut<TrackingRecHitCollection>();
0096 
0097   std::vector<reco::TrackRefVector> trackRefColl;
0098   //std::vector<reco::TrackRef> trackRefColl;
0099 
0100   for (auto akt_collection : to_merge) {
0101     edm::Handle<reco::TrackCollection> track_col_in;
0102     iEvent.getByToken(akt_collection, track_col_in);
0103 
0104     unsigned sedref_it = 0;
0105     for (reco::TrackCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end();
0106          ++it, ++sedref_it) {
0107       outTracks->push_back(reco::Track(*it));
0108       outTracks_ex->push_back(reco::TrackExtra(*it->extra()));
0109       outTracks->back().setExtra(reco::TrackExtraRef(rTrackExtras, outTracks_ex->size() - 1));
0110       reco::TrackRef trackRefold(track_col_in, sedref_it);
0111 
0112       reco::TrackRefVector trackRefColl_helpvec;
0113       trackRefColl_helpvec.push_back(trackRefold);
0114       trackRefColl.push_back(trackRefColl_helpvec);
0115     }
0116 
0117   }  // end merge
0118 
0119   edm::OrphanHandle<reco::TrackCollection> trackHandle = iEvent.put(std::move(outTracks), instance);
0120   iEvent.put(std::move(outTracks_ex), instance);
0121 
0122   TrackToTrackMapnew::Filler filler(*outTracks_refs);
0123   filler.insert(trackHandle, trackRefColl.begin(), trackRefColl.end());
0124   filler.fill();
0125 
0126   iEvent.put(std::move(outTracks_refs));
0127   iEvent.put(std::move(outTracks_rh), instance);  // not implemented so far
0128 }
0129 
0130 template <>
0131 void TrackMergeremb<reco::GsfTrackCollection>::willproduce(std::string instance, std::string alias) {
0132   produces<reco::GsfTrackCollection>(instance).setBranchAlias(alias + "GsfTracks");
0133   produces<reco::TrackExtraCollection>(instance).setBranchAlias(alias + "TrackExtras");
0134   produces<reco::GsfTrackExtraCollection>(instance).setBranchAlias(alias + "GsfTrackExtras");
0135   produces<TrackingRecHitCollection>(instance).setBranchAlias(alias + "RecHits");
0136 }
0137 
0138 template <>
0139 void TrackMergeremb<reco::GsfTrackCollection>::merg_and_put(
0140     edm::Event& iEvent, std::string instance, std::vector<edm::EDGetTokenT<reco::GsfTrackCollection> >& to_merge) {
0141   std::unique_ptr<reco::GsfTrackCollection> outTracks = std::make_unique<reco::GsfTrackCollection>();
0142   std::unique_ptr<reco::TrackExtraCollection> outTracks_ex = std::make_unique<reco::TrackExtraCollection>();
0143   std::unique_ptr<reco::GsfTrackExtraCollection> outTracks_exgsf = std::make_unique<reco::GsfTrackExtraCollection>();
0144   std::unique_ptr<TrackingRecHitCollection> outTracks_rh = std::make_unique<TrackingRecHitCollection>();
0145 
0146   auto rTrackExtras = iEvent.getRefBeforePut<reco::TrackExtraCollection>();
0147   auto rTrackExtras_gsf = iEvent.getRefBeforePut<reco::GsfTrackExtraCollection>();
0148 
0149   auto rHits = iEvent.getRefBeforePut<TrackingRecHitCollection>();
0150 
0151   for (auto akt_collection : to_merge) {
0152     edm::Handle<reco::GsfTrackCollection> track_col_in;
0153     iEvent.getByToken(akt_collection, track_col_in);
0154 
0155     size_t sedref_it = 0;
0156     for (reco::GsfTrackCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end();
0157          ++it, ++sedref_it) {
0158       outTracks->push_back(reco::GsfTrack(*it));
0159       outTracks_ex->push_back(reco::TrackExtra(*it->extra()));
0160       outTracks_exgsf->push_back(reco::GsfTrackExtra(*it->gsfExtra()));
0161 
0162       outTracks->back().setExtra(reco::TrackExtraRef(rTrackExtras, outTracks_ex->size() - 1));
0163       outTracks->back().setGsfExtra(reco::GsfTrackExtraRef(rTrackExtras_gsf, outTracks_exgsf->size() - 1));
0164     }
0165 
0166   }  // end merge
0167 
0168   iEvent.put(std::move(outTracks), instance);
0169   iEvent.put(std::move(outTracks_ex), instance);
0170   iEvent.put(std::move(outTracks_exgsf), instance);
0171   iEvent.put(std::move(outTracks_rh), instance);
0172 }
0173 
0174 template <>
0175 void TrackMergeremb<reco::MuonCollection>::willproduce(std::string instance, std::string alias) {
0176   produces<reco::MuonCollection>();
0177   produces<reco::CaloMuonCollection>();
0178   produces<reco::MuonTimeExtraMap>("combined");
0179   produces<reco::MuonTimeExtraMap>("dt");
0180   produces<reco::MuonTimeExtraMap>("csc");
0181 
0182   // todo make this configurable (or not )
0183   produces<reco::IsoDepositMap>("tracker");
0184   produces<reco::IsoDepositMap>("ecal");
0185   produces<reco::IsoDepositMap>("hcal");
0186   produces<reco::IsoDepositMap>("ho");
0187   produces<reco::IsoDepositMap>("jets");
0188 
0189   produces<reco::MuonToMuonMap>();
0190 }
0191 
0192 template <>
0193 void TrackMergeremb<reco::MuonCollection>::willconsume(const edm::ParameterSet& iConfig) {
0194   inputs_fixtrackrefs_ = consumes<TrackToTrackMapnew>(edm::InputTag("generalTracks"));
0195   inputs_fixtrackcol_ = consumes<reco::TrackCollection>(edm::InputTag("generalTracks"));
0196 }
0197 
0198 template <>
0199 void TrackMergeremb<reco::MuonCollection>::merg_and_put(
0200     edm::Event& iEvent, std::string instance, std::vector<edm::EDGetTokenT<reco::MuonCollection> >& to_merge) {
0201   std::unique_ptr<reco::MuonCollection> outTracks = std::make_unique<reco::MuonCollection>();
0202   std::unique_ptr<reco::CaloMuonCollection> calomu =
0203       std::make_unique<reco::CaloMuonCollection>();  //not implemented so far
0204 
0205   edm::Handle<TrackToTrackMapnew> track_ref_map;
0206   iEvent.getByToken(inputs_fixtrackrefs_, track_ref_map);
0207 
0208   edm::Handle<reco::TrackCollection> track_new_col;
0209   iEvent.getByToken(inputs_fixtrackcol_, track_new_col);
0210   std::map<reco::TrackRef, reco::TrackRef>
0211       simple_track_to_track_map;  //I didn't find a more elegant way, so just build a good old fassion std::map
0212   for (unsigned abc = 0; abc < track_new_col->size(); ++abc) {
0213     reco::TrackRef trackRef(track_new_col, abc);
0214     simple_track_to_track_map[((*track_ref_map)[trackRef])[0]] = trackRef;
0215   }
0216 
0217   std::vector<reco::MuonRef> muonRefColl;
0218   reco::MuonRefProd outputMuonsRefProd = iEvent.getRefBeforePut<reco::MuonCollection>();
0219   unsigned new_idx = 0;
0220   for (auto akt_collection : to_merge) {
0221     edm::Handle<reco::MuonCollection> track_col_in;
0222     iEvent.getByToken(akt_collection, track_col_in);
0223     unsigned old_idx = 0;
0224     for (reco::MuonCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end();
0225          ++it, ++old_idx, ++new_idx) {
0226       outTracks->push_back(reco::Muon(*it));
0227       reco::MuonRef muRefold(track_col_in, old_idx);
0228       muonRefColl.push_back(muRefold);
0229       reco::MuonRef muRefnew(outputMuonsRefProd, new_idx);
0230 
0231       if (it->track().isNonnull()) {
0232         //std::cout<<"pfmerge tr: "<<it->trackRef().id()<< " "<< it->trackRef().key()<< " " << simple_track_to_track_map[it->trackRef()].id() <<  " " << simple_track_to_track_map[it->trackRef()].key() <<std::endl;
0233         outTracks->back().setTrack(simple_track_to_track_map[it->track()]);
0234       }
0235     }
0236 
0237   }  // end merge
0238 
0239   const int nMuons = outTracks->size();
0240 
0241   std::vector<reco::MuonTimeExtra> dtTimeColl(nMuons);
0242   std::vector<reco::MuonTimeExtra> cscTimeColl(nMuons);
0243   std::vector<reco::MuonTimeExtra> combinedTimeColl(nMuons);
0244   std::vector<reco::IsoDeposit> trackDepColl(nMuons);
0245   std::vector<reco::IsoDeposit> ecalDepColl(nMuons);
0246   std::vector<reco::IsoDeposit> hcalDepColl(nMuons);
0247   std::vector<reco::IsoDeposit> hoDepColl(nMuons);
0248   std::vector<reco::IsoDeposit> jetDepColl(nMuons);
0249 
0250   edm::OrphanHandle<reco::MuonCollection> muonHandle = iEvent.put(std::move(outTracks));
0251 
0252   auto fillMap = [](auto refH, auto& vec, edm::Event& ev, const std::string& cAl = "") {
0253     typedef edm::ValueMap<typename std::decay<decltype(vec)>::type::value_type> MapType;
0254     std::unique_ptr<MapType> oMap(new MapType());
0255     {
0256       typename MapType::Filler filler(*oMap);
0257       filler.insert(refH, vec.begin(), vec.end());
0258       vec.clear();
0259       filler.fill();
0260     }
0261     ev.put(std::move(oMap), cAl);
0262   };
0263 
0264   fillMap(muonHandle, combinedTimeColl, iEvent, "combined");
0265   fillMap(muonHandle, dtTimeColl, iEvent, "dt");
0266   fillMap(muonHandle, cscTimeColl, iEvent, "csc");
0267   fillMap(muonHandle, trackDepColl, iEvent, "tracker");
0268   fillMap(muonHandle, ecalDepColl, iEvent, "ecal");
0269   fillMap(muonHandle, hcalDepColl, iEvent, "hcal");
0270   fillMap(muonHandle, hoDepColl, iEvent, "ho");
0271   fillMap(muonHandle, jetDepColl, iEvent, "jets");
0272   fillMap(muonHandle, muonRefColl, iEvent);
0273   iEvent.put(std::move(calomu));
0274 }
0275 
0276 template <>
0277 void TrackMergeremb<reco::PFCandidateCollection>::willproduce(std::string instance, std::string alias) {
0278   produces<reco::PFCandidateCollection>(instance);
0279   // std::cout<<"Produce PF Collection: "<<instance<<std::endl;
0280 }
0281 
0282 template <>
0283 void TrackMergeremb<reco::PFCandidateCollection>::willconsume(const edm::ParameterSet& iConfig) {
0284   inputs_fixtrackrefs_ = consumes<TrackToTrackMapnew>(edm::InputTag("generalTracks"));
0285   inputs_fixtrackcol_ = consumes<reco::TrackCollection>(edm::InputTag("generalTracks"));
0286   inputs_fixmurefs_ = consumes<reco::MuonToMuonMap>(edm::InputTag("muons1stStep"));
0287   inputs_fixmucol_ = consumes<reco::MuonCollection>(edm::InputTag("muons1stStep"));
0288 }
0289 
0290 template <>
0291 void TrackMergeremb<reco::PFCandidateCollection>::merg_and_put(
0292     edm::Event& iEvent, std::string instance, std::vector<edm::EDGetTokenT<reco::PFCandidateCollection> >& to_merge) {
0293   std::unique_ptr<reco::PFCandidateCollection> outTracks = std::make_unique<reco::PFCandidateCollection>();
0294 
0295   edm::Handle<TrackToTrackMapnew> track_ref_map;
0296   iEvent.getByToken(inputs_fixtrackrefs_, track_ref_map);
0297 
0298   edm::Handle<reco::TrackCollection> track_new_col;
0299   iEvent.getByToken(inputs_fixtrackcol_, track_new_col);
0300   std::map<reco::TrackRef, reco::TrackRef>
0301       simple_track_to_track_map;  //I didn't find a more elegant way, so just build a good old fassion std::map
0302   for (unsigned abc = 0; abc < track_new_col->size(); ++abc) {
0303     reco::TrackRef trackRef(track_new_col, abc);
0304     simple_track_to_track_map[((*track_ref_map)[trackRef])[0]] = trackRef;
0305   }
0306 
0307   edm::Handle<reco::MuonToMuonMap> muon_ref_map;
0308   iEvent.getByToken(inputs_fixmurefs_, muon_ref_map);
0309 
0310   edm::Handle<reco::MuonCollection> muon_new_col;
0311   iEvent.getByToken(inputs_fixmucol_, muon_new_col);
0312   std::map<reco::MuonRef, reco::MuonRef>
0313       simple_mu_to_mu_map;  //I didn't find a more elegant way, so just build a good old fassion std::map
0314   for (unsigned abc = 0; abc < muon_new_col->size(); ++abc) {
0315     reco::MuonRef muRef(muon_new_col, abc);
0316     simple_mu_to_mu_map[(*muon_ref_map)[muRef]] = muRef;
0317   }
0318 
0319   for (auto akt_collection : to_merge) {
0320     edm::Handle<reco::PFCandidateCollection> track_col_in;
0321     iEvent.getByToken(akt_collection, track_col_in);
0322     for (reco::PFCandidateCollection::const_iterator it = track_col_in->begin(); it != track_col_in->end(); ++it) {
0323       outTracks->push_back(reco::PFCandidate(*it));
0324       //if (fabs(it->pdgId()) == 13){
0325       if (it->trackRef().isNonnull() && outTracks->back().charge()) {
0326         //std::cout<<"pfmerge tr: "<<it->trackRef().id()<< " "<< it->trackRef().key()<< " " << simple_track_to_track_map[it->trackRef()].id() <<  " " << simple_track_to_track_map[it->trackRef()].key() <<std::endl;
0327         outTracks->back().setTrackRef(simple_track_to_track_map[it->trackRef()]);
0328       }
0329       if (it->muonRef().isNonnull()) {
0330         //std::cout<<"pfmerge mu: "<<it->muonRef().id()<< " "<< it->muonRef().key()<< " " << simple_mu_to_mu_map[it->muonRef()].id() <<  " " << simple_mu_to_mu_map[it->muonRef()].key() <<std::endl;
0331         outTracks->back().setMuonRef(simple_mu_to_mu_map[it->muonRef()]);
0332       }
0333     }
0334   }  // end merge
0335 
0336   iEvent.put(std::move(outTracks), instance);
0337 }
0338 
0339 #include "FWCore/Framework/interface/MakerMacros.h"
0340 
0341 DEFINE_FWK_MODULE(TrackColMerger);
0342 DEFINE_FWK_MODULE(MuonColMerger);
0343 DEFINE_FWK_MODULE(GsfElectronColMerger);
0344 DEFINE_FWK_MODULE(PhotonColMerger);
0345 DEFINE_FWK_MODULE(PFColMerger);