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  tracks_(iConfig.getParameter<edm::InputTag>("tracks")),
110  tjTag_(iConfig.getParameter<edm::InputTag>("tjTkAssociationMapTag") ),
111  minimumHits_(iConfig.getParameter<uint32_t>("minimumHits")),
112  replaceWithInactiveHits_(iConfig.getParameter<bool>("replaceWithInactiveHits")),
113  stripFrontInvalidHits_(iConfig.getParameter<bool>("stripFrontInvalidHits")),
114  stripBackInvalidHits_( iConfig.getParameter<bool>("stripBackInvalidHits") ),
115  stripAllInvalidHits_( iConfig.getParameter<bool>("stripAllInvalidHits") ),
116  excludePixelHits_( iConfig.getParameter<bool>("excludePixelHits") ),
117  dZcut_(iConfig.getParameter<double>("dzCut") ),
118  dXYcut_(iConfig.getParameter<double>("dxyCut") ),
119  detsToIgnore_( iConfig.getParameter<std::vector<uint32_t> >("detsToIgnore") )
120  {
121  // sanity check
123  throw cms::Exception("Configuration") << "Inconsistent Configuration: you can't set both 'stripAllInvalidHits' and 'replaceWithInactiveHits' to true\n";
124  }
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.getByLabel(tracks_, tracks);
145 
146  // also need trajectories ...
147  // Retrieve trajectories and tracks from the event
148  // -> merely skip if collection is empty
150  iEvent.getByLabel( tjTag_, 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->get();
196  const TrackingRecHit* fHit = fIt->get();
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->get();
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->get();
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->get(); // 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(detid, 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 
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 
396  TrajectorySeed seed(state, TrackCandidate::RecHitContainer(), pdir);
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)
int i
Definition: DBlmapReader.cc:9
const Vector & momentum() const
track momentum vector
Definition: TrackBase.h:150
TrackCandidate makeCandidate(const reco::Track &tk, std::vector< TrackingRecHit * >::iterator hitsBegin, std::vector< TrackingRecHit * >::iterator hitsEnd)
TrajectoryStateOnSurface innerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field)
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< TrackCandidate > TrackCandidateCollection
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
Type type() const
edm::ESHandle< TrackerGeometry > theGeometry
PropagationDirection
std::pair< const_iterator, const_iterator > range
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:47
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:42
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
DataContainer const & measurements() const
Definition: Trajectory.h:215
void push_back(D *&d)
Definition: OwnVector.h:273
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
edm::ESHandle< MagneticField > theMagField
#define end
Definition: vmac.h:38
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:39
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
#define LogTrace(id)
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::GlobalCoordinateSystemTag > GlobalVector
vector in glovbal coordinate system
Definition: Vector3D.h:28
virtual TrackingRecHit * clone() const =0
Definition: DetId.h:20
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:112
tuple tracks
Definition: testEve_cfg.py:39
const T & get() const
Definition: EventSetup.h:55
TrajectoryStateOnSurface outerStateOnSurface(const reco::Track &tk, const TrackingGeometry &geom, const MagneticField *field)
bool isValid() const
char state
Definition: procUtils.cc:75
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float >, ROOT::Math::GlobalCoordinateSystemTag > GlobalPoint
point in global coordinate system
Definition: Point3D.h:18
#define begin
Definition: vmac.h:31
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:105
CosmicTrackSplitter(const edm::ParameterSet &iConfig)
const reco::PFCandidateRefVector & tracks_
DetId geographicalId() const
Detector det() const
get the detector field from this detid
Definition: DetId.h:37
virtual LocalPoint localPosition() const =0
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:61
void reserve(size_t)
Definition: OwnVector.h:267
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65