CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
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 
194  if ((&collpixel)!=0){
196  for(ipix=collpixel.data().begin();ipix!=collpixel.data().end();ipix++){
197  float ych= RHBuilder->build(&(*ipix))->globalPosition().y();
198  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
199  allHits.push_back(&(*ipix));
200  }
201  }
202 
203 
204 
205  if ((&collrphi)!=0){
206  for(istrip=collrphi.data().begin();istrip!=collrphi.data().end();istrip++){
207  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
208  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
209  allHits.push_back(&(*istrip));
210  }
211  }
212 
213 
214 
215 
216  if ((&collstereo)!=0){
217  for(istrip=collstereo.data().begin();istrip!=collstereo.data().end();istrip++){
218  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
219  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
220  allHits.push_back(&(*istrip));
221  }
222  }
223 
224 // SiStripMatchedRecHit2DCollection::DataContainer::const_iterator istripm;
225 // if ((&collmatched)!=0){
226 // for(istripm=collmatched.data().begin();istripm!=collmatched.data().end();istripm++){
227 // float ych= RHBuilder->build(&(*istripm))->globalPosition().y();
228 // if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
229 // allHits.push_back(&(*istripm));
230 // }
231 // }
232 
233  if (seed_plus){
234  stable_sort(allHits.begin(),allHits.end(),CompareHitY_plus(*tracker));
235  }
236  else {
237  stable_sort(allHits.begin(),allHits.end(),CompareHitY(*tracker));
238  }
239 
240  return allHits;
241 }
242 
245 {
246  PTrajectoryStateOnDet pState( seed.startingState());
247  const GeomDet* gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
248  TSOS State= trajectoryStateTransform::transientState( pState, &(gdet->surface()),
249  &(*magfield));
250  return State;
251 
252 }
253 
255  const vector<const TrackingRecHit*>&Hits){
256 
257 
258  unsigned int icosm2;
259  unsigned int ibestdet;
260  float chi2min;
261  for (unsigned int icosmhit=0;icosmhit<Hits.size();icosmhit++){
262  GlobalPoint gphit=RHBuilder->build(Hits[icosmhit])->globalPosition();
263  unsigned int iraw= Hits[icosmhit]->geographicalId().rawId();
264  LogDebug("CosmicTrackFinder")<<" HIT POSITION "<< gphit;
265  //RC TransientTrackingRecHit* tmphit=RHBuilder->build(Hits[icosmhit]);
266  TransientTrackingRecHit::RecHitPointer tmphit=RHBuilder->build(Hits[icosmhit]);
267  TSOS prSt= thePropagator->propagate(traj.lastMeasurement().updatedState(),
268  tracker->idToDet(Hits[icosmhit]->geographicalId())->surface());
269 
270  //After propagating the trajectory to a detector,
271  //find the most compatible hit in the det
272  chi2min=20000000;
273  ibestdet=1000;
274  if (prSt.isValid()){
275  LocalPoint prLoc = prSt.localPosition();
276  LogDebug("CosmicTrackFinder") <<"STATE PROPAGATED AT DET "<<iraw<<" "<<prSt;
277  for(icosm2=icosmhit;icosm2<Hits.size();icosm2++){
278 
279  if (iraw==Hits[icosm2]->geographicalId().rawId()){
280  TransientTrackingRecHit::RecHitPointer tmphit=RHBuilder->build(Hits[icosm2]);
281  float contr= theEstimator->estimate(prSt, *tmphit).second;
282  if (contr<chi2min) {
283  chi2min=contr;
284  ibestdet=icosm2;
285  }
286  if (icosm2!=icosmhit) icosmhit++;
287 
288  }
289  else icosm2=Hits.size();
290  }
291 
292  if(chi2min<chi2cut)
293  LogDebug("CosmicTrackFinder")<<"Chi2 contribution for hit at "
294  <<RHBuilder->build(Hits[ibestdet])->globalPosition()
295  <<" is "<<chi2min;
296 
297  if(traj.foundHits()<3 &&(chi2min<chi2cut)){
298  //check on the first hit after the seed
299  GlobalVector ck=RHBuilder->build(Hits[ibestdet])->globalPosition()-
301  if ((abs(ck.x()/ck.y())>2)||(abs(ck.z()/ck.y())>2)) chi2min=300;
302  }
303  if (chi2min<chi2cut){
304  if ( abs(prLoc.x()) < 25 && abs(prLoc.y()) < 25 ){
305  TransientTrackingRecHit::RecHitPointer tmphitbestdet=RHBuilder->build(Hits[ibestdet]);
306  TSOS UpdatedState= theUpdator->update( prSt, *tmphitbestdet);
307  if (UpdatedState.isValid()){
308 
309  traj.push(std::move(TM(prSt,UpdatedState,RHBuilder->build(Hits[ibestdet])
310  , chi2min)));
311  LogDebug("CosmicTrackFinder") <<
312  "STATE UPDATED WITH HIT AT POSITION "
313  <<tmphitbestdet->globalPosition()
314  <<UpdatedState<<" "
315  <<traj.chiSquared();
316 
317  hits.push_back(tmphitbestdet);
318  }
319  }else LogDebug("CosmicTrackFinder")<<" Hits outside module surface "<< prLoc;
320  }else LogDebug("CosmicTrackFinder")<<" State can not be updated with hit at position " <<gphit;
321  }else LogDebug("CosmicTrackFinder")<<" State can not be propagated at det "<< iraw;
322  }
323 
324 
325 
326  if ( qualityFilter( traj)){
327  const TrajectorySeed& tmpseed=traj.seed();
328  if (thePropagatorOp->propagate(traj.lastMeasurement().updatedState(),
329  tracker->idToDet((*hits.begin())->geographicalId())->surface()).isValid()){
330  TSOS startingState=
331  thePropagatorOp->propagate(traj.lastMeasurement().updatedState(),
332  tracker->idToDet((*hits.begin())->geographicalId())->surface());
333 
334  trajFit = theFitter->fit(tmpseed,hits, startingState );
335  }
336  }
337 
338 }
339 
340 
341 bool
343  int ngoodhits=0;
344  if(geometry=="MTCC"){
345  auto hits = traj.recHits();
346  for(auto hit=hits.begin();hit!=hits.end();hit++){
347  unsigned int iid=(*hit)->hit()->geographicalId().rawId();
348  //CHECK FOR 3 hits r-phi
349  if(((iid>>0)&0x3)!=1) ngoodhits++;
350  }
351  }
352  else ngoodhits=traj.foundHits();
353 
354  if ( ngoodhits >= theMinHits) {
355  return true;
356  }
357  else {
358  return false;
359  }
360 }
#define LogDebug(id)
PropagationDirection direction() const
T getParameter(std::string const &) const
int foundHits() const
Definition: Trajectory.h:279
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:330
int init
Definition: HydjetWrapper.h:67
tuple magfield
Definition: HLT_ES_cff.py:2311
T y() const
Definition: PV3DBase.h:63
GlobalPoint globalPosition() const
ConstRecHitContainer recHits() const
Definition: Trajectory.h:258
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:40
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:228
T z() const
Definition: PV3DBase.h:64
void update(const LocalTrajectoryParameters &p, const SurfaceType &aSurface, const MagneticField *field, SurfaceSide side=SurfaceSideDefinition::atCenterOfSurface)
tuple result
Definition: query.py:137
def move
Definition: eostools.py:510
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:241
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:56
T const * product() const
Definition: ESHandle.h:86
range recHits() const
float chiSquared() const
Definition: Trajectory.h:307
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)
ESHandle< TrackerGeometry > geometry
bool qualityFilter(const Trajectory &traj)
State
Definition: hltDiff.cc:319
TrajectoryStateOnSurface const & updatedState() const
Trajectory createStartingTrajectory(const TrajectorySeed &seed) const
CosmicTrajectoryBuilder(const edm::ParameterSet &conf)
T x() const
Definition: PV3DBase.h:62
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:35