CMS 3D CMS Logo

AreaSeededTrackingRegionsBuilder.cc
Go to the documentation of this file.
2 
8 
9 #include <array>
10 #include <limits>
11 
12 namespace {
13  float perp2(const std::array<float, 2>& a) {
14  return a[0]*a[0] + a[1]*a[1];
15  }
16 }
17 
19  m_extraPhi = regPSet.getParameter<double>("extraPhi");
20  m_extraEta = regPSet.getParameter<double>("extraEta");
21 
22  // RectangularEtaPhiTrackingRegion parameters:
23  m_ptMin = regPSet.getParameter<double>("ptMin");
24  m_originRadius = regPSet.getParameter<double>("originRadius");
25  m_precise = regPSet.getParameter<bool>("precise");
27  if(m_whereToUseMeasurementTracker != RectangularEtaPhiTrackingRegion::UseMeasurementTracker::kNever) {
29  }
30  m_searchOpt = regPSet.getParameter<bool>("searchOpt");
31 }
32 
34  desc.add<double>("extraPhi", 0.);
35  desc.add<double>("extraEta", 0.);
36 
37  desc.add<double>("ptMin", 0.9);
38  desc.add<double>("originRadius", 0.2);
39  desc.add<bool>("precise", true);
40 
41  desc.add<std::string>("whereToUseMeasurementTracker", "Never");
42  desc.add<edm::InputTag>("measurementTrackerName", edm::InputTag(""));
43 
44  desc.add<bool>("searchOpt", false);
45 }
46 
48  auto builder = Builder(this);
49 
53  builder.setMeasurementTracker(hmte.product());
54  }
55 
56  return builder;
57 }
58 
59 
60 std::vector<std::unique_ptr<TrackingRegion> > AreaSeededTrackingRegionsBuilder::Builder::regions(const Origins& origins, const std::vector<Area>& areas) const {
61  std::vector<std::unique_ptr<TrackingRegion> > result;
62 
63  // create tracking regions in directions of the points of interest
64  int n_regions = 0;
65  for(const auto& origin: origins) {
66  result.push_back(region(origin, areas));
67  ++n_regions;
68  }
69  LogDebug("AreaSeededTrackingRegionsBuilder") << "produced "<<n_regions<<" regions";
70 
71  return result;
72 }
73 
74 std::unique_ptr<TrackingRegion> AreaSeededTrackingRegionsBuilder::Builder::region(const Origin& origin, const std::vector<Area>& areas) const {
75  return regionImpl(origin, areas);
76 }
77 std::unique_ptr<TrackingRegion> AreaSeededTrackingRegionsBuilder::Builder::region(const Origin& origin, const edm::VecArray<Area, 2>& areas) const {
78  return regionImpl(origin, areas);
79 }
80 
81 template <typename T>
82 std::unique_ptr<TrackingRegion> AreaSeededTrackingRegionsBuilder::Builder::regionImpl(const Origin& origin, const T& areas) const {
83  float minEta=std::numeric_limits<float>::max(), maxEta=std::numeric_limits<float>::lowest();
84  float minPhi=std::numeric_limits<float>::max(), maxPhi=std::numeric_limits<float>::lowest();
85 
86  const auto& orig = origin.first;
87 
88  LogDebug("AreaSeededTrackingRegionsProducer") << "Origin x,y,z " << orig.x() << "," << orig.y() << "," << orig.z();
89 
90  auto vecFromOrigPlusRadius = [&](const std::array<float, 2>& vec) {
91  const auto invlen = 1.f/std::sqrt(perp2(vec));
92  const auto tmp = (1.f - m_conf->m_originRadius*invlen);
93  return std::array<float, 2>{{vec[0]*tmp, vec[1]*tmp}};
94  };
95  auto tangentVec = [&](const std::array<float, 2>& vec, int sign) {
96  const auto d = std::sqrt(perp2(vec));
97  const auto r = m_conf->m_originRadius;
98  const auto tmp = r/std::sqrt(d*d - r*r);
99  // sign+ for counterclockwise, sign- for clockwise
100  const auto orthvec = sign > 0 ? std::array<float, 2>{{-vec[1]*tmp, vec[0]*tmp}}
101  : std::array<float, 2>{{ vec[1]*tmp, -vec[0]*tmp}};
102  return std::array<float, 2>{{vec[0]-orthvec[0], vec[1]-orthvec[1]}};
103  };
104 
105  for(const auto& area: areas) {
106  // straight line assumption is conservative, accounding for
107  // low-pT bending would only tighten the eta-phi window
108  LogTrace("AreaSeededTrackingRegionsBuilder") << " area x,y points "
109  << area.x_rmin_phimin() << "," << area.y_rmin_phimin() << " " // go around
110  << area.x_rmin_phimax() << "," << area.y_rmin_phimax() << " "
111  << area.x_rmax_phimax() << "," << area.y_rmax_phimax() << " "
112  << area.x_rmax_phimin() << "," << area.y_rmax_phimin() << " "
113  << "z " << area.zmin() << "," << area.zmax();
114 
115  // some common variables
116  const float x_rmin_phimin = area.x_rmin_phimin()-orig.x();
117  const float x_rmin_phimax = area.x_rmin_phimax()-orig.x();
118  const float y_rmin_phimin = area.y_rmin_phimin()-orig.y();
119  const float y_rmin_phimax = area.y_rmin_phimax()-orig.y();
120 
121  const std::array<float, 2> p_rmin_phimin = {{x_rmin_phimin, y_rmin_phimin}};
122  const std::array<float, 2> p_rmin_phimax = {{x_rmin_phimax, y_rmin_phimax}};
123  const std::array<float, 2> p_rmax_phimin = {{area.x_rmax_phimin() - orig.x(), area.y_rmax_phimin() - orig.y()}};
124  const std::array<float, 2> p_rmax_phimax = {{area.x_rmax_phimax() - orig.x(), area.y_rmax_phimax() - orig.y()}};
125 
126 
127  // eta
128  {
129  // find which of the two rmin points is closer to the origin
130  //
131  // closest point for p_rmin, farthest point for p_rmax
132  const std::array<float, 2> p_rmin = perp2(p_rmin_phimin) < perp2(p_rmin_phimax) ? p_rmin_phimin : p_rmin_phimax;
133  const std::array<float, 2> p_rmax = perp2(p_rmax_phimin) > perp2(p_rmax_phimax) ? p_rmax_phimin : p_rmax_phimax;
134 
135  // then calculate the xy vector from the point closest to p_rmin on
136  // the (origin,originRadius) circle to the p_rmin
137  // this will maximize the eta window
138  const auto p_min = vecFromOrigPlusRadius(p_rmin);
139  const auto p_max = vecFromOrigPlusRadius(p_rmax);
140 
141  // then we calculate the maximal eta window
142  const auto etamin = std::min(etaFromXYZ(p_min[0], p_min[1], area.zmin() - (orig.z()+origin.second)),
143  etaFromXYZ(p_max[0], p_max[1], area.zmin() - (orig.z()+origin.second)));
144  const auto etamax = std::max(etaFromXYZ(p_min[0], p_min[1], area.zmax() - (orig.z()-origin.second)),
145  etaFromXYZ(p_max[0], p_max[1], area.zmax() - (orig.z()-origin.second)));
146 
147  LogTrace("AreaSeededTrackingRegionBuilder") << " eta min,max " << etamin << "," << etamax;
148 
149  minEta = std::min(minEta, etamin);
150  maxEta = std::max(maxEta, etamax);
151  }
152 
153  // phi
154  {
155  // For phi we construct the tangent lines of (origin,
156  // originRadius) that go though each of the 4 points (in xy
157  // plane) of the area. Easiest is to make a vector orthogonal to
158  // origin->point vector which has a length of
159  //
160  // length = r*d/std::sqrt(d*d-r*r)
161  //
162  // where r is the originRadius and d is the distance from origin
163  // to the point (to derive draw the situation and start with
164  // definitions of sin/cos of one of the angles of the
165  // right-angled triangle.
166 
167  // But we start with a "reference phi" so that we can easily
168  // decide which phi is the largest/smallest without having too
169  // much of headache with the wrapping. The reference is in
170  // principle defined by the averages of y&x phimin/phimax at
171  // rmin, but the '0.5f*' factor is omitted here to reduce
172  // computations.
173  const auto phi_ref = std::atan2(y_rmin_phimin + y_rmin_phimax,
174  x_rmin_phimin + x_rmin_phimax);
175 
176  // for maximum phi we need the orthogonal vector to the left
177  const auto tan_rmin_phimax = tangentVec(p_rmin_phimax, +1);
178  const auto tan_rmax_phimax = tangentVec(p_rmax_phimax, +1);
179  const auto phi_rmin_phimax = std::atan2(tan_rmin_phimax[1], tan_rmin_phimax[0]);
180  const auto phi_rmax_phimax = std::atan2(tan_rmax_phimax[1], tan_rmax_phimax[0]);
181 
182  auto phimax = std::abs(reco::deltaPhi(phi_rmin_phimax, phi_ref)) > std::abs(reco::deltaPhi(phi_rmax_phimax, phi_ref)) ?
183  phi_rmin_phimax : phi_rmax_phimax;
184 
185  LogTrace("AreaSeededTrackingRegionBuilder") << " rmin_phimax vec " << p_rmin_phimax[0] << "," << p_rmin_phimax[1]
186  << " tangent " << tan_rmin_phimax[0] << "," << tan_rmin_phimax[1]
187  << " phi " << phi_rmin_phimax << "\n"
188  << " rmax_phimax vec " << p_rmax_phimax[0] << "," << p_rmax_phimax[1]
189  << " tangent " << tan_rmax_phimax[0] << "," << tan_rmax_phimax[1]
190  << " phi " << phi_rmax_phimax << "\n"
191  << " phimax " << phimax;
192 
193 
194 
195  // for minimum phi we need the orthogonal vector to the right
196  const auto tan_rmin_phimin = tangentVec(p_rmin_phimin, -1);
197  const auto tan_rmax_phimin = tangentVec(p_rmax_phimin, -1);
198  const auto phi_rmin_phimin = std::atan2(tan_rmin_phimin[1], tan_rmin_phimin[0]);
199  const auto phi_rmax_phimin = std::atan2(tan_rmax_phimin[1], tan_rmax_phimin[0]);
200 
201  auto phimin = std::abs(reco::deltaPhi(phi_rmin_phimin, phi_ref)) > std::abs(reco::deltaPhi(phi_rmax_phimin, phi_ref)) ?
202  phi_rmin_phimin : phi_rmax_phimin;
203 
204 
205  LogTrace("AreaSeededTrackingRegionBuilder") << " rmin_phimin vec " << p_rmin_phimin[0] << "," << p_rmin_phimin[1]
206  << " tangent " << tan_rmin_phimin[0] << "," << tan_rmin_phimin[1]
207  << " phi " << phi_rmin_phimin << "\n"
208  << " rmax_phimin vec " << p_rmax_phimin[0] << "," << p_rmax_phimin[1]
209  << " tangent " << tan_rmax_phimin[0] << "," << tan_rmax_phimin[1]
210  << " phi " << phi_rmax_phimin << "\n"
211  << " phimin " << phimin;
212 
213  // wrapped around, need to decide which one to wrap
214  if(phimax < phimin) {
215  if(phimax < 0) phimax += 2*M_PI;
216  else phimin -= 2*M_PI;
217  }
218 
219  LogTrace("AreaSeededTrackingRegionBuilder") << " phi min,max " << phimin << "," << phimax;
220 
221  minPhi = std::min(minPhi, phimin);
222  maxPhi = std::max(maxPhi, phimax);
223  }
224 
225  LogTrace("AreaSeededTrackingRegionBuilder") << " windows after this area eta " << minEta << "," << maxEta
226  << " phi " << minPhi << "," << maxPhi;
227  }
228 
229  const auto meanEta = (minEta+maxEta)/2.f;
230  const auto meanPhi = (minPhi+maxPhi)/2.f;
231  const auto deltaEta = maxEta-meanEta + m_conf->m_extraEta;
232  const auto deltaPhi = maxPhi-meanPhi + m_conf->m_extraPhi;
233 
234  const auto x = std::cos(meanPhi);
235  const auto y = std::sin(meanPhi);
236  const auto z = (x*x+y*y)/std::tan(2.f*std::atan(std::exp(-meanEta))); // simplify?
237 
238  LogTrace("AreaSeededTrackingRegionsBuilder") << "Direction x,y,z " << x << "," << y << "," << z
239  << " eta,phi " << meanEta << "," << meanPhi
240  << " window eta " << (meanEta-deltaEta) << "," << (meanEta+deltaEta)
241  << " phi " << (meanPhi-deltaPhi) << "," << (meanPhi+deltaPhi);
242 
243  return std::make_unique<RectangularEtaPhiTrackingRegion>(
244  GlobalVector(x,y,z),
245  origin.first, // GlobalPoint
246  m_conf->m_ptMin,
247  m_conf->m_originRadius,
248  origin.second,
249  deltaEta,
250  deltaPhi,
251  m_conf->m_whereToUseMeasurementTracker,
252  m_conf->m_precise,
253  m_measurementTracker,
254  m_conf->m_searchOpt
255  );
256 }
#define LogDebug(id)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
static void fillDescriptions(edm::ParameterSetDescription &desc)
T getParameter(std::string const &) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
double maxEta
static UseMeasurementTracker stringToUseMeasurementTracker(const std::string &name)
static const double deltaEta
Definition: CaloConstants.h:8
RectangularEtaPhiTrackingRegion::UseMeasurementTracker m_whereToUseMeasurementTracker
std::unique_ptr< TrackingRegion > region(const Origin &origin, const std::vector< Area > &areas) const
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
std::vector< std::unique_ptr< TrackingRegion > > regions(const Origins &origins, const std::vector< Area > &areas) const
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
#define LogTrace(id)
#define M_PI
double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
T const * product() const
Definition: Handle.h:81
T perp2() const
Squared magnitude of transverse component.
std::unique_ptr< TrackingRegion > regionImpl(const Origin &origin, const T &areas) const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double a
Definition: hdecay.h:121
bool isUninitialized() const
Definition: EDGetToken.h:73
Builder beginEvent(const edm::Event &e) const
edm::EDGetTokenT< MeasurementTrackerEvent > token_measurementTracker
long double T
AreaSeededTrackingRegionsBuilder(const edm::ParameterSet &regPSet, edm::ConsumesCollector &&iC)
Global3DVector GlobalVector
Definition: GlobalVector.h:10