ATLAS Offline Software
CaloCellsCounterGPU.cxx
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 //
4 // Dear emacs, this is -*- c++ -*-
5 //
6 
7 #include "CaloCellsCounterGPU.h"
9 
10 #include <map>
11 #include <vector>
12 #include <filesystem>
13 
14 #include "FPHelpers.h"
15 
16 using namespace CaloRecGPU;
17 
18 CaloCellsCounterGPU::CaloCellsCounterGPU(const std::string & type, const std::string & name, const IInterface * parent):
19  base_class(type, name, parent)
20 {
21 }
22 
23 
24 namespace {
25 
26 
27 struct size_struct
28 {
29  unsigned int total = 0, seed = 0, grow = 0, term = 0, invalid = 0, shared = 0;
30  template <class Str>
31  friend Str & operator << (Str & s, const size_struct & sst)
32  {
33  s << sst.total << " " << sst.seed << " " << sst.grow << " " << sst.term << " " << sst.invalid << " " << sst.shared;
34  return s;
35  }
36 };
37 
38 struct cluster_info_struct
39 {
40  size_struct size;
41  float seed_snr = -9e99;
42  float seed_energy = -9e99;
43  template <class Str>
44  friend Str & operator << (Str & s, const cluster_info_struct & cis)
45  {
46  s << cis.size << " (" << cis.seed_snr << " " << cis.seed_energy << ")";
47  return s;
48  }
49 };
50 
51 } // anonymous namespace
52 
53 StatusCode CaloCellsCounterGPU::execute(const EventContext & ctx, const ConstantDataHolder & constant_data, EventDataHolder & event_data, void * /*temporary_buffer*/) const
54 {
55  Helpers::CPU_object<CellNoiseArr> cell_noise(constant_data.m_cell_noise_dev);
56  //We could try to keep this in CPU memory by default,
57  //but, since it isn't quite needed and we don't mind taking a bit longer for this
58  //since we're only debugging, let this be as generic as possible.
59 
63 
64  unsigned int gain_counts[GainConversion::num_gain_values()] = {0};
65 
66  size_struct global_counts, global_cluster_counts;
67 
68  std::vector<int> cluster_max_cell(clusters->number, -1);
69  std::vector<float> cluster_max_snr(clusters->number, -9e99);
70  std::vector<float> cluster_max_energy(clusters->number, -9e99);
71 
72  std::vector<size_struct> cluster_counts(clusters->number);
73 
74  std::vector<int> shared_cells;
75 
76  for (int i = 0; i < NCaloCells; ++i)
77  {
78  if (!cell_info->is_valid(i))
79  {
80  continue;
81  }
82 
83  const int gain = cell_info->gain[i];
84  ++gain_counts[gain - GainConversion::min_gain_value()];
85 
86  const float energy = cell_info->energy[i];
87 
88  const float SNR = std::abs( energy / cell_noise->get_noise(i, gain) );
89 
90  const ClusterTag tag = cell_state->clusterTag[i];
91 
92  const bool is_cluster = tag.is_part_of_cluster();
93 
94  global_cluster_counts.total += is_cluster;
95 
96  if (SNR > m_seedThreshold)
97  {
98  ++global_counts.seed;
99  global_cluster_counts.seed += is_cluster;
100  }
101  else if (SNR > m_growThreshold)
102  {
103  ++global_counts.grow;
104  global_cluster_counts.grow += is_cluster;
105  }
106  else if (SNR > m_cellThreshold)
107  {
108  ++global_counts.term;
109  global_cluster_counts.term += is_cluster;
110  }
111  else
112  {
113  ++global_counts.invalid;
114  global_cluster_counts.invalid += is_cluster;
115  }
116 
117  if (is_cluster)
118  {
119  if (tag.is_shared_between_clusters())
120  {
121  shared_cells.push_back(i);
122  }
123  const int cluster = tag.cluster_index();
124  const int other_cluster = tag.is_shared_between_clusters() ? tag.secondary_cluster_index() : cluster;
125  if (!tag.is_shared_between_clusters() && (SNR > cluster_max_snr[cluster] || (SNR == cluster_max_snr[cluster] && i > cluster_max_cell[cluster])))
126  {
127  cluster_max_snr[cluster] = SNR;
128  cluster_max_cell[cluster] = i;
129  cluster_max_energy[cluster] = std::abs(energy);
130  }
131  ++cluster_counts[cluster].total;
132  cluster_counts[other_cluster].total += (cluster != other_cluster);
133 
134  global_cluster_counts.shared += tag.is_shared_between_clusters();
135  cluster_counts[cluster].shared += tag.is_shared_between_clusters();
136  cluster_counts[other_cluster].shared += tag.is_shared_between_clusters();
137 
138  if (SNR > m_seedThreshold)
139  {
140  ++cluster_counts[cluster].seed;
141  cluster_counts[other_cluster].seed += (cluster != other_cluster);
142  }
143  else if (SNR > m_growThreshold)
144  {
145  ++cluster_counts[cluster].grow;
146  cluster_counts[other_cluster].grow += (cluster != other_cluster);
147  }
148  else if (SNR > m_cellThreshold)
149  {
150  ++cluster_counts[cluster].term;
151  cluster_counts[other_cluster].term += (cluster != other_cluster);
152  }
153  else
154  {
155  ++cluster_counts[cluster].invalid;
156  cluster_counts[other_cluster].invalid += (cluster != other_cluster);
157  }
158  }
159  }
160 
161  std::map<int, cluster_info_struct> cluster_sizes;
162 
163  for (int i = 0; i < clusters->number; ++i)
164  {
165  if (cluster_max_cell[i] >= 0)
166  {
167  cluster_sizes[cluster_max_cell[i]] = cluster_info_struct{cluster_counts[i], cluster_max_snr[i], cluster_max_energy[i]};
168  }
169  }
170 
171  std::sort(shared_cells.begin(), shared_cells.end());
172 
173 
174  const auto err1 = StandaloneDataIO::prepare_folder_for_output(std::string(m_savePath));
176  {
177  return StatusCode::FAILURE;
178  }
179 
180  const std::filesystem::path save_file = m_savePath + "/" + StandaloneDataIO::build_filename((m_filePrefix.size() > 0 ? m_filePrefix + "_counts" : "counts"),
181  ctx.evt(), m_fileSuffix, "txt", m_numWidth);
182 
183  std::ofstream out_file(save_file);
184 
185  if (!out_file.is_open())
186  {
187  return StatusCode::FAILURE;
188  }
189 
190  out_file << "Cell counts: " << global_counts << "\n\n";
191 
192  out_file << "Cells in clusters count: "<< global_cluster_counts << "\n\n";
193  out_file << "Clusters:\n\n";
194 
195  for (const auto & it : cluster_sizes)
196  {
197  out_file << it.first << " " << it.second << "\n";
198  }
199 
200  out_file << std::endl;
201 
202  if (!out_file.good())
203  {
204  return StatusCode::FAILURE;
205  }
206 
207  out_file.close();
208 
209  return StatusCode::SUCCESS;
210 
211 }
212 
StandaloneDataIO::build_filename
static std::string build_filename(const std::string &prefix, const std::string &text, const std::string &suffix, const std::string &ext)
Definition: StandaloneDataIO.h:273
CaloCellsCounterGPU::m_cellThreshold
Gaudi::Property< float > m_cellThreshold
Value to consider for the seed threshold.
Definition: CaloCellsCounterGPU.h:73
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
FPHelpers.h
CaloCellsCounterGPU::execute
virtual StatusCode execute(const EventContext &ctx, const CaloRecGPU::ConstantDataHolder &constant_data, CaloRecGPU::EventDataHolder &event_data, void *temporary_buffer) const override
Definition: CaloCellsCounterGPU.cxx:53
CaloCellsCounterGPU::CaloCellsCounterGPU
CaloCellsCounterGPU(const std::string &type, const std::string &name, const IInterface *parent)
Definition: CaloCellsCounterGPU.cxx:18
CaloCondBlobAlgs_fillNoiseFromASCII.gain
gain
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
CaloRecGPU::Helpers::SimpleHolder
Holds one objects of type \T in memory context Context.
Definition: Calorimeter/CaloRecGPU/CaloRecGPU/Helpers.h:1074
CaloRecGPU::EventDataHolder::m_cell_state_dev
CaloRecGPU::Helpers::CUDA_object< CaloRecGPU::CellStateArr > m_cell_state_dev
Definition: DataHolders.h:89
skel.it
it
Definition: skel.GENtoEVGEN.py:407
CaloRecGPU::EventDataHolder::m_clusters_dev
CaloRecGPU::Helpers::CUDA_object< CaloRecGPU::ClusterInfoArr > m_clusters_dev
Definition: DataHolders.h:90
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
CaloRecGPU::ClusterTag
Definition: TagDefinitions.h:222
CaloRecGPU::EventDataHolder::m_cell_info_dev
CaloRecGPU::Helpers::CUDA_object< CaloRecGPU::CellInfoArr > m_cell_info_dev
Definition: DataHolders.h:88
CaloCellsCounterGPU::m_numWidth
Gaudi::Property< unsigned int > m_numWidth
The number of digits to reserve for the events.
Definition: CaloCellsCounterGPU.h:58
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
StandaloneDataIO::ErrorState::OK
@ OK
CaloCellsCounterGPU::m_growThreshold
Gaudi::Property< float > m_growThreshold
Value to consider for the seed threshold.
Definition: CaloCellsCounterGPU.h:68
CaloRecGPU::EventDataHolder
Definition: DataHolders.h:35
ParticleGun_FastCalo_ChargeFlip_Config.energy
energy
Definition: ParticleGun_FastCalo_ChargeFlip_Config.py:78
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
test_pyathena.parent
parent
Definition: test_pyathena.py:15
StandaloneDataIO.h
CaloRecGPU::NCaloCells
constexpr int NCaloCells
Definition: BaseDefinitions.h:12
MuonEfficiencyCorrectionsCfg.out_file
out_file
Definition: MuonEfficiencyCorrectionsCfg.py:69
CaloCellsCounterGPU.h
CaloRecGPU::ConstantDataHolder::m_cell_noise_dev
CaloRecGPU::Helpers::CUDA_object< CaloRecGPU::CellNoiseArr > m_cell_noise_dev
Definition: DataHolders.h:30
CaloCellsCounterGPU::m_seedThreshold
Gaudi::Property< float > m_seedThreshold
Value to consider for the seed threshold.
Definition: CaloCellsCounterGPU.h:63
StandaloneDataIO::prepare_folder_for_output
static ErrorState prepare_folder_for_output(const std::filesystem::path &folder, const bool output_errors=true)
Definition: StandaloneDataIO.h:264
CaloCellsCounterGPU::m_filePrefix
Gaudi::Property< std::string > m_filePrefix
The prefix of the saved files.
Definition: CaloCellsCounterGPU.h:48
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
operator<<
std::ostream & operator<<(std::ostream &lhs, const TestGaudiProperty &rhs)
Definition: TestGaudiProperty.cxx:69
CaloCellsCounterGPU::m_fileSuffix
Gaudi::Property< std::string > m_fileSuffix
The suffix of the saved files.
Definition: CaloCellsCounterGPU.h:53
RunTileMonitoring.clusters
clusters
Definition: RunTileMonitoring.py:133
CaloRecGPU::ConstantDataHolder
Definition: DataHolders.h:19
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
CaloCellsCounterGPU::m_savePath
Gaudi::Property< std::string > m_savePath
The path specifying the folder to which the files should be saved.
Definition: CaloCellsCounterGPU.h:43
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:23
CaloRecGPU
Definition: BaseDefinitions.h:11