|
| 1 | +// Copyright 2019-2026 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +#include "SimConfig/G4ScoringMerger.h" |
| 13 | +#include <fairlogger/Logger.h> |
| 14 | +#include <filesystem> |
| 15 | +#include <fstream> |
| 16 | +#include <iomanip> |
| 17 | +#include <map> |
| 18 | +#include <regex> |
| 19 | +#include <sstream> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace o2::conf |
| 23 | +{ |
| 24 | + |
| 25 | +namespace |
| 26 | +{ |
| 27 | +// One scorer block of a Geant4 mesh dump: its header lines and the summed rows |
| 28 | +struct ScorerBlock { |
| 29 | + std::vector<std::string> header; |
| 30 | + std::vector<std::string> keys; // "iZ,iPHI,iR" in file order |
| 31 | + std::vector<double> sum; |
| 32 | + std::vector<double> sum2; |
| 33 | + std::vector<long> entries; |
| 34 | +}; |
| 35 | + |
| 36 | +// Read one mesh dump into scorer blocks; returns false on a format error |
| 37 | +bool readDump(const std::string& fileName, std::vector<std::string>& meshHeader, std::vector<ScorerBlock>& blocks) |
| 38 | +{ |
| 39 | + std::ifstream in(fileName); |
| 40 | + if (!in) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + std::string line; |
| 44 | + ScorerBlock* current = nullptr; |
| 45 | + while (std::getline(in, line)) { |
| 46 | + if (line.rfind("# mesh name", 0) == 0) { |
| 47 | + meshHeader.push_back(line); |
| 48 | + } else if (line.rfind("# primitive scorer name", 0) == 0) { |
| 49 | + blocks.emplace_back(); |
| 50 | + current = &blocks.back(); |
| 51 | + current->header.push_back(line); |
| 52 | + } else if (line.rfind("#", 0) == 0) { |
| 53 | + if (!current) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + current->header.push_back(line); |
| 57 | + } else if (!line.empty()) { |
| 58 | + if (!current) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + // iZ, iPHI, iR, total, total^2, entries |
| 62 | + std::vector<std::string> fields; |
| 63 | + std::stringstream ss(line); |
| 64 | + std::string field; |
| 65 | + while (std::getline(ss, field, ',')) { |
| 66 | + fields.push_back(field); |
| 67 | + } |
| 68 | + if (fields.size() != 6) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + current->keys.push_back(fields[0] + "," + fields[1] + "," + fields[2]); |
| 72 | + current->sum.push_back(std::stod(fields[3])); |
| 73 | + current->sum2.push_back(std::stod(fields[4])); |
| 74 | + current->entries.push_back(std::stol(fields[5])); |
| 75 | + } |
| 76 | + } |
| 77 | + return !blocks.empty(); |
| 78 | +} |
| 79 | +} // namespace |
| 80 | + |
| 81 | +std::string g4ScoringWorkerFileName(const std::string& meshName, int pid) |
| 82 | +{ |
| 83 | + return meshName + ".worker" + std::to_string(pid) + ".txt"; |
| 84 | +} |
| 85 | + |
| 86 | +int mergeG4ScoringDumps(const std::string& directory) |
| 87 | +{ |
| 88 | + namespace fs = std::filesystem; |
| 89 | + const std::regex pattern(R"((.+)\.worker([0-9]+)\.txt)"); |
| 90 | + std::map<std::string, std::vector<fs::path>> filesPerMesh; |
| 91 | + for (auto& entry : fs::directory_iterator(directory)) { |
| 92 | + std::smatch match; |
| 93 | + const auto name = entry.path().filename().string(); |
| 94 | + if (entry.is_regular_file() && std::regex_match(name, match, pattern)) { |
| 95 | + filesPerMesh[match[1]].push_back(entry.path()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + int merged = 0; |
| 100 | + for (auto& [mesh, files] : filesPerMesh) { |
| 101 | + std::vector<std::string> meshHeader; |
| 102 | + std::vector<ScorerBlock> total; |
| 103 | + for (auto& file : files) { |
| 104 | + std::vector<std::string> header; |
| 105 | + std::vector<ScorerBlock> blocks; |
| 106 | + if (!readDump(file.string(), header, blocks)) { |
| 107 | + LOG(error) << "Cannot read Geant4 scoring dump " << file; |
| 108 | + return -1; |
| 109 | + } |
| 110 | + if (total.empty()) { |
| 111 | + meshHeader = header; |
| 112 | + total = std::move(blocks); |
| 113 | + continue; |
| 114 | + } |
| 115 | + if (blocks.size() != total.size()) { |
| 116 | + LOG(error) << "Geant4 scoring dump " << file << " has a different set of scorers"; |
| 117 | + return -1; |
| 118 | + } |
| 119 | + for (size_t b = 0; b < blocks.size(); ++b) { |
| 120 | + if (blocks[b].header != total[b].header || blocks[b].keys != total[b].keys) { |
| 121 | + LOG(error) << "Geant4 scoring dump " << file << " does not match the mesh layout of the other workers"; |
| 122 | + return -1; |
| 123 | + } |
| 124 | + for (size_t i = 0; i < blocks[b].keys.size(); ++i) { |
| 125 | + total[b].sum[i] += blocks[b].sum[i]; |
| 126 | + total[b].sum2[i] += blocks[b].sum2[i]; |
| 127 | + total[b].entries[i] += blocks[b].entries[i]; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + const auto outName = (fs::path(directory) / (mesh + ".txt")).string(); |
| 133 | + std::ofstream out(outName); |
| 134 | + out << std::setprecision(16); |
| 135 | + for (auto& line : meshHeader) { |
| 136 | + out << line << "\n"; |
| 137 | + } |
| 138 | + for (auto& block : total) { |
| 139 | + for (auto& line : block.header) { |
| 140 | + out << line << "\n"; |
| 141 | + } |
| 142 | + for (size_t i = 0; i < block.keys.size(); ++i) { |
| 143 | + out << block.keys[i] << "," << block.sum[i] << "," << block.sum2[i] << "," << block.entries[i] << "\n"; |
| 144 | + } |
| 145 | + } |
| 146 | + LOG(info) << "Merged " << files.size() << " Geant4 scoring dumps into " << outName; |
| 147 | + ++merged; |
| 148 | + } |
| 149 | + return merged; |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace o2::conf |
0 commit comments