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 }
Vector3DBase
Definition: Vector3DBase.h:8
TkRotation< float >
mps_fire.i
i
Definition: mps_fire.py:428
photonAnalyzer_cfi.zMax
zMax
Definition: photonAnalyzer_cfi.py:95
makePileupJSON.denom
denom
Definition: makePileupJSON.py:147
LocalTrajectoryError::positionError
LocalError positionError() const
Definition: LocalTrajectoryError.h:81
HLT_FULL_cff.points
points
Definition: HLT_FULL_cff.py:21449
LocalError::xy
float xy() const
Definition: LocalError.h:23
PV3DBase::x
T x() const
Definition: PV3DBase.h:59
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
h
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
Definition: L1TUtmAlgorithmRcd.h:4
CaloGeometry::getSubdetectorGeometry
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:34
photonAnalyzer_cfi.yMin
yMin
Definition: photonAnalyzer_cfi.py:86
ReferenceCountingPointer< Plane >
EZArrayFL< GlobalPoint >
DetId::Calo
Definition: DetId.h:29
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
SteppingHelixStateInfo::getStateOnSurface
TrajectoryStateOnSurface getStateOnSurface(const Surface &surf, bool returnTangentPlane=false) const
Definition: SteppingHelixStateInfo.cc:60
HLT_FULL_cff.maxPhi
maxPhi
Definition: HLT_FULL_cff.py:52983
PV3DBase::z
T z() const
Definition: PV3DBase.h:61
photonAnalyzer_cfi.xMin
xMin
Definition: photonAnalyzer_cfi.py:82
DetId
Definition: DetId.h:17
Plane.h
TrajectoryStateOnSurface
Definition: TrajectoryStateOnSurface.h:16
CaloDetIdAssociator::getDetIdPoints
std::pair< const_iterator, const_iterator > getDetIdPoints(const DetId &id, std::vector< GlobalPoint > &points) const override
Definition: CaloDetIdAssociator.cc:214
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
LocalError::xx
float xx() const
Definition: LocalError.h:22
TrajectoryStateOnSurface::hasError
bool hasError() const
Definition: TrajectoryStateOnSurface.h:56
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
p2
double p2[4]
Definition: TauolaWrapper.h:90
CaloDetIdAssociator::getPosition
GlobalPoint getPosition(const DetId &id) const override
Definition: CaloDetIdAssociator.cc:203
h
Point3DBase< float, GlobalTag >
multiplicitycorr_cfi.yMax
yMax
Definition: multiplicitycorr_cfi.py:6
geometryCSVtoXML.xy
xy
Definition: geometryCSVtoXML.py:19
LocalError::scale
LocalError scale(float s) const
Definition: LocalError.h:31
SteppingHelixStateInfo
Definition: SteppingHelixStateInfo.h:27
Plane::build
static PlanePointer build(Args &&... args)
Definition: Plane.h:33
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
idealTransformation.rotation
dictionary rotation
Definition: idealTransformation.py:1
geometryCSVtoXML.yy
yy
Definition: geometryCSVtoXML.py:19
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
LocalError
Definition: LocalError.h:12
CaloDetIdAssociator::getValidDetIds
void getValidDetIds(unsigned int subDetectorIndex, std::vector< DetId > &) const override
Definition: CaloDetIdAssociator.cc:207
LocalError::rotate
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
photonAnalyzer_cfi.zMin
zMin
Definition: photonAnalyzer_cfi.py:94
PV3DBase::y
T y() const
Definition: PV3DBase.h:60
M_PI
#define M_PI
Definition: BXVectorInputProducer.cc:49
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
p1
double p1[4]
Definition: TauolaWrapper.h:89
CaloSubdetectorGeometry::getGeometry
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.
Definition: CaloSubdetectorGeometry.cc:36
unit
Basic3DVector unit() const
Definition: Basic3DVectorLD.h:162
CaloDetIdAssociator::geometry_
const CaloGeometry * geometry_
Definition: CaloDetIdAssociator.h:56
DDAxes::phi
HLT_FULL_cff.minPhi
minPhi
Definition: HLT_FULL_cff.py:52971
CaloDetIdAssociator::dummy_
std::vector< GlobalPoint > dummy_
Definition: CaloDetIdAssociator.h:57
mag
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Definition: Basic3DVectorLD.h:127
CaloGeometry::getValidDetIds
std::vector< DetId > getValidDetIds() const
Get the list of all valid detector ids.
Definition: CaloGeometry.cc:75
dttmaxenums::r32
Definition: DTTMax.h:28
GeomDetEnumerators::subDetGeom
constexpr SubDetector subDetGeom[21]
Definition: GeomDetEnumerators.h:40
Exception
Definition: hltDiff.cc:245
CaloDetIdAssociator::crossedElement
bool crossedElement(const GlobalPoint &, const GlobalPoint &, const DetId &id, const double tolerance=-1, const SteppingHelixStateInfo *=nullptr) const override
Definition: CaloDetIdAssociator.cc:3
multiplicitycorr_cfi.xMax
xMax
Definition: multiplicitycorr_cfi.py:5
CaloSubdetectorGeometry
Definition: CaloSubdetectorGeometry.h:22
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
DetIdAssociator::check_setup
virtual void check_setup() const
Definition: DetIdAssociator.cc:371
CaloDetIdAssociator.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
TrajectoryStateOnSurface::localError
const LocalTrajectoryError & localError() const
Definition: TrajectoryStateOnSurface.h:77
custom_jme_cff.area
area
Definition: custom_jme_cff.py:140
LocalError::yy
float yy() const
Definition: LocalError.h:24
PV3DBase::phi
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
geometryCSVtoXML.xx
xx
Definition: geometryCSVtoXML.py:19
TrajectoryStateOnSurface::isValid
bool isValid() const
Definition: TrajectoryStateOnSurface.h:54
CaloDetIdAssociator::check_setup
void check_setup() const override
Definition: CaloDetIdAssociator.cc:197