CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CosmicTrackSplitter.cc
Go to the documentation of this file.
5 
8 
14 
21 
22 // added by me
23 
30 
37 
39 
42 
44 
47 
48 
52 
53 
54 #include <boost/regex.hpp>
55 
77 namespace reco { namespace modules {
79  public:
80  CosmicTrackSplitter(const edm::ParameterSet &iConfig) ;
81  virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
82 
83  private:
87  size_t minimumHits_;
88 
94 
95  double dZcut_;
96  double dXYcut_;
97 
98  std::vector<uint32_t> detsToIgnore_;
99 
102 
103  TrackCandidate makeCandidate(const reco::Track &tk, std::vector<TrackingRecHit *>::iterator hitsBegin, std::vector<TrackingRecHit *>::iterator hitsEnd) ;
104 
105  }; // class
106 
107 
109  minimumHits_(iConfig.getParameter<uint32_t>("minimumHits")),
110  replaceWithInactiveHits_(iConfig.getParameter<bool>("replaceWithInactiveHits")),
111  stripFrontInvalidHits_(iConfig.getParameter<bool>("stripFrontInvalidHits")),
112  stripBackInvalidHits_( iConfig.getParameter<bool>("stripBackInvalidHits") ),
113  stripAllInvalidHits_( iConfig.getParameter<bool>("stripAllInvalidHits") ),
114  excludePixelHits_( iConfig.getParameter<bool>("excludePixelHits") ),
115  dZcut_(iConfig.getParameter<double>("dzCut") ),
116  dXYcut_(iConfig.getParameter<double>("dxyCut") ),
117  detsToIgnore_( iConfig.getParameter<std::vector<uint32_t> >("detsToIgnore") )
118  {
119  // sanity check
121  throw cms::Exception("Configuration") << "Inconsistent Configuration: you can't set both 'stripAllInvalidHits' and 'replaceWithInactiveHits' to true\n";
122  }
123  tokenTracks= consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"));
124  tokenTrajTrack = consumes<TrajTrackAssociationCollection>(iConfig.getParameter<edm::InputTag>("tjTkAssociationMapTag") );
125 
126  LogDebug("CosmicTrackSplitter") << "sanity check";
127 
128  // sort detids to ignore
129  std::sort(detsToIgnore_.begin(), detsToIgnore_.end());
130 
131  totalTracks_ = 0;
132 
133  // issue the produce<>
134  produces<TrackCandidateCollection>();
135  }
136 
137  void
139  {
140  LogDebug("CosmicTrackSplitter") << "IN THE SPLITTER!!!!!";
141 
142  // read with View, so we can read also a TrackRefVector
144  iEvent.getByToken(tokenTracks, tracks);
145 
146  // also need trajectories ...
147  // Retrieve trajectories and tracks from the event
148  // -> merely skip if collection is empty
150  iEvent.getByToken( tokenTrajTrack, m_TrajTracksMap );
151 
152  // read from EventSetup
155 
156  // prepare output collection
157  std::auto_ptr<TrackCandidateCollection> output(new TrackCandidateCollection());
158  output->reserve(tracks->size());
159 
160  // working area and tools
161  std::vector<TrackingRecHit *> hits;
162 
163  // Form pairs of trajectories and tracks
164  //ConstTrajTrackPairCollection trajTracks;
165  LogDebug("CosmicTrackSplitter") << "size of map: " << m_TrajTracksMap->size();
166  int HITTOSPLITFROM = 0;
167  for ( TrajTrackAssociationCollection::const_iterator iPair = m_TrajTracksMap->begin(); iPair != m_TrajTracksMap->end(); iPair++ ){
168  const Trajectory* trajFromMap = &(*(*iPair).key);
169  const reco::Track* trackFromMap = &(*(*iPair).val);
170 
171  // loop to find the hit to split from (by taking dot product of pT and transverse position
172  std::vector<TrajectoryMeasurement> measurements = trajFromMap->measurements();
173  int totalNumberOfHits = measurements.size();
174  int numberOfHits = 0;
175  double previousDotProduct = 0;
176  for (trackingRecHit_iterator ith = trackFromMap->recHitsBegin(), edh = trackFromMap->recHitsEnd(); ith != edh; ++ith) {
177 
178  GlobalVector stateMomentum = measurements[numberOfHits].forwardPredictedState().globalMomentum();
179  GlobalPoint statePosition = measurements[numberOfHits].forwardPredictedState().globalPosition();
180  double dotProduct = stateMomentum.x()*statePosition.x() + stateMomentum.y()*statePosition.y();
181  if ( dotProduct*previousDotProduct < 0 ){
182  //found hit to split from...
183  HITTOSPLITFROM = numberOfHits;
184  }
185 
186  previousDotProduct = dotProduct;
187  numberOfHits++;
188 
189  }
190  LogDebug("CosmicTrackSplitter") << "number of rechits: " << numberOfHits;
191 
192  // check if the trajectories and rechits are in reverse order...
193  trackingRecHit_iterator bIt = trackFromMap->recHitsBegin();
194  trackingRecHit_iterator fIt = trackFromMap->recHitsEnd() - 1;
195  const TrackingRecHit* bHit = (*bIt);
196  const TrackingRecHit* fHit = (*fIt);
197  // hit type valid = 0, missing = 1, inactive = 2, bad = 3
198  if( bHit->type() != 0 || bHit->isValid() != 1){
199  //loop over hits forwards until first Valid hit is found
201  for( ihit = trackFromMap->recHitsBegin();
202  ihit != trackFromMap->recHitsEnd(); ++ihit){
203  const TrackingRecHit* iHit = (*ihit);
204  if( iHit->type() == 0 && iHit->isValid() == 1){
205  bHit = iHit;
206  break;
207  }
208  }
209  }
210  DetId bdetid = bHit->geographicalId();
211  GlobalPoint bPosHit = theGeometry->idToDetUnit( bdetid)->surface().
212  toGlobal(bHit->localPosition());
213  if( fHit->type() != 0 || fHit->isValid() != 1){
214  //loop over hits backwards until first Valid hit is found
216  for( ihitf = trackFromMap->recHitsEnd()-1;
217  ihitf != trackFromMap->recHitsBegin(); --ihitf){
218  const TrackingRecHit* iHit = (*ihitf);
219  if( iHit->type() == 0 && iHit->isValid() == 1){
220  fHit = iHit;
221  break;
222  }
223  }
224  }
225  DetId fdetid = fHit->geographicalId();
226  GlobalPoint fPosHit = theGeometry->
227  idToDetUnit( fdetid )->surface().toGlobal(fHit->localPosition());
228  GlobalPoint bPosState = measurements[0].updatedState().globalPosition();
229  GlobalPoint fPosState = measurements[measurements.size()-1].
230  updatedState().globalPosition();
231  bool trajReversedFlag = false;
232  /*
233  DetId bdetid = bHit->geographicalId();
234  DetId fdetid = fHit->geographicalId();
235  GlobalPoint bPosHit = theGeometry->idToDetUnit( bdetid )->surface().toGlobal(bHit->localPosition());
236  GlobalPoint fPosHit = theGeometry->idToDetUnit( fdetid )->surface().toGlobal(fHit->localPosition());
237  GlobalPoint bPosState = measurements[0].updatedState().globalPosition();
238  GlobalPoint fPosState = measurements[measurements.size() - 1].updatedState().globalPosition();
239  bool trajReversedFlag = false;
240  */
241  if (( (bPosHit - bPosState).mag() > (bPosHit - fPosState).mag() ) && ( (fPosHit - fPosState).mag() > (fPosHit - bPosState).mag() ) ){
242  trajReversedFlag = true;
243  }
244  if (trajReversedFlag){ int temp = HITTOSPLITFROM; HITTOSPLITFROM = totalNumberOfHits - temp; }
245  }
246 
247  totalTracks_ = totalTracks_ + tracks->size();
248  // loop on tracks
249  for (std::vector<reco::Track>::const_iterator itt = tracks->begin(), edt = tracks->end(); itt != edt; ++itt) {
250  hits.clear(); // extra safety
251 
252  LogDebug("CosmicTrackSplitter") << "ntracks: " << tracks->size();
253 
254  // try to find distance of closest approach
255  GlobalPoint v( itt->vx(), itt->vy(), itt->vz() );
256 
257  //checks on impact parameter
258  bool continueWithTrack = true;
259  if (fabs(v.z()) > dZcut_) continueWithTrack = false;
260  if (v.perp() > dXYcut_) continueWithTrack = false;
261  if (continueWithTrack == false) return;
262 
263  // LOOP TWICE, ONCE FOR TOP AND ONCE FOR BOTTOM
264  for (int i = 0; i < 2; ++i){
265  hits.clear(); // extra safety
266  LogDebug("CosmicTrackSplitter") << " loop on hits of track #" << (itt - tracks->begin());
267  int usedHitCtr = 0;
268  int hitCtr = 0;
269  for (trackingRecHit_iterator ith = itt->recHitsBegin(), edh = itt->recHitsEnd(); ith != edh; ++ith) {
270  //hitCtr++;
271  const TrackingRecHit * hit = (*ith); // ith is an iterator on edm::Ref to rechit
272  LogDebug("CosmicTrackSplitter") << " hit number " << (ith - itt->recHitsBegin());
273  // let's look at valid hits
274  if (hit->isValid()) {
275  LogDebug("CosmicTrackSplitter") << " valid, detid = " << hit->geographicalId().rawId();
276  DetId detid = hit->geographicalId();
277 
278  if (detid.det() == DetId::Tracker) { // check for tracker hits
279  LogDebug("CosmicTrackSplitter") << " valid, tracker ";
280  bool verdict = false;
281 
282  //trying to get the global position of the hit
283  //const GeomDetUnit* geomDetUnit = theGeometry->idToDetUnit( detid ).;
284 
285  const GlobalPoint pos = theGeometry->idToDetUnit( detid )->surface().toGlobal(hit->localPosition());
286  LogDebug("CosmicTrackSplitter") << "hit pos: " << pos << ", dca pos: " << v;
287 
288  // top half
289  if ((i == 0)&&(hitCtr < HITTOSPLITFROM)){
290  verdict = true;
291  LogDebug("CosmicTrackSplitter") << "tophalf";
292  }
293  // bottom half
294  if ((i == 1)&&(hitCtr >= HITTOSPLITFROM)){
295  verdict = true;
296  LogDebug("CosmicTrackSplitter") << "bottomhalf";
297  }
298 
299  // if the hit is good, check again at module level
300  if ( verdict && std::binary_search(detsToIgnore_.begin(), detsToIgnore_.end(), detid.rawId())) {
301  verdict = false;
302  }
303 
304  // if hit is good check to make sure that we are keeping pixel hits
305  if ( excludePixelHits_){
306  if ((detid.det() == DetId::Tracker)&&((detid.subdetId() == 1)||(detid.subdetId() == 2))) { // check for pixel hits
307  verdict = false;
308  }
309  }
310 
311  LogDebug("CosmicTrackSplitter") << " verdict after module list: " << (verdict ? "ok" : "no");
312  if (verdict == true) {
313  // just copy the hit
314  hits.push_back(hit->clone());
315  usedHitCtr++;
316  }
317  else {
318  // still, if replaceWithInactiveHits is true we have to put a new hit
320  hits.push_back(new InvalidTrackingRecHit(*hit->det(), TrackingRecHit::inactive));
321  }
322  }
323  }
324  else { // just copy non tracker hits
325  hits.push_back(hit->clone());
326  }
327  }
328  else {
329  if (!stripAllInvalidHits_) {
330  hits.push_back(hit->clone());
331  }
332  } // is valid hit
333  LogDebug("CosmicTrackSplitter") << " end of hit " << (ith - itt->recHitsBegin());
334  hitCtr++;
335  } // loop on hits
336  LogDebug("CosmicTrackSplitter") << " end of loop on hits of track #" << (itt - tracks->begin());
337 
338  std::vector<TrackingRecHit *>::iterator begin = hits.begin(), end = hits.end();
339 
340  LogDebug("CosmicTrackSplitter") << " selected " << hits.size() << " hits ";
341 
342  // strip invalid hits at the beginning
344  while ( (begin != end) && ( (*begin)->isValid() == false ) ) ++begin;
345  }
346 
347  LogDebug("CosmicTrackSplitter") << " after front stripping we have " << (end - begin) << " hits ";
348 
349  // strip invalid hits at the end
350  if (stripBackInvalidHits_ && (begin != end)) {
351  --end;
352  while ( (begin != end) && ( (*end)->isValid() == false ) ) --end;
353  ++end;
354  }
355 
356  LogDebug("CosmicTrackSplitter") << " after back stripping we have " << (end - begin) << " hits ";
357 
358  // if we still have some hits
359  //if ((end - begin) >= int(minimumHits_)) {
360  if ( usedHitCtr >= int(minimumHits_)) {
361  output->push_back( makeCandidate ( *itt, begin, end ) );
362  LogDebug("CosmicTrackSplitter") << "we made a candidate of " << hits.size() << " hits!";
363  }
364  // now delete the hits not used by the candidate
365  for (begin = hits.begin(), end = hits.end(); begin != end; ++begin) {
366  if (*begin) delete *begin;
367  }
368  LogDebug("CosmicTrackSplitter") << "loop: " << i << " has " << usedHitCtr << " active hits and " << hits.size() << " total hits...";
369  hits.clear();
370  } // loop twice for top and bottom
371  } // loop on tracks
372  LogDebug("CosmicTrackSplitter") << "totalTracks_ = " << totalTracks_;
373  iEvent.put(output);
374  }
375 
377  CosmicTrackSplitter::makeCandidate(const reco::Track &tk, std::vector<TrackingRecHit *>::iterator hitsBegin, std::vector<TrackingRecHit *>::iterator hitsEnd) {
378 
379  LogDebug("CosmicTrackSplitter") << "Making a candidate!";
380 
381 
383  PTrajectoryStateOnDet state;
384  if ( pdir == anyDirection ) throw cms::Exception("UnimplementedFeature") << "Cannot work with tracks that have 'anyDirecton' \n";
385  //if ( (pdir == alongMomentum) == ( tk.p() >= tk.outerP() ) ) {
386  if ( (pdir == alongMomentum) == ( (tk.outerPosition()-tk.innerPosition()).Dot(tk.momentum()) >= 0 ) ) {
387  // use inner state
389  state = trajectoryStateTransform::persistentState( originalTsosIn, DetId(tk.innerDetId()) );
390  } else {
391  // use outer state
393  state = trajectoryStateTransform::persistentState( originalTsosOut, DetId(tk.outerDetId()) );
394  }
395 
397 
399  ownHits.reserve(hitsEnd - hitsBegin);
400  for ( ; hitsBegin != hitsEnd; ++hitsBegin) { ownHits.push_back( *hitsBegin ); }
401 
402  TrackCandidate cand(ownHits, seed, state, tk.seedRef());
403 
404 
405  LogDebug("CosmicTrackSplitter") << " dumping the hits now: ";
406  for (TrackCandidate::range hitR = cand.recHits(); hitR.first != hitR.second; ++hitR.first) {
407  LogTrace("CosmicTrackSplitter") << " hit detid = " << hitR.first->geographicalId().rawId() <<
408  ", type = " << typeid(*hitR.first).name();
409  }
410 
411  return cand;
412  }
413 
414 }} //namespaces
415 
416 
417 // ========= MODULE DEF ==============
420 
#define LogDebug(id)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
TrackCandidate makeCandidate(const reco::Track &tk, std::vector< TrackingRecHit * >::iterator hitsBegin, std::vector< TrackingRecHit * >::iterator hitsEnd)
TrajectoryStateOnSurface outerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< TrackCandidate > TrackCandidateCollection
edm::EDGetTokenT< reco::TrackCollection > tokenTracks
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
Type type() const
edm::ESHandle< TrackerGeometry > theGeometry
PropagationDirection
std::pair< const_iterator, const_iterator > range
const Vector & momentum() const
track momentum vector
Definition: TrackBase.h:662
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
DataContainer const & measurements() const
Definition: Trajectory.h:203
void push_back(D *&d)
Definition: OwnVector.h:280
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
const GeomDet * det() const
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
edm::ESHandle< MagneticField > theMagField
#define end
Definition: vmac.h:37
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
#define LogTrace(id)
edm::EDGetTokenT< TrajTrackAssociationCollection > tokenTrajTrack
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::GlobalCoordinateSystemTag > GlobalVector
vector in glovbal coordinate system
Definition: Vector3D.h:27
virtual TrackingRecHit * clone() const =0
Definition: DetId.h:18
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:213
tuple tracks
Definition: testEve_cfg.py:39
const T & get() const
Definition: EventSetup.h:56
bool isValid() const
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::GlobalCoordinateSystemTag > GlobalPoint
point in global coordinate system
Definition: Point3D.h:17
#define begin
Definition: vmac.h:30
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:204
CosmicTrackSplitter(const edm::ParameterSet &iConfig)
DetId geographicalId() const
volatile std::atomic< bool > shutdown_flag false
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
virtual LocalPoint localPosition() const =0
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
void reserve(size_t)
Definition: OwnVector.h:274
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
TrajectoryStateOnSurface innerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field, bool withErr=true)
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109