CMS 3D CMS Logo

DumpClustersDetails.h
Go to the documentation of this file.
1 #ifndef RecoLocalCalo_HGCalRecProducers_DumpClustersDetails_h
2 #define RecoLocalCalo_HGCalRecProducers_DumpClustersDetails_h
3 
4 #include <string>
5 #include <vector>
6 #include <iostream>
7 #include <fstream>
8 #include <sstream>
9 #include <algorithm>
10 #include <ctime>
11 #include <cstdlib>
12 #include <unistd.h> // For getpid
13 #include <fmt/format.h>
15 
16 namespace hgcalUtils {
17 
18  static bool sortByDetId(const std::pair<int, float>& pair1, const std::pair<int, float>& pair2) {
19  return pair1.first < pair2.first;
20  }
21 
22  class DumpClusters {
23  public:
24  DumpClusters() = default;
25 
26  template <typename T>
27  void dumpInfos(const T& clusters,
28  const std::string& moduleLabel,
32  bool dumpCellsDetId = false) const {
33  // Get the process ID
34  pid_t pid = getpid();
35 
36  // Create the filename using the PID
37  std::ostringstream filename;
38  filename << "CLUSTERS_" << pid << "_" << moduleLabel << "_" << run << "_" << lumi << "_" << event << ".txt";
39  // Open the file
40  std::ofstream outfile(filename.str());
41  int count = 0;
42  for (auto const& i : clusters) {
44  "Seed: {}, Idx: {}, energy: {:.{}f}, x: {:.{}f}, y: {:.{}f}, z: {:.{}f}, eta: {:.{}f}, phi: {:.{}f}",
45  i.seed().rawId(),
46  count++,
47  (float)i.energy(),
48  std::numeric_limits<float>::max_digits10,
49  i.x(),
50  std::numeric_limits<float>::max_digits10,
51  i.y(),
52  std::numeric_limits<float>::max_digits10,
53  i.z(),
54  std::numeric_limits<float>::max_digits10,
55  i.eta(),
56  std::numeric_limits<float>::max_digits10,
57  i.phi(),
58  std::numeric_limits<float>::max_digits10);
59  if (dumpCellsDetId) {
60  auto sorted = i.hitsAndFractions(); // copy...
61  std::stable_sort(std::begin(sorted), std::end(sorted), sortByDetId);
62  for (auto const& c : sorted) {
63  outfile << fmt::format(" ({}, {:.{}f})", c.first, c.second, std::numeric_limits<float>::max_digits10);
64  } // loop on hits and fractions
65  } else {
66  outfile << fmt::format(" ({} cells)", i.hitsAndFractions().size());
67  }
68  outfile << std::endl;
69  } // loop on clusters
70  outfile.close();
71  }
72  };
73 
75  public:
76  DumpClustersSoA() = default;
77 
78  template <typename T>
79  void dumpInfos(const T& clustersSoA,
80  const std::string& moduleLabel,
83  edm::EventNumber_t event) const {
84  // Get the process ID
85  pid_t pid = getpid();
86 
87  // Create the filename using the PID
88  std::ostringstream filename;
89  filename << "CLUSTERS_UTILS_SOA_" << pid << "_" << moduleLabel << "_" << run << "_" << lumi << "_" << event
90  << ".txt";
91  // Open the file
92  std::ofstream outfile(filename.str());
93  for (int i = 0; i < clustersSoA->metadata().size(); ++i) {
94  auto clusterSoAV = clustersSoA.view()[i];
95  outfile << fmt::format("Idx: {}, delta: {:.{}f}, rho: {:.{}f}, nearest: {}, clsIdx: {}, isSeed: {}",
96  i,
97  clusterSoAV.delta(),
98  std::numeric_limits<float>::max_digits10,
99  clusterSoAV.rho(),
100  std::numeric_limits<float>::max_digits10,
101  clusterSoAV.nearestHigher(),
102  clusterSoAV.clusterIndex(),
103  clusterSoAV.isSeed())
104  << std::endl;
105  }
106  outfile.close();
107  }
108  };
109 
110  class DumpCellsSoA {
111  public:
112  DumpCellsSoA() = default;
113 
114  template <typename T>
115  void dumpInfos(const T& cells,
116  const std::string& moduleLabel,
119  edm::EventNumber_t event) const {
120  // Get the process ID
121  pid_t pid = getpid();
122 
123  // Create the filename using the PID
124  std::ostringstream filename;
125  filename << "RECHITS_SOA_" << pid << "_" << moduleLabel << "_" << run << "_" << lumi << "_" << event << ".txt";
126  // Open the file
127  std::ofstream outfile(filename.str());
128  for (int i = 0; i < cells->metadata().size(); ++i) {
129  auto cellSoAV = cells.view()[i];
130  outfile
131  << fmt::format(
132  "Idx Cell: {}, x: {:.{}f}, y: {:.{}f}, layer: {}, weight: {:.{}f}, sigmaNoise: {:.{}f}, detid: {}",
133  i,
134  (cellSoAV.dim1()),
135  std::numeric_limits<float>::max_digits10,
136  (cellSoAV.dim2()),
137  std::numeric_limits<float>::max_digits10,
138  cellSoAV.layer(),
139  (cellSoAV.weight()),
140  std::numeric_limits<float>::max_digits10,
141  (cellSoAV.sigmaNoise()),
142  std::numeric_limits<float>::max_digits10,
143  cellSoAV.detid())
144  << std::endl;
145  }
146  outfile.close();
147  }
148  };
149 
151  public:
152  DumpLegacySoA() = default;
153 
154  template <typename T>
155  void dumpInfos(const T& cells, const std::string& moduleType) const {
156  // Get the process ID
157  pid_t pid = getpid();
158 
159  // Create the filename using the PID
160  std::ostringstream filename;
161  // Seed the random number generator
162  srand(time(0));
163  // Generate a random number between 100 and 999
164  int random_number = 100 + rand() % 900;
165 
166  filename << "RECHITS_LEGACY_" << pid << "_" << moduleType << "_" << random_number << ".txt";
167  // Open the file
168  std::ofstream outfile(filename.str());
169  for (unsigned int l = 0; l < cells.size(); l++) {
170  for (unsigned int i = 0; i < cells.at(l).dim1.size(); ++i) {
171  outfile << fmt::format(
172  "Idx Cell: {}, x: {:.{}f}, y: {:.{}f}, layer: {}, weight: {:.{}f}, sigmaNoise: {:.{}f}, "
173  "delta: {:.{}f}, rho: {:.{}f}, nearestHigher: {}, clsIdx: {}, isSeed: {}, detid: {}",
174  i,
175  (cells.at(l).dim1.at(i)),
176  std::numeric_limits<float>::max_digits10,
177  (cells.at(l).dim2.at(i)),
178  std::numeric_limits<float>::max_digits10,
179  l,
180  (cells.at(l).weight.at(i)),
181  std::numeric_limits<float>::max_digits10,
182  (cells.at(l).sigmaNoise.at(i)),
183  std::numeric_limits<float>::max_digits10,
184  cells.at(l).delta.at(i),
185  std::numeric_limits<float>::max_digits10,
186  cells.at(l).rho.at(i),
187  std::numeric_limits<float>::max_digits10,
188  cells.at(l).nearestHigher.at(i),
189  cells.at(l).clusterIndex.at(i),
190  cells.at(l).isSeed.at(i),
191  cells.at(l).detid.at(i))
192  << std::endl;
193  }
194  }
195  outfile.close();
196  }
197  };
198 } // namespace hgcalUtils
199 
200 #endif
unsigned long long EventNumber_t
void dumpInfos(const T &clusters, const std::string &moduleLabel, edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event, bool dumpCellsDetId=false) const
unsigned int LuminosityBlockNumber_t
static bool sortByDetId(const std::pair< int, float > &pair1, const std::pair< int, float > &pair2)
void dumpInfos(const T &cells, const std::string &moduleType) const
void dumpInfos(const T &cells, const std::string &moduleLabel, edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event) const
void dumpInfos(const T &clustersSoA, const std::string &moduleLabel, edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi, edm::EventNumber_t event) const
unsigned int RunNumber_t
long double T
Definition: event.py:1