CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TestPythiaDecays.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Lukas/TestPythiaDecays
4 // Class: TestPythiaDecays
5 //
13 //
14 // Original Author: Lukas Vanelderen
15 // Created: Tue, 13 May 2014 09:50:05 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
31 
35 
36 // pythia
37 #include <Pythia8/Pythia.h>
38 
39 // root
40 #include "TH1D.h"
41 #include "TFile.h"
42 #include "TLorentzVector.h"
43 
44 //
45 // class declaration
46 //
47 
49 public:
50  explicit TestPythiaDecays(const edm::ParameterSet&);
52 
53  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
54 
55 
56 private:
57  virtual void beginJob() override;
58  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
59  virtual void endJob() override;
60 
61  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
62  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
63  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
64  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
65 
66  // ----------member data ---------------------------
67  std::vector<int> pids;
68  std::map<int,TH1D*> h_mass;
69  std::map<int,TH1D*> h_p;
70  std::map<int,TH1D*> h_v;
71  std::map<int,TH1D*> h_mass_ref;
72  std::map<int,TH1D*> h_plt; // plt: proper life time
73  std::map<int,TH1D*> h_originVertexRho;
74  std::map<int,TH1D*> h_originVertexZ;
75  std::map<int,TH1D*> h_decayVertexRho;
76  std::map<int,TH1D*> h_decayVertexZ;
77  std::map<int,TH1D*> h_plt_ref; // plt: proper life time
78  std::map<int,TH1D*> h_br;
79  std::map<int,TH1D*> h_br_ref;
80 
81  std::map<int,std::vector<string> > knownDecayModes;
82 
83  Pythia8::Pythia * pythia;
85 };
86 
87 //
88 // constants, enums and typedefs
89 //
90 
91 //
92 // static data member definitions
93 //
94 
95 //
96 // constructors and destructor
97 //
99 {
100 
101  // output file
102  outputFile = iConfig.getParameter<std::string>("outputFile");
103 
104  // create pythia8 instance to access particle data
105  pythia = new Pythia8::Pythia();
106  pythia->init();
107  Pythia8::ParticleData pdt = pythia->particleData;
108 
109  // which particles will we study?
110  pids.push_back(15); // tau
111  pids.push_back(211); // pi+
112  pids.push_back(111); // pi0
113  pids.push_back(130); // K0L
114  pids.push_back(321); // K+
115  pids.push_back(323); // K*(392)
116  pids.push_back(411); // D+
117  pids.push_back(521); // B+
118 
119  // define histograms
120  for(size_t i = 0;i<pids.size();++i){
121 
122  int pid = abs(pids[i]);
123 
124  // get particle data
125  if(!pdt.isParticle(pid)){
126  std::cout << "ERROR: BAD PARTICLE, pythia is not aware of pid " << pid << std::endl;
127  std::exit(1);
128  }
129  Pythia8::ParticleDataEntry * pd = pdt.particleDataEntryPtr(pid);
130 
131  // mass histograms
132  double m0 = pd->m0();
133  double w = pd->mWidth();
134  double mmin,mmax;
135  if( w == 0){
136  mmin = m0 - m0/1000.;
137  mmax = m0 + m0/1000.;
138  }
139  else{
140  mmin = m0 - 2*w;
141  mmax = m0 + 2*w;
142  }
143  std::stringstream strstr;
144  strstr << "mass_" << pid;
145  h_mass[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,mmin,mmax);
146  h_mass_ref[pid] = (TH1D*)(h_mass[pid]->Clone(strstr.str().c_str()));
147  h_mass_ref[pid]->SetTitle(h_mass_ref[pid]->GetName());
148  if(w==0)
149  h_mass_ref[pid]->Fill(m0);
150  else{
151  for(int b =1;b<=h_mass_ref[pid]->GetNbinsX();++b){
152  double _val = h_mass_ref[pid]->GetBinCenter(b);
153  h_mass_ref[pid]->SetBinContent(b,TMath::BreitWigner(_val,m0,w));
154  }
155  }
156 
157  // p histogram
158  strstr.str("");
159  strstr << "p_" << pid;
160  h_p[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,20);
161 
162  // v histogram
163  strstr.str("");
164  strstr << "v_" << pid;
165  h_v[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,1.);
166 
167  // ctau histograms
168  double ctau0 = pd->tau0()/10.;
169  strstr.str("");
170  strstr << "plt_" << pid;
171  h_plt[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,std::min(5.*ctau0,500.));
172  h_plt_ref[pid] = (TH1D*)(h_plt[pid]->Clone(strstr.str().c_str()));
173  h_plt_ref[pid]->SetTitle(h_plt_ref[pid]->GetName());
174  for(int b =1;b<=h_plt_ref[pid]->GetNbinsX();++b){
175  double _val = h_plt_ref[pid]->GetBinCenter(b);
176  h_plt_ref[pid]->SetBinContent(b,TMath::Exp(-_val/ctau0)); //convert mm to cm
177  }
178 
179 
180  // br histograms
181  strstr.str("");
182  strstr << "br_" << pid;
183  h_br[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),0,0,0);
184  h_br[pid]->SetBit(TH1::kCanRebin);
185  h_br_ref[pid] = (TH1D*)(h_br[pid]->Clone(strstr.str().c_str()));
186  h_br_ref[pid]->SetTitle(h_br_ref[pid]->GetName());
187  knownDecayModes[pid] = vector<string>();
188  for(int d = 0;d<pd->sizeChannels();++d){
189  Pythia8::DecayChannel & channel = pd->channel(d);
190  std::vector<int> prod;
191  for(int p = 0;p<channel.multiplicity();++p){
192  int pId = abs(channel.product(p));
193  // from FastSimulation/Event/src/KineParticleFilter.cc
194  bool particleCut = ( pId > 10 && pId != 12 && pId != 14 &&
195  pId != 16 && pId != 18 && pId != 21 &&
196  (pId < 23 || pId > 40 ) &&
197  (pId < 81 || pId > 100 ) && pId != 2101 &&
198  pId != 3101 && pId != 3201 && pId != 1103 &&
199  pId != 2103 && pId != 2203 && pId != 3103 &&
200  pId != 3203 && pId != 3303 );
201  if(particleCut)
202  prod.push_back(abs(channel.product(p)));
203  }
204  std::sort(prod.begin(),prod.end());
205  strstr.str("");
206  for(size_t p = 0;p<prod.size();++p){
207  strstr << "_" << prod[p];
208  }
209  std::string label = strstr.str();
210  h_br[pid]->Fill(label.c_str(),0.);
211  h_br_ref[pid]->Fill(label.c_str(),channel.bRatio());
212  h_br[pid]->SetEntries(0);
213  knownDecayModes[pid].push_back(label);
214  }
215 
216  // vertex plots
217  strstr.str("");
218  strstr << "originVertexRho_" << pid;
219  h_originVertexRho[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,200);
220  strstr.str("");
221  strstr << "originVertexZ_" << pid;
222  h_originVertexZ[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,400);
223  strstr.str("");
224  strstr << "decayVertexRho_" << pid;
225  h_decayVertexRho[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,200);
226  strstr.str("");
227  strstr << "decayVertexZ_" << pid;
228  h_decayVertexZ[pid] = new TH1D(strstr.str().c_str(),strstr.str().c_str(),100,0,400);
229  }
230 
231 }
232 
233 
235 {
236 
237  // do anything here that needs to be done at desctruction time
238  // (e.g. close files, deallocate resources etc.)
239  TFile * f = TFile::Open(outputFile.c_str(),"RECREATE");
240  f->cd();
241  f->mkdir("observed");
242  f->mkdir("prediction");
243  for(size_t i = 0;i<pids.size();++i){
244  int pid = pids[i];
245  f->cd("observed");
246  h_mass[pid]->Write();
247  h_plt[pid]->Write();
248  h_br[pid]->Write();
249  h_originVertexZ[pid]->Write();
250  h_originVertexRho[pid]->Write();
251  h_decayVertexZ[pid]->Write();
252  h_decayVertexRho[pid]->Write();
253  h_p[pid]->Write();
254  h_v[pid]->Write();
255  f->cd("prediction");
256  h_mass_ref[pid]->Write();
257  h_plt_ref[pid]->Write();
258  h_br_ref[pid]->Write();
259  }
260  f->Close();
261 }
262 
263 
264 //
265 // member functions
266 //
267 
268 // ------------ method called for each event ------------
269 void
271 {
272  using namespace edm;
273 
274  Handle<std::vector<SimTrack> > simtracks;
275  iEvent.getByLabel("famosSimHits",simtracks);
276 
277  Handle<std::vector<SimVertex> > simvertices;
278  iEvent.getByLabel("famosSimHits",simvertices);
279 
280  // create maps
281 
282  // initialize
283  std::map<size_t,std::vector<size_t> > childMap; // child indices vs parent index
284  std::map<size_t,int> parentMap; // parent index vs child index
285  for(size_t j = 0;j<simtracks->size();j++){
286  childMap[j] = std::vector<size_t>();
287  parentMap[j] = -1;
288  }
289 
290  // do the mapping
291  for(size_t j = 0;j<simtracks->size();j++){
292  size_t childIndex = j;
293  const SimTrack & child = simtracks->at(childIndex);
294  if(child.noVertex())
295  continue;
296  const SimVertex & vertex = simvertices->at(child.vertIndex());
297  if(vertex.noParent())
298  continue;
299  size_t parentIndex = vertex.parentIndex();
300  childMap[parentIndex].push_back(childIndex);
301  parentMap[childIndex] = int(parentIndex);
302  }
303 
304 
305  for(size_t j = 0;j<simtracks->size();j++){
306  const SimTrack & parent = simtracks->at(j);
307  int pid = abs(parent.type());
308  if(std::find(pids.begin(),pids.end(),pid)==pids.end())
309  continue;
310 
311  // fill mass hist
312  double mass = parent.momentum().M();
313  h_mass[pid]->Fill(mass);
314 
315  // fill p hist
316  h_p[pid]->Fill(parent.momentum().P());
317 
318  // fill vertex position hist
319  if(!parent.noVertex()){
320  const SimVertex & originVertex = simvertices->at(parent.vertIndex());
321  h_originVertexRho[pid]->Fill(originVertex.position().Rho());
322  h_originVertexZ[pid]->Fill(std::fabs(originVertex.position().Z()));
323  }
324  if(childMap[j].size() > 0){
325  const SimTrack & child = simtracks->at(childMap[j][0]);
326  const SimVertex & decayVertex = simvertices->at(child.vertIndex());
327  h_decayVertexRho[pid]->Fill(decayVertex.position().Rho());
328  h_decayVertexZ[pid]->Fill(std::fabs(decayVertex.position().Z()));
329  }
330  }
331 
332 
333  for(std::map<size_t,std::vector<size_t> >::iterator it = childMap.begin();it != childMap.end();++it){
334 
335  // fill ctau hist
336  size_t parentIndex = it->first;
337  const SimTrack & parent = simtracks->at(parentIndex);
338  int pid = abs(parent.type());
339  vector<size_t> & childIndices = it->second;
340  if(childIndices.size() == 0)
341  continue;
342 
343  if(std::find(pids.begin(),pids.end(),pid)==pids.end())
344  continue;
345 
346 
347  const SimVertex & origin_vertex = simvertices->at(parent.vertIndex());
348  const SimTrack & child0 = simtracks->at(childIndices[0]);
349  const SimVertex & decay_vertex = simvertices->at(child0.vertIndex());
350 
351  TLorentzVector lv_origin_vertex(origin_vertex.position().X(),origin_vertex.position().Y(),origin_vertex.position().Z(),origin_vertex.position().T());
352  TLorentzVector lv_decay_vertex(decay_vertex.position().X(),decay_vertex.position().Y(),decay_vertex.position().Z(),decay_vertex.position().T());
353  TLorentzVector lv_dist = lv_decay_vertex - lv_origin_vertex;
354  TLorentzVector lv_parent(parent.momentum().Px(),parent.momentum().Py(),parent.momentum().Pz(),parent.momentum().E());
355  TVector3 boost = lv_parent.BoostVector();
356  lv_dist.Boost(-boost);
357  h_v[pid]->Fill(boost.Mag());
358  double plt = lv_dist.T();
359  h_plt[pid]->Fill(plt);
360 
361  // fill br hist
362  std::vector<int> prod;
363  for(size_t d = 0;d<childIndices.size();++d){
364  prod.push_back(abs(simtracks->at(childIndices[d]).type()));
365  }
366  std::sort(prod.begin(),prod.end());
367  std::stringstream strstr;
368  for(size_t p = 0;p<prod.size();++p){
369  strstr << "_" << prod[p];
370  }
371  std::string label = strstr.str();
372  if(std::find(knownDecayModes[pid].begin(),knownDecayModes[pid].end(),label)==knownDecayModes[pid].end())
373  label = "u" + label;
374  h_br[pid]->Fill(label.c_str(),1.);
375  h_br_ref[pid]->Fill(label.c_str(),0.); // keep h_br and h_br_ref in sync
376  }
377 }
378 
379 
380 // ------------ method called once each job just before starting event loop ------------
381 void
383 {
384 }
385 
386 // ------------ method called once each job just after ending the event loop ------------
387 void
389 {
390 }
391 
392 // ------------ method called when starting to processes a run ------------
393 /*
394 void
395 TestPythiaDecays::beginRun(edm::Run const&, edm::EventSetup const&)
396 {
397 }
398 */
399 
400 // ------------ method called when ending the processing of a run ------------
401 /*
402 void
403 TestPythiaDecays::endRun(edm::Run const&, edm::EventSetup const&)
404 {
405 }
406 */
407 
408 // ------------ method called when starting to processes a luminosity block ------------
409 /*
410 void
411 TestPythiaDecays::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
412 {
413 }
414 */
415 
416 // ------------ method called when ending the processing of a luminosity block ------------
417 /*
418 void
419 TestPythiaDecays::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
420 {
421 }
422 */
423 
424 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
425 void
427  //The following says we do not know what parameters are allowed so do no validation
428  // Please change this to state exactly what you do use, even if it is no parameters
430  desc.setUnknown();
431  descriptions.addDefault(desc);
432 }
433 
434 //define this as a plug-in
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::map< int, std::vector< string > > knownDecayModes
list parent
Definition: dbtoconf.py:74
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void endJob() override
std::map< int, TH1D * > h_v
std::vector< int > pids
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
std::map< int, TH1D * > h_decayVertexZ
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
int parentIndex() const
Definition: SimVertex.h:33
Pythia8::Pythia * pythia
std::map< int, TH1D * > h_br_ref
std::map< int, TH1D * > h_plt_ref
std::map< int, TH1D * > h_decayVertexRho
std::map< int, TH1D * > h_originVertexZ
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
bool noVertex() const
Definition: SimTrack.h:30
double f[11][100]
const math::XYZTLorentzVectorD & position() const
Definition: CoreSimVertex.h:26
#define end
Definition: vmac.h:37
HepPDT::ParticleData ParticleData
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
int vertIndex() const
index of the vertex in the Event container (-1 if no vertex)
Definition: SimTrack.h:29
std::map< int, TH1D * > h_plt
tuple pid
Definition: sysUtil.py:22
double b
Definition: hdecay.h:120
TestPythiaDecays(const edm::ParameterSet &)
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:25
std::string outputFile
#define begin
Definition: vmac.h:30
std::map< int, TH1D * > h_br
const math::XYZTLorentzVectorD & momentum() const
Definition: CoreSimTrack.h:22
std::map< int, TH1D * > h_mass
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tuple cout
Definition: gather_cfg.py:121
std::map< int, TH1D * > h_p
T w() const
bool noParent() const
Definition: SimVertex.h:34
std::map< int, TH1D * > h_mass_ref
virtual void beginJob() override
std::map< int, TH1D * > h_originVertexRho
tuple size
Write out results.