CMS 3D CMS Logo

GeneralTracksImporter.cc
Go to the documentation of this file.
11 
13 public:
15  edm::ConsumesCollector& sumes) :
16  BlockElementImporterBase(conf,sumes),
17  src_(sumes.consumes<reco::PFRecTrackCollection>(conf.getParameter<edm::InputTag>("source"))),
18  muons_(sumes.consumes<reco::MuonCollection>(conf.getParameter<edm::InputTag>("muonSrc"))),
19  DPtovPtCut_(conf.getParameter<std::vector<double> >("DPtOverPtCuts_byTrackAlgo")),
20  NHitCut_(conf.getParameter<std::vector<unsigned> >("NHitCuts_byTrackAlgo")),
21  useIterTracking_(conf.getParameter<bool>("useIterativeTracking")),
22  cleanBadConvBrems_(conf.existsAs<bool>("cleanBadConvertedBrems") ? conf.getParameter<bool>("cleanBadConvertedBrems") : false),
23  debug_(conf.getUntrackedParameter<bool>("debug",false)) {
24 
25  pfmu_ = std::unique_ptr<PFMuonAlgo>(new PFMuonAlgo());
26  pfmu_->setParameters(conf);
27 
28  }
29 
30  void importToBlock( const edm::Event& ,
31  ElementList& ) const override;
32 
33 private:
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 
63  // remove converted brems with bad pT resolution if requested
64  // this reproduces the old behavior of PFBlockAlgo
65  if( cleanBadConvBrems_ ) {
66  auto itr = elems.begin();
67  while( itr != elems.end() ) {
68  if( (*itr)->type() == reco::PFBlockElement::TRACK ) {
69  const reco::PFBlockElementTrack* trkel =
70  static_cast<reco::PFBlockElementTrack*>(itr->get());
71  const reco::ConversionRefVector& cRef = trkel->convRefs();
72  const reco::PFDisplacedTrackerVertexRef& dvRef =
75  trkel->V0Ref();
76  // if there is no displaced vertex reference and it is marked
77  // as a conversion it's gotta be a converted brem
79  cRef.empty() && dvRef.isNull() && v0Ref.isNull() ) {
80  // if the Pt resolution is bad we kill this element
82  itr = elems.erase(itr);
83  continue;
84  }
85  }
86  }
87  ++itr;
88  } // loop on existing elements
89  }
90  // preprocess existing tracks in the element list and create a mask
91  // so that we do not import tracks twice, tag muons we find
92  // in this collection
93  auto TKs_end = std::partition(elems.begin(),elems.end(),
94  [](const ElementType& a){
95  return a->type() == reco::PFBlockElement::TRACK;
96  });
97  auto btk_elems = elems.begin();
98  auto btrack = tracks->cbegin();
99  auto etrack = tracks->cend();
100  for( auto track = btrack; track != etrack; ++track) {
101  auto tk_elem = std::find_if(btk_elems,TKs_end,
102  [&](const ElementType& a){
103  return ( a->trackRef() ==
104  track->trackRef() );
105  });
106  if( tk_elem != TKs_end ) {
107  mask[std::distance(tracks->cbegin(),track)] = false;
108  // check and update if this track is a muon
109  const int muId = muAssocToTrack( (*tk_elem)->trackRef(), muons );
110  if( muId != -1 ) {
111  muonref= reco::MuonRef( muons, muId );
112  if( PFMuonAlgo::isLooseMuon(muonref) || PFMuonAlgo::isMuon(muonref) ) {
113  static_cast<reco::PFBlockElementTrack*>(tk_elem->get())->setMuonRef(muonref);
114  }
115  }
116  }
117  }
118  // now we actually insert tracks, again tagging muons along the way
119  reco::PFRecTrackRef pftrackref;
120  reco::PFBlockElementTrack* trkElem = nullptr;
121  for( auto track = btrack; track != etrack; ++track) {
122  const unsigned idx = std::distance(btrack,track);
123  // since we already set muon refs in the previously imported tracks,
124  // here we can skip everything that is already imported
125  if( !mask[idx] ) continue;
126  muonref = reco::MuonRef();
127  pftrackref = reco::PFRecTrackRef(tracks,idx);
128  // Get the eventual muon associated to this track
129  const int muId = muAssocToTrack( pftrackref->trackRef(), muons );
130  bool thisIsAPotentialMuon = false;
131  if( muId != -1 ) {
132  muonref= reco::MuonRef( muons, muId );
133  thisIsAPotentialMuon = ( (pfmu_->hasValidTrack(muonref,true)&&PFMuonAlgo::isLooseMuon(muonref)) ||
134  (pfmu_->hasValidTrack(muonref,false)&&PFMuonAlgo::isMuon(muonref)));
135  }
136  if(thisIsAPotentialMuon || PFTrackAlgoTools::goodPtResolution( pftrackref->trackRef(), DPtovPtCut_, NHitCut_, useIterTracking_, debug_ ) ) {
137  trkElem = new reco::PFBlockElementTrack( pftrackref );
138  if (thisIsAPotentialMuon && debug_) {
139  std::cout << "Potential Muon P " << pftrackref->trackRef()->p()
140  << " pt " << pftrackref->trackRef()->p() << std::endl;
141  }
142  if( muId != -1 ) trkElem->setMuonRef(muonref);
143  elems.emplace_back(trkElem);
144  }
145  }
146  elems.shrink_to_fit();
147 }
148 
151  const edm::Handle<reco::MuonCollection>& muonh) const {
152  auto muon = std::find_if(muonh->cbegin(),muonh->cend(),
153  [&](const reco::Muon& m) {
154  return ( m.track().isNonnull() &&
155  m.track() == trackref );
156  });
157  return ( muon != muonh->cend() ? std::distance(muonh->cbegin(),muon) : -1 );
158 }
void importToBlock(const edm::Event &, ElementList &) const override
GeneralTracksImporter(const edm::ParameterSet &conf, edm::ConsumesCollector &sumes)
const std::vector< double > DPtovPtCut_
edm::EDGetTokenT< reco::MuonCollection > muons_
static bool isMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:155
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
const reco::TrackRef & trackRef() const override
std::unique_ptr< PFMuonAlgo > pfmu_
const ConversionRefVector & convRefs() const override
bool trackType(TrackType trType) const override
int muAssocToTrack(const reco::TrackRef &trackref, const edm::Handle< reco::MuonCollection > &muonh) const
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:104
edm::EDGetTokenT< reco::PFRecTrackCollection > src_
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
edm::Ref< PFRecTrackCollection > PFRecTrackRef
persistent reference to PFRecTrack objects
Definition: PFRecTrackFwd.h:15
const VertexCompositeCandidateRef & V0Ref() const override
edm::Ref< MuonCollection > MuonRef
presistent reference to a Muon
Definition: MuonFwd.h:13
const std::vector< unsigned > NHitCut_
bool isNull() const
Checks for null.
Definition: Ref.h:250
const PFDisplacedTrackerVertexRef & displacedVertexRef(TrackType trType) const override
static bool isLooseMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:168
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
void setMuonRef(const MuonRef &muref) override
reference to the Muon
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< std::unique_ptr< reco::PFBlockElement > > ElementList
bool goodPtResolution(const reco::TrackRef &, const std::vector< double > &DPtovPtCut, const std::vector< unsigned > &NHitCut, bool useIterTracking, bool debug=false)
std::vector< PFRecTrack > PFRecTrackCollection
collection of PFRecTrack objects
Definition: PFRecTrackFwd.h:9