ATLAS Offline Software
Generators/ParticleGun/python/__init__.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 from GeneratorModules.EvgenAlg import EvgenAlg
4 from ParticleGun.samplers import ParticleSampler
5 from ParticleGun.samplers import * # noqa: F401, F403 (import into our namespace)
6 from AthenaPython.PyAthena import StatusCode
7 import ROOT,random
8 from cppyy.gbl import std as std
9 try:
10  from AthenaPython.PyAthena import HepMC3 as HepMC
11  HepMCVersion=3
12 except ImportError:
13  from AthenaPython.PyAthena import HepMC as HepMC
14  HepMCVersion=2
15 __author__ = "Andy Buckley <andy.buckley@cern.ch>, Andrii Verbytskyi <andrii.verbytskyi@cern.ch>"
16 
17 class ParticleGun(EvgenAlg):
18  """
19  A simple but flexible algorithm for generating events from simple distributions.
20  """
21 
22  def __init__(self, name="ParticleGun", randomStream="ParticleGun", randomSeed=None):
23  super(ParticleGun, self).__init__(name=name)
24  self.samplers = [ParticleSampler()]
25  self.randomStream = randomStream
26  self.randomSeed = randomSeed
27 
28  @property
29  def sampler(self):
30  "Get the first (and presumed only) sampler"
31  return self.samplers[0] if self.samplers else None
32  @sampler.setter
33  def sampler(self, s):
34  "Set the samplers list to include only a single sampler, s"
35  self.samplers = [s]
36 
37 
38  def initialize(self):
39  return StatusCode.Success
40 
41 
42  def fillEvent(self, evt):
43  """
44  Sample a list of particle properties, which are then used to create a new GenEvent in StoreGate.
45  """
46  # set the random seed
47  offset = self.randomSeed if self.randomSeed is not None else 0
48  seed = ROOT.ATHRNG.calculateSeedsPython(self.randomStream, self._ctx.eventID().event_number(), self._ctx.eventID().run_number(), offset)
49 
50  if seed is None:
51  self.msg.warning("Failed to find a seed for the random stream named '%s'.", self.randomStream)
52  seed = self.randomSeed
53  if seed is not None:
54  self.msg.debug("Set random seed to %s.", str(seed))
55  random.seed(seed)
56  else:
57  self.msg.error("Failed to set random seed.")
58  return StatusCode.Failure
59 
60  if HepMCVersion == 2:
61  evt.weights().push_back(1.0)
62 
63  for s in self.samplers:
64  particles = s.shoot()
65  for p in particles:
66 
67  pos = HepMC.FourVector(p.pos.X(), p.pos.Y(), p.pos.Z(), p.pos.T())
68  gv = HepMC.GenVertex(pos)
69  ROOT.SetOwnership(gv, False)
70  evt.add_vertex(gv)
71 
72 
73  mom = HepMC.FourVector(p.mom.Px(), p.mom.Py(), p.mom.Pz(), p.mom.E())
74  gp = HepMC.GenParticle()
75  gp.set_status(1)
76  gp.set_pdg_id(p.pid)
77  gp.set_momentum(mom)
78  if p.mass is not None:
79  gp.set_generated_mass(p.mass)
80  ROOT.SetOwnership(gp, False)
81  gv.add_particle_out(gp)
82 
83  if HepMCVersion == 3:
84  evt.set_units(HepMC.Units.MEV, HepMC.Units.MM)
85  evt.weights().push_back(1.0)
86  beamparticle1 = std.shared_ptr['HepMC3::GenParticle'](HepMC.GenParticle(HepMC.FourVector(0,0,-7000,7000),2212,4))
87  ROOT.SetOwnership(beamparticle1, False)
88  beamparticle2 = std.shared_ptr['HepMC3::GenParticle'](HepMC.GenParticle(HepMC.FourVector(0,0,7000,7000),2212,4))
89  ROOT.SetOwnership(beamparticle2, False)
90  primary = std.shared_ptr['HepMC3::GenVertex'](HepMC.GenVertex())
91  ROOT.SetOwnership(primary, False)
92  primary.add_particle_in(beamparticle1)
93  primary.add_particle_in(beamparticle2)
94  evt.add_vertex(primary)
95  evt.add_beam_particle(beamparticle1)
96  evt.add_beam_particle(beamparticle2)
97  #Create all the needed particles
98  for s in self.samplers:
99  particles = s.shoot()
100  for p in particles:
101  # Create the production vertex of the particle
102  gv = std.shared_ptr['HepMC3::GenVertex'](HepMC.GenVertex(HepMC.FourVector(p.pos.X(), p.pos.Y(), p.pos.Z(), p.pos.T())))
103  ROOT.SetOwnership(gv, False)
104  evt.add_vertex(gv)
105  # Create a fake particle to connect the production vertex of the particle of interest to the primary
106  fakeparticle = std.shared_ptr['HepMC3::GenParticle'](HepMC.GenParticle(HepMC.FourVector(p.mom.Px(), p.mom.Py(), p.mom.Pz(), p.mom.E()),p.pid,11))
107  ROOT.SetOwnership(fakeparticle, False)
108  gv.add_particle_in(fakeparticle)
109  primary.add_particle_out(fakeparticle)
110  # Create the particle
111  gp = std.shared_ptr['HepMC3::GenParticle'](HepMC.GenParticle(HepMC.FourVector(p.mom.Px(), p.mom.Py(), p.mom.Pz(), p.mom.E()),p.pid,1))
112  ROOT.SetOwnership(gp, False)
113  if p.mass is not None:
114  gp.set_generated_mass(p.mass)
115  gv.add_particle_out(gp)
116  for p in evt.particles():
117  att = std.shared_ptr['HepMC3::IntAttribute'](HepMC.IntAttribute(p.id()))
118  p.add_attribute("barcode",att)
119  for v in evt.vertices():
120  att = std.shared_ptr['HepMC3::IntAttribute'](HepMC.IntAttribute(v.id()))
121  v.add_attribute("barcode",att)
122  return StatusCode.Success
plotting.plot_kinematics.run_number
run_number
Definition: plot_kinematics.py:29
python.ParticleGun
Definition: Generators/ParticleGun/python/__init__.py:17
samplers
python.ParticleGun.initialize
def initialize(self)
Definition: Generators/ParticleGun/python/__init__.py:38
python.ParticleGun.randomSeed
randomSeed
Definition: Generators/ParticleGun/python/__init__.py:26
python.ParticleGun.sampler
def sampler(self)
Definition: Generators/ParticleGun/python/__init__.py:29
python.ParticleGun.__init__
def __init__(self, name="ParticleGun", randomStream="ParticleGun", randomSeed=None)
Definition: Generators/ParticleGun/python/__init__.py:22
python.ParticleGun.fillEvent
def fillEvent(self, evt)
Definition: Generators/ParticleGun/python/__init__.py:42
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
python.ParticleGun.samplers
samplers
Definition: Generators/ParticleGun/python/__init__.py:24
str
Definition: BTagTrackIpAccessor.cxx:11
error
Definition: IImpactPoint3dEstimator.h:70
python.ParticleGun.randomStream
randomStream
Definition: Generators/ParticleGun/python/__init__.py:25