CMS 3D CMS Logo

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