CMS 3D CMS Logo

PFRecoTauChargedHadronFromPFCandidatePlugin.cc
Go to the documentation of this file.
1 /*
2  * PFRecoTauChargedHadronFromPFCandidatePlugin
3  *
4  * Build PFRecoTauChargedHadron objects
5  * using charged PFCandidates and/or PFNeutralHadrons as input
6  *
7  * Author: Christian Veelken, LLR
8  *
9  */
10 
12 
27 
31 
36 
37 #include <memory>
38 
39 namespace reco {
40 
41  namespace tau {
42 
44  public:
47  // Return type is unique_ptr<ChargedHadronVector>
48  return_type operator()(const reco::Jet&) const override;
49  // Hook to update PV information
50  void beginEvent() override;
51 
52  private:
53  typedef std::vector<reco::CandidatePtr> CandPtrs;
54 
56 
58 
59  std::vector<int> inputParticleIds_; // type of candidates to clusterize
60 
76 
78  double bField_;
79 
81  };
82 
86  vertexAssociator_(pset.getParameter<edm::ParameterSet>("qualityCuts"), std::move(iC)),
87  qcuts_(nullptr),
88  bFieldToken_(iC.esConsumes()) {
89  edm::ParameterSet qcuts_pset = pset.getParameterSet("qualityCuts").getParameterSet("signalQualityCuts");
90  qcuts_ = new RecoTauQualityCuts(qcuts_pset);
91 
92  inputParticleIds_ = pset.getParameter<std::vector<int> >("chargedHadronCandidatesParticleIds");
93 
94  dRmergeNeutralHadronWrtChargedHadron_ = pset.getParameter<double>("dRmergeNeutralHadronWrtChargedHadron");
95  dRmergeNeutralHadronWrtNeutralHadron_ = pset.getParameter<double>("dRmergeNeutralHadronWrtNeutralHadron");
96  dRmergeNeutralHadronWrtElectron_ = pset.getParameter<double>("dRmergeNeutralHadronWrtElectron");
97  dRmergeNeutralHadronWrtOther_ = pset.getParameter<double>("dRmergeNeutralHadronWrtOther");
98  minBlockElementMatchesNeutralHadron_ = pset.getParameter<int>("minBlockElementMatchesNeutralHadron");
99  maxUnmatchedBlockElementsNeutralHadron_ = pset.getParameter<int>("maxUnmatchedBlockElementsNeutralHadron");
100  dRmergePhotonWrtChargedHadron_ = pset.getParameter<double>("dRmergePhotonWrtChargedHadron");
101  dRmergePhotonWrtNeutralHadron_ = pset.getParameter<double>("dRmergePhotonWrtNeutralHadron");
102  dRmergePhotonWrtElectron_ = pset.getParameter<double>("dRmergePhotonWrtElectron");
103  dRmergePhotonWrtOther_ = pset.getParameter<double>("dRmergePhotonWrtOther");
104  minBlockElementMatchesPhoton_ = pset.getParameter<int>("minBlockElementMatchesPhoton");
105  maxUnmatchedBlockElementsPhoton_ = pset.getParameter<int>("maxUnmatchedBlockElementsPhoton");
106  minMergeNeutralHadronEt_ = pset.getParameter<double>("minMergeNeutralHadronEt");
107  minMergeGammaEt_ = pset.getParameter<double>("minMergeGammaEt");
108  minMergeChargedHadronPt_ = pset.getParameter<double>("minMergeChargedHadronPt");
109 
110  verbosity_ = pset.getParameter<int>("verbosity");
111  }
112 
114 
115  // Update the primary vertex
118 
119  bField_ = evtSetup()->getData(bFieldToken_).inTesla(GlobalPoint(0, 0, 0)).z();
120  }
121 
122  namespace {
123  bool isMatchedByBlockElement(const reco::PFCandidate& pfCandidate1,
124  const reco::PFCandidate& pfCandidate2,
125  int minMatches1,
126  int minMatches2,
127  int maxUnmatchedBlockElements1plus2) {
128  reco::PFCandidate::ElementsInBlocks blockElements1 = pfCandidate1.elementsInBlocks();
129  int numBlocks1 = blockElements1.size();
130  reco::PFCandidate::ElementsInBlocks blockElements2 = pfCandidate2.elementsInBlocks();
131  int numBlocks2 = blockElements2.size();
132  int numBlocks_matched = 0;
133  for (reco::PFCandidate::ElementsInBlocks::const_iterator blockElement1 = blockElements1.begin();
134  blockElement1 != blockElements1.end();
135  ++blockElement1) {
136  bool isMatched = false;
137  for (reco::PFCandidate::ElementsInBlocks::const_iterator blockElement2 = blockElements2.begin();
138  blockElement2 != blockElements2.end();
139  ++blockElement2) {
140  if (blockElement1->first.id() == blockElement2->first.id() &&
141  blockElement1->first.key() == blockElement2->first.key() &&
142  blockElement1->second == blockElement2->second) {
143  isMatched = true;
144  }
145  }
146  if (isMatched)
147  ++numBlocks_matched;
148  }
149  assert(numBlocks_matched <= numBlocks1);
150  assert(numBlocks_matched <= numBlocks2);
151  if (numBlocks_matched >= minMatches1 && numBlocks_matched >= minMatches2 &&
152  ((numBlocks1 - numBlocks_matched) + (numBlocks2 - numBlocks_matched)) <= maxUnmatchedBlockElements1plus2) {
153  return true;
154  } else {
155  return false;
156  }
157  }
158  } // namespace
159 
161  const reco::Jet& jet) const {
162  if (verbosity_) {
163  edm::LogPrint("TauChHadronFromPF") << "<PFRecoTauChargedHadronFromPFCandidatePlugin::operator()>:";
164  edm::LogPrint("TauChHadronFromPF") << " pluginName = " << name();
165  }
166 
168 
169  // Get the candidates passing our quality cuts
172 
173  for (CandPtrs::iterator cand = candsVector.begin(); cand != candsVector.end(); ++cand) {
174  if (verbosity_) {
175  edm::LogPrint("TauChHadronFromPF")
176  << "processing PFCandidate: Pt = " << (*cand)->pt() << ", eta = " << (*cand)->eta()
177  << ", phi = " << (*cand)->phi() << " (pdgId = " << (*cand)->pdgId() << ", charge = " << (*cand)->charge()
178  << ")";
179  }
180 
182  if (std::abs((*cand)->charge()) > 0.5)
184  else
186  auto chargedHadron = std::make_unique<PFRecoTauChargedHadron>(**cand, algo);
187 
188  const reco::PFCandidate* pfCand = dynamic_cast<const reco::PFCandidate*>(&**cand);
189  if (pfCand) {
190  if (pfCand->trackRef().isNonnull())
191  chargedHadron->track_ = edm::refToPtr(pfCand->trackRef());
192  else if (pfCand->muonRef().isNonnull() && pfCand->muonRef()->innerTrack().isNonnull())
193  chargedHadron->track_ = edm::refToPtr(pfCand->muonRef()->innerTrack());
194  else if (pfCand->muonRef().isNonnull() && pfCand->muonRef()->globalTrack().isNonnull())
195  chargedHadron->track_ = edm::refToPtr(pfCand->muonRef()->globalTrack());
196  else if (pfCand->muonRef().isNonnull() && pfCand->muonRef()->outerTrack().isNonnull())
197  chargedHadron->track_ = edm::refToPtr(pfCand->muonRef()->outerTrack());
198  else if (pfCand->gsfTrackRef().isNonnull())
199  chargedHadron->track_ = edm::refToPtr(pfCand->gsfTrackRef());
200  } // TauReco@MiniAOD: Tracks only available dynamically, so no possiblity to save ref here; checked by code downstream
201 
202  chargedHadron->positionAtECALEntrance_ = atECALEntrance(&**cand, bField_);
203  chargedHadron->chargedPFCandidate_ = (*cand);
204  chargedHadron->addDaughter(*cand);
205 
206  int pdgId = std::abs(chargedHadron->chargedPFCandidate_->pdgId());
207 
209  for (const auto& jetConstituent : jet.daughterPtrVector()) {
210  // CV: take care of not double-counting energy in case "charged" PFCandidate is in fact a PFNeutralHadron
211  if (jetConstituent == chargedHadron->chargedPFCandidate_)
212  continue;
213 
214  int jetConstituentPdgId = std::abs(jetConstituent->pdgId());
215  if (!(jetConstituentPdgId == 130 || jetConstituentPdgId == 22))
216  continue;
217 
218  double dR = deltaR(atECALEntrance(jetConstituent.get(), bField_),
219  atECALEntrance(chargedHadron->chargedPFCandidate_.get(), bField_));
220  double dRmerge = -1.;
221  int minBlockElementMatches = 1000;
222  int maxUnmatchedBlockElements = 0;
223  double minMergeEt = 1.e+6;
224  if (jetConstituentPdgId == 130) {
225  if (pdgId == 211)
227  else if (pdgId == 130)
229  else if (pdgId == 11)
231  else
233  minBlockElementMatches = minBlockElementMatchesNeutralHadron_;
234  maxUnmatchedBlockElements = maxUnmatchedBlockElementsNeutralHadron_;
235  minMergeEt = minMergeNeutralHadronEt_;
236  } else if (jetConstituentPdgId == 22) {
237  if (pdgId == 211)
239  else if (pdgId == 130)
241  else if (pdgId == 11)
242  dRmerge = dRmergePhotonWrtElectron_;
243  else
244  dRmerge = dRmergePhotonWrtOther_;
245  minBlockElementMatches = minBlockElementMatchesPhoton_;
246  maxUnmatchedBlockElements = maxUnmatchedBlockElementsPhoton_;
247  minMergeEt = minMergeGammaEt_;
248  }
249 
250  if (jetConstituent->et() > minMergeEt) {
251  if (dR < dRmerge) {
252  chargedHadron->neutralPFCandidates_.push_back(jetConstituent);
253  chargedHadron->addDaughter(jetConstituent);
254  } else {
255  // TauReco@MiniAOD: No access to PF blocks at MiniAOD level, but the code below seems to have very minor impact
256  const reco::PFCandidate* pfJetConstituent =
257  dynamic_cast<const reco::PFCandidate*>(jetConstituent.get());
258  if (pfCand != nullptr && pfJetConstituent != nullptr) {
259  if (isMatchedByBlockElement(*pfJetConstituent,
260  *pfCand,
261  minBlockElementMatches,
262  minBlockElementMatches,
263  maxUnmatchedBlockElements)) {
264  chargedHadron->neutralPFCandidates_.push_back(jetConstituent);
265  chargedHadron->addDaughter(jetConstituent);
266  }
267  }
268  }
269  }
270  }
271  }
272 
274 
275  if (verbosity_) {
276  edm::LogPrint("TauChHadronFromPF") << *chargedHadron;
277  }
278  // Update the vertex
279  if (chargedHadron->daughterPtr(0).isNonnull())
280  chargedHadron->setVertex(chargedHadron->daughterPtr(0)->vertex());
281  output.push_back(std::move(chargedHadron));
282  }
283 
284  return output;
285  }
286 
287  } // namespace tau
288 } // namespace reco
289 
291 
294  "PFRecoTauChargedHadronFromPFCandidatePlugin");
std::vector< CandidatePtr > pfCandidates(const Jet &jet, int particleId, bool sort=true)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::vector< std::unique_ptr< PFRecoTauChargedHadron > > ChargedHadronVector
math::XYZPointF atECALEntrance(const reco::Candidate *part, double bField)
Ptr< typename C::value_type > refToPtr(Ref< C, typename C::value_type, refhelper::FindUsingAdvance< C, typename C::value_type > > const &ref)
Definition: RefToPtr.h:18
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
return_type operator()(const reco::Jet &) const override
Build a collection of chargedHadrons from objects in the input jet.
const edm::EventSetup * evtSetup() const
Base class for all types of Jets.
Definition: Jet.h:20
PFRecoTauChargedHadronFromPFCandidatePlugin(const edm::ParameterSet &, edm::ConsumesCollector &&iC)
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
void setEvent(const edm::Event &evt)
Load the vertices from the event.
assert(be >=bs)
reco::VertexRef associatedVertex(const Jet &jet) const
std::vector< ElementInBlock > ElementsInBlocks
Definition: PFCandidate.h:404
const ElementsInBlocks & elementsInBlocks() const
Definition: PFCandidate.cc:665
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isMatched(TrackingRecHit const &hit)
Log< level::Warning, true > LogPrint
Coll filterCandRefs(const Coll &refcoll, bool invert=false) const
Filter a ref vector of Candidates.
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
fixed size matrix
HLT enums.
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > bFieldToken_
const std::string & name() const
#define DEFINE_EDM_PLUGIN(factory, type, name)
void setChargedHadronP4(reco::PFRecoTauChargedHadron &chargedHadron, double scaleFactor_neutralPFCands=1.0)
void setPV(const reco::VertexRef &vtx)
Update the primary vertex.
void beginEvent() override
Hook called at the beginning of the event.
def move(src, dest)
Definition: eostools.py:511