CMS 3D CMS Logo

GeneralTracksImporterWithVeto.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  veto_(sumes.consumes<reco::PFRecTrackCollection>(conf.getParameter<edm::InputTag>("veto"))),
19  muons_(sumes.consumes<reco::MuonCollection>(conf.getParameter<edm::InputTag>("muonSrc"))),
20  DPtovPtCut_(conf.getParameter<std::vector<double> >("DPtOverPtCuts_byTrackAlgo")),
21  NHitCut_(conf.getParameter<std::vector<unsigned> >("NHitCuts_byTrackAlgo")),
22  useTiming_(conf.existsAs<edm::InputTag>("timeValueMap")),
23  srcTime_(useTiming_ ? sumes.consumes<edm::ValueMap<float>>(conf.getParameter<edm::InputTag>("timeValueMap")) : edm::EDGetTokenT<edm::ValueMap<float>>()),
24  srcTimeError_(useTiming_ ? sumes.consumes<edm::ValueMap<float>>(conf.getParameter<edm::InputTag>("timeErrorMap")) : edm::EDGetTokenT<edm::ValueMap<float>>()),
25  useIterTracking_(conf.getParameter<bool>("useIterativeTracking")),
26  cleanBadConvBrems_(conf.existsAs<bool>("cleanBadConvertedBrems") ? conf.getParameter<bool>("cleanBadConvertedBrems") : false),
27  debug_(conf.getUntrackedParameter<bool>("debug",false)) {
28 
29  pfmu_ = std::unique_ptr<PFMuonAlgo>(new PFMuonAlgo());
30  pfmu_->setParameters(conf);
31 
32  }
33 
34  void importToBlock( const edm::Event& ,
35  ElementList& ) const override;
36 
37 private:
38  int muAssocToTrack( const reco::TrackRef& trackref,
39  const edm::Handle<reco::MuonCollection>& muonh) const;
40 
43  const std::vector<double> DPtovPtCut_;
44  const std::vector<unsigned> NHitCut_;
45  const bool useTiming_;
48 
49  std::unique_ptr<PFMuonAlgo> pfmu_;
50 
51 };
52 
55  "GeneralTracksImporterWithVeto");
56 
62  e.getByToken(src_,tracks);
64  e.getByToken(veto_,vetosH);
65  const auto& vetos = *vetosH;
66  std::unordered_set<unsigned> vetoed;
67  for(unsigned i = 0; i < vetos.size(); ++i ) {
68  vetoed.insert(vetos[i].trackRef().key());
69  }
71  e.getByToken(muons_,muons);
72  elems.reserve(elems.size() + tracks->size());
73  std::vector<bool> mask(tracks->size(),true);
74  reco::MuonRef muonref;
75  edm::Handle<edm::ValueMap<float>> timeH, timeErrH;
76  if (useTiming_) {
77  e.getByToken(srcTime_, timeH);
78  e.getByToken(srcTimeError_, timeErrH);
79  }
80  // remove converted brems with bad pT resolution if requested
81  // this reproduces the old behavior of PFBlockAlgo
82  if( cleanBadConvBrems_ ) {
83  auto itr = elems.begin();
84  while( itr != elems.end() ) {
85  if( (*itr)->type() == reco::PFBlockElement::TRACK ) {
86  const reco::PFBlockElementTrack* trkel =
87  static_cast<reco::PFBlockElementTrack*>(itr->get());
88  const reco::ConversionRefVector& cRef = trkel->convRefs();
89  const reco::PFDisplacedTrackerVertexRef& dvRef =
92  trkel->V0Ref();
93  // if there is no displaced vertex reference and it is marked
94  // as a conversion it's gotta be a converted brem
96  cRef.size() == 0 && dvRef.isNull() && v0Ref.isNull() ) {
97  // if the Pt resolution is bad we kill this element
99  itr = elems.erase(itr);
100  continue;
101  }
102  }
103  }
104  ++itr;
105  } // loop on existing elements
106  }
107  // preprocess existing tracks in the element list and create a mask
108  // so that we do not import tracks twice, tag muons we find
109  // in this collection
110  auto TKs_end = std::partition(elems.begin(),elems.end(),
111  [](const ElementType& a){
112  return a->type() == reco::PFBlockElement::TRACK;
113  });
114  auto btk_elems = elems.begin();
115  auto btrack = tracks->cbegin();
116  auto etrack = tracks->cend();
117  for( auto track = btrack; track != etrack; ++track) {
118  auto tk_elem = std::find_if(btk_elems,TKs_end,
119  [&](const ElementType& a){
120  return ( a->trackRef() ==
121  track->trackRef() );
122  });
123  if( tk_elem != TKs_end ) {
124  mask[std::distance(tracks->cbegin(),track)] = false;
125  // check and update if this track is a muon
126  const int muId = muAssocToTrack( (*tk_elem)->trackRef(), muons );
127  if( muId != -1 ) {
128  muonref= reco::MuonRef( muons, muId );
129  if( PFMuonAlgo::isLooseMuon(muonref) || PFMuonAlgo::isMuon(muonref) ) {
130  static_cast<reco::PFBlockElementTrack*>(tk_elem->get())->setMuonRef(muonref);
131  }
132  }
133  }
134  }
135  // now we actually insert tracks, again tagging muons along the way
136  reco::PFRecTrackRef pftrackref;
137  reco::PFBlockElementTrack* trkElem = NULL;
138  for( auto track = btrack; track != etrack; ++track) {
139  const unsigned idx = std::distance(btrack,track);
140  // since we already set muon refs in the previously imported tracks,
141  // here we can skip everything that is already imported
142  if( !mask[idx] ) continue;
143  muonref = reco::MuonRef();
144  pftrackref = reco::PFRecTrackRef(tracks,idx);
145  // Get the eventual muon associated to this track
146  const int muId = muAssocToTrack( pftrackref->trackRef(), muons );
147  bool thisIsAPotentialMuon = false;
148  if( muId != -1 ) {
149  muonref= reco::MuonRef( muons, muId );
150  thisIsAPotentialMuon = ( (pfmu_->hasValidTrack(muonref,true)&&PFMuonAlgo::isLooseMuon(muonref)) ||
151  (pfmu_->hasValidTrack(muonref,false)&&PFMuonAlgo::isMuon(muonref)));
152  }
153  if(thisIsAPotentialMuon || PFTrackAlgoTools::goodPtResolution( pftrackref->trackRef(), DPtovPtCut_, NHitCut_, useIterTracking_, debug_ ) ) {
154  trkElem = new reco::PFBlockElementTrack( pftrackref );
155  if (thisIsAPotentialMuon && debug_) {
156  std::cout << "Potential Muon P " << pftrackref->trackRef()->p()
157  << " pt " << pftrackref->trackRef()->p() << std::endl;
158  }
159  if( muId != -1 ) trkElem->setMuonRef(muonref);
160  if ( useTiming_ ) {
161  trkElem->setTime( (*timeH)[pftrackref->trackRef()], (*timeErrH)[pftrackref->trackRef()] );
162  }
163  if( vetoed.count(pftrackref->trackRef().key()) == 0 || muonref.isNonnull()){
164  elems.emplace_back(trkElem);
165  }
166  }
167  }
168  elems.shrink_to_fit();
169 }
170 
173  const edm::Handle<reco::MuonCollection>& muonh) const {
174  auto muon = std::find_if(muonh->cbegin(),muonh->cend(),
175  [&](const reco::Muon& m) {
176  return ( m.track().isNonnull() &&
177  m.track() == trackref );
178  });
179  return ( muon != muonh->cend() ? std::distance(muonh->cbegin(),muon) : -1 );
180 }
const VertexCompositeCandidateRef & V0Ref() const
const reco::TrackRef & trackRef() const
int muAssocToTrack(const reco::TrackRef &trackref, const edm::Handle< reco::MuonCollection > &muonh) const
static bool isMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:155
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
edm::EDGetTokenT< reco::MuonCollection > muons_
#define NULL
Definition: scimark2.h:8
edm::EDGetTokenT< reco::PFRecTrackCollection > veto_
key_type key() const
Accessor for product key.
Definition: Ref.h:265
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
const ConversionRefVector & convRefs() const
edm::EDGetTokenT< reco::PFRecTrackCollection > src_
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
edm::EDGetTokenT< edm::ValueMap< float > > srcTimeError_
bool isNull() const
Checks for null.
Definition: Ref.h:250
virtual bool trackType(TrackType trType) const
GeneralTracksImporterWithVeto(const edm::ParameterSet &conf, edm::ConsumesCollector &sumes)
void setMuonRef(const MuonRef &muref)
reference to the Muon
void importToBlock(const edm::Event &, ElementList &) const override
static bool isLooseMuon(const reco::PFBlockElement &elt)
Definition: PFMuonAlgo.cc:168
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
const PFDisplacedTrackerVertexRef & displacedVertexRef(TrackType trType) const
size_type size() const
Size of the RefVector.
Definition: RefVector.h:107
#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
void setTime(float time, float timeError=0.f)
the timing information
const std::vector< unsigned > NHitCut_
edm::EDGetTokenT< edm::ValueMap< float > > srcTime_