13 #include "messagefacility/MessageLogger/MessageLogger.h"
30 , fG4ModuleLabel(config.G4ModuleLabel())
31 , fSimChannelModuleLabel(config.SimChannelModuleLabel())
32 , fHitLabel(config.DefaultHitModuleLabel())
33 , fMinHitEnergyFraction(config.MinHitEnergyFraction())
34 , fOverrideRealData(config.OverrideRealData())
35 , fHitTimeRMS(config.HitTimeRMS())
44 , fG4ModuleLabel(pSet.
get<art::InputTag>(
"G4ModuleLabel",
"largeant"))
45 , fSimChannelModuleLabel(pSet.
get<art::InputTag>(
"SimChannelModuleLabel", fG4ModuleLabel))
47 fHitLabel(pSet.
get<art::InputTag>(
"DefaultHitModuleLabel",
"hitfd"))
48 , fMinHitEnergyFraction(pSet.
get<double>(
"MinHitEnergyFraction", 0.010))
49 , fOverrideRealData(pSet.
get<
bool>(
"OverrideRealData",
false))
50 , fHitTimeRMS(pSet.
get<double>(
"HitTimeRMS", 1.0))
62 std::vector<const sim::IDE*>
65 std::vector<const sim::IDE*> ideps;
69 for (
auto mapitr = tdcidemap.begin(); mapitr != tdcidemap.end(); mapitr++) {
71 const std::vector<sim::IDE>& idevec =
74 for (
size_t iv = 0; iv < idevec.size(); ++iv) {
78 if (
abs(idevec[iv].trackID) == id) ideps.push_back(&(idevec[iv]));
86 std::vector<const sim::IDE*>
89 std::vector<const sim::IDE*> ide_Ps;
90 for (
const art::Ptr<sim::SimChannel> sc :
fSimChannels) {
91 if (
fGeom->
View(sc->Channel()) != view)
continue;
94 for (
const auto& item : sc->TDCIDEMap()) {
97 for (
const sim::IDE& ide : item.second) {
98 if (
abs(ide.
trackID) == id) ide_Ps.push_back(&ide);
107 art::Ptr<sim::SimChannel>
114 return (a->Channel() < channel);
116 return ((ilb !=
fSimChannels.end()) && ((*ilb)->Channel() == channel))
117 ? *ilb: art::Ptr<sim::SimChannel>{};
121 art::Ptr<sim::SimChannel>
125 throw cet::exception(
"BackTracker") <<
"No sim::SimChannel corresponding "
126 <<
"to channel: " << channel <<
"\n";
130 std::vector<sim::TrackIDE>
133 const double hit_start_time,
134 const double hit_end_time)
const
137 if (!schannel)
return {};
139 std::vector<sim::TrackIDE> trackIDEs;
144 int start_tdc = clockData.
TPCTick2TDC(hit_start_time);
146 if (start_tdc < 0) start_tdc = 0;
147 if (end_tdc < 0) end_tdc = 0;
148 std::vector<sim::IDE> simides = schannel->TrackIDsAndEnergies(start_tdc, end_tdc);
152 for (
size_t e = 0;
e < simides.size(); ++
e)
153 totalE += simides[
e].energy;
156 if (totalE < 1.
e-5) totalE = 1.;
160 for (
size_t e = 0;
e < simides.size(); ++
e) {
167 info.
energy = simides[
e].energy;
170 trackIDEs.push_back(info);
177 std::vector<sim::TrackIDE>
191 std::vector<int> retVec;
192 for (
auto const trackIDE : this->
HitToTrackIDEs(clockData, hit)) {
193 retVec.push_back(trackIDE.trackID);
200 std::vector<sim::TrackIDE>
204 std::vector<sim::TrackIDE> eveIDEs;
205 std::vector<sim::TrackIDE> trackIDEs = this->
HitToTrackIDEs(clockData, hit);
206 std::map<int, std::pair<double, double>> eveToEMap;
208 for (
const auto& trackIDE : trackIDEs) {
210 std::make_pair(trackIDE.energy, trackIDE.numElectrons));
211 if (
check.second ==
false) {
212 check.first->second.first += trackIDE.energy;
213 check.first->second.second += trackIDE.numElectrons;
217 totalE += trackIDE.energy;
219 eveIDEs.reserve(eveToEMap.size());
220 for (
const auto& eveToE : eveToEMap) {
223 eveTrackIDE_tmp.
trackID = eveToE.first;
224 eveTrackIDE_tmp.
energy = eveToE.second.first;
228 eveIDEs.push_back(eveTrackIDE_tmp);
234 std::vector<art::Ptr<recob::Hit>>
237 std::vector<art::Ptr<recob::Hit>>
const& hitsIn)
const
244 std::vector<art::Ptr<recob::Hit>> hitList;
245 std::vector<sim::TrackIDE> trackIDE;
246 for (
auto itr = hitsIn.begin(); itr != hitsIn.end(); ++itr) {
248 art::Ptr<recob::Hit>
const&
hit = *itr;
251 for (
auto itr_trakIDE = trackIDE.begin(); itr_trakIDE != trackIDE.end(); ++itr_trakIDE) {
253 hitList.push_back(hit);
261 std::vector<std::vector<art::Ptr<recob::Hit>>>
263 std::vector<int>
const& tkIds,
264 std::vector<art::Ptr<recob::Hit>>
const& hitsIn)
const
271 std::vector<std::pair<int, art::Ptr<recob::Hit>>> hitList;
272 std::vector<sim::TrackIDE> tids;
273 for (
auto itr = hitsIn.begin(); itr != hitsIn.end(); ++itr) {
275 art::Ptr<recob::Hit>
const&
hit = *itr;
278 for (
auto itid = tids.begin(); itid != tids.end(); ++itid) {
279 for (
auto itkid = tkIds.begin(); itkid != tkIds.end(); ++itkid) {
280 if (itid->trackID == *itkid) {
282 hitList.push_back(std::make_pair(*itkid, hit));
289 std::vector<std::vector<art::Ptr<recob::Hit>>> truHits;
291 std::vector<art::Ptr<recob::Hit>> tmpHits;
292 for (
auto itkid = tkIds.begin(); itkid != tkIds.end(); ++itkid) {
294 for (
auto itr = hitList.begin(); itr != hitList.end(); ++itr) {
295 if (*itkid == (*itr).first) tmpHits.push_back((*itr).second);
297 truHits.push_back(tmpHits);
304 std::vector<sim::IDE>
312 if (start_tdc < 0) start_tdc = 0;
313 if (end_tdc < 0) end_tdc = 0;
319 std::vector<const sim::IDE*>
323 std::vector<const sim::IDE*> retVec;
326 if (start_tdc < 0) start_tdc = 0;
327 if (end_tdc < 0) end_tdc = 0;
329 if (start_tdc > end_tdc) {
throw; }
331 const std::vector<std::pair<unsigned short, std::vector<sim::IDE>>>& tdcIDEMap =
335 std::vector<const std::pair<unsigned short, std::vector<sim::IDE>>*> tdcIDEMap_SortedPointers;
336 for (
auto& pair : tdcIDEMap) {
337 tdcIDEMap_SortedPointers.push_back(&pair);
346 auto pairSort = [](
auto&
a,
auto& b) {
return a->first < b->first; };
348 tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), pairSort)) {
349 std::sort(tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), pairSort);
352 std::vector<sim::IDE> dummyVec;
354 std::pair<double, std::vector<sim::IDE>> start_tdcPair =
355 std::make_pair(start_tdc, dummyVec);
357 std::pair<double, std::vector<sim::IDE>> end_tdcPair = std::make_pair(end_tdc, dummyVec);
358 auto start_tdcPair_P = &start_tdcPair;
359 auto end_tdcPair_P = &end_tdcPair;
360 auto mapFirst = std::lower_bound(
361 tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), start_tdcPair_P, pairSort);
364 auto mapLast = std::upper_bound(
365 tdcIDEMap_SortedPointers.begin(), tdcIDEMap_SortedPointers.end(), end_tdcPair_P, pairSort);
366 for (
auto& mapitr = mapFirst; mapitr != mapLast; ++mapitr) {
367 for (
auto& ide : (*mapitr)->second) {
368 retVec.push_back(&ide);
378 std::vector<double> xyz(3, 0.0);
380 for (
auto const& ide : ides) {
381 double weight = ide.numElectrons;
383 xyz[0] += (weight * ide.x);
384 xyz[1] += (weight * ide.y);
385 xyz[2] += (weight * ide.z);
388 throw cet::exception(
"BackTracker") <<
"No sim::IDEs providing non-zero number of electrons"
389 <<
" can't determine originating location from truth\n";
400 std::vector<sim::IDE> ides;
401 for (
auto ide_P : ide_Ps) {
402 ides.push_back(*ide_P);
411 std::vector<const sim::IDE*> ide_Ps = this->
HitToSimIDEs_Ps(clockData, hit);
418 std::set<int>
const& trackIds,
419 std::vector<art::Ptr<recob::Hit>>
const& hits)
const
422 for (
const auto&
hit : hits) {
424 for (
const auto& tIDE : hitTrackIDEs) {
425 if (trackIds.find(tIDE.trackID) != trackIds.end()) {
431 if (hits.size() > 0) {
return double(
double(desired) /
double(hits.size())); }
438 std::set<int>
const& trackIds,
439 std::vector<art::Ptr<recob::Hit>>
const& hits)
const
441 double totalCharge = 0., desired = 0.;
442 for (
const auto&
hit : hits) {
443 totalCharge +=
hit->Integral();
445 for (
const auto& trackIDE : trackIDEs) {
446 if (trackIds.find(trackIDE.trackID) != trackIds.end()) {
447 desired +=
hit->Integral();
452 if (totalCharge > 0.0) {
return (desired / totalCharge); }
459 std::set<int>
const& trackIds,
465 int desired = 0, total = 0;
467 for (
const auto&
hit : hits) {
469 for (
const auto& trackIDE : hitTrackIDEs) {
470 if (trackIds.find(trackIDE.trackID) != trackIds.end() &&
478 for (
const auto&
hit : allHits) {
483 for (
const auto& hitIDE : hitTrackIDEs) {
484 if (trackIds.find(hitIDE.trackID) != trackIds.end() &&
491 if (total >= 0) {
return double(
double(desired) /
double(total)); }
498 std::set<int>
const& trackIds,
503 double desired = 0., total = 0.;
504 for (
const auto&
hit : hits) {
506 for (
const auto& hitIDE : hitTrackIDEs) {
507 if (trackIds.find(hitIDE.trackID) != trackIds.end() &&
509 desired +=
hit->Integral();
515 for (
const auto&
hit : allHits) {
516 if (
hit->View() != view && view !=
geo::k3D) {
continue; }
518 for (
const auto& hitIDE : hitTrackIDEs) {
519 if (trackIds.find(hitIDE.trackID) != trackIds.end() &&
521 total +=
hit->Integral();
527 if (total > 0.) {
return desired / total; }
534 std::vector<art::Ptr<recob::Hit>>
const& hits)
const
537 for (
const auto&
hit : hits) {
540 std::vector<sim::TrackIDE> trackIDEs =
542 for (
const auto& ide : trackIDEs) {
543 tids.insert(ide.trackID);
552 std::vector<art::Ptr<recob::Hit>>
const& hits)
const
554 std::set<int> eveIds;
555 for (
const auto&
hit : hits) {
557 for (
const auto& ide : ides) {
558 eveIds.insert(ide.trackID);
567 std::vector<art::Ptr<recob::Hit>>
const& hits)
const
569 std::vector<double> xyz(3, -99999.9);
570 std::vector<std::vector<std::vector<int>>> numHits(
fGeom->
Ncryostats());
571 std::vector<std::vector<std::vector<double>>> hitWeight(
fGeom->
Ncryostats());
572 std::vector<std::vector<std::vector<std::vector<double>>>> hitPos(
fGeom->
Ncryostats());
574 for (
size_t c = 0; c < numHits.size(); ++c) {
578 for (
size_t t = 0; t < numHits[c].size(); ++t) {
585 for (art::PtrVector<recob::Hit>::const_iterator ihit = hits.begin(); ihit != hits.end();
592 std::vector<double> hitOrigin = this->
HitToXYZ(clockData, *ihit);
593 unsigned int cstat = 0;
594 unsigned int tpc = 0;
595 const double worldLoc[3] = {hitOrigin[0], hitOrigin[1], hitOrigin[2]};
601 hitPos[cstat][tpc][hit.
WireID().
Plane] = hitOrigin;
611 for (
size_t c = 0; c < numHits.size(); ++c) {
612 for (
size_t t = 0; t < numHits[c].size(); ++t) {
613 for (
size_t p = 0;
p < numHits[c][t].size(); ++
p) {
615 if (numHits[c][t][
p] == 1) {
617 xyz[0] += hitPos[c][t][
p][0];
618 xyz[1] += hitPos[c][t][
p][1];
619 xyz[2] += hitPos[c][t][
p][2];
628 throw cet::exception(
"BackTracker")
629 <<
"No hits to determine originating location from truth\n";
TrackID_t trackID
Geant4 supplied track ID.
const geo::GeometryCore * fGeom
std::vector< art::Ptr< sim::SimChannel > > fSimChannels
std::vector< std::vector< art::Ptr< recob::Hit > > > TrackIdsToHits_Ps(detinfo::DetectorClocksData const &clockData, std::vector< int > const &tkIds, std::vector< art::Ptr< recob::Hit >> const &hitsIn) const
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
art::Ptr< sim::SimChannel > FindSimChannelPtr(raw::ChannelID_t channel) const
Returns the cached sim::SimChannel on the specified channel.
std::vector< sim::TrackIDE > HitToEveTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
geo::WireID WireID() const
BackTracker(const fhiclConfig &config, const cheat::ParticleInventory *partInv, const geo::GeometryCore *geom)
float numElectrons
number of electrons from the particle detected on the wires
const double fMinHitEnergyFraction
CryostatID_t Cryostat
Index of cryostat.
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
float energy
energy from the particle with this trackID [MeV]
unsigned int Ncryostats() const
Returns the number of cryostats in the detector.
pure virtual base interface for detector clocks
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
geo::TPCGeo const & PositionToTPC(geo::Point_t const &point) const
Returns the TPC at specified location.
3-dimensional objects, potentially hits, clusters, prongs, etc.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Access the description of detector geometry.
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
std::vector< double > HitToXYZ(detinfo::DetectorClocksData const &clockData, const recob::Hit &hit) const
Ionization at a point of the TPC sensitive volume.
static const int NoParticleId
art::Ptr< sim::SimChannel > FindSimChannel(raw::ChannelID_t channel) const
Returns the cached sim::SimChannel on the specified channel.
std::vector< art::Ptr< recob::Hit > > TrackIdToHits_Ps(detinfo::DetectorClocksData const &clockData, int tkId, std::vector< art::Ptr< recob::Hit >> const &hitsIn) const
std::set< int > GetSetOfTrackIds() const
double HitCollectionPurity(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits) const
auto end(FixedBins< T, C > const &) noexcept
float energyFrac
fraction of hit energy from the particle with this trackID
const cheat::ParticleInventory * fPartInv
process_name tightIsolTest check
PlaneID_t Plane
Index of the plane within its TPC.
Description of geometry of one entire detector.
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
Definition of data types for geometry description.
float PeakTimeMinusRMS(float sigmas=+1.) const
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
std::vector< sim::TrackIDE > ChannelToTrackIDEs(detinfo::DetectorClocksData const &clockData, raw::ChannelID_t channel, const double hit_start_time, const double hit_end_time) const
std::set< int > GetSetOfEveIds() const
double HitChargeCollectionPurity(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits) const
int trackID
Geant4 supplied trackID.
Contains all timing reference information for the detector.
std::vector< int > HitToTrackIds(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
double HitCollectionEfficiency(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits, std::vector< art::Ptr< recob::Hit >> const &allhits, geo::View_t const &view) const
std::vector< const sim::IDE * > TrackIdToSimIDEs_Ps(int const &id) const
2D representation of charge deposited in the TDC/wire plane
float PeakTimePlusRMS(float sigmas=+1.) const
Returns a time sigmas RMS away from the peak time.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
TPCID_t TPC
Index of the TPC within its cryostat.
std::vector< sim::IDE > HitToAvgSimIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
int TrackIdToEveTrackId(const int &tid) const
Ionization energy from a Geant4 track.
raw::ChannelID_t Channel() const
ID of the readout channel the hit was extracted from.
std::vector< double > SimIDEsToXYZ(std::vector< sim::IDE > const &ides) const
double HitChargeCollectionEfficiency(detinfo::DetectorClocksData const &clockData, std::set< int > const &trackIds, std::vector< art::Ptr< recob::Hit >> const &hits, std::vector< art::Ptr< recob::Hit >> const &allhits, geo::View_t const &view) const
double TPCTick2TDC(double const tick) const
Tools and modules for checking out the basics of the Monte Carlo.
std::vector< const sim::IDE * > HitToSimIDEs_Ps(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
std::vector< double > SpacePointHitsToWeightedXYZ(detinfo::DetectorClocksData const &clockData, std::vector< art::Ptr< recob::Hit >> const &hits) const