Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-11 04:33:34

0001 // -*- C++ -*-
0002 ///bookLayer
0003 // Package:    Phase2ITValidateCluster
0004 // Class:      Phase2ITValidateCluster
0005 //
0006 /**\class Phase2ITValidateCluster Phase2ITValidateCluster.cc 
0007 
0008  Description: Validation plots tracker clusters. 
0009 
0010 */
0011 //
0012 // Author: Gabriel Ramirez, Suvankar Roy Chowdhury
0013 // Date: May 23, 2020
0014 //
0015 #include <memory>
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/ESHandle.h"
0019 #include "FWCore/Framework/interface/MakerMacros.h"
0020 #include "FWCore/Framework/interface/ESWatcher.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/ServiceRegistry/interface/Service.h"
0024 #include "FWCore/Utilities/interface/InputTag.h"
0025 
0026 #include "DataFormats/Common/interface/Handle.h"
0027 #include "DataFormats/DetId/interface/DetId.h"
0028 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0029 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0030 #include "DataFormats/Common/interface/DetSetVector.h"
0031 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
0032 #include "DataFormats/SiPixelDetId/interface/PixelChannelIdentifier.h"
0033 
0034 #include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
0035 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0036 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
0037 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0038 
0039 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0040 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0041 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0042 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0043 
0044 // DQM Histograming
0045 #include "DQMServices/Core/interface/MonitorElement.h"
0046 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0047 #include "DQMServices/Core/interface/DQMStore.h"
0048 
0049 class Phase2ITValidateCluster : public DQMEDAnalyzer {
0050 public:
0051   typedef std::map<unsigned int, std::vector<PSimHit>> SimHitsMap;
0052   typedef std::map<unsigned int, SimTrack> SimTracksMap;
0053 
0054   explicit Phase2ITValidateCluster(const edm::ParameterSet&);
0055   ~Phase2ITValidateCluster() override;
0056   void bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const& iSetup) override;
0057   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0058   void dqmBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) override;
0059   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0060 
0061 private:
0062   struct ClusterMEs {
0063     MonitorElement* deltaX_P = nullptr;
0064     MonitorElement* deltaY_P = nullptr;
0065     MonitorElement* deltaX_P_primary = nullptr;
0066     MonitorElement* deltaY_P_primary = nullptr;
0067   };
0068 
0069   void fillITHistos(const edm::Event& iEvent,
0070                     const std::vector<edm::Handle<edm::PSimHitContainer>>& simHits,
0071                     const std::map<unsigned int, SimTrack>& simTracks);
0072   void bookLayerHistos(DQMStore::IBooker& ibooker, uint32_t det_it, const std::string& subdir);
0073   std::vector<unsigned int> getSimTrackId(const edm::Handle<edm::DetSetVector<PixelDigiSimLink>>& pixelSimLinks,
0074                                           const DetId& detId,
0075                                           unsigned int channel);
0076 
0077   std::map<std::string, ClusterMEs> layerMEs_;
0078 
0079   edm::ParameterSet config_;
0080   double simtrackminpt_;
0081   std::vector<edm::EDGetTokenT<edm::PSimHitContainer>> simHitTokens_;
0082   edm::EDGetTokenT<edm::DetSetVector<PixelDigiSimLink>> simITLinksToken_;
0083   edm::EDGetTokenT<edm::SimTrackContainer> simTracksToken_;
0084   edm::EDGetTokenT<edmNew::DetSetVector<SiPixelCluster>> clustersToken_;
0085   std::vector<edm::InputTag> pSimHitSrc_;
0086   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> geomToken_;
0087   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;
0088   const TrackerGeometry* tkGeom_ = nullptr;
0089   const TrackerTopology* tTopo_ = nullptr;
0090 };
0091 #include "Validation/SiTrackerPhase2V/interface/TrackerPhase2ValidationUtil.h"
0092 #include "DQM/SiTrackerPhase2/interface/TrackerPhase2DQMUtil.h"
0093 
0094 //
0095 // constructors
0096 //
0097 
0098 Phase2ITValidateCluster::Phase2ITValidateCluster(const edm::ParameterSet& iConfig)
0099     : config_(iConfig),
0100       simtrackminpt_(config_.getParameter<double>("SimTrackMinPt")),
0101       simITLinksToken_(consumes<edm::DetSetVector<PixelDigiSimLink>>(
0102           config_.getParameter<edm::InputTag>("InnerTrackerDigiSimLinkSource"))),
0103       simTracksToken_(consumes<edm::SimTrackContainer>(config_.getParameter<edm::InputTag>("simtracks"))),
0104       clustersToken_(
0105           consumes<edmNew::DetSetVector<SiPixelCluster>>(config_.getParameter<edm::InputTag>("ClusterSource"))),
0106       pSimHitSrc_(config_.getParameter<std::vector<edm::InputTag>>("PSimHitSource")),
0107       geomToken_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord, edm::Transition::BeginRun>()),
0108       topoToken_(esConsumes<TrackerTopology, TrackerTopologyRcd, edm::Transition::BeginRun>()) {
0109   edm::LogInfo("Phase2ITValidateCluster") << ">>> Construct Phase2ITValidateCluster ";
0110   for (const auto& itag : pSimHitSrc_)
0111     simHitTokens_.push_back(consumes<edm::PSimHitContainer>(itag));
0112 }
0113 
0114 Phase2ITValidateCluster::~Phase2ITValidateCluster() {
0115   // do anything here that needs to be done at desctruction time
0116   // (e.g. close files, deallocate resources etc.)
0117   edm::LogInfo("Phase2ITValidateCluster") << ">>> Destroy Phase2ITValidateCluster ";
0118 }
0119 //
0120 // -- DQM Begin Run
0121 //
0122 void Phase2ITValidateCluster::dqmBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
0123   tkGeom_ = &iSetup.getData(geomToken_);
0124   tTopo_ = &iSetup.getData(topoToken_);
0125 }
0126 
0127 // -- Analyze
0128 //
0129 void Phase2ITValidateCluster::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0130   // Getting simHits
0131   std::vector<edm::Handle<edm::PSimHitContainer>> simHits;
0132   for (const auto& itoken : simHitTokens_) {
0133     const auto& simHitHandle = iEvent.getHandle(itoken);
0134     if (!simHitHandle.isValid())
0135       continue;
0136     simHits.emplace_back(simHitHandle);
0137   }
0138   // Get the SimTracks
0139   const auto& simTracksRaw = iEvent.getHandle(simTracksToken_);
0140   // Rearrange the simTracks for ease of use <simTrackID, simTrack>
0141   SimTracksMap simTracks;
0142   for (edm::SimTrackContainer::const_iterator simTrackIt(simTracksRaw->begin()); simTrackIt != simTracksRaw->end();
0143        ++simTrackIt) {
0144     if (simTrackIt->momentum().pt() > simtrackminpt_) {
0145       simTracks.emplace(simTrackIt->trackId(), *simTrackIt);
0146     }
0147   }
0148   fillITHistos(iEvent, simHits, simTracks);
0149 }
0150 
0151 void Phase2ITValidateCluster::fillITHistos(const edm::Event& iEvent,
0152                                            const std::vector<edm::Handle<edm::PSimHitContainer>>& simHits,
0153                                            const std::map<unsigned int, SimTrack>& simTracks) {
0154   // Getting the clusters
0155   const auto& clusterHandle = iEvent.getHandle(clustersToken_);
0156   // Getting PixelDigiSimLinks
0157   const auto& pixelSimLinksHandle = iEvent.getHandle(simITLinksToken_);
0158 
0159   for (const auto& DSVItr : *clusterHandle) {
0160     // Getting the id of detector unit
0161     uint32_t rawid = DSVItr.detId();
0162     DetId detId(rawid);
0163     const GeomDetUnit* geomDetUnit(tkGeom_->idToDetUnit(detId));
0164     if (!geomDetUnit)
0165       continue;
0166 
0167     std::string folderkey = phase2tkutil::getITHistoId(detId, tTopo_);
0168     for (const auto& clusterItr : DSVItr) {
0169       MeasurementPoint mpCluster(clusterItr.x(), clusterItr.y());
0170       Local3DPoint localPosCluster = geomDetUnit->topology().localPosition(mpCluster);
0171       // Get simTracks from the cluster
0172       std::vector<unsigned int> clusterSimTrackIds;
0173       for (int irow = clusterItr.minPixelRow(); irow <= clusterItr.maxPixelRow(); ++irow) {
0174         for (int icol = clusterItr.minPixelCol(); icol <= clusterItr.maxPixelCol(); ++icol) {
0175           uint32_t channel = PixelChannelIdentifier::pixelToChannel(irow, icol);
0176           std::vector<unsigned int> simTrackIds(getSimTrackId(pixelSimLinksHandle, detId, channel));
0177           for (auto it : simTrackIds) {
0178             bool add = true;
0179             for (unsigned int j = 0; j < clusterSimTrackIds.size(); ++j) {
0180               // only save simtrackids that are not present yet
0181               if (it == clusterSimTrackIds.at(j))
0182                 add = false;
0183             }
0184             if (add)
0185               clusterSimTrackIds.push_back(it);
0186           }
0187         }
0188       }
0189       std::sort(clusterSimTrackIds.begin(), clusterSimTrackIds.end());
0190       const PSimHit* closestSimHit = nullptr;
0191       float mind = 10000.;
0192       // Get the SimHit
0193       for (const auto& psimhitCont : simHits) {
0194         for (const auto& simhitIt : *psimhitCont) {
0195           if (rawid == simhitIt.detUnitId()) {
0196             auto it = std::lower_bound(clusterSimTrackIds.begin(), clusterSimTrackIds.end(), simhitIt.trackId());
0197             if (it != clusterSimTrackIds.end() && *it == simhitIt.trackId()) {
0198               float dx = simhitIt.localPosition().x() - localPosCluster.x();
0199               float dy = simhitIt.localPosition().y() - localPosCluster.y();
0200               float dist = dx * dx + dy * dy;
0201               if (!closestSimHit || dist < mind) {
0202                 mind = dist;
0203                 closestSimHit = &simhitIt;
0204               }
0205             }
0206           }
0207         }  //end loop over PSimhitcontainers
0208       }  //end loop over simHits
0209 
0210       if (!closestSimHit)
0211         continue;
0212       // only look at simhits from highpT tracks
0213       auto simTrackIt(simTracks.find(closestSimHit->trackId()));
0214       if (simTrackIt == simTracks.end())
0215         continue;
0216       Local3DPoint localPosSimHit(closestSimHit->localPosition());
0217       const double deltaX = phase2tkutil::cmtomicron * (localPosCluster.x() - localPosSimHit.x());
0218       const double deltaY = phase2tkutil::cmtomicron * (localPosCluster.y() - localPosSimHit.y());
0219 
0220       auto layerMEIt = layerMEs_.find(folderkey);
0221       if (layerMEIt == layerMEs_.end())
0222         continue;
0223 
0224       ClusterMEs& local_mes = layerMEIt->second;
0225       local_mes.deltaX_P->Fill(deltaX);
0226       local_mes.deltaY_P->Fill(deltaY);
0227       // Primary particles only
0228       if (phase2tkutil::isPrimary(simTrackIt->second, closestSimHit)) {
0229         local_mes.deltaX_P_primary->Fill(deltaX);
0230         local_mes.deltaY_P_primary->Fill(deltaY);
0231       }
0232     }
0233   }
0234 }
0235 
0236 //
0237 // -- Book Histograms
0238 //
0239 void Phase2ITValidateCluster::bookHistograms(DQMStore::IBooker& ibooker,
0240                                              edm::Run const& iRun,
0241                                              edm::EventSetup const& iSetup) {
0242   std::string top_folder = config_.getParameter<std::string>("TopFolderName");
0243   edm::LogInfo("Phase2ITValidateCluster") << " Booking Histograms in: " << top_folder;
0244 
0245   edm::ESWatcher<TrackerDigiGeometryRecord> theTkDigiGeomWatcher;
0246   if (theTkDigiGeomWatcher.check(iSetup)) {
0247     for (auto const& det_u : tkGeom_->detUnits()) {
0248       //Always check TrackerNumberingBuilder before changing this part
0249       if (!(det_u->subDetector() == GeomDetEnumerators::SubDetector::P2PXB ||
0250             det_u->subDetector() == GeomDetEnumerators::SubDetector::P2PXEC))
0251         continue;  //continue if not Pixel
0252       uint32_t detId_raw = det_u->geographicalId().rawId();
0253       bookLayerHistos(ibooker, detId_raw, top_folder);
0254     }
0255   }
0256 }
0257 
0258 //////////////////Layer Histo/////////////////////////////////
0259 void Phase2ITValidateCluster::bookLayerHistos(DQMStore::IBooker& ibooker, uint32_t det_id, const std::string& subdir) {
0260   std::string folderName = phase2tkutil::getITHistoId(det_id, tTopo_);
0261   if (folderName.empty()) {
0262     edm::LogWarning("Phase2ITValidateCluster") << ">>>> Invalid histo_id ";
0263     return;
0264   }
0265 
0266   if (layerMEs_.find(folderName) == layerMEs_.end()) {
0267     ibooker.cd();
0268     ibooker.setCurrentFolder(subdir + '/' + folderName);
0269     edm::LogInfo("Phase2TrackerValidateDigi") << " Booking Histograms in: " << subdir + '/' + folderName;
0270     ClusterMEs local_mes;
0271 
0272     local_mes.deltaX_P =
0273         phase2tkutil::book1DFromPSet(config_.getParameter<edm::ParameterSet>("Delta_X_Pixel"), ibooker);
0274 
0275     local_mes.deltaY_P =
0276         phase2tkutil::book1DFromPSet(config_.getParameter<edm::ParameterSet>("Delta_Y_Pixel"), ibooker);
0277 
0278     // Puting primary digis in a subfolder
0279     ibooker.setCurrentFolder(subdir + '/' + folderName + "/PrimarySimHits");
0280 
0281     local_mes.deltaX_P_primary =
0282         phase2tkutil::book1DFromPSet(config_.getParameter<edm::ParameterSet>("Delta_X_Pixel_Primary"), ibooker);
0283 
0284     local_mes.deltaY_P_primary =
0285         phase2tkutil::book1DFromPSet(config_.getParameter<edm::ParameterSet>("Delta_Y_Pixel_Primary"), ibooker);
0286     layerMEs_.emplace(folderName, local_mes);
0287   }
0288 }
0289 
0290 std::vector<unsigned int> Phase2ITValidateCluster::getSimTrackId(
0291     const edm::Handle<edm::DetSetVector<PixelDigiSimLink>>& pixelSimLinks, const DetId& detId, unsigned int channel) {
0292   std::vector<unsigned int> retvec;
0293   edm::DetSetVector<PixelDigiSimLink>::const_iterator DSViter(pixelSimLinks->find(detId));
0294   if (DSViter == pixelSimLinks->end())
0295     return retvec;
0296   for (edm::DetSet<PixelDigiSimLink>::const_iterator it = DSViter->data.begin(); it != DSViter->data.end(); ++it) {
0297     if (channel == it->channel()) {
0298       retvec.push_back(it->SimTrackId());
0299     }
0300   }
0301   return retvec;
0302 }
0303 
0304 void Phase2ITValidateCluster::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0305   edm::ParameterSetDescription desc;
0306   //for macro-pixel sensors
0307   {
0308     edm::ParameterSetDescription psd0;
0309     psd0.add<std::string>("name", "Delta_X_Pixel");
0310     psd0.add<std::string>("title", "#Delta X;Cluster resolution X coordinate [#mum]");
0311     psd0.add<bool>("switch", true);
0312     psd0.add<double>("xmax", 250.);
0313     psd0.add<double>("xmin", -250.);
0314     psd0.add<int>("NxBins", 100);
0315     desc.add<edm::ParameterSetDescription>("Delta_X_Pixel", psd0);
0316   }
0317   {
0318     edm::ParameterSetDescription psd0;
0319     psd0.add<std::string>("name", "Delta_Y_Pixel");
0320     psd0.add<std::string>("title", "#Delta Y ;Cluster resolution Y coordinate [#mum]");
0321     psd0.add<double>("xmin", -250.);
0322     psd0.add<double>("xmax", 250.);
0323     psd0.add<bool>("switch", true);
0324     psd0.add<int>("NxBins", 100);
0325     desc.add<edm::ParameterSetDescription>("Delta_Y_Pixel", psd0);
0326   }
0327   {
0328     edm::ParameterSetDescription psd0;
0329     psd0.add<std::string>("name", "Delta_X_Pixel_Primary");
0330     psd0.add<std::string>("title", "#Delta X ;cluster resolution X coordinate [#mum]");
0331     psd0.add<double>("xmin", -250.);
0332     psd0.add<double>("xmax", 250.);
0333     psd0.add<bool>("switch", true);
0334     psd0.add<int>("NxBins", 100);
0335     desc.add<edm::ParameterSetDescription>("Delta_X_Pixel_Primary", psd0);
0336   }
0337   {
0338     edm::ParameterSetDescription psd0;
0339     psd0.add<std::string>("name", "Delta_Y_Pixel_Primary");
0340     psd0.add<std::string>("title", "#Delta Y ;cluster resolution Y coordinate [#mum]");
0341     psd0.add<double>("xmin", -250.);
0342     psd0.add<double>("xmax", 250.);
0343     psd0.add<bool>("switch", true);
0344     psd0.add<int>("NxBins", 100);
0345     desc.add<edm::ParameterSetDescription>("Delta_Y_Pixel_Primary", psd0);
0346   }
0347 
0348   desc.add<std::string>("TopFolderName", "TrackerPhase2ITClusterV");
0349   desc.add<edm::InputTag>("ClusterSource", edm::InputTag("siPixelClusters"));
0350   desc.add<edm::InputTag>("InnerTrackerDigiSimLinkSource", edm::InputTag("simSiPixelDigis", "Pixel"));
0351   desc.add<edm::InputTag>("simtracks", edm::InputTag("g4SimHits"));
0352   desc.add<double>("SimTrackMinPt", 0.0);
0353   desc.add<std::vector<edm::InputTag>>("PSimHitSource",
0354                                        {
0355                                            edm::InputTag("g4SimHits:TrackerHitsPixelBarrelLowTof"),
0356                                            edm::InputTag("g4SimHits:TrackerHitsPixelBarrelHighTof"),
0357                                            edm::InputTag("g4SimHits:TrackerHitsPixelEndcapLowTof"),
0358                                            edm::InputTag("g4SimHits:TrackerHitsPixelEndcapHighTof"),
0359                                        });
0360   descriptions.add("Phase2ITValidateCluster", desc);
0361 }
0362 DEFINE_FWK_MODULE(Phase2ITValidateCluster);