CMS 3D CMS Logo

HLTMCtruth.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <istream>
4 #include <fstream>
5 #include <iomanip>
6 #include <string>
7 #include <cmath>
8 #include <functional>
9 #include <cstdlib>
10 #include <cstring>
11 
14 
16 
17  //set parameter defaults
18  _Monte=false;
19  _Gen=false;
20  _Debug=false;
21 }
22 
23 /* Setup the analysis to put the branch-variables into the tree. */
24 void HLTMCtruth::setup(const edm::ParameterSet& pSet, TTree* HltTree) {
25 
26  edm::ParameterSet myMCParams = pSet.getParameter<edm::ParameterSet>("RunParameters") ;
27  std::vector<std::string> parameterNames = myMCParams.getParameterNames() ;
28 
29  for (auto & parameterName : parameterNames){
30  if ( parameterName == "Monte" ) _Monte = myMCParams.getParameter<bool>( parameterName );
31  else if ( parameterName == "GenTracks" ) _Gen = myMCParams.getParameter<bool>( parameterName );
32  else if ( parameterName == "Debug" ) _Debug = myMCParams.getParameter<bool>( parameterName );
33  }
34 
35  const int kMaxMcTruth = 10000;
36  mcpid = new int[kMaxMcTruth];
37  mcstatus = new int[kMaxMcTruth];
38  mcvx = new float[kMaxMcTruth];
39  mcvy = new float[kMaxMcTruth];
40  mcvz = new float[kMaxMcTruth];
41  mcpt = new float[kMaxMcTruth];
42  mceta = new float[kMaxMcTruth];
43  mcphi = new float[kMaxMcTruth];
44 
45  // MCtruth-specific branches of the tree
46  HltTree->Branch("NMCpart",&nmcpart,"NMCpart/I");
47  HltTree->Branch("MCpid",mcpid,"MCpid[NMCpart]/I");
48  HltTree->Branch("MCstatus",mcstatus,"MCstatus[NMCpart]/I");
49  HltTree->Branch("MCvtxX",mcvx,"MCvtxX[NMCpart]/F");
50  HltTree->Branch("MCvtxY",mcvy,"MCvtxY[NMCpart]/F");
51  HltTree->Branch("MCvtxZ",mcvz,"MCvtxZ[NMCpart]/F");
52  HltTree->Branch("MCpt",mcpt,"MCpt[NMCpart]/F");
53  HltTree->Branch("MCeta",mceta,"MCeta[NMCpart]/F");
54  HltTree->Branch("MCphi",mcphi,"MCphi[NMCpart]/F");
55  HltTree->Branch("MCPtHat",&pthatf,"MCPtHat/F");
56  HltTree->Branch("MCWeight",&weightf,"MCWeight/F");
57  HltTree->Branch("MCWeightSign",&weightsignf,"MCWeightSign/F");
58  HltTree->Branch("MCmu3",&nmu3,"MCmu3/I");
59  HltTree->Branch("MCel3",&nel3,"MCel3/I");
60  HltTree->Branch("MCbb",&nbb,"MCbb/I");
61  HltTree->Branch("MCab",&nab,"MCab/I");
62  HltTree->Branch("MCWenu",&nwenu,"MCWenu/I");
63  HltTree->Branch("MCWmunu",&nwmunu,"MCmunu/I");
64  HltTree->Branch("MCZee",&nzee,"MCZee/I");
65  HltTree->Branch("MCZmumu",&nzmumu,"MCZmumu/I");
66  HltTree->Branch("MCptEleMax",&ptEleMax,"MCptEleMax/F");
67  HltTree->Branch("MCptMuMax",&ptMuMax,"MCptMuMax/F");
68  HltTree->Branch("NPUTrueBX0",&npubx0, "NPUTrueBX0/I");
69  HltTree->Branch("NPUgenBX0",&npuvertbx0, "NPUgenBX0/I");
70 
71 }
72 
73 /* **Analyze the event** */
75  const double & pthat,
76  const double & weight,
77  const edm::Handle<std::vector<SimTrack> > & simTracks,
78  const edm::Handle<std::vector<SimVertex> > & simVertices,
79  const edm::Handle<std::vector< PileupSummaryInfo > > & PupInfo,
80  TTree* HltTree) {
81 
82  //std::cout << " Beginning HLTMCtruth " << std::endl;
83 
84  if (_Monte) {
85  int nmc = 0;
86  int mu3 = 0;
87  int el3 = 0;
88  int mab = 0;
89  int mbb = 0;
90  int wel = 0;
91  int wmu = 0;
92  int zee = 0;
93  int zmumu = 0;
94 
95  ptEleMax = -999.0;
96  ptMuMax = -999.0;
97  pthatf = pthat;
98  weightf = weight;
99  weightsignf = (weight > 0) ? 1. : -1.;
100  npubx0 = 0.0;
101  npuvertbx0 = 0;
102 
103  int npvtrue = 0;
104  int npuvert = 0;
105 
106  if(PupInfo.isValid()) {
107  std::vector<PileupSummaryInfo>::const_iterator PVI;
108  for(PVI = PupInfo->begin(); PVI != PupInfo->end(); ++PVI) {
109 
110  int BX = PVI->getBunchCrossing();
111  npvtrue = PVI->getTrueNumInteractions();
112  npuvert = PVI->getPU_NumInteractions();
113 
114  if(BX == 0)
115  {
116  npubx0+=npvtrue;
117  npuvertbx0+=npuvert;
118  }
119  }
120  }
121 
122  if((simTracks.isValid())&&(simVertices.isValid())){
123  for (auto const & j : *simTracks) {
124  int pdgid = j.type();
125  if (abs(pdgid)!=13) continue;
126  double pt = j.momentum().pt();
127  if (pt<5.0) continue;
128  double eta = j.momentum().eta();
129  if (abs(eta)>2.5) continue;
130  if (j.noVertex()) continue;
131  int vertIndex = j.vertIndex();
132  double x = simVertices->at(vertIndex).position().x();
133  double y = simVertices->at(vertIndex).position().y();
134  double r = sqrt(x*x+y*y);
135  if (r>200.) continue; // I think units are cm here
136  double z = simVertices->at(vertIndex).position().z();
137  if (abs(z)>400.) continue; // I think units are cm here
138  mu3 += 1;
139  break;
140  }
141 
142  }
143 
144  if (_Gen && mctruth.isValid()){
145 
146  for (size_t i = 0; i < mctruth->size(); ++ i) {
147  const reco::Candidate & p = (*mctruth)[i];
148 
149  mcpid[nmc] = p.pdgId();
150  mcstatus[nmc] = p.status();
151  mcpt[nmc] = p.pt();
152  mceta[nmc] = p.eta();
153  mcphi[nmc] = p.phi();
154  mcvx[nmc] = p.vx();
155  mcvy[nmc] = p.vy();
156  mcvz[nmc] = p.vz();
157 
158  if ((mcpid[nmc]==24)||(mcpid[nmc]==-24)) { // Checking W -> e/mu nu
159  size_t idg = p.numberOfDaughters();
160  for (size_t j=0; j != idg; ++j){
161  const reco::Candidate & d = *p.daughter(j);
162  if ((d.pdgId()==11)||(d.pdgId()==-11)){wel += 1;}
163  if ((d.pdgId()==13)||(d.pdgId()==-13)){wmu += 1;}
164 // if ( (abs(d.pdgId())!=24) && ((mcpid[nmc])*(d.pdgId())>0) )
165 // {std::cout << "Wrong sign between mother-W and daughter !" << std::endl;}
166  }
167  }
168  if (mcpid[nmc]==23) { // Checking Z -> 2 e/mu
169  size_t idg = p.numberOfDaughters();
170  for (size_t j=0; j != idg; ++j){
171  const reco::Candidate & d = *p.daughter(j);
172  if (d.pdgId()==11){zee += 1;}
173  if (d.pdgId()==-11){zee += 2;}
174  if (d.pdgId()==13){zmumu += 1;}
175  if (d.pdgId()==-13){zmumu += 2;}
176  }
177  }
178 
179  // Set-up flags, based on Pythia-generator information, for avoiding double-counting events when
180  // using both pp->{e,mu}X AND QCD samples
181 // if (((mcpid[nmc]==13)||(mcpid[nmc]==-13))&&(mcpt[nmc]>2.5)) {mu3 += 1;} // Flag for muons with pT > 2.5 GeV/c
182  if (((mcpid[nmc]==11)||(mcpid[nmc]==-11))&&(mcpt[nmc]>2.5)) {el3 += 1;} // Flag for electrons with pT > 2.5 GeV/c
183 
184  if (mcpid[nmc]==-5) {mab += 1;} // Flag for bbar
185  if (mcpid[nmc]==5) {mbb += 1;} // Flag for b
186 
187  if ((mcpid[nmc]==13)||(mcpid[nmc]==-13))
188  {if (p.pt()>ptMuMax) {ptMuMax=p.pt();} } // Save max pt of generated Muons
189  if ((mcpid[nmc]==11)||(mcpid[nmc]==-11))
190  {if (p.pt() > ptEleMax) ptEleMax=p.pt();} // Save max pt of generated Electrons
191 
192  nmc++;
193  }
194 
195  }
196  // else {std::cout << "%HLTMCtruth -- No MC truth information" << std::endl;}
197 
198  nmcpart = nmc;
199  nmu3 = mu3;
200  nel3 = el3;
201  nbb = mbb;
202  nab = mab;
203  nwenu = wel;
204  nwmunu = wmu;
205  if((zee%3)==0){nzee = zee/3;}
206 // else {std::cout << "Z does not decay in e+ e- !" << std::endl;}
207  if ((zmumu%3)==0){nzmumu = zmumu/3;}
208 // else {std::cout << "Z does not decay in mu+ mu- !" << std::endl;}
209 
210  }
211 
212 }
T getParameter(std::string const &) const
int * mcpid
Definition: HLTMCtruth.h:49
bool _Gen
Definition: HLTMCtruth.h:55
float * mcpt
Definition: HLTMCtruth.h:48
virtual double vx() const =0
x coordinate of vertex position
float pthatf
Definition: HLTMCtruth.h:52
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
size_type size() const
Definition: weight.py:1
float weightf
Definition: HLTMCtruth.h:52
virtual double vy() const =0
y coordinate of vertex position
bool _Debug
Definition: HLTMCtruth.h:55
float * mceta
Definition: HLTMCtruth.h:48
int nmcpart
Definition: HLTMCtruth.h:50
virtual int status() const =0
status word
int nzmumu
Definition: HLTMCtruth.h:50
float ptEleMax
Definition: HLTMCtruth.h:53
float ptMuMax
Definition: HLTMCtruth.h:53
virtual int pdgId() const =0
PDG identifier.
T sqrt(T t)
Definition: SSEVec.h:18
float * mcvy
Definition: HLTMCtruth.h:48
int npubx0
Definition: HLTMCtruth.h:51
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool _Monte
Definition: HLTMCtruth.h:55
float weightsignf
Definition: HLTMCtruth.h:52
std::vector< std::string > getParameterNames() const
bool isValid() const
Definition: HandleBase.h:74
virtual double eta() const =0
momentum pseudorapidity
virtual double pt() const =0
transverse momentum
void analyze(const edm::Handle< reco::CandidateView > &mctruth, const double &pthat, const double &weight, const edm::Handle< std::vector< SimTrack > > &simTracks, const edm::Handle< std::vector< SimVertex > > &simVertices, const edm::Handle< std::vector< PileupSummaryInfo > > &PupInfo, TTree *tree)
Definition: HLTMCtruth.cc:74
float * mcvx
Definition: HLTMCtruth.h:48
float * mcphi
Definition: HLTMCtruth.h:48
float * mcvz
Definition: HLTMCtruth.h:48
void setup(const edm::ParameterSet &pSet, TTree *tree)
Definition: HLTMCtruth.cc:24
int nwmunu
Definition: HLTMCtruth.h:50
virtual double vz() const =0
z coordinate of vertex position
virtual size_type numberOfDaughters() const =0
number of daughters
virtual double phi() const =0
momentum azimuthal angle
int npuvertbx0
Definition: HLTMCtruth.h:51
int * mcstatus
Definition: HLTMCtruth.h:49