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