test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PixelTripletHLTGenerator.cc
Go to the documentation of this file.
2 
4 #include "ThirdHitRZPrediction.h"
7 
10 #include "ThirdHitCorrection.h"
13 #include <iostream>
14 
17 
19 //#include "RecoParticleFlow/PFProducer/interface/KDTreeLinkerAlgo.h"
20 //#include "RecoParticleFlow/PFProducer/interface/KDTreeLinkerTools.h"
21 #include "RecoPixelVertexing/PixelTriplets/plugins/KDTreeLinkerAlgo.h" //amend to point at your copy...
23 
24 #include<cstdio>
25 
28 
29 using namespace std;
30 using namespace ctfseeding;
31 
33  : thePairGenerator(0),
34  theLayerCache(0),
35  useFixedPreFiltering(cfg.getParameter<bool>("useFixedPreFiltering")),
36  extraHitRZtolerance(cfg.getParameter<double>("extraHitRZtolerance")),
37  extraHitRPhitolerance(cfg.getParameter<double>("extraHitRPhitolerance")),
38  useMScat(cfg.getParameter<bool>("useMultScattering")),
39  useBend(cfg.getParameter<bool>("useBending"))
40 {
41  theMaxElement=cfg.getParameter<unsigned int>("maxElement");
42  dphi = (useFixedPreFiltering) ? cfg.getParameter<double>("phiPreFiltering") : 0;
43 
44  edm::ParameterSet comparitorPSet =
45  cfg.getParameter<edm::ParameterSet>("SeedComparitorPSet");
46  std::string comparitorName = comparitorPSet.getParameter<std::string>("ComponentName");
47  if(comparitorName != "none") {
48  theComparitor.reset( SeedComparitorFactory::get()->create( comparitorName, comparitorPSet, iC) );
49  }
50 }
51 
53  delete thePairGenerator;
54 }
55 
57  LayerCacheType* layerCache)
58 {
59  thePairGenerator = pairs.clone();
60  theLayerCache = layerCache;
61 }
62 
64  std::vector<SeedingLayerSetsHits::SeedingLayer> thirdLayers) {
66  theLayers = thirdLayers;
67 }
68 
71  const edm::Event & ev,
72  const edm::EventSetup& es)
73 {
74 
75  if (theComparitor) theComparitor->init(ev, es);
76 
77  auto const & doublets = thePairGenerator->doublets(region,ev,es);
78 
79  if (doublets.empty()) return;
80 
81  auto outSeq = doublets.detLayer(HitDoublets::outer)->seqNum();
82 
83 
84  // std::cout << "pairs " << doublets.size() << std::endl;
85 
86  float regOffset = region.origin().perp(); //try to take account of non-centrality (?)
87  int size = theLayers.size();
88 
90 
91  const RecHitsSortedInPhi * thirdHitMap[size];
93 
94  using NodeInfo = KDTreeNodeInfo<unsigned int>;
95  std::vector<NodeInfo > layerTree; // re-used throughout
96  std::vector<unsigned int> foundNodes; // re-used thoughout
97  foundNodes.reserve(100);
98 
100  float rzError[size]; //save maximum errors
101  float maxphi = Geom::ftwoPi(), minphi = -maxphi; // increase to cater for any range
102 
103  // fill the prediction vector
104  for (int il=0; il!=size; ++il) {
105  thirdHitMap[il] = &(*theLayerCache)(theLayers[il], region, ev, es);
106  auto const & hits = *thirdHitMap[il];
107  ThirdHitRZPrediction<PixelRecoLineRZ> & pred = preds[il];
108  pred.initLayer(theLayers[il].detLayer());
110 
111  layerTree.clear();
112  float minv=999999.0, maxv= -999999.0; // Initialise to extreme values in case no hits
113  float maxErr=0.0f;
114  for (unsigned int i=0; i!=hits.size(); ++i) {
115  auto angle = hits.phi(i);
116  auto v = hits.gv(i);
117  //use (phi,r) for endcaps rather than (phi,z)
118  minv = std::min(minv,v); maxv = std::max(maxv,v);
119  float myerr = hits.dv[i];
120  maxErr = std::max(maxErr,myerr);
121  layerTree.emplace_back(i, angle, v); // save it
122  if (angle < 0) // wrap all points in phi
123  { layerTree.emplace_back(i, angle+Geom::ftwoPi(), v);}
124  else
125  { layerTree.emplace_back(i, angle-Geom::ftwoPi(), v);}
126  }
127  KDTreeBox phiZ(minphi, maxphi, minv-0.01f, maxv+0.01f); // declare our bounds
128  //add fudge factors in case only one hit and also for floating-point inaccuracy
129  hitTree[il].build(layerTree, phiZ); // make KDtree
130  rzError[il] = maxErr; //save error
131  // std::cout << "layer " << theLayers[il].detLayer()->seqNum() << " " << layerTree.size() << std::endl;
132  }
133 
134  float imppar = region.originRBound();
135  float imppartmp = region.originRBound()+region.origin().perp();
136  float curv = PixelRecoUtilities::curvature(1.f/region.ptMin(), es);
137 
138  for (std::size_t ip =0; ip!=doublets.size(); ip++) {
139  auto xi = doublets.x(ip,HitDoublets::inner);
140  auto yi = doublets.y(ip,HitDoublets::inner);
141  auto zi = doublets.z(ip,HitDoublets::inner);
142  auto rvi = doublets.rv(ip,HitDoublets::inner);
143  auto xo = doublets.x(ip,HitDoublets::outer);
144  auto yo = doublets.y(ip,HitDoublets::outer);
145  auto zo = doublets.z(ip,HitDoublets::outer);
146  auto rvo = doublets.rv(ip,HitDoublets::outer);
147 
148  PixelRecoPointRZ point1(rvi, zi);
149  PixelRecoPointRZ point2(rvo, zo);
150  PixelRecoLineRZ line(point1, point2);
151  ThirdHitPredictionFromInvParabola predictionRPhi(xi-region.origin().x(),yi-region.origin().y(),
152  xo-region.origin().x(),yo-region.origin().y(),
153  imppar,curv,extraHitRPhitolerance);
154 
155  ThirdHitPredictionFromInvParabola predictionRPhitmp(xi,yi,xo,yo,imppartmp,curv,extraHitRPhitolerance);
156 
157  // printf("++Constr %f %f %f %f %f %f %f\n",xi,yi,xo,yo,imppartmp,curv,extraHitRPhitolerance);
158 
159  // std::cout << ip << ": " << point1.r() << ","<< point1.z() << " "
160  // << point2.r() << ","<< point2.z() <<std::endl;
161 
162  for (int il=0; il!=size; ++il) {
163  if (hitTree[il].empty()) continue; // Don't bother if no hits
164 
165  auto const & hits = *thirdHitMap[il];
166 
167  const DetLayer * layer = theLayers[il].detLayer();
168  auto barrelLayer = layer->isBarrel();
169 
170  ThirdHitCorrection correction(es, region.ptMin(), layer, line, point2, outSeq, useMScat, useBend);
171 
172  ThirdHitRZPrediction<PixelRecoLineRZ> & predictionRZ = preds[il];
173 
174  predictionRZ.initPropagator(&line);
175  Range rzRange = predictionRZ();
176  correction.correctRZRange(rzRange);
177 
178  Range phiRange;
179  if (useFixedPreFiltering) {
180  float phi0 = doublets.phi(ip,HitDoublets::outer);
181  phiRange = Range(phi0-dphi,phi0+dphi);
182  }
183  else {
184  Range radius;
185  if (barrelLayer) {
186  radius = predictionRZ.detRange();
187  } else {
188  radius = Range(max(rzRange.min(), predictionRZ.detSize().min()),
189  min(rzRange.max(), predictionRZ.detSize().max()) );
190  }
191  if (radius.empty()) continue;
192 
193  // std::cout << "++R " << radius.min() << " " << radius.max() << std::endl;
194 
195  /*
196  Range rPhi1m = predictionRPhitmp(radius.max(), -1);
197  Range rPhi1p = predictionRPhitmp(radius.max(), 1);
198  Range rPhi2m = predictionRPhitmp(radius.min(), -1);
199  Range rPhi2p = predictionRPhitmp(radius.min(), 1);
200  Range rPhi1 = rPhi1m.sum(rPhi1p);
201  Range rPhi2 = rPhi2m.sum(rPhi2p);
202 
203 
204  auto rPhi1N = predictionRPhitmp(radius.max());
205  auto rPhi2N = predictionRPhitmp(radius.min());
206 
207  std::cout << "VI "
208  << rPhi1N.first <<'/'<< rPhi1.first << ' '
209  << rPhi1N.second <<'/'<< rPhi1.second << ' '
210  << rPhi2N.first <<'/'<< rPhi2.first << ' '
211  << rPhi2N.second <<'/'<< rPhi2.second
212  << std::endl;
213 
214  */
215 
216  auto rPhi1 = predictionRPhitmp(radius.max());
217  auto rPhi2 = predictionRPhitmp(radius.min());
218 
219 
220  correction.correctRPhiRange(rPhi1);
221  correction.correctRPhiRange(rPhi2);
222  rPhi1.first /= radius.max();
223  rPhi1.second /= radius.max();
224  rPhi2.first /= radius.min();
225  rPhi2.second /= radius.min();
226  phiRange = mergePhiRanges(rPhi1,rPhi2);
227  }
228 
229  constexpr float nSigmaRZ = 3.46410161514f; // std::sqrt(12.f); // ...and continue as before
230  constexpr float nSigmaPhi = 3.f;
231 
232  foundNodes.clear(); // Now recover hits in bounding box...
233  float prmin=phiRange.min(), prmax=phiRange.max();
234  if ((prmax-prmin) > Geom::ftwoPi())
235  { prmax=Geom::fpi(); prmin = -Geom::fpi();}
236  else
237  { while (prmax>maxphi) { prmin -= Geom::ftwoPi(); prmax -= Geom::ftwoPi();}
238  while (prmin<minphi) { prmin += Geom::ftwoPi(); prmax += Geom::ftwoPi();}
239  // This needs range -twoPi to +twoPi to work
240  }
241  if (barrelLayer)
242  {
243  Range regMax = predictionRZ.detRange();
244  Range regMin = predictionRZ(regMax.min()-regOffset);
245  regMax = predictionRZ(regMax.max()+regOffset);
246  correction.correctRZRange(regMin);
247  correction.correctRZRange(regMax);
248  if (regMax.min() < regMin.min()) { swap(regMax, regMin);}
249  KDTreeBox phiZ(prmin, prmax, regMin.min()-nSigmaRZ*rzError[il], regMax.max()+nSigmaRZ*rzError[il]);
250  hitTree[il].search(phiZ, foundNodes);
251  }
252  else
253  {
254  KDTreeBox phiZ(prmin, prmax,
255  rzRange.min()-regOffset-nSigmaRZ*rzError[il],
256  rzRange.max()+regOffset+nSigmaRZ*rzError[il]);
257  hitTree[il].search(phiZ, foundNodes);
258  }
259 
260  // std::cout << ip << ": " << theLayers[il].detLayer()->seqNum() << " " << foundNodes.size() << " " << prmin << " " << prmax << std::endl;
261 
262 
263  // int kk=0;
264  for (auto KDdata : foundNodes) {
265 
266  if (theMaxElement!=0 && result.size() >= theMaxElement){
267  result.clear();
268  edm::LogError("TooManyTriplets")<<" number of triples exceeds maximum. no triplets produced.";
269  return;
270  }
271 
272  float p3_u = hits.u[KDdata];
273  float p3_v = hits.v[KDdata];
274  float p3_phi = hits.lphi[KDdata];
275 
276  //if ((kk++)%100==0)
277  //std::cout << kk << ": " << p3_u << " " << p3_v << " " << p3_phi << std::endl;
278 
279 
280  Range allowed = predictionRZ(p3_u);
281  correction.correctRZRange(allowed);
282  float vErr = nSigmaRZ *hits.dv[KDdata];
283  Range hitRange(p3_v-vErr, p3_v+vErr);
284  Range crossingRange = allowed.intersection(hitRange);
285  if (crossingRange.empty()) continue;
286 
287  float ir = 1.f/hits.rv(KDdata);
288  float phiErr = nSigmaPhi * hits.drphi[KDdata]*ir;
289  for (int icharge=-1; icharge <=1; icharge+=2) {
290  Range rangeRPhi = predictionRPhi(hits.rv(KDdata), icharge);
291  correction.correctRPhiRange(rangeRPhi);
292  if (checkPhiInRange(p3_phi, rangeRPhi.first*ir-phiErr, rangeRPhi.second*ir+phiErr)) {
293  // insert here check with comparitor
294  OrderedHitTriplet hittriplet( doublets.hit(ip,HitDoublets::inner), doublets.hit(ip,HitDoublets::outer), hits.theHits[KDdata].hit());
295  if (!theComparitor || theComparitor->compatible(hittriplet,region) ) {
296  result.push_back( hittriplet );
297  } else {
298  LogDebug("RejectedTriplet") << "rejected triplet from comparitor ";
299  }
300  break;
301  }
302  }
303  }
304  }
305  }
306  // std::cout << "triplets " << result.size() << std::endl;
307 }
308 
309 bool PixelTripletHLTGenerator::checkPhiInRange(float phi, float phi1, float phi2) const
310 {
311  while (phi > phi2) phi -= Geom::ftwoPi();
312  while (phi < phi1) phi += Geom::ftwoPi();
313  return ( (phi1 <= phi) && (phi <= phi2) );
314 }
315 
316 std::pair<float,float> PixelTripletHLTGenerator::mergePhiRanges(const std::pair<float,float>& r1,
317  const std::pair<float,float>& r2) const
318 { float r2_min=r2.first;
319  float r2_max=r2.second;
320  while (r1.first-r2_min > Geom::fpi()) { r2_min += Geom::ftwoPi(); r2_max += Geom::ftwoPi();}
321  while (r1.first-r2_min < -Geom::fpi()) { r2_min -= Geom::ftwoPi(); r2_max -= Geom::ftwoPi(); }
322 
323  return std::make_pair(min(r1.first,r2_min),max(r1.second,r2_max));
324 }
#define LogDebug(id)
std::vector< SeedingLayerSetsHits::SeedingLayer > theLayers
float originRBound() const
bounds the particle vertex in the transverse plane
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
virtual HitPairGenerator * clone() const =0
void build(std::vector< KDTreeNodeInfo > &eltList, const KDTreeBox &region)
void initPropagator(const Propagator *propagator)
T perp() const
Definition: PV3DBase.h:72
GlobalPoint const & origin() const
void initLayer(const DetLayer *layer)
T y() const
Definition: PV3DBase.h:63
bool ev
float fpi()
Definition: Pi.h:35
void search(const KDTreeBox &searchBox, std::vector< KDTreeNodeInfo > &resRecHitList)
#define constexpr
std::pair< float, float > mergePhiRanges(const std::pair< float, float > &r1, const std::pair< float, float > &r2) const
std::unique_ptr< SeedComparitor > theComparitor
PixelTripletHLTGenerator(const edm::ParameterSet &cfg, edm::ConsumesCollector &iC)
int seqNum() const
Definition: DetLayer.h:36
T curvature(T InversePt, const edm::EventSetup &iSetup)
tuple result
Definition: query.py:137
double f[11][100]
virtual HitDoublets doublets(const TrackingRegion &reg, const edm::Event &ev, const edm::EventSetup &es)
T min(T a, T b)
Definition: MathUtil.h:58
SeedingHitSet::ConstRecHitPointer Hit
BaseTrackerRecHit const * Hit
bool isBarrel() const
Definition: DetLayer.h:32
PixelRecoRange< float > Range
void setSeedingLayers(SeedingLayerSetsHits::SeedingLayerSet pairLayers, std::vector< SeedingLayerSetsHits::SeedingLayer > thirdLayers) override
float ptMin() const
minimal pt of interest
PixelRecoRange< T > intersection(const PixelRecoRange< T > &r) const
bool checkPhiInRange(float phi, float phi1, float phi2) const
void initTolerance(float tolerance)
list pairs
sort tag files by run number
void init(const HitPairGenerator &pairs, LayerCacheType *layerCache) override
virtual unsigned int size() const
T x() const
Definition: PV3DBase.h:62
virtual void hitTriplets(const TrackingRegion &region, OrderedHitTriplets &trs, const edm::Event &ev, const edm::EventSetup &es)
DetLayer const * detLayer(layer l) const
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple size
Write out results.
virtual void setSeedingLayers(SeedingLayerSetsHits::SeedingLayerSet layers)=0
T get(const Candidate &c)
Definition: component.h:55
float ftwoPi()
Definition: Pi.h:36
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11
Definition: DDAxes.h:10