Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4EvtGenDecayer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4EvtGenDecayer.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id: G4EvtGenDecayer.cc,v 1.2 2014/10/07 03:06:54 mccumber Exp $
27 //
30 
31 // ----------------------------------------------------------------------------
32 // According to TPythia6Decayer class in Root:
33 // http://root.cern.ch/
34 // see http://root.cern.ch/root/License.html
35 // ----------------------------------------------------------------------------
36 
37 #include "G4EvtGenDecayer.hh"
38 #include "PHEvtGenRandomEngine.hh" // for PHEvtGenRandomEngine
39 
40 #include <EvtGen/EvtGen.hh> // for EvtGen
41 #include <EvtGenBase/EvtAbsRadCorr.hh> // for EvtAbsRadCorr
42 #include <EvtGenBase/EvtHepMCEvent.hh> // for EvtHepMCEvent
43 #include <EvtGenBase/EvtId.hh> // for EvtId
44 #include <EvtGenBase/EvtPDL.hh> // for EvtPDL
45 #include <EvtGenBase/EvtParticle.hh> // for EvtParticle
46 #include <EvtGenBase/EvtParticleFactory.hh> // for EvtParticleFactory
47 #include <EvtGenBase/EvtRandom.hh> // for EvtRandom
48 #include <EvtGenBase/EvtVector4R.hh> // for EvtVector4R
49 #include <EvtGenExternal/EvtExternalGenList.hh> // for EvtExternalGenList
50 
51 #include <HepMC3/Attribute.h> // for string
52 #include <HepMC3/FourVector.h> // for FourVector
53 #include <HepMC3/GenEvent.h> // for GenEvent
54 #include <HepMC3/GenParticle.h> // for GenParticle
55 #include <HepMC3/GenParticle_fwd.h> // for GenParticlePtr
56 #include <HepMC3/GenVertex.h> // for GenVertex
57 #include <HepMC3/GenVertex_fwd.h> // for GenVertexPtr
58 #include <HepMC3/Print.h> // for Print
59 
60 #include <Geant4/G4DecayProducts.hh>
61 #include <Geant4/G4DynamicParticle.hh>
62 #include <Geant4/G4ParticleDefinition.hh> // for G4ParticleDefinition
63 #include <Geant4/G4ParticleTable.hh>
64 #include <Geant4/G4String.hh> // for G4String
65 #include <Geant4/G4SystemOfUnits.hh>
66 #include <Geant4/G4ThreeVector.hh> // for G4ThreeVector
67 #include <Geant4/G4Track.hh>
68 #include <Geant4/G4Types.hh> // for G4int, G4double
69 #include <Geant4/G4VExtDecayer.hh> // for G4VExtDecayer
70 
71 #include <algorithm> // for copy, max
72 #include <cstdlib> // for abs
73 #include <iostream> // for operator<<, basic_ostream:...
74 #include <memory> // for __shared_ptr_access
75 #include <stack> // for operator<<
76 #include <string> // for operator<<
77 #include <vector> // for vector
78 
79 class EvtDecayBase;
80 
81 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
82 
84  : G4VExtDecayer("G4EvtGenDecayer")
85  , fVerboseLevel(0)
86 {
88 
89  EvtRandom::setRandomEngine(static_cast<EvtRandomEngine*>(mEvtGenRandomEngine));
90 
91  radCorrEngine = genList.getPhotosModel();
92 
93  extraModels = genList.getListOfModels();
94 
95  // the hardcoded paths are temporary
96  const char* offline_main = getenv("OFFLINE_MAIN");
97 
98  if (!offline_main)
99  {
100  exit(1);
101  }
102 
103  string decay = string(offline_main) + "/share/EvtGen/DECAY.DEC"; // Using PDG 2019 reference as the input for now
104  string evt = string(offline_main) + "/share/EvtGen/evt.pdl";
105 
106  mEvtGen = new EvtGen(decay, evt, static_cast<EvtRandomEngine*>(mEvtGenRandomEngine), radCorrEngine, &extraModels);
107  extraModels.clear();
108 }
109 
110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
111 
113 {
114  delete mEvtGen;
115 
116  delete radCorrEngine;
117 }
118 
119 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
120 
121 G4ParticleDefinition* G4EvtGenDecayer::
122  GetParticleDefinition(int ParPDGID) const
123 {
125 
126  // get particle definition from G4ParticleTable
127  G4int pdgEncoding = ParPDGID;
128  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
129  G4ParticleDefinition* particleDefinition = nullptr;
130  if (pdgEncoding != 0)
131  {
132  particleDefinition = particleTable->FindParticle(pdgEncoding);
133  }
134 
135  return particleDefinition;
136 }
137 
138 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
139 
140 G4DecayProducts* G4EvtGenDecayer::ImportDecayProducts(const G4Track& track)
141 {
143 
144  G4ThreeVector momentum = track.GetMomentum();
145  G4double etot = track.GetDynamicParticle()->GetTotalEnergy();
146 
147  G4ParticleDefinition* particleDef = track.GetDefinition();
148  G4int pdgEncoding = particleDef->GetPDGEncoding();
149 
150  EvtVector4R p_init(etot / GeV, momentum.x() / GeV, momentum.y() / GeV, momentum.z() / GeV);
151 
152  EvtId parentID = EvtPDL::evtIdFromLundKC(pdgEncoding);
153 
154  auto mParticle = EvtParticleFactory::particleFactory(parentID, p_init);
155  // Decay the particle
156  mEvtGen->generateDecay(mParticle);
157 
158  EvtHepMCEvent theEvent;
159 
160  theEvent.constructEvent(mParticle);
161 
162  HepMC3::GenEvent* evt = theEvent.getEvent(); // Directly use HepMC3 records
163 
164  G4DecayProducts* decayProducts = new G4DecayProducts(*(track.GetDynamicParticle()));
165 
166  std::stack<HepMC3::GenParticlePtr> stack;
167 
168  auto part = evt->particles()[0];
169 
170  if (part->pdg_id() != pdgEncoding)
171  {
172  std::cout << "Issue Found: The first particle in the decay chain is NOT the incoming particle decayed by EvtGen" << std::endl;
173  }
174 
175  stack.push(part);
176 
177  while (!stack.empty())
178  {
179  auto particle = stack.top();
180  stack.pop();
181 
182  for (const auto& children : particle->children())
183  {
184  float EvtWidth = 9999;
185  int pdg = children->pdg_id();
186 
187  G4ParticleDefinition* particleDefinition = GetParticleDefinition(pdg);
188  if (EvtPDL::evtIdFromLundKC(pdg).getId() != -1)
189  {
190  EvtWidth = EvtPDL::getWidth(EvtPDL::evtIdFromLundKC(pdg)) * 1000000; // Decay Width Unit: GeV -> keV to define stable particles in EvtGen -> G4
191  }
192 
193  if (particleDefinition && EvtWidth < WidthThreshold)
194  {
195  bool SameVtxPos = true;
196 
197  if (children->production_vertex()->position().x() != particle->end_vertex()->position().x() || children->production_vertex()->position().y() != particle->end_vertex()->position().y() || children->production_vertex()->position().z() != particle->end_vertex()->position().z() || children->production_vertex()->position().t() != particle->end_vertex()->position().t())
198  {
199  SameVtxPos = false;
200  }
201  if (!SameVtxPos)
202  {
203  std::cout << "Issue Found: in this vertex, particles pushed to GEANT have different production positions, need to check and understand why!!!" << std::endl;
204  std::cout << "This is due to the particle PDGID: " << children->pdg_id() << " from the decay vertex of the parent particle:" << particle->pdg_id() << std::endl;
205  std::cout << "Here is the Event Content" << std::endl;
206  const HepMC3::GenEvent& CheckEvent = *evt;
207  HepMC3::Print::content(CheckEvent);
208  }
209 
210  G4ThreeVector G4Mom = G4ThreeVector(children->momentum().px() * GeV, children->momentum().py() * GeV, children->momentum().pz() * GeV);
211  G4DynamicParticle* dynamicParticle = new G4DynamicParticle(particleDefinition, G4Mom);
212 
213  if (dynamicParticle)
214  {
215  if (fVerboseLevel > 0)
216  {
217  std::cout << " G4 particle name: "
218  << dynamicParticle->GetDefinition()->GetParticleName()
219  << std::endl;
220  }
221 
222  decayProducts->PushProducts(dynamicParticle);
223  }
224  else
225  {
226  std::cout << "Dynamical particle to be pushed from EvtGen to GEANT 4 is not properly defined: need to check why this happens!" << std::endl;
227  exit(1);
228  }
229  }
230  else
231  {
232  stack.push(children);
233  }
234  }
235  }
236 
237  evt->clear();
238  mParticle->deleteTree();
239  return decayProducts;
240 }
241 
242 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
243 
244 void G4EvtGenDecayer::SetDecayTable(const string& decayTable, bool useXml)
245 {
246  mEvtGen->readUDecay(decayTable, useXml);
247 }