Skip to content
Snippets Groups Projects
Commit ca4718ec authored by Frank Winklmeier's avatar Frank Winklmeier
Browse files

ParticleGun: enable flake8 and make code compliant

parent c43e9493
No related branches found
No related tags found
No related merge requests found
......@@ -6,5 +6,5 @@
atlas_subdir( ParticleGun )
# Install files from the package:
atlas_install_python_modules( python/*.py )
atlas_install_python_modules( python/*.py POST_BUILD_CMD ${ATLAS_FLAKE8} )
# Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
from AthenaCommon.AppMgr import ServiceMgr as svcMgr
from GeneratorModules.EvgenAlg import EvgenAlg
from ParticleGun.samplers import *
from ParticleGun.histsampling import TH1, TH2
from ParticleGun.samplers import ParticleSampler
from AthenaPython.PyAthena import StatusCode
import ROOT
import random
__author__ = "Andy Buckley <andy.buckley@cern.ch>"
......@@ -35,7 +36,7 @@ class ParticleGun(EvgenAlg):
"""
Pass the AtRndmGenSvc seed to Python's random module, or use a fixed value set via pg.randomSeed.
"""
import McParticleEvent.Pythonizations
import McParticleEvent.Pythonizations # noqa: F401
seed = None
## Use self.randomSeed directly, or if it's None find a seed string from the ATLAS random number service
if self.randomSeed is not None:
......@@ -46,18 +47,18 @@ class ParticleGun(EvgenAlg):
for seedstr in randomSvc.Seeds:
if seedstr.startswith(self.randomStream):
seed = seedstr
self.msg.info("ParticleGun: Using random seed '%s'" % seed)
self.msg.info("ParticleGun: Using random seed '%s'", seed)
break
if seed is None:
self.msg.warning("ParticleGun: Failed to find a seed for the random stream named '%s'" % self.randomStream)
self.msg.warning("ParticleGun: Failed to find a seed for the random stream named '%s'", self.randomStream)
else:
self.msg.warning("ParticleGun: Failed to find random number service called '%s'" % self.randomSvcName)
self.msg.warning("ParticleGun: Failed to find random number service called '%s'", self.randomSvcName)
## Apply the seed
if seed is not None:
random.seed(seed)
return StatusCode.Success
else:
self.msg.error("ParticleGun: randomSeed property not set, and no %s random number service found" % self.randomSvcName)
self.msg.error("ParticleGun: randomSeed property not set, and no %s random number service found", self.randomSvcName)
return StatusCode.Failure
......
# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
"""
Tools for histogram sampling, in particular inverse transform sampling which is
......@@ -19,9 +19,9 @@ def load_hist(*args):
if len(args) == 1 and issubclass(type(args[0]), ROOT.TH1):
h = args[0].Clone()
elif len(args) == 2:
if type(args[0]) is type(args[1]) is str:
if isinstance(args[0], str) and isinstance(args[1], str):
f = ROOT.TFile.Open(args[0])
htmp = f.Get(args[1])
htmp = f.Get(args[1]) # noqa: F841
h = f.Get(args[1]).Clone()
#f.Close()
elif type(args[0]) is ROOT.TFile and type(args[1]) is str:
......@@ -42,14 +42,14 @@ def get_sampling_vars(h):
globalbins = [] # because they aren't easily predicted, nor contiguous
cheights = [0] # cumulative "histogram" from which to uniformly sample
if issubclass(type(h), ROOT.TH1):
for ix in xrange(1, h.GetNbinsX()+1):
for ix in range(1, h.GetNbinsX()+1):
iglobal = h.GetBin(ix)
globalbins.append(iglobal)
globalbin_to_axisbin[iglobal] = (ix,)
cheights.append(cheights[-1] + h.GetBinContent(iglobal))
elif issubclass(type(h), ROOT.TH2):
for ix in xrange(1, h.GetNbinsX()+1):
for iy in xrange(1, h.GetNbinsY()+1):
for ix in range(1, h.GetNbinsX()+1):
for iy in range(1, h.GetNbinsY()+1):
iglobal = h.GetBin(ix, iy)
globalbins.append(iglobal)
globalbin_to_axisbin[iglobal] = (ix, iy)
......@@ -66,7 +66,6 @@ def get_random_bin(globalbins, cheights):
"""
assert len(cheights) == len(globalbins)+1
randomheight = random.uniform(0, cheights[-1])
randombin = None
for i, iglobal in enumerate(globalbins):
if randomheight >= cheights[i] and randomheight < cheights[i+1]:
return iglobal
......
# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
from AthenaPython import PyAthena
import ROOT, math, random
from ParticleGun.histsampling import TH1, TH2
from ParticleGun.histsampling import TH1
## For convenience
PI = math.pi
......@@ -487,7 +486,7 @@ class EEtaMPhiSampler(MomSampler):
=> theta = 2 atan( exp(-eta) )
"""
eta = self.eta()
theta = 2 * math.atan(math.exp(-eta));
theta = 2 * math.atan(math.exp(-eta))
e = self.energy()
m = self.mass()
p = math.sqrt( e**2 - m**2 )
......@@ -505,7 +504,7 @@ class ERapMPhiSampler(MomSampler):
# TODO: ensure that E >= m!
def __init__(self, energy, eta, mass=0.0, phi=[0, TWOPI]):
def __init__(self, energy, rap, mass=0.0, phi=[0, TWOPI]):
self.energy = energy
self.rap = rap
self.mass = mass
......@@ -559,8 +558,8 @@ class ERapMPhiSampler(MomSampler):
m = self.mass()
pt = math.sqrt( sqrt_pt2_m2**2 - m**2 )
phi = self.phi()
px = pt * math.cos(phi);
py = pt * math.sin(phi);
px = pt * math.cos(phi)
py = pt * math.sin(phi)
v4 = ROOT.TLorentzVector(px, py, pz, e)
return v4
......@@ -674,7 +673,7 @@ class PtEtaMPhiSampler(MomSampler):
=> theta = 2 atan( exp(-eta) )
"""
eta = self.eta()
theta = 2 * math.atan(math.exp(-eta));
theta = 2 * math.atan(math.exp(-eta))
pt = self.pt()
p = pt / math.sin(theta)
phi = self.phi()
......@@ -746,8 +745,8 @@ class PtRapMPhiSampler(MomSampler):
e = sqrt_pt2_m2 * math.cosh(y)
pz = sqrt_pt2_m2 * math.sinh(y)
phi = self.phi()
px = pt * math.cos(phi);
py = pt * math.sin(phi);
px = pt * math.cos(phi)
py = pt * math.sin(phi)
v4 = ROOT.TLorentzVector(px, py, pz, e)
return v4
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment