ATLAS Offline Software
L1TopoLUT.cxx
Go to the documentation of this file.
2 
3 #define BOOST_BIND_GLOBAL_PLACEHOLDERS // silence Boost pragma message (fixed in Boost 1.76)
4 #include <boost/property_tree/json_parser.hpp>
5 
6 #include <fstream>
7 #include <map>
8 #include <iostream>
9 #include <sstream>
10 
11 namespace LVL1MUCTPIPHASE1
12 {
13  void L1TopoLUT::fillFromPtree(const boost::property_tree::ptree& node, std::map<unsigned short,std::vector<float>>& theLut) const
14  {
15  theLut.clear();
16  size_t outerIndex = 0;
17  for (const boost::property_tree::ptree::value_type& outer_elem : node){
18 
19  std::vector<float> buff(outer_elem.second.size());
20  size_t index = 0;
21  for (const boost::property_tree::ptree::value_type& inner_elem : outer_elem.second) {
22  buff[index] = inner_elem.second.get_value<float>();
23  ++index; //count index manually to avoid some stoi
24  }
25  //cannot get around stoi as outer layer may be non-continuous (some indices/keys are not present)
26  if (outer_elem.first.length() > 0) { //string type key
27  theLut[std::stoi(outer_elem.first)] = buff;
28  } else {
29  theLut[outerIndex] = buff;
30  ++outerIndex; //ok to only count in this case, mixed keys/indices at same depth are not valid
31  }
32  }
33  return;
34  }
35 
36  void L1TopoLUT::fillFromPtree(const boost::property_tree::ptree& node, std::map<unsigned short,std::map<unsigned short, std::pair<unsigned short, unsigned short>>>& theLut) const
37  {
38  theLut.clear();
39  for (const boost::property_tree::ptree::value_type& outer_elem : node){
40 
41  std::map<unsigned short,std::pair<unsigned short, unsigned short>> buff;
42  for (const boost::property_tree::ptree::value_type& inner_elem : outer_elem.second) {
43  std::vector<unsigned short> codeBuff;
44  for (const boost::property_tree::ptree::value_type& code_elem : inner_elem.second) {
45  codeBuff.push_back( code_elem.second.get_value<unsigned short>() );
46  }
47  if ( codeBuff.size() != 2 ) {
48  //stricter error checking/handling comes later
49  continue;
50  }
51  buff[std::stoi(inner_elem.first)] = { codeBuff[0], codeBuff[1] } ;
52  }
53  //cannot get around stoi as outer layer may be non-continuous (some indices/keys are not present)
54  theLut[std::stoi(outer_elem.first)] = buff;
55  }
56  return;
57  }
58 
59 
60  bool L1TopoLUT::initializeCompactedLUTs(const std::string& side0LUTFileName,
61  const std::string& side1LUTFileName) {
62  bool success = true;
64  boost::property_tree::read_json(side0LUTFileName.c_str(), root0);
66  boost::property_tree::read_json(side1LUTFileName.c_str(), root1);
67 
68  //Barrel encoding
69  fillFromPtree(root0.get_child("encode_lookup_barrel") , m_barrel_encoding0);
70  fillFromPtree(root1.get_child("encode_lookup_barrel") , m_barrel_encoding1);
71 
72  //Barrel, A-side
73  fillFromPtree(root0.get_child("lookup_barrel_eta") , m_barrel_eta_lookup0);
74  fillFromPtree(root0.get_child("lookup_barrel_phi") , m_barrel_phi_lookup0);
75  //Barrel, C-side
76  fillFromPtree(root1.get_child("lookup_barrel_eta") , m_barrel_eta_lookup1);
77  fillFromPtree(root1.get_child("lookup_barrel_phi") , m_barrel_phi_lookup1);
78  //Endcap, A-side
79  fillFromPtree(root0.get_child("lookup_endcap_eta") , m_endcap_eta_lookup0);
80  fillFromPtree(root0.get_child("lookup_endcap_phi") , m_endcap_phi_lookup0);
81  //Endcap, C-side
82  fillFromPtree(root1.get_child("lookup_endcap_eta") , m_endcap_eta_lookup1);
83  fillFromPtree(root1.get_child("lookup_endcap_phi") , m_endcap_phi_lookup1);
84  //Forward, A-side
85  fillFromPtree(root0.get_child("lookup_forward_eta") , m_forward_eta_lookup0);
86  fillFromPtree(root0.get_child("lookup_forward_phi") , m_forward_phi_lookup0);
87  //Forward, C-side
88  fillFromPtree(root1.get_child("lookup_forward_eta") , m_forward_eta_lookup1);
89  fillFromPtree(root1.get_child("lookup_forward_phi") , m_forward_phi_lookup1);
90 
91 
92  if (m_barrel_encoding1.size() && m_barrel_encoding1.size() &&
93  m_barrel_phi_lookup1.size() && m_barrel_phi_lookup0.size() &&
94  m_endcap_phi_lookup1.size() && m_endcap_phi_lookup0.size() &&
95  m_forward_phi_lookup1.size() && m_forward_phi_lookup0.size() ) {
96  return success;
97  }
98  return false;
99  }
100 
101  bool L1TopoLUT::initializeLUT(const std::string& barrelFileName,
102  const std::string& ecfFileName,
103  const std::string& side0LUTFileName,
104  const std::string& side1LUTFileName) {
105  m_encoding.clear();
106  m_errors.clear();
107 
108  bool success = true;
109 
110  // read the compacted LUTs containing the simplified eta/phi values
111  // used for encoding/decoding for/at L1Topo
112  success = success && initializeCompactedLUTs(side0LUTFileName, side1LUTFileName);
113 
114  //read remaining info from the json files
115  //(the input labels are swapped between A-side and C-side)
116  //success = success && initializeJSON(side1LUTFileName, 0);
117  //success = success && initializeJSON(side0LUTFileName, 1);
118 
119  //read the barrel file
120  success = success && initializeLUT(barrelFileName, true);
121 
122  //read the endcap+forward file
123  success = success && initializeLUT(ecfFileName, false);
124 
125  return success;
126  }
127 
128  bool L1TopoLUT::initializeLUT(const std::string& inFileName, const bool& isBarrel) {
129  //map between L1TopoLUTKey -> map between eta/phi -> index
130  std::unordered_map<L1TopoLUTKey, std::map<float, unsigned short>, L1TopoLUTKeyHasher> sector_eta_indices, sector_phi_indices;
131 
132  std::ifstream inFile(inFileName.c_str());
133  if (!inFile) return false;
134  while (!inFile.eof() && inFile.good()) {
135  unsigned short side;
136  unsigned short subsystem=0;
137  unsigned short sectorID;
138  unsigned short roi;
139  double eta_min, eta_max;
140  double phi_min, phi_max;
141 
142  inFile >> side;
143  if (inFile.eof()) { break; }
144 
145  if (!isBarrel) {
146  inFile >> subsystem;
147  //in the EC+FW file, EC=0 and FW=1, so increment to distinguish from barrel
148  subsystem++;
149  side = !side; //EC+FW file uses inverted side numbers w.r.t. Barrel...
150  }
151  //at this point the "side" convention is
152  // 0 -> C side
153  // 1 -> A side
154  // the LUT json file naming, however, if inverse (follows EC+FW file...)
155 
156  inFile >> sectorID;
157  inFile >> roi;
158  inFile >> eta_min;
159  inFile >> eta_max;
160  inFile >> phi_min;
161  inFile >> phi_max;
162 
163  double eta = getCompactedValue_eta(subsystem, side, sectorID, roi);
164  double phi = getCompactedValue_phi(subsystem, side, sectorID, roi);
165 
166  unsigned short ieta = 0;
167  unsigned short iphi = 0;
168  if (subsystem == 0) { //barrel
169  //note: interpretation of values 0/1 for side flipped on purpose here (different conventions)!
170  ieta = (side ? m_barrel_encoding0 : m_barrel_encoding1)[sectorID][roi].first;
171  iphi = (side ? m_barrel_encoding0 : m_barrel_encoding1)[sectorID][roi].second;
172  } else if (subsystem == 1) { //endcap
173  ieta = (roi>>2); //eta part of roi
174  ieta |= 1 << 6; //detector code
175  iphi = roi & 0x3; //phi part of roi
176  iphi |= (sectorID & 0x3F) << 2;
177  } else if (subsystem == 2) { //forward
178  ieta = (roi>>2); //eta part of roi
179  ieta |= 1 << 5; //detector code
180  iphi = (roi & 0x3) << 1; //phi part of roi
181  iphi |= (sectorID & 0x1F) << 3;
182  }
183  //truncate to 8bit values each:
184  ieta &= 0xFF;
185  iphi &= 0xFF;
186 
187  if (subsystem == 0) {
188  //populate reverse map for barrel
189  unsigned short etaPhiSubWord = ((ieta&0xF) << 9) | ((sectorID&0x1F) << 3) | (iphi&0x7);
190  if (side) {
191  m_barrel_reverse_encoding0[etaPhiSubWord] = roi;
192  } else {
193  m_barrel_reverse_encoding1[etaPhiSubWord] = roi;
194  }
195  }
196 
197  //reminder in case of debugging needs:
198  //compacted values should be close to these averages
199  //(albeit some are different enough to cause mismatches in Topo!)
200  //float avgEta = (eta_min+eta_max)/2.;
201  //float avgPhi = (phi_min+phi_max)/2.;
202 
203 
204  L1TopoLUTKey key = {side, subsystem, sectorID, roi};
205  L1TopoCoordinates value = {eta, phi, eta_min, eta_max, phi_min, phi_max, ieta, iphi};
206 
207  if (m_encoding.find(key) != m_encoding.end())
208  {
209  m_errors.push_back("Duplicate key found in L1TopoLUT: "+key.info());
210  return false;
211  }
212  m_encoding[key] = value;
213  }
214  return true;
215  }
216 
217 
219  const unsigned short& subsystem,
220  const unsigned short& sectorID,
221  const unsigned short& roi) const {
222  L1TopoLUTKey key = {side, subsystem, sectorID, roi};
223  auto itr = m_encoding.find(key);
224  if (itr == m_encoding.end())
225  {
226  L1TopoCoordinates null;
227  return null;
228  }
229  return itr->second;
230  }
231 
232  unsigned short L1TopoLUT::getBarrelROI(unsigned short side, unsigned short sector, unsigned short ieta, unsigned short iphi) const {
233  unsigned short etaPhiSubWord = ((ieta&0xF) << 9) | ((sector&0x1F) << 3) | (iphi&0x7);
234  unsigned short retval = 0;
235  if (side) {
236  retval = m_barrel_reverse_encoding0.at(etaPhiSubWord);
237  } else {
238  retval = m_barrel_reverse_encoding1.at(etaPhiSubWord);
239  }
240  return retval;
241  }
242 
243 
244  float L1TopoLUT::getCompactedValue_eta(unsigned short subsystem, unsigned short side, unsigned short sectorID, unsigned short roi) {
245  //note: scheme used for "side" argument here is
246  // 0 -> C side
247  // 1 -> A side
248  float retval = std::numeric_limits<float>::quiet_NaN();
249  try {
250  if (subsystem == 0) { //barrel
251  //barrel is very irregular, so need to retrieve indices for MUCTPI->L1Topo encoding scheme first
252  unsigned short lutCode = (side ? m_barrel_encoding0 : m_barrel_encoding1)[sectorID][roi].first;
253  retval = (side ? m_barrel_eta_lookup0 : m_barrel_eta_lookup1)[sectorID][lutCode];
254  } else if (subsystem == 1) { //endcap
255  retval = (side ? m_endcap_eta_lookup0 : m_endcap_eta_lookup1)[roi >> 2][sectorID & 0x1];
256  } else if (subsystem == 2) { //forward
257  retval = (side ? m_forward_eta_lookup0 : m_forward_eta_lookup1)[sectorID & 0x1][roi >> 2];
258  } else {
259  std::stringstream ess;
260  ess << "[TrigT1MuctpiPhase1/L1TopoLUT] Unknown muon subsystem value: " << subsystem;
261  throw std::out_of_range(ess.str());
262  }
263  } catch (const std::exception& e) {
264  std::stringstream ess;
265  ess << "[TrigT1MuctpiPhase1/L1TopoLUT] Failed to look up eta value for subsystem=" << subsystem
266  << ", side="<< (side?"A":"C")
267  << ", sectorID="<< sectorID
268  << ", roi="<< roi
269  << "\n (caught exception: " << e.what() <<")";
270  throw std::out_of_range(ess.str());
271  }
272  return retval;
273  }
274 
275  float L1TopoLUT::getCompactedValue_phi(unsigned short subsystem, unsigned short side, unsigned short sectorID, unsigned short roi) {
276  //note: scheme used for "side" argument here is
277  // 0 -> C side
278  // 1 -> A side
279  float retval = std::numeric_limits<float>::quiet_NaN();
280  try {
281  if (subsystem == 0) { //barrel
282  //barrel is very irregular, so need to retrieve indices for MUCTPI->L1Topo encoding scheme first
283  unsigned short lutCode = (side ? m_barrel_encoding0 : m_barrel_encoding1)[sectorID][roi].second;
284  retval = (side ? m_barrel_phi_lookup0 : m_barrel_phi_lookup1)[sectorID][lutCode];
285  } else if (subsystem == 1) { //endcap
286  retval = (side ? m_endcap_phi_lookup0 : m_endcap_phi_lookup1)[sectorID][roi & 0x3];
287  } else if (subsystem == 2) { //forward
288  retval = (side ? m_forward_phi_lookup0 : m_forward_phi_lookup1)[roi & 0x3][sectorID];
289  } else {
290  std::stringstream ess;
291  ess << "[TrigT1MuctpiPhase1/L1TopoLUT] Unknown muon subsystem value: " << subsystem;
292  throw std::out_of_range(ess.str());
293  }
294  } catch (const std::exception& e) {
295  std::stringstream ess;
296  ess << "[TrigT1MuctpiPhase1/L1TopoLUT] Failed to look up phi value for subsystem=" << subsystem
297  << ", side="<< (side?"A":"C")
298  << ", sectorID="<< sectorID
299  << ", roi="<< roi
300  << "\n (caught exception: " << e.what() <<")";
301  throw std::out_of_range(ess.str());
302  }
303  return retval;
304  }
305 
306 
307 }
LVL1MUCTPIPHASE1::L1TopoLUT::m_forward_eta_lookup1
std::map< unsigned short, std::vector< float > > m_forward_eta_lookup1
Definition: L1TopoLUT.h:125
LVL1MUCTPIPHASE1::L1TopoLUT::m_endcap_phi_lookup1
std::map< unsigned short, std::vector< float > > m_endcap_phi_lookup1
Definition: L1TopoLUT.h:123
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
get_hdefs.buff
buff
Definition: get_hdefs.py:61
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
plotBeamSpotCompare.x1
x1
Definition: plotBeamSpotCompare.py:216
LVL1MUCTPIPHASE1::L1TopoLUT::getBarrelROI
unsigned short getBarrelROI(unsigned short side, unsigned short sector, unsigned short ieta, unsigned short iphi) const
Definition: L1TopoLUT.cxx:232
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_reverse_encoding0
std::map< unsigned short, unsigned short > m_barrel_reverse_encoding0
Definition: L1TopoLUT.h:133
index
Definition: index.py:1
makeTOC.inFile
string inFile
Definition: makeTOC.py:5
LVL1MUCTPIPHASE1::L1TopoLUT::fillFromPtree
void fillFromPtree(const boost::property_tree::ptree &node, std::map< unsigned short, std::vector< float >> &theLut) const
Definition: L1TopoLUT.cxx:13
LVL1MUCTPIPHASE1::L1TopoLUT::m_encoding
std::unordered_map< L1TopoLUTKey, L1TopoCoordinates, L1TopoLUTKeyHasher > m_encoding
Definition: L1TopoLUT.h:136
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_encoding1
std::map< unsigned short, std::map< unsigned short, std::pair< unsigned short, unsigned short > > > m_barrel_encoding1
Definition: L1TopoLUT.h:130
LVL1MUCTPIPHASE1::L1TopoLUT::m_errors
std::vector< std::string > m_errors
Definition: L1TopoLUT.h:137
athena.value
value
Definition: athena.py:124
LVL1MUCTPIPHASE1::L1TopoLUT::getCompactedValue_eta
float getCompactedValue_eta(unsigned short subsystem, unsigned short side, unsigned short sectorID, unsigned short roi)
Definition: L1TopoLUT.cxx:244
LVL1MUCTPIPHASE1::L1TopoCoordinates
Definition: L1TopoLUT.h:23
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_phi_lookup1
std::map< unsigned short, std::vector< float > > m_barrel_phi_lookup1
Definition: L1TopoLUT.h:119
LVL1MUCTPIPHASE1::L1TopoLUT::m_endcap_phi_lookup0
std::map< unsigned short, std::vector< float > > m_endcap_phi_lookup0
Definition: L1TopoLUT.h:122
TRT::Hit::side
@ side
Definition: HitInfo.h:83
LArCellBinning_test.retval
def retval
Definition: LArCellBinning_test.py:112
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_eta_lookup0
std::map< unsigned short, std::vector< float > > m_barrel_eta_lookup0
Definition: L1TopoLUT.h:116
L1TopoLUT.h
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_eta_lookup1
std::map< unsigned short, std::vector< float > > m_barrel_eta_lookup1
Definition: L1TopoLUT.h:117
LVL1MUCTPIPHASE1
Definition: Configuration.h:11
LVL1MUCTPIPHASE1::L1TopoLUT::getCoordinates
L1TopoCoordinates getCoordinates(const unsigned short &side, const unsigned short &subsystem, const unsigned short &sectorID, const unsigned short &roi) const
Definition: L1TopoLUT.cxx:218
LVL1MUCTPIPHASE1::L1TopoLUT::L1TopoLUTKey
Definition: L1TopoLUT.h:85
calibdata.exception
exception
Definition: calibdata.py:496
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_phi_lookup0
std::map< unsigned short, std::vector< float > > m_barrel_phi_lookup0
Definition: L1TopoLUT.h:118
IDTPMcnv.inFileName
inFileName
Definition: IDTPMcnv.py:17
LVL1MUCTPIPHASE1::L1TopoLUT::initializeCompactedLUTs
bool initializeCompactedLUTs(const std::string &side0LUTFileName, const std::string &side1LUTFileName)
Definition: L1TopoLUT.cxx:60
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_encoding0
std::map< unsigned short, std::map< unsigned short, std::pair< unsigned short, unsigned short > > > m_barrel_encoding0
Definition: L1TopoLUT.h:129
ptree
boost::property_tree::ptree ptree
Definition: JsonFileLoader.cxx:16
LVL1MUCTPIPHASE1::L1TopoLUT::m_endcap_eta_lookup1
std::map< unsigned short, std::vector< float > > m_endcap_eta_lookup1
Definition: L1TopoLUT.h:121
LVL1MUCTPIPHASE1::L1TopoLUT::m_barrel_reverse_encoding1
std::map< unsigned short, unsigned short > m_barrel_reverse_encoding1
Definition: L1TopoLUT.h:134
DeMoScan.index
string index
Definition: DeMoScan.py:364
LVL1MUCTPIPHASE1::L1TopoLUT::L1TopoLUTKeyHasher
Definition: L1TopoLUT.h:110
LVL1MUCTPIPHASE1::L1TopoLUT::getCompactedValue_phi
float getCompactedValue_phi(unsigned short subsystem, unsigned short side, unsigned short sectorID, unsigned short roi)
Definition: L1TopoLUT.cxx:275
DeMoScan.first
bool first
Definition: DeMoScan.py:536
LVL1MUCTPIPHASE1::L1TopoLUT::initializeLUT
bool initializeLUT(const std::string &barrelFileName, const std::string &ecfFileName, const std::string &side0LUTFileName, const std::string &side1LUTFileName)
Definition: L1TopoLUT.cxx:101
python.LArCondContChannels.isBarrel
isBarrel
Definition: LArCondContChannels.py:659
LVL1MUCTPIPHASE1::L1TopoLUT::m_forward_phi_lookup0
std::map< unsigned short, std::vector< float > > m_forward_phi_lookup0
Definition: L1TopoLUT.h:126
LVL1MUCTPIPHASE1::L1TopoLUT::m_forward_phi_lookup1
std::map< unsigned short, std::vector< float > > m_forward_phi_lookup1
Definition: L1TopoLUT.h:127
LVL1MUCTPIPHASE1::L1TopoLUT::m_endcap_eta_lookup0
std::map< unsigned short, std::vector< float > > m_endcap_eta_lookup0
Definition: L1TopoLUT.h:120
L1TopoSimulationConfig.subsystem
subsystem
Definition: L1TopoSimulationConfig.py:263
LVL1MUCTPIPHASE1::L1TopoLUT::m_forward_eta_lookup0
std::map< unsigned short, std::vector< float > > m_forward_eta_lookup0
Definition: L1TopoLUT.h:124
node
Definition: memory_hooks-stdcmalloc.h:74
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37