CMS 3D CMS Logo

CaloDetIdAssociator.cc
Go to the documentation of this file.
1 #include "CaloDetIdAssociator.h"
4  const GlobalPoint& point2,
5  const DetId& id,
6  const double toleranceInSigmas,
7  const SteppingHelixStateInfo* initialState) const {
8  std::vector<GlobalPoint> pointBuffer;
9  const std::pair<const_iterator, const_iterator>& points = getDetIdPoints(id, pointBuffer);
10  // fast check
11  bool xLess(false), xIn(false), xMore(false);
12  bool yLess(false), yIn(false), yMore(false);
13  bool zLess(false), zIn(false), zMore(false);
14  double xMin(point1.x()), xMax(point2.x());
15  double yMin(point1.y()), yMax(point2.y());
16  double zMin(point1.z()), zMax(point2.z());
17  if (xMin > xMax)
19  if (yMin > yMax)
21  if (zMin > zMax)
23  for (std::vector<GlobalPoint>::const_iterator it = points.first; it != points.second; ++it) {
24  if (it->x() < xMin) {
25  xLess = true;
26  } else {
27  if (it->x() > xMax)
28  xMore = true;
29  else
30  xIn = true;
31  }
32  if (it->y() < yMin) {
33  yLess = true;
34  } else {
35  if (it->y() > yMax)
36  yMore = true;
37  else
38  yIn = true;
39  }
40  if (it->z() < zMin) {
41  zLess = true;
42  } else {
43  if (it->z() > zMax)
44  zMore = true;
45  else
46  zIn = true;
47  }
48  }
49  if (((xLess && !xIn && !xMore) || (!xLess && !xIn && xMore)) ||
50  ((yLess && !yIn && !yMore) || (!yLess && !yIn && yMore)) ||
51  ((zLess && !zIn && !zMore) || (!zLess && !zIn && zMore)))
52  return false;
53 
54  // Define plane normal to the trajectory direction at the first point
55  GlobalVector vector = (point2 - point1).unit();
56  float r21 = 0;
57  float r22 = vector.z() / sqrt(1 - pow(vector.x(), 2));
58  float r23 = -vector.y() / sqrt(1 - pow(vector.x(), 2));
59  float r31 = vector.x();
60  float r32 = vector.y();
61  float r33 = vector.z();
62  float r11 = r22 * r33 - r23 * r32;
63  float r12 = r23 * r31;
64  float r13 = -r22 * r31;
65 
66  Surface::RotationType rotation(r11, r12, r13, r21, r22, r23, r31, r32, r33);
67  Plane::PlanePointer plane = Plane::build(point1, rotation);
68  double absoluteTolerance = -1;
69  if (toleranceInSigmas > 0 && initialState) {
70  TrajectoryStateOnSurface tsos = initialState->getStateOnSurface(*plane);
71  if (tsos.isValid() and tsos.hasError()) {
72  LocalError localErr = tsos.localError().positionError();
73  localErr.scale(toleranceInSigmas);
74  float xx = localErr.xx();
75  float xy = localErr.xy();
76  float yy = localErr.yy();
77 
78  float denom = yy - xx;
79  float phi = 0.;
80  if (xy == 0 && denom == 0)
81  phi = M_PI_4;
82  else
83  phi = 0.5 * atan2(2. * xy, denom); // angle of MAJOR axis
84  // Unrotate the error ellipse to get the semimajor and minor axes. Then place points on
85  // the endpoints of semiminor an seminajor axes on original(rotated) error ellipse.
86  LocalError rotErr = localErr.rotate(-phi); // xy covariance of rotErr should be zero
87  float semi1 = sqrt(rotErr.xx());
88  float semi2 = sqrt(rotErr.yy());
89  absoluteTolerance = std::max(semi1, semi2);
90  }
91  }
92 
93  // distance between the points.
94  double trajectorySegmentLength = (point2 - point1).mag();
95 
96  // we need to find the angle that covers all points.
97  // if it's bigger than 180 degree, we are inside
98  // otherwise we are outside, i.e. the volume is not crossed
99  bool allBehind = true;
100  bool allTooFar = true;
101  std::vector<GlobalPoint>::const_iterator p = points.first;
102  if (p == points.second) {
103  edm::LogWarning("TrackAssociator") << "calo geometry for element " << id.rawId() << "is empty. Ignored";
104  return false;
105  }
106  LocalPoint localPoint = plane->toLocal(*p);
107  double minPhi = localPoint.phi();
108  double maxPhi = localPoint.phi();
109  if (localPoint.z() < 0)
110  allTooFar = false;
111  else {
112  allBehind = false;
113  if (localPoint.z() < trajectorySegmentLength)
114  allTooFar = false;
115  }
116  ++p;
117  for (; p != points.second; ++p) {
118  localPoint = plane->toLocal(*p);
119  double localPhi = localPoint.phi();
120  if (localPoint.z() < 0)
121  allTooFar = false;
122  else {
123  allBehind = false;
124  if (localPoint.z() < trajectorySegmentLength)
125  allTooFar = false;
126  }
127  if (localPhi >= minPhi && localPhi <= maxPhi)
128  continue;
129  if (localPhi + 2 * M_PI >= minPhi && localPhi + 2 * M_PI <= maxPhi)
130  continue;
131  if (localPhi - 2 * M_PI >= minPhi && localPhi - 2 * M_PI <= maxPhi)
132  continue;
133  // find the closest limit
134  if (localPhi > maxPhi) {
135  double delta1 = fabs(localPhi - maxPhi);
136  double delta2 = fabs(localPhi - 2 * M_PI - minPhi);
137  if (delta1 < delta2)
138  maxPhi = localPhi;
139  else
140  minPhi = localPhi - 2 * M_PI;
141  continue;
142  }
143  if (localPhi < minPhi) {
144  double delta1 = fabs(localPhi - minPhi);
145  double delta2 = fabs(localPhi + 2 * M_PI - maxPhi);
146  if (delta1 < delta2)
147  minPhi = localPhi;
148  else
149  maxPhi = localPhi + 2 * M_PI;
150  continue;
151  }
152  cms::Exception("FatalError")
153  << "Algorithm logic error - this should never happen. Problems with trajectory-volume matching.";
154  }
155  if (allBehind)
156  return false;
157  if (allTooFar)
158  return false;
159  if (fabs(maxPhi - minPhi) > M_PI)
160  return true;
161 
162  // now if the tolerance is positive, check how far we are
163  // from the closest line segment
164  if (absoluteTolerance < 0)
165  return false;
166  double distanceToClosestLineSegment = 1e9;
167  std::vector<GlobalPoint>::const_iterator i, j;
168  for (i = points.first; i != points.second; ++i)
169  for (j = i + 1; j != points.second; ++j) {
170  LocalPoint p1(plane->toLocal(*i));
171  LocalPoint p2(plane->toLocal(*j));
172  // now we deal with high school level math to get
173  // the triangle paramaters
174  double side1squared = p1.perp2();
175  double side2squared = p2.perp2();
176  double side3squared = (p2.x() - p1.x()) * (p2.x() - p1.x()) + (p2.y() - p1.y()) * (p2.y() - p1.y());
177  double area = fabs(p1.x() * p2.y() - p2.x() * p1.y()) / 2;
178  // all triangle angles must be smaller than 90 degree
179  // otherwise the projection is out of the line segment
180  if (side1squared + side2squared > side3squared && side2squared + side3squared > side1squared &&
181  side1squared + side3squared > side1squared) {
182  double h(2 * area / sqrt(side3squared));
183  if (h < distanceToClosestLineSegment)
184  distanceToClosestLineSegment = h;
185  } else {
186  if (sqrt(side1squared) < distanceToClosestLineSegment)
187  distanceToClosestLineSegment = sqrt(side1squared);
188  if (sqrt(side2squared) < distanceToClosestLineSegment)
189  distanceToClosestLineSegment = sqrt(side2squared);
190  }
191  }
192  if (distanceToClosestLineSegment < absoluteTolerance)
193  return true;
194  return false;
195 }
196 
199  if (geometry_ == nullptr)
200  throw cms::Exception("CaloGeometry is not set");
201 }
202 
204  return geometry_->getSubdetectorGeometry(id)->getGeometry(id)->getPosition();
205 }
206 
207 void CaloDetIdAssociator::getValidDetIds(unsigned int subDectorIndex, std::vector<DetId>& detIds) const {
208  if (subDectorIndex != 0)
209  cms::Exception("FatalError")
210  << "Calo sub-dectors are all handle as one sub-system, but subDetectorIndex is not zero.\n";
211  detIds = geometry_->getValidDetIds(DetId::Calo, 1);
212 }
213 
214 std::pair<DetIdAssociator::const_iterator, DetIdAssociator::const_iterator> CaloDetIdAssociator::getDetIdPoints(
215  const DetId& id, std::vector<GlobalPoint>& points) const {
217  if (!subDetGeom) {
218  LogDebug("TrackAssociator") << "Cannot find sub-detector geometry for " << id.rawId() << "\n";
219  return std::pair<const_iterator, const_iterator>(dummy_.end(), dummy_.end());
220  }
221  auto cellGeom = subDetGeom->getGeometry(id);
222  if (!cellGeom) {
223  LogDebug("TrackAssociator") << "Cannot find CaloCell geometry for " << id.rawId() << "\n";
224  return std::pair<const_iterator, const_iterator>(dummy_.end(), dummy_.end());
225  }
226  const CaloCellGeometry::CornersVec& cor(cellGeom->getCorners());
227  return std::pair<const_iterator, const_iterator>(cor.begin(), cor.end());
228 }
LocalError scale(float s) const
Definition: LocalError.h:31
TrajectoryStateOnSurface getStateOnSurface(const Surface &surf, bool returnTangentPlane=false) const
const LocalTrajectoryError & localError() const
LocalError rotate(float x, float y) const
Return a new LocalError, rotated by an angle defined by the direction (x,y)
Definition: LocalError.h:37
T z() const
Definition: PV3DBase.h:61
constexpr SubDetector subDetGeom[21]
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
constexpr int pow(int x)
Definition: conifer.h:24
LocalError positionError() const
GlobalPoint getPosition(const DetId &id) const override
static PlanePointer build(Args &&... args)
Definition: Plane.h:33
float yy() const
Definition: LocalError.h:24
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
T sqrt(T t)
Definition: SSEVec.h:19
void check_setup() const override
Basic3DVector unit() const
virtual std::shared_ptr< const CaloCellGeometry > getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
#define M_PI
std::vector< GlobalPoint > dummy_
float xy() const
Definition: LocalError.h:23
const CaloGeometry * geometry_
Definition: DetId.h:17
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
void getValidDetIds(unsigned int subDetectorIndex, std::vector< DetId > &) const override
std::vector< DetId > getValidDetIds() const
Get the list of all valid detector ids.
Definition: CaloGeometry.cc:75
std::pair< const_iterator, const_iterator > getDetIdPoints(const DetId &id, std::vector< GlobalPoint > &points) const override
Log< level::Warning, false > LogWarning
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
bool crossedElement(const GlobalPoint &, const GlobalPoint &, const DetId &id, const double tolerance=-1, const SteppingHelixStateInfo *=nullptr) const override
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:34
float xx() const
Definition: LocalError.h:22
virtual void check_setup() const
#define LogDebug(id)