LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
SpacePointAlg_TimeSort.cxx
Go to the documentation of this file.
1 
19 
20 #include <math.h>
21 
22 // Framework includes
25 #include "cetlib_except/exception.h"
27 
28 // LArSoft Includes
34 
35 //boost includes
36 #include "boost/multi_array.hpp"
37 
38 namespace {
39  inline bool HitTimeComparison(art::Ptr<recob::Hit> const& a, art::Ptr<recob::Hit> const& b)
40  {
41  return a->PeakTime() < b->PeakTime();
42  }
43 }
44 
45 namespace sppt {
46 
47  //-------------------------------------------------
49  {
50  fTimeDiffMax = pset.get<float>("TimeDiffMax");
51  fZDiffMax = pset.get<float>("ZDiffMax");
52  fYDiffMax = pset.get<float>("YDiffMax");
53 
54  //enforce a minimum time diff
55  if (fTimeDiffMax < 0) {
56  throw cet::exception("SpacePointAlg_TimeSort")
57  << "Time difference must be greater than zero.";
58  }
59  if (fZDiffMax < 0) {
60  throw cet::exception("SpacePointAlg_TimeSort")
61  << "Z-coordinate difference must be greater than zero.";
62  }
63  if (fYDiffMax < 0) {
64  throw cet::exception("SpacePointAlg_TimeSort")
65  << "Y-coordinate difference must be greater than zero.";
66  }
67  }
68 
69  //-------------------------------------------------
71  {
72  TIME_OFFSET_U = -1 * detProp.GetXTicksOffset(geo::View_t::kU, 0, 0);
73  TIME_OFFSET_V = -1 * detProp.GetXTicksOffset(geo::View_t::kV, 0, 0);
74  TIME_OFFSET_Y = -1 * detProp.GetXTicksOffset(geo::View_t::kZ, 0, 0);
75  TICKS_TO_X = detProp.GetXTicksCoefficient();
76 
77  TIME_OFFSET_SET = true;
78  }
79 
80  //-------------------------------------------------
82  {
83  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout const>()->Get();
84  constexpr geo::TPCID tpcid{0, 0};
85  constexpr geo::PlaneID uplane_id{tpcid, geo::View_t::kU};
86  constexpr geo::PlaneID vplane_id{tpcid, geo::View_t::kV};
87  constexpr geo::PlaneID zplane_id{tpcid, geo::View_t::kZ};
88 
89  unsigned int nwires_u = wireReadoutGeom.Nwires(uplane_id);
90  unsigned int nwires_v = wireReadoutGeom.Nwires(vplane_id);
91  unsigned int nwires_y = wireReadoutGeom.Nwires(zplane_id);
92 
93  coordinates_UV_y.resize(boost::extents[nwires_v][nwires_u]);
94  coordinates_UV_z.resize(boost::extents[nwires_v][nwires_u]);
95  coordinates_UY_y.resize(boost::extents[nwires_y][nwires_u]);
96  coordinates_UY_z.resize(boost::extents[nwires_y][nwires_u]);
97  for (unsigned int iu = 0; iu < nwires_u; iu++) {
98  geo::WireID const uplane_wire_id{uplane_id, iu};
99  for (unsigned int iv = 0; iv < nwires_v; iv++) {
100  auto intersection =
101  wireReadoutGeom.WireIDsIntersect(uplane_wire_id, geo::WireID{vplane_id, iv})
103  coordinates_UV_y[iv][iu] = intersection.y;
104  coordinates_UV_z[iv][iu] = intersection.z;
105  }
106  for (unsigned int iy = 0; iy < nwires_y; iy++) {
107  auto intersection =
108  wireReadoutGeom.WireIDsIntersect(uplane_wire_id, geo::WireID{zplane_id, iy})
110  coordinates_UY_y[iy][iu] = intersection.y;
111  coordinates_UY_z[iy][iu] = intersection.z;
112  }
113  }
114 
115  COORDINATES_FILLED = true;
116  }
117 
118  //-------------------------------------------------
120  detinfo::DetectorPropertiesData const& detProp,
124  std::unique_ptr<std::vector<recob::SpacePoint>>& spptCollection,
125  std::unique_ptr<std::vector<std::vector<art::Ptr<recob::Hit>>>>& spptAssociatedHits)
126  {
127 
128  if (!TIME_OFFSET_SET) {
129  mf::LogWarning("SpacePointAlg_TimeSort")
130  << "Time offsets not set before createSpacePoints call!"
131  << "\nYou should call SpacePointAlg_TimeSort::setTimeOffsets() in beginRun()!"
132  << "\nWill be set now, but you should modify your code!";
133  setTimeOffsets(detProp);
134  }
135  if (!COORDINATES_FILLED) {
136  mf::LogWarning("SpacePointAlg_TimeSort")
137  << "Coordinate arrays not filled before createSpacePoints call!"
138  << "\nYou should call SpacePointAlg_TimeSort::fillCoordinateArrays() in beginRun()!"
139  << "\nWill be filled now, but you should modify your code!";
141  }
142 
143  //sort the hits by the time
144  sortHitsByTime(hitVec_U);
145  sortHitsByTime(hitVec_V);
146  sortHitsByTime(hitVec_Y);
147 
148  MF_LOG_DEBUG("SpacePointAlg_TimeSort")
149  << "Sorted " << hitVec_U.size() << " u hits, " << hitVec_V.size() << " v hits, "
150  << hitVec_Y.size() << " y hits.";
151 
152  //now, do the loop to search for like-timed hits across the three planes
153  std::vector<art::Ptr<recob::Hit>>::iterator ihitu = hitVec_U.begin();
154  std::vector<art::Ptr<recob::Hit>>::iterator ihitv = hitVec_V.begin();
155  std::vector<art::Ptr<recob::Hit>>::iterator ihity = hitVec_Y.begin();
156  std::vector<art::Ptr<recob::Hit>>::iterator ihitv_inner, ihity_inner;
157  double time_hitu = (*ihitu)->PeakTime() + TIME_OFFSET_U;
158  double time_hitv = (*ihitv)->PeakTime() + TIME_OFFSET_V;
159  double time_hity = (*ihity)->PeakTime() + TIME_OFFSET_Y;
160  double time_hitv_inner, time_hity_inner;
161  while (ihitu != hitVec_U.end()) {
162  time_hitu = (*ihitu)->PeakTime() + TIME_OFFSET_U;
163 
164  mf::LogInfo("SpacePointAlg_TimeSort")
165  << "Hit times (u,v,y)=(" << time_hitu << "," << time_hitv << "," << time_hity << ")";
166 
167  //if time_hitu is too much bigger than time_hitv, need to advance hitv iterator
168  while ((time_hitu - time_hitv) > fTimeDiffMax) {
169  ihitv++;
170  if (ihitv == hitVec_V.end()) break;
171  time_hitv = (*ihitv)->PeakTime() + TIME_OFFSET_V;
172  }
173  if (ihitv == hitVec_V.end()) break;
174 
175  //same thing with time_hitu and time_hity
176  while ((time_hitu - time_hity) > fTimeDiffMax) {
177  ihity++;
178  if (ihity == hitVec_Y.end()) break;
179  time_hity = (*ihity)->PeakTime() + TIME_OFFSET_Y;
180  }
181  if (ihity == hitVec_Y.end()) break;
182 
183  //OK, now we know time_hitu <= time_hitv and time_hitu <= time_hity.
184  //Next, check if time_hitu is near time_hitv and time_hit y. If not,
185  //we have to increment ihitu.
186  if (std::abs(time_hitu - time_hitv) > fTimeDiffMax ||
187  std::abs(time_hitu - time_hity) > fTimeDiffMax) {
188  ihitu++;
189  continue;
190  }
191 
192  //OK! Note we KNOW that these three match in time:
193  // -- time_hitu is within fTimeDiffMax of both time_hitv and time_hity; and
194  // -- time_hitu <= time_hitv AND time_hitu <=time_hity, so time_hitv and time_hity are near too
195 
196  mf::LogInfo("SpacePointAlg_TimeSort") << "Matching hit times (u,v,y)=(" << time_hitu << ","
197  << time_hitv << "," << time_hity << ")";
198 
199  //Next thing to do, we need to loop over all possible 3-hit matches for our given u-hit.
200  //We need new iterators in v and y at this location, and will loop over those
201  ihitv_inner = ihitv;
202  time_hitv_inner = (*ihitv_inner)->PeakTime() + TIME_OFFSET_V;
203  ihity_inner = ihity;
204  time_hity_inner = (*ihity_inner)->PeakTime() + TIME_OFFSET_Y;
205 
206  while (std::abs(time_hitu - time_hitv_inner) < fTimeDiffMax &&
207  std::abs(time_hitu - time_hity_inner) < fTimeDiffMax) {
208 
209  unsigned int uwire = (*ihitu)->WireID().Wire;
210  unsigned int vwire = (*ihitv_inner)->WireID().Wire;
211  unsigned int ywire = (*ihity_inner)->WireID().Wire;
212 
213  mf::LogInfo("SpacePointAlg_TimeSort")
214  << "(y,z) coordinate for uv/uy: (" << coordinates_UV_y[vwire][uwire] << ","
215  << coordinates_UV_z[vwire][uwire] << ")/(" << 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  }
249 
250  //now increment the v or y hit, whichever is smalles (closest to u hit) in time
251  if (time_hitv_inner <= time_hity_inner) {
252  ihitv_inner++;
253  if (ihitv_inner == hitVec_V.end()) break;
254  time_hitv_inner = (*ihitv_inner)->PeakTime() + TIME_OFFSET_V;
255  }
256  else {
257  ihity_inner++;
258  if (ihity_inner == hitVec_Y.end()) break;
259  time_hity_inner = (*ihity_inner)->PeakTime() + TIME_OFFSET_Y;
260  }
261  }
262 
263  ihitu++;
264  } // end while looping over u hits
265 
266  MF_LOG_DEBUG("SpacePointAlg_TimeSort")
267  << "Finished with " << spptCollection->size() << " spacepoints.";
268 
269  } //end createSpacePoints
270 
271  //-------------------------------------------------
273  {
274  std::sort(hitVec.begin(), hitVec.end(), HitTimeComparison);
275  }
276 
277 } //end sppt namespace
void sortHitsByTime(std::vector< art::Ptr< recob::Hit >> &hits_handle) const
SpacePointAlg_TimeSort(fhicl::ParameterSet const &pset)
float fYDiffMax
Maximum allowed time difference.
void setTimeOffsets(detinfo::DetectorPropertiesData const &detProp)
double GetXTicksCoefficient(int t, int c) const
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Planes which measure V.
Definition: geo_types.h:132
double GetXTicksOffset(int p, int t, int c) const
Declaration of signal hit object.
The data type to uniquely identify a Plane.
Definition: geo_types.h:364
constexpr auto abs(T v)
Returns the absolute value of the argument.
boost::multi_array< double, 2 > coordinates_UV_y
Planes which measure Z direction.
Definition: geo_types.h:134
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
float fZDiffMax
Maximum allowed y-coordinate difference.
boost::multi_array< double, 2 > coordinates_UY_z
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
Planes which measure U.
Definition: geo_types.h:131
bool TIME_OFFSET_SET
Maximum allowed z-coordinate difference.
T get(std::string const &key) const
Definition: ParameterSet.h:314
The data type to uniquely identify a TPC.
Definition: geo_types.h:306
Definition of data types for geometry description.
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:226
boost::multi_array< double, 2 > coordinates_UY_y
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
#define MF_LOG_DEBUG(id)
void createSpacePoints(detinfo::DetectorPropertiesData const &detProp, 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)
static constexpr WireIDIntersection invalid()
Definition: geo_types.h:594
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
boost::multi_array< double, 2 > coordinates_UV_z