ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimBinnedHits.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
9
17#include <GaudiKernel/StatusCode.h>
18#include <nlohmann/json.hpp>
19
21
23 // Dump the configuration to make sure it propagated through right
24 const std::vector<Gaudi::Details::PropertyBase*> props = this->getProperties();
25 for( Gaudi::Details::PropertyBase* prop : props ) {
26 if (prop->ownerTypeName()==this->type()) {
27 ATH_MSG_DEBUG("Property:\t" << prop->name() << "\t : \t" << prop->toString());
28 }
29 }
30
31
32 ATH_MSG_DEBUG("Retrieving BinTool");
33 ATH_CHECK(m_bintool.retrieve());
34 ATH_MSG_DEBUG("Retrieving EvtSel");
35 ATH_CHECK(m_EvtSel.retrieve());
36 // Compute which bins correspond to track parameters that are in the region
37 // i.e. the pT, eta, phi, z0 and d0 bounds
38 // list of valid bins is extracted from the layer map if its loaded
39 m_bintool->initValidBins();
40
41 if ((m_lyrmapFile.size()!=0)&&((m_lyrradiiFile.size()!=0))) {
42 ATH_MSG_FATAL("Both lyrmap and layradii, these are mutually exclusive");
43 }
44
45 if (m_lyrmapFile.size()!=0) {
47 } else if (m_lyrradiiFile.size()!=0) {
49 } else {
50 ATH_MSG_INFO("Analytically computing valid bins");
51 m_bintool->computeValidBins(m_EvtSel.get());
52 }
53
54 m_bintool->printValidBin(); // also dumps firmware constants
56
57 return StatusCode::SUCCESS;
58}
59
61 m_binnedHitsStep.resize(m_bintool->steps().size());
62 int i = 0;
63 for (auto &step : m_bintool->steps()) {
64 m_binnedHitsStep[i].setsize(step->nBins(), BinEntry());
65 ATH_MSG_INFO("Step" << step->stepName() << " q Size (full): "
66 << m_binnedHitsStep[i].size());
67 ++i;
68 }
69 resetBins(); // just to be sure
70}
71
73 for (auto &stepdata : m_binnedHitsStep) {
75 bin.data().reset();
76 }
77 }
78}
79
80// Put hits in all track parameter bins they could be a part of (binning is defined
81// by m_binning object)
83 const std::vector<std::shared_ptr<const FPGATrackSimHit>> &hits) {
84 ATH_MSG_DEBUG("In fill");
85
86 int stepnum = 0;
87 for (const auto &step : m_bintool->steps()) {
88
89 ATH_MSG_DEBUG("fill binning: step num " << stepnum << " " << step->stepName());
90 for (auto &bin : step->validBinsFull()) {
91
92 // skip bin if it is invalid
93 if (!bin.data())
94 continue;
95
96 ATH_MSG_VERBOSE("valid bin");
97 if (stepnum == 0) {
98 // first step, hits from input stream
99 for (const std::shared_ptr<const FPGATrackSimHit> &hit : hits) {
100 StoredHit storedhit(hit);
101 if (m_bintool->binDesc()->hitInBin(*step.get(), bin.idx(),
102 storedhit)) {
103 m_binnedHitsStep[stepnum][bin.idx()].addHit(storedhit);
104 }
105 }
106
107 } else {
108 // subsequent steps, use hits from previous step
109 if (m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size()!=0)
110 ATH_MSG_VERBOSE("Looping over previous hits of size = " << m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size());
111
112 for (const auto &hit :
113 m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits) {
114 StoredHit storedhit(hit);
115 if (m_bintool->binDesc()->hitInBin(*step.get(), bin.idx(),storedhit)) {
116 bool writeHit = true;
117
118 // if it is the laststep, set the layer and only write hit if there is a valid layer
119 if (step.get() == m_bintool->lastStep()) {
120 if (m_mod_to_lyr_map.size() != 0) { // there is a layermap
121 if (m_mod_to_lyr_map[bin.idx()].contains(hit.hitptr->getIdentifierHash())) {
122 storedhit.layer = m_mod_to_lyr_map[bin.idx()][hit.hitptr->getIdentifierHash()];
123 } else {
124 ATH_MSG_VERBOSE("Hit not in layermap" << hit);
125 writeHit=false;
126 }
127 } else if (m_lyr_radii.size()!=0) { // there is a layer radius set
128 auto lyr_radii = m_lyr_radii[bin.idx()];
129 for (unsigned lyr = 0; lyr < lyr_radii.size(); lyr++) {
130 writeHit=false;
131 if ((hit.rzrad() > lyr_radii[lyr].first)&&(hit.rzrad() < lyr_radii[lyr].second)) {
132 storedhit.layer = lyr;
133 writeHit=true;
134 break;
135 }
136 }
137 if(msgLvl(MSG::VERBOSE) && !writeHit) {
138 std::vector<double> rads;
139 for (unsigned lyr = 0; lyr < lyr_radii.size(); lyr++) {
140 rads.push_back(lyr_radii[lyr].first);
141 rads.push_back(lyr_radii[lyr].second);
142 }
143 ATH_MSG_VERBOSE("Hit not in layer radii " << bin.idx() << " " << hit.rzrad() << " " << rads);
144 }
145 } else { // no layer map
146 // TODO is it right to ignore hits that were unmapped?
147 if (hit.hitptr->isMapped()) {
148 storedhit.layer = hit.hitptr->getLayer();
149 } else {
150 ATH_MSG_VERBOSE("Hit not mapped" << hit);
151 writeHit=false;
152 }
153 }
154 }
155
156 // add hit to bin
157 if (writeHit) m_binnedHitsStep[stepnum][bin.idx()].addHit(storedhit);
158 }
159 }
160
161 if (m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size()!=0)
162 ATH_MSG_VERBOSE("Bin Hit Count: step " << step.name()
163 << " binidx = " << bin.idx()
164 << " input hits = " << m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size()
165 << " layers=" << m_binnedHitsStep[stepnum][bin.idx()].lyrCnt()
166 << " hits=" << m_binnedHitsStep[stepnum][bin.idx()].hitCnt );
167 }
168
169 } // end loop over bins
170
171 stepnum++;
172 } // end loop over steps
173
174 return StatusCode::SUCCESS;
175}
176
178// Internal Storage Class
179
181{
182 hitCnt = 0;
183 lyrhit = 0;
184 hits.clear();
185}
186
188{
189 hitCnt++;
190 hits.push_back(hit);
192 {
193 lyrhit |= (0x1 << hit.layer);
194 }
195}
196
198 return std::count_if(hits.begin(),hits.end(),[lyr](auto& hit){return hit.layer==lyr;});
199}
200
201
202//-------------------------------------------------------------------------------
203//
204// Layer map implementation
205//
206//-------------------------------------------------------------------------------
207
208void FPGATrackSimBinnedHits::readLayerMap(const std::string &filename) {
209
210 ATH_MSG_INFO("Reading lyrmap json: " << filename);
211
212 std::ifstream f(filename);
213 nlohmann::json data = nlohmann::json::parse(f);
214
215 m_lyr_to_mod_map.setsize(m_bintool->lastStep()->nBins(),
216 std::vector <std::set<unsigned> >());
217 m_mod_to_lyr_map.setsize(m_bintool->lastStep()->nBins(),
218 std::map <unsigned,unsigned>());
219
220 // This sets the number of layer, to the number of layers in the layerMap
221 m_nLayers = 0;
222 for (const auto &binelem : data) {
223 std::vector<unsigned> bin;
224 binelem.at("bin").get_to(bin);
225 auto& lyrmap = binelem["lyrmap"];
226 ATH_MSG_VERBOSE("bin = " << bin);
227 if (!m_lyr_to_mod_map.isValid(bin)) {
228 ATH_MSG_FATAL("lyrmap includeds invalid bin:" << bin);
229 }
230 ATH_MSG_VERBOSE("lyrmap = " << lyrmap);
231 for (auto &lyrelem : lyrmap) {
232 unsigned lyr;
233 lyrelem.at("lyr").get_to(lyr);
234 m_lyr_to_mod_map[bin].push_back(std::set<unsigned>());
235 lyrelem.at("mods").get_to(m_lyr_to_mod_map[bin][lyr]);
236 ATH_MSG_VERBOSE("lyr = " << lyr);
237 ATH_MSG_VERBOSE("mods = " << m_lyr_to_mod_map[bin][lyr]);
238 for (auto &mod : m_lyr_to_mod_map[bin][lyr]) {
239 m_mod_to_lyr_map[bin][mod]=lyr;
240 }
241 // set valid bins, this expects binning based on the last step
242 m_bintool->setValidBin(bin);
243 }
244 for (auto &lyrmods : m_lyr_to_mod_map[bin]) {
245 ATH_MSG_VERBOSE(" mods: " << lyrmods);
246 }
247 if (m_nLayers != 0 && m_nLayers != m_lyr_to_mod_map[bin].size()) {
248 ATH_MSG_WARNING("Layer map bins have inconsistent numbers of layers: " << m_nLayers << ", " << m_lyr_to_mod_map[bin].size());
249 }
250 m_nLayers = std::max(m_nLayers, static_cast<unsigned>(m_lyr_to_mod_map[bin].size()));
251 }
252 ATH_MSG_INFO("JSON layer map speficied " << m_nLayers << " layers" );
253}
254
255
256void FPGATrackSimBinnedHits::readLayerRadii(const std::string &filename) {
257
258 ATH_MSG_INFO("Reading lyrmap json: " << filename);
259
260 std::ifstream f(filename);
261 nlohmann::json data = nlohmann::json::parse(f);
262
263 m_lyr_radii.setsize(m_bintool->lastStep()->nBins(),
264 std::vector < std::pair<double,double> > ());
265
266 // This sets the number of layer, to the number of layers in the layerMap
267 m_nLayers = 0;
268 for (const auto &binelem : data) {
269 std::vector<unsigned> bin;
270 binelem.at("bin").get_to(bin);
271 ATH_MSG_VERBOSE("bin = " << bin);
272 if (!m_lyr_radii.isValid(bin)) {
273 ATH_MSG_FATAL("lyrmap includeds invalid bin:" << bin);
274 }
275
276 auto& lyrradii = binelem["lyrradii"];
277 ATH_MSG_VERBOSE("lyrradii = " << lyrradii);
278
279 for (auto &lyrelem : lyrradii) {
280 unsigned lyr;
281 lyrelem.at("lyr").get_to(lyr);
282 std::vector<double> temp(2);
283 lyrelem.at("radii").get_to(temp);
284 m_lyr_radii[bin].push_back(std::pair<double,double>(temp[0],temp[1]));
285 ATH_MSG_VERBOSE("lyr = " << lyr);
286 ATH_MSG_VERBOSE("radii = " << m_lyr_radii[bin][lyr]);
287 // set valid bins, this expects binning based on the last step
288 m_bintool->setValidBin(bin);
289 }
290 if (m_nLayers != 0 && m_nLayers != m_lyr_radii[bin].size()) {
291 ATH_MSG_WARNING("Layer map bins have inconsistent numbers of layers: " << m_nLayers << ", " << m_lyr_to_mod_map[bin].size());
292 }
293 m_nLayers = std::max(m_nLayers, static_cast<unsigned>(m_lyr_radii[bin].size()));
294 }
295 ATH_MSG_INFO("JSON layer map speficied " << m_nLayers << " layers" );
296}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Binning Classes for BinStep.
Binning Utilities for GenScanTool.
Binning Classes for GenScanTool.
: FPGATrackSim-specific class to represent an hit in the detector.
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
Defines Parameters used for binning.
bool msgLvl(const MSG::Level lvl) const
FPGATrackSimBinArray< std::vector< std::pair< double, double > > > m_lyr_radii
void readLayerMap(const std::string &filename)
ServiceHandle< IFPGATrackSimEventSelectionSvc > m_EvtSel
Gaudi::Property< std::string > m_lyrradiiFile
virtual StatusCode initialize() override
StatusCode fill(const std::vector< std::shared_ptr< const FPGATrackSimHit > > &hits)
std::vector< FPGATrackSimBinArray< BinEntry > > m_binnedHitsStep
Gaudi::Property< std::string > m_lyrmapFile
FPGATrackSimBinArray< std::map< unsigned, unsigned > > m_mod_to_lyr_map
FPGATrackSimBinArray< std::vector< std::set< unsigned > > > m_lyr_to_mod_map
void readLayerRadii(const std::string &filename)
ToolHandle< FPGATrackSimBinTool > m_bintool
unsigned int hitsInLyr(unsigned lyr) const
void addHit(const FPGATrackSimBinUtil::StoredHit &hit)
std::vector< FPGATrackSimBinUtil::StoredHit > hits
static const unsigned invalidLayer