9 #include <trg/cdc/modules/houghtracking/CDCTrigger2DFinderModule.h>
11 #include <cdc/geometry/CDCGeometryPar.h>
12 #include <framework/logging/Logger.h>
13 #include <framework/gearbox/Const.h>
20 #define CDC_SUPER_LAYERS 9
38 setDescription(
"Hough tracking algorithm for CDC trigger.");
39 setPropertyFlags(c_ParallelProcessingCertified);
42 addParam(
"hitCollectionName", m_hitCollectionName,
43 "Name of the input StoreArray of CDCTriggerSegmentHits.",
45 addParam(
"outputCollectionName", m_outputCollectionName,
46 "Name of the StoreArray holding the tracks found in the Hough tracking.",
47 string(
"TRGCDC2DFinderTracks"));
48 addParam(
"clusterCollectionName", m_clusterCollectionName,
49 "Name of the StoreArray holding the clusters formed in the Hough plane.",
51 addParam(
"nCellsPhi", m_nCellsPhi,
52 "Number of Hough cells in phi (limits: [-180, 180]). Must be an even number.",
54 addParam(
"nCellsR", m_nCellsR,
55 "Number of Hough cells in 1/r. Must be an even number.",
57 addParam(
"minPt", m_minPt,
59 "Hough plane limits in 1/r are [-1/r(minPt), 1/r(minPt)]", (
double)(0.3));
60 addParam(
"shiftPt", m_shiftPt,
61 "Shift the Hough plane by 1/4 cell size in 1/r to avoid "
62 "curvature 0 tracks (<0: shift in negative direction, "
63 "0: no shift, >0: shift in positive direction).", 0);
65 addParam(
"minHits", m_minHits,
66 "Minimum hits from different super layers required in a peak cell.",
68 addParam(
"minHitsShort", m_minHitsShort,
69 "Minimum hits required required in a peak cell for a short track"
70 " (must be in the first minHitsShort super layers).",
72 addParam(
"minCells", m_minCells,
73 "Peaks with less than minCells connected cells are ignored.",
75 addParam(
"onlyLocalMax", m_onlyLocalMax,
76 "Switch to remove cells connected to a cell with higher super layer count.",
78 addParam(
"connect", m_connect,
79 "Definition for connected cells. 4: direct (left/right/top/bottom), "
80 "6: direct + 2 diagonal (top right/bottom left), "
81 "8: direct + all diagonal (top right/top left/bottom right/bottom left)",
83 addParam(
"ignore2ndPriority", m_ignore2nd,
84 "Switch to skip second priority hits.",
false);
85 addParam(
"usePriorityPosition", m_usePriority,
86 "If true, use wire position of priority cell in track segment, "
87 "otherwise use wire position of center cell.",
true);
88 addParam(
"requireSL0", m_requireSL0,
89 "Switch to check separately for a hit in the innermost superlayer.",
false);
90 addParam(
"storeHoughPlane", m_storePlane,
91 "Switch for saving Hough plane as TMatrix in DataStore. "
92 "0: don't store anything, 1: store only peaks, 2: store full plane "
93 "(will increase runtime).", (
unsigned)(0));
94 addParam(
"clusterPattern", m_clusterPattern,
95 "use nested pattern algorithm to find clusters",
true);
96 addParam(
"clusterSizeX", m_clusterSizeX,
97 "maximum number of 2 x 2 squares in cluster for pattern algorithm",
99 addParam(
"clusterSizeY", m_clusterSizeY,
100 "maximum number of 2 x 2 squares in cluster for pattern algorithm",
102 addParam(
"hitRelationsFromCorners", m_hitRelationsFromCorners,
103 "Switch for creating relations to hits in the pattern algorithm. "
104 "If true, create relations from cluster corners, otherwise "
105 "from estimated cluster center (might not have relations).",
false);
107 addParam(
"testFilename", m_testFilename,
108 "If not empty, a file with input (hits) and output (tracks) "
109 "for each event is written (for firmware debugging).",
string(
""));
111 addParam(
"suppressClone", m_suppressClone,
112 "Switch to send only the first found track and suppress the "
113 "subsequent clones." ,
false);
117 CDCTrigger2DFinderModule::initialize()
119 m_segmentHits.isRequired(m_hitCollectionName);
120 m_tracks.registerInDataStore(m_outputCollectionName);
121 m_clusters.registerInDataStore(m_clusterCollectionName);
123 m_tracks.registerRelationTo(m_segmentHits);
124 m_tracks.registerRelationTo(m_clusters);
126 if (m_storePlane > 0) m_houghPlane.registerInDataStore(
"HoughPlane");
131 for (
int iSL = 0; iSL < 9; ++iSL) {
133 nTS += cdc.nWiresInLayer(layerId);
134 TSoffset[iSL + 1] = nTS;
135 for (
int priority = 0; priority < 2; ++ priority) {
136 radius[iSL][priority] = cdc.senseWireR(layerId + priority);
138 layerId += (iSL > 0 ? 6 : 7);
141 if (m_testFilename !=
"") {
142 testFile.open(m_testFilename);
145 if (m_suppressClone) {
146 B2INFO(
"2D finder will exit with the first track candidate in time.");
151 CDCTrigger2DFinderModule::event()
158 setReturnValue(
true);
160 if (m_testFilename !=
"") {
161 testFile << StoreObjPtr<EventMetaData>()->getEvent() <<
" "
162 << m_segmentHits.getEntries() << endl;
165 if (m_segmentHits.getEntries() == 0) {
171 for (
int iHit = 0; iHit < m_segmentHits.getEntries(); iHit++) {
172 unsigned short iSL = m_segmentHits[iHit]->getISuperLayer();
173 if (m_testFilename !=
"") {
174 testFile << iSL <<
" " << m_segmentHits[iHit]->getSegmentID() - TSoffset[iSL] <<
" "
175 << m_segmentHits[iHit]->getPriorityPosition() << endl;
177 if (iSL % 2)
continue;
178 if (m_ignore2nd && m_segmentHits[iHit]->getPriorityPosition() < 3)
continue;
179 double phi = m_segmentHits[iHit]->getSegmentID() - TSoffset[iSL];
181 phi += 0.5 * (((m_segmentHits[iHit]->getPriorityPosition() >> 1) & 1)
182 - (m_segmentHits[iHit]->getPriorityPosition() & 1));
184 phi = phi * 2. * M_PI / (TSoffset[iSL + 1] - TSoffset[iSL]);
185 double r = radius[iSL][int(m_usePriority &&
186 m_segmentHits[iHit]->getPriorityPosition() < 3)];
187 TVector2 pos(cos(phi) / r, sin(phi) / r);
188 hitMap.insert(std::make_pair(iHit, std::make_pair(iSL, pos)));
194 maxIterations = ceil(log2(max(m_nCellsPhi, m_nCellsR))) - 1;
195 nCells = pow(2, maxIterations + 1);
197 double rectX = M_PI * nCells / m_nCellsPhi;
199 maxR = 0.5 * Const::speedOfLight * 1.5e-4 / m_minPt;
200 double rectY = maxR * nCells / m_nCellsR;
203 shiftR = -maxR / 2. / m_nCellsR;
204 }
else if (m_shiftPt > 0) {
205 shiftR = maxR / 2. / m_nCellsR;
208 B2DEBUG(50,
"extending Hough plane to " << maxIterations <<
" iterations, "
209 << nCells <<
" cells: phi in ["
210 << -rectX * 180. / M_PI <<
", " << rectX * 180. / M_PI
211 <<
"] deg, 1/r in [" << -rectY + shiftR <<
", " << rectY + shiftR <<
"] /cm");
214 if (m_storePlane > 0) {
215 m_houghPlane.create();
216 m_houghPlane->ResizeTo(m_nCellsPhi, m_nCellsR);
221 if (m_suppressClone && !hitMap.empty()) {
228 typedef pair<int, cdcPair> cdcHitPair;
230 typedef vector<cdcHitPair> cdcSeqMap;
233 for (
auto hit : hitMap) {
234 seqHitMap.push_back(hit);
236 sort(seqHitMap.begin(), seqHitMap.end(), [
this](cdcHitPair i, cdcHitPair j) {
237 return m_segmentHits[i.first]->foundTime() < m_segmentHits[j.first]->foundTime();
239 auto seqHitItr = seqHitMap.begin();
241 vector<bool> layerHit(CDC_SUPER_LAYERS,
false);
244 short firstTick = m_segmentHits[(*seqHitMap.begin()).first]->foundTime() / period + 1;
245 short lastTick = m_segmentHits[(*(seqHitMap.end() - 1)).first]->foundTime() / period + 1;
247 for (
auto tick = firstTick * period; tick < lastTick * period; tick += period) {
249 for (
auto itr = seqHitItr; itr < seqHitMap.end(); ++itr) {
250 cdcHitPair currentHit = *itr;
252 if (count(layerHit.begin(), layerHit.end(),
true) >= m_minHits &&
253 m_segmentHits[currentHit.first]->foundTime() > tick) {
257 layerHit[m_segmentHits[currentHit.first]->getISuperLayer()] =
true;
259 copy_n(seqHitItr, nHitInCycle, inserter(fastHitMap, fastHitMap.end()));
260 fastInterceptFinder(fastHitMap, -rectX, rectX, -rectY + shiftR, rectY + shiftR, 0, 0, 0);
261 B2DEBUG(20,
"at tick " << tick <<
", number of candidates: " << houghCand.size());
262 if (!houghCand.empty()) {
263 B2DEBUG(10,
"found a track at clock " << tick <<
" with "
264 << fastHitMap.size() <<
"hits");
267 advance(seqHitItr, nHitInCycle);
271 fastInterceptFinder(hitMap, -rectX, rectX, -rectY + shiftR, rectY + shiftR, 0, 0, 0);
272 if (!houghCand.empty()) {
273 B2DEBUG(10,
"found a track with " << hitMap.size() <<
"hits");
278 if (m_clusterPattern)
279 patternClustering(fastHitMap);
283 if (m_testFilename !=
"") {
284 testFile << m_tracks.getEntries() << endl;
285 for (
int i = 0; i < m_tracks.getEntries(); ++i) {
286 float ix = (m_tracks[i]->getPhi0() - M_PI_4) * m_nCellsPhi / 2. / M_PI - 0.5;
287 float iy = (m_tracks[i]->getOmega() / 2. + maxR - shiftR) * m_nCellsR / 2. / maxR - 0.5;
288 testFile << round(2 * ix) / 2. <<
" " << round(2 * iy) / 2. <<
" "
289 << m_tracks[i]->getChargeSign() << endl;
292 testFile << hits.size() << endl;
293 for (
unsigned ihit = 0; ihit < hits.size(); ++ihit) {
294 unsigned short iSL = hits[ihit]->getISuperLayer();
295 testFile << iSL <<
" " << hits[ihit]->getSegmentID() - TSoffset[iSL] <<
" "
296 << hits[ihit]->getPriorityPosition() <<
" "
297 << hits.weight(ihit) << endl;
304 CDCTrigger2DFinderModule::terminate()
306 if (m_testFilename !=
"") testFile.close();
Combination of several CDCHits to a track segment hit for the trigger.
The Class for CDC Geometry Parameters.
Class for type safe access to objects that are referred to in relations.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
std::map< int, cdcPair > cdcMap
Map of <counter, cdcPair>, for hits with indices.
Abstract base class for different kinds of events.