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.
25 
26 
28  conf_(conf),
29  minHits_(conf.getParameter<int>("MinHits")){}
30 
31 
33  const MagneticField * theMF,
34  const std::vector<Trajectory>& theTrajectoryCollection,
35  const MeasurementTrackerEvent *measTk,
36  const TrajectoryFitter * theFitter,
37  const TransientTrackingRecHitBuilder* builder,
38  const MultiRecHitCollector* measurementCollector,
40  const reco::BeamSpot& bs,
41  AlgoProductCollection& algoResults,
42  TrajAnnealingCollection& trajann,
43  bool TrajAnnSaving_) const
44 {
45  LogDebug("DAFTrackProducerAlgorithm") << "Number of Trajectories: " << theTrajectoryCollection.size() << "\n";
46  int cont = 0;
47 
48  //running on src trajectory collection
49  for (std::vector<Trajectory>::const_iterator ivtraj = theTrajectoryCollection.begin();
50  ivtraj != theTrajectoryCollection.end(); ivtraj++)
51  {
52 
53  float ndof = 0;
54  Trajectory currentTraj;
55 
56  //getting trajectory
57  //no need to have std::vector<Trajectory> vtraj !
58  if ( (*ivtraj).isValid() ){
59 
60  LogDebug("DAFTrackProducerAlgorithm") << "The trajectory is valid. \n";
61 
62  //getting the MultiRecHit collection and the trajectory with a first fit-smooth round
63  std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface> hits =
64  collectHits(*ivtraj, measurementCollector, &*measTk);
65 
66  currentTraj = fit(hits, theFitter, *ivtraj);
67 
68  //starting the annealing program
69  for (std::vector<double>::const_iterator ian = updator->getAnnealingProgram().begin();
70  ian != updator->getAnnealingProgram().end(); ian++){
71 
72  if (currentTraj.isValid()){
73 
74  LogDebug("DAFTrackProducerAlgorithm") << "Seed direction is " << currentTraj.seed().direction()
75  << ".Traj direction is " << currentTraj.direction();
76 
77  //updating MultiRecHits and fit-smooth again
78  std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface> curiterationhits =
79  updateHits(currentTraj,updator,*ian);
80  currentTraj = fit(curiterationhits, theFitter, currentTraj);
81 
82  //saving trajectory for each annealing cycle ...
83  if(TrajAnnSaving_){
84  TrajAnnealing temp(currentTraj, *ian);
85  trajann.push_back(temp);
86  }
87 
88  LogDebug("DAFTrackProducerAlgorithm") << "done annealing value " << (*ian) ;
89 
90  }
91  else break;
92 
93 
94  } //end of annealing program
95 
96  LogDebug("DAFTrackProducerAlgorithm") << "Ended annealing program with " << (1.*checkHits(*ivtraj, currentTraj))/(1.*(*ivtraj).measurements().size())*100. << " unchanged." << std::endl;
97 
98  //computing the ndof keeping into account the weights
99  ndof = calculateNdof(currentTraj);
100 
101  //checking if the trajectory has the minimum number of valid hits ( weight (>1e-6) )
102  //in order to remove tracks with too many outliers.
103 
104  //std::vector<Trajectory> filtered;
105  //filter(theFitter, vtraj, minHits_, filtered, builder);
106 
107  if(currentTraj.foundHits() >= minHits_) {
108 
109  bool ok = buildTrack(currentTraj, algoResults, ndof, bs) ;
110  if(ok) cont++;
111 
112  }
113  else{
114  LogDebug("DAFTrackProducerAlgorithm") << "Rejecting trajectory with "
115  << currentTraj.foundHits()<<" hits";
116  }
117  }
118  else
119  LogDebug("DAFTrackProducerAlgorithm") << "Rejecting empty trajectory" << std::endl;
120 
121  } //end run on track collection
122 
123  LogDebug("DAFTrackProducerAlgorithm") << "Number of Tracks found: " << cont << "\n";
124 
125 }
126 /*------------------------------------------------------------------------------------------------------*/
127 std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface>
129  const MultiRecHitCollector* measurementCollector,
130  const MeasurementTrackerEvent *measTk) const
131 {
132 
133  LogDebug("DAFTrackProducerAlgorithm") << "Calling DAFTrackProducerAlgorithm::collectHits";
134 
135  //getting the traj measurements from the MeasurementCollector
136  int nHits = 0;
138  std::vector<TrajectoryMeasurement> collectedmeas = measurementCollector->recHits(vtraj, measTk);
139 
140  //if the MeasurementCollector is empty, make an "empty" pair
141  //else taking the collected measured hits and building the pair
142  if( collectedmeas.empty() )
144 
145  for( std::vector<TrajectoryMeasurement>::const_iterator iter = collectedmeas.begin();
146  iter!=collectedmeas.end(); iter++ ){
147 
148  nHits++;
149  hits.push_back(iter->recHit());
150 
151  }
152 
153 
154  //TrajectoryStateWithArbitraryError() == Creates a TrajectoryState with the same parameters
155  // as the input one, but with "infinite" errors, i.e. errors so big that they don't
156  // bias a fit starting with this state.
157  //return std::make_pair(hits,TrajectoryStateWithArbitraryError()(collectedmeas.front().predictedState()));
158 
159  // I do not have to rescale the error because it is already rescaled in the fit code
160  TrajectoryStateOnSurface initialStateFromTrack = collectedmeas.front().predictedState();
161 
162  return std::make_pair(hits, initialStateFromTrack);
163 
164 }
165 /*------------------------------------------------------------------------------------------------------*/
166 std::pair<TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface>
169  double annealing) const
170 {
172  std::vector<TrajectoryMeasurement> vmeas = vtraj.measurements();
173  std::vector<TrajectoryMeasurement>::reverse_iterator imeas;
174 
175  //I run inversely on the trajectory obtained and update the state
176  for (imeas = vmeas.rbegin(); imeas != vmeas.rend(); imeas++){
177  TransientTrackingRecHit::RecHitPointer updated = updator->update(imeas->recHit(),
178  imeas->updatedState(), annealing);
179  hits.push_back(updated);
180  }
181 
182  TrajectoryStateOnSurface updatedStateFromTrack = vmeas.back().predictedState();
183  //updatedStateFromTrack.rescaleError(10);
184 
185  //return std::make_pair(hits,TrajectoryStateWithArbitraryError()(vmeas.back().updatedState()));
186  return std::make_pair(hits,updatedStateFromTrack);
187 }
188 /*------------------------------------------------------------------------------------------------------*/
191  const TrajectoryFitter * theFitter,
192  Trajectory vtraj) const {
193 
194  //creating a new trajectory starting from the direction of the seed of the input one and the hits
197  vtraj.seed().direction()),
198  hits.first, hits.second);
199 
200  if( newVec.isValid() ) return newVec;
201  else{
202  LogDebug("DAFTrackProducerAlgorithm") << "Fit no valid.";
203  return Trajectory();
204  }
205 
206 }
207 /*------------------------------------------------------------------------------------------------------*/
209  AlgoProductCollection& algoResults,
210  float ndof,
211  const reco::BeamSpot& bs) const
212 {
213  //variable declarations
214  reco::Track * theTrack;
215  Trajectory * theTraj;
216 
217  LogDebug("DAFTrackProducerAlgorithm") <<" BUILDER " << std::endl;;
218  TrajectoryStateOnSurface innertsos;
219 
220  if ( vtraj.isValid() ){
221 
222  theTraj = new Trajectory( vtraj );
223 
224  if (vtraj.direction() == alongMomentum) {
225  //if (theTraj->direction() == oppositeToMomentum) {
226  innertsos = vtraj.firstMeasurement().updatedState();
227  } else {
228  innertsos = vtraj.lastMeasurement().updatedState();
229  }
230 
231  TSCBLBuilderNoMaterial tscblBuilder;
232  TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(*(innertsos.freeState()),bs);
233 
234  if (tscbl.isValid()==false) return false;
235 
237  math::XYZPoint pos( v.x(), v.y(), v.z() );
239  math::XYZVector mom( p.x(), p.y(), p.z() );
240 
241  // LogDebug("TrackProducer") <<v<<p<<std::endl;
242 
243  theTrack = new reco::Track(vtraj.chiSquared(),
244  ndof, //in the DAF the ndof is not-integer
245  pos, mom, tscbl.trackStateAtPCA().charge(),
247 
248  AlgoProduct aProduct(theTraj,std::make_pair(theTrack,vtraj.direction()));
249  algoResults.push_back(aProduct);
250 
251  return true;
252  }
253  else
254  return false;
255 }
256 /*------------------------------------------------------------------------------------------------------*/
257 void DAFTrackProducerAlgorithm::filter(const TrajectoryFitter* fitter, std::vector<Trajectory>& input,
258  int minhits, std::vector<Trajectory>& output,
259  const TransientTrackingRecHitBuilder* builder) const
260 {
261  if (input.empty()) return;
262 
263  int ngoodhits = 0;
264  std::vector<TrajectoryMeasurement> vtm = input[0].measurements();
266 
267  //count the number of non-outlier and non-invalid hits
268  for (std::vector<TrajectoryMeasurement>::reverse_iterator tm=vtm.rbegin(); tm!=vtm.rend();tm++){
269  //if the rechit is valid
270  if (tm->recHit()->isValid()) {
271  SiTrackerMultiRecHit const & mHit = dynamic_cast<SiTrackerMultiRecHit const &>(*tm->recHit());
272  std::vector<const TrackingRecHit*> components = mHit.recHits();
273  int iComp = 0;
274  bool isGood = false;
275  for(std::vector<const TrackingRecHit*>::const_iterator iter = components.begin(); iter != components.end(); iter++, iComp++){
276  //if there is at least one component with weight higher than 1e-6 then the hit is not an outlier
277  if (mHit.weight(iComp)>1e-6) {ngoodhits++; iComp++; isGood = true; break;}
278  }
279  if (isGood) {
280  TkClonerImpl hc = static_cast<TkTransientTrackingRecHitBuilder const *>(builder)->cloner();
281  auto tempHit = hc.makeShared(tm->recHit(),tm->updatedState());
282  hits.push_back(tempHit);
283  }
284  else hits.push_back(std::make_shared<InvalidTrackingRecHit>(*tm->recHit()->det(), TrackingRecHit::missing));
285  } else {
286  TkClonerImpl hc = static_cast<TkTransientTrackingRecHitBuilder const *>(builder)->cloner();
287  auto tempHit = hc.makeShared(tm->recHit(),tm->updatedState());
288  hits.push_back(tempHit);
289  }
290  }
291 
292 
293  LogDebug("DAFTrackProducerAlgorithm") << "Original number of valid hits " << input[0].foundHits() << "; after filtering " << ngoodhits;
294  //debug
295  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();
296 
297  if (ngoodhits < minhits) return;
298 
299  TrajectoryStateOnSurface curstartingTSOS = input.front().lastMeasurement().updatedState();
300  LogDebug("DAFTrackProducerAlgorithm") << "starting tsos for final refitting " << curstartingTSOS ;
301  //curstartingTSOS.rescaleError(100);
302 
303  output = fitter->fit(TrajectorySeed(PTrajectoryStateOnDet(),
305  input.front().seed().direction()),
306  hits,
307  TrajectoryStateWithArbitraryError()(curstartingTSOS));
308  LogDebug("DAFTrackProducerAlgorithm") << "After filtering " << output.size() << " trajectories";
309 
310 }
311 /*------------------------------------------------------------------------------------------------------*/
313 {
314 
315  if (!vtraj.isValid()) return 0;
316  float ndof = 0;
317  const std::vector<TrajectoryMeasurement>& meas = vtraj.measurements();
318  for (std::vector<TrajectoryMeasurement>::const_iterator iter = meas.begin(); iter != meas.end(); iter++){
319  if (iter->recHit()->isValid()){
320  SiTrackerMultiRecHit const & mHit = dynamic_cast<SiTrackerMultiRecHit const &>(*iter->recHit());
321  std::vector<const TrackingRecHit*> components = mHit.recHits();
322  int iComp = 0;
323  for(std::vector<const TrackingRecHit*>::const_iterator iter2 = components.begin(); iter2 != components.end(); iter2++, iComp++){
324  if ((*iter2)->isValid())
325  ndof += ((*iter2)->dimension())*mHit.weight(iComp);
326  }
327 
328  }
329  }
330 
331  return ndof-5;
332 
333 }
334 //------------------------------------------------------------------------------------------------
335 int DAFTrackProducerAlgorithm::checkHits( Trajectory iInitTraj, const Trajectory iFinalTraj) const {
336 
337  std::vector<TrajectoryMeasurement> initmeasurements = iInitTraj.measurements();
338  std::vector<TrajectoryMeasurement> finalmeasurements = iFinalTraj.measurements();
339  std::vector<TrajectoryMeasurement>::iterator imeas;
340  std::vector<TrajectoryMeasurement>::iterator jmeas;
341  int nSame = 0;
342  int ihit = 0;
343 
344  if( initmeasurements.empty() || finalmeasurements.empty() ){
345  LogDebug("DAFTrackProducerAlgorithm") << "Initial or Final Trajectory empty.";
346  return 0;
347  }
348 
349  if( initmeasurements.size() != finalmeasurements.size() ) {
350  LogDebug("DAFTrackProducerAlgorithm") << "Initial and Final Trajectory have different size.";
351  return 0;
352  }
353 
354  for(jmeas = initmeasurements.begin(); jmeas != initmeasurements.end(); jmeas++){
355 
356  const TrackingRecHit* initHit = jmeas->recHit()->hit();
357 
358  if(!initHit->isValid() && ihit == 0 ) continue;
359 
360  if(initHit->isValid()){
361 
362  TrajectoryMeasurement imeas = finalmeasurements.at(ihit);
363  const TrackingRecHit* finalHit = imeas.recHit()->hit();
364  const TrackingRecHit* MaxWeightHit=0;
365  float maxweight = 0;
366 
367  const SiTrackerMultiRecHit* mrh = dynamic_cast<const SiTrackerMultiRecHit*>(finalHit);
368  if (mrh){
369  std::vector<const TrackingRecHit*> components = mrh->recHits();
370  std::vector<const TrackingRecHit*>::const_iterator icomp;
371  int hitcounter=0;
372 
373  for (icomp = components.begin(); icomp != components.end(); icomp++) {
374  if((*icomp)->isValid()) {
375  double weight = mrh->weight(hitcounter);
376  if(weight > maxweight) {
377  MaxWeightHit = *icomp;
378  maxweight = weight;
379  }
380  }
381  hitcounter++;
382  }
383 
384  auto myref1 = reinterpret_cast<const BaseTrackerRecHit *>(initHit)->firstClusterRef();
385  auto myref2 = reinterpret_cast<const BaseTrackerRecHit *>(MaxWeightHit)->firstClusterRef();
386 
387  if( myref1 == myref2 ){
388  nSame++;
389  }
390  }
391  } else {
392 
393  TrajectoryMeasurement imeas = finalmeasurements.at(ihit);
394  const TrackingRecHit* finalHit = imeas.recHit()->hit();
395  if(!finalHit->isValid()){
396  nSame++;
397  }
398  }
399 
400  ihit++;
401  }
402 
403  return nSame;
404 }
#define LogDebug(id)
PropagationDirection direction() const
int foundHits() const
Definition: Trajectory.h:234
ConstRecHitPointer const & recHit() const
tuple cont
load Luminosity info ##
Definition: generateEDF.py:622
std::pair< Trajectory *, std::pair< reco::Track *, PropagationDirection > > AlgoProduct
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:275
virtual TransientTrackingRecHit::RecHitPointer update(TransientTrackingRecHit::ConstRecHitPointer original, const TrajectoryStateOnSurface &tsos, double annealing=1.) const
T y() const
Definition: PV3DBase.h:63
std::vector< ConstRecHitPointer > RecHitContainer
virtual std::vector< TrajectoryMeasurement > recHits(const Trajectory &, const MeasurementTrackerEvent *theMTE) const =0
int checkHits(Trajectory iInitTraj, const Trajectory iFinalTraj) const
TrackCharge charge() const
virtual TrackingRecHit::ConstRecHitPointer makeShared(SiPixelRecHit const &hit, TrajectoryStateOnSurface const &tsos) const override
Definition: TkClonerImpl.cc:47
const CurvilinearTrajectoryError & curvilinearError() const
const std::vector< double > & getAnnealingProgram() const
std::vector< TrajAnnealing > TrajAnnealingCollection
float weight(unsigned int i) const
static std::string const input
Definition: EdmProvDump.cc:43
PropagationDirection const & direction() const
Definition: Trajectory.cc:118
void filter(const TrajectoryFitter *fitter, std::vector< Trajectory > &input, int minhits, std::vector< Trajectory > &output, const TransientTrackingRecHitBuilder *builder) const
DataContainer const & measurements() const
Definition: Trajectory.h:203
virtual std::vector< const TrackingRecHit * > recHits() const
Access to component RecHits (if any)
std::vector< AlgoProduct > AlgoProductCollection
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
FreeTrajectoryState const * freeState(bool withErrors=true) const
std::pair< TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface > updateHits(const Trajectory vtraj, const SiTrackerMultiRecHitUpdator *updator, double annealing) const
T z() const
Definition: PV3DBase.h:64
Trajectory fit(const std::pair< TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface > &hits, const TrajectoryFitter *theFitter, Trajectory vtraj) const
accomplishes the fitting-smoothing step for each annealing value
virtual Trajectory fitOne(const Trajectory &traj, fitType type=standard) const =0
GlobalVector momentum() const
std::shared_ptr< TrackingRecHit const > RecHitPointer
tuple conf
Definition: dbtoconf.py:185
GlobalPoint position() const
virtual TrackingRecHit const * hit() const
bool isValid() const
Definition: Trajectory.h:269
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:194
std::pair< TransientTrackingRecHit::RecHitContainer, TrajectoryStateOnSurface > collectHits(const Trajectory vtraj, const MultiRecHitCollector *measurementCollector, const MeasurementTrackerEvent *measTk) const
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
bool isValid() const
bool buildTrack(const Trajectory, AlgoProductCollection &algoResults, float, const reco::BeamSpot &) const
Construct Tracks to be put in the event.
float chiSquared() const
Definition: Trajectory.h:252
float calculateNdof(const Trajectory vtraj) const
void runWithCandidate(const TrackingGeometry *, const MagneticField *, const std::vector< Trajectory > &, const MeasurementTrackerEvent *measTk, const TrajectoryFitter *, const TransientTrackingRecHitBuilder *, const MultiRecHitCollector *measurementTracker, const SiTrackerMultiRecHitUpdator *, const reco::BeamSpot &, AlgoProductCollection &, TrajAnnealingCollection &, bool) const
Run the Final Fit taking TrackCandidates as input.
susybsm::HSCParticleCollection hc
Definition: classes.h:25
TrajectoryStateOnSurface const & updatedState() const
std::vector< Trajectory > fit(const Trajectory &traj, fitType type=standard) const
int weight
Definition: histoStyle.py:50
DAFTrackProducerAlgorithm(const edm::ParameterSet &conf)
T x() const
Definition: PV3DBase.h:62
reference front()
Definition: OwnVector.h:355