LDMX Software
TrackerPedestalProvider.cxx
1#include <fstream>
2#include <iostream>
3#include <nlohmann/json.hpp>
4
7#include "Framework/Exception/Exception.h"
8#include "Tracking/Reco/SiStripChannelMap.h"
9#include "Tracking/Reco/TrackerPedestals.h"
10
11namespace tracking::reco {
12
13const std::string TrackerPedestals::CONDITIONS_NAME = "TrackerPedestals";
14
24 public:
25 TrackerPedestalProvider(const std::string& name, const std::string& tagname,
26 const framework::config::Parameters& parameters,
29 tagname, parameters, process) {
31 EXCEPTION_RAISE("BadConfig",
32 "The name provided to TrackerPedestalProvider '" + name +
33 "' is not the expected '" +
35 }
36 pedestal_file_ = parameters.get<std::string>("pedestal_file");
37 pedestal_format_ =
38 parameters.get<std::string>("pedestal_format", pedestal_format_);
39 }
40
41 std::pair<const framework::ConditionsObject*, framework::ConditionsIOV>
42 getCondition(const ldmx::EventHeader& /*context*/) override {
43 auto* peds = new TrackerPedestals();
44
45 // Dispatch on the configured storage backend. Only JSON is supported
46 // today; additional backends (e.g. SQLite) can be added as new load*().
47 if (pedestal_format_ == "json") {
48 loadJson(*peds);
49 } else {
50 EXCEPTION_RAISE("BadConfiguration",
51 "TrackerPedestalProvider unknown pedestal_format '" +
52 pedestal_format_ + "' (supported: 'json').");
53 }
54
55 std::cout << "[TrackerPedestalProvider] Loaded " << peds->size()
56 << " channel pedestals from '" << pedestal_file_ << "'"
57 << std::endl;
58
59 // Pedestals are valid for all runs (data and MC) for now.
60 return {peds, framework::ConditionsIOV(true, true)};
61 }
62
63 private:
64 void loadJson(TrackerPedestals& peds) const {
65 std::ifstream in(pedestal_file_);
66 if (!in) {
67 EXCEPTION_RAISE("FileNotFound",
68 "TrackerPedestalProvider could not open pedestal file '" +
69 pedestal_file_ + "'.");
70 }
71
72 nlohmann::json doc;
73 try {
74 in >> doc;
75 } catch (const nlohmann::json::parse_error& e) {
76 EXCEPTION_RAISE(
77 "BadFormat",
78 "TrackerPedestalProvider failed to parse pedestal file '" +
79 pedestal_file_ + "': " + e.what());
80 }
81
82 // Each channel is keyed "feb:hyb:apv:ch" -> {"mean":[...], "noise":[...]}.
83 for (const auto& [key, ch] : doc.at("channels").items()) {
84 uint8_t feb, hybrid, apv, channel;
85 if (!channelmap::parseChannelKey(key, feb, hybrid, apv, channel))
86 continue;
87 TrackerPedestals::Channel ped;
88 ped.mean_ = ch.at("mean").get<decltype(ped.mean_)>();
89 ped.noise_ = ch.at("noise").get<decltype(ped.noise_)>();
90 peds.add(feb, hybrid, apv, channel, ped);
91 }
92 }
93
94 std::string pedestal_file_;
95 std::string pedestal_format_{"json"};
96};
97
98} // namespace tracking::reco
99
Base class for provider of conditions information like pedestals, gains, electronics maps,...
#define DECLARE_CONDITIONS_PROVIDER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Class that provides header information about an event such as event number and timestamp.
Class which defines the run/event/type range for which a given condition is valid,...
Base class for all providers of conditions objects.
const Process & process() const
Get the process handle.
Class which represents the process under execution.
Definition Process.h:37
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
Provides header information an event such as event number and timestamp.
Definition EventHeader.h:44
Conditions provider for the per-channel tracker pedestals/noise.
std::pair< const framework::ConditionsObject *, framework::ConditionsIOV > getCondition(const ldmx::EventHeader &) override
Pure virtual getCondition function.
Per-channel tracker pedestal conditions: the per-sample pedestal mean and RMS noise for every silicon...
void add(uint8_t feb, uint8_t hybrid, uint8_t apv, uint8_t channel, const Channel &ped)
Insert the pedestal for one electronics channel.
static const std::string CONDITIONS_NAME
Name of the conditions object (must match the python registration).
bool parseChannelKey(const std::string &key, uint8_t &feb, uint8_t &hybrid, uint8_t &apv, uint8_t &channel)
Parse a "feb:hybrid:apv:channel" key (as produced by channelKey()) back into its electronics fields.