13 #ifndef __RAT_DS_ChannelStatus__
14 #define __RAT_DS_ChannelStatus__
19 #include <RAT/DS/PMTInfo.hh>
31 virtual void AddChannel(
int lcn,
int is_online,
double offset,
double chargescale,
double pulsewidthscale) {
33 online.push_back(is_online);
34 cable_offset.push_back(offset);
35 charge_scale.push_back(chargescale);
36 pulse_width_scale.push_back(pulsewidthscale);
37 lcn_to_index[lcn] = lcns.size() - 1;
40 virtual bool GetOnlineByChannel(
int lcn)
const {
return online.at(lcn_to_index.at(lcn)); }
41 virtual bool GetOnlineByPMTID(
int pmtid)
const {
return online.at(pmtid_to_index.at(pmtid)); }
43 virtual double GetCableOffsetByChannel(
int lcn)
const {
return cable_offset.at(lcn_to_index.at(lcn)); }
44 virtual double GetCableOffsetByPMTID(
int pmtid)
const {
return cable_offset.at(pmtid_to_index.at(pmtid)); }
46 virtual double GetChargeScaleByChannel(
int lcn)
const {
return charge_scale.at(lcn_to_index.at(lcn)); }
47 virtual double GetChargeScaleByPMTID(
int pmtid)
const {
return charge_scale.at(pmtid_to_index.at(pmtid)); }
49 virtual double GetPulseWidthScaleByChannel(
int lcn)
const {
return pulse_width_scale.at(lcn_to_index.at(lcn)); }
50 virtual double GetPulseWidthScaleByPMTID(
int pmtid)
const {
return pulse_width_scale.at(pmtid_to_index.at(pmtid)); }
52 virtual void LinkPMT(
int pmtid,
int lcn) {
54 if (lcn_to_index.find(lcn) == lcn_to_index.end()) {
55 AddChannel(lcn, default_is_online, default_offset, default_charge_scale, default_pulse_width_scale);
57 pmtid_to_index[pmtid] = lcn_to_index[lcn];
60 virtual void Load(
const PMTInfo* pmtinfo,
const std::string index =
"") {
62 DBLinkPtr lCableOffset = get_dblink_for_status(
"cable_offset", lChannelStatusSelection);
63 default_offset = lCableOffset->GetD(
"default_value");
64 DBLinkPtr lChannelOnline = get_dblink_for_status(
"channel_online", lChannelStatusSelection);
65 default_is_online = lChannelOnline->GetI(
"default_value");
66 DBLinkPtr lChargeScale = get_dblink_for_status(
"charge_scale", lChannelStatusSelection);
67 default_charge_scale = lChargeScale->GetD(
"default_value");
68 DBLinkPtr lPulseWidthScale = get_dblink_for_status(
"pulse_width_scale", lChannelStatusSelection);
69 default_pulse_width_scale = lPulseWidthScale->GetD(
"default_value");
72 for (
int pmtid = 0; pmtid < pmtinfo->GetPMTCount(); pmtid++) {
73 int lcn = pmtinfo->GetChannelNumber(pmtid);
78 populate_status<double>(lCableOffset, &cable_offset);
79 populate_status<double>(lChargeScale, &charge_scale);
80 populate_status<double>(lPulseWidthScale, &pulse_width_scale);
81 populate_status<int>(lChannelOnline, &online);
84 std::vector<int> get_lcns(
DBLinkPtr& lTable) {
85 std::vector<int> lcns;
88 lcns = lTable->GetIArray(
"channel_number");
91 std::vector<int> lcn_range = lTable->GetIArray(
"channel_number_range");
92 Log::Assert(lcn_range.size() == 2,
"Expect a length-2 array for channel_number_range");
93 Log::Assert(lcn_range.at(0) <= lcn_range.at(1),
"begin element must be smaller or equal to end");
94 for (
int current_lcn = lcn_range.at(0); current_lcn <= lcn_range.at(1); current_lcn++) {
95 lcns.push_back(current_lcn);
104 template <
typename T>
105 void insert_values(std::vector<int> lcns, std::vector<T> values, std::vector<T>* target) {
106 Log::Assert(lcns.size() == values.size(),
"LCN and value arrays are of different lengths!");
107 for (
size_t i = 0; i < lcns.size(); i++) {
108 int current_lcn = lcns.at(i);
109 if (lcn_to_index.find(current_lcn) == lcn_to_index.end()) {
110 LinkPMT(-current_lcn, current_lcn);
112 double current_value = values.at(i);
113 size_t insertion_index = lcn_to_index.at(current_lcn);
114 target->at(insertion_index) = current_value;
118 DBLinkPtr get_dblink_for_status(
const std::string& status_name,
DBLinkPtr channel_status_select) {
121 index = channel_status_select->GetS(status_name);
123 index = channel_status_select->GetS(
"default");
125 info <<
"Using " << status_name <<
"[" << index <<
"] for channel status" << newline;
129 template <
typename T>
130 void populate_status(
DBLinkPtr lStatus, std::vector<T>* target) {
132 std::vector<int> lcns = get_lcns(lStatus);
133 std::vector<T> values = lStatus->Get<std::vector<T>>(
"value");
134 insert_values(lcns, values, target);
136 warn << lStatus->GetName() <<
"[" << lStatus->GetIndex() <<
"]"
137 <<
" has no per-channel table. Using the default value. \n";
143 std::map<int, size_t> lcn_to_index;
144 std::map<int, size_t> pmtid_to_index;
145 std::vector<int> lcns;
146 std::vector<int> online;
147 std::vector<double> cable_offset;
148 std::vector<double> charge_scale;
149 std::vector<double> pulse_width_scale;
150 double default_offset;
151 int default_is_online;
152 std::string ChargeScaleIndex;
153 double default_charge_scale;
154 double default_pulse_width_scale;
Definition: DBTable.hh:275
DBLinkPtr GetLink(std::string tblname, std::string index="")
Definition: DB.cc:200
static DB * Get()
Definition: DB.hh:135
Definition: ChannelStatus.hh:26
Definition: PMTInfo.hh:19
static void Assert(bool condition, std::string message, int return_code=1)
Definition: Log.cc:95
Definition: CCCrossSecMessenger.hh:29