All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
APAGeometryAlg.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // \fileAPAGeometryAlg.cxx
4 //
5 // tylerdalion@gmail.com
6 //
7 // Geometry interface to smooth over the awkwardness of fitting an
8 // APA into LArSoft. It is geared towards making reconstruction simpler
9 //
10 ////////////////////////////////////////////////////////////////////////
11 
12 
13 //Framework includes:
14 #include "messagefacility/MessageLogger/MessageLogger.h"
15 
21 
22 #include <cmath>
23 #include <algorithm>
24 #include <iostream>
25 #include <cstdlib>
26 
27 namespace apa{
28 
29  APAGeometryAlg::APAGeometryAlg(fhicl::ParameterSet const& pset)
30  {
31  this->reconfigure(pset);
32  this->Init();
33  }
34 
35 
36  //----------------------------------------------------------
38  {
39  this->Init();
40  }
41 
42  //----------------------------------------------------------
43  void APAGeometryAlg::reconfigure(fhicl::ParameterSet const& /*p*/)
44  {
45  }
46 
47  //----------------------------------------------------------
49  {
50 
51  // find the number of channels per APA
52  uint32_t channel = 0;
53 
54  // old logic -- segfaults if there is just one APA
55  //while( fGeom->ChannelToWire(channel+1)[0].TPC < 2 ) channel++;
56  //fChannelsPerAPA = channel + 1;
57 
58  fChannelsPerAPA = fGeom->Nchannels();
59  for (channel=0; channel < fGeom->Nchannels(); ++channel)
60  {
61  if ( fGeom->ChannelToWire(channel)[0].TPC > 1 )
62  {
63  fChannelsPerAPA = channel;
64  break;
65  }
66  }
67 
68 
69  // Step through channel c and find the view boundaries, until
70  // outside of first APA - these help optimize ChannelToAPAView
71  // (very dependent on the conventions implimented in the channel map)
72  fFirstU = 0;
73  uint32_t c = 1;
74  geo::WireID wid = (fGeom->ChannelToWire(c))[0];
75  geo::WireID lastwid;
76  while( wid.TPC < 2 ){
77 
78  if( fGeom->View(c) == geo::kV && fGeom->View(c-1) == geo::kU ){
79  fLastU = c-1;
80  fFirstV = c; }
81 
82  if( fGeom->View(c) == geo::kZ && fGeom->View(c-1) == geo::kV ){
83  fLastV = c-1;
84  fFirstZ0 = c; }
85 
86  if( wid.TPC == lastwid.TPC + 1 ){
87  fLastZ0 = c-1;
88  fFirstZ1 = c; }
89 
90  lastwid = wid;
91  c++;
92  if (c >= fGeom->Nchannels()) break;
93  wid = (fGeom->ChannelToWire(c))[0]; // for the while condition
94 
95  }
96 
97  fLastZ1 = c - 1;
98 
99 
100  if( fLastZ1 + 1 != fChannelsPerAPA ) throw cet::exception("APAGeometryAlg")
101  << "Channel boundaries are inconsistent.\n";
102 
103  // some other things that will be needed
104  fAPAsPerCryo = fGeom->NTPC(0)/2;
105  fChannelRange[0] = (fLastU-fFirstU + 1)*fGeom->WirePitch(geo::kU);
106  fChannelRange[1] = (fLastV-fFirstV + 1)*fGeom->WirePitch(geo::kV);
107 
108  }
109 
110 
111  //----------------------------------------------------------
112  void APAGeometryAlg::ChannelToAPA( uint32_t chan,
113  unsigned int & apa,
114  unsigned int & cryo) const {
115 
116  cryo = chan / (fAPAsPerCryo*fChannelsPerAPA);
117 
118  // Number apa uniquely across cryostats so that
119  // apa to recob::Object maps are easy to work with.
120  // If we decide to reset APA number per cryo, uncomment:
121  //chan -= cryo*fAPAsPerCryo*fChannelsPerAPA;
122  apa = chan / fChannelsPerAPA;
123 
124  return;
125  }
126 
127  //----------------------------------------------------------
128  unsigned int APAGeometryAlg::ChannelToAPA( uint32_t chan ) const {
129 
130  return chan / fChannelsPerAPA;
131  }
132 
133 
134  //----------------------------------------------------------
135  unsigned int APAGeometryAlg::ChannelsInView( geo::View_t geoview ) const {
136 
137  switch(geoview){
138  default :
139  return 0;
140  case geo::kU :
141  return ChannelsInAPAView( kU );
142  case geo::kV :
143  return ChannelsInAPAView( kV );
144  case geo::kZ :
146  return ChannelsInAPAView( kZ0 );
147  else throw cet::exception("ChannelsInView")
148  << "Both Z sides should have the same amount of channels\n";
149  }
150 
151  }
152 
153 
154  //----------------------------------------------------------
155  unsigned int APAGeometryAlg::ChannelsInAPAView( APAView_t apaview ) const {
156 
157  switch(apaview){
158  default :
159  return 0;
160  case kU :
161  return fFirstV-fFirstU;
162  case kV :
163  return fFirstZ0-fFirstV;
164  case kZ0 :
165  return fFirstZ1-fFirstZ0;
166  case kZ1 :
167  return fLastZ1-fFirstZ1 + 1;
168  }
169 
170  }
171 
172 
173  //----------------------------------------------------------
175  unsigned int apa,
176  unsigned int cryo ) const {
177 
178 
179  switch(geoview){
180  default :
181  return 0 + (uint32_t)(apa + cryo*fAPAsPerCryo)*fChannelsPerAPA;
182  case geo::kU :
183  return fFirstU + (uint32_t)(apa + cryo*fAPAsPerCryo)*fChannelsPerAPA;
184  case geo::kV :
185  return fFirstV + (uint32_t)(apa + cryo*fAPAsPerCryo)*fChannelsPerAPA;
186  case geo::kZ :
187  //TODO: would need tpc number for the rest of this
188  return fFirstZ0 + (uint32_t)(apa + cryo*fAPAsPerCryo)*fChannelsPerAPA;
189  }
190 
191  }
192 
193  //----------------------------------------------------------
194  uint32_t APAGeometryAlg::FirstChannelInView( uint32_t chan ) const {
195 
196  geo::View_t geoview = fGeom->View(chan);
197  unsigned int apa, cryo;
198  this->ChannelToAPA( chan, apa, cryo );
199  return this->FirstChannelInView( geoview, chan );
200 
201  }
202 
203  //----------------------------------------------------------
205  uint32_t chan ) const {
206 
207  unsigned int apa, cryo;
208  this->ChannelToAPA( chan, apa, cryo );
209  return this->FirstChannelInView( geoview, apa, cryo );
210 
211  }
212 
213 
214 
215  //----------------------------------------------------------
216  APAView_t APAGeometryAlg::APAView( uint32_t chan ) const{
217 
218  // it seems trivial to do this for U and V, but this gives a side to
219  // geo::kZ, unlike Geometry::View(c), as is often needed in disambiguation
220 
221  geo::View_t view = fGeom->View( chan );
222  switch(view){
223  default :
224  break;
225  case geo::kU :
226  return kU;
227  case geo::kV :
228  return kV;
229  case geo::kZ :
230  unsigned int modchan = chan % fChannelsPerAPA;
231  // Channel mapping number in the order of U, V, Z0, then Z1
232  if( modchan > fLastV && modchan < fFirstZ1 ) return kZ0;
233  if( modchan > fLastZ0 ) return kZ1;
234  }
235 
236  return kUnknown;
237 
238  }
239 
240 
241  //----------------------------------------------------------
242  std::vector<geo::WireID> APAGeometryAlg::ChanSegsPerSide(uint32_t chan, unsigned int side) const {
243 
244  std::vector<geo::WireID> wids = fGeom->ChannelToWire(chan);
245  return this->ChanSegsPerSide(wids, side);
246 
247  }
248 
249 
250 
251  //----------------------------------------------------------
252  std::vector<geo::WireID> APAGeometryAlg::ChanSegsPerSide(std::vector<geo::WireID> wids, unsigned int side) const
253  {
254  // Given a vector of wireIDs and an APA side, return
255  // the wireIDs the the tpc side where tpc%2 = side
256 
257  std::vector<geo::WireID> thisSide;
258 
259  for(size_t i = 0; i < wids.size(); i++)
260  if( wids[i].TPC % 2 == side ) thisSide.push_back(wids[i]);
261 
262  return thisSide;
263  }
264 
265 
266 
267 
268  //----------------------------------------------------------
270  uint32_t chan,
271  unsigned int const plane,
272  unsigned int const tpc,
273  unsigned int const cstat ) const
274  {
275 
276  std::vector<geo::WireID> cWids = fGeom->ChannelToWire( chan );
277 
278  if( cWids[0].Cryostat != cstat )
279  throw cet::exception("APAGeometryAlg") << "Channel " << chan
280  << "not in cryostat " << cstat << "\n";
281  if( std::floor( cWids[0].TPC / 2 ) != std::floor( tpc / 2 ) )
282  throw cet::exception("APAGeometryAlg") << "Channel " << chan
283  << "not in APA " << std::floor(tpc/2) << "\n";
284 
285  // special case for vertical wires
286  if(fGeom->View(chan)==geo::kZ) return fGeom->ChannelToWire(chan)[0];
287 
288  unsigned int xyzWire = fGeom->NearestWireID( WorldLoc, plane, tpc, cstat ).Wire;
289 
290  // The desired wire ID will be the only channel
291  // segment within half the channel range.
292  geo::WireID wid;
293  for(size_t i=0; i<cWids.size(); i++){
294  if( cWids[i].TPC != tpc ) continue;
295  if( std::abs((int)cWids[i].Wire - (int)xyzWire) < fChannelRange[plane]/2 ) wid=cWids[i];
296  }
297 
298  return wid;
299 
300  }
301 
302 
303  //----------------------------------------------------------
304  bool APAGeometryAlg::LineSegChanIntersect( TVector3 xyzStart, TVector3 xyzEnd, uint32_t chan,
305  std::vector<geo::WireID> & widsCrossed,
306  bool ExtendLine = true ) const
307  {
308 
309  // This assumes a smooth wire numbering, and that the line seg is contained in a tpc.
310  // Meant for use with the approximate line calculated
311  // by matching cluster endpoints in disambiguation.
312 
313  // Find tpc, use midpoint in case start/end is on a boundary
314  unsigned int tpc, cryo;
315  double xyzMid[3];
316  xyzMid[0] = (xyzStart[0]+xyzEnd[0])/2;
317  xyzMid[1] = (xyzStart[1]+xyzEnd[1])/2;
318  xyzMid[2] = (xyzStart[2]+xyzEnd[2])/2;
319  fGeom->PositionToTPC(xyzMid, tpc, cryo);
320 
321  // Find the nearest wire number to the line segment endpoints
322  std::vector<geo::WireID> wids = fGeom->ChannelToWire(chan);
323  unsigned int startW = fGeom->NearestWire( xyzStart, wids[0].Plane, tpc, cryo );
324  unsigned int endW = fGeom->NearestWire( xyzEnd, wids[0].Plane, tpc, cryo );
325 
326  if( startW > endW ) std::swap(startW, endW);
327 
328 
329  // Loop through wireIDs and check for intersection, if in the right TPC
330  for( size_t w = 0; w < wids.size(); w++ ){
331  if( wids[w].TPC != tpc ) continue;
332  if( wids[w].Cryostat != cryo ) throw cet::exception("LineSegChanIntersect")
333  << "Channel and line not in the same crostat.\n";
334 
335  // If the current wire id wire number is inbetween the start/end
336  // point wires, the line segment intersects the wireID at some point.
337 
338  // TODO: for now, extend range, but that is application specific. fix asap
339  // The longer we make the range, the more conservative it is, so it is safe
340  // to extend the range a bit to get hits at the ends of the line
341  unsigned int ext = 0;
342  if ( ExtendLine) ext = 10;
343 
344  if( fGeom->ValueInRange( wids[w].Wire*1., (startW-ext)*1., (endW+ext)*1. ) ) widsCrossed.push_back(wids[w]);
345 
346  }
347 
348  if( widsCrossed.size() > 0 ) return true;
349  else return false;
350 
351  }
352 
353 
354 
355  //----------------------------------------------------------
356  std::vector<double> APAGeometryAlg::ThreeChanPos( uint32_t u, uint32_t v, uint32_t z ) const
357  {
358 
359  // Say we've associated a U, V, and Z channel -- perhaps by associating hits
360  // or cluster endpoints -- these don't necessarily all intersect, but they
361  // are hopefully pretty close. Find the center of the 3 intersections.
362 
363  // get data needed along the way
364  std::vector< geo::WireIDIntersection > UVIntersects;
365  this->APAChannelsIntersect( u, v, UVIntersects );
366  std::vector< double > UVzToZ(UVIntersects.size());
367  geo::WireID Zwid = fGeom->ChannelToWire(z)[0];
368  unsigned int cryo = Zwid.Cryostat;
369  unsigned int tpc = Zwid.TPC;
370  std::vector<geo::WireID> Uwids = fGeom->ChannelToWire(u);
371  std::vector<geo::WireID> Vwids = fGeom->ChannelToWire(v);
372  std::vector<geo::WireID> UwidsInTPC, VwidsInTPC;
373  for(size_t i=0; i<Uwids.size(); i++) if( Uwids[i].TPC==tpc ) UwidsInTPC.push_back(Uwids[i]);
374  for(size_t i=0; i<Vwids.size(); i++) if( Vwids[i].TPC==tpc ) VwidsInTPC.push_back(Vwids[i]);
375  double Zcent[3] = {0.};
376  fGeom->WireIDToWireGeo( Zwid ).GetCenter(Zcent);
377 
378  std::cout << "Zcent = " << Zcent[2] << ", UVintersects zpos = ";
379  for(size_t uv=0; uv<UVIntersects.size(); uv++){
380  std::cout << UVIntersects[uv].z << ", ";
381  }
382  std::cout << "\n";
383 
384  /////////////////////////////////////
385  /////////////////////////////////////
386 
387  //std::cout << "U = " << u << " V = " << v << ", " << UVIntersects.size() << std::endl;
388 
389  if( UVIntersects.size() == 0 ){
390  if( UwidsInTPC.size() > 1 || VwidsInTPC.size() > 1 )
391  throw cet::exception("ThreeChanPos") << "U/V channels don't intersect, bad return.\n";
392 
393  // Now assume there are only one of each u and v wireIDs on in this TPC
394  mf::LogWarning("ThreeChanPos") << "No U/V intersect, exceptional channels. See if U or V intersects Z\n";
395  std::vector<double> yzCenter(2,0.);
396  geo::WireID Uwid = UwidsInTPC[0];
397  geo::WireID Vwid = VwidsInTPC[0];
398  geo::WireIDIntersection UZInt, VZInt;
399  bool checkUZ = fGeom->WireIDsIntersect( Uwid, Zwid, UZInt );
400  bool checkVZ = fGeom->WireIDsIntersect( Vwid, Zwid, VZInt );
401  if( !checkUZ && !checkVZ )
402  throw cet::exception("NoChanIntersect") << "No channels intersect, bad return.\n";
403  if( checkUZ && !checkVZ ){ yzCenter[0] = UZInt.y; yzCenter[1] = UZInt.z; }
404  if( checkVZ && !checkUZ ){ yzCenter[0] = VZInt.y; yzCenter[1] = VZInt.z; }
405  if( checkUZ && checkVZ ){ yzCenter[0] = (VZInt.y+UZInt.y)/2; yzCenter[1] = (VZInt.z+UZInt.z)/2; }
406  return yzCenter;
407  }
408 
409  /////////////////////////////////////
410  /////////////////////////////////////
411 
412 
413  // In case the uv channels intersect twice on the same side, choose the best case.
414  // Note: this will not happen for APAs with UV angle at about 36, but will for 45
415  std::cout << "UVzToZ = ";
416  for( size_t widI = 0; widI < UVIntersects.size(); widI++ ){
417  UVzToZ[widI] = std::abs( UVIntersects[widI].z - Zcent[2] );
418  std::cout << UVzToZ[widI] << ", ";
419  }
420  std::cout<<"\n";
421 
422  unsigned int bestWidI = 0;
423  double minZdiff = fGeom->Cryostat(cryo).TPC(tpc).Length(); // start it out at maximum z
424  for( unsigned int widI = 0; widI < UVIntersects.size(); widI++ ){
425 
426  //std::cout << "widI = " << widI << std::endl;
427 
428  if( UVIntersects[widI].TPC == tpc && UVzToZ[widI] < minZdiff ){
429  minZdiff = UVzToZ[widI];
430  bestWidI = widI;
431  //std::cout << "bestWidI = " << bestWidI << std::endl;
432  }
433  }
434  geo::WireIDIntersection ChosenUVInt = UVIntersects[bestWidI];
435 
436  // Now having the UV intersection, get the UZ and VZ
437  double UVInt[3] = {0.};
438  UVInt[1] = ChosenUVInt.y; UVInt[2] = ChosenUVInt.z;
439  geo::WireID Uwid = this->NearestWireIDOnChan( UVInt, u, 0, tpc, cryo );
440  geo::WireID Vwid = this->NearestWireIDOnChan( UVInt, v, 1, tpc, cryo );
441  geo::WireIDIntersection UZInt, VZInt;
442  bool checkUZ = fGeom->WireIDsIntersect( Uwid, Zwid, UZInt );
443  bool checkVZ = fGeom->WireIDsIntersect( Vwid, Zwid, VZInt );
444 
445  std::cout << "UZint.z = " << UZInt.z << " (" << checkUZ << "), VZint.z = " << VZInt.z << " (" << checkVZ << ")\n";
446 
447  // find the center
448  std::vector<double> yzCenter(2,0.);
449 
450 
451  if( !checkUZ || !checkVZ ){
452  //throw cet::exception("ThreeChanPos") << "WireIDs were expected to intersect.\n";
453 
454  std::cout << "ChosenUVint.y = " << ChosenUVInt.y << "ChosenUVint.z = " << ChosenUVInt.z << std::endl;
455 
456  //temporary case
457  yzCenter[0] = ChosenUVInt.y;
458  yzCenter[1] = ChosenUVInt.z;
459 
460 
461  } else {
462 
463  yzCenter[0] = (ChosenUVInt.y + UZInt.y + VZInt.y)/3;
464  yzCenter[1] = (ChosenUVInt.z + UZInt.z + VZInt.z)/3;
465 
466  }
467 
468 
469  return yzCenter;
470 
471  }
472 
473 
474 
475 
476  //----------------------------------------------------------
477  bool APAGeometryAlg::APAChannelsIntersect( uint32_t chan1, uint32_t chan2,
478  std::vector< geo::WireIDIntersection >& IntersectVector ) const
479  {
480 
481 
482  // Get the WireIDs and view for each channel, make sure views are different
483  geo::WireIDIntersection widIntersect;
484  std::vector< geo::WireID > wids1 = fGeom->ChannelToWire( chan1 );
485  std::vector< geo::WireID > wids2 = fGeom->ChannelToWire( chan2 );
486  geo::View_t view1 = fGeom->View( chan1 );
487  geo::View_t view2 = fGeom->View( chan2 );
488  if( view1 == view2 ){
489  mf::LogWarning("APAChannelsIntersect") << "Comparing two channels in the same view, return false";
490  return false; }
491  if( wids1[0].Cryostat != wids2[0].Cryostat ||
492  this->ChannelToAPA(chan1) != this->ChannelToAPA(chan2) ){
493  throw cet::exception("APAChannelsIntersect") << "Comparing two channels in in different APAs: "
494  << "channel " << chan1 << " in Cryo "
495  << wids1[0].Cryostat << ", APA " << this->ChannelToAPA(chan1)
496  << ", and channel " << chan2 << " in Cryo "
497  << wids2[0].Cryostat << ", APA " << this->ChannelToAPA(chan2)
498  << "\n";
499  return false; }
500 
501 
502  // Loop through wids1 and see if wids2 has any intersecting wires,
503  // given that the WireIDs are in the same TPC
504  for( unsigned int i1 = 0; i1 < wids1.size() ; i1++){
505  for( unsigned int i2 = 0; i2 < wids2.size() ; i2++){
506 
507  // make sure it is reasonable to intersect
508  if( wids1[i1].Plane == wids2[i2].Plane ||
509  wids1[i1].TPC != wids2[i2].TPC || // not reasonable for a *WireID*
510  wids1[i1].Cryostat != wids2[i2].Cryostat ) continue;
511 
512 // std::cout << "Checking: \n WireID 1 = ("
513 // << wids1[i1].Cryostat << "," << wids1[i1].TPC << ","
514 // << wids1[i1].Plane << "," << wids1[i1].Wire
515 // << ") \n WireID 2 = ("
516 // << wids2[i2].Cryostat << "," << wids2[i2].TPC << ","
517 // << wids2[i2].Plane << "," << wids2[i2].Wire << ")" << std::endl;
518 
519  // Check if they even intersect; if they do, push back
520  if( fGeom->WireIDsIntersect( wids1[i1], wids2[i2], widIntersect ) ){
521 
522 // std::cout << "we have an intersect" << std::endl;
523 
524  IntersectVector.push_back( widIntersect );
525  }
526  }
527  }
528 
529  // Of all considered configurations, there are never more than
530  // 4 intersections per channel pair
531  if( IntersectVector.size() > 4 ){
532  mf::LogWarning("APAChannelsIntersect") << "Got " << IntersectVector.size()
533  << " intersections for channels "
534  << chan1 << " and " << chan2
535  << " - never expect more than 4, so far"; }
536 
537 
538  // With increasing IntersectVector index, the WireID
539  // vector indices of the intersecting wireIDs increase.
540  // This matches the direction in which ChannelToWire
541  // builds its output WireID vector in the APA/35t Alg
542  std::sort( IntersectVector.begin(), IntersectVector.end() );
543 
544  // return true if any intersection points were found
545  if( IntersectVector.size() == 0 ) return false;
546  else return true;
547 
548  }
549 
550 
551 
552 
553 } //end namespace apa
process_name opflash particleana ie ie ie z
double z
z position of intersection
Definition: geo_types.h:805
BEGIN_PROLOG true icarus_rawdigitfilter FilterTools FilterPlane1 Plane
Encapsulate the construction of a single cyostat.
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
Planes which measure V.
Definition: geo_types.h:130
bool LineSegChanIntersect(TVector3 xyzStart, TVector3 xyzEnd, uint32_t chan, std::vector< geo::WireID > &widsCrossed, bool ExtendLine) const
If a line given by start/end points intersects a channel.
void Init()
Initialize some chanel numbers to speed up other methods.
unsigned int ChannelsInAPAView(APAView_t apaview) const
uint32_t FirstChannelInView(geo::View_t geoview, unsigned int apa, unsigned int cryo) const
Planes which measure Z direction.
Definition: geo_types.h:132
U view on both sides of the APA.
unsigned int fAPAsPerCryo
V view on both sides of the APA.
bool APAChannelsIntersect(uint32_t chan1, uint32_t chan2, std::vector< geo::WireIDIntersection > &IntersectVector) const
If the channels intersect, get all intersections.
BEGIN_PROLOG TPC
Z view on the larger-x side of the APA.
enum apa::_apa_plane_proj APAView_t
T abs(T value)
APAView_t APAView(uint32_t chan) const
Get which of the 4 APA views the channel is in.
Planes which measure U.
Definition: geo_types.h:129
unsigned int ChannelToAPA(uint32_t chan) const
Get number of the APA containing the given channel.
unsigned int ChannelsInView(geo::View_t geoview) const
unsigned int fChannelsPerAPA
All APAs have this same number of channels.
Definition of data types for geometry description.
Z view on the smaller-x side of the APA.
Encapsulate the geometry of a wire.
art::ServiceHandle< geo::Geometry const > fGeom
geo::WireID NearestWireIDOnChan(const double WorldLoc[3], uint32_t chan, unsigned int const plane, unsigned int const tpc=0, unsigned int const cstat=0) const
double y
y position of intersection
Definition: geo_types.h:804
std::vector< geo::WireID > ChanSegsPerSide(uint32_t chan, unsigned int side) const
void reconfigure(fhicl::ParameterSet const &p)
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:406
std::vector< double > ThreeChanPos(uint32_t u, uint32_t v, uint32_t z) const
Find the center of the 3 intersections, choose best if multiple.
BEGIN_PROLOG could also be cout
Encapsulate the construction of a single detector plane.