CMS 3D CMS Logo

MuonSeedCleaner.cc
Go to the documentation of this file.
1 
8 
9 // Data Formats
18 
19 // Geometry
25 
26 // muon service
29 
30 // Framework
36 
37 // C++
38 #include <vector>
39 #include <deque>
40 #include <utility>
41 
42 //typedef std::pair<double, TrajectorySeed> seedpr ;
43 //static bool ptDecreasing(const seedpr s1, const seedpr s2) { return ( s1.first > s2.first ); }
44 static bool lengthSorting(const TrajectorySeed& s1, const TrajectorySeed& s2) { return (s1.nHits() > s2.nHits()); }
45 
46 /*
47  * Constructor
48  */
50  // Local Debug flag
51  debug = pset.getParameter<bool>("DebugMuonSeed");
52 
53  // muon service
54  edm::ParameterSet serviceParameters = pset.getParameter<edm::ParameterSet>("ServiceParameters");
55  theService = new MuonServiceProxy(serviceParameters, std::move(iC));
56 }
57 
58 /*
59  * Destructor
60  */
62  if (theService)
63  delete theService;
64 }
65 
66 /***********************************
67  *
68  * Seed Cleaner
69  *
70  ***********************************/
71 
72 std::vector<TrajectorySeed> MuonSeedCleaner::seedCleaner(const edm::EventSetup& eventSetup,
73  std::vector<TrajectorySeed>& seeds) {
75 
76  std::vector<TrajectorySeed> FinalSeeds;
77 
78  // group the seeds
79  std::vector<SeedContainer> theCollection = GroupSeeds(seeds);
80 
81  // ckeck each group and pick the good one
82  for (size_t i = 0; i < theCollection.size(); i++) {
83  // separate seeds w/ more than 1 segments and w/ 1st layer segment information
84  SeedContainer goodSeeds = SeedCandidates(theCollection[i], true);
85  SeedContainer otherSeeds = SeedCandidates(theCollection[i], false);
86  if (MomentumFilter(goodSeeds)) {
87  //std::cout<<" == type1 "<<std::endl;
88  TrajectorySeed bestSeed = Chi2LengthSelection(goodSeeds);
89  FinalSeeds.push_back(bestSeed);
90 
91  GlobalPoint seedgp = SeedPosition(bestSeed);
92  double eta = fabs(seedgp.eta());
93  if (goodSeeds.size() > 2 && eta > 1.5) {
94  TrajectorySeed anotherSeed = MoreRecHits(goodSeeds);
95  FinalSeeds.push_back(anotherSeed);
96  }
97  } else if (MomentumFilter(otherSeeds)) {
98  //std::cout<<" == type2 "<<std::endl;
99  TrajectorySeed bestSeed = MoreRecHits(otherSeeds);
100  FinalSeeds.push_back(bestSeed);
101 
102  GlobalPoint seedgp = SeedPosition(bestSeed);
103  double eta = fabs(seedgp.eta());
104  if (otherSeeds.size() > 2 && eta > 1.5) {
105  TrajectorySeed anotherSeed = LeanHighMomentum(otherSeeds);
106  FinalSeeds.push_back(anotherSeed);
107  }
108  } else {
109  //std::cout<<" == type3 "<<std::endl;
110  TrajectorySeed bestSeed = LeanHighMomentum(theCollection[i]);
111  FinalSeeds.push_back(bestSeed);
112 
113  GlobalPoint seedgp = SeedPosition(bestSeed);
114  double eta = fabs(seedgp.eta());
115  if (theCollection.size() > 2 && eta > 1.5) {
116  TrajectorySeed anotherSeed = BiggerCone(theCollection[i]);
117  FinalSeeds.push_back(anotherSeed);
118  }
119  }
120  }
121  return FinalSeeds;
122 }
123 
125  if (seeds.size() == 1)
126  return seeds[0];
127 
128  int winner = 0;
129  int moreHits = 0;
130  double bestChi2 = 99999.;
131  for (size_t i = 0; i < seeds.size(); i++) {
132  // 1. fill out the Nchi2 of segments of the seed
133  //GlobalVector mom = SeedMomentum( seeds[i] ); // temporary use for debugging
134  //double pt = sqrt( (mom.x()*mom.x()) + (mom.y()*mom.y()) );
135  //std::cout<<" > SEED"<<i<<" pt:"<<pt<< std::endl;
136 
137  double theChi2 = SeedChi2(seeds[i]);
138  double dChi2 = fabs(1. - (theChi2 / bestChi2));
139  int theHits = seeds[i].nHits();
140  int dHits = theHits - moreHits;
141  //std::cout<<" ----- "<<std::endl;
142 
143  // 2. better chi2
144  if (theChi2 < bestChi2 && dChi2 > 0.05) {
145  winner = static_cast<int>(i);
146  bestChi2 = theChi2;
147  moreHits = theHits;
148  }
149  // 3. if chi2 is not much better, pick more rechits one
150  if (theChi2 >= bestChi2 && dChi2 < 0.05 && dHits > 0) {
151  winner = static_cast<int>(i);
152  bestChi2 = theChi2;
153  moreHits = theHits;
154  }
155  }
156  //std::cout<<" Winner is "<< winner <<std::endl;
157  TrajectorySeed theSeed = seeds[winner];
158  seeds.erase(seeds.begin() + winner);
159  return theSeed;
160 }
161 
162 TrajectorySeed MuonSeedCleaner::BiggerCone(std::vector<TrajectorySeed>& seeds) {
163  if (seeds.size() == 1)
164  return seeds[0];
165 
166  float biggerProjErr = 9999.;
167  int winner = 0;
168  AlgebraicSymMatrix mat(5, 0);
169  for (size_t i = 0; i < seeds.size(); i++) {
170  auto r1 = seeds[i].recHits().begin();
171  mat = r1->parametersError().similarityT(r1->projectionMatrix());
172 
173  int NRecHits = NRecHitsFromSegment(*r1);
174 
175  float ddx = mat[1][1];
176  float ddy = mat[2][2];
177  float dxx = mat[3][3];
178  float dyy = mat[4][4];
179  float projectErr = sqrt((ddx * 10000.) + (ddy * 10000.) + dxx + dyy);
180 
181  if (NRecHits < 5)
182  continue;
183  if (projectErr < biggerProjErr)
184  continue;
185 
186  winner = static_cast<int>(i);
187  biggerProjErr = projectErr;
188  }
189  TrajectorySeed theSeed = seeds[winner];
190  seeds.erase(seeds.begin() + winner);
191  return theSeed;
192 }
193 
195  if (seeds.size() == 1)
196  return seeds[0];
197 
198  double highestPt = 0.;
199  int winner = 0;
200  for (size_t i = 0; i < seeds.size(); i++) {
202  double pt = sqrt((mom.x() * mom.x()) + (mom.y() * mom.y()));
203  if (pt > highestPt) {
204  winner = static_cast<int>(i);
205  highestPt = pt;
206  }
207  }
208  TrajectorySeed theSeed = seeds[winner];
209  seeds.erase(seeds.begin() + winner);
210  return theSeed;
211 }
212 
213 TrajectorySeed MuonSeedCleaner::MoreRecHits(std::vector<TrajectorySeed>& seeds) {
214  if (seeds.size() == 1)
215  return seeds[0];
216 
217  int winner = 0;
218  int moreHits = 0;
219  double betterChi2 = 99999.;
220  for (size_t i = 0; i < seeds.size(); i++) {
221  int theHits = 0;
222  for (auto const& r1 : seeds[i].recHits()) {
224  }
225 
226  double theChi2 = SeedChi2(seeds[i]);
227 
228  if (theHits == moreHits && theChi2 < betterChi2) {
229  betterChi2 = theChi2;
230  winner = static_cast<int>(i);
231  }
232  if (theHits > moreHits) {
233  moreHits = theHits;
234  betterChi2 = theChi2;
235  winner = static_cast<int>(i);
236  }
237  }
238  TrajectorySeed theSeed = seeds[winner];
239  seeds.erase(seeds.begin() + winner);
240  return theSeed;
241 }
242 
243 SeedContainer MuonSeedCleaner::LengthFilter(std::vector<TrajectorySeed>& seeds) {
244  SeedContainer longSeeds;
245  int NSegs = 0;
246  for (size_t i = 0; i < seeds.size(); i++) {
247  int theLength = static_cast<int>(seeds[i].nHits());
248  if (theLength > NSegs) {
249  NSegs = theLength;
250  longSeeds.clear();
251  longSeeds.push_back(seeds[i]);
252  } else if (theLength == NSegs) {
253  longSeeds.push_back(seeds[i]);
254  } else {
255  continue;
256  }
257  }
258  //std::cout<<" final Length :"<<NSegs<<std::endl;
259 
260  return longSeeds;
261 }
262 
263 bool MuonSeedCleaner::MomentumFilter(std::vector<TrajectorySeed>& seeds) {
264  bool findgoodMomentum = false;
265  SeedContainer goodMomentumSeeds = seeds;
266  seeds.clear();
267  for (size_t i = 0; i < goodMomentumSeeds.size(); i++) {
268  GlobalVector mom = SeedMomentum(goodMomentumSeeds[i]);
269  double pt = sqrt((mom.x() * mom.x()) + (mom.y() * mom.y()));
270  if (pt < 6. || pt > 2000.)
271  continue;
272  //if ( pt < 6. ) continue;
273  //std::cout<<" passed momentum :"<< pt <<std::endl;
274  seeds.push_back(goodMomentumSeeds[i]);
275  findgoodMomentum = true;
276  }
277  if (seeds.empty())
278  seeds = goodMomentumSeeds;
279 
280  return findgoodMomentum;
281 }
282 
283 SeedContainer MuonSeedCleaner::SeedCandidates(std::vector<TrajectorySeed>& seeds, bool good) {
284  SeedContainer theCandidate;
285  theCandidate.clear();
286 
287  bool longSeed = false;
288  bool withFirstLayer = false;
289 
290  //std::cout<<"***** Seed Classification *****"<< seeds.size() <<std::endl;
291  for (size_t i = 0; i < seeds.size(); i++) {
292  if (seeds[i].nHits() > 1)
293  longSeed = true;
294  //std::cout<<" Seed: "<<i<<" w/"<<seeds[i].nHits()<<" segs "<<std::endl;
295  // looking for 1st layer segment
296  int idx = 0;
297  for (auto const& r1 : seeds[i].recHits()) {
298  idx++;
299  const GeomDet* gdet = theService->trackingGeometry()->idToDet(r1.geographicalId());
300  DetId geoId = gdet->geographicalId();
301 
302  if (geoId.subdetId() == MuonSubdetId::DT) {
303  DTChamberId DT_Id(r1.geographicalId());
304  //std::cout<<" ID:"<<DT_Id <<" pos:"<< r1->localPosition() <<std::endl;
305  if (DT_Id.station() != 1)
306  continue;
307  withFirstLayer = true;
308  }
309  if (geoId.subdetId() == MuonSubdetId::CSC) {
310  idx++;
311  CSCDetId CSC_Id = CSCDetId(r1.geographicalId());
312  //std::cout<<" ID:"<<CSC_Id <<" pos:"<< r1->localPosition() <<std::endl;
313  if (CSC_Id.station() != 1)
314  continue;
315  withFirstLayer = true;
316  }
317  }
318  bool goodseed = (longSeed && withFirstLayer) ? true : false;
319 
320  if (goodseed == good)
321  theCandidate.push_back(seeds[i]);
322  }
323  return theCandidate;
324 }
325 
326 std::vector<SeedContainer> MuonSeedCleaner::GroupSeeds(std::vector<TrajectorySeed>& seeds) {
327  std::vector<SeedContainer> seedCollection;
328  seedCollection.clear();
329  std::vector<TrajectorySeed> theGroup;
330  std::vector<bool> usedSeed(seeds.size(), false);
331 
332  // categorize seeds by comparing overlapping segments or a certian eta-phi cone
333  for (size_t i = 0; i < seeds.size(); i++) {
334  if (usedSeed[i])
335  continue;
336  theGroup.push_back(seeds[i]);
337  usedSeed[i] = true;
338 
339  GlobalPoint pos1 = SeedPosition(seeds[i]);
340 
341  for (size_t j = i + 1; j < seeds.size(); j++) {
342  // 1.1 seeds with overlaaping segments will be grouped together
343  unsigned int overlapping = OverlapSegments(seeds[i], seeds[j]);
344  if (!usedSeed[j] && overlapping > 0) {
345  // reject the identical seeds
346  if (seeds[i].nHits() == overlapping && seeds[j].nHits() == overlapping) {
347  usedSeed[j] = true;
348  continue;
349  }
350  theGroup.push_back(seeds[j]);
351  usedSeed[j] = true;
352  }
353  if (usedSeed[j])
354  continue;
355 
356  // 1.2 seeds in a certain cone are grouped together
357  GlobalPoint pos2 = SeedPosition(seeds[j]);
358  double dh = pos1.eta() - pos2.eta();
359  double df = pos1.phi() - pos2.phi();
360  double dR = sqrt((dh * dh) + (df * df));
361 
362  if (dR > 0.3 && seeds[j].nHits() == 1)
363  continue;
364  if (dR > 0.2 && seeds[j].nHits() > 1)
365  continue;
366  theGroup.push_back(seeds[j]);
367  usedSeed[j] = true;
368  }
369  sort(theGroup.begin(), theGroup.end(), lengthSorting);
370  seedCollection.push_back(theGroup);
371  //std::cout<<" group "<<seedCollection.size() <<" w/"<< theGroup.size() <<" seeds"<<std::endl;
372  theGroup.clear();
373  }
374  return seedCollection;
375 }
376 
377 unsigned int MuonSeedCleaner::OverlapSegments(const TrajectorySeed& seed1, const TrajectorySeed& seed2) {
378  unsigned int overlapping = 0;
379  for (auto const& r1 : seed1.recHits()) {
380  DetId id1 = r1.geographicalId();
381  const GeomDet* gdet1 = theService->trackingGeometry()->idToDet(id1);
382  GlobalPoint gp1 = gdet1->toGlobal(r1.localPosition());
383 
384  for (auto const& r2 : seed2.recHits()) {
385  DetId id2 = r2.geographicalId();
386  if (id1 != id2)
387  continue;
388 
389  const GeomDet* gdet2 = theService->trackingGeometry()->idToDet(id2);
390  GlobalPoint gp2 = gdet2->toGlobal(r2.localPosition());
391 
392  double dx = gp1.x() - gp2.x();
393  double dy = gp1.y() - gp2.y();
394  double dz = gp1.z() - gp2.z();
395  double dL = sqrt(dx * dx + dy * dy + dz * dz);
396 
397  if (dL < 1.)
398  overlapping++;
399  }
400  }
401  return overlapping;
402 }
403 
405  double theChi2 = 0.;
406  for (auto const& r1 : seed.recHits()) {
407  //std::cout<<" segmet : "<<it <<std::endl;
408  theChi2 += NChi2OfSegment(r1);
409  }
410  theChi2 = theChi2 / seed.nHits();
411 
412  //std::cout<<" final Length :"<<NSegs<<std::endl;
413  return theChi2;
414 }
415 
417  int theHits = 0;
418  for (auto const& recHit : seed.recHits()) {
419  //std::cout<<" segmet : "<<it <<std::endl;
421  }
422 
423  //std::cout<<" final Length :"<<NSegs<<std::endl;
424  return theHits;
425 }
426 
428  PTrajectoryStateOnDet pTSOD = seed.startingState();
429  DetId SeedDetId(pTSOD.detId());
430  const GeomDet* geoDet = theService->trackingGeometry()->idToDet(SeedDetId);
431  TrajectoryStateOnSurface SeedTSOS =
432  trajectoryStateTransform::transientState(pTSOD, &(geoDet->surface()), &*theService->magneticField());
433  GlobalPoint pos = SeedTSOS.globalPosition();
434 
435  return pos;
436 }
437 
439  PTrajectoryStateOnDet pTSOD = seed.startingState();
440  DetId SeedDetId(pTSOD.detId());
441  const GeomDet* geoDet = theService->trackingGeometry()->idToDet(SeedDetId);
442  TrajectoryStateOnSurface SeedTSOS =
443  trajectoryStateTransform::transientState(pTSOD, &(geoDet->surface()), &*theService->magneticField());
444  GlobalVector mom = SeedTSOS.globalMomentum();
445 
446  return mom;
447 }
448 
450  int NRechits = 0;
451  const GeomDet* gdet = theService->trackingGeometry()->idToDet(rhit.geographicalId());
454 
455  DetId geoId = gdet->geographicalId();
456  if (geoId.subdetId() == MuonSubdetId::DT) {
457  DTChamberId DT_Id(rhit.geographicalId());
458  std::vector<TrackingRecHit*> DThits = theSeg->recHits();
459  int dt1DHits = 0;
460  for (size_t j = 0; j < DThits.size(); j++) {
461  dt1DHits += (DThits[j]->recHits()).size();
462  }
463  NRechits = dt1DHits;
464  }
465 
466  if (geoId.subdetId() == MuonSubdetId::CSC) {
467  NRechits = (theSeg->recHits()).size();
468  }
469  return NRechits;
470 }
471 
473  int NRechits = 0;
474  DetId geoId = rhit->geographicalId();
475  if (geoId.subdetId() == MuonSubdetId::DT) {
476  DTChamberId DT_Id(geoId);
477  std::vector<TrackingRecHit*> DThits = rhit->recHits();
478  int dt1DHits = 0;
479  for (size_t j = 0; j < DThits.size(); j++) {
480  dt1DHits += (DThits[j]->recHits()).size();
481  }
482  NRechits = dt1DHits;
483  //std::cout<<" D_rh("<< dt1DHits <<") " ;
484  }
485  if (geoId.subdetId() == MuonSubdetId::CSC) {
486  NRechits = (rhit->recHits()).size();
487  //std::cout<<" C_rh("<<(rhit->recHits()).size() <<") " ;
488  }
489  return NRechits;
490 }
491 
493  double NChi2 = 999999.;
494  const GeomDet* gdet = theService->trackingGeometry()->idToDet(rhit.geographicalId());
497 
498  double dof = static_cast<double>(theSeg->degreesOfFreedom());
499  NChi2 = theSeg->chi2() / dof;
500  //std::cout<<" Chi2 = "<< NChi2 <<" |" ;
501 
502  return NChi2;
503 }
size
Write out results.
uint8_t geoId(const VFATFrame &frame)
retrieve the GEO information for this channel
MuonSeedCleaner(const edm::ParameterSet &, edm::ConsumesCollector &&)
Constructor.
TrajectorySeed BiggerCone(std::vector< TrajectorySeed > &seeds)
select the seed with bigger projection cone to next layer
edm::ESHandle< GlobalTrackingGeometry > trackingGeometry() const
get the tracking geometry
edm::ESHandle< MagneticField > magneticField() const
get the magnetic field
static bool lengthSorting(const TrajectorySeed &s1, const TrajectorySeed &s2)
T z() const
Definition: PV3DBase.h:61
RecHitRange recHits() const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
T eta() const
Definition: PV3DBase.h:73
MuonServiceProxy * theService
std::vector< const TrackingRecHit * > recHits() const override
Access to component RecHits (if any)
GlobalPoint SeedPosition(const TrajectorySeed &seed)
retrieve seed global position
~MuonSeedCleaner()
Destructor.
double SeedChi2(const TrajectorySeed &seed)
const GeomDet * idToDet(DetId) const override
unsigned int OverlapSegments(const TrajectorySeed &seed1, const TrajectorySeed &seed2)
check overlapping segment for seeds
unsigned int detId() const
bool MomentumFilter(std::vector< TrajectorySeed > &seeds)
filter out the bad pt seeds, if all are bad pt seeds then keep all
double NChi2OfSegment(const TrackingRecHit &rhit)
retrieve number of rechits& normalized chi2 of associated segments of a seed
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
GlobalPoint globalPosition() const
std::shared_ptr< MuonTransientTrackingRecHit > MuonRecHitPointer
unsigned int nHits() const
T sqrt(T t)
Definition: SSEVec.h:19
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
TrajectorySeed LeanHighMomentum(std::vector< TrajectorySeed > &seeds)
select the highest momentum pt seed
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
virtual TrackingRecHit * clone() const =0
Definition: DetId.h:17
int NRecHitsFromSegment(const TrackingRecHit &rhit)
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
int station() const
Definition: CSCDetId.h:79
DetId geographicalId() const
std::vector< SeedContainer > GroupSeeds(std::vector< TrajectorySeed > &seeds)
group the seeds
SeedContainer SeedCandidates(std::vector< TrajectorySeed > &seeds, bool good)
pick the seeds w/ 1st layer information and w/ more than 1 segments
GlobalVector globalMomentum() const
SeedContainer LengthFilter(std::vector< TrajectorySeed > &seeds)
CLHEP::HepSymMatrix AlgebraicSymMatrix
GlobalVector SeedMomentum(const TrajectorySeed &seed)
retrieve seed global momentum
void update(const edm::EventSetup &setup, bool duringEvent=true)
update the services each event
std::vector< TrajectorySeed > seedCleaner(const edm::EventSetup &eventSetup, std::vector< TrajectorySeed > &seeds)
Cache pointer to geometry.
TrajectorySeed MoreRecHits(std::vector< TrajectorySeed > &seeds)
select the seed with more rechits
static constexpr int DT
Definition: MuonSubdetId.h:11
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
dh
Definition: cuy.py:354
std::vector< TrajectorySeed > SeedContainer
static constexpr int CSC
Definition: MuonSubdetId.h:12
static MuonRecHitPointer specificBuild(const GeomDet *geom, const TrackingRecHit *rh)
def move(src, dest)
Definition: eostools.py:511
int SeedLength(const TrajectorySeed &seed)
TrajectorySeed Chi2LengthSelection(std::vector< TrajectorySeed > &seeds)
select seed by balance length and chi2