Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-03 04:18:11

0001 #ifndef RecoLocalCalo_HGCalRecProducers_DumpClustersDetails_h
0002 #define RecoLocalCalo_HGCalRecProducers_DumpClustersDetails_h
0003 
0004 #include <string>
0005 #include <vector>
0006 #include <iostream>
0007 #include <fstream>
0008 #include <sstream>
0009 #include <algorithm>
0010 #include <ctime>
0011 #include <cstdlib>
0012 #include <unistd.h>  // For getpid
0013 #include <fmt/format.h>
0014 #include "DataFormats/Provenance/interface/RunLumiEventNumber.h"
0015 
0016 namespace hgcalUtils {
0017 
0018   static bool sortByDetId(const std::pair<int, float>& pair1, const std::pair<int, float>& pair2) {
0019     return pair1.first < pair2.first;
0020   }
0021 
0022   class DumpClusters {
0023   public:
0024     DumpClusters() = default;
0025 
0026     template <typename T>
0027     void dumpInfos(const T& clusters,
0028                    const std::string& moduleLabel,
0029                    edm::RunNumber_t run,
0030                    edm::LuminosityBlockNumber_t lumi,
0031                    edm::EventNumber_t event,
0032                    bool dumpCellsDetId = false) const {
0033       // Get the process ID
0034       pid_t pid = getpid();
0035 
0036       // Create the filename using the PID
0037       std::ostringstream filename;
0038       filename << "CLUSTERS_" << pid << "_" << moduleLabel << "_" << run << "_" << lumi << "_" << event << ".txt";
0039       // Open the file
0040       std::ofstream outfile(filename.str());
0041       int count = 0;
0042       for (auto const& i : clusters) {
0043         outfile << fmt::format(
0044             "Seed: {}, Idx: {}, energy: {:.{}f}, x: {:.{}f}, y: {:.{}f}, z: {:.{}f}, eta: {:.{}f}, phi: {:.{}f}",
0045             i.seed().rawId(),
0046             count++,
0047             (float)i.energy(),
0048             std::numeric_limits<float>::max_digits10,
0049             i.x(),
0050             std::numeric_limits<float>::max_digits10,
0051             i.y(),
0052             std::numeric_limits<float>::max_digits10,
0053             i.z(),
0054             std::numeric_limits<float>::max_digits10,
0055             i.eta(),
0056             std::numeric_limits<float>::max_digits10,
0057             i.phi(),
0058             std::numeric_limits<float>::max_digits10);
0059         if (dumpCellsDetId) {
0060           auto sorted = i.hitsAndFractions();  // copy...
0061           std::stable_sort(std::begin(sorted), std::end(sorted), sortByDetId);
0062           for (auto const& c : sorted) {
0063             outfile << fmt::format(" ({}, {:.{}f})", c.first, c.second, std::numeric_limits<float>::max_digits10);
0064           }  // loop on hits and fractions
0065         } else {
0066           outfile << fmt::format(" ({} cells)", i.hitsAndFractions().size());
0067         }
0068         outfile << std::endl;
0069       }  // loop on clusters
0070       outfile.close();
0071     }
0072   };
0073 
0074   class DumpClustersSoA {
0075   public:
0076     DumpClustersSoA() = default;
0077 
0078     template <typename T>
0079     void dumpInfos(const T& clustersSoA,
0080                    const std::string& moduleLabel,
0081                    edm::RunNumber_t run,
0082                    edm::LuminosityBlockNumber_t lumi,
0083                    edm::EventNumber_t event) const {
0084       // Get the process ID
0085       pid_t pid = getpid();
0086 
0087       // Create the filename using the PID
0088       std::ostringstream filename;
0089       filename << "CLUSTERS_UTILS_SOA_" << pid << "_" << moduleLabel << "_" << run << "_" << lumi << "_" << event
0090                << ".txt";
0091       // Open the file
0092       std::ofstream outfile(filename.str());
0093       for (int i = 0; i < clustersSoA->metadata().size(); ++i) {
0094         auto clusterSoAV = clustersSoA.view()[i];
0095         outfile << fmt::format("Idx: {}, delta: {:.{}f}, rho: {:.{}f}, nearest: {}, clsIdx: {}, isSeed: {}",
0096                                i,
0097                                clusterSoAV.delta(),
0098                                std::numeric_limits<float>::max_digits10,
0099                                clusterSoAV.rho(),
0100                                std::numeric_limits<float>::max_digits10,
0101                                clusterSoAV.nearestHigher(),
0102                                clusterSoAV.clusterIndex(),
0103                                clusterSoAV.isSeed())
0104                 << std::endl;
0105       }
0106       outfile.close();
0107     }
0108   };
0109 
0110   class DumpCellsSoA {
0111   public:
0112     DumpCellsSoA() = default;
0113 
0114     template <typename T>
0115     void dumpInfos(const T& cells,
0116                    const std::string& moduleLabel,
0117                    edm::RunNumber_t run,
0118                    edm::LuminosityBlockNumber_t lumi,
0119                    edm::EventNumber_t event) const {
0120       // Get the process ID
0121       pid_t pid = getpid();
0122 
0123       // Create the filename using the PID
0124       std::ostringstream filename;
0125       filename << "RECHITS_SOA_" << pid << "_" << moduleLabel << "_" << run << "_" << lumi << "_" << event << ".txt";
0126       // Open the file
0127       std::ofstream outfile(filename.str());
0128       for (int i = 0; i < cells->metadata().size(); ++i) {
0129         auto cellSoAV = cells.view()[i];
0130         outfile
0131             << fmt::format(
0132                    "Idx Cell: {}, x: {:.{}f}, y: {:.{}f}, layer: {}, weight: {:.{}f}, sigmaNoise: {:.{}f}, detid: {}",
0133                    i,
0134                    (cellSoAV.dim1()),
0135                    std::numeric_limits<float>::max_digits10,
0136                    (cellSoAV.dim2()),
0137                    std::numeric_limits<float>::max_digits10,
0138                    cellSoAV.layer(),
0139                    (cellSoAV.weight()),
0140                    std::numeric_limits<float>::max_digits10,
0141                    (cellSoAV.sigmaNoise()),
0142                    std::numeric_limits<float>::max_digits10,
0143                    cellSoAV.detid())
0144             << std::endl;
0145       }
0146       outfile.close();
0147     }
0148   };
0149 
0150   class DumpLegacySoA {
0151   public:
0152     DumpLegacySoA() = default;
0153 
0154     template <typename T>
0155     void dumpInfos(const T& cells, const std::string& moduleType) const {
0156       // Get the process ID
0157       pid_t pid = getpid();
0158 
0159       // Create the filename using the PID
0160       std::ostringstream filename;
0161       // Seed the random number generator
0162       srand(time(0));
0163       // Generate a random number between 100 and 999
0164       int random_number = 100 + rand() % 900;
0165 
0166       filename << "RECHITS_LEGACY_" << pid << "_" << moduleType << "_" << random_number << ".txt";
0167       // Open the file
0168       std::ofstream outfile(filename.str());
0169       for (unsigned int l = 0; l < cells.size(); l++) {
0170         for (unsigned int i = 0; i < cells.at(l).dim1.size(); ++i) {
0171           outfile << fmt::format(
0172                          "Idx Cell: {}, x: {:.{}f}, y: {:.{}f}, layer: {}, weight: {:.{}f}, sigmaNoise: {:.{}f}, "
0173                          "delta: {:.{}f}, rho: {:.{}f}, nearestHigher: {}, clsIdx: {}, isSeed: {}, detid: {}",
0174                          i,
0175                          (cells.at(l).dim1.at(i)),
0176                          std::numeric_limits<float>::max_digits10,
0177                          (cells.at(l).dim2.at(i)),
0178                          std::numeric_limits<float>::max_digits10,
0179                          l,
0180                          (cells.at(l).weight.at(i)),
0181                          std::numeric_limits<float>::max_digits10,
0182                          (cells.at(l).sigmaNoise.at(i)),
0183                          std::numeric_limits<float>::max_digits10,
0184                          cells.at(l).delta.at(i),
0185                          std::numeric_limits<float>::max_digits10,
0186                          cells.at(l).rho.at(i),
0187                          std::numeric_limits<float>::max_digits10,
0188                          cells.at(l).nearestHigher.at(i),
0189                          cells.at(l).clusterIndex.at(i),
0190                          cells.at(l).isSeed.at(i),
0191                          cells.at(l).detid.at(i))
0192                   << std::endl;
0193         }
0194       }
0195       outfile.close();
0196     }
0197   };
0198 }  // namespace hgcalUtils
0199 
0200 #endif