babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
mc_tools.cc
Go to the documentation of this file.
1 // MC_TOOLS: Functions that deal with MC particles
2 
3 #include <iostream>
5 #include "TRegexp.h"
6 
7 using namespace std;
8 using namespace utilities;
9 
10 // Checks if "id" is one of the immediate daughters of "mc"
11 bool mc_tools::hasDaughter(const reco::GenParticle &mc, size_t id){
12  for(size_t idau(0); idau < mc.numberOfDaughters(); idau++)
13  if(abs(mc.daughter(idau)->pdgId())==id) return true;
14 
15  return false;
16 }
17 
18 
19 int mc_tools::numChargeDaughters(const reco::GenParticle &mc){
20  int ncharged=0;
21  for(size_t idau(0); idau < mc.numberOfDaughters(); idau++)
22  if(abs(mc.daughter(idau)->charge())!=0) ncharged++;
23 
24  return ncharged;
25 }
26 
27 
28 // Checks if "mc" is "id" and does not decay to itself
29 bool mc_tools::isLast(const reco::GenParticle &mc, size_t id){
30  return (abs(mc.pdgId())==id && !hasDaughter(mc, id));
31 }
32 // Checks if "mc" comes from a W or a tau from a W
33 bool mc_tools::fromWOrWTau(const reco::GenParticle &mc){
34  const reco::GenParticle *mcMom;
35  int momId = abs(mom(mc, mcMom));
36  if(momId == 24) return true;
37  if(momId == 15 && abs(mom(*mcMom, mcMom)) == 24) return true;
38  return false;
39 }
40 
41 
42 bool mc_tools::fromTau(const reco::GenParticle &mc){
43  const reco::GenParticle *mcMom;
44  int momId = abs(mom(mc, mcMom));
45  if(momId == 15 && abs(mom(*mcMom, mcMom)) == 24) return true;
46  return false;
47 }
48 
49 
50 // Returns the id and the pointer to the mother of "mc" not being itself
51 int mc_tools::mom(const reco::GenParticle &mc, const reco::GenParticle *&mcMom){
52  mcMom = static_cast<const reco::GenParticle *>(mc.mother());
53  if(mcMom){
54  if(mcMom->pdgId() == mc.pdgId()) return mom(*mcMom, mcMom);
55  else return mcMom->pdgId();
56  } else return 0;
57 }
58 
59 // Returns the index of the mother in the "indices" list
60 int mc_tools::getMomIndex(const reco::GenParticle &mc, vector<pair<int, const reco::GenParticle *> > indices){
61  const reco::GenParticle * mcMom = static_cast<const reco::GenParticle *>(mc.mother());
62  if(mcMom){
63  for(size_t ind=0; ind<indices.size(); ind++)
64  if(indices[ind].second == mcMom) return static_cast<int>(ind);
65  return getMomIndex(*mcMom, indices);
66  } else return -1;
67 }
68 
69 // Checks if "mc" eventually decays to "id" (after it stops decays to itself)
70 bool mc_tools::decaysTo(const reco::GenParticle &mc, size_t id, const reco::GenParticle *&mcDau){
71  bool idInDaughters(false);
72  for(size_t idau(0); idau < mc.numberOfDaughters(); idau++) {
73  size_t dauid(abs(mc.daughter(idau)->pdgId()));
74  if(dauid == id) {
75  idInDaughters = true;
76  mcDau = static_cast<const reco::GenParticle *>(mc.daughter(idau));
77  }
78  if(dauid == abs(mc.pdgId())) return decaysTo(*static_cast<const reco::GenParticle*>((mc.daughter(idau))), id, mcDau);
79  } // Loop over daughters
80  return idInDaughters;
81 }
82 // Code for identifying gluon splitting based on
83 // https://raw.githubusercontent.com/cms-btv-pog/RecoBTag-PerformanceMeasurements/7_4_X/plugins/BTagAnalyzer.cc
84 bool mc_tools::isFromGSP(const reco::Candidate *c)
85 {
86  // for example, in case a matched genParton does not exist
87  if(!c) return false;
88 
89  bool isFromGSP = false;
90  if( c->numberOfMothers() == 1 ) {
91  const reco::Candidate* dau = c;
92  const reco::Candidate* mom = c->mother();
93  while( dau->numberOfMothers() == 1 && !( isHardProcess(mom->status()) && (abs(mom->pdgId())==4 || abs(mom->pdgId())==5) ) ) {
94  if( abs(mom->pdgId())==21 && (abs(c->pdgId())==4 || abs(c->pdgId())==5) )
95  {
96  isFromGSP = true;
97  break;
98  }
99  dau = mom;
100  mom = dau->mother();
101  }
102  }
103 
104  return isFromGSP;
105 }
106 
107 bool mc_tools::isHardProcess(const int status)
108 {
109  // Assume Pythia8 status codes
110  if( status>=21 && status<=29 ) return true;
111  else return false;
112 
113 }
114 
115 void mc_tools::printParticle(const reco::GenParticle &mc){
116  int momid(0);
117  if(mc.mother()) momid = mc.mother()->pdgId();
118  cout<<setw(8)<<mc.pdgId()<<", mom "<<setw(8)<<momid<<", status "<<setw(3)<<mc.status()
119  <<", (pt,eta,phi) = ("<<setw(7)<<roundNumber(mc.pt(),2)<<", "
120  <<setw(5)<<roundNumber(mc.eta(),2)<<", "<<setw(5)<<roundNumber(mc.phi(),2)
121  <<"), mass "<<setw(6)<<roundNumber(mc.mass(),1)
122  <<", nDau "<<setw(2)<<mc.numberOfDaughters()<<": ";
123  for(size_t idau(0); idau < mc.numberOfDaughters(); idau++) {
124  cout<<mc.daughter(idau)->pdgId();
125  if(idau < mc.numberOfDaughters()-1) cout<<", ";
126  }
127  cout<<endl;
128 
129 }
130 
131 void mc_tools::getMassPoints(TString mpoints, int &mgluino, int &mlsp){
132 
133  TString ml(mpoints), mg(mpoints);
134  if (mpoints.Contains("TChiHH")){
135  mg.Remove(0, ml.Last('_')+1);
136  mg.ReplaceAll("\n","");
137  ml = "1";
138  if(!mg.IsFloat()){
139  cout<<"ERROR: Improper parsing of mass points"<<endl;
140  cout<<"Mass of gluino parsed as \""<<mg<<"\""<<endl;
141  exit(0);
142  }
143  } else {
144  ml.Remove(0, ml.Last('_')+1);
145  ml.ReplaceAll("\n","");
146  mg.Remove(mg.Last('_'));
147  mg.Remove(0, mg.Last('_')+1);
148 
149  if(!mg.IsFloat() || !ml.IsFloat()){
150  cout<<"ERROR: Improper parsing of mass points"<<endl;
151  cout<<"Mass of gluino parsed as \""<<mg<<"\""<<endl;
152  cout<<"Mass of LSP parsed as \""<<ml<<"\""<<endl;
153  exit(0);
154  }
155  }
156 
157  mgluino = mg.Atoi();
158  mlsp = ml.Atoi();
159 }
160 
162 }
163 
165 }
166 
~mc_tools()
Definition: mc_tools.cc:164
STL namespace.
int numChargeDaughters(const reco::GenParticle &mc)
Definition: mc_tools.cc:19
bool isLast(const reco::GenParticle &mc, size_t id)
Definition: mc_tools.cc:29
bool fromTau(const reco::GenParticle &mc)
Definition: mc_tools.cc:42
bool decaysTo(const reco::GenParticle &mc, size_t id, const reco::GenParticle *&mcDau)
Definition: mc_tools.cc:70
bool fromWOrWTau(const reco::GenParticle &mc)
Definition: mc_tools.cc:33
int mom(const reco::GenParticle &mc, const reco::GenParticle *&mcMom)
Definition: mc_tools.cc:51
mc_tools()
Definition: mc_tools.cc:161
bool isHardProcess(const int status)
Definition: mc_tools.cc:107
TString roundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cc:478
void getMassPoints(TString mpoints, int &mgluino, int &mlsp)
Definition: mc_tools.cc:131
bool hasDaughter(const reco::GenParticle &mc, size_t id)
Definition: mc_tools.cc:11
bool isFromGSP(const reco::Candidate *c)
Definition: mc_tools.cc:84
tuple ind
Definition: resubmit.py:140
int getMomIndex(const reco::GenParticle &mc, std::vector< std::pair< int, const reco::GenParticle * > > indices)
Definition: mc_tools.cc:60
void printParticle(const reco::GenParticle &mc)
Definition: mc_tools.cc:115