TripletFinder.cxx
Go to the documentation of this file.
2 
4 
5 #include "TVector3.h"
6 
10 
11 namespace reco3d {
12  // -------------------------------------------------------------------------
14  const std::vector<art::Ptr<recob::Hit>>& xhits,
15  const std::vector<art::Ptr<recob::Hit>>& uhits,
16  const std::vector<art::Ptr<recob::Hit>>& vhits,
17  const std::vector<raw::ChannelID_t>& xbad,
18  const std::vector<raw::ChannelID_t>& ubad,
19  const std::vector<raw::ChannelID_t>& vbad,
20  double distThresh,
21  double distThreshDrift,
22  double xhitOffset)
23  : geom(art::ServiceHandle<geo::Geometry const>()->provider())
24  , fDistThresh(distThresh)
25  , fDistThreshDrift(distThreshDrift)
26  , fXHitOffset(xhitOffset)
27  {
28  FillHitMap(detProp, xhits, fX_by_tpc);
29  FillHitMap(detProp, uhits, fU_by_tpc);
30  FillHitMap(detProp, vhits, fV_by_tpc);
31 
32  FillBadMap(xbad, fXbad_by_tpc);
33  FillBadMap(ubad, fUbad_by_tpc);
34  FillBadMap(vbad, fVbad_by_tpc);
35  }
36 
37  // -------------------------------------------------------------------------
38  void
40  const std::vector<art::Ptr<recob::Hit>>& hits,
41  std::map<geo::TPCID, std::vector<HitOrChan>>& out)
42  {
43  for (const art::Ptr<recob::Hit>& hit : hits) {
44  for (geo::TPCID tpc : geom->ROPtoTPCs(geom->ChannelToROP(hit->Channel()))) {
45  double xpos = 0;
46  for (geo::WireID wire : geom->ChannelToWire(hit->Channel())) {
47  if (geo::TPCID(wire) == tpc) {
48  xpos = detProp.ConvertTicksToX(hit->PeakTime(), wire);
49  if (geom->SignalType(wire) == geo::kCollection) xpos += fXHitOffset;
50  }
51  }
52 
53  out[tpc].emplace_back(hit.get(), xpos);
54  }
55  }
56  for (auto& it : out)
57  std::sort(it.second.begin(), it.second.end(), [](auto a, auto b) { return a.xpos < b.xpos; });
58  }
59 
60  // -------------------------------------------------------------------------
61  void
62  TripletFinder::FillBadMap(const std::vector<raw::ChannelID_t>& bads,
63  std::map<geo::TPCID, std::vector<raw::ChannelID_t>>& out)
64  {
65  for (raw::ChannelID_t chan : bads) {
66  for (geo::TPCID tpc : geom->ROPtoTPCs(geom->ChannelToROP(chan))) {
67  out[tpc].push_back(chan);
68  }
69  }
70  }
71 
72  // -------------------------------------------------------------------------
74  public:
76  : geom(art::ServiceHandle<geo::Geometry const>()->provider()), fTPC(tpc)
77  {}
78 
79  bool
81  {
82  const auto key = std::make_pair(a, b);
83 
84  auto it = fMap.find(key);
85  if (it != fMap.end()) {
86  pt = fPtMap[key];
87  return it->second;
88  }
89 
90  const bool res = ISect(a, b, pt);
91  fMap.insert({key, res});
92  fPtMap.insert({key, pt});
93  return res;
94  }
95 
96  protected:
97  bool
99  {
100  for (geo::WireID awire : geom->ChannelToWire(chanA)) {
101  if (geo::TPCID(awire) != fTPC) continue;
102  for (geo::WireID bwire : geom->ChannelToWire(chanB)) {
103  if (geo::TPCID(bwire) != fTPC) continue;
104 
105  if (geom->WireIDsIntersect(awire, bwire, pt)) return true;
106  }
107  }
108 
109  return false;
110  }
111 
113 
114  std::map<std::pair<raw::ChannelID_t, raw::ChannelID_t>, bool> fMap;
115  std::map<std::pair<raw::ChannelID_t, raw::ChannelID_t>, geo::WireIDIntersection> fPtMap;
116 
118  };
119 
120  // -------------------------------------------------------------------------
121  bool
122  TripletFinder::CloseDrift(double xa, double xb) const
123  {
124  return fabs(xa - xb) < fDistThreshDrift;
125  }
126 
127  // -------------------------------------------------------------------------
128  bool
130  {
131  const TVector3 pa(ra.y, ra.z, 0);
132  const TVector3 pb(rb.y, rb.z, 0);
133 
134  return (pa - pb).Mag() < fDistThresh;
135  }
136 
137  bool
139  {
140  // Make sure the bad hits get sorted too
141  if (a.a.hit == 0 && b.a.hit == 0) return a.a.chan < b.a.chan;
142  // But mostly just order the real hits (in some arbitrary order)
143  return a.a.hit < b.a.hit;
144  }
145 
146  bool
148  {
149  if (a.a.hit == 0 && b.a.hit == 0) return a.a.chan == b.a.chan;
150  return a.a.hit == b.a.hit;
151  }
152 
153  // -------------------------------------------------------------------------
154  std::vector<HitTriplet>
156  {
157  std::vector<HitTriplet> ret;
158 
159  for (const auto& it : fX_by_tpc) {
160  const geo::TPCID& tpc = it.first;
161 
162  std::vector<ChannelDoublet> xus = DoubletsXU(tpc);
163  std::vector<ChannelDoublet> xvs = DoubletsXV(tpc);
164 
165  // Cache to prevent repeating the same questions
166  IntersectionCache isectUV(tpc);
167 
168  // For the efficient looping below to work we need to sort the doublet
169  // lists so the X hits occur in the same order.
170  std::sort(xus.begin(), xus.end(), LessThanXHit);
171  std::sort(xvs.begin(), xvs.end(), LessThanXHit);
172 
173  auto xvit_begin = xvs.begin();
174 
175  int nxuv = 0;
176  for (const ChannelDoublet& xu : xus) {
177  const HitOrChan& x = xu.a;
178  const HitOrChan& u = xu.b;
179 
180  // Catch up until we're looking at the same X hit in XV
181  while (xvit_begin != xvs.end() && LessThanXHit(*xvit_begin, xu))
182  ++xvit_begin;
183 
184  // Loop through all those matching hits
185  for (auto xvit = xvit_begin; xvit != xvs.end() && SameXHit(*xvit, xu); ++xvit) {
186  const HitOrChan& v = xvit->b;
187 
188  // Only allow one bad channel per triplet
189  if (!x.hit && !u.hit) continue;
190  if (!x.hit && !v.hit) continue;
191  if (!u.hit && !v.hit) continue;
192 
193  if (u.hit && v.hit && !CloseDrift(u.xpos, v.xpos)) continue;
194 
196  if (!isectUV(u.chan, v.chan, ptUV)) continue;
197 
198  if (!CloseSpace(xu.pt, xvit->pt) || !CloseSpace(xu.pt, ptUV) ||
199  !CloseSpace(xvit->pt, ptUV))
200  continue;
201 
202  double xavg = 0;
203  int nx = 0;
204  if (x.hit) {
205  xavg += x.xpos;
206  ++nx;
207  }
208  if (u.hit) {
209  xavg += u.xpos;
210  ++nx;
211  }
212  if (v.hit) {
213  xavg += v.xpos;
214  ++nx;
215  }
216  xavg /= nx;
217 
218  const XYZ pt{
219  xavg, (xu.pt.y + xvit->pt.y + ptUV.y) / 3, (xu.pt.z + xvit->pt.z + ptUV.z) / 3};
220 
221  ret.emplace_back(HitTriplet{x.hit, u.hit, v.hit, pt});
222  ++nxuv;
223  } // end for xv
224  } // end for xu
225 
226  std::cout << tpc << " " << xus.size() << " XUs and " << xvs.size() << " XVs -> " << nxuv
227  << " XUVs" << std::endl;
228 
229  } // end for tpc
230 
231  std::cout << ret.size() << " XUVs total" << std::endl;
232 
233  return ret;
234  }
235 
236  // -------------------------------------------------------------------------
237  std::vector<HitTriplet>
239  {
240  std::vector<HitTriplet> ret;
241 
242  for (const auto& it : fX_by_tpc) {
243  const geo::TPCID& tpc = it.first;
244 
245  std::vector<ChannelDoublet> xus = DoubletsXU(tpc);
246 
247  for (const ChannelDoublet& xu : xus) {
248  const HitOrChan& x = xu.a;
249  const HitOrChan& u = xu.b;
250 
251  double xavg = x.xpos;
252  int nx = 1;
253  if (u.hit) {
254  xavg += u.xpos;
255  ++nx;
256  }
257  xavg /= nx;
258 
259  const XYZ pt{xavg, xu.pt.y, xu.pt.z};
260 
261  ret.emplace_back(HitTriplet{x.hit, u.hit, 0, pt});
262  } // end for xu
263  } // end for tpc
264 
265  std::cout << ret.size() << " XUs total" << std::endl;
266 
267  return ret;
268  }
269 
270  // -------------------------------------------------------------------------
271  std::vector<ChannelDoublet>
273  {
274  std::vector<ChannelDoublet> ret =
275  DoubletHelper(tpc, fX_by_tpc[tpc], fU_by_tpc[tpc], fUbad_by_tpc[tpc]);
276 
277  // Find X(bad)+U(good) doublets, have to flip them for the final result
278  for (auto it : DoubletHelper(tpc, fU_by_tpc[tpc], {}, fXbad_by_tpc[tpc])) {
279  ret.push_back({it.b, it.a, it.pt});
280  }
281 
282  return ret;
283  }
284 
285  // -------------------------------------------------------------------------
286  std::vector<ChannelDoublet>
288  {
289  std::vector<ChannelDoublet> ret =
290  DoubletHelper(tpc, fX_by_tpc[tpc], fV_by_tpc[tpc], fVbad_by_tpc[tpc]);
291 
292  // Find X(bad)+V(good) doublets, have to flip them for the final result
293  for (auto it : DoubletHelper(tpc, fV_by_tpc[tpc], {}, fXbad_by_tpc[tpc])) {
294  ret.push_back({it.b, it.a, it.pt});
295  }
296 
297  return ret;
298  }
299 
300  // -------------------------------------------------------------------------
301  std::vector<ChannelDoublet>
303  const std::vector<HitOrChan>& ahits,
304  const std::vector<HitOrChan>& bhits,
305  const std::vector<raw::ChannelID_t>& bbads) const
306  {
307  std::vector<ChannelDoublet> ret;
308 
309  IntersectionCache isect(tpc);
310 
311  auto b_begin = bhits.begin();
312 
313  for (const HitOrChan& a : ahits) {
314  // Bad channels are easy because there's no timing constraint
315  for (raw::ChannelID_t b : bbads) {
317  if (isect(a.chan, b, pt)) { ret.emplace_back(a, b, pt); }
318  }
319 
320  while (b_begin != bhits.end() && b_begin->xpos < a.xpos && !CloseDrift(b_begin->xpos, a.xpos))
321  ++b_begin;
322 
323  for (auto bit = b_begin; bit != bhits.end(); ++bit) {
324  const HitOrChan& b = *bit;
325 
326  if (b.xpos > a.xpos && !CloseDrift(b.xpos, a.xpos)) break;
327 
329  if (!isect(a.chan, b.chan, pt)) continue;
330 
331  ret.emplace_back(a, b, pt);
332  } // end for b
333  } // end for a
334 
335  return ret;
336  }
337 }
bool CloseSpace(geo::WireIDIntersection ra, geo::WireIDIntersection rb) const
std::vector< geo::TPCID > ROPtoTPCs(readout::ROPID const &ropid) const
Returns a list of ID of TPCs the specified ROP spans.
raw::ChannelID_t chan
Definition: TripletFinder.h:30
double z
z position of intersection
Definition: geo_types.h:805
std::vector< ChannelDoublet > DoubletHelper(geo::TPCID tpc, const std::vector< HitOrChan > &ahits, const std::vector< HitOrChan > &bhits, const std::vector< raw::ChannelID_t > &bbads) const
IntersectionCache(geo::TPCID tpc)
TH3F * xpos
Definition: doAna.cpp:29
std::map< std::pair< raw::ChannelID_t, raw::ChannelID_t >, geo::WireIDIntersection > fPtMap
std::map< geo::TPCID, std::vector< HitOrChan > > fX_by_tpc
Definition: TripletFinder.h:94
struct vector vector
virtual const provider_type * provider() const override
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
const geo::GeometryCore * geom
SigType_t SignalType(geo::PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
art framework interface to geometry description
std::map< std::pair< raw::ChannelID_t, raw::ChannelID_t >, bool > fMap
std::map< geo::TPCID, std::vector< HitOrChan > > fV_by_tpc
Definition: TripletFinder.h:96
std::map< geo::TPCID, std::vector< raw::ChannelID_t > > fXbad_by_tpc
Definition: TripletFinder.h:99
std::map< geo::TPCID, std::vector< HitOrChan > > fU_by_tpc
Definition: TripletFinder.h:95
def key(type, name=None)
Definition: graph.py:13
const geo::GeometryCore * geom
Definition: TripletFinder.h:69
const double a
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
Description of geometry of one entire detector.
bool SameXHit(const ChannelDoublet &a, const ChannelDoublet &b)
Detector simulation of raw signals on wires.
double ConvertTicksToX(double ticks, int p, int t, int c) const
readout::ROPID ChannelToROP(raw::ChannelID_t channel) const
TripletFinder(const detinfo::DetectorPropertiesData &detProp, const std::vector< art::Ptr< recob::Hit >> &xhits, const std::vector< art::Ptr< recob::Hit >> &uhits, const std::vector< art::Ptr< recob::Hit >> &vhits, const std::vector< raw::ChannelID_t > &xbad, const std::vector< raw::ChannelID_t > &ubad, const std::vector< raw::ChannelID_t > &vbad, double distThresh, double distThreshDrift, double xhitOffset)
bool WireIDsIntersect(WireID const &wid1, WireID const &wid2, geo::Point_t &intersection) const
Computes the intersection between two wires.
std::vector< HitTriplet > TripletsTwoView()
Only search for XU intersections.
const recob::Hit * hit
Definition: TripletFinder.h:31
std::vector< ChannelDoublet > DoubletsXV(geo::TPCID tpc)
std::vector< ChannelDoublet > DoubletsXU(geo::TPCID tpc)
double y
y position of intersection
Definition: geo_types.h:804
bool CloseDrift(double xa, double xb) const
std::vector< HitTriplet > Triplets()
bool LessThanXHit(const ChannelDoublet &a, const ChannelDoublet &b)
static bool * b
Definition: config.cpp:1043
Access the description of detector geometry.
static constexpr double pb
Definition: Units.h:82
list x
Definition: train.py:276
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
std::map< geo::TPCID, std::vector< raw::ChannelID_t > > fUbad_by_tpc
void FillBadMap(const std::vector< raw::ChannelID_t > &bads, std::map< geo::TPCID, std::vector< raw::ChannelID_t >> &out)
Helper for constructor.
bool operator()(raw::ChannelID_t a, raw::ChannelID_t b, geo::WireIDIntersection &pt)
LArSoft geometry interface.
Definition: ChannelGeo.h:16
bool ISect(raw::ChannelID_t chanA, raw::ChannelID_t chanB, geo::WireIDIntersection &pt) const
std::map< geo::TPCID, std::vector< raw::ChannelID_t > > fVbad_by_tpc
QTextStream & endl(QTextStream &s)
void FillHitMap(const detinfo::DetectorPropertiesData &clockData, const std::vector< art::Ptr< recob::Hit >> &hits, std::map< geo::TPCID, std::vector< HitOrChan >> &out)
Helper for constructor.
Signal from collection planes.
Definition: geo_types.h:146