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 // WriteHandle for gFEX EDMs
75 ATH_CHECK( gTowersContainer.record(std::make_unique<xAOD::gFexTowerContainer>(),
76 std::make_unique<xAOD::gFexTowerAuxContainer>()) );
77 ATH_MSG_DEBUG("Recorded gFexEmulatedTower container with key " << gTowersContainer.key());
78
79 if (ScellContainer->empty() || triggerTowerContainer->empty()) {
81 "Cannot fill gTowers here, at least one container is empty. "
82 "ScellContainer.size="
83 << ScellContainer->size()
84 << " or triggerTowerContainer.size=" << triggerTowerContainer->size());
85 return StatusCode::SUCCESS;
86 }
87
88 // building Scell ID pointers
89 std::unordered_map<uint64_t, const CaloCell*> map_ScellID2ptr;
90 map_ScellID2ptr.reserve(ScellContainer->size());
91
92 for (const CaloCell* scell : *ScellContainer) {
93 const uint64_t ID = scell->ID().get_compact();
94 map_ScellID2ptr[ID] = scell;
95 }
96
97 // building Tile ID pointers
98 std::unordered_map<uint32_t, const xAOD::TriggerTower*> map_TileID2ptr;
99 map_TileID2ptr.reserve(triggerTowerContainer->size());
100
101 for (const xAOD::TriggerTower* tower : *triggerTowerContainer) {
102 map_TileID2ptr[tower->coolId()] = tower;
103 }
104 for (const auto& [key, element] : m_Firm2Tower_map) {
105 unsigned int towerID = key;
106 const auto [fpga, eta, phi, source] = element;
107
108 // the summed encoded Et from LAr or Tile
109 uint16_t total_et_encoded = 0;
110 char gTower_sat = 0;
111 // Note input fpga distinguishes between LAr (0,1,2), Tile (3) and duplicated channels (4)
112 if (source == 0) {
113
114 // check if the towerID exists in the LAr map
115 auto it_TTower2SCells = m_map_TTower2SCells.find(towerID);
116 if (it_TTower2SCells == m_map_TTower2SCells.end()) {
117 ATH_MSG_ERROR("gFEX ID: " << towerID
118 << " not found on map m_map_TTower2SCells");
119 return StatusCode::FAILURE;
120 }
121
122
123 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
124 bool masked = m_apply_masking;
125
126 // loop over the SCell IDs and calculate encoded Et
127 int total_Et = 0;
128 bool isConnected = true;
129 for (const auto& scellID : it_TTower2SCells->second) {
130 // check if the SCell ID exists in the map
131 auto it_ScellID2ptr = map_ScellID2ptr.find(scellID);
132
133 // unconnected Towers have a single SCell with special id, set energy to zero
134 if (scellID == 0xffffffffffffffff) {
135 isConnected = false;
136 continue;
137 }
138
139 // check if other SCells are in the map
140 std::string str_hex = std::format("{:x}", scellID);
141
142 if (it_ScellID2ptr == map_ScellID2ptr.end()) {
143 if (m_isDATA)
144 ATH_MSG_WARNING("SCell ID: "
145 << scellID
146 << " not found in the CaloCellContainer, skipping");
147 continue;
148 }
149
150 const CaloCell* scell = it_ScellID2ptr->second;
151 int val = std::round(
152 scell->energy() /
153 (12.5 * std::cosh(scell->eta()))); // 12.5 is b.c. energy is in
154 // units of 12.5 MeV per count
155
156 bool isMasked =
157 m_apply_masking ? ((scell)->provenance() & 0x80) : false;
158 bool isInvalid =
159 (m_apply_masking&&m_isDATA) ? ((scell)->provenance()&0x40) : false;
160 bool isSaturated = (m_isDATA) ? scell->quality() : false; // saturation algorithm not implemented in MC yet
161
162 invalid &= isInvalid;
163 masked &= isMasked;
164 if (!isMasked) {
165 gTower_sat |= isSaturated;
166 }
167
168 if (isMasked) {
169 val = 0;
170 } else if( isInvalid&&m_isDATA) {
171 val = 0;
172 }
173
174 if (val != 0)
175 total_Et += val;
176
177 } // end of SCell loop
178
179 // now must convert Total_Et int value into fex value: multi-level encoding
180 if(!isConnected) {
181 total_et_encoded = 0; // no data
182 } else if(masked) {
183 total_et_encoded = 0; // no data
184 } else if(invalid) {
185 total_et_encoded = 4095; // invalid
186 } else {
187 total_et_encoded = gFEXCompression::compress(12.5 * total_Et);
188 }
189
190
191 } else if (source == 1) {
192
193 // Tile
194 // check that the gFEX Tower ID exists in the Tile map
195 auto it_TTower2Tile = m_map_TTower2Tile.find(towerID);
196
197 int Tile_Et = 0;
198 for (auto const& TileTowerID : it_TTower2Tile->second) {
199 auto it_TileID2ptr = map_TileID2ptr.find(TileTowerID);
200 if (it_TileID2ptr == map_TileID2ptr.end()) {
201 if(m_isDATA) {
202 ATH_MSG_WARNING("Tile cool ID: " << TileTowerID
203 << " not found in the xAOD::TriggerTower (map_TileID2ptr)");
204 }
205 continue; // in MC the xAODTriggerTowers have variable size due to noise cuts, continue on to the next tower
206 } else {
207 const xAOD::TriggerTower* tileTower = it_TileID2ptr->second;
208 unsigned int jepEt = tileTower->jepET();
209 Tile_Et += jepEt; // add the encoded energies
210 }
211 }
212 total_et_encoded = Tile_Et;
213
214 } else if (source == 2) {
215 // duplicated Towers, LATOME sends 0
216 total_et_encoded = 0;
217 }
218
219 // the EDM requires a float
220 float total_et_encoded_flt = total_et_encoded;
221
222 unsigned int fpga_out = (towerID < 10000) ? 0 : (towerID < 20000) ? 1 : 2;
223 unsigned int iEta = 0;
224 unsigned int iPhi = 0;
225
226 gTowersContainer->push_back(std::make_unique<xAOD::gFexTower>());
227 gTowersContainer->back()->initialize(iEta, iPhi, eta, phi,
228 total_et_encoded_flt, fpga_out,
229 gTower_sat, towerID);
230 }
231
232 return StatusCode::SUCCESS;
233 }
234
235 StatusCode gFexTowerBuilder::ReadFibersfromFile(const std::string& fileName) {
236 // opening file with ifstream
237 std::ifstream file(fileName);
238
239 if (!file.is_open()) {
240 ATH_MSG_ERROR("Could not open file:" << fileName);
241 return StatusCode::FAILURE;
242 }
243 std::string line;
244 // loading the mapping information
245 while (std::getline(file, line)) {
246 // removing the header of the file (it is just information!)
247 if (line[0] == '#') continue;
248
249 // Splitting line in different substrings
250 std::stringstream oneLine(line);
251 // reading elements
252 std::vector<float> elements;
253 elements.reserve(5);
254 std::string element;
255 while (std::getline(oneLine, element, ' ')) {
256 elements.push_back(std::stof(element));
257 }
258
259 // It should have 5 elements
260 // ordered as: towerID fpga source eta phi
261
262 if (elements.size() != 5) {
264 "Unexpected number of elements (5 expected) in file: " << fileName);
265 return StatusCode::FAILURE;
266 }
267
268 // building array of <fpga, eta, phi, source>
269 std::array<float, 4> aux_arr{{elements.at(1), elements.at(3),
270 elements.at(4), elements.at(2)}};
271
272 // filling the map with the hash given by mapIndex()
273 m_Firm2Tower_map[elements.at(0)] = aux_arr;
274 }
275
276 file.close();
277
278 return StatusCode::SUCCESS;
279 }
280
281 StatusCode gFexTowerBuilder::ReadSCfromFile(const std::string& fileName) {
282
283 // opening file with ifstream
284 std::ifstream file(fileName);
285
286 if (!file.is_open()) {
287 ATH_MSG_ERROR("Could not open file:" << fileName);
288 return StatusCode::FAILURE;
289 }
290
291 std::string line;
292 // loading the mapping information into an unordered_map <Fex Tower ID, vector
293 // of SCell IDs>
294 while (std::getline(file, line)) {
295 // removing the header of the file (it is just information!)
296 if (line[0] == '#')
297 continue;
298
299 std::vector<uint64_t> SCellvector;
300
301 // Splitting line in different substrings
302 std::stringstream oneSCellID(line);
303
304 // reading elements
305 std::string substr = "";
306 int TTID = 0;
307 int elem = 0;
308
309 while (std::getline(oneSCellID, substr, ' ')) {
310 ++elem;
311 if (elem == 1) {
312 TTID = std::stoi(substr);
313 } else {
314 // Check if it looks like a SCell Identifier
315 if (isBadSCellID(substr)) {
316 return StatusCode::FAILURE;
317 }
318
319 // converts hex number to unsigned long long int
320 // unconnnected slots are filled with 0xffffffffffffffff
321 // otherwise SCell map used shorter form, add extra zeroes back
322 std::string scell_full =
323 (substr == "0xffffffffffffffff") ? substr : substr + "00000000";
324 uint64_t scid_uint64 = std::strtoull(scell_full.c_str(), nullptr, 0);
325 SCellvector.push_back(scid_uint64);
326 }
327 }
328
329 m_map_TTower2SCells[TTID] = std::move(SCellvector);
330 }
331 file.close();
332
333 return StatusCode::SUCCESS;
334 }
335
336 bool gFexTowerBuilder::isBadSCellID(const std::string& ID) const {
337
338 // does it start with "0x"?, if so then is a GOOD SCell ID!
339 if (ID.find("0x") == std::string::npos) {
340 ATH_MSG_ERROR("Invalid SuperCell ID "
341 << ID
342 << ". Expecting hexadecimal number on the mapping file");
343 return true;
344 }
345 return false;
346 }
347
348 StatusCode gFexTowerBuilder::ReadTilefromFile(const std::string& fileName) {
349
350 std::string myline;
351
352 // openning file with ifstream
353 std::ifstream myfile(fileName);
354
355 if (!myfile.is_open()) {
356 ATH_MSG_FATAL("Could not open file:" << fileName);
357 return StatusCode::FAILURE;
358 }
359
360 // loading the mapping information into an unordered_map <Fex Tower ID, vector
361 // of Tile IDs>
362 while (std::getline(myfile, myline)) {
363 // removing the header of the file
364 myline.erase(myline.begin(),
365 std::find_if(myline.begin(), myline.end(),
366 [](int ch) { return !std::isspace(ch); }));
367 if (myline[0] == '#')
368 continue;
369
370 std::vector<uint32_t> Tilevector;
371
372 // Splitting myline in different substrings
373 std::stringstream oneTileID(myline);
374
375 // reading elements
376 std::string substr = "";
377 int gTowerID = 0;
378 int elem = 0;
379
380 while (std::getline(oneTileID, substr, ' ')) {
381 ++elem;
382 if (elem == 1) {
383 gTowerID = std::stoi(substr);
384 } else {
385 uint32_t tileid_uint32 = std::strtoul(substr.c_str(), nullptr, 0);
386 Tilevector.push_back(tileid_uint32);
387 }
388 }
389 m_map_TTower2Tile[gTowerID] = std::move(Tilevector);
390 }
391 myfile.close();
392
393 return StatusCode::SUCCESS;
394 }
395
396
397} // 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)
std::vector< Identifier > ID
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
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_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
eFexTowerBuilder creates xAOD::eFexTowerContainer from supercells (LATOME) and triggerTowers (TREX) i...
TriggerTower_v2 TriggerTower
Define the latest version of the TriggerTower class.
TFile * file