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 
20 using namespace std;
22  //minimum number of hits per tracks
23 
24  theMinHits=conf.getParameter<int>("MinHits");
25  //cut on chi2
26  chi2cut=conf.getParameter<double>("Chi2Cut");
27  edm::LogInfo("CosmicTrackFinder")<<"Minimum number of hits "<<theMinHits<<" Cut on Chi2= "<<chi2cut;
28 
29  geometry=conf.getUntrackedParameter<std::string>("GeometricStructure","STANDARD");
30  theBuilderName = conf.getParameter<std::string>("TTRHBuilder");
31 }
32 
33 
35 }
36 
37 
38 void CosmicTrajectoryBuilder::init(const edm::EventSetup& es, bool seedplus){
39 
40  // FIXME: this is a memory leak generator
41 
42  //services
43  es.get<IdealMagneticFieldRecord>().get(magfield);
45 
46 
47 
48  if (seedplus) {
49  seed_plus=true;
50  thePropagator= new PropagatorWithMaterial(alongMomentum,0.1057,&(*magfield) );
51  thePropagatorOp= new PropagatorWithMaterial(oppositeToMomentum,0.1057,&(*magfield) );}
52  else {
53  seed_plus=false;
54  thePropagator= new PropagatorWithMaterial(oppositeToMomentum,0.1057,&(*magfield) );
55  thePropagatorOp= new PropagatorWithMaterial(alongMomentum,0.1057,&(*magfield) );
56  }
57 
58  theUpdator= new KFUpdator();
59  theEstimator= new Chi2MeasurementEstimator(chi2cut);
60 
61 
63  es.get<TransientRecHitRecord>().get(theBuilderName,theBuilder);
64 
65 
66  RHBuilder= theBuilder.product();
67  hitCloner = static_cast<TkTransientTrackingRecHitBuilder const *>(RHBuilder)->cloner();
68 
69 
70 
71  theFitter= new KFTrajectoryFitter(*thePropagator,
72  *theUpdator,
73  *theEstimator) ;
74  theFitter->setHitCloner(&hitCloner);
75 
76  theSmoother= new KFTrajectorySmoother(*thePropagatorOp,
77  *theUpdator,
78  *theEstimator);
79  theSmoother->setHitCloner(&hitCloner);
80 }
81 
83  const SiStripRecHit2DCollection &collstereo,
84  const SiStripRecHit2DCollection &collrphi ,
85  const SiStripMatchedRecHit2DCollection &collmatched,
86  const SiPixelRecHitCollection &collpixel,
87  const edm::EventSetup& es,
88  edm::Event& e,
89  vector<Trajectory> &trajoutput)
90 {
91 
92 
93 
94 
95  std::vector<Trajectory> trajSmooth;
96  std::vector<Trajectory>::iterator trajIter;
97 
98  TrajectorySeedCollection::const_iterator iseed;
99  unsigned int IS=0;
100  for(iseed=collseed.begin();iseed!=collseed.end();iseed++){
101  bool seedplus=((*iseed).direction()==alongMomentum);
102  init(es,seedplus);
103  hits.clear();
104  trajFit.clear();
105  trajSmooth.clear();
106  //order all the hits
107  vector<const TrackingRecHit*> allHits= SortHits(collstereo,collrphi,collmatched,collpixel,*iseed);
108  Trajectory startingTraj = createStartingTrajectory(*iseed);
109  AddHit(startingTraj,allHits);
110  for (trajIter=trajFit.begin(); trajIter!=trajFit.end();trajIter++){
111  trajSmooth=theSmoother->trajectories((*trajIter));
112  }
113  for (trajIter= trajSmooth.begin(); trajIter!=trajSmooth.end();trajIter++){
114  if((*trajIter).isValid()){
115  trajoutput.push_back((*trajIter));
116  }
117  }
118  delete theUpdator;
119  delete theEstimator;
120  delete thePropagator;
121  delete thePropagatorOp;
122  delete theFitter;
123  delete theSmoother;
124  //Only the first 30 seeds are considered
125  if (IS>30) return;
126  IS++;
127 
128  }
129 }
130 
132 {
133  Trajectory result( seed, seed.direction());
134  std::vector<TM> && seedMeas = seedMeasurements(seed);
135  for (auto & i : seedMeas) result.push(std::move(i));
136  return result;
137 }
138 
139 
140 std::vector<TrajectoryMeasurement>
142 {
143  std::vector<TrajectoryMeasurement> result;
144  TrajectorySeed::range hitRange = seed.recHits();
145  for (TrajectorySeed::const_iterator ihit = hitRange.first;
146  ihit != hitRange.second; ihit++) {
147  //RC TransientTrackingRecHit* recHit = RHBuilder->build(&(*ihit));
148  TransientTrackingRecHit::RecHitPointer recHit = RHBuilder->build(&(*ihit));
149  const GeomDet* hitGeomDet = (&(*tracker))->idToDet( ihit->geographicalId());
150  TSOS invalidState(new BasicSingleTrajectoryState( hitGeomDet->surface()));
151 
152  if (ihit == hitRange.second - 1) {
153  TSOS updatedState=startingTSOS(seed);
154  result.emplace_back(invalidState, updatedState, recHit);
155  }
156  else {
157  result.emplace_back(invalidState, recHit);
158  }
159 
160  }
161 
162  return result;
163 }
164 
165 
166 
167 
168 
169 vector<const TrackingRecHit*>
171  const SiStripRecHit2DCollection &collrphi ,
172  const SiStripMatchedRecHit2DCollection &collmatched,
173  const SiPixelRecHitCollection &collpixel,
174  const TrajectorySeed &seed){
175 
176 
177  //The Hits with global y more than the seed are discarded
178  //The Hits correspondign to the seed are discarded
179  //At the end all the hits are sorted in y
180  vector<const TrackingRecHit*> allHits;
181 
183  TrajectorySeed::range hRange= seed.recHits();
185  float yref=0.;
186  for (ihit = hRange.first;
187  ihit != hRange.second; ihit++) {
188  yref=RHBuilder->build(&(*ihit))->globalPosition().y();
189  hits.push_back((RHBuilder->build(&(*ihit))));
190  LogDebug("CosmicTrackFinder")<<"SEED HITS"<<RHBuilder->build(&(*ihit))->globalPosition();
191  }
192 
193 
195  for(ipix=collpixel.data().begin();ipix!=collpixel.data().end();ipix++){
196  float ych= RHBuilder->build(&(*ipix))->globalPosition().y();
197  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
198  allHits.push_back(&(*ipix));
199  }
200 
201  for(istrip=collrphi.data().begin();istrip!=collrphi.data().end();istrip++){
202  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
203  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
204  allHits.push_back(&(*istrip));
205  }
206 
207  for(istrip=collstereo.data().begin();istrip!=collstereo.data().end();istrip++){
208  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
209  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
210  allHits.push_back(&(*istrip));
211  }
212 
213  if (seed_plus)
214  stable_sort(allHits.begin(),allHits.end(),CompareHitY_plus(*tracker));
215  else
216  stable_sort(allHits.begin(),allHits.end(),CompareHitY(*tracker));
217 
218  return allHits;
219 }
220 
223 {
224  PTrajectoryStateOnDet pState( seed.startingState());
225  const GeomDet* gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
226  TSOS State= trajectoryStateTransform::transientState( pState, &(gdet->surface()),
227  &(*magfield));
228  return State;
229 
230 }
231 
233  const vector<const TrackingRecHit*>&Hits){
234 
235 
236  unsigned int icosm2;
237  unsigned int ibestdet;
238  float chi2min;
239  for (unsigned int icosmhit=0;icosmhit<Hits.size();icosmhit++){
240  GlobalPoint gphit=RHBuilder->build(Hits[icosmhit])->globalPosition();
241  unsigned int iraw= Hits[icosmhit]->geographicalId().rawId();
242  LogDebug("CosmicTrackFinder")<<" HIT POSITION "<< gphit;
243  //RC TransientTrackingRecHit* tmphit=RHBuilder->build(Hits[icosmhit]);
244  TransientTrackingRecHit::RecHitPointer tmphit=RHBuilder->build(Hits[icosmhit]);
245  TSOS prSt= thePropagator->propagate(traj.lastMeasurement().updatedState(),
246  tracker->idToDet(Hits[icosmhit]->geographicalId())->surface());
247 
248  //After propagating the trajectory to a detector,
249  //find the most compatible hit in the det
250  chi2min=20000000;
251  ibestdet=1000;
252  if (prSt.isValid()){
253  LocalPoint prLoc = prSt.localPosition();
254  LogDebug("CosmicTrackFinder") <<"STATE PROPAGATED AT DET "<<iraw<<" "<<prSt;
255  for(icosm2=icosmhit;icosm2<Hits.size();icosm2++){
256 
257  if (iraw==Hits[icosm2]->geographicalId().rawId()){
258  TransientTrackingRecHit::RecHitPointer tmphit=RHBuilder->build(Hits[icosm2]);
259  float contr= theEstimator->estimate(prSt, *tmphit).second;
260  if (contr<chi2min) {
261  chi2min=contr;
262  ibestdet=icosm2;
263  }
264  if (icosm2!=icosmhit) icosmhit++;
265 
266  }
267  else icosm2=Hits.size();
268  }
269 
270  if(chi2min<chi2cut)
271  LogDebug("CosmicTrackFinder")<<"Chi2 contribution for hit at "
272  <<RHBuilder->build(Hits[ibestdet])->globalPosition()
273  <<" is "<<chi2min;
274 
275  if(traj.foundHits()<3 &&(chi2min<chi2cut)){
276  //check on the first hit after the seed
277  GlobalVector ck=RHBuilder->build(Hits[ibestdet])->globalPosition()-
279  if ((abs(ck.x()/ck.y())>2)||(abs(ck.z()/ck.y())>2)) chi2min=300;
280  }
281  if (chi2min<chi2cut){
282  if ( abs(prLoc.x()) < 25 && abs(prLoc.y()) < 25 ){
283  TransientTrackingRecHit::RecHitPointer tmphitbestdet=RHBuilder->build(Hits[ibestdet]);
284  TSOS UpdatedState= theUpdator->update( prSt, *tmphitbestdet);
285  if (UpdatedState.isValid()){
286 
287  traj.push(std::move(TM(prSt,UpdatedState,RHBuilder->build(Hits[ibestdet])
288  , chi2min)));
289  LogDebug("CosmicTrackFinder") <<
290  "STATE UPDATED WITH HIT AT POSITION "
291  <<tmphitbestdet->globalPosition()
292  <<UpdatedState<<" "
293  <<traj.chiSquared();
294 
295  hits.push_back(tmphitbestdet);
296  }
297  }else LogDebug("CosmicTrackFinder")<<" Hits outside module surface "<< prLoc;
298  }else LogDebug("CosmicTrackFinder")<<" State can not be updated with hit at position " <<gphit;
299  }else LogDebug("CosmicTrackFinder")<<" State can not be propagated at det "<< iraw;
300  }
301 
302 
303 
304  if ( qualityFilter( traj)){
305  const TrajectorySeed& tmpseed=traj.seed();
306  if (thePropagatorOp->propagate(traj.lastMeasurement().updatedState(),
307  tracker->idToDet((*hits.begin())->geographicalId())->surface()).isValid()){
308  TSOS startingState=
309  thePropagatorOp->propagate(traj.lastMeasurement().updatedState(),
310  tracker->idToDet((*hits.begin())->geographicalId())->surface());
311 
312  trajFit = theFitter->fit(tmpseed,hits, startingState );
313  }
314  }
315 
316 }
317 
318 
319 bool
321  int ngoodhits=0;
322  if(geometry=="MTCC"){
323  auto hits = traj.recHits();
324  for(auto hit=hits.begin();hit!=hits.end();hit++){
325  unsigned int iid=(*hit)->hit()->geographicalId().rawId();
326  //CHECK FOR 3 hits r-phi
327  if(((iid>>0)&0x3)!=1) ngoodhits++;
328  }
329  }
330  else ngoodhits=traj.foundHits();
331 
332  if ( ngoodhits >= theMinHits) {
333  return true;
334  }
335  else {
336  return false;
337  }
338 }
#define LogDebug(id)
PropagationDirection direction() const
T getParameter(std::string const &) const
int foundHits() const
Definition: Trajectory.h:225
T getUntrackedParameter(std::string const &, T const &) const
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:285
int init
Definition: HydjetWrapper.h:67
T y() const
Definition: PV3DBase.h:63
GlobalPoint globalPosition() const
ConstRecHitContainer recHits() const
Definition: Trajectory.h:204
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:42
void init(const edm::EventSetup &es, bool)
std::vector< TrajectoryMeasurement > seedMeasurements(const TrajectorySeed &seed) const
std::vector< TrajectorySeed > TrajectorySeedCollection
recHitContainer::const_iterator const_iterator
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:174
T z() const
Definition: PV3DBase.h:64
void update(const LocalTrajectoryParameters &p, const SurfaceType &aSurface, const MagneticField *field, SurfaceSide side=SurfaceSideDefinition::atCenterOfSurface)
data_type const * data(size_t cell) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::pair< const_iterator, const_iterator > range
TSOS startingTSOS(const TrajectorySeed &seed) const
std::shared_ptr< TrackingRecHit const > RecHitPointer
Definition: DetId.h:18
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:187
PTrajectoryStateOnDet const & startingState() const
int iseed
Definition: AMPTWrapper.h:124
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
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.
const T & get() const
Definition: EventSetup.h:59
range recHits() const
float chiSquared() const
Definition: Trajectory.h:262
std::vector< const TrackingRecHit * > SortHits(const SiStripRecHit2DCollection &collstereo, const SiStripRecHit2DCollection &collrphi, const SiStripMatchedRecHit2DCollection &collmatched, const SiPixelRecHitCollection &collpixel, const TrajectorySeed &seed)
void AddHit(Trajectory &traj, const std::vector< const TrackingRecHit * > &Hits)
bool qualityFilter(const Trajectory &traj)
TrajectoryStateOnSurface const & updatedState() const
Trajectory createStartingTrajectory(const TrajectorySeed &seed) const
CosmicTrajectoryBuilder(const edm::ParameterSet &conf)
T x() const
Definition: PV3DBase.h:62
T const * product() const
Definition: ESHandle.h:86
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:50
def move(src, dest)
Definition: eostools.py:510