CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GeneralTracksImporter.cc
Go to the documentation of this file.
10 
12 public:
14  edm::ConsumesCollector& sumes) :
15  BlockElementImporterBase(conf,sumes),
16  _src(sumes.consumes<reco::PFRecTrackCollection>(conf.getParameter<edm::InputTag>("source"))),
17  _muons(sumes.consumes<reco::MuonCollection>(conf.getParameter<edm::InputTag>("muonSrc"))),
18  _DPtovPtCut(conf.getParameter<std::vector<double> >("DPtOverPtCuts_byTrackAlgo")),
19  _NHitCut(conf.getParameter<std::vector<unsigned> >("NHitCuts_byTrackAlgo")),
20  _useIterTracking(conf.getParameter<bool>("useIterativeTracking")),
21  _cleanBadConvBrems(conf.existsAs<bool>("cleanBadConvertedBrems") ? conf.getParameter<bool>("cleanBadConvertedBrems") : false),
22  _debug(conf.getUntrackedParameter<bool>("debug",false)) {
23 
24  pfmu_ = std::unique_ptr<PFMuonAlgo>(new PFMuonAlgo());
25  pfmu_->setParameters(conf);
26 
27  }
28 
29  void importToBlock( const edm::Event& ,
30  ElementList& ) const override;
31 
32 private:
33  bool goodPtResolution( const reco::TrackRef& trackref) const;
34  int muAssocToTrack( const reco::TrackRef& trackref,
35  const edm::Handle<reco::MuonCollection>& muonh) const;
36 
39  const std::vector<double> _DPtovPtCut;
40  const std::vector<unsigned> _NHitCut;
42 
43  std::unique_ptr<PFMuonAlgo> pfmu_;
44 
45 };
46 
49  "GeneralTracksImporter");
50 
56  e.getByToken(_src,tracks);
58  e.getByToken(_muons,muons);
59  elems.reserve(elems.size() + tracks->size());
60  std::vector<bool> mask(tracks->size(),true);
61  reco::MuonRef muonref;
62  // remove converted brems with bad pT resolution if requested
63  // this reproduces the old behavior of PFBlockAlgo
64  if( _cleanBadConvBrems ) {
65  auto itr = elems.begin();
66  while( itr != elems.end() ) {
67  if( (*itr)->type() == reco::PFBlockElement::TRACK ) {
68  const reco::PFBlockElementTrack* trkel =
69  static_cast<reco::PFBlockElementTrack*>(itr->get());
70  const reco::ConversionRefVector& cRef = trkel->convRefs();
71  const reco::PFDisplacedTrackerVertexRef& dvRef =
74  trkel->V0Ref();
75  // if there is no displaced vertex reference and it is marked
76  // as a conversion it's gotta be a converted brem
78  cRef.size() == 0 && dvRef.isNull() && v0Ref.isNull() ) {
79  // if the Pt resolution is bad we kill this element
80  if( !goodPtResolution( trkel->trackRef() ) ) {
81  itr = elems.erase(itr);
82  continue;
83  }
84  }
85  }
86  ++itr;
87  } // loop on existing elements
88  }
89  // preprocess existing tracks in the element list and create a mask
90  // so that we do not import tracks twice, tag muons we find
91  // in this collection
92  auto TKs_end = std::partition(elems.begin(),elems.end(),
93  [](const ElementType& a){
94  return a->type() == reco::PFBlockElement::TRACK;
95  });
96  auto btk_elems = elems.begin();
97  auto btrack = tracks->cbegin();
98  auto etrack = tracks->cend();
99  for( auto track = btrack; track != etrack; ++track) {
100  auto tk_elem = std::find_if(btk_elems,TKs_end,
101  [&](const ElementType& a){
102  return ( a->trackRef() ==
103  track->trackRef() );
104  });
105  if( tk_elem != TKs_end ) {
106  mask[std::distance(tracks->cbegin(),track)] = false;
107  // check and update if this track is a muon
108  const int muId = muAssocToTrack( (*tk_elem)->trackRef(), muons );
109  if( muId != -1 ) {
110  muonref= reco::MuonRef( muons, muId );
111  if( PFMuonAlgo::isLooseMuon(muonref) || PFMuonAlgo::isMuon(muonref) ) {
112  static_cast<reco::PFBlockElementTrack*>(tk_elem->get())->setMuonRef(muonref);
113  }
114  }
115  }
116  }
117  // now we actually insert tracks, again tagging muons along the way
118  reco::PFRecTrackRef pftrackref;
119  reco::PFBlockElementTrack* trkElem = NULL;
120  for( auto track = btrack; track != etrack; ++track) {
121  const unsigned idx = std::distance(btrack,track);
122  // since we already set muon refs in the previously imported tracks,
123  // here we can skip everything that is already imported
124  if( !mask[idx] ) continue;
125  muonref = reco::MuonRef();
126  pftrackref = reco::PFRecTrackRef(tracks,idx);
127  // Get the eventual muon associated to this track
128  const int muId = muAssocToTrack( pftrackref->trackRef(), muons );
129  bool thisIsAPotentialMuon = false;
130  if( muId != -1 ) {
131  muonref= reco::MuonRef( muons, muId );
132  thisIsAPotentialMuon = ( (pfmu_->hasValidTrack(muonref,true)&&PFMuonAlgo::isLooseMuon(muonref)) ||
133  (pfmu_->hasValidTrack(muonref,false)&&PFMuonAlgo::isMuon(muonref)));
134  }
135  if(thisIsAPotentialMuon || goodPtResolution( pftrackref->trackRef() ) ) {
136  trkElem = new reco::PFBlockElementTrack( pftrackref );
137  if (thisIsAPotentialMuon && _debug) {
138  std::cout << "Potential Muon P " << pftrackref->trackRef()->p()
139  << " pt " << pftrackref->trackRef()->p() << std::endl;
140  }
141  if( muId != -1 ) trkElem->setMuonRef(muonref);
142  elems.emplace_back(trkElem);
143  }
144  }
145  elems.shrink_to_fit();
146 }
147 
149 goodPtResolution( const reco::TrackRef& trackref) const {
150  //recheck that the track is high purity!
151  if (!trackref->quality(reco::TrackBase::highPurity))
152  return false;
153 
154 
155  const double P = trackref->p();
156  const double Pt = trackref->pt();
157  const double DPt = trackref->ptError();
158  const unsigned int NHit =
159  trackref->hitPattern().trackerLayersWithMeasurement();
160  const unsigned int NLostHit =
161  trackref->hitPattern().trackerLayersWithoutMeasurement(reco::HitPattern::TRACK_HITS);
162  const unsigned int LostHits = trackref->numberOfLostHits();
163  const double sigmaHad = sqrt(1.20*1.20/P+0.06*0.06) / (1.+LostHits);
164 
165  // Protection against 0 momentum tracks
166  if ( P < 0.05 ) return false;
167 
168  if (_debug) std::cout << " PFBlockAlgo: PFrecTrack->Track Pt= "
169  << Pt << " DPt = " << DPt << std::endl;
170 
171 
172  double dptCut = PFTrackAlgoTools::dPtCut(trackref->algo(),_DPtovPtCut,_useIterTracking);
173  unsigned int nhitCut = PFTrackAlgoTools::nHitCut(trackref->algo(),_NHitCut,_useIterTracking);
174 
175  if ( ( dptCut > 0. &&
176  DPt/Pt > dptCut*sigmaHad ) ||
177  NHit < nhitCut ) {
178  if (_debug) std::cout << " PFBlockAlgo: skip badly measured track"
179  << ", P = " << P
180  << ", Pt = " << Pt
181  << " DPt = " << DPt
182  << ", N(hits) = " << NHit << " (Lost : " << LostHits << "/" << NLostHit << ")"
183  << ", Algo = " << trackref->algo()
184  << std::endl;
185  if (_debug) std::cout << " cut is DPt/Pt < " << dptCut * sigmaHad << std::endl;
186  if (_debug) std::cout << " cut is NHit >= " << nhitCut << std::endl;
187  return false;
188  }
189 
190  return true;
191 }
192 
195  const edm::Handle<reco::MuonCollection>& muonh) const {
196  auto muon = std::find_if(muonh->cbegin(),muonh->cend(),
197  [&](const reco::Muon& m) {
198  return ( m.track().isNonnull() &&
199  m.track() == trackref );
200  });
201  return ( muon != muonh->cend() ? std::distance(muonh->cbegin(),muon) : -1 );
202 }
const VertexCompositeCandidateRef & V0Ref() const
const reco::TrackRef & trackRef() const
GeneralTracksImporter(const edm::ParameterSet &conf, edm::ConsumesCollector &sumes)
static bool isMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:155
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
std::unique_ptr< PFMuonAlgo > pfmu_
const std::vector< double > _DPtovPtCut
#define P
#define NULL
Definition: scimark2.h:8
int muAssocToTrack(const reco::TrackRef &trackref, const edm::Handle< reco::MuonCollection > &muonh) const
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
bool goodPtResolution(const reco::TrackRef &trackref) const
void importToBlock(const edm::Event &, ElementList &) const override
unsigned int nHitCut(const reco::TrackBase::TrackAlgorithm &, const std::vector< unsigned int > &, bool)
const std::vector< unsigned > _NHitCut
T sqrt(T t)
Definition: SSEVec.h:18
const ConversionRefVector & convRefs() const
edm::EDGetTokenT< reco::MuonCollection > _muons
edm::Ref< PFRecTrackCollection > PFRecTrackRef
persistent reference to PFRecTrack objects
Definition: PFRecTrackFwd.h:15
edm::Ref< MuonCollection > MuonRef
presistent reference to a Muon
Definition: MuonFwd.h:13
bool isNull() const
Checks for null.
Definition: Ref.h:249
virtual bool trackType(TrackType trType) const
void setMuonRef(const MuonRef &muref)
reference to the Muon
tuple tracks
Definition: testEve_cfg.py:39
static bool isLooseMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:168
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
double dPtCut(const reco::TrackBase::TrackAlgorithm &, const std::vector< double > &, bool)
edm::EDGetTokenT< reco::PFRecTrackCollection > _src
tuple muons
Definition: patZpeak.py:38
double a
Definition: hdecay.h:121
const PFDisplacedTrackerVertexRef & displacedVertexRef(TrackType trType) const
size_type size() const
Size of the RefVector.
Definition: RefVector.h:107
tuple cout
Definition: gather_cfg.py:145
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< std::unique_ptr< reco::PFBlockElement > > ElementList
volatile std::atomic< bool > shutdown_flag false
std::vector< PFRecTrack > PFRecTrackCollection
collection of PFRecTrack objects
Definition: PFRecTrackFwd.h:9