Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // Package:    Phase2OTValidateTrackingRecHit
0002 // Class:      Phase2OTValidateTrackingRecHit
0003 //
0004 /**\class Phase2OTValidateTrackingRecHit Phase2OTValidateTrackingRecHit.cc 
0005  Description:  Standalone  Plugin for Phase2 RecHit validation
0006 */
0007 //
0008 // Author: Suvankar Roy Chowdhury
0009 // Date: March 2021
0010 //
0011 // system include files
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/ESHandle.h"
0014 #include "FWCore/Framework/interface/MakerMacros.h"
0015 #include "FWCore/Framework/interface/ESWatcher.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 #include "FWCore/Utilities/interface/InputTag.h"
0020 
0021 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0022 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0023 #include "Geometry/CommonDetUnit/interface/TrackerGeomDet.h"
0024 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0025 #include "Geometry/CommonDetUnit/interface/PixelGeomDetType.h"
0026 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0027 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0028 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0029 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0030 
0031 #include "DataFormats/Common/interface/Handle.h"
0032 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0033 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0034 #include "DataFormats/DetId/interface/DetId.h"
0035 #include "DataFormats/Common/interface/DetSetVector.h"
0036 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0037 #include "DataFormats/TrackReco/interface/Track.h"
0038 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0039 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0040 
0041 #include "DataFormats/TrackerRecHit2D/interface/Phase2TrackerRecHit1D.h"
0042 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0043 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
0044 #include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
0045 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0046 #include "SimTracker/TrackerHitAssociation/interface/TrackerHitAssociator.h"
0047 
0048 #include "Validation/SiTrackerPhase2V/interface/Phase2OTValidateRecHitBase.h"
0049 #include "DQM/SiTrackerPhase2/interface/TrackerPhase2DQMUtil.h"
0050 
0051 class Phase2OTValidateTrackingRecHit : public Phase2OTValidateRecHitBase {
0052 public:
0053   explicit Phase2OTValidateTrackingRecHit(const edm::ParameterSet&);
0054   ~Phase2OTValidateTrackingRecHit() override;
0055   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0056 
0057   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0058 
0059 private:
0060   void fillOTHistos(const edm::Event& iEvent,
0061                     const TrackerHitAssociator& associateRecHit,
0062                     const std::vector<edm::Handle<edm::PSimHitContainer>>& simHits,
0063                     const std::map<unsigned int, SimTrack>& selectedSimTrackMap);
0064 
0065   edm::ParameterSet config_;
0066   TrackerHitAssociator::Config trackerHitAssociatorConfig_;
0067   const double simtrackminpt_;
0068   const edm::EDGetTokenT<reco::TrackCollection> tokenTracks_;
0069   const edm::EDGetTokenT<edm::SimTrackContainer> simTracksToken_;
0070   std::vector<edm::EDGetTokenT<edm::PSimHitContainer>> simHitTokens_;
0071 };
0072 
0073 //
0074 // constructors
0075 //
0076 Phase2OTValidateTrackingRecHit::Phase2OTValidateTrackingRecHit(const edm::ParameterSet& iConfig)
0077     : Phase2OTValidateRecHitBase(iConfig),
0078       config_(iConfig),
0079       trackerHitAssociatorConfig_(iConfig, consumesCollector()),
0080       simtrackminpt_(iConfig.getParameter<double>("SimTrackMinPt")),
0081       tokenTracks_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracksSrc"))),
0082       simTracksToken_(consumes<edm::SimTrackContainer>(iConfig.getParameter<edm::InputTag>("simTracksSrc"))) {
0083   edm::LogInfo("Phase2OTValidateTrackingRecHit") << ">>> Construct Phase2OTValidateTrackingRecHit ";
0084   for (const auto& itag : config_.getParameter<std::vector<edm::InputTag>>("PSimHitSource"))
0085     simHitTokens_.push_back(consumes<edm::PSimHitContainer>(itag));
0086 }
0087 
0088 //
0089 // destructor
0090 //
0091 Phase2OTValidateTrackingRecHit::~Phase2OTValidateTrackingRecHit() {
0092   // do anything here that needs to be done at desctruction time
0093   // (e.g. close files, deallocate resources etc.)
0094   edm::LogInfo("Phase2OTValidateTrackingRecHit") << ">>> Destroy Phase2OTValidateTrackingRecHit ";
0095 }
0096 
0097 //
0098 // -- Analyze
0099 //
0100 void Phase2OTValidateTrackingRecHit::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0101   std::vector<edm::Handle<edm::PSimHitContainer>> simHits;
0102   for (const auto& itoken : simHitTokens_) {
0103     const auto& simHitHandle = iEvent.getHandle(itoken);
0104     if (!simHitHandle.isValid())
0105       continue;
0106     simHits.emplace_back(simHitHandle);
0107   }
0108   // Get the SimTracks and push them in a map of id, SimTrack
0109   const auto& simTracks = iEvent.getHandle(simTracksToken_);
0110   std::map<unsigned int, SimTrack> selectedSimTrackMap;
0111   for (edm::SimTrackContainer::const_iterator simTrackIt(simTracks->begin()); simTrackIt != simTracks->end();
0112        ++simTrackIt) {
0113     if (simTrackIt->momentum().pt() > simtrackminpt_) {
0114       selectedSimTrackMap.insert(std::make_pair(simTrackIt->trackId(), *simTrackIt));
0115     }
0116   }
0117   TrackerHitAssociator associateRecHit(iEvent, trackerHitAssociatorConfig_);
0118   fillOTHistos(iEvent, associateRecHit, simHits, selectedSimTrackMap);
0119 }
0120 
0121 void Phase2OTValidateTrackingRecHit::fillOTHistos(const edm::Event& iEvent,
0122                                                   const TrackerHitAssociator& associateRecHit,
0123                                                   const std::vector<edm::Handle<edm::PSimHitContainer>>& simHits,
0124                                                   const std::map<unsigned int, SimTrack>& selectedSimTrackMap) {
0125   const auto& tracks = iEvent.getHandle(tokenTracks_);
0126   if (!tracks.isValid())
0127     return;
0128   std::map<std::string, unsigned int> nrechitLayerMapP_primary;
0129   std::map<std::string, unsigned int> nrechitLayerMapS_primary;
0130   // Loop over tracks
0131   for (const auto& track : *tracks) {
0132     for (const auto& hit : track.recHits()) {
0133       // Get the detector unit's id
0134       if (!hit->isValid())
0135         continue;
0136       auto detId = hit->geographicalId();
0137       // check that we are in the pixel
0138       auto subdetid = (detId.subdetId());
0139       if (subdetid == PixelSubdetector::PixelBarrel || subdetid == PixelSubdetector::PixelEndcap)
0140         continue;
0141 
0142       // determine the detector we are in
0143       TrackerGeometry::ModuleType mType = tkGeom_->getDetectorType(detId);
0144       std::string key = phase2tkutil::getOTHistoId(detId.rawId(), tTopo_);
0145       if (mType == TrackerGeometry::ModuleType::Ph2PSP) {
0146         if (nrechitLayerMapP_primary.find(key) == nrechitLayerMapP_primary.end()) {
0147           nrechitLayerMapP_primary.emplace(key, 1);
0148         } else {
0149           nrechitLayerMapP_primary[key] += 1;
0150         }
0151       } else if (mType == TrackerGeometry::ModuleType::Ph2PSS || mType == TrackerGeometry::ModuleType::Ph2SS) {
0152         if (nrechitLayerMapS_primary.find(key) == nrechitLayerMapS_primary.end()) {
0153           nrechitLayerMapS_primary.emplace(key, 1);
0154         } else {
0155           nrechitLayerMapS_primary[key] += 1;
0156         }
0157       }
0158       //GetSimHits
0159       const Phase2TrackerRecHit1D* rechit = dynamic_cast<const Phase2TrackerRecHit1D*>(hit);
0160       if (!rechit) {
0161         edm::LogError("Phase2OTValidateTrackingRecHit")
0162             << "Cannot cast tracking rechit to Phase2TrackerRecHit1D!" << std::endl;
0163         continue;
0164       }
0165       const std::vector<SimHitIdpr>& matchedId = associateRecHit.associateHitId(*rechit);
0166       const PSimHit* simhitClosest = nullptr;
0167       LocalPoint lp = rechit->localPosition();
0168       float mind = 1e4;
0169       for (const auto& simHitCol : simHits) {
0170         for (const auto& simhitIt : *simHitCol) {
0171           if (detId.rawId() != simhitIt.detUnitId())
0172             continue;
0173           for (auto& mId : matchedId) {
0174             if (simhitIt.trackId() == mId.first) {
0175               float dx = simhitIt.localPosition().x() - lp.x();
0176               float dy = simhitIt.localPosition().y() - lp.y();
0177               float dist = dx * dx + dy * dy;
0178               if (!simhitClosest || dist < mind) {
0179                 mind = dist;
0180                 simhitClosest = &simhitIt;
0181               }
0182             }
0183           }
0184         }  //end loop over PSimhitcontainers
0185       }    //end loop over simHits
0186       if (!simhitClosest)
0187         continue;
0188       fillOTRecHitHistos(
0189           simhitClosest, rechit, selectedSimTrackMap, nrechitLayerMapP_primary, nrechitLayerMapS_primary);
0190 
0191     }  //end loop over rechits of a track
0192   }    //End loop over tracks
0193 
0194   //fill nRecHits per event
0195   //fill nRecHit counter per layer
0196   for (auto& lme : nrechitLayerMapP_primary) {
0197     layerMEs_[lme.first].numberRecHitsprimary_P->Fill(nrechitLayerMapP_primary[lme.first]);
0198   }
0199   for (auto& lme : nrechitLayerMapS_primary) {
0200     layerMEs_[lme.first].numberRecHitsprimary_S->Fill(nrechitLayerMapS_primary[lme.first]);
0201   }
0202 }
0203 
0204 void Phase2OTValidateTrackingRecHit::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0205   edm::ParameterSetDescription desc;
0206   // call the base fillPsetDescription for the plots bookings
0207   Phase2OTValidateRecHitBase::fillPSetDescription(desc);
0208 
0209   //for macro-pixel sensors
0210   ///////
0211   desc.add<edm::InputTag>("SimVertexSource", edm::InputTag("g4SimHits"));
0212   desc.add<bool>("associatePixel", false);
0213   desc.add<bool>("associateHitbySimTrack", true);
0214   desc.add<bool>("Verbosity", false);
0215   desc.add<bool>("associateStrip", true);
0216   desc.add<edm::InputTag>("phase2TrackerSimLinkSrc", edm::InputTag("simSiPixelDigis", "Tracker"));
0217   desc.add<bool>("associateRecoTracks", false);
0218   desc.add<edm::InputTag>("pixelSimLinkSrc", edm::InputTag("simSiPixelDigis", "Pixel"));
0219   desc.add<bool>("usePhase2Tracker", true);
0220   desc.add<edm::InputTag>("rechitsSrc", edm::InputTag("siPhase2RecHits"));
0221   desc.add<edm::InputTag>("simTracksSrc", edm::InputTag("g4SimHits"));
0222   desc.add<double>("SimTrackMinPt", 2.0);
0223   desc.add<std::vector<edm::InputTag>>("PSimHitSource",
0224                                        {
0225                                            edm::InputTag("g4SimHits:TrackerHitsTIBLowTof"),
0226                                            edm::InputTag("g4SimHits:TrackerHitsTIBHighTof"),
0227                                            edm::InputTag("g4SimHits:TrackerHitsTIDLowTof"),
0228                                            edm::InputTag("g4SimHits:TrackerHitsTIDHighTof"),
0229                                            edm::InputTag("g4SimHits:TrackerHitsTOBLowTof"),
0230                                            edm::InputTag("g4SimHits:TrackerHitsTOBHighTof"),
0231                                            edm::InputTag("g4SimHits:TrackerHitsTECLowTof"),
0232                                            edm::InputTag("g4SimHits:TrackerHitsTECHighTof"),
0233                                            edm::InputTag("g4SimHits:TrackerHitsPixelBarrelLowTof"),
0234                                            edm::InputTag("g4SimHits:TrackerHitsPixelBarrelHighTof"),
0235                                            edm::InputTag("g4SimHits:TrackerHitsPixelEndcapLowTof"),
0236                                            edm::InputTag("g4SimHits:TrackerHitsPixelEndcapHighTof"),
0237                                        });
0238 
0239   desc.add<std::vector<std::string>>("ROUList",
0240                                      {"TrackerHitsPixelBarrelLowTof",
0241                                       "TrackerHitsPixelBarrelHighTof",
0242                                       "TrackerHitsTIBLowTof",
0243                                       "TrackerHitsTIBHighTof",
0244                                       "TrackerHitsTIDLowTof",
0245                                       "TrackerHitsTIDHighTof",
0246                                       "TrackerHitsTOBLowTof",
0247                                       "TrackerHitsTOBHighTof",
0248                                       "TrackerHitsPixelEndcapLowTof",
0249                                       "TrackerHitsPixelEndcapHighTof",
0250                                       "TrackerHitsTECLowTof",
0251                                       "TrackerHitsTECHighTof"});
0252   desc.add<edm::InputTag>("tracksSrc", edm::InputTag("generalTracks"));
0253   desc.add<std::string>("TopFolderName", "TrackerPhase2OTTrackingRecHitV");
0254   descriptions.add("Phase2OTValidateTrackingRecHit", desc);
0255 }
0256 
0257 //define this as a plug-in
0258 DEFINE_FWK_MODULE(Phase2OTValidateTrackingRecHit);