|
| 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_except/exception.h" |
| 9 | +#include "cetlib/cpu_timer.h" |
| 10 | +#include "fhiclcpp/ParameterSet.h" |
| 11 | +#include "messagefacility/MessageLogger/MessageLogger.h" |
| 12 | + |
| 13 | +#include "larevt/CalibrationDBI/Providers/DBFolder.h" |
| 14 | + |
| 15 | +// Tool include |
| 16 | +#include "larreco/Calorimetry/INormalizeCharge.h" |
| 17 | + |
| 18 | +// Services |
| 19 | +#include "lardata/DetectorInfoServices/DetectorClocksService.h" |
| 20 | + |
| 21 | +// Lab helpers |
| 22 | +#include "wda.h" |
| 23 | + |
| 24 | +// C++ |
| 25 | +#include <string> |
| 26 | +#include <optional> |
| 27 | +#include <cassert> |
| 28 | + |
| 29 | +namespace sbnd { |
| 30 | + namespace calo { |
| 31 | + |
| 32 | +class NormalizeDriftSQLite : public INormalizeCharge |
| 33 | +{ |
| 34 | +public: |
| 35 | + NormalizeDriftSQLite(fhicl::ParameterSet const &pset); |
| 36 | + |
| 37 | + void configure(const fhicl::ParameterSet& pset) override; |
| 38 | + void setup(const art::Event& e) override; |
| 39 | + 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; |
| 40 | + |
| 41 | +private: |
| 42 | + // Configuration |
| 43 | + std::string fDBFileName; |
| 44 | + std::string fDBTag; |
| 45 | + bool fVerbose; |
| 46 | + |
| 47 | + lariov::DBFolder fDB; |
| 48 | + |
| 49 | + std::optional<detinfo::DetectorClocksData> fClockData; // need delayed construction |
| 50 | + |
| 51 | + // Class to hold data from DB |
| 52 | + class RunInfo { |
| 53 | + public: |
| 54 | + double tau_E; |
| 55 | + double tau_W; |
| 56 | + }; |
| 57 | + |
| 58 | + // Helpers |
| 59 | + RunInfo GetRunInfo(uint64_t run); |
| 60 | + |
| 61 | + // Cache run requests |
| 62 | + std::map<uint32_t, RunInfo> fRunInfos; |
| 63 | +}; |
| 64 | + |
| 65 | +DEFINE_ART_CLASS_TOOL(NormalizeDriftSQLite) |
| 66 | + |
| 67 | + } // end namespace calo |
| 68 | +} // end namespace sbnd |
| 69 | + |
| 70 | + |
| 71 | +sbnd::calo::NormalizeDriftSQLite::NormalizeDriftSQLite(fhicl::ParameterSet const &pset): |
| 72 | + fDBFileName(pset.get<std::string>("DBFileName")), |
| 73 | + fDBTag(pset.get<std::string>("DBTag")), |
| 74 | + fVerbose(pset.get<bool>("Verbose", false)), |
| 75 | + fDB(fDBFileName, "", "", fDBTag, true, false) |
| 76 | +{} |
| 77 | + |
| 78 | +void sbnd::calo::NormalizeDriftSQLite::configure(const fhicl::ParameterSet& pset) {} |
| 79 | + |
| 80 | +void sbnd::calo::NormalizeDriftSQLite::setup(const art::Event& e) { |
| 81 | + fClockData.emplace(art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e)); |
| 82 | +} |
| 83 | + |
| 84 | +sbnd::calo::NormalizeDriftSQLite::RunInfo sbnd::calo::NormalizeDriftSQLite::GetRunInfo(uint64_t run) { |
| 85 | + // check the cache |
| 86 | + if (fRunInfos.count(run)) { |
| 87 | + return fRunInfos.at(run); |
| 88 | + } |
| 89 | + |
| 90 | + // Look up the run |
| 91 | + // |
| 92 | + // Translate the run into a fake "timestamp" |
| 93 | + fDB.UpdateData((run+1000000000)*1000000000); |
| 94 | + |
| 95 | + RunInfo thisrun; |
| 96 | + |
| 97 | + double this_tau_E, this_tau_W; |
| 98 | + fDB.GetNamedChannelData(0, "etau_sce_spatial_east", this_tau_E); |
| 99 | + fDB.GetNamedChannelData(0, "etau_sce_spatial_west", this_tau_W); |
| 100 | + thisrun.tau_E = this_tau_E; |
| 101 | + thisrun.tau_W = this_tau_W; |
| 102 | + |
| 103 | + if (fVerbose) std::cout << "NormalizeDriftSQLite Tool -- Lifetime Data:" << "\nTPC East: " << thisrun.tau_E << "\nTPC West: " << thisrun.tau_W << std::endl; |
| 104 | + |
| 105 | + // Set the cache |
| 106 | + fRunInfos[run] = thisrun; |
| 107 | + |
| 108 | + return thisrun; |
| 109 | +} |
| 110 | + |
| 111 | +double sbnd::calo::NormalizeDriftSQLite::Normalize(double dQdx, const art::Event &e, |
| 112 | + const recob::Hit &hit, const geo::Point_t &location, const geo::Vector_t &direction, double t0) { |
| 113 | + |
| 114 | + if (!fClockData) { |
| 115 | + std::cout << "Error: fClockData is not valid" << std::endl; |
| 116 | + throw cet::exception("fClockData is not valid"); |
| 117 | + } |
| 118 | + |
| 119 | + // Get the info |
| 120 | + RunInfo runelifetime = GetRunInfo(e.id().runID().run()); |
| 121 | + |
| 122 | + // lookup the TPC |
| 123 | + double thiselifetime = -1; |
| 124 | + unsigned tpc = hit.WireID().TPC; |
| 125 | + unsigned cryo = hit.WireID().Cryostat; |
| 126 | + |
| 127 | + // East |
| 128 | + if (cryo == 0 && tpc == 0) thiselifetime = runelifetime.tau_E; |
| 129 | + |
| 130 | + // West |
| 131 | + if (cryo == 0 && tpc == 1) thiselifetime = runelifetime.tau_W; |
| 132 | + |
| 133 | + // Get the hit time |
| 134 | + double thit = fClockData->TPCTick2TrigTime(hit.PeakTime()) - t0; |
| 135 | + thit = thit * 1.e-3; |
| 136 | + |
| 137 | + if (fVerbose) std::cout << "NormalizeDriftSQLite Tool -- Norm factor: " << exp(thit / thiselifetime) << " at TPC: " << tpc << " Cryo: " << cryo << " Time: " << thit << " Track T0: " << t0 << ", x: " << location.X() << std::endl; |
| 138 | + |
| 139 | + // Scale |
| 140 | + if (thiselifetime > 0) { |
| 141 | + dQdx = dQdx*exp(thit / thiselifetime); |
| 142 | + } |
| 143 | + // Throw exception if thiselifetime is not updated to non-zero value |
| 144 | + else { |
| 145 | + std::cout << "sbnd::calo::NormalizeDriftSQLite::Normalize electron lifetime is not found for run " << e.id().runID().run() << std::endl; |
| 146 | + throw cet::exception("Electron lifetime is not found"); |
| 147 | + } |
| 148 | + |
| 149 | + return dQdx; |
| 150 | +} |
| 151 | + |
0 commit comments