ATLAS Offline Software
Loading...
Searching...
No Matches
SFGen_i/python/EventFiller.py
Go to the documentation of this file.
1# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2
3# This module fills the EVNT file using the information from the LHE file generated by SFGen in the 'evrecs/' directory
4
5from GeneratorModules.EvgenAlg import EvgenAlg
6from AthenaPython.PyAthena import StatusCode
7try:
8 from AthenaPython.PyAthena import HepMC3 as HepMC
9except ImportError:
10 from AthenaPython.PyAthena import HepMC as HepMC
11
12import os
13import math
14import ROOT
15
16# Getting pz in MeV unit
17
18def init_pz(energy):
19 return math.sqrt(energy**2 - 938.272046**2)
20
21class LheEVNTFiller(EvgenAlg):
22
23# Init definition
24
25 def __init__(self, ecmEnergy, name="LheEVNTFiller"):
26 super(LheEVNTFiller, self).__init__(name=name)
27 self.beamEnergyMeV = ecmEnergy * 1000. / 2.
28
29 fileName = "evrecs/evrecout.dat"
30 outputFileName = "outputs/outputout.dat"
31 eventsProcessed = 0
32
33# Checking out evrec and output directories
34
35 def initialize(self):
36
37 if(os.path.isfile(self.fileName) and os.path.isfile(self.outputFileName)):
38 print(self.fileName)
39 return StatusCode.Success
40 else:
41 return StatusCode.Failure
42
43# Filling the EVNT file
44
45 def fillEvent(self, evt):
46
47 eventsSeen = 0
48 firstLine = True
49
50# Reading cross section in the outputout.dat file in nb unit
51
52 with open(self.outputFileName, 'r') as inputOutputFile:
53 if self.eventsProcessed == 0:
54 for line in inputOutputFile:
55 if 'Cross section =' in line:
56 splitLine = line.split()
57 factor = 1.
58 if(splitLine[-1] == "pb"):
59 factor = 0.001
60 if(splitLine[-1] == "fb"):
61 factor = 0.000001
62 if(splitLine[-1] == "ub"):
63 factor = 1000.
64 if(splitLine[-1] == "mb"):
65 factor = 1000000.
66 print("MetaData: cross-section (nb)= "+str(float(splitLine[3])*factor))
67
68 break
69
70 with open(self.fileName, 'r') as inputfile:
71 event = False
72 for line in inputfile:
73 if not event and '<event>' not in line:
74 continue
75 # Check if we are just starting an event
76 if not event and '<event>' in line and eventsSeen == self.eventsProcessed:
77 event = True
78 continue
79 # Check if we have finished and are doing something else
80 if '<' in line or '>' in line:
81 event = False
82 eventsSeen += 1
83 firstLine = True
84 continue
85 if event and firstLine:
86 firstLine = False
87 evt.weights().push_back(float(line.split()[2]))
88
89 # Add the initial state protons
90 pos = HepMC.FourVector(0.0, 0.0, 0.0, 0.0)
91 gv = HepMC.GenVertex(pos)
92 ROOT.SetOwnership(gv, False)
93 evt.add_vertex(gv)
94 mom = HepMC.FourVector(0., 0., init_pz(self.beamEnergyMeV), self.beamEnergyMeV)
95 gp = HepMC.GenParticle()
96 gp.set_status( 2 )
97 gp.set_pdg_id( 2212 )
98 gp.set_momentum(mom)
99 gp.set_generated_mass(938.272046)
100 ROOT.SetOwnership(gp, False)
101 gv.add_particle_out(gp)
102
103 pos = HepMC.FourVector(0.0, 0.0, 0.0, 0.0)
104 gv = HepMC.GenVertex(pos)
105 ROOT.SetOwnership(gv, False)
106 evt.add_vertex(gv)
107 mom = HepMC.FourVector(0., 0., -init_pz(self.beamEnergyMeV), self.beamEnergyMeV)
108 gp = HepMC.GenParticle()
109 gp.set_status( 2 )
110 gp.set_pdg_id( 2212 )
111 gp.set_momentum(mom)
112 gp.set_generated_mass(938.272046)
113 ROOT.SetOwnership(gp, False)
114 gv.add_particle_out(gp)
115
116 continue
117 if event:
118 pos = HepMC.FourVector(0.0, 0.0, 0.0, 0.0)
119 gv = HepMC.GenVertex(pos)
120 ROOT.SetOwnership(gv, False)
121 evt.add_vertex(gv)
122 mom = HepMC.FourVector( float(line.split()[6])*1000. , float(line.split()[7])*1000. , float(line.split()[8])*1000. , float(line.split()[9])*1000. )
123 gp = HepMC.GenParticle()
124 gp.set_status(int(line.split()[1]) )
125 gp.set_pdg_id(int(line.split()[0]) )
126 gp.set_momentum(mom)
127 gp.set_generated_mass(float(line.split()[10]) * 1000.)
128 ROOT.SetOwnership(gp, False)
129 gv.add_particle_out(gp)
130
131 self.eventsProcessed += 1
132
133 return StatusCode.Success
134
if(febId1==febId2)
void print(char *figname, TCanvas *c1)
__init__(self, ecmEnergy, name="LheEVNTFiller")
void initialize()