CMS 3D CMS Logo

PileupJetIdAlgo.cc
Go to the documentation of this file.
2 
10 
11 #include "TMatrixDSym.h"
12 #include "TMatrixDSymEigen.h"
13 
14 #include <utility>
15 
16 // ------------------------------------------------------------------------------------------
18 
19 // ------------------------------------------------------------------------------------------
20 
22  cutBased_(ps.getParameter<bool>("cutBased")),
23  etaBinnedWeights_(false),
24  runMvas_(runMvas),
25  nEtaBins_(0),
26  label_(ps.getParameter<std::string>("label")),
27  mvacut_{},
28  rmsCut_{},
29  betaStarCut_{}
30  {
31 
32  std::vector<edm::FileInPath> tmvaEtaWeights;
33  std::vector<std::string> tmvaSpectators;
34  int version;
35 
36  if (!cutBased_) {
37  etaBinnedWeights_ = ps.getParameter<bool>("etaBinnedWeights");
38  if (etaBinnedWeights_) {
39  const std::vector<edm::ParameterSet>& trainings = ps.getParameter<std::vector <edm::ParameterSet> >("trainings");
40  nEtaBins_ = ps.getParameter<int>("nEtaBins");
41  for (int v = 0; v < nEtaBins_; v++) {
42  tmvaEtaWeights.push_back(trainings.at(v).getParameter<edm::FileInPath>("tmvaWeights"));
43  jEtaMin_.push_back( trainings.at(v).getParameter<double>("jEtaMin") );
44  jEtaMax_.push_back( trainings.at(v).getParameter<double>("jEtaMax") );
45  }
46  for (int v = 0; v < nEtaBins_; v++) {
47  tmvaEtaVariables_.push_back( trainings.at(v).getParameter<std::vector<std::string> >("tmvaVariables") );
48  }
49  } else {
50  tmvaVariables_ = ps.getParameter<std::vector<std::string> >("tmvaVariables");
51  }
52  tmvaMethod_ = ps.getParameter<std::string>("tmvaMethod");
53  tmvaSpectators = ps.getParameter<std::vector<std::string> >("tmvaSpectators");
54  version = ps.getParameter<int>("version");
55  } else {
56  version = USER;
57  }
58 
59  edm::ParameterSet jetConfig = ps.getParameter<edm::ParameterSet>("JetIdParams");
60  for (int i0 = 0; i0 < 3; i0++) {
61  std::string lCutType = "Tight";
62  if (i0 == PileupJetIdentifier::kMedium) lCutType = "Medium";
63  if (i0 == PileupJetIdentifier::kLoose) lCutType = "Loose";
64  int nCut = 1;
65  if(cutBased_) nCut++;
66  for (int i1 = 0; i1 < nCut; i1++) {
67  std::string lFullCutType = lCutType;
68  if (cutBased_ && i1 == 0) lFullCutType = "BetaStar"+ lCutType;
69  if (cutBased_ && i1 == 1) lFullCutType = "RMS" + lCutType;
70  std::vector<double> pt010 = jetConfig.getParameter<std::vector<double> >(("Pt010_" +lFullCutType).c_str());
71  std::vector<double> pt1020 = jetConfig.getParameter<std::vector<double> >(("Pt1020_"+lFullCutType).c_str());
72  std::vector<double> pt2030 = jetConfig.getParameter<std::vector<double> >(("Pt2030_"+lFullCutType).c_str());
73  std::vector<double> pt3050 = jetConfig.getParameter<std::vector<double> >(("Pt3050_"+lFullCutType).c_str());
74  if (!cutBased_) {
75  for (int i2 = 0; i2 < 4; i2++) mvacut_[i0][0][i2] = pt010 [i2];
76  for (int i2 = 0; i2 < 4; i2++) mvacut_[i0][1][i2] = pt1020[i2];
77  for (int i2 = 0; i2 < 4; i2++) mvacut_[i0][2][i2] = pt2030[i2];
78  for (int i2 = 0; i2 < 4; i2++) mvacut_[i0][3][i2] = pt3050[i2];
79  }
80  if (cutBased_ && i1 == 0) {
81  for (int i2 = 0; i2 < 4; i2++) betaStarCut_[i0][0][i2] = pt010 [i2];
82  for (int i2 = 0; i2 < 4; i2++) betaStarCut_[i0][1][i2] = pt1020[i2];
83  for (int i2 = 0; i2 < 4; i2++) betaStarCut_[i0][2][i2] = pt2030[i2];
84  for (int i2 = 0; i2 < 4; i2++) betaStarCut_[i0][3][i2] = pt3050[i2];
85  }
86  if (cutBased_ && i1 == 1) {
87  for (int i2 = 0; i2 < 4; i2++) rmsCut_[i0][0][i2] = pt010 [i2];
88  for (int i2 = 0; i2 < 4; i2++) rmsCut_[i0][1][i2] = pt1020[i2];
89  for (int i2 = 0; i2 < 4; i2++) rmsCut_[i0][2][i2] = pt2030[i2];
90  for (int i2 = 0; i2 < 4; i2++) rmsCut_[i0][3][i2] = pt3050[i2];
91  }
92  }
93  }
94 
95  if ( ! cutBased_ ) {
96  assert( tmvaMethod_.empty() || ((! tmvaVariables_.empty() || ( !tmvaEtaVariables_.empty() )) && version == USER) );
97  }
98 
99  if (( ! cutBased_ ) && (runMvas_)) {
100  if (etaBinnedWeights_) {
101  for (int v = 0; v < nEtaBins_; v++) {
102  etaReader_.push_back(createGBRForest(tmvaEtaWeights.at(v)));
103  }
104  } else {
105  reader_ = createGBRForest(ps.getParameter<std::string>("tmvaWeights"));
106  }
107  }
108 }
109 
111  cache_(cache) {
112 
113  initVariables();
114 }
115 
116 // ------------------------------------------------------------------------------------------
118 {
119 }
120 
121 // ------------------------------------------------------------------------------------------
122 void assign(const std::vector<float> & vec, float & a, float & b, float & c, float & d )
123 {
124  size_t sz = vec.size();
125  a = ( sz > 0 ? vec[0] : 0. );
126  b = ( sz > 1 ? vec[1] : 0. );
127  c = ( sz > 2 ? vec[2] : 0. );
128  d = ( sz > 3 ? vec[3] : 0. );
129 }
130 // ------------------------------------------------------------------------------------------
131 void setPtEtaPhi(const reco::Candidate & p, float & pt, float & eta, float &phi )
132 {
133  pt = p.pt();
134  eta = p.eta();
135  phi = p.phi();
136 }
137 
138 // ------------------------------------------------------------------------------------------
140 {
141  internalId_ = id;
142 }
143 
144 // ------------------------------------------------------------------------------------------
145 
146 float PileupJetIdAlgo::getMVAval(const std::vector<std::string> &varList, const std::unique_ptr<const GBRForest> &reader)
147 {
148  std::vector<float> vars;
149  for(std::vector<std::string>::const_iterator it=varList.begin(); it!=varList.end(); ++it) {
150  std::pair<float *,float> var = variables_.at(*it);
151  vars.push_back( *var.first );
152  }
153  return reader->GetClassifier(vars.data());
154 }
155 
157 {
158  if( cache_->cutBased() ) {
159  internalId_.idFlag_ = computeCutIDflag(internalId_.betaStarClassic_,internalId_.dR2Mean_,internalId_.nvtx_,internalId_.jetPt_,internalId_.jetEta_);
160  } else {
161  if(std::abs(internalId_.jetEta_) >= 5.0) {
162  internalId_.mva_ = -2.;
163  } else {
164  if(cache_->etaBinnedWeights()){
165  if(std::abs(internalId_.jetEta_) > cache_->jEtaMax().at(cache_->nEtaBins() - 1)) {
166  internalId_.mva_ = -2.;
167  } else {
168  for(int v = 0; v < cache_->nEtaBins(); v++){
169  if(std::abs(internalId_.jetEta_) >= cache_->jEtaMin().at(v) && std::abs(internalId_.jetEta_) < cache_->jEtaMax().at(v)) {
171  break;
172  }
173  }
174  }
175  } else {
177  }
178  }
180  }
181 }
182 
183 // ------------------------------------------------------------------------------------------
184 std::pair<int,int> PileupJetIdAlgo::getJetIdKey(float jetPt, float jetEta)
185 {
186  int ptId = 0;
187  if(jetPt >= 10 && jetPt < 20) ptId = 1;
188  if(jetPt >= 20 && jetPt < 30) ptId = 2;
189  if(jetPt >= 30 ) ptId = 3;
190 
191  int etaId = 0;
192  if(std::abs(jetEta) >= 2.5 && std::abs(jetEta) < 2.75) etaId = 1;
193  if(std::abs(jetEta) >= 2.75 && std::abs(jetEta) < 3.0 ) etaId = 2;
194  if(std::abs(jetEta) >= 3.0 && std::abs(jetEta) < 5.0 ) etaId = 3;
195 
196  return std::pair<int,int>(ptId,etaId);
197 }
198 // ------------------------------------------------------------------------------------------
199 int PileupJetIdAlgo::computeCutIDflag(float betaStarClassic,float dR2Mean,float nvtx, float jetPt, float jetEta)
200 {
201  std::pair<int,int> jetIdKey = getJetIdKey(jetPt,jetEta);
202  float betaStarModified = betaStarClassic/log(nvtx-0.64);
203  int idFlag(0);
204  if(betaStarModified < cache_->betaStarCut()[PileupJetIdentifier::kTight ][jetIdKey.first][jetIdKey.second] &&
205  dR2Mean < cache_->rmsCut() [PileupJetIdentifier::kTight ][jetIdKey.first][jetIdKey.second]
206  ) idFlag += 1 << PileupJetIdentifier::kTight;
207 
208  if(betaStarModified < cache_->betaStarCut()[PileupJetIdentifier::kMedium ][jetIdKey.first][jetIdKey.second] &&
209  dR2Mean < cache_->rmsCut() [PileupJetIdentifier::kMedium ][jetIdKey.first][jetIdKey.second]
210  ) idFlag += 1 << PileupJetIdentifier::kMedium;
211 
212  if(betaStarModified < cache_->betaStarCut()[PileupJetIdentifier::kLoose ][jetIdKey.first][jetIdKey.second] &&
213  dR2Mean < cache_->rmsCut() [PileupJetIdentifier::kLoose ][jetIdKey.first][jetIdKey.second]
214  ) idFlag += 1 << PileupJetIdentifier::kLoose;
215  return idFlag;
216 }
217 // ------------------------------------------------------------------------------------------
219 {
220  std::pair<int,int> jetIdKey = getJetIdKey(jetPt,jetEta);
221  return computeIDflag(mva,jetIdKey.first,jetIdKey.second);
222 }
223 
224 // ------------------------------------------------------------------------------------------
225 int PileupJetIdAlgo::computeIDflag(float mva,int ptId,int etaId)
226 {
227  int idFlag(0);
228  if(mva > cache_->mvacut()[PileupJetIdentifier::kTight ][ptId][etaId]) idFlag += 1 << PileupJetIdentifier::kTight;
229  if(mva > cache_->mvacut()[PileupJetIdentifier::kMedium][ptId][etaId]) idFlag += 1 << PileupJetIdentifier::kMedium;
230  if(mva > cache_->mvacut()[PileupJetIdentifier::kLoose ][ptId][etaId]) idFlag += 1 << PileupJetIdentifier::kLoose;
231  return idFlag;
232 }
233 
234 
235 // ------------------------------------------------------------------------------------------
237 {
238  runMva();
240 }
241 
242 // ------------------------------------------------------------------------------------------
244  const reco::VertexCollection & allvtx, double rho, bool usePuppi)
245 {
246 
247  // initialize all variables to 0
248  resetVariables();
249 
250  // loop over constituents, accumulate sums and find leading candidates
251  const pat::Jet * patjet = dynamic_cast<const pat::Jet *>(jet);
252  const reco::PFJet * pfjet = dynamic_cast<const reco::PFJet *>(jet);
253  assert( patjet != nullptr || pfjet != nullptr );
254  if( patjet != nullptr && jec == 0. ) { // if this is a pat jet and no jec has been passed take the jec from the object
255  jec = patjet->pt()/patjet->correctedJet(0).pt();
256  }
257  if( jec <= 0. ) {
258  jec = 1.;
259  }
260 
261  const reco::Candidate* lLead = nullptr, *lSecond = nullptr, *lLeadNeut = nullptr, *lLeadEm = nullptr, *lLeadCh = nullptr, *lTrail = nullptr;
262  std::vector<float> frac, fracCh, fracEm, fracNeut;
263  float cones[] = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7 };
264  size_t ncones = sizeof(cones)/sizeof(float);
265  float * coneFracs[] = { &internalId_.frac01_, &internalId_.frac02_, &internalId_.frac03_, &internalId_.frac04_,
266  &internalId_.frac05_, &internalId_.frac06_, &internalId_.frac07_ };
273  TMatrixDSym covMatrix(2); covMatrix = 0.;
274  float jetPt = jet->pt() / jec; // use uncorrected pt for shape variables
275  float sumPt = 0., sumPt2 = 0., sumTkPt = 0.,sumPtCh=0,sumPtNe = 0;
276  float multNeut = 0.0;
277  setPtEtaPhi(*jet,internalId_.jetPt_,internalId_.jetEta_,internalId_.jetPhi_); // use corrected pt for jet kinematics
278  internalId_.jetM_ = jet->mass();
279  internalId_.nvtx_ = allvtx.size();
280  internalId_.rho_ = rho;
281 
282  float dRmin(1000);
283 
284  for ( unsigned i = 0; i < jet->numberOfSourceCandidatePtrs(); ++i ) {
285  reco::CandidatePtr pfJetConstituent = jet->sourceCandidatePtr(i);
286 
287  const reco::Candidate* icand = pfJetConstituent.get();
288  const pat::PackedCandidate* lPack = dynamic_cast<const pat::PackedCandidate *>( icand );
289  const reco::PFCandidate *lPF=dynamic_cast<const reco::PFCandidate*>( icand );
290  bool isPacked = true;
291  if (lPack == nullptr){
292  isPacked = false;
293  }
294  float candPuppiWeight = 1.0;
295  if (usePuppi && isPacked) candPuppiWeight = lPack->puppiWeight();
296  float candPt = (icand->pt())*candPuppiWeight;
297  float candPtFrac = candPt/jetPt;
298  float candDr = reco::deltaR(*icand,*jet);
299  float candDeta = icand->eta() - jet->eta();
300  float candDphi = reco::deltaPhi(*icand,*jet);
301  float candPtDr = candPt * candDr;
302  size_t icone = std::lower_bound(&cones[0],&cones[ncones],candDr) - &cones[0];
303 
304  if(candDr < dRmin) dRmin = candDr;
305 
306  // // all particles
307  if( lLead == nullptr || candPt > lLead->pt() ) {
308  lSecond = lLead;
309  lLead = icand;
310  } else if( (lSecond == nullptr || candPt > lSecond->pt()) && (candPt < lLead->pt()) ) {
311  lSecond = icand;
312  }
313 
314  // // average shapes
315  internalId_.dRMean_ += candPtDr;
316  internalId_.dR2Mean_ += candPtDr*candPtDr;
317  covMatrix(0,0) += candPt*candPt*candDeta*candDeta;
318  covMatrix(0,1) += candPt*candPt*candDeta*candDphi;
319  covMatrix(1,1) += candPt*candPt*candDphi*candDphi;
320  internalId_.ptD_ += candPt*candPt;
321  sumPt += candPt;
322  sumPt2 += candPt*candPt;
323 
324  // single most energetic candiates and jet shape profiles
325  frac.push_back(candPtFrac);
326 
327  if( icone < ncones ) { *coneFracs[icone] += candPt; }
328 
329  // neutrals
330  if( abs(icand->pdgId()) == 130) {
331  if (lLeadNeut == nullptr || candPt > lLeadNeut->pt()) { lLeadNeut = icand; }
332  internalId_.dRMeanNeut_ += candPtDr;
333  fracNeut.push_back(candPtFrac);
334  if( icone < ncones ) { *coneNeutFracs[icone] += candPt; }
335  internalId_.ptDNe_ += candPt*candPt;
336  sumPtNe += candPt;
337  multNeut += candPuppiWeight;
338  }
339  // EM candidated
340  if( icand->pdgId() == 22 ) {
341  if(lLeadEm == nullptr || candPt > lLeadEm->pt()) { lLeadEm = icand; }
342  internalId_.dRMeanEm_ += candPtDr;
343  fracEm.push_back(candPtFrac);
344  if( icone < ncones ) { *coneEmFracs[icone] += candPt; }
345  internalId_.ptDNe_ += candPt*candPt;
346  sumPtNe += candPt;
347  multNeut += candPuppiWeight;
348  }
349  if((abs(icand->pdgId()) == 1) || (abs(icand->pdgId()) == 2)) multNeut += candPuppiWeight;
350 
351  // Charged particles
352  if( icand->charge() != 0 ) {
353  if (lLeadCh == nullptr || candPt > lLeadCh->pt()) {
354  lLeadCh = icand;
355 
356  const reco::Track* pfTrk = icand->bestTrack();
357  if (lPF && std::abs(icand->pdgId()) == 13 && pfTrk == nullptr){
358  reco::MuonRef lmuRef = lPF->muonRef();
359  if (lmuRef.isNonnull()){
360  const reco::Muon& lmu = *lmuRef.get();
361  pfTrk = lmu.bestTrack();
362  edm::LogWarning("BadMuon")<<"Found a PFCandidate muon without a trackRef: falling back to Muon::bestTrack ";
363  }
364  }
365  if(pfTrk==nullptr) { //protection against empty pointers for the miniAOD case
366  //To handle the electron case
367  if(isPacked) {
368  internalId_.d0_ = std::abs(lPack->dxy(vtx->position()));
369  internalId_.dZ_ = std::abs(lPack->dz(vtx->position()));
370  }
371  else if(lPF!=nullptr) {
372  pfTrk=(lPF->trackRef().get()==nullptr)?lPF->gsfTrackRef().get():lPF->trackRef().get();
373  internalId_.d0_ = std::abs(pfTrk->dxy(vtx->position()));
374  internalId_.dZ_ = std::abs(pfTrk->dz(vtx->position()));
375  }
376  }
377  else {
378  internalId_.d0_ = std::abs(pfTrk->dxy(vtx->position()));
379  internalId_.dZ_ = std::abs(pfTrk->dz(vtx->position()));
380  }
381  }
382  internalId_.dRMeanCh_ += candPtDr;
383  internalId_.ptDCh_ += candPt*candPt;
384  fracCh.push_back(candPtFrac);
385  if( icone < ncones ) { *coneChFracs[icone] += candPt; }
386  sumPtCh += candPt;
387  }
388  // // beta and betastar
389  if( icand->charge() != 0 ) {
390  if (!isPacked){
391  if(lPF->trackRef().isNonnull() ) {
392  float tkpt = candPt;
393  sumTkPt += tkpt;
394  // 'classic' beta definition based on track-vertex association
395  bool inVtx0 = vtx->trackWeight ( lPF->trackRef()) > 0 ;
396 
397  bool inAnyOther = false;
398  // alternative beta definition based on track-vertex distance of closest approach
399  double dZ0 = std::abs(lPF->trackRef()->dz(vtx->position()));
400  double dZ = dZ0;
401  for(reco::VertexCollection::const_iterator vi=allvtx.begin(); vi!=allvtx.end(); ++vi ) {
402  const reco::Vertex & iv = *vi;
403  if( iv.isFake() || iv.ndof() < 4 ) { continue; }
404  // the primary vertex may have been copied by the user: check identity by position
405  bool isVtx0 = (iv.position() - vtx->position()).r() < 0.02;
406  // 'classic' beta definition: check if the track is associated with
407  // any vertex other than the primary one
408  if( ! isVtx0 && ! inAnyOther ) {
409  inAnyOther = vtx->trackWeight ( lPF->trackRef()) <= 0 ;
410  }
411  // alternative beta: find closest vertex to the track
412  dZ = std::min(dZ,std::abs(lPF->trackRef()->dz(iv.position())));
413  }
414  // classic beta/betaStar
415  if( inVtx0 && ! inAnyOther ) {
416  internalId_.betaClassic_ += tkpt;
417  } else if( ! inVtx0 && inAnyOther ) {
418  internalId_.betaStarClassic_ += tkpt;
419  }
420  // alternative beta/betaStar
421  if( dZ0 < 0.2 ) {
422  internalId_.beta_ += tkpt;
423  } else if( dZ < 0.2 ) {
424  internalId_.betaStar_ += tkpt;
425  }
426  }
427  }
428  else{
429  float tkpt = candPt;
430  sumTkPt += tkpt;
431  bool inVtx0 = false;
432  bool inVtxOther = false;
433  double dZ0=9999.;
434  double dZ_tmp = 9999.;
435  for (unsigned vtx_i = 0 ; vtx_i < allvtx.size() ; vtx_i++ ) {
436  auto iv = allvtx[vtx_i];
437 
438  if (iv.isFake())
439  continue;
440 
441  // Match to vertex in case of copy as above
442  bool isVtx0 = (iv.position() - vtx->position()).r() < 0.02;
443 
444  if (isVtx0) {
445  if (lPack->fromPV(vtx_i) == pat::PackedCandidate::PVUsedInFit) inVtx0 = true;
446  if (lPack->fromPV(vtx_i) == 0) inVtxOther = true;
447  dZ0 = lPack->dz(iv.position());
448  }
449 
450  if (fabs(lPack->dz(iv.position())) < fabs(dZ_tmp)) {
451  dZ_tmp = lPack->dz(iv.position());
452  }
453  }
454  if (inVtx0){
455  internalId_.betaClassic_ += tkpt;
456  } else if (inVtxOther){
457  internalId_.betaStarClassic_ += tkpt;
458  }
459  if (fabs(dZ0) < 0.2){
460  internalId_.beta_ += tkpt;
461  } else if (fabs(dZ_tmp) < 0.2){
462  internalId_.betaStar_ += tkpt;
463  }
464  }
465  }
466 
467  // trailing candidate
468  if( lTrail == nullptr || candPt < lTrail->pt() ) {
469  lTrail = icand;
470  }
471 
472  }
473 
474  // // Finalize all variables
475  assert( !(lLead == nullptr) );
476 
477  if ( lSecond == nullptr ) { lSecond = lTrail; }
478  if ( lLeadNeut == nullptr ) { lLeadNeut = lTrail; }
479  if ( lLeadEm == nullptr ) { lLeadEm = lTrail; }
480  if ( lLeadCh == nullptr ) { lLeadCh = lTrail; }
481 
482  if( patjet != nullptr ) { // to enable running on MiniAOD slimmedJets
483  internalId_.nCharged_ = patjet->chargedMultiplicity();
484  internalId_.nNeutrals_ = patjet->neutralMultiplicity();
485  internalId_.chgEMfrac_ = patjet->chargedEmEnergy() /jet->energy();
486  internalId_.neuEMfrac_ = patjet->neutralEmEnergy() /jet->energy();
489  if (usePuppi) internalId_.nNeutrals_ = multNeut;
490  } else {
491  internalId_.nCharged_ = pfjet->chargedMultiplicity();
492  internalId_.nNeutrals_ = pfjet->neutralMultiplicity();
493  internalId_.chgEMfrac_ = pfjet->chargedEmEnergy() /jet->energy();
494  internalId_.neuEMfrac_ = pfjet->neutralEmEnergy() /jet->energy();
497  }
498  internalId_.nParticles_ = jet->nConstituents();
499 
501  float sumW2(0.0);
502  float sum_deta(0.0),sum_dphi(0.0);
503  float ave_deta(0.0), ave_dphi(0.0);
504  for (size_t j = 0; j < jet->numberOfDaughters(); j++) {
505  const auto& part = jet->daughterPtr(j);
506  if (!(part.isAvailable() && part.isNonnull()) ){
507  continue;
508  }
509 
510  float partPuppiWeight=1.0;
511  if (usePuppi){
512  const pat::PackedCandidate* partpack = dynamic_cast<const pat::PackedCandidate *>( part.get() );
513  if (partpack!=nullptr) partPuppiWeight = partpack->puppiWeight();
514  }
515 
516  float weight = (part->pt())*partPuppiWeight;
517  float weight2 = weight * weight;
518  sumW2 += weight2;
519  float deta = part->eta() - jet->eta();
520  float dphi = reco::deltaPhi(*part, *jet);
521  sum_deta += deta*weight2;
522  sum_dphi += dphi*weight2;
523  if (sumW2 > 0) {
524  ave_deta = sum_deta/sumW2;
525  ave_dphi = sum_dphi/sumW2;
526  }
527  }
528 
529  float ddetaR_sum(0.0), ddphiR_sum(0.0), pull_tmp(0.0);
530  for (size_t i = 0; i < jet->numberOfDaughters(); i++) {
531  const auto& part = jet->daughterPtr(i);
532  if (!(part.isAvailable() && part.isNonnull()) ){
533  continue;
534  }
535 
536  float partPuppiWeight=1.0;
537  if (usePuppi){
538  const pat::PackedCandidate* partpack = dynamic_cast<const pat::PackedCandidate *>( part.get() );
539  if (partpack!=nullptr) partPuppiWeight = partpack->puppiWeight();
540  }
541 
542  float weight = partPuppiWeight*(part->pt())*partPuppiWeight*(part->pt());
543  float deta = part->eta() - jet->eta();
544  float dphi = reco::deltaPhi(*part, *jet);
545  float ddeta, ddphi, ddR;
546  ddeta = deta - ave_deta ;
547  ddphi = dphi-ave_dphi;
548  ddR = sqrt(ddeta*ddeta + ddphi*ddphi);
549  ddetaR_sum += ddR*ddeta*weight;
550  ddphiR_sum += ddR*ddphi*weight;
551  }
552  if (sumW2 > 0) {
553  float ddetaR_ave = ddetaR_sum/sumW2;
554  float ddphiR_ave = ddphiR_sum/sumW2;
555  pull_tmp = sqrt(ddetaR_ave*ddetaR_ave+ddphiR_ave*ddphiR_ave);
556  }
557  internalId_.pull_ = pull_tmp;
559 
560 
566 
567  std::sort(frac.begin(),frac.end(),std::greater<float>());
568  std::sort(fracCh.begin(),fracCh.end(),std::greater<float>());
569  std::sort(fracEm.begin(),fracEm.end(),std::greater<float>());
570  std::sort(fracNeut.begin(),fracNeut.end(),std::greater<float>());
575 
576  covMatrix(0,0) /= sumPt2;
577  covMatrix(0,1) /= sumPt2;
578  covMatrix(1,1) /= sumPt2;
579  covMatrix(1,0) = covMatrix(0,1);
580  internalId_.etaW_ = sqrt(covMatrix(0,0));
581  internalId_.phiW_ = sqrt(covMatrix(1,1));
583  TVectorD eigVals(2); eigVals = TMatrixDSymEigen(covMatrix).GetEigenValues();
584  internalId_.majW_ = sqrt(std::abs(eigVals(0)));
585  internalId_.minW_ = sqrt(std::abs(eigVals(1)));
586  if( internalId_.majW_ < internalId_.minW_ ) { std::swap(internalId_.majW_,internalId_.minW_); }
587 
588  internalId_.dRLeadCent_ = reco::deltaR(*jet,*lLead);
589  if( lSecond == nullptr ) { internalId_.dRLead2nd_ = reco::deltaR(*jet,*lSecond); }
590  internalId_.dRMean_ /= jetPt;
594  internalId_.dR2Mean_ /= sumPt2;
595 
596  for(size_t ic=0; ic<ncones; ++ic){
597  *coneFracs[ic] /= jetPt;
598  *coneEmFracs[ic] /= jetPt;
599  *coneNeutFracs[ic] /= jetPt;
600  *coneChFracs[ic] /= jetPt;
601  }
602  //http://jets.physics.harvard.edu/qvg/
603  double ptMean = sumPt/internalId_.nParticles_;
604  double ptRMS = 0;
605  for(unsigned int i0 = 0; i0 < frac.size(); i0++) {ptRMS+=(frac[i0]-ptMean)*(frac[i0]-ptMean);}
606  ptRMS/=internalId_.nParticles_;
607  ptRMS=sqrt(ptRMS);
608 
609  internalId_.ptMean_ = ptMean;
610  internalId_.ptRMS_ = ptRMS/jetPt;
611  internalId_.pt2A_ = sqrt( internalId_.ptD_ /internalId_.nParticles_)/jetPt;
612  internalId_.ptD_ = sqrt( internalId_.ptD_) / sumPt;
613  internalId_.ptDCh_ = sqrt( internalId_.ptDCh_) / sumPtCh;
614  internalId_.ptDNe_ = sqrt( internalId_.ptDNe_) / sumPtNe;
616  internalId_.sumChPt_ = sumPtCh;
617  internalId_.sumNePt_ = sumPtNe;
618 
619  internalId_.jetR_ = lLead->pt()/sumPt;
620  internalId_.jetRchg_ = lLeadCh->pt()/sumPt;
621  internalId_.dRMatch_ = dRmin;
622 
623  if( sumTkPt != 0. ) {
624  internalId_.beta_ /= sumTkPt;
625  internalId_.betaStar_ /= sumTkPt;
626  internalId_.betaClassic_ /= sumTkPt;
627  internalId_.betaStarClassic_ /= sumTkPt;
628  } else {
629  assert( internalId_.beta_ == 0. && internalId_.betaStar_ == 0.&& internalId_.betaClassic_ == 0. && internalId_.betaStarClassic_ == 0. );
630  }
631 
632  if( cache_->runMvas() ) {
633  runMva();
634  }
635 
637 }
638 
639 
640 
641 // ------------------------------------------------------------------------------------------
643 {
644  std::stringstream out;
645  for(variables_list_t::const_iterator it=variables_.begin();
646  it!=variables_.end(); ++it ) {
647  out << std::setw(15) << it->first << std::setw(3) << "="
648  << std::setw(5) << *it->second.first
649  << " (" << std::setw(5) << it->second.second << ")" << std::endl;
650  }
651  return out.str();
652 }
653 
654 // ------------------------------------------------------------------------------------------
656 {
657  internalId_.idFlag_ = 0;
658  for(variables_list_t::iterator it=variables_.begin();
659  it!=variables_.end(); ++it ) {
660  *it->second.first = it->second.second;
661  }
662 }
663 
664 // ------------------------------------------------------------------------------------------
665 #define INIT_VARIABLE(NAME,TMVANAME,VAL) \
666  internalId_.NAME ## _ = VAL; \
667  variables_[ # NAME ] = std::make_pair(& internalId_.NAME ## _, VAL);
668 
669 // ------------------------------------------------------------------------------------------
671 {
672  internalId_.idFlag_ = 0;
673  INIT_VARIABLE(mva , "", -100.);
674  //INIT_VARIABLE(jetPt , "jspt_1", 0.);
675  //INIT_VARIABLE(jetEta , "jseta_1", large_val);
676  INIT_VARIABLE(jetPt , "", 0.);
678  INIT_VARIABLE(jetPhi , "jsphi_1", large_val);
679  INIT_VARIABLE(jetM , "jm_1", 0.);
680 
681  INIT_VARIABLE(nCharged , "", 0.);
682  INIT_VARIABLE(nNeutrals , "", 0.);
683 
684  INIT_VARIABLE(chgEMfrac , "", 0.);
685  INIT_VARIABLE(neuEMfrac , "", 0.);
686  INIT_VARIABLE(chgHadrfrac, "", 0.);
687  INIT_VARIABLE(neuHadrfrac, "", 0.);
688 
689  INIT_VARIABLE(d0 , "jd0_1" , -1000.);
690  INIT_VARIABLE(dZ , "jdZ_1" , -1000.);
691  //INIT_VARIABLE(nParticles , "npart_1" , 0.);
692  INIT_VARIABLE(nParticles , "" , 0.);
693 
694  INIT_VARIABLE(leadPt , "lpt_1" , 0.);
695  INIT_VARIABLE(leadEta , "leta_1" , large_val);
696  INIT_VARIABLE(leadPhi , "lphi_1" , large_val);
697  INIT_VARIABLE(secondPt , "spt_1" , 0.);
698  INIT_VARIABLE(secondEta , "seta_1" , large_val);
699  INIT_VARIABLE(secondPhi , "sphi_1" , large_val);
700  INIT_VARIABLE(leadNeutPt , "lnept_1" , 0.);
701  INIT_VARIABLE(leadNeutEta, "lneeta_1" , large_val);
702  INIT_VARIABLE(leadNeutPhi, "lnephi_1" , large_val);
703  INIT_VARIABLE(leadEmPt , "lempt_1" , 0.);
704  INIT_VARIABLE(leadEmEta , "lemeta_1" , large_val);
705  INIT_VARIABLE(leadEmPhi , "lemphi_1" , large_val);
706  INIT_VARIABLE(leadChPt , "lchpt_1" , 0.);
707  INIT_VARIABLE(leadChEta , "lcheta_1" , large_val);
708  INIT_VARIABLE(leadChPhi , "lchphi_1" , large_val);
709  INIT_VARIABLE(leadFrac , "lLfr_1" , 0.);
710 
711  INIT_VARIABLE(dRLeadCent , "drlc_1" , 0.);
712  INIT_VARIABLE(dRLead2nd , "drls_1" , 0.);
713  INIT_VARIABLE(dRMean , "drm_1" , 0.);
714  INIT_VARIABLE(dRMean , "" , 0.);
715  INIT_VARIABLE(pull , "" , 0.);
716  INIT_VARIABLE(dRMeanNeut , "drmne_1" , 0.);
717  INIT_VARIABLE(dRMeanEm , "drem_1" , 0.);
718  INIT_VARIABLE(dRMeanCh , "drch_1" , 0.);
719  INIT_VARIABLE(dR2Mean , "" , 0.);
720 
721  INIT_VARIABLE(ptD , "", 0.);
722  INIT_VARIABLE(ptMean , "", 0.);
723  INIT_VARIABLE(ptRMS , "", 0.);
724  INIT_VARIABLE(pt2A , "", 0.);
725  INIT_VARIABLE(ptDCh , "", 0.);
726  INIT_VARIABLE(ptDNe , "", 0.);
727  INIT_VARIABLE(sumPt , "", 0.);
728  INIT_VARIABLE(sumChPt , "", 0.);
729  INIT_VARIABLE(sumNePt , "", 0.);
730 
731  INIT_VARIABLE(secondFrac ,"" ,0.);
732  INIT_VARIABLE(thirdFrac ,"" ,0.);
733  INIT_VARIABLE(fourthFrac ,"" ,0.);
734 
735  INIT_VARIABLE(leadChFrac ,"" ,0.);
736  INIT_VARIABLE(secondChFrac ,"" ,0.);
737  INIT_VARIABLE(thirdChFrac ,"" ,0.);
738  INIT_VARIABLE(fourthChFrac ,"" ,0.);
739 
740  INIT_VARIABLE(leadNeutFrac ,"" ,0.);
741  INIT_VARIABLE(secondNeutFrac ,"" ,0.);
742  INIT_VARIABLE(thirdNeutFrac ,"" ,0.);
743  INIT_VARIABLE(fourthNeutFrac ,"" ,0.);
744 
745  INIT_VARIABLE(leadEmFrac ,"" ,0.);
746  INIT_VARIABLE(secondEmFrac ,"" ,0.);
747  INIT_VARIABLE(thirdEmFrac ,"" ,0.);
748  INIT_VARIABLE(fourthEmFrac ,"" ,0.);
749 
750  INIT_VARIABLE(jetW ,"" ,1.);
751  INIT_VARIABLE(etaW ,"" ,1.);
752  INIT_VARIABLE(phiW ,"" ,1.);
753 
754  INIT_VARIABLE(majW ,"" ,1.);
755  INIT_VARIABLE(minW ,"" ,1.);
756 
757  INIT_VARIABLE(frac01 ,"" ,0.);
758  INIT_VARIABLE(frac02 ,"" ,0.);
759  INIT_VARIABLE(frac03 ,"" ,0.);
760  INIT_VARIABLE(frac04 ,"" ,0.);
761  INIT_VARIABLE(frac05 ,"" ,0.);
762  INIT_VARIABLE(frac06 ,"" ,0.);
763  INIT_VARIABLE(frac07 ,"" ,0.);
764 
765  INIT_VARIABLE(chFrac01 ,"" ,0.);
766  INIT_VARIABLE(chFrac02 ,"" ,0.);
767  INIT_VARIABLE(chFrac03 ,"" ,0.);
768  INIT_VARIABLE(chFrac04 ,"" ,0.);
769  INIT_VARIABLE(chFrac05 ,"" ,0.);
770  INIT_VARIABLE(chFrac06 ,"" ,0.);
771  INIT_VARIABLE(chFrac07 ,"" ,0.);
772 
773  INIT_VARIABLE(neutFrac01 ,"" ,0.);
774  INIT_VARIABLE(neutFrac02 ,"" ,0.);
775  INIT_VARIABLE(neutFrac03 ,"" ,0.);
776  INIT_VARIABLE(neutFrac04 ,"" ,0.);
777  INIT_VARIABLE(neutFrac05 ,"" ,0.);
778  INIT_VARIABLE(neutFrac06 ,"" ,0.);
779  INIT_VARIABLE(neutFrac07 ,"" ,0.);
780 
781  INIT_VARIABLE(emFrac01 ,"" ,0.);
782  INIT_VARIABLE(emFrac02 ,"" ,0.);
783  INIT_VARIABLE(emFrac03 ,"" ,0.);
784  INIT_VARIABLE(emFrac04 ,"" ,0.);
785  INIT_VARIABLE(emFrac05 ,"" ,0.);
786  INIT_VARIABLE(emFrac06 ,"" ,0.);
787  INIT_VARIABLE(emFrac07 ,"" ,0.);
788 
789  INIT_VARIABLE(beta ,"" ,0.);
790  INIT_VARIABLE(betaStar ,"" ,0.);
791  INIT_VARIABLE(betaClassic ,"" ,0.);
792  INIT_VARIABLE(betaStarClassic ,"" ,0.);
793 
794  INIT_VARIABLE(nvtx ,"" ,0.);
795  INIT_VARIABLE(rho ,"" ,0.);
796  INIT_VARIABLE(nTrueInt ,"" ,0.);
797 
798  INIT_VARIABLE(jetR , "", 0.);
799  INIT_VARIABLE(jetRchg , "", 0.);
800  INIT_VARIABLE(dRMatch , "", 0.);
801 
802 }
803 
804 #undef INIT_VARIABLE
float puppiWeight() const
Set both weights at once (with option for only full PUPPI)
const float large_val
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
T getParameter(std::string const &) const
void set(const PileupJetIdentifier &)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
AlgoGBRForestsAndConstants const * cache_
double eta() const final
momentum pseudorapidity
#define INIT_VARIABLE(NAME, TMVANAME, VAL)
AlgoGBRForestsAndConstants(edm::ParameterSet const &, bool runMvas)
float chargedHadronEnergy() const
chargedHadronEnergy
Definition: Jet.h:655
float chargedEmEnergy() const
chargedEmEnergy
Definition: PFJet.h:142
std::vector< double > const & jEtaMin() const
float neutralEmEnergy() const
neutralEmEnergy
Definition: Jet.h:676
std::vector< std::unique_ptr< const GBRForest > > etaReader_
virtual const Track * bestTrack() const
Definition: Candidate.h:247
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:159
Base class for all types of Jets.
Definition: Jet.h:20
float neutralHadronEnergy() const
neutralHadronEnergy
Definition: Jet.h:662
std::pair< int, int > getJetIdKey(float jetPt, float jetEta)
size_type numberOfSourceCandidatePtrs() const override
double pt() const final
transverse momentum
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
Definition: weight.py:1
int neutralMultiplicity() const
neutralMultiplicity
Definition: Jet.h:422
int computeCutIDflag(float betaStarClassic, float dR2Mean, float nvtx, float jetPt, float jetEta)
std::vector< double > const & jEtaMax() const
int chargedMultiplicity() const
chargedMultiplicity
Definition: PFJet.h:155
const Track * bestTrack() const override
Definition: Muon.h:61
const Point & position() const
position
Definition: Vertex.h:109
Jets made from PFObjects.
Definition: PFJet.h:21
float neutralEmEnergy() const
neutralEmEnergy
Definition: PFJet.h:150
float chargedEmEnergy() const
chargedEmEnergy
Definition: Jet.h:669
size_t numberOfDaughters() const override
number of daughters
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
std::vector< std::string > tmvaVariables_
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool usePuppi)
PileupJetIdentifier internalId_
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
virtual int pdgId() const =0
PDG identifier.
const PVAssoc fromPV(size_t ipv=0) const
T sqrt(T t)
Definition: SSEVec.h:18
std::string dumpVariables() const
double energy() const final
energy
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void assign(const std::vector< float > &vec, float &a, float &b, float &c, float &d)
float trackWeight(const TREF &r) const
returns the weight with which a Track has contributed to the vertex-fit.
Definition: Vertex.h:81
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:243
T min(T a, T b)
Definition: MathUtil.h:58
std::unique_ptr< const GBRForest > const & reader() const
std::vector< std::vector< std::string > > tmvaEtaVariables_
int computeIDflag(float mva, float jetPt, float jetEta)
double ndof() const
Definition: Vertex.h:105
def cache(function)
int neutralMultiplicity() const
neutralMultiplicity
Definition: PFJet.h:157
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:459
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
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:642
void setPtEtaPhi(const reco::Candidate &p, float &pt, float &eta, float &phi)
virtual double eta() const =0
momentum pseudorapidity
bool isFake() const
Definition: Vertex.h:72
virtual double pt() const =0
transverse momentum
part
Definition: HCALResponse.h:20
virtual CandidatePtr daughterPtr(size_type i) const
reference to daughter at given position
PileupJetIdentifier computeMva()
double b
Definition: hdecay.h:120
Analysis-level calorimeter jet class.
Definition: Jet.h:80
std::vector< std::unique_ptr< const GBRForest > > const & etaReader() const
virtual int charge() const =0
electric charge
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
virtual int nConstituents() const
of constituents
Definition: Jet.h:65
double a
Definition: hdecay.h:121
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
CandidatePtr sourceCandidatePtr(size_type i) const override
std::vector< std::vector< std::string > > const & tmvaEtaVariables() const
variables_list_t variables_
float neutralHadronEnergy() const
neutralHadronEnergy
Definition: PFJet.h:102
virtual float dxy() const
dxy with respect to the PV ref
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:624
Jet correctedJet(const std::string &level, const std::string &flavor="none", const std::string &set="") const
std::vector< std::string > const & tmvaVariables() const
float getMVAval(const std::vector< std::string > &, const std::unique_ptr< const GBRForest > &)
virtual double phi() const =0
momentum azimuthal angle
int chargedMultiplicity() const
chargedMultiplicity
Definition: Jet.h:690
float chargedHadronEnergy() const
chargedHadronEnergy
Definition: PFJet.h:98
std::unique_ptr< const GBRForest > reader_
PileupJetIdAlgo(AlgoGBRForestsAndConstants const *cache)
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
double mass() const final
mass