All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NormalizeDrift_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  NormalizeDrift(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 RunInfo {
43  public:
44  double tau_EE;
45  double tau_EW;
46  double tau_WE;
47  double tau_WW;
48  };
49 
50  // Helpers
51  RunInfo GetRunInfo(uint32_t run);
52  std::string URL(uint32_t run);
53 
54  // Cache run requests
55  std::map<uint32_t, RunInfo> fRunInfos;
56 };
57 
58 DEFINE_ART_CLASS_TOOL(NormalizeDrift)
59 
60  } // end namespace calo
61 } // end namespace icarus
62 
63 
64 icarus::calo::NormalizeDrift::NormalizeDrift(fhicl::ParameterSet const &pset) {
65  this->configure(pset);
66 }
67 
68 void icarus::calo::NormalizeDrift::configure(const fhicl::ParameterSet& pset) {
69  fURL = pset.get<std::string>("URL");
70  fTimeout = pset.get<unsigned>("Timeout");
71  fVerbose = pset.get<bool>("Verbose", false);
72 }
73 
74 std::string icarus::calo::NormalizeDrift::URL(uint32_t run) {
75  return fURL + std::to_string(run);
76 }
77 
79  // check the cache
80  if (fRunInfos.count(run)) {
81  return fRunInfos.at(run);
82  }
83 
84  // Otherwise, look it up
85  int error = 0;
86  std::string url = URL(run);
87 
88  if (fVerbose) std::cout << "NormalizeDrift Tool -- New Run info, requesting data from url:\n" << url << std::endl;
89 
90  Dataset d = getDataWithTimeout(url.c_str(), "", fTimeout, &error);
91 
92  if (error) {
93  throw cet::exception("NormalizeDrift") << "Calibration Database access failed. URL: (" << url << ") Error Code: " << error;
94  }
95 
96  if (fVerbose) std::cout << "NormalizeDrift Tool -- Received HTTP response:\n" << getHTTPmessage(d) << std::endl;
97 
98  if (getHTTPstatus(d) != 200) {
99  throw cet::exception("NormalizeDrift")
100  << "Calibration Database access failed. URL: (" << url
101  << "). HTTP error status: " << getHTTPstatus(d) << ". HTTP error message: " << getHTTPmessage(d);
102  }
103 
104 
105  // Check all the TPC's are set
106  std::vector<bool> tpc_set(4, false);
107  RunInfo thisrun;
108 
109  // Iterate over the rows
110  // Should be 4: one for each TPC
111  // The first 4 rows are metadata
112  for (unsigned row = 4; row < 8; row++) {
113  Tuple tup = getTuple(d, row);
114 
115  int err = 0;
116  // Get the channel number
117  int ch = getLongValue(tup, 0, &err);
118  if (error) {
119  throw cet::exception("NormalizeDrift") << "Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 0. Error Code: " << error;
120  }
121 
122  // .. and the purity
123  double tau = getDoubleValue(tup, 1, &err);
124  if (error) {
125  throw cet::exception("NormalizeDrift") << "Calibration Database access failed. URL: (" << url << ") Failed on tuple access, row: " << row << ", col 1. Error Code: " << error;
126  }
127 
128  // Check the channel number
129  if (ch < 0 || ch > 3) {
130  throw cet::exception("NormalizeDrift") << "Calibration Database access failed. URL: (" << url << ") Bad channel number: " << ch;
131  }
132 
133  tpc_set.at(ch) = true;
134 
135  // Map channel to TPC
136  if (ch == 0) thisrun.tau_EE = tau;
137  if (ch == 1) thisrun.tau_EW = tau;
138  if (ch == 2) thisrun.tau_WE = tau;
139  if (ch == 3) thisrun.tau_WW = tau;
140  }
141 
142  if (fVerbose) std::cout << "NormalizeDrift Tool -- Lifetime Data:" << "\nTPC EE: " << thisrun.tau_EE << "\nTPC EW: " << thisrun.tau_EW << "\nTPC WE: " << thisrun.tau_WE << "\nTPC WW: " << thisrun.tau_WW << std::endl;
143 
144  // Make sure all the channels are set
145  for (unsigned tpc = 0; tpc < 4; tpc++) {
146  if (!tpc_set[tpc]) {
147  throw cet::exception("NormalizeDrift") << "Calibration Database access failed. URL: (" << url << ") TPC not set: " << tpc;
148  }
149  }
150 
151  // Set the cache
152  fRunInfos[run] = thisrun;
153 
154  return thisrun;
155 }
156 
157 double icarus::calo::NormalizeDrift::Normalize(double dQdx, const art::Event &e,
158  const recob::Hit &hit, const geo::Point_t &location, const geo::Vector_t &direction, double t0) {
159  // Services
160  auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e);
161 
162  // Get the info
163  RunInfo runelifetime = GetRunInfo(e.id().runID().run());
164 
165  // lookup the TPC
166  double thiselifetime = -1;
167  unsigned tpc = hit.WireID().TPC;
168  unsigned cryo = hit.WireID().Cryostat;
169 
170  // EE
171  if (cryo == 0 && (tpc == 0 || tpc == 1)) thiselifetime = runelifetime.tau_EE;
172  // EW
173  if (cryo == 0 && (tpc == 2 || tpc == 3)) thiselifetime = runelifetime.tau_EW;
174  // WE
175  if (cryo == 1 && (tpc == 0 || tpc == 1)) thiselifetime = runelifetime.tau_WE;
176  // WW
177  if (cryo == 1 && (tpc == 2 || tpc == 3)) thiselifetime = runelifetime.tau_WW;
178 
179  // Get the hit time
180  double thit = clock_data.TPCTick2TrigTime(hit.PeakTime()) - t0;
181 
182  if (fVerbose) std::cout << "NormalizeDrift Tool -- Norm factor: " << exp(thit / thiselifetime) << " at TPC: " << tpc << " Cryo: " << cryo << " Time: " << thit << " Track T0: " << t0 << std::endl;
183 
184  // Scale
185  if (thiselifetime > 0) {
186  dQdx = dQdx*exp(thit / thiselifetime);
187  }
188  // TODO: what to do if no lifetime is found? throw an exception??
189  else {}
190 
191  return dQdx;
192 }
193 
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)
RunInfo GetRunInfo(uint32_t run)
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.
while getopts h
process_name can override from command line with o or output calo
Definition: pid.fcl:40
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
std::string URL(uint32_t run)
std::map< uint32_t, RunInfo > fRunInfos
void * Dataset
Definition: DBFolder.h:12
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:218
std::string to_string(WindowPattern const &pattern)
void configure(const fhicl::ParameterSet &pset) override
do i e
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:406
NormalizeDrift(fhicl::ParameterSet const &pset)
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...