Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-03 02:42:08

0001 #include "DataFormats/Common/interface/Handle.h"
0002 #include "SimDataFormats/Associations/interface/TrackAssociation.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "SimMuon/MCTruth/plugins/MuonAssociatorEDProducer.h"
0005 #include <memory>
0006 
0007 MuonAssociatorEDProducer::MuonAssociatorEDProducer(const edm::ParameterSet &parset)
0008     : tracksTag(parset.getParameter<edm::InputTag>("tracksTag")),
0009       tpTag(parset.getParameter<edm::InputTag>("tpTag")),
0010       tpRefVector(parset.getParameter<bool>("tpRefVector")),
0011       ignoreMissingTrackCollection(parset.getUntrackedParameter<bool>("ignoreMissingTrackCollection", false)),
0012       parset_(parset) {
0013   edm::LogVerbatim("MuonAssociatorEDProducer") << "constructing  MuonAssociatorEDProducer";
0014   produces<reco::RecoToSimCollection>();
0015   produces<reco::SimToRecoCollection>();
0016   if (tpRefVector)
0017     tpRefVectorToken_ = consumes<TrackingParticleRefVector>(tpTag);
0018   else
0019     tpToken_ = consumes<TrackingParticleCollection>(tpTag);
0020   tracksToken_ = consumes<edm::View<reco::Track>>(tracksTag);
0021 
0022   /// Perform some sanity checks of the configuration
0023   LogTrace("MuonAssociatorEDProducer") << "constructing  MuonAssociatorByHits" << parset_.dump();
0024   edm::LogVerbatim("MuonAssociatorEDProducer") << "\n MuonAssociatorByHits will associate reco::Tracks with "
0025                                                << tracksTag << "\n\t\t and TrackingParticles with " << tpTag;
0026   const std::string recoTracksLabel = tracksTag.label();
0027 
0028   // check and fix inconsistent input settings
0029   // tracks with hits only on muon detectors
0030   if (recoTracksLabel == "seedsOfSTAmuons" || recoTracksLabel == "standAloneMuons" ||
0031       recoTracksLabel == "refittedStandAloneMuons" || recoTracksLabel == "seedsOfDisplacedSTAmuons" ||
0032       recoTracksLabel == "displacedStandAloneMuons" || recoTracksLabel == "cosmicMuons" ||
0033       recoTracksLabel == "cosmicMuons1Leg" || recoTracksLabel == "hltL2Muons") {
0034     if (parset_.getParameter<bool>("UseTracker")) {
0035       edm::LogWarning("MuonAssociatorEDProducer")
0036           << "\n*** WARNING : inconsistent input tracksTag = " << tracksTag << "\n with UseTracker = true"
0037           << "\n ---> setting UseTracker = false ";
0038       parset_.addParameter<bool>("UseTracker", false);
0039     }
0040     if (!parset_.getParameter<bool>("UseMuon")) {
0041       edm::LogWarning("MuonAssociatorEDProducer")
0042           << "\n*** WARNING : inconsistent input tracksTag = " << tracksTag << "\n with UseMuon = false"
0043           << "\n ---> setting UseMuon = true ";
0044       parset_.addParameter<bool>("UseMuon", true);
0045     }
0046   }
0047   // tracks with hits only on tracker
0048   if (recoTracksLabel == "generalTracks" || recoTracksLabel == "probeTracks" || recoTracksLabel == "displacedTracks" ||
0049       recoTracksLabel == "extractGemMuons" || recoTracksLabel == "extractMe0Muons" ||
0050       recoTracksLabel == "ctfWithMaterialTracksP5LHCNavigation" || recoTracksLabel == "ctfWithMaterialTracksP5" ||
0051       recoTracksLabel == "hltIterL3OIMuonTrackSelectionHighPurity" || recoTracksLabel == "hltIterL3MuonMerged" ||
0052       recoTracksLabel == "hltIterL3MuonAndMuonFromL1Merged") {
0053     if (parset_.getParameter<bool>("UseMuon")) {
0054       edm::LogWarning("MuonAssociatorEDProducer")
0055           << "\n*** WARNING : inconsistent input tracksTag = " << tracksTag << "\n with UseMuon = true"
0056           << "\n ---> setting UseMuon = false ";
0057       parset_.addParameter<bool>("UseMuon", false);
0058     }
0059     if (!parset_.getParameter<bool>("UseTracker")) {
0060       edm::LogWarning("MuonAssociatorEDProducer")
0061           << "\n*** WARNING : inconsistent input tracksTag = " << tracksTag << "\n with UseTracker = false"
0062           << "\n ---> setting UseTracker = true ";
0063       parset_.addParameter<bool>("UseTracker", true);
0064     }
0065   }
0066 
0067   LogTrace("MuonAssociatorEDProducer") << "MuonAssociatorEDProducer::beginJob "
0068                                           ": constructing MuonAssociatorByHits";
0069   associatorByHits = new MuonAssociatorByHits(parset_, consumesCollector());
0070 }
0071 
0072 MuonAssociatorEDProducer::~MuonAssociatorEDProducer() {}
0073 
0074 void MuonAssociatorEDProducer::beginJob() {}
0075 
0076 void MuonAssociatorEDProducer::endJob() {}
0077 
0078 void MuonAssociatorEDProducer::produce(edm::Event &event, const edm::EventSetup &setup) {
0079   using namespace edm;
0080 
0081   TrackingParticleRefVector tmpTP;
0082   const TrackingParticleRefVector *tmpTPptr = nullptr;
0083   Handle<TrackingParticleCollection> TPCollection;
0084   Handle<TrackingParticleRefVector> TPCollectionRefVector;
0085 
0086   if (tpRefVector) {
0087     event.getByToken(tpRefVectorToken_, TPCollectionRefVector);
0088     tmpTPptr = TPCollectionRefVector.product();
0089     //
0090     tmpTP = *tmpTPptr;
0091   } else {
0092     event.getByToken(tpToken_, TPCollection);
0093     size_t nTP = TPCollection->size();
0094     for (size_t i = 0; i < nTP; ++i) {
0095       tmpTP.push_back(TrackingParticleRef(TPCollection, i));
0096     }
0097     tmpTPptr = &tmpTP;
0098   }
0099 
0100   LogTrace("MuonAssociatorEDProducer") << "getting TrackingParticle collection - " << tpTag;
0101   LogTrace("MuonAssociatorEDProducer") << "\t... size = " << tmpTPptr->size();
0102 
0103   Handle<edm::View<reco::Track>> trackCollection;
0104   LogTrace("MuonAssociatorEDProducer") << "getting reco::Track collection - " << tracksTag;
0105   bool trackAvailable = event.getByToken(tracksToken_, trackCollection);
0106   if (trackAvailable)
0107     LogTrace("MuonAssociatorEDProducer") << "\t... size = " << trackCollection->size();
0108   else
0109     LogTrace("MuonAssociatorEDProducer") << "\t... NOT FOUND.";
0110 
0111   std::unique_ptr<reco::RecoToSimCollection> rts;
0112   std::unique_ptr<reco::SimToRecoCollection> str;
0113 
0114   if (ignoreMissingTrackCollection && !trackAvailable) {
0115     // the track collection is not in the event and we're being told to ignore
0116     // this. do not output anything to the event, other wise this would be
0117     // considered as inefficiency.
0118     LogTrace("MuonAssociatorEDProducer") << "\n ignoring missing track collection."
0119                                          << "\n";
0120   } else {
0121     edm::RefToBaseVector<reco::Track> tmpT;
0122     for (size_t i = 0; i < trackCollection->size(); ++i)
0123       tmpT.push_back(trackCollection->refAt(i));
0124 
0125     edm::LogVerbatim("MuonAssociatorEDProducer")
0126         << "\n >>> RecoToSim association <<< \n"
0127         << "     Track collection : " << tracksTag.label() << ":" << tracksTag.instance()
0128         << " (size = " << trackCollection->size() << ") \n"
0129         << "     TrackingParticle collection : " << tpTag.label() << ":" << tpTag.instance()
0130         << " (size = " << tmpTPptr->size() << ")";
0131 
0132     reco::RecoToSimCollection recSimColl = associatorByHits->associateRecoToSim(tmpT, tmpTP, &event, &setup);
0133 
0134     edm::LogVerbatim("MuonAssociatorEDProducer")
0135         << "\n >>> SimToReco association <<< \n"
0136         << "     TrackingParticle collection : " << tpTag.label() << ":" << tpTag.instance()
0137         << " (size = " << tmpTPptr->size() << ") \n"
0138         << "     Track collection : " << tracksTag.label() << ":" << tracksTag.instance()
0139         << " (size = " << trackCollection->size() << ")";
0140 
0141     reco::SimToRecoCollection simRecColl = associatorByHits->associateSimToReco(tmpT, tmpTP, &event, &setup);
0142 
0143     rts = std::make_unique<reco::RecoToSimCollection>(recSimColl);
0144     str = std::make_unique<reco::SimToRecoCollection>(simRecColl);
0145 
0146     event.put(std::move(rts));
0147     event.put(std::move(str));
0148   }
0149 }