ATLAS Offline Software
FPGATrackSimBinnedHits.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
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  if (m_lyrmapFile.size()==0)
41  {
42  m_bintool->computeValidBins(m_EvtSel.get());
43  } else {
45  }
46  m_bintool->printValidBin(); // also dumps firmware constants
48 
49  return StatusCode::SUCCESS;
50 }
51 
53  m_binnedHitsStep.resize(m_bintool->steps().size());
54  int i = 0;
55  for (auto &step : m_bintool->steps()) {
56  m_binnedHitsStep[i].setsize(step->nBins(), BinEntry());
57  ATH_MSG_INFO("Step" << step->stepName() << " Image Size (full): "
58  << m_binnedHitsStep[i].size());
59  ++i;
60  }
61  resetBins(); // just to be sure
62 }
63 
65  for (auto &stepdata : m_binnedHitsStep) {
67  bin.data().reset();
68  }
69  }
70 }
71 
72 // Put hits in all track parameter bins they could be a part of (binning is defined
73 // by m_binning object)
75  const std::vector<std::shared_ptr<const FPGATrackSimHit>> &hits) {
76  ATH_MSG_DEBUG("In fill");
77 
78  int stepnum = 0;
79  for (const auto &step : m_bintool->steps()) {
80 
81  ATH_MSG_DEBUG("fill binning: step num " << stepnum << " " << step->stepName());
82  ATH_MSG_DEBUG("Number of valid bins (full) = " << step->validBinsFull().size());
83  for (auto &bin : step->validBinsFull()) {
84 
85  // skip bin if it is invalid
86  if (!bin.data())
87  continue;
88 
89  ATH_MSG_VERBOSE("valid bin");
90  if (stepnum == 0) {
91  // first step, hits from input stream
92  for (const std::shared_ptr<const FPGATrackSimHit> &hit : hits) {
93  StoredHit storedhit(hit);
94  if (m_bintool->binDesc()->hitInBin(*step.get(), bin.idx(),
95  storedhit)) {
96  m_binnedHitsStep[stepnum][bin.idx()].addHit(storedhit);
97  }
98  }
99 
100  } else {
101  // subsequent steps, use hits from previous step
102  if (m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size()!=0)
103  ATH_MSG_VERBOSE("Looping over previous hits of size = " << m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size());
104 
105  for (const auto &hit :
106  m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits) {
107  StoredHit storedhit(hit);
108  if (m_bintool->binDesc()->hitInBin(*step.get(), bin.idx(),storedhit)) {
109  bool writeHit = true;
110 
111  // if it is the laststep, set the layer and only write hit if there is a valid layer
112  if (step.get() == m_bintool->lastStep()) {
113  if (m_mod_to_lyr_map.size() != 0) { // there is a layermap
114  if (m_mod_to_lyr_map[bin.idx()].contains(hit.hitptr->getIdentifierHash())) {
115  storedhit.layer = m_mod_to_lyr_map[bin.idx()][hit.hitptr->getIdentifierHash()];
116  } else {
117  ATH_MSG_VERBOSE("Hit not in layermap" << hit);
118  writeHit=false;
119  }
120  } else { // no layer map
121  // TODO is it right to ignore hits that were unmapped?
122  if (hit.hitptr->isMapped()) {
123  storedhit.layer = hit.hitptr->getLayer();
124  } else {
125  ATH_MSG_VERBOSE("Hit not mapped" << hit);
126  writeHit=false;
127  }
128  }
129  }
130 
131  // add hit to bin
132  if (writeHit) m_binnedHitsStep[stepnum][bin.idx()].addHit(storedhit);
133  }
134  }
135 
136  if (m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size()!=0)
137  ATH_MSG_DEBUG("Bin Hit Count: step " << step.name()
138  << " binidx = " << bin.idx()
139  << " input hits = " << m_binnedHitsStep[stepnum - 1][step->convertToPrev(bin.idx())].hits.size()
140  << " layers=" << m_binnedHitsStep[stepnum][bin.idx()].lyrCnt()
141  << " hits=" << m_binnedHitsStep[stepnum][bin.idx()].hitCnt );
142  }
143 
144  } // end loop over bins
145 
146  stepnum++;
147  } // end loop over steps
148 
149  return StatusCode::SUCCESS;
150 }
151 
153 // Internal Storage Class
154 
156 {
157  hitCnt = 0;
158  lyrhit = 0;
159  hits.clear();
160 }
161 
163 {
164  hitCnt++;
165  hits.push_back(hit);
166  if (hit.layer != StoredHit::invalidLayer)
167  {
168  lyrhit |= (0x1 << hit.layer);
169  }
170 }
171 
172 unsigned FPGATrackSimBinnedHits::BinEntry::hitsInLyr(unsigned lyr) const {
173  return std::count_if(hits.begin(),hits.end(),[lyr](auto& hit){return hit.layer==lyr;});
174 }
175 
176 
177 //-------------------------------------------------------------------------------
178 //
179 // Layer map implementation
180 //
181 //-------------------------------------------------------------------------------
182 
184 
185  ATH_MSG_INFO("Reading lyrmap json: " << filename);
186 
187  std::ifstream f(filename);
189 
190  m_lyr_to_mod_map.setsize(m_bintool->lastStep()->nBins(),
191  std::vector <std::set<unsigned> >());
192  m_mod_to_lyr_map.setsize(m_bintool->lastStep()->nBins(),
193  std::map <unsigned,unsigned>());
194 
195  // This sets the number of layer, to the number of layers in the layerMap
196  m_nLayers = 0;
197  for (const auto &binelem : data) {
198  std::vector<unsigned> bin;
199  binelem.at("bin").get_to(bin);
200  auto& lyrmap = binelem["lyrmap"];
201  ATH_MSG_VERBOSE("bin = " << bin);
202  ATH_MSG_VERBOSE("lyrmap = " << lyrmap);
203  for (auto &lyrelem : lyrmap) {
204  unsigned lyr;
205  lyrelem.at("lyr").get_to(lyr);
206  m_lyr_to_mod_map[bin].push_back(std::set<unsigned>());
207  lyrelem.at("mods").get_to(m_lyr_to_mod_map[bin][lyr]);
208  ATH_MSG_VERBOSE("lyr = " << lyr);
209  ATH_MSG_VERBOSE("mods = " << m_lyr_to_mod_map[bin][lyr]);
210  for (auto &mod : m_lyr_to_mod_map[bin][lyr]) {
211  m_mod_to_lyr_map[bin][mod]=lyr;
212  }
213  // set valid bins, this expects binning based on the last step
214  m_bintool->setValidBin(bin);
215  }
216  for (auto &lyrmods : m_lyr_to_mod_map[bin]) {
217  ATH_MSG_VERBOSE(" mods: " << lyrmods);
218  }
219  if (m_nLayers != 0 && m_nLayers != m_lyr_to_mod_map[bin].size()) {
220  ATH_MSG_WARNING("Layer map bins have inconsistent numbers of layers: " << m_nLayers << ", " << m_lyr_to_mod_map[bin].size());
221  }
222  m_nLayers = std::max(m_nLayers, static_cast<unsigned>(m_lyr_to_mod_map[bin].size()));
223  }
224  ATH_MSG_INFO("JSON layer map speficied " << m_nLayers << " layers" );
225 }
FPGATrackSimBinnedHits::m_lyr_to_mod_map
FPGATrackSimBinArray< std::vector< std::set< unsigned > > > m_lyr_to_mod_map
Definition: FPGATrackSimBinnedHits.h:92
FPGATrackSimBinnedHits::m_binnedHitsStep
std::vector< FPGATrackSimBinArray< BinEntry > > m_binnedHitsStep
Definition: FPGATrackSimBinnedHits.h:104
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FPGATrackSimBinnedHits::m_nLayers
unsigned m_nLayers
Definition: FPGATrackSimBinnedHits.h:112
AthMsgStreamMacros.h
json
nlohmann::json json
Definition: HistogramDef.cxx:9
FPGATrackSimBinnedHits::BinEntry::hitCnt
unsigned int hitCnt
Definition: FPGATrackSimBinnedHits.h:76
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
FPGATrackSimBinUtil::StoredHit::layer
unsigned layer
Definition: FPGATrackSimBinUtil.h:106
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1113
FPGATrackSimBinnedHits::m_EvtSel
ServiceHandle< IFPGATrackSimEventSelectionSvc > m_EvtSel
Definition: FPGATrackSimBinnedHits.h:101
bin
Definition: BinsDiffFromStripMedian.h:43
FPGATrackSimBinnedHits::fill
StatusCode fill(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits)
Definition: FPGATrackSimBinnedHits.cxx:74
FPGATrackSimBinnedHits::BinEntry::lyrhit
layer_bitmask_t lyrhit
Definition: FPGATrackSimBinnedHits.h:77
FPGATrackSimBinnedHits::BinEntry::hitsInLyr
unsigned int hitsInLyr(unsigned lyr) const
Definition: FPGATrackSimBinnedHits.cxx:172
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
FPGATrackSimBinnedHits.h
Binning Classes for GenScanTool.
FPGATrackSimBinStep.h
Binning Classes for BinStep.
FPGATrackSimBinnedHits::initialize
virtual StatusCode initialize() override
Definition: FPGATrackSimBinnedHits.cxx:22
FPGATrackSimBinnedHits::BinEntry::reset
void reset()
Definition: FPGATrackSimBinnedHits.cxx:155
FPGATrackSimBinArray::size
unsigned int size() const
Definition: FPGATrackSimBinArray.h:69
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
FPGATrackSimBinArray::setsize
void setsize(const std::vector< unsigned int > &dims, const T &initval)
Definition: FPGATrackSimBinArray.h:51
maskDeadModules.mod
mod
Definition: maskDeadModules.py:36
FPGATrackSimBinnedHits::m_bintool
ToolHandle< FPGATrackSimBinTool > m_bintool
Definition: FPGATrackSimBinnedHits.h:107
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
FPGATrackSimBinUtil.h
Binning Utilities for GenScanTool.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
FPGATrackSimBinnedHits::resetBins
void resetBins()
Definition: FPGATrackSimBinnedHits.cxx:64
FPGATrackSimBinnedHits::m_mod_to_lyr_map
FPGATrackSimBinArray< std::map< unsigned, unsigned > > m_mod_to_lyr_map
Definition: FPGATrackSimBinnedHits.h:93
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:140
FPGATrackSimBinnedHits::BinEntry::addHit
void addHit(const FPGATrackSimBinUtil::StoredHit &hit)
Definition: FPGATrackSimBinnedHits.cxx:162
FPGATrackSimBinnedHits::BinEntry
Definition: FPGATrackSimBinnedHits.h:70
FPGATrackSimBinUtil::StoredHit
Definition: FPGATrackSimBinUtil.h:101
IFPGATrackSimBinDesc.h
Defines Parameters used for binning.
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:82
FPGATrackSimHit.h
: FPGATrackSim-specific class to represent an hit in the detector.
FPGATrackSimBinArray::Iterator
Definition: FPGATrackSimBinArray.h:191
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:23
LArCellBinning.step
step
Definition: LArCellBinning.py:158
FPGATrackSimBinnedHits::readLayerMap
void readLayerMap(const std::string &filename)
Definition: FPGATrackSimBinnedHits.cxx:183
FPGATrackSimBinnedHits::m_lyrmapFile
Gaudi::Property< std::string > m_lyrmapFile
Definition: FPGATrackSimBinnedHits.h:100
FPGATrackSimBinnedHits::initBinnedDataArrays
void initBinnedDataArrays()
Definition: FPGATrackSimBinnedHits.cxx:52
FPGATrackSimBinnedHits::BinEntry::hits
std::vector< FPGATrackSimBinUtil::StoredHit > hits
Definition: FPGATrackSimBinnedHits.h:78