CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DAFTrackProducerAlgorithm.cc
Go to the documentation of this file.
4 
6 
9 
13 
22 
24 
26  const MagneticField * theMF,
27  //const TrackCandidateCollection& theTCCollection,
28  const std::vector<Trajectory>& theTrajectoryCollection,
29  const TrajectoryFitter * theFitter,
30  const TransientTrackingRecHitBuilder* builder,
31  const MultiRecHitCollector* measurementCollector,
33  const reco::BeamSpot& bs,
34  AlgoProductCollection& algoResults) const
35 {
36  edm::LogInfo("TrackProducer") << "Number of Trajectories: " << theTrajectoryCollection.size() << "\n";
37  int cont = 0;
38  for (std::vector<Trajectory>::const_iterator i=theTrajectoryCollection.begin(); i!=theTrajectoryCollection.end() ;i++)
39 
40 
41 {
42 
43 
44 
45 
46  float ndof=0;
47 
48  //first of all do a fit-smooth round to get the trajectory
49  LogDebug("DAFTrackProducerAlgorithm") << "About to build the trajectory for the first round...";
50  //theMRHChi2Estimator->setAnnealingFactor(1);
51  // std::vector<Trajectory> vtraj = theFitter->fit(seed, hits, theTSOS);
52  std::vector<Trajectory> vtraj;
53  vtraj.push_back(*i);
54 
55  LogDebug("DAFTrackProducerAlgorithm") << "done" << std::endl;
56  LogDebug("DAFTrackProducerAlgorithm") << "after the first round found " << vtraj.size() << " trajectories" << std::endl;
57 
58  if (vtraj.size() != 0){
59 
60  //bool isFirstIteration=true;
61  std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface> hits = collectHits(vtraj, measurementCollector);
62  fit(hits, theFitter, vtraj);
63 
64  for (std::vector<double>::const_iterator ian = updator->getAnnealingProgram().begin(); ian != updator->getAnnealingProgram().end(); ian++){
65  if (vtraj.size()){
66  LogDebug("DAFTrackProducerAlgorithm") << "Seed direction is " << vtraj.front().seed().direction()
67  << " Traj direction is " << vtraj.front().direction();
68  std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface> curiterationhits = updateHits(vtraj,updator,*ian);
69  fit(curiterationhits, theFitter, vtraj);
70 
71  LogDebug("DAFTrackProducerAlgorithm") << "done annealing value " << (*ian) << " with " << vtraj.size() << " trajectories";
72  } else break;
73  LogDebug("DAFTrackProducerAlgorithm") << "number of trajectories after annealing value " << (*ian) << " annealing step " << vtraj.size();
74  }
75  LogDebug("DAFTrackProducerAlgorithm") << "Ended annealing program with " << vtraj.size() << " trajectories" << std::endl;
76 
77  //check the trajectory to see that the number of valid hits with
78  //reasonable weight (>1e-6) is higher than the minimum set in the DAFTrackProducer.
79  //This is a kind of filter to remove tracks with too many outliers.
80  //Outliers are mrhits with all components with weight less than 1e-6
81 
82  //std::vector<Trajectory> filtered;
83  //filter(theFitter, vtraj, conf_.getParameter<int>("MinHits"), filtered);
84  //ndof = calculateNdof(filtered);
85  ndof=calculateNdof(vtraj);
86  //bool ok = buildTrack(filtered, algoResults, ndof, bs);
87  if (vtraj.size()){
88  if(vtraj.front().foundHits()>=conf_.getParameter<int>("MinHits"))
89  {
90 
91  bool ok = buildTrack(vtraj, algoResults, ndof, bs) ;
92  if(ok) cont++;
93  }
94 
95  else{
96  LogDebug("DAFTrackProducerAlgorithm") << "Rejecting trajectory with " << vtraj.front().foundHits()<<" hits";
97  }
98  }
99 
100  else LogDebug("DAFTrackProducerAlgorithm") << "Rejecting empty trajectory" << std::endl;
101 
102 
103  }
104  }
105  LogDebug("TrackProducer") << "Number of Tracks found: " << cont << "\n";
106 }
107 
108 std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface>
109 DAFTrackProducerAlgorithm::collectHits(const std::vector<Trajectory>& vtraj,
110  const MultiRecHitCollector* measurementCollector) const{
112  std::vector<TrajectoryMeasurement> collectedmeas = measurementCollector->recHits(vtraj.front());
113  if (collectedmeas.empty()) return std::make_pair(TransientTrackingRecHit::RecHitContainer(), TrajectoryStateOnSurface());
114  for (std::vector<TrajectoryMeasurement>::const_iterator iter = collectedmeas.begin(); iter!=collectedmeas.end(); iter++){
115  hits.push_back(iter->recHit());
116  }
117  return std::make_pair(hits,TrajectoryStateWithArbitraryError()(collectedmeas.front().predictedState()));
118 }
119 
120 std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface>
121 DAFTrackProducerAlgorithm::updateHits(const std::vector<Trajectory>& vtraj,
123  double annealing) const {
125  std::vector<TrajectoryMeasurement> vmeas = vtraj.front().measurements();
126  std::vector<TrajectoryMeasurement>::reverse_iterator imeas;
127  for (imeas = vmeas.rbegin(); imeas != vmeas.rend(); imeas++){
128  TransientTrackingRecHit::RecHitPointer updated = updator->update(imeas->recHit(), imeas->updatedState(), annealing);
129  hits.push_back(updated);
130  }
131  return std::make_pair(hits,TrajectoryStateWithArbitraryError()(vmeas.back().updatedState()));
132 }
133 
134 void DAFTrackProducerAlgorithm::fit(const std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface>& hits,
135  const TrajectoryFitter * theFitter,
136  std::vector<Trajectory>& vtraj) const {
137  std::vector<Trajectory> newVec = theFitter->fit(TrajectorySeed(PTrajectoryStateOnDet(),
139  vtraj.front().seed().direction()),
140  hits.first,
141  hits.second);
142  vtraj.reserve(newVec.size());
143  vtraj.swap(newVec);
144  LogDebug("DAFTrackProducerAlgorithm") << "swapped!" << std::endl;
145 }
146 
147 
148 bool DAFTrackProducerAlgorithm::buildTrack (const std::vector<Trajectory>& vtraj,
149  AlgoProductCollection& algoResults,
150  float ndof,
151  const reco::BeamSpot& bs) const{
152  //variable declarations
153  reco::Track * theTrack;
154  Trajectory * theTraj;
155 
156  //perform the fit: the result's size is 1 if it succeded, 0 if fails
157 
158 
159  //LogDebug("TrackProducer") <<" FITTER FOUND "<< vtraj.size() << " TRAJECTORIES" <<"\n";
160  LogDebug("DAFTrackProducerAlgorithm") <<" FITTER FOUND "<< vtraj.size() << " TRAJECTORIES" << std::endl;;
161  TrajectoryStateOnSurface innertsos;
162 
163  if (vtraj.size() != 0){
164 
165  theTraj = new Trajectory( vtraj.front() );
166 
167  if (theTraj->direction() == alongMomentum) {
168  //if (theTraj->direction() == oppositeToMomentum) {
169  innertsos = theTraj->firstMeasurement().updatedState();
170  //std::cout << "Inner momentum " << innertsos.globalParameters().momentum().mag() << std::endl;
171  } else {
172  innertsos = theTraj->lastMeasurement().updatedState();
173  }
174 
175  TSCBLBuilderNoMaterial tscblBuilder;
176  TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(*(innertsos.freeState()),bs);
177 
178  if (tscbl.isValid()==false) return false;
179 
181  math::XYZPoint pos( v.x(), v.y(), v.z() );
183  math::XYZVector mom( p.x(), p.y(), p.z() );
184 
185  // LogDebug("TrackProducer") <<v<<p<<std::endl;
186 
187  theTrack = new reco::Track(theTraj->chiSquared(),
188  ndof, //in the DAF the ndof is not-integer
189  pos, mom, tscbl.trackStateAtPCA().charge(), tscbl.trackStateAtPCA().curvilinearError());
190 
191 
192 
193  AlgoProduct aProduct(theTraj,std::make_pair(theTrack,theTraj->direction()));
194 
195  algoResults.push_back(aProduct);
196 
197 
198  return true;
199  }
200  else return false;
201 }
202 
203 void DAFTrackProducerAlgorithm::filter(const TrajectoryFitter* fitter, std::vector<Trajectory>& input, int minhits, std::vector<Trajectory>& output) const {
204  if (input.empty()) return;
205 
206  int ngoodhits = 0;
207 
208  std::vector<TrajectoryMeasurement> vtm = input[0].measurements();
209 
211 
212  //count the number of non-outlier and non-invalid hits
213  for (std::vector<TrajectoryMeasurement>::reverse_iterator tm=vtm.rbegin(); tm!=vtm.rend();tm++){
214  //if the rechit is valid
215  if (tm->recHit()->isValid()) {
216  TransientTrackingRecHit::ConstRecHitContainer components = tm->recHit()->transientHits();
217  bool isGood = false;
218  for (TransientTrackingRecHit::ConstRecHitContainer::iterator rechit = components.begin(); rechit != components.end(); rechit++){
219  //if there is at least one component with weight higher than 1e-6 then the hit is not an outlier
220  if ((*rechit)->weight()>1e-6) {ngoodhits++; isGood = true; break;}
221  }
222  if (isGood) hits.push_back(tm->recHit()->clone(tm->updatedState()));
223  else hits.push_back(InvalidTransientRecHit::build(tm->recHit()->det(), TrackingRecHit::missing));
224  } else {
225  hits.push_back(tm->recHit()->clone(tm->updatedState()));
226  }
227  }
228 
229 
230  LogDebug("DAFTrackProducerAlgorithm") << "Original number of valid hits " << input[0].foundHits() << "; after filtering " << ngoodhits;
231  //debug
232  if (ngoodhits>input[0].foundHits()) edm::LogError("DAFTrackProducerAlgorithm") << "Something wrong: the number of good hits from DAFTrackProducerAlgorithm::filter " << ngoodhits << " is higher than the original one " << input[0].foundHits();
233 
234  if (ngoodhits < minhits) return;
235 
236  TrajectoryStateOnSurface curstartingTSOS = input.front().lastMeasurement().updatedState();
237  LogDebug("DAFTrackProducerAlgorithm") << "starting tsos for final refitting " << curstartingTSOS ;
238  //curstartingTSOS.rescaleError(100);
239 
240  output = fitter->fit(TrajectorySeed(PTrajectoryStateOnDet(),
242  input.front().seed().direction()),
243  hits,
244  TrajectoryStateWithArbitraryError()(curstartingTSOS));
245  LogDebug("DAFTrackProducerAlgorithm") << "After filtering " << output.size() << " trajectories";
246 
247 }
248 
249 float DAFTrackProducerAlgorithm::calculateNdof(const std::vector<Trajectory>& vtraj) const {
250  if (vtraj.empty()) return 0;
251  float ndof = 0;
252  const std::vector<TrajectoryMeasurement>& meas = vtraj.front().measurements();
253  for (std::vector<TrajectoryMeasurement>::const_iterator iter = meas.begin(); iter != meas.end(); iter++){
254  if (iter->recHit()->isValid()){
255  TransientTrackingRecHit::ConstRecHitContainer components = iter->recHit()->transientHits();
256  TransientTrackingRecHit::ConstRecHitContainer::const_iterator iter2;
257  for (iter2 = components.begin(); iter2 != components.end(); iter2++){
258  if ((*iter2)->isValid())ndof+=((*iter2)->dimension())*(*iter2)->weight();
259  }
260  }
261  }
262  return ndof-5;
263 }
#define LogDebug(id)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::pair< Trajectory *, std::pair< reco::Track *, PropagationDirection > > AlgoProduct
static RecHitPointer build(const GeomDet *geom, Type type=TrackingRecHit::missing, const DetLayer *layer=0)
float calculateNdof(const std::vector< Trajectory > &vtraj) const
T y() const
Definition: PV3DBase.h:57
TrackCharge charge() const
const CurvilinearTrajectoryError & curvilinearError() const
void runWithCandidate(const TrackingGeometry *, const MagneticField *, const std::vector< Trajectory > &, const TrajectoryFitter *, const TransientTrackingRecHitBuilder *, const MultiRecHitCollector *measurementTracker, const SiTrackerMultiRecHitUpdator *, const reco::BeamSpot &, AlgoProductCollection &) const
Run the Final Fit taking TrackCandidates as input.
const std::vector< double > & getAnnealingProgram() const
std::vector< ConstRecHitPointer > RecHitContainer
virtual TransientTrackingRecHit::RecHitPointer update(TransientTrackingRecHit::ConstRecHitPointer original, TrajectoryStateOnSurface tsos, double annealing=1.) const
PropagationDirection const & direction() const
Definition: Trajectory.cc:195
void filter(const TrajectoryFitter *fitter, std::vector< Trajectory > &input, int minhits, std::vector< Trajectory > &output) const
FreeTrajectoryState * freeState(bool withErrors=true) const
std::vector< AlgoProduct > AlgoProductCollection
bool buildTrack(const std::vector< Trajectory > &, AlgoProductCollection &algoResults, float, const reco::BeamSpot &) const
Construct Tracks to be put in the event.
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:147
T z() const
Definition: PV3DBase.h:58
virtual std::vector< TrajectoryMeasurement > recHits(const Trajectory &) const =0
std::pair< TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface > updateHits(const std::vector< Trajectory > &vtraj, const SiTrackerMultiRecHitUpdator *updator, double annealing) const
std::pair< TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface > collectHits(const std::vector< Trajectory > &vtraj, const MultiRecHitCollector *measurementCollector) const
TrajectoryStateOnSurface updatedState() const
GlobalVector momentum() const
tuple input
Definition: collect_tpl.py:10
std::vector< ConstRecHitPointer > ConstRecHitContainer
GlobalPoint position() const
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:160
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
int cont
virtual std::vector< Trajectory > fit(const Trajectory &) const =0
void fit(const std::pair< TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface > &hits, const TrajectoryFitter *theFitter, std::vector< Trajectory > &vtraj) const
accomplishes the fitting-smoothing step for each annealing value
T x() const
Definition: PV3DBase.h:56
reference front()
Definition: OwnVector.h:371
mathSSE::Vec4< T > v
double chiSquared() const
Definition: Trajectory.h:208