CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrajectoryCleanerMerger.cc
Go to the documentation of this file.
3 
4 #include <map>
5 #include <vector>
6 
7 using namespace std;
8 
10 
15 
18 
23 
26 
27 #include <fstream>
28 
29 /*****************************************************************************/
30 class HitComparator
31 {
32  public:
33  bool operator() (const TransientTrackingRecHit* ta,
34  const TransientTrackingRecHit* tb) const
35  {
36  const TrackingRecHit* a = ta->hit();
37  const TrackingRecHit* b = tb->hit();
38 
39  if(getId(a) < getId(b)) return true;
40  if(getId(b) < getId(a)) return false;
41 
42  if(a->geographicalId() < b->geographicalId()) return true;
43  if(b->geographicalId() < a->geographicalId()) return false;
44 
45  const SiPixelRecHit* a_ = dynamic_cast<const SiPixelRecHit*>(a);
46  if(a_ != 0)
47  {
48  const SiPixelRecHit* b_ = dynamic_cast<const SiPixelRecHit*>(b);
49  return less(a_, b_);
50  }
51  else
52  {
53  const SiStripMatchedRecHit2D* a_ =
54  dynamic_cast<const SiStripMatchedRecHit2D*>(a);
55 
56  if(a_ != 0)
57  {
58  const SiStripMatchedRecHit2D* b_ =
59  dynamic_cast<const SiStripMatchedRecHit2D*>(b);
60  return less(a_, b_);
61  }
62  else
63  {
64  const SiStripRecHit2D* a_ =
65  dynamic_cast<const SiStripRecHit2D*>(a);
66 
67  if(a_ != 0)
68  {
69  const SiStripRecHit2D* b_ =
70  dynamic_cast<const SiStripRecHit2D*>(b);
71  return less(a_, b_);
72  }
73  else
74  {
75  const ProjectedSiStripRecHit2D* a_ =
76  dynamic_cast<const ProjectedSiStripRecHit2D*>(a);
77 
78 //std::cerr << " comp proj" << std::endl;
79 
80  if(a_ != 0)
81  {
82  const ProjectedSiStripRecHit2D* b_ =
83  dynamic_cast<const ProjectedSiStripRecHit2D*>(b);
84 
85  return less(&(a_->originalHit()), &(b_->originalHit()));
86  }
87  else
88  return false;
89  }
90  }
91  }
92  }
93 
94  private:
95  int getId(const TrackingRecHit* a) const
96  {
97  if(dynamic_cast<const SiPixelRecHit*>(a) != 0) return 0;
98  if(dynamic_cast<const SiStripRecHit2D*>(a) != 0) return 1;
99  if(dynamic_cast<const SiStripMatchedRecHit2D*>(a) != 0) return 2;
100  if(dynamic_cast<const ProjectedSiStripRecHit2D*>(a) != 0) return 3;
101  return -1;
102  }
103 
104  bool less(const SiPixelRecHit* a,
105  const SiPixelRecHit* b) const
106  {
107 //std::cerr << " comp pixel" << std::endl;
108  return a->cluster() < b->cluster();
109  }
110 
111  bool less(const SiStripRecHit2D* a,
112  const SiStripRecHit2D *b) const
113  {
114 //std::cerr << " comp strip" << std::endl;
115  return a->cluster() < b->cluster();
116  }
117 
119  const SiStripMatchedRecHit2D *b) const
120  {
121 //std::cerr << " comp matched strip" << std::endl;
122  if(less(a->monoHit(), b->monoHit())) return true;
123  if(less(b->monoHit(), a->monoHit())) return false;
124 
125  if(less(a->stereoHit(), b->stereoHit())) return true;
126  return false;
127  }
128 };
129 
130 /*****************************************************************************/
132 {
133 }
134 
135 /*****************************************************************************/
137 {
138  std::vector<TrajectoryMeasurement> meas_ = traj.measurements();
139  std::vector<TrajectoryMeasurement> meas;
140 
141  for(std::vector<TrajectoryMeasurement>::iterator
142  im = meas_.begin();
143  im!= meas_.end(); im++)
144  if(im->recHit()->isValid())
145  meas.push_back(*im);
146 
147  bool changed;
148 
149  do
150  {
151  changed = false;
152 
153  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
154  im!= meas.end()-1; im++)
155  if( (*im).recHit()->globalPosition().mag2() >
156  (*(im+1)).recHit()->globalPosition().mag2() + 1e-6)
157  {
158  swap(*im,*(im+1));
159  changed = true;
160  }
161  }
162  while(changed);
163 
164  for(unsigned int i = 0 ; i < meas.size(); i++)
165  traj.pop();
166 
167  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
168  im!= meas.end(); im++)
169  traj.push(*im);
170 }
171 /*****************************************************************************/
173 {
174  if(s1.nHits() != s2.nHits()) return false;
175 
176  TrajectorySeed::range r1 = s1.recHits();
177  TrajectorySeed::range r2 = s2.recHits();
178 
179  TrajectorySeed::const_iterator h1 = r1.first;
180  TrajectorySeed::const_iterator h2 = r2.first;
181 
182  do
183  {
184  if(!(h1->sharesInput(&(*h2),TrackingRecHit::all)))
185  return false;
186 
187  h1++; h2++;
188  }
189  while(h1 != s1.recHits().second &&
190  h2 != s2.recHits().second);
191 
192  return true;
193 }
194 
195 /*****************************************************************************/
197 {
198  // PXB layer, ladder -> (layer - 1)<<2 + (ladder-1)%2
199  // PXF disk , panel
200  // TIB layer, module
201  // TOB layer, module
202  // TID wheel, ring
203  // TEC wheel, ring
204 
205  if(id.subdetId() == (unsigned int) PixelSubdetector::PixelBarrel)
206  { PXBDetId pid(id); return (100 * id.subdetId()+ ((pid.layer() - 1)<<1) + (pid.ladder() - 1)%2); }
207 
208  if(id.subdetId() == (unsigned int) PixelSubdetector::PixelEndcap)
209  { PXFDetId pid(id); return (100 * id.subdetId()+ ((pid.disk() - 1)<<1) + (pid.panel() - 1)%2); }
210 
211  if(id.subdetId() == StripSubdetector::TIB)
212  { TIBDetId pid(id); return (100 * id.subdetId()+ ((pid.layer() - 1)<<1) + (pid.module() - 1)%2); }
213  if(id.subdetId() == StripSubdetector::TOB)
214  { TOBDetId pid(id); return (100 * id.subdetId()+ ((pid.layer() - 1)<<1) + (pid.module() - 1)%2); }
215 
216  if(id.subdetId() == StripSubdetector::TID)
217  { TIDDetId pid(id); return (100 * id.subdetId()+ ((pid.wheel() - 1)<<1) + (pid.ring() - 1)%2); }
218  if(id.subdetId() == StripSubdetector::TEC)
219  { TECDetId pid(id); return (100 * id.subdetId()+ ((pid.wheel() - 1)<<1) + (pid.ring() - 1)%2); }
220 
221  return 0;
222 }
223 
224 /***************************************************************************/
225 
227  (TrajectoryContainer& trajs) const
228 {
229  if(trajs.size() == 0) return;
230 
231  // Fill the rechit map
232  typedef std::map<const TransientTrackingRecHit*,
233  std::vector<unsigned int>, HitComparator> RecHitMap;
234  RecHitMap recHitMap;
235 
236  std::vector<bool> keep(trajs.size(),true);
237 
238  for(unsigned int i = 0; i < trajs.size(); i++)
239  {
240  std::vector<TrajectoryMeasurement> meas = trajs[i].measurements();
241 
242  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
243  im!= meas.end(); im++)
244  if(im->recHit()->isValid())
245  {
246  const TransientTrackingRecHit* recHit = &(*(im->recHit()));
247  if(recHit->isValid())
248  recHitMap[recHit].push_back(i);
249  }
250  }
251 
252  // Look at each track
253  typedef std::map<unsigned int,int,less<unsigned int> > TrajMap;
254 
255  for(unsigned int i = 0; i < trajs.size(); i++)
256  if(keep[i])
257  {
258  TrajMap trajMap;
259  std::vector<DetId> detIds;
260  std::vector<int> detLayers;
261 
262  // Go trough all rechits of this track
263  std::vector<TrajectoryMeasurement> meas = trajs[i].measurements();
264  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
265  im!= meas.end(); im++)
266  {
267  if(im->recHit()->isValid())
268  {
269  // Get trajs sharing this rechit
270  const TransientTrackingRecHit* recHit = &(*(im->recHit()));
271  const std::vector<unsigned int>& sharing(recHitMap[recHit]);
272 
273  for(std::vector<unsigned int>::const_iterator j = sharing.begin();
274  j!= sharing.end(); j++)
275  if(i < *j) trajMap[*j]++;
276 
277  // Fill detLayers vector
278  detIds.push_back(recHit->geographicalId());
279  detLayers.push_back(getLayer(recHit->geographicalId()));
280  }
281  }
282 
283  // Check for trajs with shared rechits
284  for(TrajMap::iterator sharing = trajMap.begin();
285  sharing!= trajMap.end(); sharing++)
286  {
287  unsigned int j = (*sharing).first;
288  if(!keep[i] || !keep[j]) continue;
289 
290  // More than 50% shared
291  if((*sharing).second > min(trajs[i].foundHits(),
292  trajs[j].foundHits())/2)
293  {
294  if( sameSeed(trajs[i].seed(), trajs[j].seed()) )
295  {
296  bool hasCommonLayer = false;
297 
298 /*
299  std::vector<TrajectoryMeasurement> measi = trajs[i].measurements();
300  std::vector<TrajectoryMeasurement> measj = trajs[j].measurements();
301  for(std::vector<TrajectoryMeasurement>::iterator
302  tmj = measj.begin(); tmj!= measj.end(); tmj++)
303  if(find(measi.begin(), measi.end(), tmj) == measi.end())
304  if(find(detLayers.begin(),detLayers.end(),
305  getLayer(tmj->recHit()->geographicalId()))
306  != detLayers.end())
307  hasCommonLayer = true;
308 */
309 
310  if(hasCommonLayer == false)
311  { // merge tracks, add separate hits of the second to the first one
312  std::vector<TrajectoryMeasurement> measj = trajs[j].measurements();
313  for(std::vector<TrajectoryMeasurement>::iterator
314  tmj = measj.begin(); tmj!= measj.end(); tmj++)
315  if(tmj->recHit()->isValid())
316  {
317  bool match = false;
318 
319  std::vector<TrajectoryMeasurement> measi = trajs[i].measurements();
320  for(std::vector<TrajectoryMeasurement>::iterator
321  tmi = measi.begin(); tmi!= measi.end(); tmi++)
322  if(tmi->recHit()->isValid())
323  if(!HitComparator()(&(*(tmi->recHit())),
324  &(*(tmj->recHit()))) &&
325  !HitComparator()(&(*(tmj->recHit())),
326  &(*(tmi->recHit()))))
327  { match = true ; break; }
328 
329  if(!match)
330  trajs[i].push(*tmj);
331  }
332 
333  // Remove second track
334  keep[j] = false;
335  }
336  else
337  {
338  // remove track with higher impact / chi2
339  if(trajs[i].chiSquared() < trajs[j].chiSquared())
340  keep[j] = false;
341  else
342  keep[i] = false;
343  }
344  }
345  }
346  }
347  }
348 
349  // Final copy
350  int ok = 0;
351  for(unsigned int i = 0; i < trajs.size(); i++)
352  if(keep[i])
353  {
354  reOrderMeasurements(trajs[i]);
355  ok++;
356  }
357  else
358  trajs[i].invalidate();
359 
360  std::cerr << " [TrajecCleaner] cleaned trajs : " << ok << "/" << trajs.size() <<
361 " (with " << trajs[0].measurements().size() << "/" << recHitMap.size() << " hits)" << std::endl;
362 }
363 
int i
Definition: DBlmapReader.cc:9
unsigned int panel() const
panel id
Definition: PXFDetId.h:52
std::vector< Trajectory > TrajectoryContainer
unsigned int layer() const
layer id
Definition: TOBDetId.h:39
const SiStripRecHit2D * stereoHit() const
static unsigned int getId(void)
virtual const TrackingRecHit * hit() const =0
void reOrderMeasurements(Trajectory &traj) const
unsigned int ladder() const
ladder id
Definition: PXBDetId.h:39
#define min(a, b)
Definition: mlp_lapack.h:161
unsigned int layer() const
layer id
Definition: PXBDetId.h:35
tuple s2
Definition: indexGen.py:106
std::vector< Trajectory * > TrajectoryPointerContainer
bool less(const SiStripRecHit2D *a, const SiStripRecHit2D *b) const
DataContainer const & measurements() const
Definition: Trajectory.h:169
const int keep
int getId(const TrackingRecHit *a) const
bool sameSeed(const TrajectorySeed &s1, const TrajectorySeed &s2) const
recHitContainer::const_iterator const_iterator
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
unsigned int ring() const
ring id
Definition: TIDDetId.h:55
int j
Definition: DBlmapReader.cc:9
std::pair< const_iterator, const_iterator > range
unsigned int disk() const
disk id
Definition: PXFDetId.h:43
Definition: DetId.h:20
int getLayer(const DetId &id) const
unsigned int module() const
detector id
Definition: TIBDetId.h:61
void pop()
Definition: Trajectory.cc:17
ClusterRef const & cluster() const
range recHits() const
bool isValid() const
double b
Definition: hdecay.h:120
unsigned int wheel() const
wheel id
Definition: TECDetId.h:52
virtual void clean(TrajectoryPointerContainer &) const
unsigned int layer() const
layer id
Definition: TIBDetId.h:41
unsigned int nHits() const
double a
Definition: hdecay.h:121
unsigned int ring() const
ring id
Definition: TECDetId.h:71
unsigned int module() const
detector id
Definition: TOBDetId.h:58
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:6
DetId geographicalId() const
const SiStripRecHit2D * monoHit() const
ClusterRef const & cluster() const
Definition: SiPixelRecHit.h:42
bool less(const SiPixelRecHit *a, const SiPixelRecHit *b) const
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:35
const SiStripRecHit2D & originalHit() const
bool less(const SiStripMatchedRecHit2D *a, const SiStripMatchedRecHit2D *b) const
unsigned int wheel() const
wheel id
Definition: TIDDetId.h:50
Our base class.
Definition: SiPixelRecHit.h:27