ATLAS Offline Software
FPGATrackSimHough1DShiftTool.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
22 #include "TH2.h"
23 
24 #include <sstream>
25 #include <cmath>
26 #include <algorithm>
27 #include <boost/dynamic_bitset.hpp>
28 #include <iostream>
29 
30 static inline std::string to_string(std::vector<size_t> v);
31 static inline boost::dynamic_bitset<> lshift(boost::dynamic_bitset<> const & b, int n);
32 static inline boost::dynamic_bitset<> rshift(boost::dynamic_bitset<> const & b, int n);
33 static inline void updateBinHits(std::vector<boost::dynamic_bitset<>> & binHits, unsigned layer, boost::dynamic_bitset<> const & b);
34 static inline int layersHit(FPGATrackSimRoad& r);
35 
37 // AthAlgTool
38 
39 FPGATrackSimHough1DShiftTool::FPGATrackSimHough1DShiftTool(const std::string& algname, const std::string &name, const IInterface *ifc) :
40  base_class(algname, name, ifc)
41 {
42  declareInterface<IFPGATrackSimRoadFinderTool>(this);
43 }
44 
45 
47 {
48  // Config printout
49  ATH_MSG_INFO("Phi range: (" << m_phiMin << "," << m_phiMax << "," << m_phiBins << ")");
50  if (m_useDiff) ATH_MSG_INFO("useDiff Set True");
51  if (m_variableExtend) ATH_MSG_INFO("variableExtend Set True");
52  ATH_MSG_INFO("enhancedHighPt " << m_enhanceHighPt.value());
53  ATH_MSG_INFO("applyDropable " << m_applyDropable.value());
54  ATH_MSG_INFO("threshold " << m_threshold.value());
55 
56  // Retrieve info
58  ATH_CHECK(m_FPGATrackSimMapping.retrieve());
59  m_nLayers = m_FPGATrackSimMapping->PlaneMap_1st(0)->getNLogiLayers();
60 
61  // Error checking
62  if (m_phiMin >= m_phiMax || m_phiBins == 0u) {
63  ATH_MSG_FATAL("initialize() Phi range invalid");
64  return StatusCode::FAILURE;
65  }
66 
67  // Fix inputs
68  if (!m_hitExtendProperty.value().empty()) {
69  if (m_hitExtendProperty.value().size() != m_nLayers) {
70  ATH_MSG_FATAL("initialize() Hit extend must have size == nLayers");
71  return StatusCode::FAILURE;
72  } else {
74  }
75  } else {
76  m_hitExtend.resize(m_nLayers,0); // all 0
77  }
78  if (m_iterStep == 0u) m_iterStep = m_hitExtend[m_iterLayer] * 2 + 1; // default 1
79 
80  // Copy correct r values from the region map.
81  m_r.resize(m_nLayers);
82  for (unsigned ilayer = 0; ilayer < m_nLayers; ilayer++) {
83  m_r[ilayer] = m_FPGATrackSimMapping->SubRegionMap()->getAvgRadius(m_subRegion, ilayer);
84  }
85 
86  // Warnings / corrections
87  if (m_idealGeoRoads)
88  {
89  if (m_useSectors)
90  {
91  ATH_MSG_WARNING("initialize() idealGeoRoads conflicts with useSectors, switching off FPGATrackSim sector matching");
92  m_useSectors = false;
93  }
94  if (!m_traceHits)
95  {
96  ATH_MSG_WARNING("initialize() idealGeoRoads requires tracing hits, turning on automatically");
97  m_traceHits = true;
98  }
99  }
100 
101  // Fill convenience variables
103  for (unsigned i = 0; i <= m_phiBins; i++)
104  m_bins.push_back(m_phiMin + m_phiStep * i);
105  m_regionMin = m_EvtSel->getRegions()->getMin(m_EvtSel->getRegionID());
106  m_regionMax = m_EvtSel->getRegions()->getMax(m_EvtSel->getRegionID());
107 
108  m_regionMin.phi = m_regionMin.phi - 0.01; // For phi boundary effect
109  m_regionMax.phi = m_regionMax.phi + 0.01; // For phi boundary effect
110 
111  m_currentcounts.resize(m_phiBins);
112 
113  // Calculate the shift amounts
114  if (m_bitShift_path.value().empty()) calculateShifts();
116  printShifts();
117 
118 
119  return StatusCode::SUCCESS;
120 }
121 
122 
123 // Fills m_shifts
125 {
126  // Calculated d0shift if selected
127  if (m_d0spread>0) calculated0Shifts();
128 
129 
130  // Iterate over a steps of m_iterStep bin shift in m_iterLayer
131  for (int sign = -1; sign<2; sign+=2) {
132 
133  // start at highest pT
134  float iterShift = (sign>0) ? 0.0 : -1.0; // don't double count zero
135 
136  while (true)
137  {
138  float qpt = qPt(m_r[m_iterLayer], m_phiStep * iterShift);
139  if (abs(qpt)>std::max(abs(m_qptMin),abs(m_qptMax))) break;
140 
141  if ((qpt <= m_qptMax ) && (qpt >= m_qptMin )) {
142 
143  std::vector<int> shifts; // Fill both +iterShift and -iterShift here
144  for (unsigned i = 0; i < m_nLayers; i++)
145  {
146  float dPhi = deltaPhi(m_r[i], qpt);
147  if (m_useDiff){
148  dPhi -= deltaPhi(m_r[0], qpt);
149  }
150 
151  int shift = static_cast<int>(round(dPhi / m_phiStep));
152  if (shift >= static_cast<int>(m_phiBins) || shift <= -(static_cast<int>(m_phiBins))) return;
153  shifts.push_back(shift);
154  }
155 
156  // if thre are d0shifts, apply them
157  for (const std::vector<int>& d0shift: m_d0shifts){
158  m_shifts.push_back(applyVariation(shifts,d0shift,1));
159  m_shifts.push_back(applyVariation(shifts,d0shift,-1));
160  }
161 
162  m_qpt.push_back(qpt);
163  m_shifts.push_back(shifts);
164 
165  }
166 
167  // smaller steps if in "enhanceHighPt" range
168  iterShift += ((std::abs(qpt)>m_enhanceHighPt) ? sign*float(m_iterStep): sign*float(m_iterStep)/2.0) ;
169  }
170  ATH_MSG_INFO("Ending shift, qpt("<< m_qptMin << ", " << m_qptMax <<") sign:" << sign << " shift:" << iterShift);
171 
172 
173  // figure out which patterns are the same after dropping a hit
175  }
176 
177 }
178 
179 
180 // Fills m_shifts
181 //
182 // This reads a bitshift pattern file, which is a simple space-separated text file
183 // where each row respresents one shift set / pattern. Each row contains
184 // (nLayers - 1) columns representing the shifts in layers [1, nLayers)
185 // (implicit 0 shift in layer 0), followed by a column that is a 'droplist'.
186 // The droplist is a string of 0s and 1s indicating which layers can be dropped
187 // for 4/5 matching, i.e (layer 0 is right-most bit).
188 //
189 // See /eos/atlas/atlascerngroupdisk/det-htt/sectors_constants/BitShiftHoughPatterns/2021-02-20/
190 void FPGATrackSimHough1DShiftTool::readShifts(std::string const & filepath)
191 {
192  // Open the file
193  std::ifstream fin(filepath);
194  if (!fin.is_open())
195  {
196  ATH_MSG_FATAL("Couldn't open " << filepath);
197  throw ("FPGATrackSimHough1DShiftTool::readShifts couldn't open " + filepath);
198  }
199 
200  // Variables to fill
201  std::string line;
202  bool ok = true;
203  int subregion = -1;
204  bool newsubr = false;
205  bool headerdone = false;
206  int nslices = 0;
207  int nphi=0;
208 
209 
210  // Parse the file
211  while (getline(fin, line))
212  {
213  newsubr|=(line.empty());
214  if (line.empty() || line[0] == '#') continue;
215  std::istringstream sline(line);
216  if (!headerdone) {
217  std::string towers, phi;
218  ok = ok && (sline >> towers);
219  if (towers!="towers") {
220  ATH_MSG_FATAL("Parse Error expected 'towers' got "<< towers);
221  throw ("FPGATrackSimHough1DShiftTool::readShifts couldn't parse " + filepath);
222  }
223  ok = ok && (sline >> nslices);
224  ATH_MSG_INFO("FPGATrackSimHough1DShiftTool::readShifts " << nslices << "slices in shift file");
225  ok = ok && (sline >> phi);
226  if (phi!="phi") {
227  ATH_MSG_FATAL("Parse Error expected 'phi' got "<< phi);
228  throw ("FPGATrackSimHough1DShiftTool::readShifts couldn't parse " + filepath);
229  }
230  ok = ok && (sline >> nphi);
231  headerdone=true;
232  continue;
233  }
234 
235  if (newsubr) {
236  ok = ok && (sline >> subregion);
237  ATH_MSG_INFO("FPGATrackSimHough1DShiftTool::readShifts " << subregion << " looking for " << m_subRegion);
238  newsubr=false;
239  continue;
240  }
241 
242  if (subregion==m_subRegion) {
243  ATH_MSG_INFO("FPGATrackSimHough1DShiftTool::readShifts found subregion " << m_subRegion);
244  // Shifts
245  std::vector<int> shifts;
246  shifts.push_back(0); // layer 0 is implicit 0 TODO
247  for (unsigned layer = 1; layer < m_nLayers; layer++)
248  {
249  int shift = 0;
250  ok = ok && (sline >> shift);
251  if (!ok) break;
252  shifts.push_back(shift);
253  }
254  if (!ok) break;
255  if (shifts[0]<-1000) {
256  ATH_MSG_INFO("FPGATrackSimHough1DShiftTool::readShifts skipping line with shift out of range: " << line);
257  continue;
258  }
259 
260  // Dropable
261  std::string droplist;
262  ok = ok && (sline >> droplist);
263  ok = ok && (droplist.size() == m_nLayers);
264  if (!ok) break;
265 
266  boost::dynamic_bitset<> drops(m_nLayers);
267  for (unsigned i = 0; i < m_nLayers; i++)
268  if (droplist[i] == '1')
269  drops[m_nLayers - i - 1] = true; // Droplist is printed with MSB on left
270 
271  // qpT
272  float qpt = 0;
273  ok = ok && (sline >> qpt);
274  if (!ok) break;
275  if ((qpt<m_qptMin) or (qpt>m_qptMax)) continue;
276 
277  // Phi Vals
278  std::vector<float> phivals;
279  for (unsigned layer = 0; layer < m_nLayers; layer++)
280  {
281  float phival = 0;
282  ok = ok && (sline >> phival);
283  if (!ok) break;
284  phivals.push_back(phival);
285  }
286 
287  m_qpt.push_back(qpt);
288  m_shifts.push_back(shifts);
289  m_dropable.push_back(drops);
290  m_phivals.push_back(phivals);
291 
292 
293  }
294  }
295 
296  if (!ok)
297  {
298  ATH_MSG_FATAL("Found error reading file at line: " << line);
299  throw "FPGATrackSimHough1DShiftTool::readShifts read error";
300  }
301 
302  if (m_shifts.size()==0) ATH_MSG_FATAL("FPGATrackSimHough1DShiftTool::readShifts no shifts read");
303  ATH_MSG_INFO("Read " << m_shifts.size() << " patterns from " << filepath);
304 }
305 
306 
308 {
309  return StatusCode::SUCCESS;
310 }
311 
312 
314 // Main Algorithm
315 
316 StatusCode FPGATrackSimHough1DShiftTool::getRoads(const std::vector<std::shared_ptr<const FPGATrackSimHit>> & hits, std::vector<std::shared_ptr<const FPGATrackSimRoad>> & roads)
317 {
318 
319  roads.clear();
320  m_roads.clear();
321 
322  ATH_MSG_DEBUG("Input Hits Size:" << hits.size() << "\n");
323 
324  // Get hit masks
325  std::vector<boost::dynamic_bitset<>> hitMasks(makeHitMasks(hits));
326  if (m_drawHitMasks) drawHitMasks(hitMasks, m_name + "_e" + std::to_string(m_event));
327 
328  // Iterate over shifts
329  for (size_t iShift = 0; iShift < m_shifts.size(); iShift++)
330  {
331  // Track hit layers in each bin
332  std::vector<boost::dynamic_bitset<>> binHits(m_phiBins, boost::dynamic_bitset<>(m_nLayers));
333 
334 
335  // Add the counts
336  for (unsigned i = 0; i < m_nLayers; i++)
337  updateBinHits(binHits, i, lshift(hitMasks[i], m_shifts[iShift][i]));
338 
339  // Check the threshold
340  for (unsigned bin = 0; bin < m_phiBins; bin++)
341  {
342  // First check we're not missing hits in not allowed layers
343  if (m_applyDropable && !m_dropable.empty() && (~(binHits[bin] | m_dropable[iShift])).any()) continue;
344 
345  m_currentcounts[bin]=binHits[bin].count();
346 
347  if (passThreshold(binHits,bin))
348  {
349  FPGATrackSimRoad r = makeRoad(hits, bin, iShift);
350 
351  if (m_phiRangeCut && (r.getX() > m_regionMax.phi || r.getX() < m_regionMin.phi)) continue;
352 
353  m_roads.push_back(r);
354 
355  ATH_MSG_DEBUG("New Road: "<< layersHit(r) <<" lyrs, " << r.getNHits() << " hits :"
356  << to_string(r.getNHits_layer()) << " reg=" << r.getSubRegion()
357  << " bin=" << bin << " shift=" << iShift << " phi=" << r.getX() << " pti=" << r.getY() << " " << m_qpt[iShift]);
358 
359 
360  }
361 
362  }
363 
364  // Add/Remove history
365  m_vetolist.push_front(m_currentcounts);
366  if (m_vetolist.size() > m_historyWindow) {
367  m_vetolist.pop_back();
368  }
369  }
370 
371  roads.reserve(m_roads.size());
372  for (FPGATrackSimRoad & r : m_roads) roads.emplace_back(std::make_shared<const FPGATrackSimRoad>(r));
373 
374  if (roads.size()==0 && m_drawHitMasks) drawHitMasks(hitMasks, m_name + "_fail_e" + std::to_string(m_event));
375 
376  m_event++;
377  return StatusCode::SUCCESS;
378 }
379 
380 
381 
382 bool FPGATrackSimHough1DShiftTool::passThreshold(std::vector<boost::dynamic_bitset<>>& binHits, int bin ) const
383 {
384  // Check the threshold
385  if (binHits[bin].count() < m_threshold) return false;
386 
387  // Apply neighbor veto
388  if (m_neighborWindow>0) {
389  for (int i = -m_neighborWindow; i <= m_neighborWindow; i++) {
390  if ((bin+i < 0) or ((bin+i)>=int(m_phiBins))) continue; // ignore edges
391  if (binHits[bin+i].count()>binHits[bin].count()) {
392  // neighbor has more layers
393  return false;
394  }
395  if (i!=0 && (binHits[bin+i].count()==binHits[bin].count())) {
396  // favor lower phi bin
397  return false;
398  }
399  for (unsigned j = 0; j < m_vetolist.size(); j++) {
400  if (m_vetolist[j][bin+i]>binHits[bin].count()) {
401  // past neighbor has more layers
402  return false;
403  }
404  if ((m_vetolist[j][bin+i]==binHits[bin].count()) and (i<0)) {
405  // favor previous shift and lower phi bin
406  return false;
407  }
408  }
409  }
410  }
411 
412  return true; // passed threshold, wasn't vetoed by neighbors
413 
414 }
415 
416 
417 
418 std::vector<boost::dynamic_bitset<>> FPGATrackSimHough1DShiftTool::makeHitMasks(const std::vector<std::shared_ptr<const FPGATrackSimHit>> & hits)
419 {
420  std::vector<boost::dynamic_bitset<>> hitMasks(m_nLayers, boost::dynamic_bitset<>(m_phiBins));
421  for (auto const &hit : hits)
422  {
423  if (m_subRegion >= 0 && !m_FPGATrackSimMapping->SubRegionMap()->isInRegion(m_subRegion, *hit)) continue;
424 
425  auto bins = getBins(hit);
426 
427  for (int i = std::max(bins.first, 0); i <= std::min(bins.second, (int)m_phiBins - 1); i++)
428  hitMasks[hit->getLayer()][i] = true;
429  }
430 
431  return hitMasks;
432 }
433 
434 
435 FPGATrackSimRoad FPGATrackSimHough1DShiftTool::makeRoad(const std::vector<std::shared_ptr<const FPGATrackSimHit>>& hits, int bin_track, size_t iShift)
436 {
437  std::vector<int> const & shifts = m_shifts[iShift];
438  float qpT = m_qpt[iShift];
439 
440  std::vector<std::shared_ptr<const FPGATrackSimHit>> road_hits;
441  layer_bitmask_t hitLayers = 0;
442 
443  for (const auto & hit : hits)
444  {
445  if (m_subRegion >= 0 && !m_FPGATrackSimMapping->SubRegionMap()->isInRegion(m_subRegion, *hit)) continue;
446 
447  // Get the shifted bins of the hit
448  auto bins = getBins(hit);
449  bins.first += shifts[hit->getLayer()]; // note boost::dynamic_bitset stores [0] as the LSB, i.e. rightmost. So leftshift = +bin
450  bins.second += shifts[hit->getLayer()];
451 
452  // Check if it's the same bin as the track
453  if (bin_track >= bins.first && bin_track <= bins.second)
454  {
455  road_hits.push_back(hit);
456  hitLayers |= 1 << hit->getLayer();
457  }
458  }
459 
460  auto sorted_hits = ::sortByLayer(road_hits);
461  sorted_hits.resize(m_nLayers); // If no hits in last layer, return from sortByLayer will be too short
462  // Combination of last two addRoad() functions from FPGATrackSimHoughTransformTool.cxx are implemented above (up to here)
463  // Below want to look at first addRoad()/matchIdealGeoSector() function in FPGATrackSimHoughTransformTool.cxx
464 
466  r.setHitLayers(hitLayers);
467  r.setHits(std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>>(std::move(sorted_hits)));
468  r.setSubRegion(m_subRegion);
469  if (m_fieldCorrection) {
470  int inner_bin = static_cast<int>(bin_track)-static_cast<int>(shifts[0]);
471  if ((inner_bin < 0) || (inner_bin >= static_cast<int>(m_phiBins))) {
472  r.setX(-100000);
473  } else {
474  r.setX(m_bins[inner_bin] + 0.5*m_phiStep + deltaPhi(m_r[0],qpT));
475  }
476  } else {
477  r.setX(phitrk(bin_track,shifts).first); // will be a large negative number if invalid
478  }
479  r.setY(qpT);
480 
481  return r;
482  // TODO sector, wildcard layers?
483 }
484 
485 
487 // Helpers
488 
489 // Given a relative shift between iterLayer and layer 0, returns the corresponding qpt.
490 // This does a linear approximation of the Hough transform equation.
492 {
493  if (m_iterLayer == 0u) ATH_MSG_FATAL("getPtFromShiftDiff() iterLayer can't be 0");
494  return (shift * m_phiStep / fpgatracksim::A) / (m_r[m_iterLayer] - m_r[0]);
495 }
496 
497 
498 // Returns the range of bins (inclusive) given a phi and an extension in number of bins
499 std::pair<int, int> FPGATrackSimHough1DShiftTool::getBins(const std::shared_ptr<const FPGATrackSimHit>& hit) const
500 {
501  float phi = hit->getGPhi();
502  float bin_extend = m_hitExtend[hit->getLayer()];
503  float center = (phi - m_phiMin) / m_phiStep;
504 
505  if (m_variableExtend){
506  float r = hit->getR();
507  float dphimax = deltaPhi(r,m_qptMax);
508  float maxshift = +1*((std::sin(dphimax)*(r-m_r[hit->getLayer()]))/r)/m_phiStep;
509  float dphimin = deltaPhi(r,m_qptMin);
510  float minshift = +1*((std::sin(dphimin)*(r-m_r[hit->getLayer()]))/r)/m_phiStep;
511 
512  center += (maxshift+minshift)/2.0;
513  bin_extend += std::abs((maxshift-minshift)/2.0);
514 
515  }
516 
517  int low = std::max(static_cast<int>(center - bin_extend),0);
518  int high = std::min(static_cast<int>(center + bin_extend),static_cast<int>(m_phiBins-1));
519  return { low, high };
520 }
521 
523 {
524  if (m_useDiff) {
525  float r1=m_r[0];
526  float r2=r;
527  float sign = deltaPhi>0 ? 1 : -1;
528  return sign*1/(2*fpgatracksim::A) * std::sqrt((4*std::sin(deltaPhi)*std::sin(deltaPhi))/( r1*r1 + r2*r2 -2*r2*r1*std::cos(deltaPhi)));
529  }
530  return std::sin(deltaPhi) / (fpgatracksim::A * r);
531 }
532 
533 float FPGATrackSimHough1DShiftTool::phitrkDiff(float r1, float phi1, float r2, float phi2) const
534 {
535  float phi_track = phi1+ std::atan2(r2-r1*std::cos(phi2-phi1),r1*std::sin(phi2-phi1)) - TMath::Pi()/2.0;
536  return phi_track;
537 }
538 
539 
540 std::pair<float, bool> FPGATrackSimHough1DShiftTool::phitrk(int bin, std::vector<int> const & shifts ) const
541 {
542  int inner_bin = static_cast<int>(bin)-static_cast<int>(shifts[0]);
543  int outer_bin = static_cast<int>(bin)-static_cast<int>(shifts[m_nLayers-1]);
544  if ((inner_bin < 0) || (inner_bin >= static_cast<int>(m_phiBins))) return {-100000,false};
545  if ((outer_bin < 0) || (outer_bin >= static_cast<int>(m_phiBins))) return {-100000,false};
546  float phi1 = m_bins[inner_bin];
547  float phi2 = m_bins[outer_bin];
548  float phi_track = phitrkDiff(m_r[0],phi1,m_r[m_nLayers-1],phi2)+0.5*m_phiStep;
549  return {phi_track,true};
550 }
551 
552 
553 float FPGATrackSimHough1DShiftTool::deltaPhi(float r, float qPt) const
554 {
555  float dPhi = std::asin(fpgatracksim::A * r * qPt);
556  if (m_fieldCorrection) dPhi += fieldCorrection(m_EvtSel->getRegionID(), qPt, r);
557  return dPhi;
558 }
559 
560 
561 static inline std::string to_string(std::vector<size_t> v)
562 {
563  std::ostringstream oss;
564  oss << "[";
565  if (!v.empty())
566  {
567  std::copy(v.begin(), v.end()-1, std::ostream_iterator<size_t>(oss, ", "));
568  oss << v.back();
569  }
570  oss << "]";
571  return oss.str();
572 }
573 
574 
575 static inline boost::dynamic_bitset<> lshift(boost::dynamic_bitset<> const & b, int n)
576 {
577  if (n < 0) return rshift(b, -n);
578  return b << n;
579 }
580 
581 static inline boost::dynamic_bitset<> rshift(boost::dynamic_bitset<> const & b, int n)
582 {
583  if (n < 0) return lshift(b, -n);
584 
585  boost::dynamic_bitset<> out(b >> n);
586 
587  // Force 0-padding
588  for (int i = 0; i < n; i++)
589  out[out.size() - 1 - i] = false;
590 
591  return out;
592 }
593 
594 static inline void updateBinHits(std::vector<boost::dynamic_bitset<>> & binHits, unsigned layer, boost::dynamic_bitset<> const & b)
595 {
596  for (size_t i = 0; i < b.size(); i++)
597  if (b[i]) binHits[i].set(layer);
598 }
599 
600 void FPGATrackSimHough1DShiftTool::printHitMasks(std::vector<boost::dynamic_bitset<>> const & hitMasks) const
601 {
602  std::stringstream ss;
603  ss << "Hit Masks:\n";
604  for (auto const & b : hitMasks)
605  {
606  ss << "\t";
607  for (size_t i = 0; i < b.size(); i++)
608  {
609  if (b[i]) ss << 1;
610  else ss << ".";
611  }
612  ss << "\n";
613  }
614  ATH_MSG_DEBUG(ss.str() << "\n\n");
615 }
616 
617 void FPGATrackSimHough1DShiftTool::drawHitMasks(std::vector<boost::dynamic_bitset<>> const & hitMasks, std::string const & name)
618 {
619  m_monitorFile.cd();
620 
621  TH2F h(name.c_str(), "Hough 1D Shift;phi;layer",
623  m_nLayers, 0, m_nLayers
624  );
625 
626  for (size_t layer = 0; layer < m_nLayers; layer++)
627  for (size_t i = 0; i < m_phiBins; i++)
628  h.SetBinContent(i+1, layer+1, hitMasks[layer][i]); // +1 since root bins are 1-indexed
629 
630  h.Write();
631 }
632 
633 void FPGATrackSimHough1DShiftTool::drawHitMasks(std::vector<boost::dynamic_bitset<>> const & hitMasks, std::string const & name, std::vector<int> const & shifts)
634 {
635  std::vector<boost::dynamic_bitset<>> shiftedMasks;
636  for (size_t layer = 0; layer < m_nLayers; layer++)
637  shiftedMasks.push_back(lshift(hitMasks[layer], shifts[layer]));
638 
639  drawHitMasks(shiftedMasks, name);
640 }
641 
643 {
644  std::stringstream ss;
645  ss << "Shifts:\n";
646  for (size_t i = 0; i < m_shifts.size(); i++)
647  {
648  float qpt = qPt(m_r[m_iterLayer], m_phiStep * m_shifts[i][m_iterLayer]); // q/pT represented by this shift
649  ss << "q/pT=" << qpt << "; ";
650  for (int shift : m_shifts[i]) ss << shift << " ";
651  if (!m_dropable.empty()) ss << m_dropable[i];
652  ss << "\n";
653  }
654  ATH_MSG_VERBOSE(ss.str());
655 }
656 
657 
659 {
660  int steps=1;
661  while (true) {
662  float d0step = steps*m_phiStep*m_r[0];
663  ATH_MSG_DEBUG("d0step = " << d0step);
664  std::vector<int> d0shift;
665  for (unsigned lyr = 0; lyr < m_nLayers; lyr++) {
666  float phi_for_d0step = d0step/m_r[lyr];
667  int shift = static_cast<int>(round(phi_for_d0step/ m_phiStep));
668  d0shift.push_back(shift);
669  }
670  m_d0shifts.push_back(d0shift);
671  if (d0step>m_d0spread) break;
672  ++steps;
673  }
674  ATH_MSG_DEBUG("d0 Shifts Found = " << m_d0shifts.size());
675 }
676 
677 std::vector<int> FPGATrackSimHough1DShiftTool::applyVariation(const std::vector<int>& base, const std::vector<int>& var, int sign) const
678 {
679  std::vector<int> retv;
680 
681  if (base.size()!=var.size()) {
682  ATH_MSG_ERROR("Base and Var lengths must match " << base.size() << " " << var.size() );
683  }
684 
685  for (unsigned i = 0; i < base.size(); i++) {
686  retv.push_back(base[i]+sign*var[i]);
687  }
688  return retv;
689 }
690 
691 std::vector<int> FPGATrackSimHough1DShiftTool::shiftWithDrop(std::vector<int>& shift,unsigned droplayer) const
692 {
693  std::vector<int> retv(m_nLayers-1);
694  for (unsigned lyr = 0; lyr < m_nLayers; lyr++) {
695  if (lyr !=droplayer) retv.push_back(shift[lyr]);
696  }
697  return retv;
698 }
699 
701 {
702 
703  // one set per layer with shift set
704  std::vector< std::set< std::vector<int> > > reducedShifts(m_nLayers);
705 
706  m_dropable.resize(m_shifts.size());
707 
708  for (size_t iShift = 0; iShift < m_shifts.size(); iShift++)
709  {
710  boost::dynamic_bitset<> drops(m_nLayers);
711  for (unsigned droplayer =0; droplayer < m_nLayers; droplayer++) {
712  if (reducedShifts[droplayer].insert(shiftWithDrop(m_shifts[iShift],droplayer)).second) {
713  // true if the is a new shift in the set
714  drops.set(droplayer);
715  }
716  }
717 
718  m_dropable[iShift]=drops;
719 
720  }
721 
722 }
723 
724 
725 static inline int layersHit(FPGATrackSimRoad& r) {
726  int cnt =0;
727  for (unsigned i = 0; i < r.getNLayers(); i++) {
728  if (r.getHitLayers() & (1<<i)) cnt++;
729  }
730  return cnt;
731 }
FPGATrackSimHough1DShiftTool::printHitMasks
void printHitMasks(std::vector< boost::dynamic_bitset<>> const &hitMasks) const
Definition: FPGATrackSimHough1DShiftTool.cxx:600
FPGATrackSimHough1DShiftTool::deltaPhi
float deltaPhi(float r, float qPt) const
Definition: FPGATrackSimHough1DShiftTool.cxx:553
base
std::string base
Definition: hcg.cxx:78
FPGATrackSimHough1DShiftTool::m_traceHits
Gaudi::Property< bool > m_traceHits
Definition: FPGATrackSimHough1DShiftTool.h:112
beamspotman.r
def r
Definition: beamspotman.py:676
FPGATrackSimHough1DShiftTool::calculated0Shifts
void calculated0Shifts()
Definition: FPGATrackSimHough1DShiftTool.cxx:658
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
getMenu.algname
algname
Definition: getMenu.py:54
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
FPGATrackSimHough1DShiftTool::m_fieldCorrection
Gaudi::Property< bool > m_fieldCorrection
Definition: FPGATrackSimHough1DShiftTool.h:142
FPGATrackSimHough1DShiftTool::makeRoad
FPGATrackSimRoad makeRoad(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits, int bin_track, size_t iShift)
Definition: FPGATrackSimHough1DShiftTool.cxx:435
module_driven_slicing.nslices
nslices
Definition: module_driven_slicing.py:575
checkFileSG.line
line
Definition: checkFileSG.py:75
FPGATrackSimTrackPars::phi
double phi
Definition: FPGATrackSimTrackPars.h:24
FPGATrackSimRegionSlices.h
Stores slice definitions for FPGATrackSim regions.
FPGATrackSimHough1DShiftTool::m_regionMax
FPGATrackSimTrackPars m_regionMax
Definition: FPGATrackSimHough1DShiftTool.h:160
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FPGATrackSimPlaneMap.h
Maps physical layers to logical layers.
FPGATrackSimHough1DShiftTool::m_phiStep
float m_phiStep
Definition: FPGATrackSimHough1DShiftTool.h:154
FPGATrackSimHough1DShiftTool::calculateShifts
void calculateShifts()
Definition: FPGATrackSimHough1DShiftTool.cxx:124
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
FPGATrackSimHough1DShiftTool::m_threshold
Gaudi::Property< unsigned > m_threshold
Definition: FPGATrackSimHough1DShiftTool.h:128
xAOD::deltaPhi
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap setEtaBin setIsTgcFailure setDeltaPt deltaPhi
Definition: L2StandAloneMuon_v1.cxx:160
FPGATrackSimHough1DShiftTool::phitrk
std::pair< float, bool > phitrk(int bin, std::vector< int > const &shifts) const
Definition: FPGATrackSimHough1DShiftTool.cxx:540
FPGATrackSimHough1DShiftTool::m_qpt
std::vector< float > m_qpt
Definition: FPGATrackSimHough1DShiftTool.h:166
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
IFPGATrackSimMappingSvc.h
FPGATrackSimHoughTransformTool.h
Implements road finding using a Hough transform.
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
FPGATrackSimHough1DShiftTool::m_regionMin
FPGATrackSimTrackPars m_regionMin
Definition: FPGATrackSimHough1DShiftTool.h:159
FPGATrackSimHit::getLayer
unsigned getLayer() const
Definition: FPGATrackSimHit.cxx:77
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
FPGATrackSimHough1DShiftTool::m_phiMax
Gaudi::Property< float > m_phiMax
Definition: FPGATrackSimHough1DShiftTool.h:123
bin
Definition: BinsDiffFromStripMedian.h:43
FPGATrackSimHough1DShiftTool::m_phiRangeCut
Gaudi::Property< bool > m_phiRangeCut
Definition: FPGATrackSimHough1DShiftTool.h:134
FPGATrackSimHough1DShiftTool::m_FPGATrackSimMapping
ServiceHandle< IFPGATrackSimMappingSvc > m_FPGATrackSimMapping
Definition: FPGATrackSimHough1DShiftTool.h:107
FPGATrackSimHough1DShiftTool::m_vetolist
std::deque< std::vector< unsigned > > m_vetolist
Definition: FPGATrackSimHough1DShiftTool.h:170
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
FPGATrackSimHough1DShiftTool.h
Implements road finding using Elliot's simplified Hough transform.
FPGATrackSimHough1DShiftTool::m_subRegion
Gaudi::Property< int > m_subRegion
Definition: FPGATrackSimHough1DShiftTool.h:121
FPGATrackSimHough1DShiftTool::m_bins
std::vector< double > m_bins
Definition: FPGATrackSimHough1DShiftTool.h:155
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
FPGATrackSimHough1DShiftTool::m_applyDropable
Gaudi::Property< bool > m_applyDropable
Definition: FPGATrackSimHough1DShiftTool.h:139
FPGATrackSimConstants.h
FPGATrackSimHough1DShiftTool::m_currentcounts
std::vector< unsigned > m_currentcounts
Definition: FPGATrackSimHough1DShiftTool.h:169
FPGATrackSimHough1DShiftTool::shiftWithDrop
std::vector< int > shiftWithDrop(std::vector< int > &shift, unsigned droplayer) const
Definition: FPGATrackSimHough1DShiftTool.cxx:691
FPGATrackSimHough1DShiftTool::m_event
unsigned m_event
Definition: FPGATrackSimHough1DShiftTool.h:181
FPGATrackSimHough1DShiftTool::m_neighborWindow
Gaudi::Property< int > m_neighborWindow
Definition: FPGATrackSimHough1DShiftTool.h:140
FPGATrackSimHit::getGPhi
float getGPhi() const
Definition: FPGATrackSimHit.h:141
FPGATrackSimRegionMap.h
Maps ITK module indices to FPGATrackSim regions.
FPGATrackSimHough1DShiftTool::m_dropable
std::vector< boost::dynamic_bitset<> > m_dropable
Definition: FPGATrackSimHough1DShiftTool.h:163
FPGATrackSimHough1DShiftTool::drawHitMasks
void drawHitMasks(std::vector< boost::dynamic_bitset<>> const &hitMasks, std::string const &name)
Definition: FPGATrackSimHough1DShiftTool.cxx:617
FPGATrackSimHough1DShiftTool::finalize
virtual StatusCode finalize() override
Definition: FPGATrackSimHough1DShiftTool.cxx:307
beamspotman.steps
int steps
Definition: beamspotman.py:505
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
FPGATrackSimHough1DShiftTool::m_variableExtend
Gaudi::Property< bool > m_variableExtend
Definition: FPGATrackSimHough1DShiftTool.h:133
FPGATrackSimHough1DShiftTool::m_shifts
std::vector< std::vector< int > > m_shifts
Definition: FPGATrackSimHough1DShiftTool.h:162
FPGATrackSimHough1DShiftTool::m_useSectors
Gaudi::Property< bool > m_useSectors
Definition: FPGATrackSimHough1DShiftTool.h:116
FPGATrackSimHough1DShiftTool::qPt
float qPt(float r, float deltaPhi) const
Definition: FPGATrackSimHough1DShiftTool.cxx:522
FPGATrackSimHough1DShiftTool::m_d0spread
Gaudi::Property< float > m_d0spread
Definition: FPGATrackSimHough1DShiftTool.h:136
lumiFormat.i
int i
Definition: lumiFormat.py:85
FPGATrackSimHough1DShiftTool::m_hitExtendProperty
Gaudi::Property< std::vector< float > > m_hitExtendProperty
Definition: FPGATrackSimHough1DShiftTool.h:137
beamspotman.n
n
Definition: beamspotman.py:731
FPGATrackSimHough1DShiftTool::m_historyWindow
Gaudi::Property< unsigned > m_historyWindow
Definition: FPGATrackSimHough1DShiftTool.h:141
FPGATrackSimHough1DShiftTool::FPGATrackSimHough1DShiftTool
FPGATrackSimHough1DShiftTool(const std::string &, const std::string &, const IInterface *)
Definition: FPGATrackSimHough1DShiftTool.cxx:39
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
extractSporadic.h
list h
Definition: extractSporadic.py:97
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
TauGNNUtils::Variables::Track::dPhi
bool dPhi(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:538
FPGATrackSimHough1DShiftTool::m_useDiff
Gaudi::Property< bool > m_useDiff
Definition: FPGATrackSimHough1DShiftTool.h:132
FPGATrackSimHough1DShiftTool::m_idealGeoRoads
Gaudi::Property< bool > m_idealGeoRoads
Definition: FPGATrackSimHough1DShiftTool.h:117
fieldCorrection
double fieldCorrection(unsigned region, double qpt, double r)
Definition: FPGATrackSimFunctions.cxx:163
FPGATrackSimHough1DShiftTool::m_qptMin
Gaudi::Property< float > m_qptMin
Definition: FPGATrackSimHough1DShiftTool.h:124
sign
int sign(int a)
Definition: TRT_StrawNeighbourSvc.h:107
FPGATrackSimHough1DShiftTool::phitrkDiff
float phitrkDiff(float r1, float phi1, float r2, float phi2) const
Definition: FPGATrackSimHough1DShiftTool.cxx:533
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FPGATrackSimHough1DShiftTool::readShifts
void readShifts(std::string const &filepath)
Definition: FPGATrackSimHough1DShiftTool.cxx:190
FPGATrackSimHough1DShiftTool::m_qptMax
Gaudi::Property< float > m_qptMax
Definition: FPGATrackSimHough1DShiftTool.h:125
plotting.yearwise_luminosity_vs_mu.bins
bins
Definition: yearwise_luminosity_vs_mu.py:30
FPGATrackSimHough1DShiftTool::getRoads
virtual StatusCode getRoads(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits, std::vector< std::shared_ptr< const FPGATrackSimRoad >> &roads) override
Definition: FPGATrackSimHough1DShiftTool.cxx:316
FPGATrackSimHough1DShiftTool::m_bitShift_path
Gaudi::Property< std::string > m_bitShift_path
Definition: FPGATrackSimHough1DShiftTool.h:138
FPGATrackSimHough1DShiftTool::m_iterLayer
Gaudi::Property< unsigned > m_iterLayer
Definition: FPGATrackSimHough1DShiftTool.h:130
FPGATrackSimHough1DShiftTool::m_EvtSel
ServiceHandle< IFPGATrackSimEventSelectionSvc > m_EvtSel
Definition: FPGATrackSimHough1DShiftTool.h:105
FPGATrackSimHough1DShiftTool::m_drawHitMasks
Gaudi::Property< bool > m_drawHitMasks
Definition: FPGATrackSimHough1DShiftTool.h:119
FPGATrackSimHough1DShiftTool::m_phivals
std::vector< std::vector< float > > m_phivals
Definition: FPGATrackSimHough1DShiftTool.h:167
FPGATrackSimHough1DShiftTool::m_monitorFile
TFile m_monitorFile
Definition: FPGATrackSimHough1DShiftTool.h:183
FPGATrackSimHough1DShiftTool::calculateDropable
void calculateDropable()
Definition: FPGATrackSimHough1DShiftTool.cxx:700
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:83
FPGATrackSimHit.h
: FPGATrackSim-specific class to represent an hit in the detector.
FPGATrackSimHough1DShiftTool::m_r
std::vector< float > m_r
Definition: FPGATrackSimHough1DShiftTool.h:147
FPGATrackSimHough1DShiftTool::getBins
std::pair< int, int > getBins(const std::shared_ptr< const FPGATrackSimHit > &hit) const
Definition: FPGATrackSimHough1DShiftTool.cxx:499
python.PyAthena.v
v
Definition: PyAthena.py:154
FPGATrackSimHough1DShiftTool::getPtFromShiftDiff
float getPtFromShiftDiff(int shift) const
Definition: FPGATrackSimHough1DShiftTool.cxx:491
FPGATrackSimHit::getR
float getR() const
Definition: FPGATrackSimHit.h:140
h
IFPGATrackSimEventSelectionSvc.h
RunTileMonitoring.towers
towers
Definition: RunTileMonitoring.py:133
trigbs_pickEvents.cnt
cnt
Definition: trigbs_pickEvents.py:71
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
FPGATrackSimHough1DShiftTool::initialize
virtual StatusCode initialize() override
Definition: FPGATrackSimHough1DShiftTool.cxx:46
layer_bitmask_t
uint32_t layer_bitmask_t
Definition: FPGATrackSimTypes.h:22
FPGATrackSimHough1DShiftTool::m_iterStep
Gaudi::Property< unsigned > m_iterStep
Definition: FPGATrackSimHough1DShiftTool.h:129
IFPGATrackSimBankSvc.h
FPGATrackSimHough1DShiftTool::m_roads
std::vector< FPGATrackSimRoad > m_roads
Definition: FPGATrackSimHough1DShiftTool.h:176
compute_lumi.fin
fin
Definition: compute_lumi.py:19
FPGATrackSimHough1DShiftTool::m_nLayers
unsigned m_nLayers
Definition: FPGATrackSimHough1DShiftTool.h:152
FPGATrackSimHough1DShiftTool::printShifts
void printShifts() const
Definition: FPGATrackSimHough1DShiftTool.cxx:642
FPGATrackSimHough1DShiftTool::m_phiBins
Gaudi::Property< unsigned > m_phiBins
Definition: FPGATrackSimHough1DShiftTool.h:127
calibdata.copy
bool copy
Definition: calibdata.py:27
FPGATrackSimHough1DShiftTool::applyVariation
std::vector< int > applyVariation(const std::vector< int > &base, const std::vector< int > &var, int sign) const
Definition: FPGATrackSimHough1DShiftTool.cxx:677
FPGATrackSimHough1DShiftTool::m_d0shifts
std::vector< std::vector< int > > m_d0shifts
Definition: FPGATrackSimHough1DShiftTool.h:168
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
FPGATrackSimHough1DShiftTool::passThreshold
bool passThreshold(std::vector< boost::dynamic_bitset<>> &binHits, int bin) const
Definition: FPGATrackSimHough1DShiftTool.cxx:382
FPGATrackSimTypes.h
FPGATrackSimHough1DShiftTool::m_phiMin
Gaudi::Property< float > m_phiMin
Definition: FPGATrackSimHough1DShiftTool.h:122
FPGATrackSimHough1DShiftTool::m_enhanceHighPt
Gaudi::Property< float > m_enhanceHighPt
Definition: FPGATrackSimHough1DShiftTool.h:143
readCCLHist.float
float
Definition: readCCLHist.py:83
FPGATrackSimHough1DShiftTool::m_hitExtend
std::vector< float > m_hitExtend
Definition: FPGATrackSimHough1DShiftTool.h:146
FPGATrackSimSectorBank.h
This file declares a class that stores the module IDs of the sectors.
FPGATrackSimRoad
Definition: FPGATrackSimRoad.h:30
FPGATrackSimHough1DShiftTool::makeHitMasks
std::vector< boost::dynamic_bitset<> > makeHitMasks(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits)
Definition: FPGATrackSimHough1DShiftTool.cxx:418
FPGATrackSimHough1DShiftTool::m_name
std::string m_name
Definition: FPGATrackSimHough1DShiftTool.h:182
FPGATrackSimHough1DShiftTool::m_FPGATrackSimBankSvc
ServiceHandle< IFPGATrackSimBankSvc > m_FPGATrackSimBankSvc
Definition: FPGATrackSimHough1DShiftTool.h:106
sortByLayer
std::vector< std::vector< std::shared_ptr< const FPGATrackSimHit > > > sortByLayer(Container const &hits)
Definition: FPGATrackSimHit.h:270