CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PythiaFilterGammaGamma.cc
Go to the documentation of this file.
3 #include "Math/GenVector/VectorUtil.h"
4 //#include "CLHEP/HepMC/GenParticle.h"
6 
7 #include "TFile.h"
8 #include "TLorentzVector.h"
9 
10 #include <iostream>
11 
12 using namespace edm;
13 using namespace std;
14 using namespace HepMC;
15 
16 
18  label(iConfig.getUntrackedParameter<std::string>("moduleLabel",std::string("generator"))),
19  //fileName(iConfig.getUntrackedParameter<std::string>("fileName", std::string("plots.root"))),
20  maxEvents(iConfig.getUntrackedParameter<int>("maxEvents", 100000)),
21  ptSeedThr(iConfig.getUntrackedParameter<double>("PtSeedThr")),
22  etaSeedThr(iConfig.getUntrackedParameter<double>("EtaSeedThr")),
23  ptGammaThr(iConfig.getUntrackedParameter<double>("PtGammaThr")),
24  etaGammaThr(iConfig.getUntrackedParameter<double>("EtaGammaThr")),
25  ptTkThr(iConfig.getUntrackedParameter<double>("PtTkThr")),
26  etaTkThr(iConfig.getUntrackedParameter<double>("EtaTkThr")),
27  ptElThr(iConfig.getUntrackedParameter<double>("PtElThr")),
28  etaElThr(iConfig.getUntrackedParameter<double>("EtaElThr")),
29  dRTkMax(iConfig.getUntrackedParameter<double>("dRTkMax")),
30  dRSeedMax(iConfig.getUntrackedParameter<double>("dRSeedMax")),
31  dPhiSeedMax(iConfig.getUntrackedParameter<double>("dPhiSeedMax")),
32  dEtaSeedMax(iConfig.getUntrackedParameter<double>("dEtaSeedMax")),
33  dRNarrowCone(iConfig.getUntrackedParameter<double>("dRNarrowCone")),
34  pTMinCandidate1(iConfig.getUntrackedParameter<double>("PtMinCandidate1")),
35  pTMinCandidate2(iConfig.getUntrackedParameter<double>("PtMinCandidate2")),
36  etaMaxCandidate(iConfig.getUntrackedParameter<double>("EtaMaxCandidate")),
37  invMassWide(iConfig.getUntrackedParameter<double>("InvMassWide")),
38  invMassNarrow(iConfig.getUntrackedParameter<double>("InvMassNarrow")),
39  nTkConeMax(iConfig.getUntrackedParameter<int>("NTkConeMax")),
40  nTkConeSum(iConfig.getUntrackedParameter<int>("NTkConeSum")),
41  acceptPrompts(iConfig.getUntrackedParameter<bool>("AcceptPrompts")),
42  promptPtThreshold(iConfig.getUntrackedParameter<double>("PromptPtThreshold")) {
43 
44  //cout<<"ptSeedThr "<<ptSeedThr<<endl;
45  //cout<<"etaSeedThr "<<etaSeedThr<<endl;
46  //cout<<"ptGammaThr "<<ptGammaThr<<endl;
47  //cout<<"etaGammaThr "<<etaGammaThr<<endl;
48  //cout<<"ptTkThr "<<ptTkThr<<endl;
49  //cout<<"etaTkThr "<<etaTkThr<<endl;
50  //cout<<"ptElThr "<<ptElThr<<endl;
51  //cout<<"etaElThr "<<etaElThr<<endl;
52  //cout<<"dRTkMax "<<dRTkMax<<endl;
53  //cout<<"dRSeedMax "<<dRSeedMax<<endl;
54  //cout<<"dPhiSeedMax "<<dPhiSeedMax<<endl;
55  //cout<<"dEtaSeedMax "<<dEtaSeedMax<<endl;
56  //cout<<"dRNarrowCone "<<dRNarrowCone<<endl;
57  //cout<<"pTMinCandidate1 "<<pTMinCandidate1<<endl;
58  //cout<<"pTMinCandidate2 "<<pTMinCandidate2<<endl;
59  //cout<<"etaMaxCandidate "<<etaMaxCandidate<<endl;
60  //cout<<"invMassWide "<<invMassWide<<endl;
61  //cout<<"invMassNarrow "<<invMassNarrow<<endl;
62  //cout<<"nTkConeMax "<<nTkConeMax<<endl;
63  //cout<<"nTkConeSum "<<nTkConeSum<<endl;
64  //cout<<"acceptPrompts "<<acceptPrompts<<endl;
65  //cout<<"promptPtThreshold "<<promptPtThreshold<<endl;
66 
67  nSelectedEvents = 0;
68  nGeneratedEvents = 0;
69 
70  //char a[20];
71  //for(int i=0; i<2; i++) {
72  // sprintf(a, "PT Seed %d", i+1);
73  // hPtSeed[i] = new TH1D(a, a, 100, 0, 200);
74  // sprintf(a, "Eta Seed %d", i+1);
75  // hEtaSeed[i]= new TH1D(a, a, 100, -3., 3.);
76  // sprintf(a, "Pid Seed %d", i+1);
77  // hPidSeed[i]= new TH1I(a, a, 50, 0, 500);
78  // sprintf(a, "PT Candidate %d", i+1);
79  // hPtCandidate[i] = new TH1D(a, a, 100, 0, 200);
80  // sprintf(a, "Eta Candidate %d", i+1);
81  // hEtaCandidate[i]= new TH1D(a, a, 100, -3., 3.);
82  // sprintf(a, "Pid Candidate %d", i+1);
83  // hPidCandidate[i]= new TH1I(a, a, 50, 0, 500);
84  // sprintf(a, "NTk Iso %d", i+1);
85  // hNTk[i]= new TH1I(a, a, 50, 0, 50);
86  //}
87  //hMassNarrow = new TH1D("Mass narr.", "Mass narr.", 100, 0, 1000);
88  //hMassWide= new TH1D("Mass wide", "Mass wide", 100, 0, 1000);
89  //hNTkSum= new TH1I("NTk Sum Iso", "NTk Sum Iso", 50, 0, 50);
90 
91 }
92 
94 {
95  //writeFile();
96  cout << "Number of Selected Events: " << nSelectedEvents << endl;
97  cout << "Number of Generated Events: " << nGeneratedEvents << endl;
98 }
99 
100 //void PythiaFilterGammaGamma::writeFile() {
101 
102  //TFile* file = new TFile (fileName.c_str(), "recreate");
103 
104  //for(int i=0; i<2; i++) {
105  // hPtSeed[i]->Write();
106  // hEtaSeed[i]->Write();
107  // hPidSeed[i]->Write();
108  // hPtCandidate[i]->Write();
109  // hEtaCandidate[i]->Write();
110  // hPidCandidate[i]->Write();
111  // hNTk[i]->Write();
112  //}
113  //hMassNarrow->Write();
114  //hMassWide->Write();
115  //hNTkSum->Write();
116 
117  //file->Close();
118 
119 //}
120 
122 
123  if(nSelectedEvents >= maxEvents) {
124  //writeFile();
125  throw cms::Exception("endJob")<<"We have reached the maximum number of events...";
126  }
127 
129 
130  bool accepted = false;
131 
133  iEvent.getByLabel(label, evt);
134  myGenEvent = evt->GetEvent();
135 
136  std::vector<const GenParticle*> seeds, egamma, stable;
137  std::vector<const GenParticle*>::const_iterator itPart, itStable, itEn;
138 
139  for(HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p) {
140 
141  if (
142  ((*p)->status()==1&&(*p)->pdg_id() == 22) || // gamma
143  ((*p)->status()==1&&abs((*p)->pdg_id()) == 11) || // electron
144  (*p)->pdg_id() == 111 || // pi0
145  abs((*p)->pdg_id()) == 221 || // eta
146  abs((*p)->pdg_id()) == 331 || // eta prime
147  abs((*p)->pdg_id()) == 113 || // rho0
148  abs((*p)->pdg_id()) == 223) // omega
149  {
150  // check for eta and pT threshold for seed in gamma, el
151  if ((*p)->momentum().perp() > ptSeedThr &&
152  fabs((*p)->momentum().eta()) < etaSeedThr) {
153 
154  // check if found is daughter of one already taken
155  bool isUsed = false;
156 
157  const GenParticle* mother = (*p)->production_vertex() ?
158  *((*p)->production_vertex()->particles_in_const_begin()) : 0;
159  const GenParticle* motherMother = (mother != 0 && mother->production_vertex()) ?
160  *(mother->production_vertex()->particles_in_const_begin()) : 0;
161  const GenParticle* motherMotherMother = (motherMother != 0 && motherMother->production_vertex()) ?
162  *(motherMother->production_vertex()->particles_in_const_begin()) : 0;
163 
164  for(itPart = seeds.begin(); itPart != seeds.end(); itPart++) {
165 
166  if ((*itPart) == mother ||
167  (*itPart) == motherMother ||
168  (*itPart) == motherMotherMother) {
169  isUsed = true;
170  break;
171  }
172  }
173 
174  if (!isUsed) seeds.push_back(*p);
175  }
176  }
177  }
178 
179  if (seeds.size() < 2) return accepted;
180 
181  for(HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p) {
182 
183  if ((*p)->status() == 1) {
184 
185  // save charged stable tracks
186  if (abs((*p)->pdg_id()) == 211 ||
187  abs((*p)->pdg_id()) == 321 ||
188  abs((*p)->pdg_id()) == 11 ||
189  abs((*p)->pdg_id()) == 13 ||
190  abs((*p)->pdg_id()) == 15) {
191  // check if it passes the cut
192  if ((*p)->momentum().perp() > ptTkThr &&
193  fabs((*p)->momentum().eta()) < etaTkThr) {
194  stable.push_back(*p);
195  }
196  }
197 
198  // save egamma for candidate calculation
199  if ((*p)->pdg_id() == 22 &&
200  (*p)->momentum().perp() > ptGammaThr &&
201  fabs((*p)->momentum().eta()) < etaGammaThr) {
202  egamma.push_back(*p);
203  }
204  if (abs((*p)->pdg_id()) == 11 &&
205  (*p)->momentum().perp() > ptElThr &&
206  fabs((*p)->momentum().eta()) < etaElThr) {
207  egamma.push_back(*p);
208  }
209  }
210  }
211 
212  std::vector<int> nTracks;
213  std::vector<TLorentzVector> candidate, candidateNarrow, candidateSeed;
214  std::vector<const GenParticle*>::iterator itSeed;
215 
216  for(itSeed = seeds.begin(); itSeed != seeds.end(); ++itSeed) {
217 
218  TLorentzVector energy, narrowCone, temp1, temp2, tempseed;
219 
220  tempseed.SetXYZM((*itSeed)->momentum().px(), (*itSeed)->momentum().py(), (*itSeed)->momentum().pz(), 0);
221  for(itEn = egamma.begin(); itEn != egamma.end(); ++itEn) {
222  temp1.SetXYZM((*itEn)->momentum().px(), (*itEn)->momentum().py(), (*itEn)->momentum().pz(), 0);
223 
224  double DR = temp1.DeltaR(tempseed);
225  double DPhi = temp1.DeltaPhi(tempseed);
226  double DEta = (*itEn)->momentum().eta()-(*itSeed)->momentum().eta();
227  if(DPhi<0) DPhi=-DPhi;
228  if(DEta<0) DEta=-DEta;
229 
230  if (DR < dRSeedMax || (DPhi<dPhiSeedMax&&DEta<dEtaSeedMax)) {
231  energy += temp1;
232  }
233  if (DR < dRNarrowCone) {
234  narrowCone += temp1;
235  }
236  }
237 
238  int counter = 0;
239  //bool isIsolated = true;
240 
241  if ( energy.Et() != 0. ) {
242  if (fabs(energy.Eta()) < etaMaxCandidate) {
243 
244  temp2.SetXYZM(energy.Px(), energy.Py(), energy.Pz(), 0);
245 
246  for(itStable = stable.begin(); itStable != stable.end(); ++itStable) {
247  temp1.SetXYZM((*itStable)->momentum().px(), (*itStable)->momentum().py(), (*itStable)->momentum().pz(), 0);
248  double DR = temp1.DeltaR(temp2);
249  if (DR < dRTkMax) counter++;
250  }
251 
252  if(acceptPrompts) {
253  bool isPrompt=false;
254  if((*itSeed)->status() == 1&&(*itSeed)->pdg_id() == 22) {
255  const GenParticle* mother = (*itSeed)->production_vertex() ?
256  *((*itSeed)->production_vertex()->particles_in_const_begin()) : 0;
257  if(mother) {
258  if(mother->pdg_id()>=-22&&mother->pdg_id()<=22) {
259  const GenParticle* motherMother = (mother != 0 && mother->production_vertex()) ?
260  *(mother->production_vertex()->particles_in_const_begin()) : 0;
261  if(motherMother) {
262  if(motherMother->pdg_id()>=-22&&motherMother->pdg_id()<=22) {
263  if((*itSeed)->momentum().perp()>promptPtThreshold) {
264  isPrompt=true;
265  }
266  }
267  }
268  }
269  }
270  }
271  if(isPrompt) counter=0;
272  }
273  // check number of tracks
274  //if (counter <= nTkConeMax) isIsolated = true;
275  }
276  }
277 
278  // check pt candidate, check nTrack, check eta
279  //if (isIsolated) {
280  candidate.push_back(energy);
281  candidateSeed.push_back(tempseed);
282  candidateNarrow.push_back(narrowCone);
283  nTracks.push_back(counter);
284  //++itSeed;
285  //}
286  }
287 
288  if (candidate.size() <2) return accepted;
289 
290  TLorentzVector minv, minvNarrow;
291 
292 
293  //bool filled=false;
294 
295  int i1, i2;
296  for(unsigned int i=0; i<candidate.size()-1; ++i) {
297 
298  if (candidate[i].Energy() <1.) continue;
299  if(nTracks[i]>nTkConeMax) continue;
300  if (fabs(candidate[i].Eta()) > etaMaxCandidate) continue;
301 
302  for(unsigned int j=i+1; j<candidate.size(); ++j) {
303  if (candidate[j].Energy() <1.) continue;
304  if(nTracks[j]>nTkConeMax) continue;
305  if (fabs(candidate[j].Eta()) > etaMaxCandidate) continue;
306 
307  if (nTracks[i] + nTracks[j] > nTkConeSum) continue;
308 
309  if (candidate[i].Pt() > candidate[j].Pt()) {
310  i1 = i;
311  i2 = j;
312  }
313  else {
314  i1 = j;
315  i2 = i;
316  }
317 
318  if (candidate[i1].Pt() < pTMinCandidate1 || candidate[i2].Pt() < pTMinCandidate2) continue;
319 
320  minv = candidate[i] + candidate[j];
321  if (minv.M() < invMassWide) continue;
322 
323  minvNarrow = candidateNarrow[i] + candidateNarrow[j];
324  if (minvNarrow.M() > invMassNarrow) continue;
325 
326  accepted = true;
327 
328  //if(!filled) {
329  //hMassWide->Fill(minv.M());
330  //hMassNarrow->Fill(minvNarrow.M());
331  //hNTkSum->Fill(nTracks[i] + nTracks[j]);
332  //hPtCandidate[0]->Fill(candidate[i1].Pt());
333  //hPtCandidate[1]->Fill(candidate[i2].Pt());
334  //hEtaCandidate[0]->Fill(candidate[i1].Eta());
335  //hEtaCandidate[1]->Fill(candidate[i2].Eta());
336  //hPidCandidate[0]->Fill(seeds[i1]->pdg_id());
337  //hPidCandidate[1]->Fill(seeds[i2]->pdg_id());
338  //hPtSeed[0]->Fill(candidateSeed[i1].Pt());
339  //hPtSeed[1]->Fill(candidateSeed[i2].Pt());
340  //hEtaSeed[0]->Fill(candidateSeed[i1].Eta());
341  //hEtaSeed[1]->Fill(candidateSeed[i2].Eta());
342  //hPidSeed[0]->Fill(seeds[i1]->pdg_id());
343  //hPidSeed[1]->Fill(seeds[i2]->pdg_id());
344  //hNTk[0]->Fill(nTracks[i1]);
345  //hNTk[1]->Fill(nTracks[i2]);
346  //filled=true;
347  //}
348  }
349  }
350 
351  if (accepted) nSelectedEvents++;
352  return accepted;
353 }
354 
int i
Definition: DBlmapReader.cc:9
static const int stable
Definition: TopGenEvent.h:11
#define abs(x)
Definition: mlp_lapack.h:159
virtual bool filter(edm::Event &, const edm::EventSetup &)
int iEvent
Definition: GenABIO.cc:243
int j
Definition: DBlmapReader.cc:9
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
const HepMC::GenEvent * myGenEvent
PythiaFilterGammaGamma(const edm::ParameterSet &)
tuple cout
Definition: gather_cfg.py:121