CMS 3D CMS Logo

CosmicTrajectoryBuilder.cc
Go to the documentation of this file.
1 //
2 // Package: RecoTracker/SingleTrackPattern
3 // Class: CosmicTrajectoryBuilder
4 // Original Author: Michele Pioppi-INFN perugia
5 #include <vector>
6 #include <iostream>
7 #include <cmath>
8 
19 using namespace std;
21  : magfieldToken_(iC.esConsumes()),
22  trackerToken_(iC.esConsumes()),
23  builderToken_(iC.esConsumes(edm::ESInputTag("", conf.getParameter<std::string>("TTRHBuilder")))) {
24  //minimum number of hits per tracks
25 
26  theMinHits = conf.getParameter<int>("MinHits");
27  //cut on chi2
28  chi2cut = conf.getParameter<double>("Chi2Cut");
29  edm::LogInfo("CosmicTrackFinder") << "Minimum number of hits " << theMinHits << " Cut on Chi2= " << chi2cut;
30 
31  geometry = conf.getUntrackedParameter<std::string>("GeometricStructure", "STANDARD");
32 }
33 
35 
36 void CosmicTrajectoryBuilder::init(const edm::EventSetup &es, bool seedplus) {
37  // FIXME: this is a memory leak generator
38 
39  //services
42 
43  if (seedplus) {
44  seed_plus = true;
47  } else {
48  seed_plus = false;
51  }
52 
53  theUpdator = new KFUpdator();
55 
57  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(RHBuilder)->cloner();
58 
59  theFitter = new KFTrajectoryFitter(*thePropagator, *theUpdator, *theEstimator);
60  theFitter->setHitCloner(&hitCloner);
61 
64 }
65 
67  const SiStripRecHit2DCollection &collstereo,
68  const SiStripRecHit2DCollection &collrphi,
69  const SiStripMatchedRecHit2DCollection &collmatched,
70  const SiPixelRecHitCollection &collpixel,
71  const edm::EventSetup &es,
72  edm::Event &e,
73  vector<Trajectory> &trajoutput) {
74  std::vector<Trajectory> trajSmooth;
75  std::vector<Trajectory>::iterator trajIter;
76 
77  TrajectorySeedCollection::const_iterator iseed;
78  unsigned int IS = 0;
79  for (iseed = collseed.begin(); iseed != collseed.end(); iseed++) {
80  bool seedplus = ((*iseed).direction() == alongMomentum);
81  init(es, seedplus);
82  hits.clear();
83  trajFit.clear();
84  trajSmooth.clear();
85  //order all the hits
86  vector<const TrackingRecHit *> allHits = SortHits(collstereo, collrphi, collmatched, collpixel, *iseed);
87  Trajectory startingTraj = createStartingTrajectory(*iseed);
88  AddHit(startingTraj, allHits);
89  for (trajIter = trajFit.begin(); trajIter != trajFit.end(); trajIter++) {
90  trajSmooth = theSmoother->trajectories((*trajIter));
91  }
92  for (trajIter = trajSmooth.begin(); trajIter != trajSmooth.end(); trajIter++) {
93  if ((*trajIter).isValid()) {
94  trajoutput.push_back((*trajIter));
95  }
96  }
97  delete theUpdator;
98  delete theEstimator;
99  delete thePropagator;
100  delete thePropagatorOp;
101  delete theFitter;
102  delete theSmoother;
103  //Only the first 30 seeds are considered
104  if (IS > 30)
105  return;
106  IS++;
107  }
108 }
109 
111  Trajectory result(seed, seed.direction());
112  std::vector<TM> &&seedMeas = seedMeasurements(seed);
113  for (auto &i : seedMeas)
114  result.push(std::move(i));
115  return result;
116 }
117 
118 std::vector<TrajectoryMeasurement> CosmicTrajectoryBuilder::seedMeasurements(const TrajectorySeed &seed) const {
119  std::vector<TrajectoryMeasurement> result;
120  auto const &hitRange = seed.recHits();
121  for (auto ihit = hitRange.begin(); ihit != hitRange.end(); ihit++) {
122  //RC TransientTrackingRecHit* recHit = RHBuilder->build(&(*ihit));
124  const GeomDet *hitGeomDet = (&(*tracker))->idToDet(ihit->geographicalId());
125  TSOS invalidState(new BasicSingleTrajectoryState(hitGeomDet->surface()));
126 
127  if (ihit == hitRange.end() - 1) {
128  TSOS updatedState = startingTSOS(seed);
129  result.emplace_back(invalidState, updatedState, recHit);
130  } else {
131  result.emplace_back(invalidState, recHit);
132  }
133  }
134 
135  return result;
136 }
137 
138 vector<const TrackingRecHit *> CosmicTrajectoryBuilder::SortHits(const SiStripRecHit2DCollection &collstereo,
139  const SiStripRecHit2DCollection &collrphi,
140  const SiStripMatchedRecHit2DCollection &collmatched,
141  const SiPixelRecHitCollection &collpixel,
142  const TrajectorySeed &seed) {
143  //The Hits with global y more than the seed are discarded
144  //The Hits correspondign to the seed are discarded
145  //At the end all the hits are sorted in y
146  vector<const TrackingRecHit *> allHits;
147 
149  float yref = 0.;
150  for (auto const &recHit : seed.recHits()) {
151  yref = RHBuilder->build(&recHit)->globalPosition().y();
152  hits.push_back((RHBuilder->build(&recHit)));
153  LogDebug("CosmicTrackFinder") << "SEED HITS" << RHBuilder->build(&recHit)->globalPosition();
154  }
155 
157  for (ipix = collpixel.data().begin(); ipix != collpixel.data().end(); ipix++) {
158  float ych = RHBuilder->build(&(*ipix))->globalPosition().y();
159  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
160  allHits.push_back(&(*ipix));
161  }
162 
163  for (istrip = collrphi.data().begin(); istrip != collrphi.data().end(); istrip++) {
164  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
165  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
166  allHits.push_back(&(*istrip));
167  }
168 
169  for (istrip = collstereo.data().begin(); istrip != collstereo.data().end(); istrip++) {
170  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
171  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
172  allHits.push_back(&(*istrip));
173  }
174 
175  if (seed_plus)
176  stable_sort(allHits.begin(), allHits.end(), CompareHitY_plus(*tracker));
177  else
178  stable_sort(allHits.begin(), allHits.end(), CompareHitY(*tracker));
179 
180  return allHits;
181 }
182 
184  PTrajectoryStateOnDet pState(seed.startingState());
185  const GeomDet *gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
186  TSOS State = trajectoryStateTransform::transientState(pState, &(gdet->surface()), &(*magfield));
187  return State;
188 }
189 
190 void CosmicTrajectoryBuilder::AddHit(Trajectory &traj, const vector<const TrackingRecHit *> &Hits) {
191  unsigned int icosm2;
192  unsigned int ibestdet;
193  float chi2min;
194  for (unsigned int icosmhit = 0; icosmhit < Hits.size(); icosmhit++) {
195  GlobalPoint gphit = RHBuilder->build(Hits[icosmhit])->globalPosition();
196  unsigned int iraw = Hits[icosmhit]->geographicalId().rawId();
197  LogDebug("CosmicTrackFinder") << " HIT POSITION " << gphit;
198  //RC TransientTrackingRecHit* tmphit=RHBuilder->build(Hits[icosmhit]);
199  TransientTrackingRecHit::RecHitPointer tmphit = RHBuilder->build(Hits[icosmhit]);
201  tracker->idToDet(Hits[icosmhit]->geographicalId())->surface());
202 
203  //After propagating the trajectory to a detector,
204  //find the most compatible hit in the det
205  chi2min = 20000000;
206  ibestdet = 1000;
207  if (prSt.isValid()) {
208  LocalPoint prLoc = prSt.localPosition();
209  LogDebug("CosmicTrackFinder") << "STATE PROPAGATED AT DET " << iraw << " " << prSt;
210  for (icosm2 = icosmhit; icosm2 < Hits.size(); icosm2++) {
211  if (iraw == Hits[icosm2]->geographicalId().rawId()) {
213  float contr = theEstimator->estimate(prSt, *tmphit).second;
214  if (contr < chi2min) {
215  chi2min = contr;
216  ibestdet = icosm2;
217  }
218  if (icosm2 != icosmhit)
219  icosmhit++;
220 
221  } else
222  icosm2 = Hits.size();
223  }
224 
225  if (chi2min < chi2cut)
226  LogDebug("CosmicTrackFinder") << "Chi2 contribution for hit at "
227  << RHBuilder->build(Hits[ibestdet])->globalPosition() << " is " << chi2min;
228 
229  if (traj.foundHits() < 3 && (chi2min < chi2cut)) {
230  //check on the first hit after the seed
231  GlobalVector ck = RHBuilder->build(Hits[ibestdet])->globalPosition() -
233  if ((abs(ck.x() / ck.y()) > 2) || (abs(ck.z() / ck.y()) > 2))
234  chi2min = 300;
235  }
236  if (chi2min < chi2cut) {
237  if (abs(prLoc.x()) < 25 && abs(prLoc.y()) < 25) {
238  TransientTrackingRecHit::RecHitPointer tmphitbestdet = RHBuilder->build(Hits[ibestdet]);
239  TSOS UpdatedState = theUpdator->update(prSt, *tmphitbestdet);
240  if (UpdatedState.isValid()) {
241  traj.push(TM(prSt, UpdatedState, RHBuilder->build(Hits[ibestdet]), chi2min));
242  LogDebug("CosmicTrackFinder") << "STATE UPDATED WITH HIT AT POSITION " << tmphitbestdet->globalPosition()
243  << UpdatedState << " " << traj.chiSquared();
244 
245  hits.push_back(tmphitbestdet);
246  }
247  } else
248  LogDebug("CosmicTrackFinder") << " Hits outside module surface " << prLoc;
249  } else
250  LogDebug("CosmicTrackFinder") << " State can not be updated with hit at position " << gphit;
251  } else
252  LogDebug("CosmicTrackFinder") << " State can not be propagated at det " << iraw;
253  }
254 
255  if (qualityFilter(traj)) {
256  const TrajectorySeed &tmpseed = traj.seed();
257  if (thePropagatorOp
258  ->propagate(traj.lastMeasurement().updatedState(),
259  tracker->idToDet((*hits.begin())->geographicalId())->surface())
260  .isValid()) {
261  TSOS startingState = thePropagatorOp->propagate(traj.lastMeasurement().updatedState(),
262  tracker->idToDet((*hits.begin())->geographicalId())->surface());
263 
264  trajFit = theFitter->fit(tmpseed, hits, startingState);
265  }
266  }
267 }
268 
270  int ngoodhits = 0;
271  if (geometry == "MTCC") {
272  auto hits = traj.recHits();
273  for (auto hit = hits.begin(); hit != hits.end(); hit++) {
274  unsigned int iid = (*hit)->hit()->geographicalId().rawId();
275  //CHECK FOR 3 hits r-phi
276  if (((iid >> 0) & 0x3) != 1)
277  ngoodhits++;
278  }
279  } else
280  ngoodhits = traj.foundHits();
281 
282  if (ngoodhits >= theMinHits) {
283  return true;
284  } else {
285  return false;
286  }
287 }
void AddHit(Trajectory &traj, const std::vector< const TrackingRecHit *> &Hits)
KFTrajectoryFitter * theFitter
KFTrajectorySmoother * theSmoother
CosmicTrajectoryBuilder(const edm::ParameterSet &conf, edm::ConsumesCollector iC)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
const MagneticField * magfield
virtual TrajectoryContainer trajectories(const Trajectory &traj) const
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
const bool isValid(const Frame &aFrame, const FrameQuality &aQuality, const uint16_t aExpectedPos)
Trajectory createStartingTrajectory(const TrajectorySeed &seed) const
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerToken_
T z() const
Definition: PV3DBase.h:61
PropagatorWithMaterial * thePropagatorOp
data_type const * data(size_t cell) const
float chiSquared() const
Definition: Trajectory.h:241
const TrackerGeometry * tracker
std::vector< Trajectory > trajFit
int foundHits() const
Definition: Trajectory.h:206
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:150
T getUntrackedParameter(std::string const &, T const &) const
void init(const edm::EventSetup &es, bool)
const TransientTrackingRecHitBuilder * RHBuilder
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
const edm::ESGetToken< TransientTrackingRecHitBuilder, TransientRecHitRecord > builderToken_
GlobalPoint globalPosition() const
TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TrackingRecHit &) const override
Definition: KFUpdator.cc:177
std::vector< TrajectorySeed > TrajectorySeedCollection
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
ConstRecHitContainer recHits() const
Definition: Trajectory.h:186
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< TrajectoryMeasurement > seedMeasurements(const TrajectorySeed &seed) const
const TrackerGeomDet * idToDet(DetId) const override
std::shared_ptr< TrackingRecHit const > RecHitPointer
Log< level::Info, false > LogInfo
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetId.h:17
TSOS startingTSOS(const TrajectorySeed &seed) const
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
int iseed
Definition: AMPTWrapper.h:134
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:263
void run(const TrajectorySeedCollection &collseed, const SiStripRecHit2DCollection &collstereo, const SiStripRecHit2DCollection &collrphi, const SiStripMatchedRecHit2DCollection &collmatched, const SiPixelRecHitCollection &collpixel, const edm::EventSetup &es, edm::Event &e, std::vector< Trajectory > &trajoutput)
Runs the algorithm.
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
PropagatorWithMaterial * thePropagator
TrajectoryStateOnSurface const & updatedState() const
std::vector< const TrackingRecHit * > SortHits(const SiStripRecHit2DCollection &collstereo, const SiStripRecHit2DCollection &collrphi, const SiStripMatchedRecHit2DCollection &collmatched, const SiPixelRecHitCollection &collpixel, const TrajectorySeed &seed)
Chi2MeasurementEstimator * theEstimator
HLT enums.
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:166
bool qualityFilter(const Trajectory &traj)
void setHitCloner(TkCloner const *hc) override
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magfieldToken_
TransientTrackingRecHit::RecHitContainer hits
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:50
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)