CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackCleaner.cc
Go to the documentation of this file.
3 
5 
8 
12 
13 using namespace std;
14 using namespace pixeltrackfitting;
15 
16 /*****************************************************************************/
18 { // No access to geometry!
19  public:
20  HitComparatorByRadius(const TrackerTopology *tTopo) {tTopo_=tTopo;}
21  private:
23  public:
24  bool operator() (const TrackingRecHit* a, const TrackingRecHit* b) const
25  {
26  DetId i1 = a->geographicalId();
27  DetId i2 = b->geographicalId();
28 
29  bool isBarrel = (i1.subdetId() == int(PixelSubdetector::PixelBarrel));
30 
31  if(i1.subdetId() != i2.subdetId())
32  {
33  return isBarrel;
34  }
35  else
36  {
37  if(isBarrel)
38  {
39  int r1 = (tTopo_->pxbLayer(i1) - 1)*2 + (tTopo_->pxbLadder(i1) - 1)%2;
40  int r2 = (tTopo_->pxbLayer(i2) - 1)*2 + (tTopo_->pxbLadder(i2) - 1)%2;
41 
42  return (r1 < r2);
43  }
44  else
45  {
46  int r1 = (tTopo_->pxfDisk(i1) - 1)*2 + (tTopo_->pxfPanel(i1) - 1)%2;
47  int r2 = (tTopo_->pxfDisk(i2) - 1)*2 + (tTopo_->pxfPanel(i2) - 1)%2;
48 
49  return (r1 < r2);
50  }
51  }
52  }
53 };
54 
55 /*****************************************************************************/
57 {
58 public:
59  bool operator() (const TrackingRecHit* a, const TrackingRecHit* b) const
60  {
61  if(a->geographicalId() < b->geographicalId()) return true;
62  if(b->geographicalId() < a->geographicalId()) return false;
63 
64  if(a->localPosition().x() < b->localPosition().x() - 1e-5) return true;
65  if(b->localPosition().x() < a->localPosition().x() - 1e-5) return false;
66 
67  if(a->localPosition().y() < b->localPosition().y() - 1e-5) return true;
68  if(b->localPosition().y() < a->localPosition().y() - 1e-5) return false;
69 
70  return false;
71  }
72 };
73 
74 /*****************************************************************************/
76  (const edm::ParameterSet& ps)
77 {
78 }
79 
80 /*****************************************************************************/
82 {
83 }
84 
85 /*****************************************************************************/
87  const TrackingRecHit * b)
88 {
89  if(a->geographicalId() != b->geographicalId())
90  return false;
91 
92  if(fabs(a->localPosition().x() - b->localPosition().x()) < 1e-5 &&
93  fabs(a->localPosition().y() - b->localPosition().y()) < 1e-5)
94  return true;
95  else
96  return false;
97 }
98 
99 /*****************************************************************************/
101  const DetId & i2,
102  const TrackerTopology *tTopo)
103 {
104  // different subdet
105  if(i1.subdetId() != i2.subdetId()) return true;
106 
107  if(i1.subdetId() == int(PixelSubdetector::PixelBarrel))
108  { // barrel
109 
110  if(tTopo->pxbLayer(i1) != tTopo->pxbLayer(i2)) return true;
111 
112  int dphi = abs(int(tTopo->pxbLadder(i1) - tTopo->pxbLadder(i2)));
113  static int max[3] = {20, 32, 44};
114  if(dphi > max[tTopo->pxbLayer(i1)-1] / 2) dphi = max[tTopo->pxbLayer(i1)-1] - dphi;
115 
116  int dz = abs(int(tTopo->pxbModule(i1) - tTopo->pxbModule(i2)));
117 
118  if(dphi == 1 && dz <= 1) return true;
119  }
120  else
121  { // endcap
122 
123  if(tTopo->pxfSide(i1) != tTopo->pxfSide(i2) ||
124  tTopo->pxfDisk(i1) != tTopo->pxfDisk(i2)) return true;
125 
126  int dphi = abs(int(tTopo->pxfBlade(i1) - tTopo->pxfBlade(i2)));
127  static int max = 24;
128  if(dphi > max / 2) dphi = max - dphi;
129 
130  int dr = abs(int( ((tTopo->pxfModule(i1)-1) * 2 + (tTopo->pxfPanel(i1)-1)) -
131  ((tTopo->pxfModule(i2)-1) * 2 + (tTopo->pxfPanel(i2)-1)) ));
132 
133  if(dphi <= 1 && dr <= 1 && !(dphi == 0 && dr == 0)) return true;
134  }
135 
136  return false;
137 }
138 
139 /*****************************************************************************/
141  (const vector<const TrackingRecHit *>& recHitsA,
142  const vector<const TrackingRecHit *>& recHitsB,
143  const TrackerTopology *tTopo)
144 {
145  bool ok = true;
146 
147  for(vector<const TrackingRecHit *>::const_iterator
148  recHitA = recHitsA.begin(); recHitA!= recHitsA.end(); recHitA++)
149  for(vector<const TrackingRecHit *>::const_iterator
150  recHitB = recHitsB.begin(); recHitB!= recHitsB.end(); recHitB++)
151  if(!areSame(*recHitA,*recHitB))
152  if(!isCompatible((*recHitA)->geographicalId(),
153  (*recHitB)->geographicalId(),
154  tTopo))
155  ok = false;
156 
157  return ok;
158 }
159 
160 /*****************************************************************************/
163 {
164  // Local copy
166 
167  typedef map<const TrackingRecHit*,vector<unsigned int>,HitComparator>
168  RecHitMap;
169 
170  vector<bool> keep(tracks.size(),true);
171 
172  int changes;
173 
174  LogTrace("MinBiasTracking")
175  << " [TrackCleaner] initial tracks : " << tracks.size();
176 
177  for(unsigned int i = 0; i < tracks.size(); i++)
178  LogTrace("TrackCleaner")
179  << " Track #" << i << " : " << HitInfo::getInfo(tracks[i].second,tTopo);
180 
181  do
182  {
183  changes = 0;
184 
185  RecHitMap recHitMap;
186 
187 /*
188  LogTrace("MinBiasTracking")
189  << " [TrackCleaner] fill rechit map";
190 */
191 
192  // Fill the rechit map
193  for(unsigned int i = 0; i < tracks.size(); i++)
194  if(keep[i])
195  {
196  for(vector<const TrackingRecHit *>::const_iterator
197  recHit = tracks[i].second.begin();
198  recHit!= tracks[i].second.end(); recHit++)
199  recHitMap[*recHit].push_back(i);
200  }
201 
202  // Look at each track
203  typedef map<unsigned int,int,less<unsigned int> > TrackMap;
204 
205  for(unsigned int i = 0; i < tracks.size(); i++)
206  {
207  // Skip if 'i' already removed
208  if(!keep[i]) continue;
209 
210 /*
211 
212  bool addedNewHit = false;
213  do
214  {
215 */
216  TrackMap trackMap;
217 
218  // Go trough all rechits of this track
219  for(vector<const TrackingRecHit *>::const_iterator
220  recHit = tracks[i].second.begin();
221  recHit!= tracks[i].second.end(); recHit++)
222  {
223  // Get tracks sharing this rechit
224  vector<unsigned int> sharing = recHitMap[*recHit];
225 
226  for(vector<unsigned int>::iterator j = sharing.begin();
227  j!= sharing.end(); j++)
228  if(i < *j)
229  trackMap[*j]++;
230  }
231 
232  // Check for tracks with shared rechits
233  for(TrackMap::iterator sharing = trackMap.begin();
234  sharing!= trackMap.end(); sharing++)
235  {
236  unsigned int j = (*sharing).first;
237  if(!keep[i] || !keep[j]) continue;
238 
239  if(tracks[i].second.size() >=3)
240  { // triplet tracks
241  if((*sharing).second > min(int(tracks[i].second.size()),
242  int(tracks[j].second.size()))/2)
243  { // more than min(hits1,hits2)/2 rechits are shared
244  if(canBeMerged(tracks[i].second,tracks[j].second,tTopo))
245  { // no common layer
246  // merge tracks, add separate hits of the second to the first one
247  for(vector<const TrackingRecHit *>::const_iterator
248  recHit = tracks[j].second.begin();
249  recHit!= tracks[j].second.end(); recHit++)
250  {
251  bool ok = true;
252  for(vector<const TrackingRecHit *>::const_iterator
253  recHitA = tracks[i].second.begin();
254  recHitA!= tracks[i].second.end(); recHitA++)
255  if(areSame(*recHit,*recHitA)) ok = false;
256 
257  if(ok)
258  {
259  tracks[i].second.push_back(*recHit);
260  recHitMap[*recHit].push_back(i);
261 
262  sort(tracks[i].second.begin(),
263  tracks[i].second.end(),
264  HitComparatorByRadius(tTopo));
265 
266  //addedNewHit = true;
267  }
268  }
269 
270  LogTrace("TrackCleaner")
271  << " Merge #" << i << " #" << j
272  << ", first now has " << tracks[i].second.size();
273 
274  // Remove second track
275  keep[j] = false;
276 
277  changes++;
278  }
279  else
280  { // there is a common layer, keep smaller impact
281  if(fabs(tracks[i].first->d0())
282  < fabs(tracks[j].first->d0()))
283  keep[j] = false;
284  else
285  keep[i] = false;
286 
287  LogTrace("TrackCleaner")
288  << " Clash #" << i << " #" << j
289  << " keep lower d0 " << tracks[i].first->d0()
290  << " " << tracks[j].first->d0()
291  << ", keep #" << (keep[i] ? i : ( keep[j] ? j : 9999 ) );
292 
293  changes++;
294  }
295  }
296  else
297  { // note more than 50%, but at least two are shared
298  if((*sharing).second > 1)
299  {
300  if(tracks[i].second.size() != tracks[j].second.size())
301  { // keep longer
302  if(tracks[i].second.size() > tracks[j].second.size())
303  keep[j] = false; else keep[i] = false;
304  changes++;
305 
306  LogTrace("TrackCleaner")
307  << " Sharing " << (*sharing).second << " remove by size";
308  }
309  else
310  { // keep smaller impact
311  if(fabs(tracks[i].first->d0())
312  < fabs(tracks[j].first->d0()))
313  keep[j] = false; else keep[i] = false;
314  changes++;
315 
316  LogTrace("TrackCleaner")
317  << " Sharing " << (*sharing).second << " remove by d0";
318  }
319  }
320  }
321  }
322  else
323  { // pair tracks
324  if((*sharing).second > 0)
325  {
326  // Remove second track
327  keep[j] = false;
328 
329  changes++;
330  }
331  }
332  }
333 /*
334  }
335  while(addedNewHit);
336 */
337  }
338  }
339  while(changes > 0);
340 
341  // Final copy
342  TracksWithRecHits cleaned;
343 
344  for(unsigned int i = 0; i < tracks.size(); i++)
345  if(keep[i])
346  cleaned.push_back(tracks[i]);
347  else delete tracks_[i].first;
348 
349  LogTrace("MinBiasTracking")
350  << " [TrackCleaner] cleaned tracks : " << cleaned.size();
351 
352  for(unsigned int i = 0; i < cleaned.size(); i++)
353  LogTrace("TrackCleaner")
354  << " Track #" << i << " : " << HitInfo::getInfo(cleaned[i].second,tTopo);
355 
356  return cleaned;
357 }
358 
TrackCleaner(const edm::ParameterSet &ps)
Definition: TrackCleaner.cc:76
int i
Definition: DBlmapReader.cc:9
const std::vector< reco::PFCandidatePtr > & tracks_
unsigned int pxfDisk(const DetId &id) const
bool areSame(const TrackingRecHit *a, const TrackingRecHit *b)
Definition: TrackCleaner.cc:86
bool canBeMerged(const std::vector< const TrackingRecHit * > &recHitsA, const std::vector< const TrackingRecHit * > &recHitsB, const TrackerTopology *tTopo)
unsigned int pxbLadder(const DetId &id) const
T y() const
Definition: PV3DBase.h:63
unsigned int pxbModule(const DetId &id) const
virtual TracksWithRecHits cleanTracks(const TracksWithRecHits &tracksWithRecHits, const TrackerTopology *tTopo)
U second(std::pair< T, U > const &p)
const int keep
HitComparatorByRadius(const TrackerTopology *tTopo)
Definition: TrackCleaner.cc:20
const T & max(const T &a, const T &b)
virtual ~TrackCleaner()
Definition: TrackCleaner.cc:81
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
bool isCompatible(const DetId &i1, const DetId &i2, const TrackerTopology *tTopo)
bool first
Definition: L1TdeRCT.cc:75
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
#define LogTrace(id)
unsigned int pxfModule(const DetId &id) const
unsigned int pxbLayer(const DetId &id) const
Definition: DetId.h:18
tuple tracks
Definition: testEve_cfg.py:39
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
std::vector< TrackWithRecHits > TracksWithRecHits
unsigned int pxfSide(const DetId &id) const
DetId geographicalId() const
static std::string getInfo(const DetId &id, const TrackerTopology *tTopo)
Definition: HitInfo.cc:23
T x() const
Definition: PV3DBase.h:62
virtual LocalPoint localPosition() const =0
unsigned int pxfPanel(const DetId &id) const
unsigned int pxfBlade(const DetId &id) const
pixeltrackfitting::TracksWithRecHits TracksWithRecHits
const TrackerTopology * tTopo_
Definition: TrackCleaner.cc:22