LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar::util Namespace Reference

LArSoft utility namespace. More...

Namespaces

 details
 LArSoft utility implementation details.
 
 simple_geo
 Simple class definitions for geometry concepts.
 

Classes

struct  assns_has_metadata
 Trait: value true if Assns (association or its node) has metadata. More...
 
struct  assns_iterator_type
 Trait: type is iterator of Assns. More...
 
struct  assns_iterator_type< art::Assns< L, R, D > >
 
struct  assns_iterator_type< art::Assns< L, R, void > >
 
struct  assns_metadata_type
 Trait: type is metadata in Assns (association or its node). More...
 
struct  assns_metadata_type< art::Assns< L, R, D > >
 
struct  assns_metadata_type< art::AssnsNode< L, R, D > >
 
struct  assns_metadata_type< proxy::details::AssnsNode< ArtAssnsIterValue > >
 
struct  assns_metadata_type< std::pair< art::Ptr< L >, art::Ptr< R > > >
 
struct  assns_traits
 Data types for the specified association type (or its node). More...
 
struct  assns_traits< art::Assns< L, R, D > >
 
struct  assns_traits< art::AssnsNode< L, R, D > >
 
struct  assns_traits< std::pair< art::Ptr< L >, art::Ptr< R > > >
 
class  ChiSquareAccumulator
 Computes a χ² from expectation function and data points. More...
 
struct  dereferenced_type
 Class defining the dereferenced type of the specified type. More...
 
class  GaussianFit
 "Fast" Gaussian fit More...
 
struct  identity
 A unary functor returning its own argument (any type) More...
 
class  LinearFit
 Performs a linear regression of data. More...
 
class  MinMaxCollector
 Keeps track of the minimum and maximum value we observed. More...
 
class  QuadraticFit
 Performs a second-degree fit of data. More...
 
struct  RealComparisons
 Provides simple real number checks. More...
 
class  StatCollector
 Collects statistics on a single quantity (weighted) More...
 
class  StatCollector2D
 Collects statistics on two homogeneous quantities (weighted) More...
 
struct  Vector2DComparison
 Class comparing 2D vectors. More...
 
struct  Vector3DComparison
 Class comparing 2D vectors. More...
 

Typedefs

using HitToWire = details::FindAllP< recob::Hit, recob::Wire >
 Query object connecting a hit to a wire. More...
 
template<typename Assns >
using assns_metadata_t = typename assns_metadata_type< Assns >::type
 Trait: type of metadata in Assns (association or its node). More...
 
template<typename Assns >
using assns_iterator_t = typename assns_iterator_type< Assns >::type
 Trait: type of iterator of Assns. More...
 

Functions

bool PointWithinSegments (double A_start_x, double A_start_y, double A_end_x, double A_end_y, double B_start_x, double B_start_y, double B_end_x, double B_end_y, double x, double y)
 Returns whether x and y are within both specified ranges (A and B). More...
 
template<typename RealType >
auto makeVector2DComparison (RealType threshold)
 Creates a Vector2DComparison from a RealComparisons object. More...
 
template<typename RealType >
auto makeVector2DComparison (lar::util::RealComparisons< RealType > const &comp)
 Creates a Vector2DComparison from a RealComparisons object. More...
 
template<typename RealType >
auto makeVector3DComparison (RealType threshold)
 Creates a Vector3DComparison from a RealComparisons object. More...
 
template<typename RealType >
auto makeVector3DComparison (lar::util::RealComparisons< RealType > const &comp)
 Creates a Vector3DComparison from a RealComparisons object. More...
 
double TrackProjectedLength (recob::Track const &track, geo::View_t view)
 Returns the length of the projection of a track on a view. More...
 
double TrackPitchInView (recob::Track const &track, geo::View_t view, size_t trajectory_point=0U)
 Returns the projected length of track on a wire pitch step [cm]. More...
 
template<typename F >
auto makeChiSquareAccumulator (F &&e)
 Creates a ChiSquareAccumulator object with the specified function. More...
 
template<typename T , typename F >
auto makeChiSquareAccumulator (F &&e)
 Creates a ChiSquareAccumulator object with the specified function. More...
 
template<typename T >
details::dereference_class< T, details::has_dereference_class< T >::value >::reference_type dereference (T &v)
 Returns the value pointed by the argument, or the argument itself. More...
 
template<typename T >
details::make_pointer_class< T, details::has_dereference_class< T >::value >::pointer_type make_pointer (T &v)
 Returns a pointer to the value of argument, or the argument itself. More...
 
bool ValueInRange (double value, double min, double max)
 Returns whether a value is within the specified range. More...
 
template<typename A , typename B >
constexpr auto absDiff (A const &a, B const &b)
 Returns the absolute value of the difference between two values. More...
 

Variables

template<typename Assns >
constexpr bool assns_has_metadata_v = assns_has_metadata<Assns>::value
 Trait: true if Assns (association or its node) has metadata. More...
 

Detailed Description

LArSoft utility namespace.

LArSoft utilities namespace.

Typedef Documentation

Query object connecting a hit to a wire.


Once upon a time, recob::Hit had a art::Ptr<recob::Wire> in it, and life was easy. When it was discovered that art pointers in data products were evil, they were banned from recob::Hit. As always, evil turns out to be convenient. This query object tries to provide in an efficient way a connection between a hit and the wire that has generated it.

This object expects

Example of usage: let hit_ptr be a valid art::Ptr<recob::Hit>. Then

HitToWire HtoW(evt);
art::Ptr<recob::Wire> wire_ptr = HtoW[hit_ptr];

If the association label is known, it can be used to selectively load that association:

HitToWire HtoW(evt, AssociationInputTag);
art::Ptr<recob::Wire> wire_ptr = HtoW[hit_ptr];

that has little advantage (in fact, it is possibly slower) respect to using art::FindOneP.

Definition at line 54 of file HitUtils.h.

Function Documentation

template<typename A , typename B >
constexpr auto lar::util::absDiff ( A const &  a,
B const &  b 
)

Returns the absolute value of the difference between two values.

Template Parameters
Atype of the first value
Btype of the second value (must actually be as A)
Parameters
athe first value
bthe second value
Returns
the difference between the largest and the smallest of a and b

The pecularity of this implementation is that it always avoids taking the difference between the smallest and the largest of a and b. An equivalent implementation is:

return std::max(a, b) - std::min(a, b);

It still assumes that the difference is representable in A; for example, this assumption will fail for int types with a a very large number and b a very small (i.e. negative) number.

Requirements:

  • A and B must be the same type

Definition at line 69 of file NumericUtils.h.

Referenced by cluster::ClusterCrawlerAlg::AddHit(), cluster::ClusterCrawlerAlg::ClusterHitsOK(), cluster::DBScan3DAlg::init(), AcceptFindNeighbors::isNear(), calo::Calorimetry::produce(), pma::ProjectionMatchingAlg::selectInitialHits(), and cluster::ClusterCrawlerAlg::Vtx3ClusterMatch().

70  {
71  static_assert(std::is_same<std::decay_t<A>, std::decay_t<B>>{},
72  "Arguments of util::absDiff() have to be of the same type.");
73  return (b > a) ? (b - a) : (a - b);
74  }
template<typename T >
details::dereference_class<T, details::has_dereference_class<T>::value>::reference_type lar::util::dereference ( T &  v)
inline

Returns the value pointed by the argument, or the argument itself.


Template Parameters
Tthe type of the argument
Parameters
vthe value to be dereferenced
Returns
the value v points to, if any, or v itself

This function allows the use of the same template code to process both pointers and pointed values. For example:

template <typename T>
std::vector<int> extract_int(std::vector<T> const& from) {
  std::vector<int> v;
  v.reserve(from.size());
  std::transform(from.begin(), from.end(), std::back_inserter(v),
    util::dereference);
  return v;
}

int value = 10;
std::vector<int> v(10, value);
std::vector<int*> v_ptr(10, &value);

extract_int(v);
extract_int(v_ptr);

shows that the same function can be used on vectors of integers and of their pointers.

Definition at line 241 of file Dereference.h.

Referenced by util::PxHitConverter::ToPxHit().

242  {
244  }
double value
Definition: spectrum.C:18
template<typename T >
details::make_pointer_class<T, details::has_dereference_class<T>::value>::pointer_type lar::util::make_pointer ( T &  v)
inline

Returns a pointer to the value of argument, or the argument itself.


Template Parameters
Tthe type of the argument
Parameters
vthe value to be taken the pointer of
Returns
a pointer to v, or v itself if it is already a C pointer

This function allows the use of the same template code to process both pointers and pointed values. For example:

template <typename T>
std::vector<int*> extract_int(std::vector<T> const& from) {
  std::vector<int*> v;
  v.reserve(from.size());
  std::transform(from.begin(), from.end(), std::back_inserter(v),
    util::make_pointer);
  return v;
}

int value = 10;
std::vector<int> v(10, value);
std::vector<int*> v_ptr(10, &value);

extract_int(v);
extract_int(v_ptr);

shows that the same function can be used on vectors of integers and of their pointers.

Definition at line 279 of file Dereference.h.

Referenced by cluster::ClusterParamsImportWrapper< Algo >::ImportHits().

280  {
282  }
double value
Definition: spectrum.C:18
template<typename F >
auto lar::util::makeChiSquareAccumulator ( F &&  e)

Creates a ChiSquareAccumulator object with the specified function.

Template Parameters
Ftype of function (deduced from e)
Parameters
eexpectation function
Returns
a ChiSquareAccumulator<F> instance with specified expectation

Example of usage:

auto zero = [](double){ return 0.0; }; // expectation function
auto chiSquare = lar::util::makeChiSquareAccumulator(zero);

declare chiSquare in a way equivalent to: lar::util::ChiSquareAccumulator<decltype(zero)> chiSquare(zero).

Definition at line 163 of file ChiSquareAccumulator.h.

References e.

164  {
165  return lar::util::ChiSquareAccumulator<F>(std::forward<F>(e));
166  }
Computes a χ² from expectation function and data points.
Float_t e
Definition: plot.C:35
template<typename T , typename F >
auto lar::util::makeChiSquareAccumulator ( F &&  e)

Creates a ChiSquareAccumulator object with the specified function.

Template Parameters
Ttype of data (default: double)
Ftype of function (deduced from e)
Parameters
eexpectation function
Returns
a ChiSquareAccumulator<F,T> instance with specified expectation

Example of usage:

auto zero = [](float){ return 0.0F; }; // expectation function
auto chiSquare = lar::util::makeChiSquareAccumulator<float>(zero);

declare chiSquare in a way equivalent to: lar::util::ChiSquareAccumulator<decltype(zero), float> chiSquare(zero).

Definition at line 184 of file ChiSquareAccumulator.h.

References e.

185  {
186  return lar::util::ChiSquareAccumulator<F, T>(std::forward<F>(e));
187  }
Computes a χ² from expectation function and data points.
Float_t e
Definition: plot.C:35
template<typename RealType >
auto lar::util::makeVector2DComparison ( RealType  threshold)

Creates a Vector2DComparison from a RealComparisons object.

Definition at line 178 of file RealComparisons.h.

References lar::util::RealComparisons< RealType >::threshold.

179  {
180  return Vector2DComparison<RealType>(threshold);
181  }
template<typename RealType >
auto lar::util::makeVector2DComparison ( lar::util::RealComparisons< RealType > const &  comp)

Creates a Vector2DComparison from a RealComparisons object.

Definition at line 185 of file RealComparisons.h.

186  {
187  return Vector2DComparison<RealType>(comp);
188  }
template<typename RealType >
auto lar::util::makeVector3DComparison ( RealType  threshold)

Creates a Vector3DComparison from a RealComparisons object.

Definition at line 252 of file RealComparisons.h.

References lar::util::RealComparisons< RealType >::threshold.

Referenced by checkTPCcoords(), groupTPCsByDriftDir(), geo::TPCGeo::UpdateAfterSorting(), and geo::PlaneGeo::UpdateWireDir().

253  {
254  return Vector3DComparison<RealType>(threshold);
255  }
template<typename RealType >
auto lar::util::makeVector3DComparison ( lar::util::RealComparisons< RealType > const &  comp)

Creates a Vector3DComparison from a RealComparisons object.

Definition at line 259 of file RealComparisons.h.

260  {
261  return Vector3DComparison<RealType>(comp);
262  }
bool lar::util::PointWithinSegments ( double  A_start_x,
double  A_start_y,
double  A_end_x,
double  A_end_y,
double  B_start_x,
double  B_start_y,
double  B_end_x,
double  B_end_y,
double  x,
double  y 
)

Returns whether x and y are within both specified ranges (A and B).

Definition at line 16 of file NumericUtils.cxx.

References e.

Referenced by geo::GeometryCore::ChannelsIntersect(), and geo::IntersectSegments().

26  {
27  constexpr RealComparisons<double> coordIs{1e-8};
28  return coordIs.withinSorted(x, A_start_x, A_end_x) &&
29  coordIs.withinSorted(y, A_start_y, A_end_y) &&
30  coordIs.withinSorted(x, B_start_x, B_end_x) &&
31  coordIs.withinSorted(y, B_start_y, B_end_y);
32  }
Float_t x
Definition: compare.C:6
Float_t y
Definition: compare.C:6
Float_t e
Definition: plot.C:35
double lar::util::TrackPitchInView ( recob::Track const &  track,
geo::View_t  view,
size_t  trajectory_point = 0U 
)

Returns the projected length of track on a wire pitch step [cm].

Parameters
trackthe track to be projected on a view
viewthe view for track projection
trajectory_pointat which point of the track to look for the pitch (default: 0)
Returns
wire pitch along the track direction at its specified point [cm]
Exceptions
cet::exception(category "TrackPitchInView") if the trajectory_point index is not valid in track
cet::exception(category "Geometry") if the point is in no TPC
cet::exception(category "TPCGeo") if the view is unknown, not available or otherwise invalid
cet::exception(category "Track") if the track projection on the wire plane is parallel to the wires (< 0.01%)

This function returns the distance covered by the track between two wires, projected on the wire plane. The direction of the track is the one at the specified trajectory point (the first one by default). That direction is projected on the wire plane with the specified view within the TPC that contains that point.

The returned value is the distance, in centimeters, between two consecutive wires on that projected direction. This is always a positive number, regardless the direction of the track, and never smaller than the wire pitch on the projection wire plane.

Definition at line 75 of file TrackUtils.cxx.

References util::abs(), d, recob::tracking::TrajectoryPoint_t::direction(), e, geo::PlaneGeo::GetWireDirection(), geo::PlaneGeo::ID(), geo::PlaneGeo::InterWireProjectedDistance(), recob::Track::NumberTrajectoryPoints(), recob::tracking::TrajectoryPoint_t::position, proj, geo::PlaneGeo::Projection(), recob::Track::TrajectoryPoint(), geo::PlaneGeo::ViewName(), geo::PlaneGeo::WirePitch(), and lar::util::RealComparisons< RealType >::zero().

Referenced by calo::TrackCalorimetryAlg::AnalyzeHit(), shower::EMShowerAlg::FinddEdx_(), and calo::Calorimetry::produce().

78 {
79  /*
80  * The plan:
81  * 1. find the wire plane we are talking about
82  * (in the right TPC and with the right view)
83  * 2. ask the plane the answer
84  *
85  */
86 
87  if (trajectory_point >= track.NumberTrajectoryPoints()) {
88  cet::exception("TrackPitchInView")
89  << "ERROR: Asking for trajectory point #" << trajectory_point
90  << " when trajectory vector size is of size " << track.NumberTrajectoryPoints() << ".\n";
91  }
92  recob::Track::TrajectoryPoint_t const& point = track.TrajectoryPoint(trajectory_point);
93 
94  // this throws if the position is not in any TPC,
95  // or if there is no view with specified plane
96  auto const& geom = *(lar::providerFrom<geo::Geometry>());
97  geo::PlaneGeo const& plane = geom.PositionToTPC(point.position).Plane(view);
98 
99 #if 0 // this can be enabled after `geo::PlaneGeo::InterWireProjectedDistance()` becomes available in larcorealg
100  double const d = plane.InterWireProjectedDistance(point.direction());
101 
102  // do we prefer to just return the value and let the caller check it?
103  if (d > 50.0 * plane.WirePitch()) { // after many pitches track would scatter
104  throw cet::exception("Track") << "track at point #" << trajectory_point
105  << " is almost parallel to the wires in view "
106  << geo::PlaneGeo::ViewName(view) << " (wire direction is "
107  << plane.GetWireDirection() << "; track direction is "
108  << point.direction() << ").\n";
109  }
110  return d;
111 
112 #else // !0
113  //
114  // 2. project the direction of the track on that plane
115  //
116  // this is the projection of the direction of the track on the wire plane;
117  // it is 2D and its second component ("Y()") is on wire coordinate direction;
118  // remember that the projection modulus is smaller than 1 because it is
119  // the 3D direction versor, deprived of its drift direction component
120  auto const& proj = plane.Projection(point.direction());
121 
122  if (lar::util::RealComparisons(1e-4).zero(proj.Y())) {
123  throw cet::exception("Track") << "track at point #" << trajectory_point
124  << " is almost parallel to the wires in view "
125  << geo::PlaneGeo::ViewName(view) << " (wire direction is "
126  << plane.GetWireDirection() << "; track direction is "
127  << point.direction() << ", its projection on plane " << plane.ID()
128  << " is " << proj << ").\n";
129  }
130 
131  //
132  // 3. scale that projection so that it covers a wire pitch worth in the wire
133  // coordinate direction;
134  // WirePitch() is what gives this vector a physical size [cm]
135  //
136  return proj.R() / std::abs(proj.Y()) * plane.WirePitch();
137 #endif // ?0
138 
139 } // lar::util::TrackPitchInView()
A point in the trajectory, with position and momentum.
Definition: TrackingTypes.h:85
static std::string ViewName(geo::View_t view)
Returns the name of the specified view.
Definition: PlaneGeo.cxx:682
Provides simple real number checks.
constexpr bool zero(Value_t value) const
Returns whether the value is no farther from 0 than the threshold.
constexpr auto abs(T v)
Returns the absolute value of the argument.
WireCoordProjection_t Projection(geo::Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: PlaneGeo.h:850
double InterWireProjectedDistance(WireCoordProjection_t const &projDir) const
Returns the distance between wires along the specified direction.
Definition: PlaneGeo.cxx:627
Vector_t const & GetWireDirection() const
Returns the direction of the wires.
Definition: PlaneGeo.h:459
Geometry information for a single wire plane.The plane is represented in the geometry by a solid whic...
Definition: PlaneGeo.h:78
Float_t d
Definition: plot.C:235
Vector_t direction() const
Returns the direction of the trajectory (unit vector of the momentum).
Definition: TrackingTypes.h:97
geo::PlaneID const & ID() const
Returns the identifier of this plane.
Definition: PlaneGeo.h:184
Float_t proj
Definition: plot.C:35
Float_t e
Definition: plot.C:35
Float_t track
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
double WirePitch() const
Return the wire pitch (in centimeters). It is assumed constant.
Definition: PlaneGeo.h:378
Point_t position
position in the trajectory [cm].
Definition: TrackingTypes.h:87
double lar::util::TrackProjectedLength ( recob::Track const &  track,
geo::View_t  view 
)

Returns the length of the projection of a track on a view.

Parameters
trackthe track to be projected on a view
viewthe view to project the track on
Returns
length of the projection, in centimetres
Todo:
CAREFUL: using view to determine projected length does not work for DUNE. Need to think more about this.
Todo:
is this right, or should it be dist*cosgamma???

Definition at line 33 of file TrackUtils.cxx.

References util::abs(), recob::Track::DirectionAtPoint(), larg4::dist(), geo::kUnknown, recob::Track::LocationAtPoint(), and recob::Track::NumberTrajectoryPoints().

34 {
35 
36  if (view == geo::kUnknown) {
37  throw cet::exception("TrackProjectedLength") << "cannot provide projected length for "
38  << "unknown view\n";
39  }
40  double length = 0.;
41 
42  auto const* geom = lar::providerFrom<geo::Geometry>();
43  double angleToVert = 0.;
44  for (auto const& plane : geom->Iterate<geo::PlaneGeo>(geo::CryostatID{0})) {
45  if (plane.View() == view) {
46  angleToVert = plane.Wire(0).ThetaZ(false) - 0.5 * ::util::pi<>();
47  break;
48  }
49  }
50 
51  // now loop over all points in the trajectory and add the contribution to the
52  // to the desired view
53 
54  for (size_t p = 1; p < track.NumberTrajectoryPoints(); ++p) {
55  const auto& pos_cur = track.LocationAtPoint(p);
56  const auto& pos_prev = track.LocationAtPoint(p - 1);
57  double dist =
58  std::sqrt(std::pow(pos_cur.x() - pos_prev.x(), 2) + std::pow(pos_cur.y() - pos_prev.y(), 2) +
59  std::pow(pos_cur.z() - pos_prev.z(), 2));
60 
61  // (sin(angleToVert),cos(angleToVert)) is the direction perpendicular to wire
62  // fDir[p-1] is the direction between the two relevant points
63  const auto& dir_prev = track.DirectionAtPoint(p - 1);
64  double cosgamma =
65  std::abs(std::sin(angleToVert) * dir_prev.Y() + std::cos(angleToVert) * dir_prev.Z());
66 
68  length += dist / cosgamma;
69  } // end loop over distances between trajectory points
70 
71  return length;
72 } // lar::util::TrackProjectedLength()
Unknown view.
Definition: geo_types.h:142
constexpr auto abs(T v)
Returns the absolute value of the argument.
Geometry information for a single wire plane.The plane is represented in the geometry by a solid whic...
Definition: PlaneGeo.h:78
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
Float_t track
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
The data type to uniquely identify a cryostat.
Definition: geo_types.h:192
bool lar::util::ValueInRange ( double  value,
double  min,
double  max 
)

Returns whether a value is within the specified range.

Parameters
valuethe value to be tested
minthe lower boundary
maxthe upper boundary
Returns
whether the value is within range

If min is larger than max, they are swapped. A tolerance of 10^-6 (absolute) is used.

Todo:

Use wiggle instead of 10^-6

resort source code for a bit of speed up

Definition at line 7 of file NumericUtils.cxx.

References util::abs(), and e.

Referenced by apa::APAGeometryAlg::LineSegChanIntersect().

8  {
9  if (min > max) std::swap(min, max); //protect against funny business due to wire angles
10  if (std::abs(value - min) < 1e-6 || std::abs(value - max) < 1e-6) return true;
11  return (value >= min) && (value <= max);
12  }
constexpr auto abs(T v)
Returns the absolute value of the argument.
double value
Definition: spectrum.C:18
void swap(lar::deep_const_fwd_iterator_nested< CITER, INNERCONTEXTRACT > &a, lar::deep_const_fwd_iterator_nested< CITER, INNERCONTEXTRACT > &b)
Float_t e
Definition: plot.C:35