From ca4718ecc49c93187444aa862bab83a43184bea3 Mon Sep 17 00:00:00 2001
From: Frank Winklmeier <fwinkl@cern>
Date: Wed, 11 Nov 2020 15:03:00 +0100
Subject: [PATCH] ParticleGun: enable flake8 and make code compliant

---
 Generators/ParticleGun/CMakeLists.txt         |  2 +-
 Generators/ParticleGun/python/__init__.py     | 17 +++++++++--------
 Generators/ParticleGun/python/histsampling.py | 13 ++++++-------
 Generators/ParticleGun/python/samplers.py     | 17 ++++++++---------
 4 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/Generators/ParticleGun/CMakeLists.txt b/Generators/ParticleGun/CMakeLists.txt
index 5e530ece919..b503f5b4131 100644
--- a/Generators/ParticleGun/CMakeLists.txt
+++ b/Generators/ParticleGun/CMakeLists.txt
@@ -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} )
 
diff --git a/Generators/ParticleGun/python/__init__.py b/Generators/ParticleGun/python/__init__.py
index c03c4fc7c03..bb1155eb914 100644
--- a/Generators/ParticleGun/python/__init__.py
+++ b/Generators/ParticleGun/python/__init__.py
@@ -1,10 +1,11 @@
-# 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
 
 
diff --git a/Generators/ParticleGun/python/histsampling.py b/Generators/ParticleGun/python/histsampling.py
index 6d97ca2a929..1d0e155d91a 100644
--- a/Generators/ParticleGun/python/histsampling.py
+++ b/Generators/ParticleGun/python/histsampling.py
@@ -1,4 +1,4 @@
-# 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
diff --git a/Generators/ParticleGun/python/samplers.py b/Generators/ParticleGun/python/samplers.py
index c706a9a4a61..982be4fc4e0 100644
--- a/Generators/ParticleGun/python/samplers.py
+++ b/Generators/ParticleGun/python/samplers.py
@@ -1,8 +1,7 @@
 # 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
 
-- 
GitLab