All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NormalizeYZ_tool.cc
Go to the documentation of this file.
1 // Framework Includes
2 #include "art/Framework/Core/EDProducer.h"
3 #include "art/Framework/Principal/Event.h"
4 #include "art/Framework/Principal/Handle.h"
5 #include "art/Framework/Services/Registry/ServiceHandle.h"
6 #include "art/Persistency/Common/PtrMaker.h"
7 #include "art/Utilities/ToolMacros.h"
8 #include "cetlib/cpu_timer.h"
9 #include "fhiclcpp/ParameterSet.h"
10 #include "messagefacility/MessageLogger/MessageLogger.h"
11 
12 // Tool include
14 
15 // Services
17 
18 // Lab helpers
19 #include "wda.h"
20 
21 // C++
22 #include <string>
23 
24 namespace icarus {
25  namespace calo {
26 
28 {
29 public:
30  NormalizeYZ(fhicl::ParameterSet const &pset);
31 
32  void configure(const fhicl::ParameterSet& pset) override;
33  double Normalize(double dQdx, const art::Event &e, const recob::Hit &h, const geo::Point_t &location, const geo::Vector_t &direction, double t0) override;
34 
35 private:
36  // Configuration
37  int fTimeout;
38  std::string fURL;
39  bool fVerbose;
40 
41  // Class to hold data from DB
42  class ScaleInfo {
43  public:
44  class ScaleBin {
45  public:
46  int itpc;
47  double ylo;
48  double yhi;
49  double zlo;
50  double zhi;
51 
52  double scale;
53  };
54 
55  float tzero; // Earliest time that this scale info is valid
56  std::vector<ScaleBin> bins;
57  };
58 
59  // Helpers
60  const ScaleInfo& GetScaleInfo(uint64_t timestamp);
61  std::string URL(uint64_t timestamp);
62 
63  // Cache timestamp requests
64  std::map<uint64_t, ScaleInfo> fScaleInfos;
65 };
66 
67 DEFINE_ART_CLASS_TOOL(NormalizeYZ)
68 
69  } // end namespace calo
70 } // end namespace icarus
71 
72 
73 icarus::calo::NormalizeYZ::NormalizeYZ(fhicl::ParameterSet const &pset) {
74  this->configure(pset);
75 }
76 
77 void icarus::calo::NormalizeYZ::configure(const fhicl::ParameterSet& pset) {
78  fURL = pset.get<std::string>("URL");
79  fTimeout = pset.get<unsigned>("Timeout");
80  fVerbose = pset.get<bool>("Verbose", false);
81 }
82 
83 std::string icarus::calo::NormalizeYZ::URL(uint64_t timestamp) {
84  return fURL + std::to_string(timestamp);
85 }
86 
88  // check the cache
89  if (fScaleInfos.count(timestamp)) {
90  return fScaleInfos.at(timestamp);
91  }
92 
93  // Otherwise, look it up
94  int error = 0;
95  std::string url = URL(timestamp);
96 
97  if (fVerbose) std::cout << "NormalizeYZ Tool -- New Scale info, requesting data from url:\n" << url << std::endl;
98 
99  Dataset d = getDataWithTimeout(url.c_str(), "", fTimeout, &error);
100  if (error) {
101  throw cet::exception("NormalizeYZ") << "Calibration Database access failed. URL: (" << url << ") Error Code: " << error;
102  }
103 
104  if (fVerbose) std::cout << "NormalizeYZ Tool -- Received HTTP response:\n" << getHTTPmessage(d) << std::endl;
105 
106  if (getHTTPstatus(d) != 200) {
107  throw cet::exception("NormalizeYZ")
108  << "Calibration Database access failed. URL: (" << url
109  << "). HTTP error status: " << getHTTPstatus(d) << ". HTTP error message: " << getHTTPmessage(d);
110  }
111 
112  // Collect the timestamp info
113  ScaleInfo thisscale;
114 
115  // Get the First row to get tzero
116  error = 0;
117  Tuple tup = getTuple(d, 0);
118  float tzero = getDoubleValue(tup, 0, &error);
119  if (error) {
120  throw cet::exception("NormalizeYZ")
121  << "Calibration Database access failed. URL: (" << url
122  << "). Failed on tuple access, row 0, col 0. Error Code: " << error;
123  }
124 
125  if (fVerbose) std::cout << "NormalizeYZ Tool -- Obtained T0: " << tzero << std::endl;
126 
127  // Check if we've seen this t0 before
128  bool found_scale_t0 = false;
129  for (auto const &scale_pair: fScaleInfos) {
130  const ScaleInfo &scale = scale_pair.second;
131  if (scale.tzero == tzero) {
132  thisscale = scale;
133  found_scale_t0 = true;
134 
135  if (fVerbose) std::cout << "NormalizeYZ Tool -- Found prior matching T0 from timestamp: " << scale_pair.first << std::endl;
136 
137  break;
138  }
139  }
140 
141  if (found_scale_t0) {
142  fScaleInfos[timestamp] = thisscale;
143  return fScaleInfos.at(timestamp);
144  }
145 
146  // We haven't seen this timestamp before and we haven't seen the valid t0 before.
147  //
148  // Process the HTTP response
149  thisscale.tzero = tzero;
150 
151  // Number of rows
152  int n_tuple = getNtuples(d);
153  if (n_tuple < 0) {
154  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Bad Tuple Number: " << n_tuple;
155  }
156 
157  // Iterate over the rows
158  // The first 4 are metadata
159  for (unsigned row = 4; row < (unsigned)n_tuple; row++) {
160  Tuple tup = getTuple(d, row);
161 
162  int err = 0;
163  // Get the TPC value
164  char tpcbuf[10];
165  int strl = getStringValue(tup, 1, tpcbuf, 10, &err);
166  (void) strl;
167  if (err) {
168  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 1. Error Code: " << err;
169  }
170  int itpc = -1;
171  std::string tpcname(tpcbuf);
172  if (tpcname == "EE") itpc = 0;
173  else if (tpcname == "EW") itpc = 1;
174  else if (tpcname == "WE") itpc = 2;
175  else if (tpcname == "WW") itpc = 3;
176  else {
177  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Bad TPC name (" << tpcname << ").";
178  }
179 
180  // Get the bin limits
181  double ylo = getDoubleValue(tup, 8, &err);
182  if (err) {
183  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 8. Error Code: " << err;
184  }
185  double yhi = getDoubleValue(tup, 9, &err);
186  if (err) {
187  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 9. Error Code: " << err;
188  }
189  double zlo = getDoubleValue(tup, 10, &err);
190  if (err) {
191  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 10. Error Code: " << err;
192  }
193  double zhi = getDoubleValue(tup, 11, &err);
194  if (err) {
195  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 11. Error Code: " << err;
196  }
197 
198  // Get the scale
199  double scale = getDoubleValue(tup, 4, &err);
200  if (err) {
201  throw cet::exception("NormalizeYZ") << "NormalizeYZ Tool -- Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 4. Error Code: " << err;
202  }
203 
205  bin.ylo = ylo;
206  bin.yhi = yhi;
207  bin.zlo = zlo;
208  bin.zhi = zhi;
209  bin.itpc = itpc;
210  bin.scale = scale;
211 
212  thisscale.bins.push_back(bin);
213  }
214 
215  // Set the cache
216  fScaleInfos[timestamp] = thisscale;
217  return fScaleInfos.at(timestamp);
218 }
219 
220 double icarus::calo::NormalizeYZ::Normalize(double dQdx, const art::Event &e,
221  const recob::Hit &hit, const geo::Point_t &location, const geo::Vector_t &direction, double t0) {
222  // Get the info
223  ScaleInfo i = GetScaleInfo(e.time().timeHigh());
224 
225  double scale = 1;
226  bool found_bin = false;;
227 
228  // compute itpc
229  int cryo = hit.WireID().Cryostat;
230  int tpc = hit.WireID().TPC;
231  int itpc = cryo*2 + tpc/2;
232  // position
233  double y = location.y();
234  double z = location.z();
235 
236  for (const ScaleInfo::ScaleBin &b: i.bins) {
237  if (itpc == b.itpc &&
238  (y >= b.ylo) && (y < b.yhi) &&
239  (z >= b.zlo) && (z < b.zhi)) {
240  found_bin = true;
241  scale = b.scale;
242  break;
243  }
244  }
245  // TODO: what to do if no lifetime is found? throw an exception??
246  (void) found_bin;
247 
248  if (fVerbose) std::cout << "NormalizeYZ Tool -- Data Cryo: " << cryo << " TPC: " << tpc << " iTPC: " << itpc << " Y: " << y << " Z: " << z << " scale: " << scale << std::endl;
249 
250  return dQdx / scale;
251 }
252 
double Normalize(double dQdx, const art::Event &e, const recob::Hit &h, const geo::Point_t &location, const geo::Vector_t &direction, double t0) override
process_name opflash particleana ie ie ie z
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:164
geo::WireID WireID() const
Definition: Hit.h:233
EResult err(const char *call)
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:212
process_name hit
Definition: cheaterreco.fcl:51
void * Tuple
Definition: DBFolder.h:13
INormalizeCharge interface class definiton.
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
while getopts h
process_name can override from command line with o or output calo
Definition: pid.fcl:40
std::map< uint64_t, ScaleInfo > fScaleInfos
process_name opflash particleana ie ie y
NormalizeYZ(fhicl::ParameterSet const &pset)
const ScaleInfo & GetScaleInfo(uint64_t timestamp)
j template void())
Definition: json.hpp:3108
void * Dataset
Definition: DBFolder.h:12
std::string to_string(WindowPattern const &pattern)
do i e
std::string URL(uint64_t timestamp)
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
void configure(const fhicl::ParameterSet &pset) override
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:406
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
BEGIN_PROLOG could also be cout
This is an interface for an art Tool which scales charge by some factor given information about its a...