ATLAS Offline Software
FPGATrackSimNNTrackTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
19 
22  const std::string &name, const IInterface *ifc)
23  : AthAlgTool(algname, name, ifc) {}
24 
25 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
27  ATH_CHECK(m_FPGATrackSimMapping.retrieve());
28  ATH_CHECK(m_tHistSvc.retrieve());
29 
30  return StatusCode::SUCCESS;
31 }
32 
33 StatusCode FPGATrackSimNNTrackTool::getTracks(std::vector<FPGATrackSimRoad *> &roads,
34  std::vector<FPGATrackSimTrack> &tracks,
35  const FPGATrackSimNNMap *nnMap) {
36 
37  int n_track = 0;
38 
39  // Loop over roads
40  for (auto iroad : roads) {
41 
42  double y = iroad->getY();
43 
44  // Error checking
45  int sector = iroad->getSector();
46  if (sector < 0) {
47  ATH_MSG_DEBUG("Bad sector " << sector);
48  return StatusCode::SUCCESS;
49  }
50 
51  // Get info on layers with missing hits
52  int nMissing = 0;
53  layer_bitmask_t missing_mask = 0;
54 
55  // Just used to get number of layers considered
56  const FPGATrackSimPlaneMap *planeMap = m_FPGATrackSimMapping->PlaneMap_1st();
57 
58  // Create a template track with common parameters filled already for
59  // initializing below
60  FPGATrackSimTrack temp;
62  temp.setNLayers(planeMap->getNLogiLayers());
63  temp.setBankID(-1);
64  temp.setPatternID(iroad->getPID());
65  temp.setFirstSectorID(iroad->getSector());
66  temp.setHitMap(missing_mask);
67  temp.setNMissing(nMissing);
68  temp.setQOverPt(y);
69 
71  // Get a list of indices for all possible combinations given a certain
72  // number of layers
73  std::vector<std::vector<int>> combs =
74  getComboIndices(iroad->getNHits_layer());
75 
76  // Loop over possible combinations for this road
77  for (size_t icomb = 0; icomb < combs.size(); icomb++) {
78 
79  // list of indices for this particular combination
80  std::vector<int> const &hit_indices = combs[icomb];
81  std::vector<const FPGATrackSimHit *> hit_list;
82 
83  // Loop over all layers
84  for (unsigned layer = 0; layer < planeMap->getNLogiLayers(); layer++) {
85 
86  // Check to see if this is a valid hit
87  if (hit_indices[layer] >= 0) {
88 
89  const FPGATrackSimHit *hit = iroad->getHits(layer)[hit_indices[layer]];
90  // Add this hit to the road
91  hit_list.push_back(hit);
92  }
93  }
94 
95  // Sort the list by radial distance
96  std::sort(hit_list.begin(), hit_list.end(),
97  [](const FPGATrackSimHit *hit1, const FPGATrackSimHit *hit2) {
98  double rho1 = std::hypot(hit1->getX(), hit1->getY());
99  double rho2 = std::hypot(hit2->getX(), hit2->getY());
100  return rho1 < rho2;
101  });
102 
103  // ECC - Input maps for lwtnn
104  std::map<std::string, std::map<std::string, double>> valMap;
105  std::map<std::string, std::map<std::string, std::vector<double>>>
106  vectorMap;
107 
108  // Initalize to default
109  for (int i = 1; i <= 8; i++) {
110  TString indexStr = Form("%d", i);
111  valMap["dNN"][("hitX" + indexStr).Data()] = 0;
112  valMap["dNN"][("hitY" + indexStr).Data()] = 0;
113  valMap["dNN"][("hitZ" + indexStr).Data()] = 0;
114  }
115 
116  int index = 1;
117  bool flipZ = false;
118  double rotateAngle = 0;
119 
120  // Loop over all hits
121  for (const auto &hit : hit_list) {
122 
123  // Need to rotate hits
124  double x0 = hit->getX();
125  double y0 = hit->getY();
126  double z0 = hit->getZ();
127  if (index == 1) {
128  if (z0 < 0)
129  flipZ = true;
130  rotateAngle = std::atan(x0 / y0);
131  if (y0 < 0)
132  rotateAngle += M_PI;
133  }
134 
135  double xf = x0 * std::cos(rotateAngle) - y0 * std::sin(rotateAngle);
136  double yf = x0 * std::sin(rotateAngle) + y0 * std::cos(rotateAngle);
137  double zf = z0;
138  if (flipZ)
139  zf = z0 * -1;
140 
141  // Fill map for lwtnn
142  TString indexStr = Form("%d", index);
143  valMap["dNN"][("hitX" + indexStr).Data()] = xf / getXScale();
144  valMap["dNN"][("hitY" + indexStr).Data()] = yf / getYScale();
145  valMap["dNN"][("hitZ" + indexStr).Data()] = zf / getZScale();
146  index++;
147  }
148 
149  // Use NN filter to see if these set of hits comprise a good track
150  std::shared_ptr<lwt::LightweightGraph> lwnn_map = nnMap->getNNMap();
151  auto scoreMap = lwnn_map->compute(valMap, vectorMap);
152  float nn_val = scoreMap.at("NNScore");
153 
154  // Save all hits on this road if it passes our "good" criteria
155  if (nn_val > m_NNCut) {
156 
157  // This is a track. Fill class and add to track list
158  n_track++;
159  FPGATrackSimTrack track_cand;
160  track_cand.setTrackID(n_track);
161  track_cand.setNLayers(planeMap->getNLogiLayers());
162  for (const auto &ihit : hit_list) {
163  unsigned int layer = ihit->getLayer();
164  track_cand.setFPGATrackSimHit(layer, *ihit);
165  }
166  // Nominal chi2ndof cut is 40 and we want to use NN>0.0075 (or
167  // NN<(1-0.0075) Nominal chi2ndof cut is 40 and we want to use NN>0.001
168  // (or NN<(1-0.1)
169  double scale = m_chi2_scalefactor *
170  (track_cand.getNCoords() - track_cand.getNMissing() - 5);
171  double chi2 = scale * (1 - nn_val);
172  track_cand.setOrigChi2(chi2);
173  track_cand.setChi2(chi2);
174  tracks.push_back(track_cand);
175  }
176  } // loop over combinations
177 
178  } // loop over roads
179 
180  // Add truth info
181  for (FPGATrackSimTrack &t : tracks)
182  compute_truth(t); // match the track to a geant particle using the
183  // channel-level geant info in the hit data.
184 
185  return StatusCode::SUCCESS;
186 }
187 
188 // Borrowed same code from TrackFitter - probably a nicer way to inherit instead
190  std::vector<FPGATrackSimMultiTruth> mtv;
191  const FPGATrackSimPlaneMap *planeMap = m_FPGATrackSimMapping->PlaneMap_1st();
192 
193  for (unsigned layer = 0; layer < planeMap->getNLogiLayers(); layer++) {
194  if (t.getHitMap() & (1 << planeMap->getCoordOffset(layer)))
195  continue; // no hit in this plane
196  // Sanity check that we have enough hits.
197  if (layer < t.getFPGATrackSimHits().size())
198  mtv.push_back(t.getFPGATrackSimHits().at(layer).getTruth());
199 
200  // adjust weight for hits without (and also with) a truth match, so that
201  // each is counted with the same weight.
202  mtv.back().assign_equal_normalization();
203  }
204 
207  // frac is then the fraction of the total number of hits on the track
208  // attributed to the barcode.
209 
212  const bool ok = mt.best(tbarcode, tfrac);
213  if (ok) {
214  t.setEventIndex(tbarcode.first);
215  t.setBarcode(tbarcode.second);
216  t.setBarcodeFrac(tfrac);
217  }
218 }
FPGATrackSimTrack::setHitMap
void setHitMap(unsigned int v)
Definition: FPGATrackSimTrack.h:92
FPGATrackSimNNTrackTool::getTracks
StatusCode getTracks(std::vector< FPGATrackSimRoad * > &roads, std::vector< FPGATrackSimTrack > &tracks, const FPGATrackSimNNMap *nnMap)
Definition: FPGATrackSimNNTrackTool.cxx:33
getMenu.algname
algname
Definition: getMenu.py:53
FPGATrackSimPlaneMap::getNLogiLayers
uint32_t getNLogiLayers() const
Definition: FPGATrackSimPlaneMap.h:78
FPGATrackSimTrack::setBankID
void setBankID(int v)
Definition: FPGATrackSimTrack.h:75
FPGATrackSimTrack::getNMissing
int getNMissing() const
Definition: FPGATrackSimTrack.h:44
FPGATrackSimNNMap.h
Map for NN tracking.
FPGATrackSimTrack
Definition: FPGATrackSimTrack.h:16
FPGATrackSimTrack::setPatternID
void setPatternID(int v)
Definition: FPGATrackSimTrack.h:76
index
Definition: index.py:1
FPGATrackSimMultiTruth::Weight
float Weight
Definition: FPGATrackSimMultiTruth.h:50
FPGATrackSimNNTrackTool.h
Utilize NN score to build track candidates.
Data
@ Data
Definition: BaseObject.h:11
IFPGATrackSimMappingSvc.h
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
python.HLT.MinBias.MinBiasMenuSequences.zf
zf
Definition: MinBiasMenuSequences.py:187
FPGATrackSimTrack::setNMissing
void setNMissing(int v)
Definition: FPGATrackSimTrack.h:90
M_PI
#define M_PI
Definition: ActiveFraction.h:11
FPGATrackSimTrack::setFirstSectorID
void setFirstSectorID(int v)
Definition: FPGATrackSimTrack.h:77
FPGATrackSimTrack::getNCoords
int getNCoords() const
FPGATrackSimNNTrackTool::getXScale
static float getXScale()
Definition: FPGATrackSimNNTrackTool.h:49
FPGATrackSimMultiTruth.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
FPGATrackSimTrack::setFPGATrackSimHit
void setFPGATrackSimHit(unsigned i, const FPGATrackSimHit &hit)
FPGATrackSimTrack::setOrigChi2
void setOrigChi2(float v)
Definition: FPGATrackSimTrack.h:89
FPGATrackSimMultiTruth::best
bool best(FPGATrackSimMultiTruth::Barcode &code, FPGATrackSimMultiTruth::Weight &weight) const
Definition: FPGATrackSimMultiTruth.h:86
drawFromPickle.atan
atan
Definition: drawFromPickle.py:36
FPGATrackSimNNTrackTool::m_tHistSvc
ServiceHandle< ITHistSvc > m_tHistSvc
Definition: FPGATrackSimNNTrackTool.h:62
FPGATrackSimHit
Definition: FPGATrackSimHit.h:38
FPGATrackSimPlaneMap::getCoordOffset
uint32_t getCoordOffset(size_t logiLayer) const
Definition: FPGATrackSimPlaneMap.h:95
FPGATrackSimNNTrackTool::m_chi2_scalefactor
Gaudi::Property< double > m_chi2_scalefactor
Definition: FPGATrackSimNNTrackTool.h:56
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
TrackStage::FIRST
@ FIRST
FPGATrackSimMultiTruth::Barcode
std::pair< unsigned long, unsigned long > Barcode
Definition: FPGATrackSimMultiTruth.h:49
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FPGATrackSimNNMap
Definition: FPGATrackSimNNMap.h:27
FPGATrackSimNNMap::getNNMap
std::shared_ptr< lwt::LightweightGraph > getNNMap() const
Definition: FPGATrackSimNNMap.cxx:45
TRT::Track::z0
@ z0
Definition: InnerDetector/InDetCalibEvent/TRT_CalibData/TRT_CalibData/TrackInfo.h:63
FPGATrackSimNNTrackTool::m_NNCut
Gaudi::Property< double > m_NNCut
Definition: FPGATrackSimNNTrackTool.h:55
getComboIndices
std::vector< std::vector< int > > getComboIndices(std::vector< size_t > const &sizes)
Given a vector of sizes (of arrays), generates a vector of all combinations of indices to index one e...
Definition: FPGATrackSimFunctions.cxx:21
FPGATrackSimNNTrackTool::m_FPGATrackSimMapping
ServiceHandle< IFPGATrackSimMappingSvc > m_FPGATrackSimMapping
Definition: FPGATrackSimNNTrackTool.h:61
FPGATrackSimPlaneMap
Definition: FPGATrackSimPlaneMap.h:62
FPGATrackSimNNTrackTool::getZScale
static float getZScale()
Definition: FPGATrackSimNNTrackTool.h:51
FPGATrackSimTrack::setTrackStage
void setTrackStage(TrackStage v)
Definition: FPGATrackSimTrack.h:73
FPGATrackSimNNTrackTool::getYScale
static float getYScale()
Definition: FPGATrackSimNNTrackTool.h:50
FPGATrackSimNNTrackTool::FPGATrackSimNNTrackTool
FPGATrackSimNNTrackTool(const std::string &, const std::string &, const IInterface *)
Definition: FPGATrackSimNNTrackTool.cxx:21
FPGATrackSimMultiTruth
Definition: FPGATrackSimMultiTruth.h:46
FPGATrackSimMultiTruth::AddAccumulator
Definition: FPGATrackSimMultiTruth.h:57
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
FPGATrackSimFunctions.h
FPGATrackSimTrack::setTrackID
void setTrackID(int v)
Definition: FPGATrackSimTrack.h:79
FPGATrackSimTrack::setQOverPt
void setQOverPt(float v)
Definition: FPGATrackSimTrack.h:83
FPGATrackSimNNTrackTool::compute_truth
void compute_truth(FPGATrackSimTrack &newtrk) const
Definition: FPGATrackSimNNTrackTool.cxx:189
FPGATrackSimNNTrackTool::initialize
virtual StatusCode initialize() override
Definition: FPGATrackSimNNTrackTool.cxx:26
y
#define y
FPGATrackSimTrack::setChi2
void setChi2(float v)
Definition: FPGATrackSimTrack.h:88
layer_bitmask_t
uint32_t layer_bitmask_t
Definition: FPGATrackSimTypes.h:22
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
AthAlgTool
Definition: AthAlgTool.h:26
FPGATrackSimTrack::setNLayers
void setNLayers(int)
set the number of layers in the track.
Definition: FPGATrackSimTrack.cxx:151