Skip to content

Commit

Permalink
General code cleanup
Browse files Browse the repository at this point in the history
General clean up of the pixel local reconstructon code:
  - improve comments, remove commented out code and obsolete comments
  - replace std::cout with LogDebug
  - update variable names to follow the coding rules
  • Loading branch information
fwyzard committed Dec 15, 2020
1 parent 3f06c72 commit 9dc3931
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <cuda_runtime.h>

// hack waiting for if constexpr
#include "CUDADataFormats/BeamSpot/interface/BeamSpotCUDA.h"
#include "CUDADataFormats/SiPixelCluster/interface/SiPixelClustersCUDA.h"
#include "CUDADataFormats/SiPixelDigi/interface/SiPixelDigisCUDA.h"
Expand Down Expand Up @@ -90,21 +89,24 @@ void SiPixelRecHitSoAFromLegacy::produce(edm::StreamID streamID, edm::Event& iEv
iEvent.getByToken(clusterToken_, hclusters);
auto const& input = *hclusters;

// yes a unique ptr of a unique ptr so edm is happy and the pointer stay still...
// allocate a buffer for the indices of the clusters
auto hmsp = std::make_unique<uint32_t[]>(gpuClustering::maxNumModules + 1);
// hitsModuleStart is a non-owning pointer to the buffer
auto hitsModuleStart = hmsp.get();
auto hms = std::make_unique<HMSstorage>(std::move(hmsp)); // hmsp is gone
iEvent.put(tokenModuleStart_, std::move(hms)); // hms is gone! hitsModuleStart still alive and kicking...
// wrap the buffer in a HostProduct
auto hms = std::make_unique<HMSstorage>(std::move(hmsp));
// move the HostProduct to the Event, without reallocating the buffer or affecting hitsModuleStart
iEvent.put(tokenModuleStart_, std::move(hms));

// legacy output
auto legacyOutput = std::make_unique<SiPixelRecHitCollectionNew>();

// storage
std::vector<uint16_t> xx_;
std::vector<uint16_t> yy_;
std::vector<uint16_t> adc_;
std::vector<uint16_t> moduleInd_;
std::vector<int32_t> clus_;
std::vector<uint16_t> xx;
std::vector<uint16_t> yy;
std::vector<uint16_t> adc;
std::vector<uint16_t> moduleInd;
std::vector<int32_t> clus;

std::vector<edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster>> clusterRef;

Expand Down Expand Up @@ -170,19 +172,19 @@ void SiPixelRecHitSoAFromLegacy::produce(edm::StreamID streamID, edm::Event& iEv
auto const fc = hitsModuleStart[gind];
auto const lc = hitsModuleStart[gind + 1];
assert(lc > fc);
// std::cout << "in det " << gind << ": conv " << nclus << " hits from " << DSViter->size() << " legacy clusters"
// <<' '<< fc <<','<<lc<<std::endl;
LogDebug("SiPixelRecHitSoAFromLegacy") << "in det " << gind << ": conv " << nclus << " hits from "
<< DSViter->size() << " legacy clusters" << ' ' << fc << ',' << lc;
assert((lc - fc) == nclus);
if (nclus > maxHitsInModule)
printf(
"WARNING: too many clusters %d in Module %d. Only first %d Hits converted\n", nclus, gind, maxHitsInModule);

// fill digis
xx_.clear();
yy_.clear();
adc_.clear();
moduleInd_.clear();
clus_.clear();
xx.clear();
yy.clear();
adc.clear();
moduleInd.clear();
clus.clear();
clusterRef.clear();
moduleId_ = gind;
uint32_t ic = 0;
Expand All @@ -191,11 +193,11 @@ void SiPixelRecHitSoAFromLegacy::produce(edm::StreamID streamID, edm::Event& iEv
assert(clust.size() > 0);
for (int i = 0, nd = clust.size(); i < nd; ++i) {
auto px = clust.pixel(i);
xx_.push_back(px.x);
yy_.push_back(px.y);
adc_.push_back(px.adc);
moduleInd_.push_back(gind);
clus_.push_back(ic);
xx.push_back(px.x);
yy.push_back(px.y);
adc.push_back(px.adc);
moduleInd.push_back(gind);
clus.push_back(ic);
++ndigi;
}
assert(clust.originalId() == ic); // make sure hits and clus are in sync
Expand All @@ -204,10 +206,10 @@ void SiPixelRecHitSoAFromLegacy::produce(edm::StreamID streamID, edm::Event& iEv
ic++;
}
assert(nclus == ic);
assert(clus_.size() == ndigi);
assert(clus.size() == ndigi);
numberOfHits += nclus;
// filled creates view
SiPixelDigisCUDA::DeviceConstView digiView{xx_.data(), yy_.data(), adc_.data(), moduleInd_.data(), clus_.data()};
SiPixelDigisCUDA::DeviceConstView digiView{xx.data(), yy.data(), adc.data(), moduleInd.data(), clus.data()};
assert(digiView.adc(0) != 0);
// we run on blockId.x==0
gpuPixelRecHits::getHits(&cpeView, &bsHost, &digiView, ndigi, &clusterView, output->view());
Expand Down Expand Up @@ -240,7 +242,8 @@ void SiPixelRecHitSoAFromLegacy::produce(edm::StreamID streamID, edm::Event& iEv
cms::cuda::fillManyFromVector(
output->phiBinner(), 10, output->iphi(), output->hitsLayerStart(), numberOfHits, 256, nullptr);

// std::cout << "created HitSoa for " << numberOfClusters << " clusters in " << numberOfDetUnits << " Dets" << std::endl;
LogDebug("SiPixelRecHitSoAFromLegacy") << "created HitSoa for " << numberOfClusters << " clusters in "
<< numberOfDetUnits << " Dets";
iEvent.put(std::move(output));
if (convert2Legacy_)
iEvent.put(std::move(legacyOutput));
Expand Down
92 changes: 31 additions & 61 deletions RecoLocalTracker/SiPixelRecHits/src/PixelCPEFast.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <iostream>

#include <cuda.h>
#include <cuda_runtime.h>

Expand Down Expand Up @@ -112,7 +110,8 @@ void PixelCPEFast::fillParamsForGpu() {
m_commonParamsGPU.thePitchX = m_DetParams[0].thePitchX;
m_commonParamsGPU.thePitchY = m_DetParams[0].thePitchY;

// std::cout << "pitch & thickness " << m_commonParamsGPU.thePitchX << ' ' << m_commonParamsGPU.thePitchY << " " << m_commonParamsGPU.theThicknessB << ' ' << m_commonParamsGPU.theThicknessE << std::endl;
LogDebug("PixelCPEFast") << "pitch & thickness " << m_commonParamsGPU.thePitchX << ' ' << m_commonParamsGPU.thePitchY
<< " " << m_commonParamsGPU.theThicknessB << ' ' << m_commonParamsGPU.theThicknessE;

// zero average geometry
memset(&m_averageGeometry, 0, sizeof(pixelCPEforGPU::AverageGeometry));
Expand All @@ -132,30 +131,28 @@ void PixelCPEFast::fillParamsForGpu() {
assert(p.theDet->index() == int(i));
assert(m_commonParamsGPU.thePitchY == p.thePitchY);
assert(m_commonParamsGPU.thePitchX == p.thePitchX);
//assert(m_commonParamsGPU.theThickness==p.theThickness);

g.isBarrel = GeomDetEnumerators::isBarrel(p.thePart);
g.isPosZ = p.theDet->surface().position().z() > 0;
g.layer = ttopo_.layer(p.theDet->geographicalId());
g.index = i; // better be!
g.rawId = p.theDet->geographicalId();

assert((g.isBarrel ? m_commonParamsGPU.theThicknessB : m_commonParamsGPU.theThicknessE) == p.theThickness);

//if (m_commonParamsGPU.theThickness!=p.theThickness)
// std::cout << i << (g.isBarrel ? "B " : "E ") << m_commonParamsGPU.theThickness<<"!="<<p.theThickness << std::endl;

auto ladder = ttopo_.pxbLadder(p.theDet->geographicalId());
if (oldLayer != g.layer) {
oldLayer = g.layer;
// std::cout << "new layer at " << i << (g.isBarrel ? " B " : (g.isPosZ ? " E+ " : " E- ")) << g.layer << " starting at " << g.rawId << std::endl;
// std::cout << "old layer had " << nl << " ladders" << std::endl;
LogDebug("PixelCPEFast") << "new layer at " << i << (g.isBarrel ? " B " : (g.isPosZ ? " E+ " : " E- "))
<< g.layer << " starting at " << g.rawId << '\n'
<< "old layer had " << nl << " ladders";
nl = 0;
}
if (oldLadder != ladder) {
oldLadder = ladder;
// std::cout << "new ladder at " << i << (g.isBarrel ? " B " : (g.isPosZ ? " E+ " : " E- ")) << ladder << " starting at " << g.rawId << std::endl;
// std::cout << "old ladder ave z,r,p mz " << zl/8.f << " " << rl/8.f << " " << pl/8.f << ' ' << miz << ' ' << mxz << std::endl;
LogDebug("PixelCPEFast") << "new ladder at " << i << (g.isBarrel ? " B " : (g.isPosZ ? " E+ " : " E- "))
<< ladder << " starting at " << g.rawId << '\n'
<< "old ladder ave z,r,p mz " << zl / 8.f << " " << rl / 8.f << " " << pl / 8.f << ' '
<< miz << ' ' << mxz;
rl = 0;
zl = 0;
pl = 0;
Expand Down Expand Up @@ -200,16 +197,15 @@ void PixelCPEFast::fillParamsForGpu() {
if (lape.invalid())
lape = LocalError(); // zero....

#ifdef DUMP_ERRORS
#ifdef EDM_ML_DEBUG
auto m = 10000.f;
for (float qclus = 15000; qclus < 35000; qclus += 15000) {
errorFromTemplates(p, cp, qclus);

std::cout << i << ' ' << qclus << ' ' << cp.pixmx << ' ' << m * cp.sigmax << ' ' << m * cp.sx1 << ' '
<< m * cp.sx2 << ' ' << m * cp.sigmay << ' ' << m * cp.sy1 << ' ' << m * cp.sy2 << std::endl;
LogDebug("PixelCPEFast") << i << ' ' << qclus << ' ' << cp.pixmx << ' ' << m * cp.sigmax << ' ' << m * cp.sx1
<< ' ' << m * cp.sx2 << ' ' << m * cp.sigmay << ' ' << m * cp.sy1 << ' ' << m * cp.sy2;
}
std::cout << i << ' ' << m * std::sqrt(lape.xx()) << ' ' << m * std::sqrt(lape.yy()) << std::endl;
#endif
LogDebug("PixelCPEFast") << i << ' ' << m * std::sqrt(lape.xx()) << ' ' << m * std::sqrt(lape.yy());
#endif // EDM_ML_DEBUG

errorFromTemplates(p, cp, 20000.f);
g.pixmx = std::max(0, cp.pixmx);
Expand All @@ -221,35 +217,6 @@ void PixelCPEFast::fillParamsForGpu() {
g.sy[1] = cp.sy1;
g.sy[2] = cp.sy2;

/*
// from run1??
if (i<96) {
g.sx[0] = 0.00120;
g.sx[1] = 0.00115;
g.sx[2] = 0.0050;
g.sy[0] = 0.00210;
g.sy[1] = 0.00375;
g.sy[2] = 0.0085;
} else if (g.isBarrel) {
g.sx[0] = 0.00120;
g.sx[1] = 0.00115;
g.sx[2] = 0.0050;
g.sy[0] = 0.00210;
g.sy[1] = 0.00375;
g.sy[2] = 0.0085;
} else {
g.sx[0] = 0.0020;
g.sx[1] = 0.0020;
g.sx[2] = 0.0050;
g.sy[0] = 0.0021;
g.sy[1] = 0.0021;
g.sy[2] = 0.0085;
}
*/

for (int i = 0; i < 3; ++i) {
g.sx[i] = std::sqrt(g.sx[i] * g.sx[i] + lape.xx());
g.sy[i] = std::sqrt(g.sy[i] * g.sy[i] + lape.yy());
Expand All @@ -269,7 +236,7 @@ void PixelCPEFast::fillParamsForGpu() {
aveGeom.ladderMaxZ[il] = std::max(aveGeom.ladderMaxZ[il], z);
aveGeom.ladderX[il] += 0.125f * g.frame.x();
aveGeom.ladderY[il] += 0.125f * g.frame.y();
aveGeom.ladderR[il] += 0.125 * sqrt(g.frame.x() * g.frame.x() + g.frame.y() * g.frame.y());
aveGeom.ladderR[il] += 0.125f * sqrt(g.frame.x() * g.frame.x() + g.frame.y() * g.frame.y());
}
assert(il + 1 == int(phase1PixelTopology::numberOfLaddersInBarrel));
// add half_module and tollerance
Expand All @@ -293,13 +260,16 @@ void PixelCPEFast::fillParamsForGpu() {
aveGeom.endCapZ[0] -= 1.5f;
aveGeom.endCapZ[1] += 1.5f;

/*
for (int jl=0, nl=phase1PixelTopology::numberOfLaddersInBarrel; jl<nl; ++jl) {
std::cout << jl<<':'<<aveGeom.ladderR[jl] << '/'<< std::sqrt(aveGeom.ladderX[jl]*aveGeom.ladderX[jl]+aveGeom.ladderY[jl]*aveGeom.ladderY[jl])
<<','<<aveGeom.ladderZ[jl]<<','<<aveGeom.ladderMinZ[jl]<<','<<aveGeom.ladderMaxZ[jl]<< ' ';
} std::cout<< std::endl;
std::cout << aveGeom.endCapZ[0] << ' ' << aveGeom.endCapZ[1] << std::endl;
*/
#ifdef EDM_ML_DEBUG
for (int jl = 0, nl = phase1PixelTopology::numberOfLaddersInBarrel; jl < nl; ++jl) {
LogDebug("PixelCPEFast") << jl << ':' << aveGeom.ladderR[jl] << '/'
<< std::sqrt(aveGeom.ladderX[jl] * aveGeom.ladderX[jl] +
aveGeom.ladderY[jl] * aveGeom.ladderY[jl])
<< ',' << aveGeom.ladderZ[jl] << ',' << aveGeom.ladderMinZ[jl] << ','
<< aveGeom.ladderMaxZ[jl] << '\n';
}
LogDebug("PixelCPEFast") << aveGeom.endCapZ[0] << ' ' << aveGeom.endCapZ[1];
#endif // EDM_ML_DEBUG

// fill Layer and ladders geometry
memcpy(m_layerGeometry.layerStart, phase1PixelTopology::layerStart, sizeof(phase1PixelTopology::layerStart));
Expand All @@ -325,7 +295,7 @@ void PixelCPEFast::errorFromTemplates(DetParam const& theDetParam,
float qclus) const {
float locBz = theDetParam.bz;
float locBx = theDetParam.bx;
//cout << "PixelCPEFast::localPosition(...) : locBz = " << locBz << endl;
LogDebug("PixelCPEFast") << "PixelCPEFast::localPosition(...) : locBz = " << locBz;

theClusterParam.pixmx = std::numeric_limits<int>::max(); // max pixel charge for truncation of 2-D cluster

Expand Down Expand Up @@ -411,8 +381,8 @@ LocalPoint PixelCPEFast::localPosition(DetParam const& theDetParam, ClusterParam
auto xPos = cp.xpos[0];
auto yPos = cp.ypos[0];

// std::cout<<" in PixelCPEFast:localPosition - pos = "<<xPos<<" "<<yPos
// << " size "<< cp.maxRow[0]-cp.minRow[0] << ' ' << cp.maxCol[0]-cp.minCol[0] << std::endl; //dk
LogDebug("PixelCPEFast") << " in PixelCPEFast:localPosition - pos = " << xPos << " " << yPos << " size "
<< cp.maxRow[0] - cp.minRow[0] << ' ' << cp.maxCol[0] - cp.minCol[0];

//--- Now put the two together
LocalPoint pos_in_local(xPos, yPos);
Expand Down Expand Up @@ -526,8 +496,8 @@ LocalError PixelCPEFast::localError(DetParam const& theDetParam, ClusterParam& t
} else { // simple errors

// This are the simple errors, hardcoded in the code
//cout << "Track angles are not known " << endl;
//cout << "Default angle estimation which assumes track from PV (0,0,0) does not work." << endl;
LogDebug("PixelCPEFast") << "Track angles are not known.\n"
<< "Default angle estimation which assumes track from PV (0,0,0) does not work.";

if (GeomDetEnumerators::isTrackerPixel(theDetParam.thePart)) {
if (GeomDetEnumerators::isBarrel(theDetParam.thePart)) {
Expand Down Expand Up @@ -583,7 +553,7 @@ LocalError PixelCPEFast::localError(DetParam const& theDetParam, ClusterParam& t

} // end

// std::cout<<" errors "<<xerr<<" "<<yerr<<std::endl; //dk
LogDebug("PixelCPEFast") << " errors " << xerr << " " << yerr;

auto xerr_sq = xerr * xerr;
auto yerr_sq = yerr * yerr;
Expand Down

0 comments on commit 9dc3931

Please sign in to comment.