Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-08-09 23:47:34

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/VertexFinder
0004 // Class:      TPStubValueMapProducer
0005 //
0006 /**\class TPStubValueMapProducer TPStubValueMapProducer.cc L1Trigger/VertexFinder/plugins/TPStubValueMapProducer.cc
0007 
0008  Description: Produces an two value maps
0009   - a map which stores a TP object for every edm::Ptr<TrackingParticle>
0010   - a map which stores a Stub object for every TTStubRef
0011 
0012  Implementation:
0013      [Notes on implementation]
0014 */
0015 //
0016 // Original Author:  Alexx Perloff
0017 //         Created:  Mon, 08 Feb 2021 06:11:00 GMT
0018 //
0019 //
0020 
0021 // system include files
0022 #include <algorithm>
0023 #include <memory>
0024 #include <string>
0025 #include <vector>
0026 
0027 // user include files
0028 #include "DataFormats/Common/interface/ValueMap.h"
0029 #include "DataFormats/L1TrackTrigger/interface/TTStub.h"
0030 #include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
0031 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0032 #include "FWCore/Framework/interface/Frameworkfwd.h"
0033 #include "FWCore/Framework/interface/global/EDProducer.h"
0034 #include "FWCore/Framework/interface/Event.h"
0035 #include "FWCore/Framework/interface/MakerMacros.h"
0036 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0037 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0038 #include "FWCore/Utilities/interface/StreamID.h"
0039 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0040 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0041 #include "L1Trigger/VertexFinder/interface/AnalysisSettings.h"
0042 #include "L1Trigger/VertexFinder/interface/TP.h"
0043 #include "L1Trigger/VertexFinder/interface/Stub.h"
0044 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0045 #include "SimDataFormats/Associations/interface/TTClusterAssociationMap.h"
0046 #include "SimDataFormats/Associations/interface/TTStubAssociationMap.h"
0047 #include "SimDataFormats/Associations/interface/TTTrackAssociationMap.h"
0048 
0049 //
0050 // class declaration
0051 //
0052 
0053 class TPStubValueMapProducer : public edm::global::EDProducer<> {
0054 public:
0055   explicit TPStubValueMapProducer(const edm::ParameterSet&);
0056   ~TPStubValueMapProducer() override;
0057 
0058   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0059 
0060 private:
0061   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0062 
0063   // ----------constants, enums and typedefs ---------
0064   typedef edm::Ptr<TrackingParticle> TrackingParticlePtr;
0065   typedef edmNew::DetSet<TTStub<Ref_Phase2TrackerDigi_>> DetSet;
0066   typedef edmNew::DetSetVector<TTStub<Ref_Phase2TrackerDigi_>> DetSetVec;
0067   typedef edm::Ref<DetSetVec, TTStub<Ref_Phase2TrackerDigi_>> TTStubRef;
0068   typedef TTStubAssociationMap<Ref_Phase2TrackerDigi_> TTStubAssMap;
0069   typedef TTClusterAssociationMap<Ref_Phase2TrackerDigi_> TTClusterAssMap;
0070   typedef TTTrackAssociationMap<Ref_Phase2TrackerDigi_> TTTrackAssMap;
0071 
0072   // ----------member data ---------------------------
0073   const std::vector<std::string> outputCollectionNames_;
0074   l1tVertexFinder::AnalysisSettings settings_;
0075   edm::EDGetTokenT<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>> l1TracksMapToken_;
0076   const edm::EDGetTokenT<TrackingParticleCollection> tpToken_;
0077   const edm::EDGetTokenT<DetSetVec> stubToken_;
0078   const edm::EDGetTokenT<TTStubAssMap> stubTruthToken_;
0079   const edm::EDGetTokenT<TTClusterAssMap> clusterTruthToken_;
0080   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0081   edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> tGeomToken_;
0082 };
0083 
0084 //
0085 // constructors and destructor
0086 //
0087 TPStubValueMapProducer::TPStubValueMapProducer(const edm::ParameterSet& iConfig)
0088     : outputCollectionNames_(iConfig.getParameter<std::vector<std::string>>("outputCollectionNames")),
0089       settings_(iConfig),
0090       l1TracksMapToken_(consumes<TTTrackAssMap>(iConfig.getParameter<edm::InputTag>("l1TracksTruthMapInputTags"))),
0091       tpToken_(consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("tpInputTag"))),
0092       stubToken_(consumes<DetSetVec>(iConfig.getParameter<edm::InputTag>("stubInputTag"))),
0093       stubTruthToken_(consumes<TTStubAssMap>(iConfig.getParameter<edm::InputTag>("stubTruthInputTag"))),
0094       clusterTruthToken_(consumes<TTClusterAssMap>(iConfig.getParameter<edm::InputTag>("clusterTruthInputTag"))),
0095       tTopoToken_(esConsumes<TrackerTopology, TrackerTopologyRcd>(edm::ESInputTag("", ""))),
0096       tGeomToken_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>(edm::ESInputTag("", ""))) {
0097   // Define EDM output to be written to file (if required)
0098   produces<TrackingParticleCollection>();
0099   produces<edm::ValueMap<l1tVertexFinder::TP>>(outputCollectionNames_[0]);
0100   produces<edm::ValueMap<l1tVertexFinder::TP>>(outputCollectionNames_[1]);
0101   produces<std::vector<l1tVertexFinder::TP>>(outputCollectionNames_[2]);
0102 }
0103 
0104 TPStubValueMapProducer::~TPStubValueMapProducer() {}
0105 
0106 //
0107 // member functions
0108 //
0109 
0110 // ------------ method called to produce the data  ------------
0111 void TPStubValueMapProducer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0112   auto vTrackingParticles = std::make_unique<TrackingParticleCollection>();
0113 
0114   edm::Handle<TTTrackAssMap> mcTruthTTTrackHandle;
0115   edm::Handle<TrackingParticleCollection> tpHandle;
0116   edm::Handle<DetSetVec> ttStubHandle;
0117   edm::Handle<TTStubAssMap> mcTruthTTStubHandle;
0118   edm::Handle<TTClusterAssMap> mcTruthTTClusterHandle;
0119   iEvent.getByToken(l1TracksMapToken_, mcTruthTTTrackHandle);
0120   iEvent.getByToken(tpToken_, tpHandle);
0121   iEvent.getByToken(stubToken_, ttStubHandle);
0122   iEvent.getByToken(stubTruthToken_, mcTruthTTStubHandle);
0123   iEvent.getByToken(clusterTruthToken_, mcTruthTTClusterHandle);
0124 
0125   // Produce the vector of TP for the edm::Ref<TrackingParticle>->TP value map
0126   unsigned int nTP = tpHandle->size();
0127   auto vTPs = std::make_unique<std::vector<l1tVertexFinder::TP>>();
0128   auto vTPsUse = std::make_unique<std::vector<l1tVertexFinder::TP>>();
0129   vTPs->reserve(nTP);
0130   vTPsUse->reserve(nTP);
0131   std::set<edm::Ptr<TrackingParticle>> sTPs;
0132   for (unsigned int i = 0; i < nTP; i++) {
0133     TrackingParticlePtr tpPtr(tpHandle, i);
0134     // Store the TrackingParticle info, using class TP to provide easy access to the most useful info.
0135     l1tVertexFinder::TP tp(tpPtr, settings_);
0136     // Only bother storing tp if it could be useful for tracking efficiency or fake rate measurements.
0137     // Also create map relating edm::Ptr<TrackingParticle> to TP.
0138     if (tp.use()) {
0139       vTrackingParticles->push_back(tpHandle->at(i));
0140       vTPsUse->push_back(tp);
0141       sTPs.insert(tpPtr);
0142     }
0143     vTPs->push_back(tp);
0144   }
0145 
0146   auto vAllMatchedTPs = std::make_unique<std::vector<l1tVertexFinder::TP>>(*vTPsUse);
0147   for (auto& entry : mcTruthTTTrackHandle->getTTTrackToTrackingParticleMap()) {
0148     if (sTPs.count(entry.second) == 0) {
0149       vAllMatchedTPs->push_back(l1tVertexFinder::TP(entry.second, settings_));
0150     }
0151   }
0152 
0153   // Get the tracker geometry info needed to unpack the stub info.
0154   const TrackerTopology& tTopo = iSetup.getData(tTopoToken_);
0155   const TrackerGeometry& tGeom = iSetup.getData(tGeomToken_);
0156 
0157   const TrackerTopology* tTopology = &tTopo;
0158   const TrackerGeometry* tGeometry = &tGeom;
0159 
0160   //Create the vector of Stub for the TTStubRef->Stub value map
0161   unsigned int nStubs = ttStubHandle->size();
0162   auto vAllStubs = std::make_unique<std::vector<l1tVertexFinder::Stub>>();
0163   vAllStubs->reserve(nStubs);
0164   for (DetSetVec::const_iterator p_module = ttStubHandle->begin(); p_module != ttStubHandle->end(); p_module++) {
0165     for (DetSet::const_iterator p_ttstub = p_module->begin(); p_ttstub != p_module->end(); p_ttstub++) {
0166       TTStubRef ttStubRef = edmNew::makeRefTo(ttStubHandle, p_ttstub);
0167       // Store the Stub info, using class Stub to provide easy access to the most useful info.
0168       l1tVertexFinder::Stub stub(ttStubRef, settings_, tGeometry, tTopology);
0169       // Also fill truth associating stubs to tracking particles.
0170       stub.fillTruth(mcTruthTTStubHandle, mcTruthTTClusterHandle);
0171       vAllStubs->push_back(stub);
0172     }
0173   }
0174 
0175   //Set the Stubs associate to each TP
0176   std::map<const TrackingParticlePtr, std::vector<l1tVertexFinder::Stub>> tpStubMap;
0177   for (const l1tVertexFinder::TP& tp : *vTPs)
0178     tpStubMap[tp.getTrackingParticle()] = std::vector<l1tVertexFinder::Stub>();
0179   for (const l1tVertexFinder::Stub& stub : *vAllStubs) {
0180     for (const TrackingParticlePtr& tp : stub.assocTPs()) {
0181       tpStubMap[tp].push_back(stub);
0182     }
0183   }
0184   for (l1tVertexFinder::TP& tp : *vTPs) {
0185     assert(tpStubMap.count(tp.getTrackingParticle()) == 1);
0186     tp.setMatchingStubs(tpStubMap.find(tp.getTrackingParticle())->second);
0187   }
0188 
0189   //Put the products into the event
0190   // Modeled after: https://github.com/cms-sw/cmssw/blob/master/PhysicsTools/SelectorUtils/interface/VersionedIdProducer.h
0191 
0192   // Collections of products
0193   auto vTrackingParticlesHandle = iEvent.put(std::move(vTrackingParticles));
0194   auto vAllMatchedTPsHandle = iEvent.put(std::move(vAllMatchedTPs), outputCollectionNames_[2]);
0195 
0196   // Value maps to TP/Stub
0197   auto TPV = std::make_unique<edm::ValueMap<l1tVertexFinder::TP>>();
0198   edm::ValueMap<l1tVertexFinder::TP>::Filler fillerTP(*TPV);
0199   fillerTP.insert(tpHandle, vTPs->begin(), vTPs->end());
0200   fillerTP.fill();
0201   iEvent.put(std::move(TPV), outputCollectionNames_[0]);
0202 
0203   auto TPuseV = std::make_unique<edm::ValueMap<l1tVertexFinder::TP>>();
0204   edm::ValueMap<l1tVertexFinder::TP>::Filler fillerTPuse(*TPuseV);
0205   fillerTPuse.insert(vTrackingParticlesHandle, vTPsUse->begin(), vTPsUse->end());
0206   fillerTPuse.fill();
0207   iEvent.put(std::move(TPuseV), outputCollectionNames_[1]);
0208 }
0209 
0210 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0211 void TPStubValueMapProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0212   //The following says we do not know what parameters are allowed so do no validation
0213   // Please change this to state exactly what you do use, even if it is no parameters
0214   edm::ParameterSetDescription desc;
0215   desc.setUnknown();
0216   descriptions.addDefault(desc);
0217 }
0218 
0219 //define this as a plug-in
0220 DEFINE_FWK_MODULE(TPStubValueMapProducer);