ATLAS Offline Software
Loading...
Searching...
No Matches
gFexTowerBuilder.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5//***************************************************************************
6// gFexTowerBuilder - description
7// -------------------
8// Builds a gFexTowerContainer from CaloCellContainer (for supercells)
9// TriggerTowerContainer (for ppm tile towers)
10// Information about SCellContainer objects are in:
11// - https://gitlab.cern.ch/atlas/athena/-/blob/22.0/Calorimeter/CaloEvent/CaloEvent/CaloCell.h
12//
13// begin : 22 04 2025
14// email : jared.little@cern.ch
15//***************************************************************************/
16
17#include "gFexTowerBuilder.h"
18
19#include <algorithm>
20#include <fstream>
21#include <iostream>
22#include <sstream>
23#include <string>
24#include <format>
25
27
28namespace LVL1 {
29
30 gFexTowerBuilder::gFexTowerBuilder(const std::string& name, ISvcLocator* svc)
32
34
36 "Initializing L1CaloFEXAlgos/gFexEmulatedTowers algorithm with name: "
37 << name());
38 ATH_MSG_INFO("Writing into SG key: " << m_gTowersWriteKey);
39 ATH_MSG_INFO("SCell masking: " << m_apply_masking);
40
41 ATH_CHECK(m_SCellKey.initialize());
42 ATH_CHECK(m_triggerTowerKey.initialize());
43 ATH_CHECK(m_gTowersWriteKey.initialize());
44
45 // Reading from CVMFS Fiber mapping
47
48 // Reading from CVMFS Trigger Tower and their corresponding SCell ID
51
52 return StatusCode::SUCCESS;
53 }
54
55 StatusCode gFexTowerBuilder::execute(const EventContext& ctx) const {
56
57 // Reading the Scell container
59 if (!ScellContainer.isValid()) {
60 ATH_MSG_ERROR("Could not retrieve collection " << ScellContainer.key());
61 return StatusCode::FAILURE;
62 }
63
64 // Reading the TriggerTower container
67 if (!triggerTowerContainer.isValid()) {
68 ATH_MSG_ERROR("Could not retrieve collection "
69 << triggerTowerContainer.key());
70 return StatusCode::FAILURE;
71 }
72
73
74 // WriteHandle for gFEX EDMs
76 ATH_CHECK( gTowersContainer.record(std::make_unique<xAOD::gFexTowerContainer>(),
77 std::make_unique<xAOD::gFexTowerAuxContainer>()) );
78 ATH_MSG_DEBUG("Recorded gFexEmulatedTower container with key " << gTowersContainer.key());
79
80 if (ScellContainer->empty() || triggerTowerContainer->empty()) {
82 "Cannot fill gTowers here, at least one container is empty. "
83 "ScellContainer.size="
84 << ScellContainer->size()
85 << " or triggerTowerContainer.size=" << triggerTowerContainer->size());
86 return StatusCode::SUCCESS;
87 }
88
89 // building Scell ID pointers
90 std::unordered_map<uint64_t, const CaloCell*> map_ScellID2ptr;
91 map_ScellID2ptr.reserve(ScellContainer->size());
92
93 for (const CaloCell* scell : *ScellContainer) {
94 const uint64_t ID = scell->ID().get_compact();
95 map_ScellID2ptr[ID] = scell;
96 }
97
98 // building Tile ID pointers
99 std::unordered_map<uint32_t, const xAOD::TriggerTower*> map_TileID2ptr;
100 map_TileID2ptr.reserve(triggerTowerContainer->size());
101
102 for (const xAOD::TriggerTower* tower : *triggerTowerContainer) {
103 map_TileID2ptr[tower->coolId()] = tower;
104 }
105 for (const auto& [key, element] : m_Firm2Tower_map) {
106 unsigned int towerID = key;
107 const auto [fpga, eta, phi, source] = element;
108
109 // the summed encoded Et from LAr or Tile
110 uint16_t total_et_encoded = 0;
111 char gTower_sat = 0;
112 // Note input fpga distinguishes between LAr (0,1,2), Tile (3) and duplicated channels (4)
113 if (source == 0) {
114
115 // check if the towerID exists in the LAr map
116 auto it_TTower2SCells = m_map_TTower2SCells.find(towerID);
117 if (it_TTower2SCells == m_map_TTower2SCells.end()) {
118 ATH_MSG_ERROR("gFEX ID: " << towerID
119 << " not found on map m_map_TTower2SCells");
120 return StatusCode::FAILURE;
121 }
122
123
124 bool invalid = m_apply_masking && m_isDATA; // the isDATA is because there is no concept of invalid supercell in MC (the provenance bit is actually used for BCID in MC), so can never have an invalid jTower
125 bool masked = m_apply_masking;
126
127 // loop over the SCell IDs and calculate encoded Et
128 int total_Et = 0;
129 bool isConnected = true;
130 for (const auto& scellID : it_TTower2SCells->second) {
131 // check if the SCell ID exists in the map
132 auto it_ScellID2ptr = map_ScellID2ptr.find(scellID);
133
134 // unconnected Towers have a single SCell with special id, set energy to zero
135 if (scellID == 0xffffffffffffffff) {
136 isConnected = false;
137 continue;
138 }
139
140 // check if other SCells are in the map
141 std::string str_hex = std::format("{:x}", scellID);
142
143 if (it_ScellID2ptr == map_ScellID2ptr.end()) {
144 if (m_isDATA)
145 ATH_MSG_WARNING("SCell ID: "
146 << scellID
147 << " not found in the CaloCellContainer, skipping");
148 continue;
149 }
150 // working with "local" fiber number, iFiber
151 unsigned int offset = (towerID > 20000) ? 20000 : (towerID > 10000 && towerID < 20000) ? 10000 : 0;
152 unsigned int iFiber = (towerID - offset)/16;
153
154 // Do not exceed maximum number of fibers for FPGA
155 unsigned int maxFiberN = (towerID > 20000) ? LVL1::gFEXPos::C_FIBERS : LVL1::gFEXPos::AB_FIBERS;
156 if (iFiber >= maxFiberN) continue;
157
158 int fiber_type = (towerID < 10000) ? LVL1::gFEXPos::AMPD_NFI[iFiber] :
159 (towerID > 10000 && towerID < 20000) ? LVL1::gFEXPos::BMPD_NFI[iFiber] :
161
162 // Data Type: 3 is extended region ( HEC), 6 is 200MeV region only ( HEC), 11 is HEC - Had contribution
163 int dataType = (towerID < 10000) ? LVL1::gFEXPos::AMPD_DTYP_ARR[fiber_type][towerID%16] :
164 (towerID > 10000 && towerID < 20000) ? LVL1::gFEXPos::BMPD_DTYP_ARR[fiber_type][towerID%16] :
165 LVL1::gFEXPos::CMPD_DTYP_ARR[fiber_type][towerID%16];
166
167 const CaloCell* scell = it_ScellID2ptr->second;
168 int val = std::round(scell->energy() / (12.5 * std::cosh(scell->eta()))); // 12.5 b.c. energy units of 12.5 MeV per count
169 if (!m_isDATA && m_applyTimingCut && !(scell->provenance()&0x200) && dataType != 11) {
170 val = 0; // apply timing cut to MC (already present in Data)
171 }
172 else if (!m_isDATA && m_applyTimingCutAll && !(scell->provenance()&0x200)) {
173 val = 0; // apply timing cut to MC (already present in Data)
174 }
175
176 bool isMasked =
177 m_apply_masking ? ((scell)->provenance() & 0x80) : false;
178 bool isInvalid =
179 (m_apply_masking&&m_isDATA) ? ((scell)->provenance()&0x40) : false;
180 bool isSaturated = (m_isDATA) ? scell->quality() : false; // saturation algorithm not implemented in MC yet
181
182 invalid &= isInvalid;
183 masked &= isMasked;
184 if (!isMasked) {
185 gTower_sat |= isSaturated;
186 }
187
188 if (isMasked) {
189 val = 0;
190 } else if( isInvalid&&m_isDATA) {
191 val = 0;
192 }
193
194 if (val != 0)
195 total_Et += val;
196
197 } // end of SCell loop
198
199 // now must convert Total_Et int value into fex value: multi-level encoding
200 if(!isConnected) {
201 total_et_encoded = 0; // no data
202 } else if(masked) {
203 total_et_encoded = 0; // no data
204 } else if(invalid) {
205 total_et_encoded = 4095; // invalid
206 } else {
207 total_et_encoded = gFEXCompression::compress(12.5 * total_Et);
208 }
209
210
211 } else if (source == 1) {
212
213 // Tile
214 // check that the gFEX Tower ID exists in the Tile map
215 auto it_TTower2Tile = m_map_TTower2Tile.find(towerID);
216
217 int Tile_Et = 0;
218 for (auto const& TileTowerID : it_TTower2Tile->second) {
219 auto it_TileID2ptr = map_TileID2ptr.find(TileTowerID);
220 if (it_TileID2ptr == map_TileID2ptr.end()) {
221 if(m_isDATA) {
222 ATH_MSG_WARNING("Tile cool ID: " << TileTowerID
223 << " not found in the xAOD::TriggerTower (map_TileID2ptr)");
224 }
225 continue; // in MC the xAODTriggerTowers have variable size due to noise cuts, continue on to the next tower
226 } else {
227 const xAOD::TriggerTower* tileTower = it_TileID2ptr->second;
228 unsigned int jepEt = tileTower->jepET();
229 Tile_Et += jepEt; // add the encoded energies
230 }
231 }
232 total_et_encoded = Tile_Et;
233
234 } else if (source == 2) {
235 // duplicated Towers, LATOME sends 0
236 total_et_encoded = 0;
237 }
238
239 // the EDM requires a float
240 float total_et_encoded_flt = total_et_encoded;
241
242 unsigned int fpga_out = (towerID < 10000) ? 0 : (towerID < 20000) ? 1 : 2;
243 unsigned int iEta = 0;
244 unsigned int iPhi = 0;
245
246 gTowersContainer->push_back(std::make_unique<xAOD::gFexTower>());
247 gTowersContainer->back()->initialize(iEta, iPhi, eta, phi,
248 total_et_encoded_flt, fpga_out,
249 gTower_sat, towerID);
250 }
251
252 return StatusCode::SUCCESS;
253 }
254
255 StatusCode gFexTowerBuilder::ReadFibersfromFile(const std::string& fileName) {
256 // opening file with ifstream
257 std::ifstream file(fileName);
258
259 if (!file.is_open()) {
260 ATH_MSG_ERROR("Could not open file:" << fileName);
261 return StatusCode::FAILURE;
262 }
263 std::string line;
264 // loading the mapping information
265 while (std::getline(file, line)) {
266 // removing the header of the file (it is just information!)
267 if (line[0] == '#') continue;
268
269 // Splitting line in different substrings
270 std::stringstream oneLine(line);
271 // reading elements
272 std::vector<float> elements;
273 elements.reserve(5);
274 std::string element;
275 while (std::getline(oneLine, element, ' ')) {
276 elements.push_back(std::stof(element));
277 }
278
279 // It should have 5 elements
280 // ordered as: towerID fpga source eta phi
281
282 if (elements.size() != 5) {
284 "Unexpected number of elements (5 expected) in file: " << fileName);
285 return StatusCode::FAILURE;
286 }
287
288 // building array of <fpga, eta, phi, source>
289 std::array<float, 4> aux_arr{{elements.at(1), elements.at(3),
290 elements.at(4), elements.at(2)}};
291
292 // filling the map with the hash given by mapIndex()
293 m_Firm2Tower_map[elements.at(0)] = aux_arr;
294 }
295
296 file.close();
297
298 return StatusCode::SUCCESS;
299 }
300
301 StatusCode gFexTowerBuilder::ReadSCfromFile(const std::string& fileName) {
302
303 // opening file with ifstream
304 std::ifstream file(fileName);
305
306 if (!file.is_open()) {
307 ATH_MSG_ERROR("Could not open file:" << fileName);
308 return StatusCode::FAILURE;
309 }
310
311 std::string line;
312 // loading the mapping information into an unordered_map <Fex Tower ID, vector
313 // of SCell IDs>
314 while (std::getline(file, line)) {
315 // removing the header of the file (it is just information!)
316 if (line[0] == '#')
317 continue;
318
319 std::vector<uint64_t> SCellvector;
320
321 // Splitting line in different substrings
322 std::stringstream oneSCellID(line);
323
324 // reading elements
325 std::string substr = "";
326 int TTID = 0;
327 int elem = 0;
328
329 while (std::getline(oneSCellID, substr, ' ')) {
330 ++elem;
331 if (elem == 1) {
332 TTID = std::stoi(substr);
333 } else {
334 // Check if it looks like a SCell Identifier
335 if (isBadSCellID(substr)) {
336 return StatusCode::FAILURE;
337 }
338
339 // converts hex number to unsigned long long int
340 // unconnnected slots are filled with 0xffffffffffffffff
341 // otherwise SCell map used shorter form, add extra zeroes back
342 std::string scell_full =
343 (substr == "0xffffffffffffffff") ? substr : substr + "00000000";
344 uint64_t scid_uint64 = std::strtoull(scell_full.c_str(), nullptr, 0);
345 SCellvector.push_back(scid_uint64);
346 }
347 }
348
349 m_map_TTower2SCells[TTID] = std::move(SCellvector);
350 }
351 file.close();
352
353 return StatusCode::SUCCESS;
354 }
355
356 bool gFexTowerBuilder::isBadSCellID(const std::string& ID) const {
357
358 // does it start with "0x"?, if so then is a GOOD SCell ID!
359 if (ID.find("0x") == std::string::npos) {
360 ATH_MSG_ERROR("Invalid SuperCell ID "
361 << ID
362 << ". Expecting hexadecimal number on the mapping file");
363 return true;
364 }
365 return false;
366 }
367
368 StatusCode gFexTowerBuilder::ReadTilefromFile(const std::string& fileName) {
369
370 std::string myline;
371
372 // openning file with ifstream
373 std::ifstream myfile(fileName);
374
375 if (!myfile.is_open()) {
376 ATH_MSG_FATAL("Could not open file:" << fileName);
377 return StatusCode::FAILURE;
378 }
379
380 // loading the mapping information into an unordered_map <Fex Tower ID, vector
381 // of Tile IDs>
382 while (std::getline(myfile, myline)) {
383 // removing the header of the file
384 myline.erase(myline.begin(),
385 std::find_if(myline.begin(), myline.end(),
386 [](int ch) { return !std::isspace(ch); }));
387 if (myline[0] == '#')
388 continue;
389
390 std::vector<uint32_t> Tilevector;
391
392 // Splitting myline in different substrings
393 std::stringstream oneTileID(myline);
394
395 // reading elements
396 std::string substr = "";
397 int gTowerID = 0;
398 int elem = 0;
399
400 while (std::getline(oneTileID, substr, ' ')) {
401 ++elem;
402 if (elem == 1) {
403 gTowerID = std::stoi(substr);
404 } else {
405 uint32_t tileid_uint32 = std::strtoul(substr.c_str(), nullptr, 0);
406 Tilevector.push_back(tileid_uint32);
407 }
408 }
409 m_map_TTower2Tile[gTowerID] = std::move(Tilevector);
410 }
411 myfile.close();
412
413 return StatusCode::SUCCESS;
414 }
415
416
417} // namespace LVL1
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
An algorithm that can be simultaneously executed in multiple threads.
Data object for each calorimeter readout cell.
Definition CaloCell.h:57
double energy() const
get energy (data member)
Definition CaloCell.h:327
uint16_t provenance() const
get provenance (data member)
Definition CaloCell.h:354
virtual double eta() const override final
get eta (through CaloDetDescrElement)
Definition CaloCell.h:382
uint16_t quality() const
get quality (data member)
Definition CaloCell.h:348
static unsigned int compress(float Energy)
Compress data.
std::unordered_map< uint32_t, std::vector< uint64_t > > m_map_TTower2SCells
Gaudi::Property< bool > m_apply_masking
StatusCode ReadFibersfromFile(const std::string &)
gFexTowerBuilder(const std::string &name, ISvcLocator *svc)
Gaudi::Property< std::string > m_FiberMapping
virtual StatusCode initialize() override
Function initialising the algorithm.
Gaudi::Property< std::string > m_gFEX2Scellmapping
StatusCode ReadTilefromFile(const std::string &)
bool isBadSCellID(const std::string &) const
Gaudi::Property< std::string > m_gFEX2Tilemapping
StatusCode ReadSCfromFile(const std::string &)
SG::ReadHandleKey< xAOD::TriggerTowerContainer > m_triggerTowerKey
SG::WriteHandleKey< xAOD::gFexTowerContainer > m_gTowersWriteKey
SG::ReadHandleKey< CaloCellContainer > m_SCellKey
std::unordered_map< unsigned int, std::array< float, 4 > > m_Firm2Tower_map
virtual StatusCode execute(const EventContext &) const override
Function executing the algorithm.
Gaudi::Property< bool > m_applyTimingCut
Gaudi::Property< bool > m_applyTimingCutAll
Gaudi::Property< bool > m_isDATA
std::unordered_map< uint32_t, std::vector< uint32_t > > m_map_TTower2Tile
static std::string find_calib_file(const std::string &logical_file_name)
virtual bool isValid() override final
Can the handle be successfully dereferenced?
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
uint8_t jepET() const
get jepET from peak of lut_jep
constexpr std::array< std::array< char, 20 >, 4 > CMPD_DTYP_ARR
Definition gFexPos.h:495
constexpr std::array< int, 100 > CMPD_NFI
Definition gFexPos.h:376
constexpr int C_FIBERS
Definition gFexPos.h:61
constexpr std::array< std::array< char, 20 >, 4 > AMPD_DTYP_ARR
Definition gFexPos.h:217
constexpr std::array< std::array< char, 20 >, 4 > BMPD_DTYP_ARR
Definition gFexPos.h:352
constexpr std::array< int, 100 > AMPD_NFI
Definition gFexPos.h:106
constexpr std::array< int, 100 > BMPD_NFI
Definition gFexPos.h:242
constexpr int AB_FIBERS
Definition gFexPos.h:60
eFexTowerBuilder creates xAOD::eFexTowerContainer from supercells (LATOME) and triggerTowers (TREX) i...
TriggerTower_v2 TriggerTower
Define the latest version of the TriggerTower class.
TFile * file