Skip to content

Commit

Permalink
Merge pull request #16815 from VinInn/SpeedP2Clus
Browse files Browse the repository at this point in the history
Speed up Phase2TrackerClusterizer by a factor 1000
  • Loading branch information
cmsbuild committed Dec 12, 2016
2 parents 70fce16 + 0d28f30 commit 7d4dc66
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 80 deletions.
9 changes: 8 additions & 1 deletion DataFormats/Phase2TrackerDigi/interface/Phase2TrackerDigi.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Phase2TrackerDigi {
// Access to digi information - pixel sensors
unsigned int row() const { return channelToRow(theChannel); }
unsigned int column() const { return channelToColumn(theChannel); }
uint16_t packedPosition() const { return 0x7FFF & theChannel; }
// Access to digi information - strip sensors
unsigned int strip() const { return row(); }
unsigned int edge() const { return column(); } // CD: any better name for that?
Expand Down Expand Up @@ -57,9 +58,15 @@ class Phase2TrackerDigi {

// Comparison operators
inline bool operator<( const Phase2TrackerDigi& one, const Phase2TrackerDigi& other) {
return one.channel() < other.channel();
return one.packedPosition() < other.packedPosition();
}

// distance operators
inline int operator-( const Phase2TrackerDigi& one, const Phase2TrackerDigi& other) {
return int(one.packedPosition()) - int(other.packedPosition());
}


#include<iostream>
inline std::ostream & operator<<(std::ostream & o, const Phase2TrackerDigi& digi) {
return o << " " << digi.channel();
Expand Down
14 changes: 0 additions & 14 deletions RecoLocalTracker/SiPhase2Clusterizer/BuildFile.xml

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion RecoLocalTracker/SiPhase2Clusterizer/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<use name="RecoLocalTracker/SiPhase2Clusterizer"/>
<library file="Phase2TrackerClusterizer.cc Phase2TrackerClusterizerAlgorithm.cc Phase2TrackerClusterizerArray.cc" name="Phase2TrackerClusterizerPlugin">
<library file="*.cc" name="Phase2TrackerClusterizerPlugin">
<flags EDM_PLUGIN="1"/>
</library>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/ESHandle.h"

#include "RecoLocalTracker/SiPhase2Clusterizer/interface/Phase2TrackerClusterizerAlgorithm.h"
#ifdef VERIFY_PH2_TK_CLUS
#include "Phase2TrackerClusterizerAlgorithm.h"
#endif
#include "Phase2TrackerClusterizerSequentialAlgorithm.h"

#include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
Expand All @@ -24,6 +27,8 @@
#include <vector>
#include <memory>



class Phase2TrackerClusterizer : public edm::stream::EDProducer<> {

public:
Expand All @@ -32,7 +37,9 @@ class Phase2TrackerClusterizer : public edm::stream::EDProducer<> {
virtual void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;

private:
#ifdef VERIFY_PH2_TK_CLUS
std::unique_ptr< Phase2TrackerClusterizerAlgorithm > clusterizer_;
#endif
edm::EDGetTokenT< edm::DetSetVector< Phase2TrackerDigi > > token_;

};
Expand All @@ -43,7 +50,9 @@ class Phase2TrackerClusterizer : public edm::stream::EDProducer<> {
*/

Phase2TrackerClusterizer::Phase2TrackerClusterizer(edm::ParameterSet const& conf) :
#ifdef VERIFY_PH2_TK_CLUS
clusterizer_(new Phase2TrackerClusterizerAlgorithm(conf.getParameter< unsigned int >("maxClusterSize"), conf.getParameter< unsigned int >("maxNumberClusters"))),
#endif
token_(consumes< edm::DetSetVector< Phase2TrackerDigi > >(conf.getParameter<edm::InputTag>("src"))) {
produces< Phase2TrackerCluster1DCollectionNew >();
}
Expand All @@ -60,35 +69,62 @@ class Phase2TrackerClusterizer : public edm::stream::EDProducer<> {
edm::Handle< edm::DetSetVector< Phase2TrackerDigi > > digis;
event.getByToken(token_, digis);

#ifdef VERIFY_PH2_TK_CLUS
// Get the geometry
edm::ESHandle< TrackerGeometry > geomHandle;
eventSetup.get< TrackerDigiGeometryRecord >().get(geomHandle);
const TrackerGeometry* tkGeom(&(*geomHandle));

const TrackerGeometry* tkGeom(&(*geomHandle));
// Global container for the clusters of each modules
auto outputClustersOld = std::make_unique<Phase2TrackerCluster1DCollectionNew>();
#endif
auto outputClusters = std::make_unique<Phase2TrackerCluster1DCollectionNew>();

// Go over all the modules
for (auto DSViter : *digis) {
DetId detId(DSViter.detId());

// Geometry
Phase2TrackerCluster1DCollectionNew::FastFiller clusters(*outputClusters, DSViter.detId());
Phase2TrackerClusterizerSequentialAlgorithm algo;
algo.clusterizeDetUnit(DSViter, clusters);
if (clusters.empty()) clusters.abort();

#ifdef VERIFY_PH2_TK_CLUS
// Geometry
const GeomDetUnit* geomDetUnit(tkGeom->idToDetUnit(detId));
const PixelGeomDetUnit* pixDet = dynamic_cast< const PixelGeomDetUnit* >(geomDetUnit);
if (!pixDet) assert(0);

// Container for the clusters that will be produced for this modules
Phase2TrackerCluster1DCollectionNew::FastFiller clusters(*outputClusters, DSViter.detId());
Phase2TrackerCluster1DCollectionNew::FastFiller clustersOld(*outputClustersOld, DSViter.detId());

// Setup the clusterizer algorithm for this detector (see ClusterizerAlgorithm for more details)
clusterizer_->setup(pixDet);

// Pass the list of Digis to the main algorithm
// This function will store the clusters in the previously created container
clusterizer_->clusterizeDetUnit(DSViter, clusters);
clusterizer_->clusterizeDetUnit(DSViter, clustersOld);
if (clustersOld.empty()) clustersOld.abort();

if (clusters.empty()) clusters.abort();
if (clusters.size() != clustersOld.size()) {
std::cout << "SIZEs " << int(detId) << ' ' << clusters.size() << ' ' << clustersOld.size() << std::endl;
for (auto const & cl : clusters) std::cout << cl.size() << ' ' << cl.threshold() << ' ' << cl.firstRow() << ' ' << cl.column() << std::endl;
std::cout << "Old " << std::endl;
for (auto const & cl : clustersOld) std::cout << cl.size() << ' ' << cl.threshold() << ' ' << cl.firstRow() << ' ' << cl.column() << std::endl;
}
#endif

}

#ifdef VERIFY_PH2_TK_CLUS
// std::cout << "SIZEs " << outputClusters->dataSize() << ' ' << outputClustersOld->dataSize() << std::endl;
assert(outputClusters->dataSize()==outputClustersOld->dataSize());
for (auto i=0U;i<outputClusters->dataSize(); ++i) {
assert(outputClusters->data()[i].size() == outputClustersOld->data()[i].size());
assert(outputClusters->data()[i].threshold() == outputClustersOld->data()[i].threshold());
assert(outputClusters->data()[i].firstRow() == outputClustersOld->data()[i].firstRow());
assert(outputClusters->data()[i].column() == outputClustersOld->data()[i].column());
}
#endif

// Add the data to the output
outputClusters->shrink_to_fit();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "RecoLocalTracker/SiPhase2Clusterizer/interface/Phase2TrackerClusterizerAlgorithm.h"
#include "Phase2TrackerClusterizerAlgorithm.h"

#include "Geometry/CommonTopologies/interface/PixelTopology.h"

Expand All @@ -16,7 +16,7 @@ void Phase2TrackerClusterizerAlgorithm::setup(const PixelGeomDetUnit* pixDet) {
const PixelTopology& topol(pixDet->specificTopology());
nrows_ = topol.nrows();
ncols_ = topol.ncolumns();
matrix_.setSize(nrows_, ncols_);
matrix_ = decltype(matrix_)(nrows_, ncols_);
}

/*
Expand Down Expand Up @@ -71,6 +71,7 @@ void Phase2TrackerClusterizerAlgorithm::clusterizeDetUnit(const edm::DetSet< Pha
sizeCluster = 0;
// Increase the number of clusters
++numberClusters;
HIPbit = false;
}

// Check if we hit the maximum number of clusters per module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"

#include "RecoLocalTracker/SiPhase2Clusterizer/interface/Phase2TrackerClusterizerArray.h"
#include "Phase2TrackerClusterizerArray.h"

class Phase2TrackerClusterizerAlgorithm {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
#include "RecoLocalTracker/SiPhase2Clusterizer/interface/Phase2TrackerClusterizerArray.h"
#ifndef RecoLocalTracker_SiPhase2Clusterizer_Phase2TrackerClusterizerArray_h
#define RecoLocalTracker_SiPhase2Clusterizer_Phase2TrackerClusterizerArray_h

#include <vector>

class Phase2TrackerClusterizerArray {

public:

inline Phase2TrackerClusterizerArray();
inline Phase2TrackerClusterizerArray(unsigned int, unsigned int);
// inline void setSize(unsigned int, unsigned int);
inline int operator()(unsigned int, unsigned int) const;
inline unsigned int rows() const;
inline unsigned int columns() const;
inline bool inside(unsigned int, unsigned int) const;
inline void set(unsigned int, unsigned int, bool, bool);
inline unsigned int size() const;
inline unsigned int index(unsigned int, unsigned int) const;

private:

unsigned int nrows_, ncols_;
std::vector< bool > matrix_;
std::vector< bool > hipmatrix_;

};


/*
* Create a new Array of hits
*/

Phase2TrackerClusterizerArray::Phase2TrackerClusterizerArray() : nrows_(0), ncols_(0) { }

Phase2TrackerClusterizerArray::Phase2TrackerClusterizerArray(unsigned int nrows, unsigned int ncols) {
setSize(nrows, ncols);
}

/*
* Resize the Array and clear the values
*/
Phase2TrackerClusterizerArray::Phase2TrackerClusterizerArray(unsigned int nrows, unsigned int ncols) :
nrows_(nrows),
ncols_(ncols),
matrix_(nrows * ncols,false),
hipmatrix_(nrows * ncols,false){}

void Phase2TrackerClusterizerArray::setSize(unsigned int nrows, unsigned int ncols) {
nrows_ = nrows;
ncols_ = ncols;
matrix_.resize(nrows * ncols);
hipmatrix_.resize(nrows * ncols);
for (std::vector< bool >::iterator it(matrix_.begin()); it != matrix_.end(); ++it) *it = false;
for (std::vector< bool >::iterator it(hipmatrix_.begin()); it != hipmatrix_.end(); ++it) *it = false;
}

/*
* Return the value of an element in the Array
*/

int Phase2TrackerClusterizerArray::operator()(unsigned int row, unsigned int col) const {
if (inside(row, col)) {
if (matrix_[index(row, col)]) {
if (hipmatrix_[index(row, col)]) return 2;
else return 1;
} else return 0;
}
else return 0;
if (!inside(row, col)) return 0; // FIXME this should go outside: avoid it
return matrix_[index(row, col)] ?
( hipmatrix_[index(row, col)] ? 2 : 1 ) : 0;
}

/*
Expand All @@ -58,7 +71,7 @@ unsigned int Phase2TrackerClusterizerArray::columns() const {
*/

bool Phase2TrackerClusterizerArray::inside(unsigned int row, unsigned int col) const {
return (row < nrows_ && col < ncols_);
return ( (row < nrows_) & (col < ncols_) );
}

/*
Expand Down Expand Up @@ -86,3 +99,4 @@ unsigned int Phase2TrackerClusterizerArray::index(unsigned int row, unsigned int
return col * nrows_ + row;
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef RecoLocalTracker_SiPhase2Clusterizer_Phase2TrackerClusterizerSequentialAlgorithm_h
#define RecoLocalTracker_SiPhase2Clusterizer_Phase2TrackerClusterizerSequentialAlgorithm_h

#include "DataFormats/Common/interface/DetSetVector.h"
#include "DataFormats/Common/interface/DetSetVectorNew.h"
#include "DataFormats/Phase2TrackerDigi/interface/Phase2TrackerDigi.h"
#include "DataFormats/Phase2TrackerCluster/interface/Phase2TrackerCluster1D.h"


class Phase2TrackerClusterizerSequentialAlgorithm {
public:
inline void clusterizeDetUnit(const edm::DetSet< Phase2TrackerDigi >&, Phase2TrackerCluster1DCollectionNew::FastFiller&) const;
};

void
Phase2TrackerClusterizerSequentialAlgorithm::clusterizeDetUnit(const edm::DetSet< Phase2TrackerDigi >& digis, Phase2TrackerCluster1DCollectionNew::FastFiller& clusters) const {
if (digis.empty()) return;
auto di = digis.begin();
unsigned int sizeCluster=1;
Phase2TrackerDigi firstDigi = *di;
bool HIPbit = firstDigi.overThreshold();
auto previous = firstDigi;
++di;
for (;di!=digis.end(); ++di) {
auto digi = *di;
#ifdef VERIFY_PH2_TK_CLUS
if ( !(previous<digi)) std::cout << "not ordered " << previous << ' ' << digi << std::endl;
#endif
if (digi-previous == 1) {
HIPbit |= digi.overThreshold();
++sizeCluster;
} else {
clusters.push_back(Phase2TrackerCluster1D(firstDigi, sizeCluster, HIPbit));
firstDigi=digi;
HIPbit = digi.overThreshold();
sizeCluster=1;
}
previous=digi;
}
clusters.push_back(Phase2TrackerCluster1D(firstDigi, sizeCluster, HIPbit));

}


#endif

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Clusterizer options
siPhase2Clusters = cms.EDProducer('Phase2TrackerClusterizer',
src = cms.InputTag("mix", "Tracker"),
maxClusterSize = cms.uint32(8),
maxClusterSize = cms.uint32(0), # was 8
maxNumberClusters = cms.uint32(0)
)

Expand Down

0 comments on commit 7d4dc66

Please sign in to comment.