HPS-MC
func.py
Go to the documentation of this file.
1 """! Miscellaneous math functions."""
2 
3 import gzip
4 import logging
5 
6 logger = logging.getLogger("hpsmc.func")
7 
8 
9 def lint(target_dz, num_electrons, density=6.306e-14):
10  """!
11  Calculate integrated luminosity.
12  @param target_dz target thickness in cm
13  @param num_electrons number of electrons in bunch
14  @param density 1/(cm*pb), default value is for tungsten
15  @return integrated luminosity in 1/pb
16  """
17  return density * target_dz * num_electrons
18 
19 
20 def csection(filename):
21  """!
22  Extract cross-section from gzipped LHE file.
23  WARNING: This function does not work!
24 
25  \todo remove or replace by more useful function
26  @param filename name of input file
27  """
28  logger.info("Using gzip to open '%s'" % filename)
29 
30  with gzip.open(filename, 'rb') as in_file:
31  lines = in_file.readlines()
32 
33  for line in lines:
34  if "Integrated weight" in line:
35  xs = float(line[line.rfind(":") + 1:].strip())
36  break
37 
38  if "xs" not in locals():
39  raise Exception("Could not find 'Integrated weight' in LHE input file.")
40 
41  return xs
42 
43 
44 def mu(filename, target_dz, num_electrons):
45  """!
46  Calculate mu = number of events per bunch.
47  WARNING: This function does not work properly because csection() is broken!
48 
49  @param filename name of input LHE input file containing cross section
50  @param target_dz target thickness in cm
51  @param num_electrons number of electrons in bunch
52  @return number of events per bunch (L_int[1/pb] * xsec[pb])
53  """
54  return lint(target_dz, num_electrons) * csection(filename)
55 
56 
57 def nevents(filename, confirm=False):
58  """!
59  Read number of events in file from LHE header and optionally confirm by counting <event> blocks.
60  @param filename name of input LHE file
61  @param confirm set to True to confirm number of events
62  """
63  with gzip.open(filename, 'rb') as in_file:
64  lines = in_file.readlines()
65 
66  for line in lines:
67  if "nevents" in line:
68  nevents = int(line.split()[0])
69 
70  if "nevents" not in locals():
71  raise Exception("Could not find 'nevents' in LHE input file.")
72 
73  if confirm:
74  event_count = 0
75  for line in lines:
76  if "<event>" in line:
77  event_count += 1
78  if event_count != nevents:
79  raise Exception("The number of events %d from header does not match the count %d in file '%s'." % (nevents, event_count, filename))
80 
81  return nevents
82 
83 
84 def nbunches(filename, target_dz, num_electrons):
85  """!
86  Get the approximate number of beam bunches represented by an LHE file from its event count.
87  @param filename name of input LHE file
88  @param target_dz target thickness in cm
89  @param num_electrons number of electrons in bunch
90  """
91  n = nevents(filename)
92  m = mu(filename, target_dz, num_electrons)
93  return int(n / m)
94 
95 
96 
97 """
98 echo "Transmuting A's to photons..."
99 gunzip -f wab.lhe.gz
100 sed -i 's/\\‍([:blank:]*\\‍)622 /\1 22 /' wab.lhe
101 gzip wab.lhe
102 """
def csection(filename)
Extract cross-section from gzipped LHE file.
Definition: func.py:20
def nbunches(filename, target_dz, num_electrons)
Get the approximate number of beam bunches represented by an LHE file from its event count.
Definition: func.py:84
def lint(target_dz, num_electrons, density=6.306e-14)
Calculate integrated luminosity.
Definition: func.py:9
def nevents(filename, confirm=False)
Read number of events in file from LHE header and optionally confirm by counting <event> blocks.
Definition: func.py:57
def mu(filename, target_dz, num_electrons)
Calculate mu = number of events per bunch.
Definition: func.py:44