/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/ds/include/RAT/DS/ChannelStatus.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/ds/include/RAT/DS/ChannelStatus.hh Source File
Ratpac-two
ChannelStatus.hh
1 
13 #ifndef __RAT_DS_ChannelStatus__
14 #define __RAT_DS_ChannelStatus__
15 
16 #include <TObject.h>
17 
18 #include <RAT/DB.hh>
19 #include <RAT/DS/PMTInfo.hh>
20 #include <RAT/Log.hh>
21 #include <map>
22 #include <string>
23 
24 namespace RAT {
25 namespace DS {
26 class ChannelStatus : public TObject {
27  public:
28  ChannelStatus() : TObject() {}
29  virtual ~ChannelStatus() {}
30 
31  virtual void AddChannel(int lcn, int is_online, double offset, double chargescale, double pulsewidthscale) {
32  lcns.push_back(lcn);
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;
38  }
39 
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)); }
42 
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)); }
45 
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)); }
48 
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)); }
51 
52  virtual void LinkPMT(int pmtid, int lcn) {
53  // create entry with default values if none are specified
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);
56  }
57  pmtid_to_index[pmtid] = lcn_to_index[lcn];
58  }
59 
60  virtual void Load(const PMTInfo* pmtinfo, const std::string index = "") {
61  DBLinkPtr lChannelStatusSelection = DB::Get()->GetLink("channel_status_selection", 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");
70 
71  // Set all channel status entries with default values first.
72  for (int pmtid = 0; pmtid < pmtinfo->GetPMTCount(); pmtid++) {
73  int lcn = pmtinfo->GetChannelNumber(pmtid);
74  LinkPMT(pmtid, lcn);
75  }
76 
77  // override with per-channel values specified in tables.
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);
82  }
83 
84  std::vector<int> get_lcns(DBLinkPtr& lTable) {
85  std::vector<int> lcns;
86  // I'm sorry for the nested try catch blocks
87  try {
88  lcns = lTable->GetIArray("channel_number");
89  } catch (DBNotFoundError& e) {
90  try {
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);
96  }
97  } catch (DBNotFoundError& e) {
98  throw; // upstream should handle this
99  }
100  }
101  return lcns;
102  }
103 
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);
111  }
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;
115  }
116  }
117 
118  DBLinkPtr get_dblink_for_status(const std::string& status_name, DBLinkPtr channel_status_select) {
119  std::string index;
120  try {
121  index = channel_status_select->GetS(status_name);
122  } catch (DBNotFoundError) {
123  index = channel_status_select->GetS("default");
124  }
125  info << "Using " << status_name << "[" << index << "] for channel status" << newline;
126  return DB::Get()->GetLink(status_name, index);
127  }
128 
129  template <typename T>
130  void populate_status(DBLinkPtr lStatus, std::vector<T>* target) {
131  try {
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);
135  } catch (DBNotFoundError& e) {
136  warn << lStatus->GetName() << "[" << lStatus->GetIndex() << "]"
137  << " has no per-channel table. Using the default value. \n";
138  }
139  }
140  ClassDef(ChannelStatus, 3);
141 
142  protected:
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;
155 };
156 
157 } // namespace DS
158 } // namespace RAT
159 
160 #endif
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