Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:47

0001 // -*- C++ -*-
0002 //
0003 // Package:    SimMuon/MCTruth
0004 // Class:      MuonToTrackingParticleAssociatorEDProducer
0005 //
0006 /**\class MuonToTrackingParticleAssociatorEDProducer
0007  MuonToTrackingParticleAssociatorEDProducer.cc
0008  SimMuon/MCTruth/plugins/MuonToTrackingParticleAssociatorEDProducer.cc
0009 
0010  Description: [one line class summary]
0011 
0012  Implementation:
0013      [Notes on implementation]
0014 */
0015 //
0016 // Original Author:  Christopher Jones
0017 //         Created:  Wed, 07 Jan 2015 21:30:14 GMT
0018 //
0019 //
0020 
0021 // system include files
0022 #include <memory>
0023 
0024 // user include files
0025 #include "FWCore/Framework/interface/Frameworkfwd.h"
0026 #include "FWCore/Framework/interface/stream/EDProducer.h"
0027 
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 
0031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0032 
0033 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0034 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0035 #include "MuonToTrackingParticleAssociatorByHitsImpl.h"
0036 #include "SimDataFormats/Associations/interface/MuonToTrackingParticleAssociator.h"
0037 #include "SimMuon/MCTruth/interface/TrackerMuonHitExtractor.h"
0038 
0039 //
0040 // class declaration
0041 //
0042 namespace {
0043   using TrackHitsCollection = MuonAssociatorByHitsHelper::TrackHitsCollection;
0044 
0045   class InputDumper {
0046   public:
0047     InputDumper(const edm::ParameterSet &conf, edm::ConsumesCollector &&iC)
0048         : simtracksTag(conf.getParameter<edm::InputTag>("simtracksTag")),
0049           simtracksXFTag(conf.getParameter<edm::InputTag>("simtracksXFTag")),
0050           crossingframe(conf.getParameter<bool>("crossingframe")) {
0051       if (crossingframe) {
0052         simtracksXFToken_ = iC.consumes<CrossingFrame<SimTrack>>(simtracksXFTag);
0053         simvertsXFToken_ = iC.consumes<CrossingFrame<SimVertex>>(simtracksXFTag);
0054       } else {
0055         simtracksToken_ = iC.consumes<edm::SimTrackContainer>(simtracksTag);
0056         simvertsToken_ = iC.consumes<edm::SimVertexContainer>(simtracksTag);
0057       }
0058     }
0059 
0060     void read(const edm::Event &);
0061     void dump(const TrackHitsCollection &, const TrackingParticleCollection &) const;
0062 
0063   private:
0064     edm::InputTag simtracksTag;
0065     edm::InputTag simtracksXFTag;
0066     edm::EDGetTokenT<CrossingFrame<SimTrack>> simtracksXFToken_;
0067     edm::EDGetTokenT<CrossingFrame<SimVertex>> simvertsXFToken_;
0068     edm::EDGetTokenT<edm::SimTrackContainer> simtracksToken_;
0069     edm::EDGetTokenT<edm::SimVertexContainer> simvertsToken_;
0070 
0071     edm::Handle<CrossingFrame<SimTrack>> simtracksXF_;
0072     edm::Handle<CrossingFrame<SimVertex>> simvertsXF_;
0073     edm::Handle<edm::SimTrackContainer> simtracks_;
0074     edm::Handle<edm::SimVertexContainer> simverts_;
0075     bool const crossingframe;
0076   };
0077 
0078   void InputDumper::read(const edm::Event &iEvent) {
0079     if (crossingframe) {
0080       iEvent.getByToken(simtracksXFToken_, simtracksXF_);
0081       iEvent.getByToken(simvertsXFToken_, simvertsXF_);
0082     } else {
0083       iEvent.getByToken(simtracksToken_, simtracks_);
0084       iEvent.getByToken(simvertsToken_, simverts_);
0085     }
0086   }
0087 
0088   void InputDumper::dump(const TrackHitsCollection &tC, const TrackingParticleCollection &tPC) const {
0089     using namespace std;
0090     // reco::Track collection
0091     edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer") << "\n"
0092                                                                    << "reco::Track collection --- size = " << tC.size();
0093 
0094     // TrackingParticle collection
0095     edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0096         << "\n"
0097         << "TrackingParticle collection --- size = " << tPC.size();
0098     int j = 0;
0099     for (TrackingParticleCollection::const_iterator ITER = tPC.begin(); ITER != tPC.end(); ITER++, j++) {
0100       edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0101           << "TrackingParticle " << j << ", q = " << ITER->charge() << ", p = " << ITER->p() << ", pT = " << ITER->pt()
0102           << ", eta = " << ITER->eta() << ", phi = " << ITER->phi();
0103 
0104       edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0105           << "\t pdg code = " << ITER->pdgId() << ", made of " << ITER->numberOfHits() << " PSimHit"
0106           << " (in " << ITER->numberOfTrackerLayers() << " layers)"
0107           << " from " << ITER->g4Tracks().size() << " SimTrack:";
0108       for (TrackingParticle::g4t_iterator g4T = ITER->g4Track_begin(); g4T != ITER->g4Track_end(); g4T++) {
0109         edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0110             << "\t\t Id:" << g4T->trackId() << "/Evt:(" << g4T->eventId().event() << ","
0111             << g4T->eventId().bunchCrossing() << ")";
0112       }
0113     }
0114 
0115     if (crossingframe) {
0116       std::unique_ptr<MixCollection<SimTrack>> SimTk(new MixCollection<SimTrack>(simtracksXF_.product()));
0117       edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0118           << "\n"
0119           << "CrossingFrame<SimTrack> collection with InputTag = " << simtracksXFTag << " has size = " << SimTk->size();
0120       int k = 0;
0121       for (MixCollection<SimTrack>::MixItr ITER = SimTk->begin(); ITER != SimTk->end(); ITER++, k++) {
0122         edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0123             << "SimTrack " << k << " - Id:" << ITER->trackId() << "/Evt:(" << ITER->eventId().event() << ","
0124             << ITER->eventId().bunchCrossing() << ")"
0125             << ", q = " << ITER->charge() << ", p = " << ITER->momentum().P() << ", pT = " << ITER->momentum().Pt()
0126             << ", eta = " << ITER->momentum().Eta() << ", phi = " << ITER->momentum().Phi()
0127             << "\n\t pdgId = " << ITER->type() << ", Vertex index = " << ITER->vertIndex()
0128             << ", Gen Particle index = " << (ITER->genpartIndex() > 0 ? ITER->genpartIndex() - 1 : ITER->genpartIndex())
0129             << endl;
0130       }
0131 
0132       std::unique_ptr<MixCollection<SimVertex>> SimVtx(new MixCollection<SimVertex>(simvertsXF_.product()));
0133       edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0134           << "\n"
0135           << "CrossingFrame<SimVertex> collection with InputTag = " << simtracksXFTag
0136           << " has size = " << SimVtx->size();
0137       int kv = 0;
0138       for (MixCollection<SimVertex>::MixItr VITER = SimVtx->begin(); VITER != SimVtx->end(); VITER++, kv++) {
0139         edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0140             << "SimVertex " << kv << " - Id:" << VITER->vertexId() << ", position = " << VITER->position()
0141             << ", parent SimTrack Id = " << VITER->parentIndex() << ", processType = " << VITER->processType();
0142       }
0143     } else {
0144       const edm::SimTrackContainer simTC = *(simtracks_.product());
0145       edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0146           << "\n"
0147           << "SimTrack collection with InputTag = " << simtracksTag << " has size = " << simTC.size() << endl;
0148       int k = 0;
0149       for (edm::SimTrackContainer::const_iterator ITER = simTC.begin(); ITER != simTC.end(); ITER++, k++) {
0150         edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0151             << "SimTrack " << k << " - Id:" << ITER->trackId() << "/Evt:(" << ITER->eventId().event() << ","
0152             << ITER->eventId().bunchCrossing() << ")"
0153             << ", q = " << ITER->charge() << ", p = " << ITER->momentum().P() << ", pT = " << ITER->momentum().Pt()
0154             << ", eta = " << ITER->momentum().Eta() << ", phi = " << ITER->momentum().Phi()
0155             << "\n\t pdgId = " << ITER->type() << ", Vertex index = " << ITER->vertIndex()
0156             << ", Gen Particle index = " << (ITER->genpartIndex() > 0 ? ITER->genpartIndex() - 1 : ITER->genpartIndex())
0157             << endl;
0158       }
0159       const edm::SimVertexContainer simVC = *(simverts_.product());
0160       edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer") << "\n"
0161                                                                      << "SimVertex collection with InputTag = "
0162                                                                      << "g4SimHits"
0163                                                                      << " has size = " << simVC.size() << endl;
0164       int kv = 0;
0165       for (edm::SimVertexContainer::const_iterator VITER = simVC.begin(); VITER != simVC.end(); VITER++, kv++) {
0166         edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0167             << "SimVertex " << kv << " - Id:" << VITER->vertexId() << ", position = " << VITER->position()
0168             << ", parent SimTrack Id = " << VITER->parentIndex() << ", processType = " << VITER->processType();
0169       }
0170     }
0171   }
0172 
0173 }  // namespace
0174 
0175 class MuonToTrackingParticleAssociatorEDProducer : public edm::stream::EDProducer<> {
0176 public:
0177   explicit MuonToTrackingParticleAssociatorEDProducer(const edm::ParameterSet &);
0178   ~MuonToTrackingParticleAssociatorEDProducer() override;
0179 
0180   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0181 
0182 private:
0183   void produce(edm::Event &, const edm::EventSetup &) override;
0184 
0185   // ----------member data ---------------------------
0186   edm::ParameterSet const config_;
0187   MuonAssociatorByHitsHelper helper_;
0188   TrackerHitAssociator::Config trackerHitAssociatorConfig_;
0189   TrackerMuonHitExtractor hitExtractor_;
0190   GEMHitAssociator::Config gemHitAssociatorConfig_;
0191   RPCHitAssociator::Config rpcHitAssociatorConfig_;
0192   CSCHitAssociator::Config cscHitAssociatorConfig_;
0193   DTHitAssociator::Config dtHitAssociatorConfig_;
0194 
0195   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0196   std::unique_ptr<InputDumper> diagnostics_;
0197 };
0198 
0199 //
0200 // constants, enums and typedefs
0201 //
0202 
0203 //
0204 // static data member definitions
0205 //
0206 
0207 //
0208 // constructors and destructor
0209 //
0210 MuonToTrackingParticleAssociatorEDProducer::MuonToTrackingParticleAssociatorEDProducer(const edm::ParameterSet &iConfig)
0211     : config_(iConfig),
0212       helper_(iConfig),
0213       trackerHitAssociatorConfig_(iConfig, consumesCollector()),
0214       hitExtractor_(iConfig, consumesCollector()),
0215       gemHitAssociatorConfig_(iConfig, consumesCollector()),
0216       rpcHitAssociatorConfig_(iConfig, consumesCollector()),
0217       cscHitAssociatorConfig_(iConfig, consumesCollector()),
0218       dtHitAssociatorConfig_(iConfig, consumesCollector()),
0219       tTopoToken_(esConsumes()) {
0220   // register your products
0221   produces<reco::MuonToTrackingParticleAssociator>();
0222 
0223   edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
0224       << "\n constructing MuonToTrackingParticleAssociatorEDProducer"
0225       << "\n";
0226 
0227   if (iConfig.getUntrackedParameter<bool>("dumpInputCollections")) {
0228     diagnostics_ = std::make_unique<InputDumper>(iConfig, consumesCollector());
0229   }
0230 }
0231 
0232 MuonToTrackingParticleAssociatorEDProducer::~MuonToTrackingParticleAssociatorEDProducer() {
0233   // do anything here that needs to be done at desctruction time
0234   // (e.g. close files, deallocate resources etc.)
0235 }
0236 
0237 //
0238 // member functions
0239 //
0240 
0241 // ------------ method called to produce the data  ------------
0242 void MuonToTrackingParticleAssociatorEDProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0243   using namespace edm;
0244 
0245   hitExtractor_.init(iEvent);
0246 
0247   // Retrieve tracker topology from geometry
0248   const TrackerTopology *tTopo = &iSetup.getData(tTopoToken_);
0249 
0250   std::function<void(const TrackHitsCollection &, const TrackingParticleCollection &)> diagnostics;
0251   if (diagnostics_) {
0252     diagnostics_->read(iEvent);
0253     diagnostics = [this](const TrackHitsCollection &hC, const TrackingParticleCollection &pC) {
0254       diagnostics_->dump(hC, pC);
0255     };
0256   }
0257 
0258   auto impl = std::make_unique<MuonToTrackingParticleAssociatorByHitsImpl>(hitExtractor_,
0259                                                                            trackerHitAssociatorConfig_,
0260                                                                            cscHitAssociatorConfig_,
0261                                                                            dtHitAssociatorConfig_,
0262                                                                            rpcHitAssociatorConfig_,
0263                                                                            gemHitAssociatorConfig_,
0264                                                                            iEvent,
0265                                                                            iSetup,
0266                                                                            tTopo,
0267                                                                            diagnostics,
0268                                                                            &helper_);
0269   auto toPut = std::make_unique<reco::MuonToTrackingParticleAssociator>(std::move(impl));
0270   iEvent.put(std::move(toPut));
0271 }
0272 
0273 // ------------ method fills 'descriptions' with the allowed parameters for the
0274 // module  ------------
0275 void MuonToTrackingParticleAssociatorEDProducer::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0276   // The following says we do not know what parameters are allowed so do no
0277   // validation
0278   // Please change this to state exactly what you do use, even if it is no
0279   // parameters
0280   edm::ParameterSetDescription desc;
0281   desc.setUnknown();
0282   descriptions.addDefault(desc);
0283 }
0284 
0285 // define this as a plug-in
0286 DEFINE_FWK_MODULE(MuonToTrackingParticleAssociatorEDProducer);