CMS 3D CMS Logo

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