test
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 
151  const double P = trackref->p();
152  const double Pt = trackref->pt();
153  const double DPt = trackref->ptError();
154  const unsigned int NHit =
155  trackref->hitPattern().trackerLayersWithMeasurement();
156  const unsigned int NLostHit =
157  trackref->hitPattern().trackerLayersWithoutMeasurement(reco::HitPattern::TRACK_HITS);
158  const unsigned int LostHits = trackref->numberOfLostHits();
159  const double sigmaHad = sqrt(1.20*1.20/P+0.06*0.06) / (1.+LostHits);
160 
161  // Protection against 0 momentum tracks
162  if ( P < 0.05 ) return false;
163 
164  if (_debug) std::cout << " PFBlockAlgo: PFrecTrack->Track Pt= "
165  << Pt << " DPt = " << DPt << std::endl;
166 
167 
168  double dptCut = PFTrackAlgoTools::dPtCut(trackref->algo(),_DPtovPtCut,_useIterTracking);
169  unsigned int nhitCut = PFTrackAlgoTools::nHitCut(trackref->algo(),_NHitCut,_useIterTracking);
170 
171  if ( ( dptCut > 0. &&
172  DPt/Pt > dptCut*sigmaHad ) ||
173  NHit < nhitCut ) {
174  if (_debug) std::cout << " PFBlockAlgo: skip badly measured track"
175  << ", P = " << P
176  << ", Pt = " << Pt
177  << " DPt = " << DPt
178  << ", N(hits) = " << NHit << " (Lost : " << LostHits << "/" << NLostHit << ")"
179  << ", Algo = " << trackref->algo()
180  << std::endl;
181  if (_debug) std::cout << " cut is DPt/Pt < " << dptCut * sigmaHad << std::endl;
182  if (_debug) std::cout << " cut is NHit >= " << nhitCut << std::endl;
183  return false;
184  }
185 
186  return true;
187 }
188 
191  const edm::Handle<reco::MuonCollection>& muonh) const {
192  auto muon = std::find_if(muonh->cbegin(),muonh->cend(),
193  [&](const reco::Muon& m) {
194  return ( m.track().isNonnull() &&
195  m.track() == trackref );
196  });
197  return ( muon != muonh->cend() ? std::distance(muonh->cbegin(),muon) : -1 );
198 }
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