LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SpacePointAlg_TimeSort.cxx
Go to the documentation of this file.
1 
19 
20 #include <math.h>
21 
22 // LArSoft Includes
30 
31 //boost includes
32 #include "boost/multi_array.hpp"
33 
34 namespace sppt{
35 
36  //-------------------------------------------------
38  this->reconfigure(pset);
39  TIME_OFFSET_SET = false;
40  COORDINATES_FILLED = false;
41  }
42 
43  //-------------------------------------------------
45 
46  //-------------------------------------------------
48  fTimeDiffMax = p.get< float >("TimeDiffMax");
49  fZDiffMax = p.get< float >("ZDiffMax");
50  fYDiffMax = p.get< float >("YDiffMax");
51 
52  //enforce a minimum time diff
53  if(fTimeDiffMax<0){
54  mf::LogError("SpacePointAlg_TimeSort") << "Time difference must be greater than zero.";
55  return;
56  }
57  if(fZDiffMax<0){
58  mf::LogError("SpacePointAlg_TimeSort") << "Z-coordinate difference must be greater than zero.";
59  return;
60  }
61  if(fYDiffMax<0){
62  mf::LogError("SpacePointAlg_TimeSort") << "Y-coordinate difference must be greater than zero.";
63  return;
64  }
65 
66  }
67 
68  //-------------------------------------------------
70 
71  const detinfo::DetectorProperties* detprop = lar::providerFrom<detinfo::DetectorPropertiesService>();
75  TICKS_TO_X = detprop->GetXTicksCoefficient();
76 
77  TIME_OFFSET_SET = true;
78  }
79 
80  //-------------------------------------------------
82 
84  unsigned int nwires_u = geom->Nwires(geo::View_t::kU);
85  unsigned int nwires_v = geom->Nwires(geo::View_t::kV);
86  unsigned int nwires_y = geom->Nwires(geo::View_t::kZ);
87 
88  coordinates_UV_y.resize(boost::extents[nwires_v][nwires_u]);
89  coordinates_UV_z.resize(boost::extents[nwires_v][nwires_u]);
90  coordinates_UY_y.resize(boost::extents[nwires_y][nwires_u]);
91  coordinates_UY_z.resize(boost::extents[nwires_y][nwires_u]);
92  for(unsigned int iu=0; iu<nwires_u; iu++){
93  for(unsigned int iv=0; iv<nwires_v; iv++){
94  geom->IntersectionPoint(iu,iv,
96  0,0,
97  coordinates_UV_y[iv][iu], //y
98  coordinates_UV_z[iv][iu]); //z
99  }
100  for(unsigned int iy=0; iy<nwires_y; iy++){
101  geom->IntersectionPoint(iu,iy,
103  0,0,
104  coordinates_UY_y[iy][iu],
105  coordinates_UY_z[iy][iu]);
106  }
107  }
108 
109  COORDINATES_FILLED = true;
110  }
111 
112  //-------------------------------------------------
114  std::vector< art::Ptr<recob::Hit> > &hitVec_V,
115  std::vector< art::Ptr<recob::Hit> > &hitVec_Y,
116  std::unique_ptr<std::vector<recob::SpacePoint> > &spptCollection,
117  std::unique_ptr<std::vector<std::vector<art::Ptr<recob::Hit> > > > &spptAssociatedHits)
118  {
119 
120  if(!TIME_OFFSET_SET){
121  mf::LogWarning("SpacePointAlg_TimeSort")
122  << "Time offsets not set before createSpacePoints call!"
123  << "\nYou should call SpacePointAlg_TimeSort::setTimeOffsets() in beginRun()!"
124  << "\nWill be set now, but you should modify your code!";
125  setTimeOffsets();
126  }
127  if(!COORDINATES_FILLED){
128  mf::LogWarning("SpacePointAlg_TimeSort")
129  << "Coordinate arrays not filled before createSpacePoints call!"
130  << "\nYou should call SpacePointAlg_TimeSort::fillCoordinateArrays() in beginRun()!"
131  << "\nWill be filled now, but you should modify your code!";
133  }
134 
135  //sort the hits by the time
136  sortHitsByTime(hitVec_U);
137  sortHitsByTime(hitVec_V);
138  sortHitsByTime(hitVec_Y);
139 
140  LOG_DEBUG("SpacePointAlg_TimeSort")
141  << "Sorted "
142  << hitVec_U.size() << " u hits, "
143  << hitVec_V.size() << " v hits, "
144  << hitVec_Y.size() << " y hits.";
145 
146  //now, do the loop to search for like-timed hits across the three planes
147  std::vector< art::Ptr<recob::Hit> >::iterator ihitu = hitVec_U.begin();
148  std::vector< art::Ptr<recob::Hit> >::iterator ihitv = hitVec_V.begin();
149  std::vector< art::Ptr<recob::Hit> >::iterator ihity = hitVec_Y.begin();
150  std::vector< art::Ptr<recob::Hit> >::iterator ihitv_inner,ihity_inner;
151  double time_hitu = (*ihitu)->PeakTime() + TIME_OFFSET_U;
152  double time_hitv = (*ihitv)->PeakTime() + TIME_OFFSET_V;
153  double time_hity = (*ihity)->PeakTime() + TIME_OFFSET_Y;
154  double time_hitv_inner,time_hity_inner;
155  while(ihitu != hitVec_U.end()){
156  time_hitu = (*ihitu)->PeakTime() + TIME_OFFSET_U;
157 
158  mf::LogInfo("SpacePointAlg_TimeSort")
159  << "Hit times (u,v,y)=("
160  << time_hitu << ","
161  << time_hitv << ","
162  << time_hity << ")";
163 
164  //if time_hitu is too much bigger than time_hitv, need to advance hitv iterator
165  while( (time_hitu-time_hitv)>fTimeDiffMax ){
166  ihitv++;
167  if(ihitv==hitVec_V.end()) break;
168  time_hitv = (*ihitv)->PeakTime() + TIME_OFFSET_V;
169  }
170  if(ihitv==hitVec_V.end()) break;
171 
172  //same thing with time_hitu and time_hity
173  while( (time_hitu-time_hity)>fTimeDiffMax ){
174  ihity++;
175  if(ihity==hitVec_Y.end()) break;
176  time_hity = (*ihity)->PeakTime() + TIME_OFFSET_Y;
177  }
178  if(ihity==hitVec_Y.end()) break;
179 
180  //OK, now we know time_hitu <= time_hitv and time_hitu <= time_hity.
181  //Next, check if time_hitu is near time_hitv and time_hit y. If not,
182  //we have to increment ihitu.
183  if(std::abs(time_hitu-time_hitv)>fTimeDiffMax || std::abs(time_hitu-time_hity)>fTimeDiffMax){
184  ihitu++;
185  continue;
186  }
187 
188  //OK! Note we KNOW that these three match in time:
189  // -- time_hitu is within fTimeDiffMax of both time_hitv and time_hity; and
190  // -- time_hitu <= time_hitv AND time_hitu <=time_hity, so time_hitv and time_hity are near too
191 
192  mf::LogInfo("SpacePointAlg_TimeSort")
193  << "Matching hit times (u,v,y)=("
194  << time_hitu << ","
195  << time_hitv << ","
196  << time_hity << ")";
197 
198  //Next thing to do, we need to loop over all possible 3-hit matches for our given u-hit.
199  //We need new iterators in v and y at this location, and will loop over those
200  ihitv_inner = ihitv;
201  time_hitv_inner = (*ihitv_inner)->PeakTime() + TIME_OFFSET_V;
202  ihity_inner = ihity;
203  time_hity_inner = (*ihity_inner)->PeakTime() + TIME_OFFSET_Y;
204 
205  while(std::abs(time_hitu-time_hitv_inner)<fTimeDiffMax && std::abs(time_hitu-time_hity_inner)<fTimeDiffMax){
206 
207  unsigned int uwire = (*ihitu)->WireID().Wire;
208  unsigned int vwire = (*ihitv_inner)->WireID().Wire;
209  unsigned int ywire = (*ihity_inner)->WireID().Wire;
210 
211  mf::LogInfo("SpacePointAlg_TimeSort")
212  << "(y,z) coordinate for uv/uy: ("
213  << coordinates_UV_y[vwire][uwire] << ","
214  << coordinates_UV_z[vwire][uwire] << ")/("
215  << coordinates_UY_y[ywire][uwire] << ","
216  << coordinates_UY_z[ywire][uwire] << ")";
217 
218  if(std::abs(coordinates_UV_y[vwire][uwire]-coordinates_UY_y[ywire][uwire])<fYDiffMax &&
219  std::abs(coordinates_UV_z[vwire][uwire]-coordinates_UY_z[ywire][uwire])<fZDiffMax){
220 
221  double xyz[3];
222  double xyz_err[6];
223  //triangular error matrix:
224  // | 0. |
225  // | 0. 0. |
226  // | 0. 0. 0. |
227 
228  //get average y and z, with errors
229  xyz[1] = (coordinates_UV_y[vwire][uwire] + coordinates_UY_y[ywire][uwire])*0.5;
230  xyz_err[2] = std::abs(coordinates_UV_y[vwire][uwire] - xyz[1]);
231  xyz[2] = (coordinates_UV_z[vwire][uwire] + coordinates_UY_z[ywire][uwire])*0.5;
232  xyz_err[5] = std::abs(coordinates_UV_z[vwire][uwire] - xyz[2]);
233 
234  double t_val = (time_hitu + time_hitv_inner + time_hity_inner)/3.;
235  double t_err = 0.5*std::sqrt( (time_hitu-t_val)*(time_hitu-t_val) +
236  (time_hitv_inner-t_val)*(time_hitv_inner-t_val) +
237  (time_hity_inner-t_val)*(time_hity_inner-t_val));
238  xyz[0] = TICKS_TO_X * t_val;
239  xyz_err[0] = TICKS_TO_X * t_err;
240 
241  //make space point to put on event
242  recob::SpacePoint spt(xyz, xyz_err, 0., spptCollection->size());
243  spptCollection->push_back(spt);
244 
245  //make association with hits
246  std::vector< art::Ptr<recob::Hit> > myhits = {*ihitu,*ihitv_inner,*ihity_inner};
247  spptAssociatedHits->push_back(myhits);
248  //util::CreateAssn(*this, evt, *spptCollection, myhits, *spptAssns);
249  }
250 
251  //now increment the v or y hit, whichever is smalles (closest to u hit) in time
252  if(time_hitv_inner <= time_hity_inner){
253  ihitv_inner++;
254  if(ihitv_inner==hitVec_V.end()) break;
255  time_hitv_inner = (*ihitv_inner)->PeakTime() + TIME_OFFSET_V;
256  }
257  else{
258  ihity_inner++;
259  if(ihity_inner==hitVec_Y.end()) break;
260  time_hity_inner = (*ihity_inner)->PeakTime() + TIME_OFFSET_Y;
261  }
262 
263  }
264 
265  ihitu++;
266  }// end while looping over u hits
267 
268  LOG_DEBUG("SpacePointAlg_TimeSort")
269  << "Finished with " << spptCollection->size() << " spacepoints.";
270 
271  }//end createSpacePoints
272 
273  //-------------------------------------------------
275  std::sort(hitVec.begin(),hitVec.end(),HitTimeComparison);
276  }
277 
278 
279 } //end sppt namespace
SpacePointAlg_TimeSort(fhicl::ParameterSet const &pset)
float fYDiffMax
Maximum allowed time difference.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Planes which measure V.
Definition: geo_types.h:77
Declaration of signal hit object.
boost::multi_array< double, 2 > coordinates_UV_y
Planes which measure Z direction.
Definition: geo_types.h:79
float fZDiffMax
Maximum allowed y-coordinate difference.
boost::multi_array< double, 2 > coordinates_UY_z
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
unsigned int Nwires(unsigned int p, unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wires in the specified plane.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Planes which measure U.
Definition: geo_types.h:76
bool TIME_OFFSET_SET
Maximum allowed z-coordinate difference.
bool HitTimeComparison(art::Ptr< recob::Hit > a, art::Ptr< recob::Hit > b)
virtual double GetXTicksCoefficient(int t, int c) const =0
T get(std::string const &key) const
Definition: ParameterSet.h:231
bool IntersectionPoint(geo::WireID const &wid1, geo::WireID const &wid2, double &y, double &z) const
Returns the intersection point of two wires.
Definition of data types for geometry description.
void createSpacePoints(std::vector< art::Ptr< recob::Hit > > &hitVec_U, std::vector< art::Ptr< recob::Hit > > &hitVec_V, std::vector< art::Ptr< recob::Hit > > &hitVec_Y, std::unique_ptr< std::vector< recob::SpacePoint > > &spptCollection, std::unique_ptr< std::vector< std::vector< art::Ptr< recob::Hit > > > > &spptAssociatedHits)
Utility object to perform functions of association.
boost::multi_array< double, 2 > coordinates_UY_y
void reconfigure(fhicl::ParameterSet const &pset)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
#define LOG_DEBUG(id)
art framework interface to geometry description
void sortHitsByTime(std::vector< art::Ptr< recob::Hit > > &hits_handle)
boost::multi_array< double, 2 > coordinates_UV_z
virtual double GetXTicksOffset(int p, int t, int c) const =0