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