Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:16

0001 // -*- C++ -*-
0002 // Package:    SiPixelCompareVertexSoA
0003 // Class:      SiPixelCompareVertexSoA
0004 //
0005 /**\class SiPixelCompareVertexSoA SiPixelCompareVertexSoA.cc
0006 */
0007 //
0008 // Author: Suvankar Roy Chowdhury
0009 //
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/MakerMacros.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "DataFormats/Common/interface/Handle.h"
0017 // DQM Histograming
0018 #include "DQMServices/Core/interface/MonitorElement.h"
0019 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0020 #include "DQMServices/Core/interface/DQMStore.h"
0021 #include "CUDADataFormats/Vertex/interface/ZVertexSoAHeterogeneousHost.h"
0022 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0023 
0024 class SiPixelCompareVertexSoA : public DQMEDAnalyzer {
0025 public:
0026   using IndToEdm = std::vector<uint16_t>;
0027   explicit SiPixelCompareVertexSoA(const edm::ParameterSet&);
0028   ~SiPixelCompareVertexSoA() override = default;
0029   void bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const& iSetup) override;
0030   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0031   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0032 
0033 private:
0034   const edm::EDGetTokenT<ZVertexSoAHost> tokenSoAVertexCPU_;
0035   const edm::EDGetTokenT<ZVertexSoAHost> tokenSoAVertexGPU_;
0036   const edm::EDGetTokenT<reco::BeamSpot> tokenBeamSpot_;
0037   const std::string topFolderName_;
0038   const float dzCut_;
0039   MonitorElement* hnVertex_;
0040   MonitorElement* hx_;
0041   MonitorElement* hy_;
0042   MonitorElement* hz_;
0043   MonitorElement* hchi2_;
0044   MonitorElement* hchi2oNdof_;
0045   MonitorElement* hptv2_;
0046   MonitorElement* hntrks_;
0047   MonitorElement* hxdiff_;
0048   MonitorElement* hydiff_;
0049   MonitorElement* hzdiff_;
0050 };
0051 
0052 //
0053 // constructors
0054 //
0055 
0056 // Note tokenSoAVertexGPU_ contains data copied from device to host, hence is a HostCollection
0057 SiPixelCompareVertexSoA::SiPixelCompareVertexSoA(const edm::ParameterSet& iConfig)
0058     : tokenSoAVertexCPU_(consumes<ZVertexSoAHost>(iConfig.getParameter<edm::InputTag>("pixelVertexSrcCPU"))),
0059       tokenSoAVertexGPU_(consumes<ZVertexSoAHost>(iConfig.getParameter<edm::InputTag>("pixelVertexSrcGPU"))),
0060       tokenBeamSpot_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpotSrc"))),
0061       topFolderName_(iConfig.getParameter<std::string>("topFolderName")),
0062       dzCut_(iConfig.getParameter<double>("dzCut")) {}
0063 
0064 //
0065 // -- Analyze
0066 //
0067 void SiPixelCompareVertexSoA::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0068   const auto& vsoaHandleCPU = iEvent.getHandle(tokenSoAVertexCPU_);
0069   const auto& vsoaHandleGPU = iEvent.getHandle(tokenSoAVertexGPU_);
0070   if (not vsoaHandleCPU or not vsoaHandleGPU) {
0071     edm::LogWarning out("SiPixelCompareVertexSoA");
0072     if (not vsoaHandleCPU) {
0073       out << "reference (cpu) tracks not found; ";
0074     }
0075     if (not vsoaHandleGPU) {
0076       out << "target (gpu) tracks not found; ";
0077     }
0078     out << "the comparison will not run.";
0079     return;
0080   }
0081 
0082   auto const& vsoaCPU = *vsoaHandleCPU;
0083   int nVerticesCPU = vsoaCPU.view().nvFinal();
0084   auto const& vsoaGPU = *vsoaHandleGPU;
0085   int nVerticesGPU = vsoaGPU.view().nvFinal();
0086 
0087   auto bsHandle = iEvent.getHandle(tokenBeamSpot_);
0088   float x0 = 0., y0 = 0., z0 = 0., dxdz = 0., dydz = 0.;
0089   if (!bsHandle.isValid()) {
0090     edm::LogWarning("SiPixelCompareVertexSoA") << "No beamspot found. returning vertexes with (0,0,Z) ";
0091   } else {
0092     const reco::BeamSpot& bs = *bsHandle;
0093     x0 = bs.x0();
0094     y0 = bs.y0();
0095     z0 = bs.z0();
0096     dxdz = bs.dxdz();
0097     dydz = bs.dydz();
0098   }
0099 
0100   for (int ivc = 0; ivc < nVerticesCPU; ivc++) {
0101     auto sic = vsoaCPU.view()[ivc].sortInd();
0102     auto zc = vsoaCPU.view()[sic].zv();
0103     auto xc = x0 + dxdz * zc;
0104     auto yc = y0 + dydz * zc;
0105     zc += z0;
0106 
0107     auto ndofCPU = vsoaCPU.view()[sic].ndof();
0108     auto chi2CPU = vsoaCPU.view()[sic].chi2();
0109 
0110     const int32_t notFound = -1;
0111     int32_t closestVtxidx = notFound;
0112     float mindz = dzCut_;
0113 
0114     for (int ivg = 0; ivg < nVerticesGPU; ivg++) {
0115       auto sig = vsoaGPU.view()[ivg].sortInd();
0116       auto zgc = vsoaGPU.view()[sig].zv() + z0;
0117       auto zDist = std::abs(zc - zgc);
0118       //insert some matching condition
0119       if (zDist > dzCut_)
0120         continue;
0121       if (mindz > zDist) {
0122         mindz = zDist;
0123         closestVtxidx = sig;
0124       }
0125     }
0126     if (closestVtxidx == notFound)
0127       continue;
0128 
0129     auto zg = vsoaGPU.view()[closestVtxidx].zv();
0130     auto xg = x0 + dxdz * zg;
0131     auto yg = y0 + dydz * zg;
0132     zg += z0;
0133     auto ndofGPU = vsoaGPU.view()[closestVtxidx].ndof();
0134     auto chi2GPU = vsoaGPU.view()[closestVtxidx].chi2();
0135 
0136     hx_->Fill(xc - x0, xg - x0);
0137     hy_->Fill(yc - y0, yg - y0);
0138     hz_->Fill(zc, zg);
0139     hxdiff_->Fill(xc - xg);
0140     hydiff_->Fill(yc - yg);
0141     hzdiff_->Fill(zc - zg);
0142     hchi2_->Fill(chi2CPU, chi2GPU);
0143     hchi2oNdof_->Fill(chi2CPU / ndofCPU, chi2GPU / ndofGPU);
0144     hptv2_->Fill(vsoaCPU.view()[sic].ptv2(), vsoaGPU.view()[closestVtxidx].ptv2());
0145     hntrks_->Fill(ndofCPU + 1, ndofGPU + 1);
0146   }
0147   hnVertex_->Fill(nVerticesCPU, nVerticesGPU);
0148 }
0149 
0150 //
0151 // -- Book Histograms
0152 //
0153 void SiPixelCompareVertexSoA::bookHistograms(DQMStore::IBooker& ibooker,
0154                                              edm::Run const& iRun,
0155                                              edm::EventSetup const& iSetup) {
0156   ibooker.cd();
0157   ibooker.setCurrentFolder(topFolderName_);
0158 
0159   // FIXME: all the 2D correlation plots are quite heavy in terms of memory consumption, so a as soon as DQM supports either TH2I or THnSparse
0160   // these should be moved to a less resource consuming format
0161   hnVertex_ = ibooker.book2I("nVertex", "# of Vertices;CPU;GPU", 101, -0.5, 100.5, 101, -0.5, 100.5);
0162   hx_ = ibooker.book2I("vx", "Vertez x - Beamspot x;CPU;GPU", 50, -0.1, 0.1, 50, -0.1, 0.1);
0163   hy_ = ibooker.book2I("vy", "Vertez y - Beamspot y;CPU;GPU", 50, -0.1, 0.1, 50, -0.1, 0.1);
0164   hz_ = ibooker.book2I("vz", "Vertez z;CPU;GPU", 30, -30., 30., 30, -30., 30.);
0165   hchi2_ = ibooker.book2I("chi2", "Vertex chi-squared;CPU;GPU", 40, 0., 20., 40, 0., 20.);
0166   hchi2oNdof_ = ibooker.book2I("chi2oNdof", "Vertex chi-squared/Ndof;CPU;GPU", 40, 0., 20., 40, 0., 20.);
0167   hptv2_ = ibooker.book2I("ptsq", "Vertex #sum (p_{T})^{2};CPU;GPU", 200, 0., 200., 200, 0., 200.);
0168   hntrks_ = ibooker.book2I("ntrk", "#tracks associated;CPU;GPU", 100, -0.5, 99.5, 100, -0.5, 99.5);
0169   hntrks_ = ibooker.book2I("ntrk", "#tracks associated;CPU;GPU", 100, -0.5, 99.5, 100, -0.5, 99.5);
0170   hxdiff_ = ibooker.book1D("vxdiff", ";Vertex x difference (CPU - GPU);#entries", 100, -0.001, 0.001);
0171   hydiff_ = ibooker.book1D("vydiff", ";Vertex y difference (CPU - GPU);#entries", 100, -0.001, 0.001);
0172   hzdiff_ = ibooker.book1D("vzdiff", ";Vertex z difference (CPU - GPU);#entries", 100, -2.5, 2.5);
0173 }
0174 
0175 void SiPixelCompareVertexSoA::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0176   // monitorpixelVertexSoA
0177   edm::ParameterSetDescription desc;
0178   desc.add<edm::InputTag>("pixelVertexSrcCPU", edm::InputTag("pixelVerticesSoA@cpu"));
0179   desc.add<edm::InputTag>("pixelVertexSrcGPU", edm::InputTag("pixelVerticesSoA@cuda"));
0180   desc.add<edm::InputTag>("beamSpotSrc", edm::InputTag("offlineBeamSpot"));
0181   desc.add<std::string>("topFolderName", "SiPixelHeterogeneous/PixelVertexCompareSoAGPUvsCPU");
0182   desc.add<double>("dzCut", 1.);
0183   descriptions.addWithDefaultLabel(desc);
0184 }
0185 
0186 DEFINE_FWK_MODULE(SiPixelCompareVertexSoA);