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(conf));
26 
27  }
28 
29  void importToBlock( const edm::Event& ,
30  ElementList& ) const override;
31 
32 private:
33  int muAssocToTrack( const reco::TrackRef& trackref,
34  const edm::Handle<reco::MuonCollection>& muonh) const;
35 
38  const std::vector<double> DPtovPtCut_;
39  const std::vector<unsigned> NHitCut_;
41 
42  std::unique_ptr<PFMuonAlgo> pfmu_;
43 
44 };
45 
48  "GeneralTracksImporter");
49 
54  auto tracks = e.getHandle(src_);
55  auto muons = e.getHandle(muons_);
56  elems.reserve(elems.size() + tracks->size());
57  std::vector<bool> mask(tracks->size(),true);
58  reco::MuonRef muonref;
59 
60  // remove converted brems with bad pT resolution if requested
61  // this reproduces the old behavior of PFBlockAlgo
62  if( cleanBadConvBrems_ ) {
63  auto itr = elems.begin();
64  while( itr != elems.end() ) {
65  if( (*itr)->type() == reco::PFBlockElement::TRACK ) {
66  const reco::PFBlockElementTrack* trkel =
67  static_cast<reco::PFBlockElementTrack*>(itr->get());
68  const reco::ConversionRefVector& cRef = trkel->convRefs();
69  const reco::PFDisplacedTrackerVertexRef& dvRef =
72  trkel->V0Ref();
73  // if there is no displaced vertex reference and it is marked
74  // as a conversion it's gotta be a converted brem
76  cRef.empty() && dvRef.isNull() && v0Ref.isNull() ) {
77  // if the Pt resolution is bad we kill this element
79  itr = elems.erase(itr);
80  continue;
81  }
82  }
83  }
84  ++itr;
85  } // loop on existing elements
86  }
87  // preprocess existing tracks in the element list and create a mask
88  // so that we do not import tracks twice, tag muons we find
89  // in this collection
90  auto TKs_end = std::partition(elems.begin(),elems.end(),
91  [](const ElementType& a){
92  return a->type() == reco::PFBlockElement::TRACK;
93  });
94  auto btk_elems = elems.begin();
95  auto btrack = tracks->cbegin();
96  auto etrack = tracks->cend();
97  for( auto track = btrack; track != etrack; ++track) {
98  auto tk_elem = std::find_if(btk_elems,TKs_end,
99  [&](const ElementType& a){
100  return ( a->trackRef() ==
101  track->trackRef() );
102  });
103  if( tk_elem != TKs_end ) {
104  mask[std::distance(tracks->cbegin(),track)] = false;
105  // check and update if this track is a muon
106  const int muId = muAssocToTrack( (*tk_elem)->trackRef(), muons );
107  if( muId != -1 ) {
108  muonref= reco::MuonRef( muons, muId );
109  if( PFMuonAlgo::isLooseMuon(muonref) || PFMuonAlgo::isMuon(muonref) ) {
110  static_cast<reco::PFBlockElementTrack*>(tk_elem->get())->setMuonRef(muonref);
111  }
112  }
113  }
114  }
115  // now we actually insert tracks, again tagging muons along the way
116  reco::PFRecTrackRef pftrackref;
117  reco::PFBlockElementTrack* trkElem = nullptr;
118  for( auto track = btrack; track != etrack; ++track) {
119  const unsigned idx = std::distance(btrack,track);
120  // since we already set muon refs in the previously imported tracks,
121  // here we can skip everything that is already imported
122  if( !mask[idx] ) continue;
123  muonref = reco::MuonRef();
124  pftrackref = reco::PFRecTrackRef(tracks,idx);
125  // Get the eventual muon associated to this track
126  const int muId = muAssocToTrack( pftrackref->trackRef(), muons );
127  bool thisIsAPotentialMuon = false;
128  if( muId != -1 ) {
129  muonref= reco::MuonRef( muons, muId );
130  thisIsAPotentialMuon = ( (pfmu_->hasValidTrack(muonref,true)&&PFMuonAlgo::isLooseMuon(muonref)) ||
131  (pfmu_->hasValidTrack(muonref,false)&&PFMuonAlgo::isMuon(muonref)));
132  }
133  if(thisIsAPotentialMuon || PFTrackAlgoTools::goodPtResolution( pftrackref->trackRef(), DPtovPtCut_, NHitCut_, useIterTracking_, debug_ ) ) {
134  trkElem = new reco::PFBlockElementTrack( pftrackref );
135  if (thisIsAPotentialMuon && debug_) {
136  std::cout << "Potential Muon P " << pftrackref->trackRef()->p()
137  << " pt " << pftrackref->trackRef()->p() << std::endl;
138  }
139  if( muId != -1 ) trkElem->setMuonRef(muonref);
140  elems.emplace_back(trkElem);
141  }
142  }
143  elems.shrink_to_fit();
144 }
145 
148  const edm::Handle<reco::MuonCollection>& muonh) const {
149  auto muon = std::find_if(muonh->cbegin(),muonh->cend(),
150  [&](const reco::Muon& m) {
151  return ( m.track().isNonnull() &&
152  m.track() == trackref );
153  });
154  return ( muon != muonh->cend() ? std::distance(muonh->cbegin(),muon) : -1 );
155 }
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:66
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
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:539
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:248
const PFDisplacedTrackerVertexRef & displacedVertexRef(TrackType trType) const override
static bool isLooseMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:78
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