All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NormalizeWire_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  NormalizeWire(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  std::map<unsigned, double> scale;
45  };
46 
47  // Helpers
48  ScaleInfo GetScaleInfo(uint64_t timestamp);
49  std::string URL(uint64_t timestamp);
50 
51  // Cache timestamp requests
52  std::map<uint64_t, ScaleInfo> fScaleInfos;
53 };
54 
55 DEFINE_ART_CLASS_TOOL(NormalizeWire)
56 
57  } // end namespace calo
58 } // end namespace icarus
59 
60 
61 icarus::calo::NormalizeWire::NormalizeWire(fhicl::ParameterSet const &pset) {
62  this->configure(pset);
63 }
64 
65 void icarus::calo::NormalizeWire::configure(const fhicl::ParameterSet& pset) {
66  fURL = pset.get<std::string>("URL");
67  fTimeout = pset.get<unsigned>("Timeout");
68  fVerbose = pset.get<bool>("Verbose", false);
69 }
70 
71 std::string icarus::calo::NormalizeWire::URL(uint64_t timestamp) {
72  return fURL + std::to_string(timestamp);
73 }
74 
76  // check the cache
77  if (fScaleInfos.count(timestamp)) {
78  return fScaleInfos.at(timestamp);
79  }
80 
81  // Otherwise, look it up
82  int error = 0;
83  std::string url = URL(timestamp);
84 
85  if (fVerbose) std::cout << "NormalizeWire Tool -- New Scale info, requesting data from url:\n" << url << std::endl;
86 
87  Dataset d = getDataWithTimeout(url.c_str(), "", fTimeout, &error);
88  if (error) {
89  throw cet::exception("NormalizeWire") << "Calibration Database access failed. URL: (" << url << ") Error Code: " << error;
90  }
91 
92  if (fVerbose) std::cout << "NormalizeWire Tool -- Received HTTP response:\n" << getHTTPmessage(d) << std::endl;
93 
94  if (getHTTPstatus(d) != 200) {
95  throw cet::exception("NormalizeWire")
96  << "Calibration Database access failed. URL: (" << url
97  << "). HTTP error status: " << getHTTPstatus(d) << ". HTTP error message: " << getHTTPmessage(d);
98  }
99 
100  // Collect the timestamp info
101  ScaleInfo thisscale;
102 
103  // Number of rows
104  int n_tuple = getNtuples(d);
105  if (n_tuple < 0) {
106  throw cet::exception("NormalizeWire") << "Calibration Database access failed. URL: (" << url << ") Bad Tuple Number: " << n_tuple;
107  }
108 
109  // Iterate over the rows
110  // The first 4 are metadata
111  for (unsigned row = 4; row < (unsigned)n_tuple; row++) {
112  Tuple tup = getTuple(d, row);
113 
114  int err = 0;
115  // Get the channel number
116  int ch = getLongValue(tup, 0, &err);
117  if (error) {
118  throw cet::exception("NormalizeWire") << "Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 0. Error Code: " << error;
119  }
120 
121  // and the scale
122  double scale = getDoubleValue(tup, 1, &err);
123  if (error) {
124  throw cet::exception("NormalizeWire") << "Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 1. Error Code: " << error;
125  }
126 
127  thisscale.scale[ch] = scale;
128  }
129 
130  // Set the cache
131  fScaleInfos[timestamp] = thisscale;
132 
133  return thisscale;
134 }
135 
136 double icarus::calo::NormalizeWire::Normalize(double dQdx, const art::Event &e,
137  const recob::Hit &hit, const geo::Point_t &location, const geo::Vector_t &direction, double t0) {
138  // Get the info
139  ScaleInfo i = GetScaleInfo(e.time().timeHigh());
140 
141  // Lookup the channel
142  unsigned channel = hit.Channel();
143 
144  double scale = 1;
145 
146  // TODO: what to do if no lifetime is found? throw an exception??
147  if (i.scale.count(channel)) scale = i.scale.at(channel);
148 
149  if (fVerbose) std::cout << "NormalizeWire Tool -- Data at channel: " << channel << " scale: " << scale << std::endl;
150 
151  return dQdx / scale;
152 }
153 
ScaleInfo GetScaleInfo(uint64_t timestamp)
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
std::map< uint64_t, ScaleInfo > fScaleInfos
EResult err(const char *call)
process_name hit
Definition: cheaterreco.fcl:51
NormalizeWire(fhicl::ParameterSet const &pset)
void * Tuple
Definition: DBFolder.h:13
INormalizeCharge interface class definiton.
while getopts h
process_name can override from command line with o or output calo
Definition: pid.fcl:40
std::map< unsigned, double > scale
std::string URL(uint64_t timestamp)
void configure(const fhicl::ParameterSet &pset) override
void * Dataset
Definition: DBFolder.h:12
std::string to_string(WindowPattern const &pattern)
do i e
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
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
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
Definition: Hit.h:230
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...
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