CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CandidateBoostedDoubleSecondaryVertexComputer.cc
Go to the documentation of this file.
2 
9 
10 
12  beta_(parameters.getParameter<double>("beta")),
13  R0_(parameters.getParameter<double>("R0")),
14  njettiness_(fastjet::contrib::OnePass_KT_Axes(), fastjet::contrib::NormalizedMeasure(beta_,R0_)),
15  maxSVDeltaRToJet_(parameters.getParameter<double>("maxSVDeltaRToJet")),
16  weightFile_(parameters.getParameter<edm::FileInPath>("weightFile"))
17 {
18  uses(0, "ipTagInfos");
19  uses(1, "svTagInfos");
20  uses(2, "muonTagInfos");
21  uses(3, "elecTagInfos");
22 
23  mvaID.reset(new TMVAEvaluator());
24 
25  // variable order needs to be the same as in the training
26  std::vector<std::string> variables({"PFLepton_ptrel", "z_ratio1", "tau_dot", "SV_mass_0", "SV_vtx_EnergyRatio_0",
27  "SV_vtx_EnergyRatio_1","PFLepton_IP2D", "tau2/tau1", "nSL", "jetNTracksEtaRel"});
28  std::vector<std::string> spectators({"massGroomed", "flavour", "nbHadrons", "ptGroomed", "etaGroomed"});
29 
30  mvaID->initialize("Color:Silent:Error", "BDTG", weightFile_.fullPath(), variables, spectators);
31 }
32 
33 
35 {
36  // get TagInfos
37  const reco::CandIPTagInfo & ipTagInfo = tagInfo.get<reco::CandIPTagInfo>(0);
39  const reco::CandSoftLeptonTagInfo & muonTagInfo = tagInfo.get<reco::CandSoftLeptonTagInfo>(2);
40  const reco::CandSoftLeptonTagInfo & elecTagInfo = tagInfo.get<reco::CandSoftLeptonTagInfo>(3);
41 
42  // default discriminator value
43  float value = -10.;
44 
45  // TMVAEvaluator is not thread safe
46  std::lock_guard<std::mutex> lock(m_mutex);
47 
48  // default variable values
49  float z_ratio = -1. , tau_dot = -1., SV_pt_0 = -1., SV_mass_0 = -1., SV_EnergyRatio_0 = -1., SV_EnergyRatio_1 = -1., tau21 = -1.;
50  int contSV = 0, vertexNTracks = 0;
51  int nSL = 0, nSM = 0, nSE = 0;
52 
53  // get the jet reference
54  const reco::JetBaseRef jet = svTagInfo.jet();
55 
56  std::vector<fastjet::PseudoJet> currentAxes;
57  float tau2, tau1;
58  // calculate N-subjettiness
59  calcNsubjettiness(jet, tau1, tau2, currentAxes);
60  if (tau1 != 0.) tau21 = tau2/tau1;
61 
62  const std::vector<reco::CandidatePtr> & selectedTracks( ipTagInfo.selectedTracks() );
63  size_t trackSize = selectedTracks.size();
64  const reco::VertexRef & vertexRef = ipTagInfo.primaryVertex();
65  reco::TrackKinematics allKinematics;
66 
67  for (size_t itt=0; itt < trackSize; ++itt)
68  {
69  const reco::Track & ptrack = *(reco::btag::toTrack(selectedTracks[itt]));
70  const reco::CandidatePtr ptrackRef = selectedTracks[itt];
71 
72  float track_PVweight = 0.;
73  setTracksPV(ptrackRef, vertexRef, track_PVweight);
74  if (track_PVweight>0.) { allKinematics.add(ptrack, track_PVweight); }
75  }
76 
77  math::XYZVector jetDir = jet->momentum().Unit();
78 
79  std::map<double, size_t> VTXmass;
80  for (size_t vtx = 0; vtx < svTagInfo.nVertices(); ++vtx)
81  {
82  vertexNTracks += (svTagInfo.secondaryVertex(vtx)).numberOfSourceCandidatePtrs();
83  GlobalVector flightDir = svTagInfo.flightDirection(vtx);
84  if (reco::deltaR2(flightDir, jetDir)<(maxSVDeltaRToJet_*maxSVDeltaRToJet_))
85  {
86  ++contSV;
87  VTXmass[svTagInfo.secondaryVertex(vtx).p4().mass()]=vtx;
88  }
89  }
90 
91  int cont=0;
92  GlobalVector flightDir_0, flightDir_1;
93  reco::Candidate::LorentzVector SV_p4_0 , SV_p4_1;
94  for ( std::map<double, size_t>::reverse_iterator iVtx=VTXmass.rbegin(); iVtx!=VTXmass.rend(); ++iVtx)
95  {
96  ++cont;
97  const reco::VertexCompositePtrCandidate &vertex = svTagInfo.secondaryVertex(iVtx->second);
98  reco::TrackKinematics vtxKinematics;
99  vertexKinematics(vertex, vtxKinematics);
100  math::XYZTLorentzVector allSum = allKinematics.weightedVectorSum();
101  math::XYZTLorentzVector vertexSum = vtxKinematics.weightedVectorSum();
102  if (cont==1)
103  {
104  SV_mass_0 = vertex.p4().mass() ;
105  SV_EnergyRatio_0 = vertexSum.E() / allSum.E();
106  SV_pt_0 = vertex.p4().pt();
107  flightDir_0 = svTagInfo.flightDirection(iVtx->second);
108  SV_p4_0 = vertex.p4();
109 
110  if (reco::deltaR2(flightDir_0,currentAxes[1])<reco::deltaR2(flightDir_0,currentAxes[0]))
111  tau_dot = (currentAxes[1].px()*flightDir_0.x()+currentAxes[1].py()*flightDir_0.y()+currentAxes[1].pz()*flightDir_0.z())/(sqrt(currentAxes[1].modp2())*flightDir_0.mag());
112  else
113  tau_dot = (currentAxes[0].px()*flightDir_0.x()+currentAxes[0].py()*flightDir_0.y()+currentAxes[0].pz()*flightDir_0.z())/(sqrt(currentAxes[0].modp2())*flightDir_0.mag());
114  }
115  if (cont==2)
116  {
117  SV_EnergyRatio_1= vertexSum.E() / allSum.E();
118  flightDir_1 = svTagInfo.flightDirection(iVtx->second);
119  SV_p4_1 = vertex.p4();
120  z_ratio = reco::deltaR(flightDir_0,flightDir_1)*SV_pt_0/(SV_p4_0+SV_p4_1).mass();
121  break;
122  }
123  }
124 
125  nSM = muonTagInfo.leptons();
126  nSE = elecTagInfo.leptons();
127  nSL = nSM + nSE;
128 
129  float PFLepton_ptrel = -1., PFLepton_IP2D = -1.;
130 
131  // PFMuon information
132  for (size_t leptIdx = 0; leptIdx < muonTagInfo.leptons() ; ++leptIdx)
133  {
134  float PFMuon_ptrel = (muonTagInfo.properties(leptIdx).ptRel);
135  if (PFMuon_ptrel > PFLepton_ptrel )
136  {
137  PFLepton_ptrel = PFMuon_ptrel;
138  PFLepton_IP2D = (muonTagInfo.properties(leptIdx).sip2d);
139  }
140  }
141 
142  // PFElectron information
143  for (size_t leptIdx = 0; leptIdx < elecTagInfo.leptons() ; ++leptIdx)
144  {
145  float PFElectron_ptrel = (elecTagInfo.properties(leptIdx).ptRel);
146  if (PFElectron_ptrel > PFLepton_ptrel )
147  {
148  PFLepton_ptrel = PFElectron_ptrel;
149  PFLepton_IP2D = (elecTagInfo.properties(leptIdx).sip2d);
150  }
151  }
152 
153  std::map<std::string,float> inputs;
154  inputs["z_ratio1"] = z_ratio;
155  inputs["tau_dot"] = tau_dot;
156  inputs["SV_mass_0"] = SV_mass_0;
157  inputs["SV_vtx_EnergyRatio_0"] = SV_EnergyRatio_0;
158  inputs["SV_vtx_EnergyRatio_1"] = SV_EnergyRatio_1;
159  inputs["jetNTracksEtaRel"] = vertexNTracks;
160  inputs["PFLepton_ptrel"] = PFLepton_ptrel;
161  inputs["PFLepton_IP2D"] = PFLepton_IP2D;
162  inputs["nSL"] = nSL;
163  inputs["tau2/tau1"] = tau21;
164 
165  // evaluate the MVA
166  value = mvaID->evaluate(inputs);
167 
168  // return the final discriminator value
169  return value;
170 }
171 
172 
173 void CandidateBoostedDoubleSecondaryVertexComputer::calcNsubjettiness(const reco::JetBaseRef & jet, float & tau1, float & tau2, std::vector<fastjet::PseudoJet> & currentAxes) const
174 {
175  std::vector<fastjet::PseudoJet> fjParticles;
176 
177  // loop over jet constituents and push them in the vector of FastJet constituents
178  for(const reco::CandidatePtr & daughter : jet->daughterPtrVector())
179  {
180  if ( daughter.isNonnull() && daughter.isAvailable() )
181  fjParticles.push_back( fastjet::PseudoJet( daughter->px(), daughter->py(), daughter->pz(), daughter->energy() ) );
182  else
183  edm::LogWarning("MissingJetConstituent") << "Jet constituent required for N-subjettiness computation is missing!";
184  }
185 
186  // calculate N-subjettiness
187  tau1 = njettiness_.getTau(1, fjParticles);
188  tau2 = njettiness_.getTau(2, fjParticles);
189  currentAxes = njettiness_.currentAxes();
190 }
191 
192 
193 void CandidateBoostedDoubleSecondaryVertexComputer::setTracksPVBase(const reco::TrackRef & trackRef, const reco::VertexRef & vertexRef, float & PVweight) const
194 {
195  PVweight = 0.;
196 
197  const reco::TrackBaseRef trackBaseRef( trackRef );
198 
200 
201  const reco::Vertex & vtx = *(vertexRef);
202  // loop over tracks in vertices
203  for(IT it=vtx.tracks_begin(); it!=vtx.tracks_end(); ++it)
204  {
205  const reco::TrackBaseRef & baseRef = *it;
206  // one of the tracks in the vertex is the same as the track considered in the function
207  if( baseRef == trackBaseRef )
208  {
209  PVweight = vtx.trackWeight(baseRef);
210  break;
211  }
212  }
213 }
214 
215 
216 void CandidateBoostedDoubleSecondaryVertexComputer::setTracksPV(const reco::CandidatePtr & trackRef, const reco::VertexRef & vertexRef, float & PVweight) const
217 {
218  PVweight = 0.;
219 
220  const pat::PackedCandidate * pcand = dynamic_cast<const pat::PackedCandidate *>(trackRef.get());
221 
222  if(pcand) // MiniAOD case
223  {
224  if( pcand->fromPV() == pat::PackedCandidate::PVUsedInFit )
225  {
226  PVweight = 1.;
227  }
228  }
229  else
230  {
231  const reco::PFCandidate * pfcand = dynamic_cast<const reco::PFCandidate *>(trackRef.get());
232 
233  setTracksPVBase(pfcand->trackRef(), vertexRef, PVweight);
234  }
235 }
236 
237 
239 {
240  const std::vector<reco::CandidatePtr> & tracks = vertex.daughterPtrVector();
241 
242  for(std::vector<reco::CandidatePtr>::const_iterator track = tracks.begin(); track != tracks.end(); ++track) {
243  const reco::Track& mytrack = *(*track)->bestTrack();
244  vtxKinematics.add(mytrack, 1.0);
245  }
246 }
dictionary parameters
Definition: Parameters.py:2
const math::XYZTLorentzVector & weightedVectorSum() const
const T & get(unsigned int index=0) const
tuple cont
load Luminosity info ##
Definition: generateEDF.py:622
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:44
void add(const reco::Track &track, double weight=1.0)
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:143
void setTracksPV(const reco::CandidatePtr &trackRef, const reco::VertexRef &vertexRef, float &PVweight) const
virtual Vector momentum() const
spatial momentum vector
T y() const
Definition: PV3DBase.h:63
const Container & selectedTracks() const
Definition: IPTagInfo.h:101
double deltaR(const T1 &t1, const T2 &t2)
Definition: deltaR.h:48
const reco::Track * toTrack(const reco::TrackBaseRef &t)
Definition: IPTagInfo.h:24
const edm::Ref< VertexCollection > & primaryVertex() const
Definition: IPTagInfo.h:133
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:433
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
T mag() const
Definition: PV3DBase.h:67
float trackWeight(const TrackBaseRef &r) const
returns the weight with which a Track has contributed to the vertex-fit.
const PVAssoc fromPV(size_t ipv=0) const
T sqrt(T t)
Definition: SSEVec.h:48
T z() const
Definition: PV3DBase.h:64
void uses(unsigned int id, const std::string &label)
double deltaR2(const T1 &t1, const T2 &t2)
Definition: deltaR.h:36
void setTracksPVBase(const reco::TrackRef &trackRef, const reco::VertexRef &vertexRef, float &PVweight) const
std::vector< LinkConnSpec >::const_iterator IT
tuple tracks
Definition: testEve_cfg.py:39
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
void vertexKinematics(const reco::VertexCompositePtrCandidate &vertex, reco::TrackKinematics &vertexKinematics) const
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
void calcNsubjettiness(const reco::JetBaseRef &jet, float &tau1, float &tau2, std::vector< fastjet::PseudoJet > &currentAxes) const
const daughters & daughterPtrVector() const
references to daughtes
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector&lt;TrackRef&gt;
Definition: Vertex.h:37
float discriminator(const TagInfoHelper &tagInfos) const override
std::string fullPath() const
Definition: FileInPath.cc:165
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:39
T x() const
Definition: PV3DBase.h:62
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
Definition: LeafCandidate.h:99