Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pid_lut: support reading compressed files for hpdirc.lut.gz #1432

Merged
merged 5 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/algorithms/pid_lut/PIDLookup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void PIDLookup::init() {
.azimuthal_bin_centers_in_lut=m_cfg.azimuthal_bin_centers_in_lut,
.momentum_bin_centers_in_lut=m_cfg.momentum_bin_centers_in_lut,
.polar_bin_centers_in_lut=m_cfg.polar_bin_centers_in_lut,
.skip_legacy_header=m_cfg.skip_legacy_header,
.use_radians=m_cfg.use_radians,
.missing_electron_prob=m_cfg.missing_electron_prob,
});
Expand Down
1 change: 0 additions & 1 deletion src/algorithms/pid_lut/PIDLookupConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct PIDLookupConfig {
bool azimuthal_bin_centers_in_lut {false};
bool momentum_bin_centers_in_lut {false};
bool polar_bin_centers_in_lut {false};
bool skip_legacy_header {false};
bool use_radians {false};
bool missing_electron_prob {false};
};
Expand Down
5 changes: 2 additions & 3 deletions src/detectors/DIRC/DIRC.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ extern "C" {
fmt::format("DIRC{}ParticleIDs", qualifier),
},
{
.filename="calibrations/hpdirc_positive.lut",
.filename="calibrations/hpdirc.lut.gz",
.system=BarrelDIRC_ID,
.pdg_values={11, 211, 321, 2212},
.charge_values={1},
.charge_values={-1, 1},
.momentum_edges={0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0, 3.2, 3.4, 3.6, 3.8, 4.0, 4.2, 4.4, 4.6, 4.8, 5.0, 5.2, 5.4, 5.6, 5.8, 6.0, 6.2, 6.4, 6.6, 6.8, 7.0, 7.2, 7.4, 7.6, 7.8, 8.0, 8.2, 8.4, 8.6, 8.8, 9.0, 9.2, 9.4, 9.6, 9.8, 10.0, 10.2},
.polar_edges={25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0, 117.0, 118.0, 119.0, 120.0, 121.0, 122.0, 123.0, 124.0, 125.0, 126.0, 127.0, 128.0, 129.0, 130.0, 131.0, 132.0, 133.0, 134.0, 135.0, 136.0, 137.0, 138.0, 139.0, 140.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 147.0, 148.0, 149.0, 150.0, 151.0, 152.0, 153.0, 154.0, 155.0, 156.0, 157.0, 158.0, 159.0, 160.0},
.azimuthal_binning={0.0, 30.5, 0.5}, // lower, upper, step
.skip_legacy_header=true, // originally used to define binning inside the file
},
app
));
Expand Down
4 changes: 4 additions & 0 deletions src/services/pid_lut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ plugin_add(${PLUGIN_NAME} WITH_SHARED_LIBRARY)
# correctly sets sources for ${_name}_plugin and ${_name}_library targets Adds
# headers to the correct installation directory
plugin_glob_all(${PLUGIN_NAME})

# Find dependencies
find_package(Boost REQUIRED COMPONENTS iostreams)
plugin_link_libraries(${PLUGIN_NAME} Boost::iostreams)
40 changes: 20 additions & 20 deletions src/services/pid_lut/PIDLookupTable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "services/pid_lut/PIDLookupTable.h"

#include <boost/histogram.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/close.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <fmt/core.h>
#include <math.h>
#include <stdlib.h>
Expand Down Expand Up @@ -39,32 +43,27 @@ const PIDLookupTable::Entry* PIDLookupTable::Lookup(int pdg, int charge, double
}

void PIDLookupTable::load_file(const std::string& filename, const PIDLookupTable::Binning &binning) {
std::ifstream file(filename);
bool is_compressed = filename.substr(filename.length() - 3) == ".gz";

std::ios_base::openmode mode = std::ios_base::in;
if (is_compressed) {
mode |= std::ios_base::binary;
}

std::ifstream file(filename, mode);
if (!file) {
throw std::runtime_error("Unable to open LUT file!");
throw std::runtime_error("Unable to open LUT file!");
}
boost::iostreams::filtering_istream in;
if (is_compressed) {
in.push(boost::iostreams::gzip_decompressor());
}
in.push(file);

std::string line;
std::istringstream iss;
double step;

if (binning.skip_legacy_header) {
do { std::getline(file, line); } while (line.empty() || line[0] == '#');
debug("Parsing pdg binning: {}", line);

do { std::getline(file, line); } while (line.empty() || line[0] == '#');
debug("Ignoring charge binning: {}", line);

do { std::getline(file, line); } while (line.empty() || line[0] == '#');
debug("Ignoring momentum binning: {}", line);

do { std::getline(file, line); } while (line.empty() || line[0] == '#');
debug("Ignoring theta binning: {}", line);

do { std::getline(file, line); } while (line.empty() || line[0] == '#');
debug("Ignoring phi binning: ", line);
}

const double angle_fudge = binning.use_radians ? 180. / M_PI : 1.;

bh::axis::category<int> pdg_bins(binning.pdg_values);
Expand All @@ -81,7 +80,7 @@ void PIDLookupTable::load_file(const std::string& filename, const PIDLookupTable

m_symmetrizing_charges = binning.charge_values.size() == 1;

while (std::getline(file, line)) {
while (std::getline(in, line)) {
Entry entry;
if (line.empty() || line[0] == '#' || std::all_of(std::begin(line), std::end(line), [](unsigned char c) { return std::isspace(c); })) continue;

Expand Down Expand Up @@ -139,6 +138,7 @@ void PIDLookupTable::load_file(const std::string& filename, const PIDLookupTable
}
}

boost::iostreams::close(in);
file.close();
}

Expand Down
1 change: 0 additions & 1 deletion src/services/pid_lut/PIDLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class PIDLookupTable : public algorithms::LoggerMixin {
bool azimuthal_bin_centers_in_lut;
bool momentum_bin_centers_in_lut;
bool polar_bin_centers_in_lut;
bool skip_legacy_header;
bool use_radians;
bool missing_electron_prob;
};
Expand Down
Loading