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