CMS 3D CMS Logo

JetConstituentTableProducer.cc
Go to the documentation of this file.
3 
6 
9 
15 
18 
20 
23 
28 using namespace btagbtvdeep;
29 
32 
33 template <typename T>
35 public:
37  ~JetConstituentTableProducer() override;
38 
39  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
40 
41 private:
42  void produce(edm::Event &, const edm::EventSetup &) override;
43 
45  //=====
47 
48  //const std::string name_;
53  const bool readBtag_;
54  const double jet_radius_;
55 
60 
66 
67  const reco::Vertex *pv_ = nullptr;
68 };
69 
70 //
71 // constructors and destructor
72 //
73 template <typename T>
75  : name_(iConfig.getParameter<std::string>("name")),
76  nameSV_(iConfig.getParameter<std::string>("nameSV")),
77  idx_name_(iConfig.getParameter<std::string>("idx_name")),
78  idx_nameSV_(iConfig.getParameter<std::string>("idx_nameSV")),
79  readBtag_(iConfig.getParameter<bool>("readBtag")),
80  jet_radius_(iConfig.getParameter<double>("jet_radius")),
81  jet_token_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("jets"))),
82  vtx_token_(consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertices"))),
83  cand_token_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("candidates"))),
84  sv_token_(consumes<SVCollection>(iConfig.getParameter<edm::InputTag>("secondary_vertices"))),
85  track_builder_token_(
86  esConsumes<TransientTrackBuilder, TransientTrackRecord>(edm::ESInputTag("", "TransientTrackBuilder"))) {
87  produces<nanoaod::FlatTable>(name_);
88  produces<nanoaod::FlatTable>(nameSV_);
89  produces<std::vector<reco::CandidatePtr>>();
90 }
91 
92 template <typename T>
94 
95 template <typename T>
97  // elements in all these collections must have the same order!
98  auto outCands = std::make_unique<std::vector<reco::CandidatePtr>>();
99  auto outSVs = std::make_unique<std::vector<const reco::VertexCompositePtrCandidate *>>();
100  std::vector<int> jetIdx_pf, jetIdx_sv, pfcandIdx, svIdx;
101  // PF Cands
102  std::vector<float> btagEtaRel, btagPtRatio, btagPParRatio, btagSip3dVal, btagSip3dSig, btagJetDistVal,
103  btagDecayLenVal, cand_pt, cand_dzFromPV, cand_dxyFromPV, cand_dzErrFromPV, cand_dxyErrFromPV;
104  // Secondary vertices
105  std::vector<float> sv_mass, sv_pt, sv_ntracks, sv_chi2, sv_normchi2, sv_dxy, sv_dxysig, sv_d3d, sv_d3dsig,
107  std::vector<float> sv_ptrel, sv_phirel, sv_deltaR, sv_enratio;
108 
109  auto jets = iEvent.getHandle(jet_token_);
110  iEvent.getByToken(vtx_token_, vtxs_);
111  iEvent.getByToken(cand_token_, cands_);
112  iEvent.getByToken(sv_token_, svs_);
113 
114  if (readBtag_) {
115  track_builder_ = iSetup.getHandle(track_builder_token_);
116  }
117 
118  for (unsigned i_jet = 0; i_jet < jets->size(); ++i_jet) {
119  const auto &jet = jets->at(i_jet);
120  math::XYZVector jet_dir = jet.momentum().Unit();
121  GlobalVector jet_ref_track_dir(jet.px(), jet.py(), jet.pz());
122  VertexDistance3D vdist;
123 
124  pv_ = &vtxs_->at(0);
125 
127  // Secondary Vertices
128  std::vector<const reco::VertexCompositePtrCandidate *> jetSVs;
129  std::vector<const reco::VertexCompositePtrCandidate *> allSVs;
130  for (const auto &sv : *svs_) {
131  // Factor in cuts in NanoAOD for indexing
132  Measurement1D dl = vdist.distance(
133  vtxs_->front(), VertexState(RecoVertex::convertPos(sv.position()), RecoVertex::convertError(sv.error())));
134  if (dl.significance() > 3) {
135  allSVs.push_back(&sv);
136  }
137  if (reco::deltaR2(sv, jet) < jet_radius_ * jet_radius_) {
138  jetSVs.push_back(&sv);
139  }
140  }
141  // sort by dxy significance
142  std::sort(jetSVs.begin(),
143  jetSVs.end(),
145  return sv_vertex_comparator(*sva, *svb, *pv_);
146  });
147 
148  for (const auto &sv : jetSVs) {
149  // auto svPtrs = svs_->ptrs();
150  auto svInNewList = std::find(allSVs.begin(), allSVs.end(), sv);
151  if (svInNewList == allSVs.end()) {
152  // continue;
153  svIdx.push_back(-1);
154  } else {
155  svIdx.push_back(svInNewList - allSVs.begin());
156  }
157  outSVs->push_back(sv);
158  jetIdx_sv.push_back(i_jet);
159  if (readBtag_ && !vtxs_->empty()) {
160  // Jet independent
161  sv_mass.push_back(sv->mass());
162  sv_pt.push_back(sv->pt());
163 
164  sv_ntracks.push_back(sv->numberOfDaughters());
165  sv_chi2.push_back(sv->vertexChi2());
166  sv_normchi2.push_back(catch_infs_and_bound(sv->vertexChi2() / sv->vertexNdof(), 1000, -1000, 1000));
167  const auto &dxy_meas = vertexDxy(*sv, *pv_);
168  sv_dxy.push_back(dxy_meas.value());
169  sv_dxysig.push_back(catch_infs_and_bound(dxy_meas.value() / dxy_meas.error(), 0, -1, 800));
170  const auto &d3d_meas = vertexD3d(*sv, *pv_);
171  sv_d3d.push_back(d3d_meas.value());
172  sv_d3dsig.push_back(catch_infs_and_bound(d3d_meas.value() / d3d_meas.error(), 0, -1, 800));
173  sv_costhetasvpv.push_back(vertexDdotP(*sv, *pv_));
174  // Jet related
175  sv_ptrel.push_back(sv->pt() / jet.pt());
176  sv_phirel.push_back(reco::deltaPhi(*sv, jet));
177  sv_deltaR.push_back(catch_infs_and_bound(std::fabs(reco::deltaR(*sv, jet_dir)) - 0.5, 0, -2, 0));
178  sv_enratio.push_back(sv->energy() / jet.energy());
179  }
180  }
181 
182  // PF Cands
183  std::vector<reco::CandidatePtr> const &daughters = jet.daughterPtrVector();
184 
185  for (const auto &cand : daughters) {
186  auto candPtrs = cands_->ptrs();
187  auto candInNewList = std::find(candPtrs.begin(), candPtrs.end(), cand);
188  if (candInNewList == candPtrs.end()) {
189  //std::cout << "Cannot find candidate : " << cand.id() << ", " << cand.key() << ", pt = " << cand->pt() << std::endl;
190  continue;
191  }
192  outCands->push_back(cand);
193  jetIdx_pf.push_back(i_jet);
194  pfcandIdx.push_back(candInNewList - candPtrs.begin());
195  cand_pt.push_back(cand->pt());
196  auto const *packedCand = dynamic_cast<pat::PackedCandidate const *>(cand.get());
197  if (packedCand && packedCand->hasTrackDetails()) {
198  const reco::Track *track_ptr = &(packedCand->pseudoTrack());
199  cand_dzFromPV.push_back(track_ptr->dz(pv_->position()));
200  cand_dxyFromPV.push_back(track_ptr->dxy(pv_->position()));
201  cand_dzErrFromPV.push_back(std::hypot(track_ptr->dzError(), pv_->zError()));
202  cand_dxyErrFromPV.push_back(track_ptr->dxyError(pv_->position(), pv_->covariance()));
203  } else {
204  cand_dzFromPV.push_back(-1);
205  cand_dxyFromPV.push_back(-1);
206  cand_dzErrFromPV.push_back(-1);
207  cand_dxyErrFromPV.push_back(-1);
208  }
209 
210  if (readBtag_ && !vtxs_->empty()) {
211  if (cand.isNull())
212  continue;
213  auto const *packedCand = dynamic_cast<pat::PackedCandidate const *>(cand.get());
214  if (packedCand == nullptr)
215  continue;
216  if (packedCand && packedCand->hasTrackDetails()) {
217  btagbtvdeep::TrackInfoBuilder trkinfo(track_builder_);
218  trkinfo.buildTrackInfo(&(*packedCand), jet_dir, jet_ref_track_dir, vtxs_->at(0));
219  btagEtaRel.push_back(trkinfo.getTrackEtaRel());
220  btagPtRatio.push_back(trkinfo.getTrackPtRatio());
221  btagPParRatio.push_back(trkinfo.getTrackPParRatio());
222  btagSip3dVal.push_back(trkinfo.getTrackSip3dVal());
223  btagSip3dSig.push_back(trkinfo.getTrackSip3dSig());
224  btagJetDistVal.push_back(trkinfo.getTrackJetDistVal());
225  // decay length
226  const reco::Track *track_ptr = packedCand->bestTrack();
227  reco::TransientTrack transient_track = track_builder_->build(track_ptr);
228  double decayLength = -1;
230  transient_track.impactPointState(), *pv_, jet_ref_track_dir, transient_track.field());
231  if (closest.isValid())
232  decayLength = (closest.globalPosition() - RecoVertex::convertPos(pv_->position())).mag();
233  else
234  decayLength = -1;
235  btagDecayLenVal.push_back(decayLength);
236  } else {
237  btagEtaRel.push_back(0);
238  btagPtRatio.push_back(0);
239  btagPParRatio.push_back(0);
240  btagSip3dVal.push_back(0);
241  btagSip3dSig.push_back(0);
242  btagJetDistVal.push_back(0);
243  btagDecayLenVal.push_back(0);
244  }
245  }
246  } // end jet loop
247  }
248 
249  auto candTable = std::make_unique<nanoaod::FlatTable>(outCands->size(), name_, false);
250  // We fill from here only stuff that cannot be created with the SimpleFlatTableProducer
251  candTable->addColumn<int>(idx_name_, pfcandIdx, "Index in the candidate list");
252  candTable->addColumn<int>("jetIdx", jetIdx_pf, "Index of the parent jet");
253  if (readBtag_) {
254  candTable->addColumn<float>("pt", cand_pt, "pt", 10); // to check matchind down the line
255  candTable->addColumn<float>("dzFromPV", cand_dzFromPV, "dzFromPV", 10);
256  candTable->addColumn<float>("dxyFromPV", cand_dxyFromPV, "dxyFromPV", 10);
257  candTable->addColumn<float>("dzErrFromPV", cand_dzErrFromPV, "dzErrFromPV", 10);
258  candTable->addColumn<float>("dxyErrFromPV", cand_dxyErrFromPV, "dxyErrFromPV", 10);
259  candTable->addColumn<float>("btagEtaRel", btagEtaRel, "btagEtaRel", 10);
260  candTable->addColumn<float>("btagPtRatio", btagPtRatio, "btagPtRatio", 10);
261  candTable->addColumn<float>("btagPParRatio", btagPParRatio, "btagPParRatio", 10);
262  candTable->addColumn<float>("btagSip3dVal", btagSip3dVal, "btagSip3dVal", 10);
263  candTable->addColumn<float>("btagSip3dSig", btagSip3dSig, "btagSip3dSig", 10);
264  candTable->addColumn<float>("btagJetDistVal", btagJetDistVal, "btagJetDistVal", 10);
265  candTable->addColumn<float>("btagDecayLenVal", btagDecayLenVal, "btagDecayLenVal", 10);
266  }
267  iEvent.put(std::move(candTable), name_);
268 
269  // SV table
270  auto svTable = std::make_unique<nanoaod::FlatTable>(outSVs->size(), nameSV_, false);
271  // We fill from here only stuff that cannot be created with the SimpleFlatTnameableProducer
272  svTable->addColumn<int>("jetIdx", jetIdx_sv, "Index of the parent jet");
273  svTable->addColumn<int>(idx_nameSV_, svIdx, "Index in the SV list");
274  if (readBtag_) {
275  svTable->addColumn<float>("mass", sv_mass, "SV mass", 10);
276  svTable->addColumn<float>("pt", sv_pt, "SV pt", 10);
277  svTable->addColumn<float>("ntracks", sv_ntracks, "Number of tracks associated to SV", 10);
278  svTable->addColumn<float>("chi2", sv_chi2, "chi2", 10);
279  svTable->addColumn<float>("normchi2", sv_normchi2, "chi2/ndof", 10);
280  svTable->addColumn<float>("dxy", sv_dxy, "", 10);
281  svTable->addColumn<float>("dxysig", sv_dxysig, "", 10);
282  svTable->addColumn<float>("d3d", sv_d3d, "", 10);
283  svTable->addColumn<float>("d3dsig", sv_d3dsig, "", 10);
284  svTable->addColumn<float>("costhetasvpv", sv_costhetasvpv, "", 10);
285  // Jet related
286  svTable->addColumn<float>("phirel", sv_phirel, "DeltaPhi(sv, jet)", 10);
287  svTable->addColumn<float>("ptrel", sv_ptrel, "pT relative to parent jet", 10);
288  svTable->addColumn<float>("deltaR", sv_deltaR, "dR from parent jet", 10);
289  svTable->addColumn<float>("enration", sv_enratio, "energy relative to parent jet", 10);
290  }
291  iEvent.put(std::move(svTable), nameSV_);
292 
293  iEvent.put(std::move(outCands));
294 }
295 
296 template <typename T>
299  desc.add<std::string>("name", "JetPFCands");
300  desc.add<std::string>("nameSV", "JetSV");
301  desc.add<std::string>("idx_name", "candIdx");
302  desc.add<std::string>("idx_nameSV", "svIdx");
303  desc.add<double>("jet_radius", true);
304  desc.add<bool>("readBtag", true);
305  desc.add<edm::InputTag>("jets", edm::InputTag("slimmedJetsAK8"));
306  desc.add<edm::InputTag>("vertices", edm::InputTag("offlineSlimmedPrimaryVertices"));
307  desc.add<edm::InputTag>("candidates", edm::InputTag("packedPFCandidates"));
308  desc.add<edm::InputTag>("secondary_vertices", edm::InputTag("slimmedSecondaryVertices"));
309  descriptions.addWithDefaultLabel(desc);
310 }
311 
314 
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
reco::Vertex::Point convertPos(const GlobalPoint &p)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
MPlex< T, D1, D2, N > hypot(const MPlex< T, D1, D2, N > &a, const MPlex< T, D1, D2, N > &b)
Definition: Matriplex.h:436
float vertexDdotP(const reco::VertexCompositePtrCandidate &sv, const reco::Vertex &pv)
Definition: deep_helpers.cc:78
Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:69
void buildTrackInfo(const reco::Candidate *candidate, const math::XYZVector &jetDir, GlobalVector refjetdirection, const reco::Vertex &pv)
const float getTrackJetDistVal() const
const float getTrackSip3dVal() const
edm::Handle< SVCollection > svs_
JetConstituentTableProducer< reco::GenJet > GenJetConstituentTableProducer
int closest(std::vector< int > const &vec, int value)
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
TrajectoryStateOnSurface closestApproachToJet(const TrajectoryStateOnSurface &state, const reco::Vertex &vertex, const GlobalVector &aJetDirection, const MagneticField *field)
Definition: IPTools.cc:182
edm::EDGetTokenT< reco::CandidateView > cand_token_
const float getTrackPParRatio() const
const float getTrackPtRatio() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::vector< VertexCompositePtrCandidate > VertexCompositePtrCandidateCollection
collection of Candidate objects
const float catch_infs_and_bound(const float in, const float replace_value, const float lowerbound, const float upperbound, const float offset=0., const bool use_offsets=true)
Definition: deep_helpers.cc:43
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:622
int iEvent
Definition: GenABIO.cc:224
double dxyError() const
error on dxy
Definition: TrackBase.h:769
double dzError() const
error on dz
Definition: TrackBase.h:778
Measurement1D vertexDxy(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv)
Definition: deep_helpers.cc:60
const MagneticField * field() const
edm::Handle< VertexCollection > vtxs_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
JetConstituentTableProducer< pat::Jet > PatJetConstituentTableProducer
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
const float getTrackSip3dSig() const
edm::ESHandle< TransientTrackBuilder > track_builder_
edm::EDGetTokenT< VertexCollection > vtx_token_
edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > track_builder_token_
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool sv_vertex_comparator(const SVType &sva, const SVType &svb, const PVType &pv)
Definition: deep_helpers.h:54
edm::EDGetTokenT< SVCollection > sv_token_
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
reco::VertexCompositePtrCandidateCollection SVCollection
double significance() const
Definition: Measurement1D.h:29
edm::EDGetTokenT< edm::View< T > > jet_token_
fixed size matrix
HLT enums.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
JetConstituentTableProducer(const edm::ParameterSet &)
long double T
edm::Handle< reco::CandidateView > cands_
void produce(edm::Event &, const edm::EventSetup &) override
const float getTrackEtaRel() const
def move(src, dest)
Definition: eostools.py:511
TrajectoryStateOnSurface impactPointState() const
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition: CandidateFwd.h:23
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:608