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(a->monoClusterRef() < b->monoClusterRef()) return true;
123  if(b->monoClusterRef() < a->monoClusterRef()) return false;
124  if(a->stereoClusterRef() < b->stereoClusterRef()) return true;
125  return false;
126  }
127 };
128 
129 /*****************************************************************************/
131 {
132 }
133 
134 /*****************************************************************************/
136 {
137  std::vector<TrajectoryMeasurement> meas_ = traj.measurements();
138  std::vector<TrajectoryMeasurement> meas;
139 
140  for(std::vector<TrajectoryMeasurement>::iterator
141  im = meas_.begin();
142  im!= meas_.end(); im++)
143  if(im->recHit()->isValid())
144  meas.push_back(*im);
145 
146  bool changed;
147 
148  do
149  {
150  changed = false;
151 
152  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
153  im!= meas.end()-1; im++)
154  if( (*im).recHit()->globalPosition().mag2() >
155  (*(im+1)).recHit()->globalPosition().mag2() + 1e-6)
156  {
157  swap(*im,*(im+1));
158  changed = true;
159  }
160  }
161  while(changed);
162 
163  for(unsigned int i = 0 ; i < meas.size(); i++)
164  traj.pop();
165 
166  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
167  im!= meas.end(); im++)
168  traj.push(*im);
169 }
170 /*****************************************************************************/
172 {
173  if(s1.nHits() != s2.nHits()) return false;
174 
177 
178  TrajectorySeed::const_iterator h1 = r1.first;
179  TrajectorySeed::const_iterator h2 = r2.first;
180 
181  do
182  {
183  if(!(h1->sharesInput(&(*h2),TrackingRecHit::all)))
184  return false;
185 
186  h1++; h2++;
187  }
188  while(h1 != s1.recHits().second &&
189  h2 != s2.recHits().second);
190 
191  return true;
192 }
193 
194 /*****************************************************************************/
196 {
197  // PXB layer, ladder -> (layer - 1)<<2 + (ladder-1)%2
198  // PXF disk , panel
199  // TIB layer, module
200  // TOB layer, module
201  // TID wheel, ring
202  // TEC wheel, ring
203 
204  if(id.subdetId() == (unsigned int) PixelSubdetector::PixelBarrel)
205  { PXBDetId pid(id); return (100 * id.subdetId()+ ((pid.layer() - 1)<<1) + (pid.ladder() - 1)%2); }
206 
207  if(id.subdetId() == (unsigned int) PixelSubdetector::PixelEndcap)
208  { PXFDetId pid(id); return (100 * id.subdetId()+ ((pid.disk() - 1)<<1) + (pid.panel() - 1)%2); }
209 
210  if(id.subdetId() == StripSubdetector::TIB)
211  { TIBDetId pid(id); return (100 * id.subdetId()+ ((pid.layer() - 1)<<1) + (pid.module() - 1)%2); }
212  if(id.subdetId() == StripSubdetector::TOB)
213  { TOBDetId pid(id); return (100 * id.subdetId()+ ((pid.layer() - 1)<<1) + (pid.module() - 1)%2); }
214 
215  if(id.subdetId() == StripSubdetector::TID)
216  { TIDDetId pid(id); return (100 * id.subdetId()+ ((pid.wheel() - 1)<<1) + (pid.ring() - 1)%2); }
217  if(id.subdetId() == StripSubdetector::TEC)
218  { TECDetId pid(id); return (100 * id.subdetId()+ ((pid.wheel() - 1)<<1) + (pid.ring() - 1)%2); }
219 
220  return 0;
221 }
222 
223 /***************************************************************************/
224 
226  (TrajectoryContainer& trajs) const
227 {
228  if(trajs.size() == 0) return;
229 
230  // Fill the rechit map
231  typedef std::map<const TransientTrackingRecHit*,
232  std::vector<unsigned int>, HitComparator> RecHitMap;
233  RecHitMap recHitMap;
234 
235  std::vector<bool> keep(trajs.size(),true);
236 
237  for(unsigned int i = 0; i < trajs.size(); i++)
238  {
239  std::vector<TrajectoryMeasurement> meas = trajs[i].measurements();
240 
241  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
242  im!= meas.end(); im++)
243  if(im->recHit()->isValid())
244  {
245  const TransientTrackingRecHit* recHit = &(*(im->recHit()));
246  if(recHit->isValid())
247  recHitMap[recHit].push_back(i);
248  }
249  }
250 
251  // Look at each track
252  typedef std::map<unsigned int,int,less<unsigned int> > TrajMap;
253 
254  for(unsigned int i = 0; i < trajs.size(); i++)
255  if(keep[i])
256  {
257  TrajMap trajMap;
258  std::vector<DetId> detIds;
259  std::vector<int> detLayers;
260 
261  // Go trough all rechits of this track
262  std::vector<TrajectoryMeasurement> meas = trajs[i].measurements();
263  for(std::vector<TrajectoryMeasurement>::iterator im = meas.begin();
264  im!= meas.end(); im++)
265  {
266  if(im->recHit()->isValid())
267  {
268  // Get trajs sharing this rechit
269  const TransientTrackingRecHit* recHit = &(*(im->recHit()));
270  const std::vector<unsigned int>& sharing(recHitMap[recHit]);
271 
272  for(std::vector<unsigned int>::const_iterator j = sharing.begin();
273  j!= sharing.end(); j++)
274  if(i < *j) trajMap[*j]++;
275 
276  // Fill detLayers vector
277  detIds.push_back(recHit->geographicalId());
278  detLayers.push_back(getLayer(recHit->geographicalId()));
279  }
280  }
281 
282  // Check for trajs with shared rechits
283  for(TrajMap::iterator sharing = trajMap.begin();
284  sharing!= trajMap.end(); sharing++)
285  {
286  unsigned int j = (*sharing).first;
287  if(!keep[i] || !keep[j]) continue;
288 
289  // More than 50% shared
290  if((*sharing).second > min(trajs[i].foundHits(),
291  trajs[j].foundHits())/2)
292  {
293  if( sameSeed(trajs[i].seed(), trajs[j].seed()) )
294  {
295  bool hasCommonLayer = false;
296 
297 /*
298  std::vector<TrajectoryMeasurement> measi = trajs[i].measurements();
299  std::vector<TrajectoryMeasurement> measj = trajs[j].measurements();
300  for(std::vector<TrajectoryMeasurement>::iterator
301  tmj = measj.begin(); tmj!= measj.end(); tmj++)
302  if(find(measi.begin(), measi.end(), tmj) == measi.end())
303  if(find(detLayers.begin(),detLayers.end(),
304  getLayer(tmj->recHit()->geographicalId()))
305  != detLayers.end())
306  hasCommonLayer = true;
307 */
308 
309  if(hasCommonLayer == false)
310  { // merge tracks, add separate hits of the second to the first one
311  std::vector<TrajectoryMeasurement> measj = trajs[j].measurements();
312  for(std::vector<TrajectoryMeasurement>::iterator
313  tmj = measj.begin(); tmj!= measj.end(); tmj++)
314  if(tmj->recHit()->isValid())
315  {
316  bool match = false;
317 
318  std::vector<TrajectoryMeasurement> measi = trajs[i].measurements();
319  for(std::vector<TrajectoryMeasurement>::iterator
320  tmi = measi.begin(); tmi!= measi.end(); tmi++)
321  if(tmi->recHit()->isValid())
322  if(!HitComparator()(&(*(tmi->recHit())),
323  &(*(tmj->recHit()))) &&
324  !HitComparator()(&(*(tmj->recHit())),
325  &(*(tmi->recHit()))))
326  { match = true ; break; }
327 
328  if(!match)
329  trajs[i].push(*tmj);
330  }
331 
332  // Remove second track
333  keep[j] = false;
334  }
335  else
336  {
337  // remove track with higher impact / chi2
338  if(trajs[i].chiSquared() < trajs[j].chiSquared())
339  keep[j] = false;
340  else
341  keep[i] = false;
342  }
343  }
344  }
345  }
346  }
347 
348  // Final copy
349  int ok = 0;
350  for(unsigned int i = 0; i < trajs.size(); i++)
351  if(keep[i])
352  {
353  reOrderMeasurements(trajs[i]);
354  ok++;
355  }
356  else
357  trajs[i].invalidate();
358 
359  std::cerr << " [TrajecCleaner] cleaned trajs : " << ok << "/" << trajs.size() <<
360 " (with " << trajs[0].measurements().size() << "/" << recHitMap.size() << " hits)" << std::endl;
361 }
362 
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
OmniClusterRef const & stereoClusterRef() 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:203
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
ClusterRef cluster() const
OmniClusterRef const & monoClusterRef() const
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 cluster() const
Definition: SiPixelRecHit.h:41
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
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:22