CMS 3D CMS Logo

PFRecoTauChargedHadronFromGenericTrackPlugin.cc
Go to the documentation of this file.
1 /*
2  * PFRecoTauChargedHadronFromGenericTrackPlugin
3  *
4  * Build PFRecoTauChargedHadron objects
5  * using tracks as input, from either collection of RECO/AOD reco::Tracks
6  * (PFRecoTauChargedHadronFromTrackPlugin) or from a collection of MINIAOD
7  * pat::PackedCandidates (PFRecoTauChargedHadronFromLostTrackPlugin), typically
8  * using the 'lostTracks' collection
9  *
10  * Author: Christian Veelken, LLR
11  *
12  * inclusion of lost tracks based on original implementation
13  * by Michal Bluj, NCBJ, Poland
14  */
15 
17 
19 
23 
36 
39 
44 
45 #include <TMath.h>
46 
47 #include <memory>
48 #include <cmath>
49 #include <algorithm>
50 
51 namespace reco { namespace tau {
52 
53 template<class TrackClass>
54 class PFRecoTauChargedHadronFromGenericTrackPlugin : public PFRecoTauChargedHadronBuilderPlugin
55 {
56  public:
59  // Return type is auto_ptr<ChargedHadronVector>
60  return_type operator()(const reco::Jet&) const override;
61  // Hook to update PV information
62  void beginEvent() override;
63 
64  private:
65  bool filterTrack(const edm::Handle<std::vector<TrackClass> >&, size_t iTrack) const;
66  void setChargedHadronTrack(PFRecoTauChargedHadron& chargedHadron, const edm::Ptr<TrackClass>& track) const;
67  double getTrackPtError(const TrackClass& track) const;
69 
71 
73 
76  double dRcone_;
78 
81 
83 
84  mutable int numWarnings_;
86 
88 };
89 
90 template<class TrackClass>
93  vertexAssociator_(pset.getParameter<edm::ParameterSet>("qualityCuts"),std::move(iC)),
95 {
96  edm::ParameterSet qcuts_pset = pset.getParameterSet("qualityCuts").getParameterSet("signalQualityCuts");
97  qcuts_ = new RecoTauQualityCuts(qcuts_pset);
98 
99  srcTracks_ = pset.getParameter<edm::InputTag>("srcTracks");
100  Tracks_token = iC.consumes<std::vector<TrackClass> >(srcTracks_);
101  dRcone_ = pset.getParameter<double>("dRcone");
102  dRconeLimitedToJetArea_ = pset.getParameter<bool>("dRconeLimitedToJetArea");
103 
104  dRmergeNeutralHadron_ = pset.getParameter<double>("dRmergeNeutralHadron");
105  dRmergePhoton_ = pset.getParameter<double>("dRmergePhoton");
106 
107  numWarnings_ = 0;
108  maxWarnings_ = 3;
109 
110  verbosity_ = pset.getParameter<int>("verbosity");
111 }
112 
113 template<class TrackClass>
115 {
116  delete qcuts_;
117 }
118 
119 // Update the primary vertex
120 template<class TrackClass>
122 {
123  vertexAssociator_.setEvent(*this->evt());
124 
126  const edm::EventSetup* evtSetup(this->evtSetup());
127  evtSetup->get<IdealMagneticFieldRecord>().get(magneticField);
128  magneticFieldStrength_ = magneticField->inTesla(GlobalPoint(0.,0.,0.));
129 }
130 
131 template<>
132 bool PFRecoTauChargedHadronFromGenericTrackPlugin<reco::Track>::filterTrack(const edm::Handle<std::vector<reco::Track> >& tracks, size_t iTrack) const {
133 // ignore tracks which fail quality cuts
134  reco::TrackRef trackRef(tracks, iTrack);
135  return qcuts_->filterTrack(trackRef);
136 }
137 
138 template<>
139 bool PFRecoTauChargedHadronFromGenericTrackPlugin<pat::PackedCandidate>::filterTrack(const edm::Handle<std::vector<pat::PackedCandidate> >& tracks, size_t iTrack) const {
140  // ignore tracks which fail quality cuts
141  const pat::PackedCandidate& cand = (*tracks)[iTrack];
142  if (cand.charge() == 0)
143  return false;
144 
145  return qcuts_->filterChargedCand(cand);
146 }
147 
148 template<>
150  chargedHadron.track_ = track;
151 }
152 
153 template<>
155  chargedHadron.lostTrackCandidate_ = track;
156 }
157 
158 template<>
160  return track.ptError();
161 }
162 
163 template<>
165  double trackPtError = 0.06; // MB: Approximate avarage track PtError by 2.5% (barrel), 4% (transition), 6% (endcaps) lostTracks w/o detailed track information available (after TRK-11-001)
166  const reco::Track* track(cand.bestTrack());
167  if(track != nullptr) {
168  trackPtError = track->ptError();
169  } else {
170  if( std::abs(cand.eta()) < 0.9 )
171  trackPtError = 0.025;
172  else if( std::abs(cand.eta()) < 1.4 )
173  trackPtError = 0.04;
174  }
175  return trackPtError;
176 }
177 
178 template<>
180  return XYZTLorentzVector(track.referencePoint().x(), track.referencePoint().y(), track.referencePoint().z(), 0.);
181 }
182 
183 template<>
185  return XYZTLorentzVector(track.vertex().x(), track.vertex().y(), track.vertex().z(), 0.);
186 }
187 
188 
189 namespace
190 {
191  struct Candidate_withDistance
192  {
194  double distance_;
195  };
196 
197  bool isSmallerDistance(const Candidate_withDistance& cand1, const Candidate_withDistance& cand2)
198  {
199  return (cand1.distance_ < cand2.distance_);
200  }
201 }
202 
203 template<class TrackClass>
205 {
206  if ( verbosity_ ) {
207  edm::LogPrint("TauChHFromTrack") << "<PFRecoTauChargedHadronFromGenericTrackPlugin::operator()>:" ;
208  edm::LogPrint("TauChHFromTrack") << " pluginName = " << name() ;
209  }
210 
212 
213  const edm::Event& evt = (*this->evt());
214 
216  evt.getByToken(Tracks_token, tracks);
217 
219  float jEta=jet.eta();
220  float jPhi=jet.phi();
221  size_t numTracks = tracks->size();
222  for ( size_t iTrack = 0; iTrack < numTracks; ++iTrack ) {
223  const TrackClass& track = (*tracks)[iTrack];
224 
225  // consider tracks in vicinity of tau-jet candidate only
226  double dR = deltaR(track.eta(), track.phi(), jEta,jPhi);
227  double dRmatch = dRcone_;
228  if ( dRconeLimitedToJetArea_ ) {
229  double jetArea = jet.jetArea();
230  if ( jetArea > 0. ) {
231  dRmatch = std::min(dRmatch, sqrt(jetArea/M_PI));
232  } else {
233  if ( numWarnings_ < maxWarnings_ ) {
234  edm::LogInfo("PFRecoTauChargedHadronFromGenericTrackPlugin::operator()")
235  << "Jet: Pt = " << jet.pt() << ", eta = " << jet.eta() << ", phi = " << jet.phi() << " has area = " << jetArea << " !!" << std::endl;
236  ++numWarnings_;
237  }
238  dRmatch = 0.1;
239  }
240  }
241  if ( dR > dRmatch ) continue;
242 
243  if (!this->filterTrack(tracks, iTrack)) continue;
244 
245  reco::Candidate::Charge trackCharge_int = 0;
246  if ( track.charge() > 0. ) trackCharge_int = +1;
247  else if ( track.charge() < 0. ) trackCharge_int = -1;
248 
249  const double chargedPionMass = 0.13957; // GeV
250  double chargedPionP = track.p();
251  double chargedPionEn = TMath::Sqrt(chargedPionP*chargedPionP + chargedPionMass*chargedPionMass);
252  reco::Candidate::LorentzVector chargedPionP4(track.px(), track.py(), track.pz(), chargedPionEn);
253 
254  reco::Vertex::Point vtx(0.,0.,0.);
256 
257  std::auto_ptr<PFRecoTauChargedHadron> chargedHadron(new PFRecoTauChargedHadron(trackCharge_int, chargedPionP4, vtx, 0, true, PFRecoTauChargedHadron::kTrack));
258 
259  setChargedHadronTrack(*chargedHadron, edm::Ptr<TrackClass>(tracks, iTrack));
260 
261  // CV: Take code for propagating track to ECAL entrance
262  // from RecoParticleFlow/PFTracking/src/PFTrackTransformer.cc
263  // to make sure propagation is done in the same way as for charged PFCandidates.
264  //
265  // The following replacements need to be made
266  // outerMomentum -> momentum
267  // outerPosition -> referencePoint
268  // in order to run on AOD input
269  // (outerMomentum and outerPosition require access to reco::TrackExtra objects, which are available in RECO only)
270  //
271  XYZTLorentzVector chargedPionPos(getTrackPos(track));
272  RawParticle p(chargedPionP4, chargedPionPos);
273  p.setCharge(track.charge());
274  BaseParticlePropagator trackPropagator(p, 0., 0., magneticFieldStrength_.z());
275  trackPropagator.propagateToEcalEntrance(false);
276  if ( trackPropagator.getSuccess() != 0 ) {
277  chargedHadron->positionAtECALEntrance_ = trackPropagator.particle().vertex();
278  } else {
279  if ( chargedPionP4.pt() > 2. and std::abs(chargedPionP4.eta()) < 3. ) {
280  edm::LogWarning("PFRecoTauChargedHadronFromGenericTrackPlugin::operator()")
281  << "Failed to propagate track: Pt = " << track.pt() << ", eta = " << track.eta() << ", phi = " << track.phi() << " to ECAL entrance !!" << std::endl;
282  }
283  chargedHadron->positionAtECALEntrance_ = math::XYZPointF(0.,0.,0.);
284  }
285 
286  std::vector<Candidate_withDistance> neutralJetConstituents_withDistance;
287  for ( const auto& jetConstituent : jet.daughterPtrVector() ) {
288  int pdgId = jetConstituent->pdgId();
289  if ( !(pdgId == 130 || pdgId == 22) ) continue;
290  double dR = deltaR(atECALEntrance(&*jetConstituent, magneticFieldStrength_.z()), chargedHadron->positionAtECALEntrance_);
291  double dRmerge = -1.;
292  if ( pdgId == 130 ) dRmerge = dRmergeNeutralHadron_;
293  else if ( pdgId == 22 ) dRmerge = dRmergePhoton_;
294  if ( dR < dRmerge ) {
295  Candidate_withDistance jetConstituent_withDistance;
296  jetConstituent_withDistance.pfCandidate_ = jetConstituent;
297  jetConstituent_withDistance.distance_ = dR;
298  neutralJetConstituents_withDistance.push_back(jetConstituent_withDistance);
299  chargedHadron->addDaughter(jetConstituent);
300  }
301  }
302  std::sort(neutralJetConstituents_withDistance.begin(), neutralJetConstituents_withDistance.end(), isSmallerDistance);
303 
304  const double caloResolutionCoeff = 1.0; // CV: approximate ECAL + HCAL calorimeter resolution for hadrons by 100%*sqrt(E)
305  double resolutionTrackP = track.p()*(getTrackPtError(track)/track.pt());
306  double neutralEnSum = 0.;
307  for ( std::vector<Candidate_withDistance>::const_iterator nextNeutral = neutralJetConstituents_withDistance.begin();
308  nextNeutral != neutralJetConstituents_withDistance.end(); ++nextNeutral ) {
309  double nextNeutralEn = nextNeutral->pfCandidate_->energy();
310  double resolutionCaloEn = caloResolutionCoeff*sqrt(neutralEnSum + nextNeutralEn);
311  double resolution = sqrt(resolutionTrackP*resolutionTrackP + resolutionCaloEn*resolutionCaloEn);
312  if ( (neutralEnSum + nextNeutralEn) < (track.p() + 2.*resolution) ) {
313  chargedHadron->neutralPFCandidates_.push_back(nextNeutral->pfCandidate_);
314  neutralEnSum += nextNeutralEn;
315  } else {
316  break;
317  }
318  }
319 
320  setChargedHadronP4(*chargedHadron);
321 
322  if ( verbosity_ ) {
323  edm::LogPrint("TauChHFromTrack") << *chargedHadron;
324  }
325 
326  output.push_back(chargedHadron);
327  }
328 
329  return output.release();
330 }
331 
334 
335 }} // end namespace reco::tau
336 
338 
void setCharge(float q)
set the MEASURED charge
Definition: RawParticle.cc:67
T getParameter(std::string const &) const
int Charge
electric charge type
Definition: Candidate.h:35
const Point & referencePoint() const
Reference point on the track.
Definition: TrackBase.h:720
math::XYZPointF atECALEntrance(const reco::Candidate *part, double bField)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
double eta() const final
momentum pseudorapidity
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
reco::CandidatePtr pfCandidate_
Base class for all types of Jets.
Definition: Jet.h:20
#define nullptr
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
PFRecoTauChargedHadronFromGenericTrackPlugin(const edm::ParameterSet &, edm::ConsumesCollector &&iC)
double pt() const final
transverse momentum
void setEvent(const edm::Event &evt)
Load the vertices from the event.
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float > > XYZPointF
point in space with cartesian internal representation
Definition: Point3D.h:10
const reco::Track * bestTrack() const override
return a pointer to the track if present. otherwise, return a null pointer
boost::ptr_vector< PFRecoTauChargedHadron > ChargedHadronVector
void beginEvent() override
Hook called at the beginning of the event.
virtual const daughters & daughterPtrVector() const
references to daughtes
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
int charge() const override
electric charge
void setPV(const reco::VertexRef &vtx) const
Update the primary vertex.
double getTrackPtError(const TrackClass &track) const
T sqrt(T t)
Definition: SSEVec.h:18
const Point & vertex() const override
vertex position
XYZTLorentzVector getTrackPos(const TrackClass &track) const
double ptError() const
error on Pt (set to 1000 TeV if charge==0 for safety)
Definition: TrackBase.h:814
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
math::XYZPoint Point
point in the space
Definition: Vertex.h:39
const edm::EventSetup * evtSetup() const
T min(T a, T b)
Definition: MathUtil.h:58
return_type operator()(const reco::Jet &) const override
Build a collection of chargedHadrons from objects in the input jet.
virtual GlobalVector inTesla(const GlobalPoint &gp) const =0
Field value ad specified global point, in Tesla.
bool propagateToEcalEntrance(bool first=true)
#define M_PI
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
double eta() const override
momentum pseudorapidity
bool filterChargedCand(const reco::Candidate &cand) const
or a single charged candidate
ParameterSet const & getParameterSet(std::string const &) const
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
PFRecoTauChargedHadronFromGenericTrackPlugin< reco::Track > PFRecoTauChargedHadronFromTrackPlugin
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
fixed size matrix
HLT enums.
virtual float jetArea() const
get jet area
Definition: Jet.h:105
reco::VertexRef associatedVertex(const Jet &jet) const
T get() const
Definition: EventSetup.h:71
#define DEFINE_EDM_PLUGIN(factory, type, name)
PFRecoTauChargedHadronFromGenericTrackPlugin< pat::PackedCandidate > PFRecoTauChargedHadronFromLostTrackPlugin
void setChargedHadronTrack(PFRecoTauChargedHadron &chargedHadron, const edm::Ptr< TrackClass > &track) const
const std::string & name() const
void setChargedHadronP4(reco::PFRecoTauChargedHadron &chargedHadron, double scaleFactor_neutralPFCands=1.0)
double phi() const final
momentum azimuthal angle
bool filterTrack(const reco::TrackBaseRef &track) const
Filter a single Track.
def move(src, dest)
Definition: eostools.py:511
bool filterTrack(const edm::Handle< std::vector< TrackClass > > &, size_t iTrack) const
math::XYZTLorentzVector XYZTLorentzVector
Definition: RawParticle.h:27