Skip to content

Commit

Permalink
Merge pull request #18002 from VinInn/SpeedUpCA
Browse files Browse the repository at this point in the history
Speed Up Cellular Automaton implementation
  • Loading branch information
cmsbuild committed Mar 30, 2017
2 parents 07abf0e + 80b67be commit fa37d50
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 426 deletions.
553 changes: 279 additions & 274 deletions RecoPixelVertexing/PixelTriplets/plugins/CACell.h

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions RecoPixelVertexing/PixelTriplets/plugins/CAGraph.h
Expand Up @@ -4,9 +4,6 @@
#include <vector>
#include <array>
#include <string>
#include <queue>
#include <functional>
#include "CACell.h"

struct CALayer
{
Expand All @@ -31,7 +28,7 @@ struct CALayer

std::vector<int> theOuterLayers;
std::vector<int> theInnerLayers;
std::vector< std::vector<CACell*> > isOuterHitOfCell;
std::vector< std::vector<unsigned int> > isOuterHitOfCell;


private:
Expand All @@ -58,16 +55,14 @@ struct CALayerPair
}

std::array<int, 2> theLayers;
std::vector<CACell> theFoundCells;

std::array<unsigned int, 2> theFoundCells= {{0,0}};
};

struct CAGraph
{
std::vector<CALayer> theLayers;
std::vector<CALayerPair> theLayerPairs;
std::vector<int> theRootLayers;

};

#endif /* CAGRAPH_H_ */
Expand Up @@ -138,7 +138,7 @@ namespace {
g.theLayers[i].theInnerLayerPairs.clear();
g.theLayers[i].theOuterLayers.clear();
g.theLayers[i].theOuterLayerPairs.clear();

for (auto & v : g.theLayers[i].isOuterHitOfCell) v.clear();
}

}
Expand Down Expand Up @@ -234,6 +234,7 @@ void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,
hitQuadruplets(region, result, hitDoubletsPtr, g, es);
theLayerCache.clear();
}

void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& regionDoublets,
std::vector<OrderedHitSeeds>& result,
const edm::EventSetup& es,
Expand Down Expand Up @@ -292,7 +293,8 @@ void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& region

ca.findNtuplets(foundQuadruplets, numberOfHitsInNtuplet);


auto & allCells = ca.getAllCells();

const QuantityDependsPtEval maxChi2Eval = maxChi2.evaluator(es);

// re-used thoughout
Expand Down Expand Up @@ -321,13 +323,13 @@ void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& region
};
for(unsigned int i = 0; i< 3; ++i)
{
auto const& ahit = foundQuadruplets[quadId][i]->getInnerHit();
auto const& ahit = allCells[foundQuadruplets[quadId][i]].getInnerHit();
gps[i] = ahit->globalPosition();
ges[i] = ahit->globalPositionError();
barrels[i] = isBarrel(ahit->geographicalId().subdetId());
}

auto const& ahit = foundQuadruplets[quadId][2]->getOuterHit();
auto const& ahit = allCells[foundQuadruplets[quadId][2]].getOuterHit();
gps[3] = ahit->globalPosition();
ges[3] = ahit->globalPositionError();
barrels[3] = isBarrel(ahit->geographicalId().subdetId());
Expand All @@ -336,10 +338,10 @@ void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& region
const auto fourthLayerId = tTopo->layer(ahit->geographicalId());
const auto sideId = tTopo->side(ahit->geographicalId());
const auto subDetId = ahit->geographicalId().subdetId();
const auto isTheSameTriplet = (quadId != 0) && (foundQuadruplets[quadId][0]->getCellId() == previousCellIds[0]) && (foundQuadruplets[quadId][1]->getCellId() == previousCellIds[1]);
const auto isTheSameTriplet = (quadId != 0) && (foundQuadruplets[quadId][0] == previousCellIds[0]) && (foundQuadruplets[quadId][1] == previousCellIds[1]);
const auto isTheSameFourthLayer = (quadId != 0) && (fourthLayerId == previousfourthLayerId) && (subDetId == previousSubDetId) && (sideId == previousSideId);

previousCellIds = {{foundQuadruplets[quadId][0]->getCellId(), foundQuadruplets[quadId][1]->getCellId()}};
previousCellIds = {{foundQuadruplets[quadId][0], foundQuadruplets[quadId][1]}};
previousfourthLayerId = fourthLayerId;


Expand All @@ -362,7 +364,9 @@ void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& region
const float thisMaxChi2 = maxChi2Eval.value(abscurv);
if (theComparitor)
{
SeedingHitSet tmpTriplet(foundQuadruplets[quadId][0]->getInnerHit(), foundQuadruplets[quadId][2]->getInnerHit(), foundQuadruplets[quadId][2]->getOuterHit());
SeedingHitSet tmpTriplet(allCells[foundQuadruplets[quadId][0]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getOuterHit());


if (!theComparitor->compatible(tmpTriplet) )
Expand Down Expand Up @@ -417,13 +421,19 @@ void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& region
{
result[index].pop_back();
}
result[index].emplace_back(foundQuadruplets[quadId][0]->getInnerHit(), foundQuadruplets[quadId][1]->getInnerHit(),foundQuadruplets[quadId][2]->getInnerHit(), foundQuadruplets[quadId][2]->getOuterHit());
result[index].emplace_back(allCells[foundQuadruplets[quadId][0]].getInnerHit(),
allCells[foundQuadruplets[quadId][1]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getOuterHit());
hasAlreadyPushedACandidate = true;
}
}
else
{
result[index].emplace_back(foundQuadruplets[quadId][0]->getInnerHit(), foundQuadruplets[quadId][1]->getInnerHit(), foundQuadruplets[quadId][2]->getInnerHit(), foundQuadruplets[quadId][2]->getOuterHit());
result[index].emplace_back(allCells[foundQuadruplets[quadId][0]].getInnerHit(),
allCells[foundQuadruplets[quadId][1]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getOuterHit());
}
}
index++;
Expand All @@ -433,7 +443,7 @@ void CAHitQuadrupletGenerator::hitNtuplets(const IntermediateHitDoublets& region

void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,
OrderedHitSeeds & result,
std::vector<const HitDoublets *>& hitDoublets, const CAGraph& g,
std::vector<const HitDoublets *>& hitDoublets, CAGraph& g,
const edm::EventSetup& es) {
//Retrieve tracker topology from geometry
edm::ESHandle<TrackerTopology> tTopoHand;
Expand All @@ -452,6 +462,7 @@ void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,

ca.findNtuplets(foundQuadruplets, numberOfHitsInNtuplet);

auto & allCells = ca.getAllCells();

const QuantityDependsPtEval maxChi2Eval = maxChi2.evaluator(es);

Expand Down Expand Up @@ -481,13 +492,13 @@ void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,
};
for(unsigned int i = 0; i< 3; ++i)
{
auto const& ahit = foundQuadruplets[quadId][i]->getInnerHit();
auto const& ahit = allCells[foundQuadruplets[quadId][i]].getInnerHit();
gps[i] = ahit->globalPosition();
ges[i] = ahit->globalPositionError();
barrels[i] = isBarrel(ahit->geographicalId().subdetId());
}

auto const& ahit = foundQuadruplets[quadId][2]->getOuterHit();
auto const& ahit = allCells[foundQuadruplets[quadId][2]].getOuterHit();
gps[3] = ahit->globalPosition();
ges[3] = ahit->globalPositionError();
barrels[3] = isBarrel(ahit->geographicalId().subdetId());
Expand All @@ -497,10 +508,10 @@ void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,
const auto fourthLayerId = tTopo->layer(ahit->geographicalId());
const auto sideId = tTopo->side(ahit->geographicalId());
const auto subDetId = ahit->geographicalId().subdetId();
const auto isTheSameTriplet = (quadId != 0) && (foundQuadruplets[quadId][0]->getCellId() == previousCellIds[0]) && (foundQuadruplets[quadId][1]->getCellId() == previousCellIds[1]);
const auto isTheSameTriplet = (quadId != 0) && (foundQuadruplets[quadId][0] == previousCellIds[0]) && (foundQuadruplets[quadId][1] == previousCellIds[1]);
const auto isTheSameFourthLayer = (quadId != 0) && (fourthLayerId == previousfourthLayerId) && (subDetId == previousSubDetId) && (sideId == previousSideId);

previousCellIds = {{foundQuadruplets[quadId][0]->getCellId(), foundQuadruplets[quadId][1]->getCellId()}};
previousCellIds = {{foundQuadruplets[quadId][0], foundQuadruplets[quadId][1]}};
previousfourthLayerId = fourthLayerId;


Expand All @@ -526,7 +537,9 @@ void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,

if (theComparitor)
{
SeedingHitSet tmpTriplet(foundQuadruplets[quadId][0]->getInnerHit(), foundQuadruplets[quadId][2]->getInnerHit(), foundQuadruplets[quadId][2]->getOuterHit());
SeedingHitSet tmpTriplet(allCells[foundQuadruplets[quadId][0]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getOuterHit());


if (!theComparitor->compatible(tmpTriplet) )
Expand Down Expand Up @@ -587,16 +600,21 @@ void CAHitQuadrupletGenerator::hitQuadruplets(const TrackingRegion& region,
result.pop_back();

}
result.emplace_back(foundQuadruplets[quadId][0]->getInnerHit(), foundQuadruplets[quadId][1]->getInnerHit(),
foundQuadruplets[quadId][2]->getInnerHit(), foundQuadruplets[quadId][2]->getOuterHit());

result.emplace_back(allCells[foundQuadruplets[quadId][0]].getInnerHit(),
allCells[foundQuadruplets[quadId][1]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getOuterHit());
hasAlreadyPushedACandidate = true;

}
}
else
{

result.emplace_back(foundQuadruplets[quadId][0]->getInnerHit(), foundQuadruplets[quadId][1]->getInnerHit(), foundQuadruplets[quadId][2]->getInnerHit(), foundQuadruplets[quadId][2]->getOuterHit());
result.emplace_back(allCells[foundQuadruplets[quadId][0]].getInnerHit(),
allCells[foundQuadruplets[quadId][1]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getInnerHit(),
allCells[foundQuadruplets[quadId][2]].getOuterHit());
}

}
Expand Down
Expand Up @@ -63,7 +63,7 @@ class CAHitQuadrupletGenerator : public HitQuadrupletGenerator {
// actual work
void hitQuadruplets(const TrackingRegion& reg, OrderedHitSeeds& result,
std::vector<const HitDoublets *>& hitDoublets,
const CAGraph& g,
CAGraph& g,
const edm::EventSetup& es);

edm::EDGetTokenT<SeedingLayerSetsHits> theSeedingLayerToken;
Expand Down
28 changes: 15 additions & 13 deletions RecoPixelVertexing/PixelTriplets/plugins/CAHitTripletGenerator.cc
Expand Up @@ -132,7 +132,7 @@ namespace {
g.theLayers[i].theInnerLayerPairs.clear();
g.theLayers[i].theOuterLayers.clear();
g.theLayers[i].theOuterLayerPairs.clear();

for (auto & v : g.theLayers[i].isOuterHitOfCell) v.clear();
}

}
Expand Down Expand Up @@ -273,7 +273,7 @@ void CAHitTripletGenerator::hitNtuplets(const IntermediateHitDoublets& regionDou
ca.findTriplets(hitDoublets, foundTriplets, region, caThetaCut, caPhiCut,
caHardPtCut);


auto & allCells = ca.getAllCells();

const QuantityDependsPtEval maxChi2Eval = maxChi2.evaluator(es);

Expand All @@ -290,23 +290,23 @@ void CAHitTripletGenerator::hitNtuplets(const IntermediateHitDoublets& regionDou
++tripletId)
{

OrderedHitTriplet tmpTriplet(foundTriplets[tripletId][0]->getInnerHit(),
foundTriplets[tripletId][0]->getOuterHit(),
foundTriplets[tripletId][1]->getOuterHit());
OrderedHitTriplet tmpTriplet(allCells[foundTriplets[tripletId][0]].getInnerHit(),
allCells[foundTriplets[tripletId][0]].getOuterHit(),
allCells[foundTriplets[tripletId][1]].getOuterHit());

auto isBarrel = [](const unsigned id) -> bool
{
return id == PixelSubdetector::PixelBarrel;
};
for (unsigned int i = 0; i < 2; ++i)
{
auto const& ahit = foundTriplets[tripletId][i]->getInnerHit();
auto const& ahit = allCells[foundTriplets[tripletId][i]].getInnerHit();
gps[i] = ahit->globalPosition();
ges[i] = ahit->globalPositionError();
barrels[i] = isBarrel(ahit->geographicalId().subdetId());
}

auto const& ahit = foundTriplets[tripletId][1]->getOuterHit();
auto const& ahit = allCells[foundTriplets[tripletId][1]].getOuterHit();
gps[2] = ahit->globalPosition();
ges[2] = ahit->globalPositionError();
barrels[2] = isBarrel(ahit->geographicalId().subdetId());
Expand Down Expand Up @@ -374,13 +374,15 @@ void CAHitTripletGenerator::hitNtuplets(const IntermediateHitDoublets& regionDou

void CAHitTripletGenerator::hitTriplets(const TrackingRegion& region,
OrderedHitTriplets & result,
std::vector<const HitDoublets *>& hitDoublets, const CAGraph& g,
std::vector<const HitDoublets *>& hitDoublets, CAGraph& g,
const edm::EventSetup& es) {
std::vector<CACell::CAntuplet> foundTriplets;
CellularAutomaton ca(g);

ca.findTriplets(hitDoublets, foundTriplets, region, caThetaCut, caPhiCut,
caHardPtCut);

auto & allCells = ca.getAllCells();

unsigned int numberOfFoundTriplets = foundTriplets.size();

Expand All @@ -398,23 +400,23 @@ void CAHitTripletGenerator::hitTriplets(const TrackingRegion& region,
++tripletId)
{

OrderedHitTriplet tmpTriplet(foundTriplets[tripletId][0]->getInnerHit(),
foundTriplets[tripletId][0]->getOuterHit(),
foundTriplets[tripletId][1]->getOuterHit());
OrderedHitTriplet tmpTriplet(allCells[foundTriplets[tripletId][0]].getInnerHit(),
allCells[foundTriplets[tripletId][0]].getOuterHit(),
allCells[foundTriplets[tripletId][1]].getOuterHit());

auto isBarrel = [](const unsigned id) -> bool
{
return id == PixelSubdetector::PixelBarrel;
};
for (unsigned int i = 0; i < 2; ++i)
{
auto const& ahit = foundTriplets[tripletId][i]->getInnerHit();
auto const& ahit = allCells[foundTriplets[tripletId][i]].getInnerHit();
gps[i] = ahit->globalPosition();
ges[i] = ahit->globalPositionError();
barrels[i] = isBarrel(ahit->geographicalId().subdetId());
}

auto const& ahit = foundTriplets[tripletId][1]->getOuterHit();
auto const& ahit = allCells[foundTriplets[tripletId][1]].getOuterHit();
gps[2] = ahit->globalPosition();
ges[2] = ahit->globalPositionError();
barrels[2] = isBarrel(ahit->geographicalId().subdetId());
Expand Down
Expand Up @@ -60,7 +60,7 @@ class CAHitTripletGenerator : public HitTripletGenerator {
// actual work
void hitTriplets(const TrackingRegion& reg, OrderedHitTriplets& result,
std::vector<const HitDoublets *>& hitDoublets,
const CAGraph& g,
CAGraph& g,
const edm::EventSetup& es);

edm::EDGetTokenT<SeedingLayerSetsHits> theSeedingLayerToken;
Expand Down

0 comments on commit fa37d50

Please sign in to comment.