Skip to content
Snippets Groups Projects
Commit e5067f56 authored by Christos Anastopoulos's avatar Christos Anastopoulos
Browse files

Some more flake8 related tests , for the .py under the test folder

parent b0459bf9
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ import unittest ...@@ -8,7 +8,7 @@ import unittest
class TestEgammaFactory(unittest.TestCase): class TestEgammaFactory(unittest.TestCase):
def test_eventinfo(self): def test_eventinfo(self):
event = ROOT.xAOD.TEvent() ROOT.xAOD.TEvent()
factory = ROOT.EgammaFactory() factory = ROOT.EgammaFactory()
ei1 = factory.create_eventinfo(True, 100000) ei1 = factory.create_eventinfo(True, 100000)
self.assertTrue(ei1.eventType(ROOT.xAOD.EventInfo.IS_SIMULATION)) self.assertTrue(ei1.eventType(ROOT.xAOD.EventInfo.IS_SIMULATION))
...@@ -16,7 +16,7 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -16,7 +16,7 @@ class TestEgammaFactory(unittest.TestCase):
self.assertFalse(ei2.eventType(ROOT.xAOD.EventInfo.IS_SIMULATION)) self.assertFalse(ei2.eventType(ROOT.xAOD.EventInfo.IS_SIMULATION))
def test_unconverted(self): def test_unconverted(self):
event = ROOT.xAOD.TEvent() ROOT.xAOD.TEvent()
factory = ROOT.EgammaFactory() factory = ROOT.EgammaFactory()
runnumber = 10000 runnumber = 10000
...@@ -32,11 +32,13 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -32,11 +32,13 @@ class TestEgammaFactory(unittest.TestCase):
self.assertAlmostEqual(ph.caloCluster().eta(), eta) self.assertAlmostEqual(ph.caloCluster().eta(), eta)
self.assertAlmostEqual(ph.phi(), phi) self.assertAlmostEqual(ph.phi(), phi)
self.assertAlmostEqual(ph.caloCluster().phi(), phi) self.assertAlmostEqual(ph.caloCluster().phi(), phi)
self.assertAlmostEqual(ph.caloCluster().auxdata("float")("etaCalo"), eta) self.assertAlmostEqual(
self.assertAlmostEqual(ph.caloCluster().auxdata("float")("phiCalo"), phi) ph.caloCluster().auxdata("float")("etaCalo"), eta)
self.assertAlmostEqual(
ph.caloCluster().auxdata("float")("phiCalo"), phi)
self.assertAlmostEqual(ph.e(), e, delta=0.01) self.assertAlmostEqual(ph.e(), e, delta=0.01)
self.assertEqual(ph.caloCluster().e(), e) self.assertEqual(ph.caloCluster().e(), e)
for i in xrange(3): for i in range(3):
self.assertGreater(ph.caloCluster().energyBE(i), 0) self.assertGreater(ph.caloCluster().energyBE(i), 0)
def test_converted(self): def test_converted(self):
...@@ -56,15 +58,17 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -56,15 +58,17 @@ class TestEgammaFactory(unittest.TestCase):
self.assertAlmostEqual(ph.caloCluster().eta(), eta) self.assertAlmostEqual(ph.caloCluster().eta(), eta)
self.assertAlmostEqual(ph.phi(), phi) self.assertAlmostEqual(ph.phi(), phi)
self.assertAlmostEqual(ph.caloCluster().phi(), phi) self.assertAlmostEqual(ph.caloCluster().phi(), phi)
self.assertAlmostEqual(ph.caloCluster().auxdata("float")("etaCalo"), eta) self.assertAlmostEqual(
self.assertAlmostEqual(ph.caloCluster().auxdata("float")("phiCalo"), phi) ph.caloCluster().auxdata("float")("etaCalo"), eta)
self.assertAlmostEqual(
ph.caloCluster().auxdata("float")("phiCalo"), phi)
self.assertAlmostEqual(ph.e(), e, delta=0.01) self.assertAlmostEqual(ph.e(), e, delta=0.01)
self.assertEqual(ph.caloCluster().e(), e) self.assertEqual(ph.caloCluster().e(), e)
for i in xrange(3): for i in range(3):
self.assertGreater(ph.caloCluster().energyBE(i), 0) self.assertGreater(ph.caloCluster().energyBE(i), 0)
def test_photon(self): def test_photon(self):
event = ROOT.xAOD.TEvent() ROOT.xAOD.TEvent()
factory = ROOT.EgammaFactory() factory = ROOT.EgammaFactory()
runnumber = 10000 runnumber = 10000
...@@ -89,8 +93,10 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -89,8 +93,10 @@ class TestEgammaFactory(unittest.TestCase):
self.assertAlmostEqual(ph.caloCluster().eta(), eta) self.assertAlmostEqual(ph.caloCluster().eta(), eta)
self.assertAlmostEqual(ph.phi(), phi) self.assertAlmostEqual(ph.phi(), phi)
self.assertAlmostEqual(ph.caloCluster().phi(), phi) self.assertAlmostEqual(ph.caloCluster().phi(), phi)
self.assertAlmostEqual(ph.caloCluster().auxdata("float")("etaCalo"), eta) self.assertAlmostEqual(
self.assertAlmostEqual(ph.caloCluster().auxdata("float")("phiCalo"), phi) ph.caloCluster().auxdata("float")("etaCalo"), eta)
self.assertAlmostEqual(
ph.caloCluster().auxdata("float")("phiCalo"), phi)
self.assertAlmostEqual(ph.e(), e, delta=0.01) self.assertAlmostEqual(ph.e(), e, delta=0.01)
self.assertEqual(ph.caloCluster().e(), e) self.assertEqual(ph.caloCluster().e(), e)
...@@ -101,7 +107,7 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -101,7 +107,7 @@ class TestEgammaFactory(unittest.TestCase):
tool.applyCorrection(ph, ei) tool.applyCorrection(ph, ei)
def test_electron(self): def test_electron(self):
event = ROOT.xAOD.TEvent() ROOT.xAOD.TEvent()
factory = ROOT.EgammaFactory() factory = ROOT.EgammaFactory()
runnumber = 10000 runnumber = 10000
...@@ -124,14 +130,16 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -124,14 +130,16 @@ class TestEgammaFactory(unittest.TestCase):
self.assertAlmostEqual(el.caloCluster().eta(), eta) self.assertAlmostEqual(el.caloCluster().eta(), eta)
self.assertAlmostEqual(el.phi(), phi) self.assertAlmostEqual(el.phi(), phi)
self.assertAlmostEqual(el.caloCluster().phi(), phi) self.assertAlmostEqual(el.caloCluster().phi(), phi)
self.assertAlmostEqual(el.caloCluster().auxdata("float")("etaCalo"), eta) self.assertAlmostEqual(
self.assertAlmostEqual(el.caloCluster().auxdata("float")("phiCalo"), phi) el.caloCluster().auxdata("float")("etaCalo"), eta)
self.assertAlmostEqual(
el.caloCluster().auxdata("float")("phiCalo"), phi)
self.assertAlmostEqual(el.e(), e, delta=0.01) self.assertAlmostEqual(el.e(), e, delta=0.01)
self.assertEqual(el.caloCluster().e(), e) self.assertEqual(el.caloCluster().e(), e)
self.assertAlmostEqual(el.trackParticle().eta(), eta, delta=0.00001) self.assertAlmostEqual(el.trackParticle().eta(), eta, delta=0.00001)
def test_clean(self): def test_clean(self):
event = ROOT.xAOD.TEvent() ROOT.xAOD.TEvent()
factory = ROOT.EgammaFactory() factory = ROOT.EgammaFactory()
runnumber = 10000 runnumber = 10000
...@@ -147,7 +155,7 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -147,7 +155,7 @@ class TestEgammaFactory(unittest.TestCase):
self.assertAlmostEqual(el.eta(), 1) self.assertAlmostEqual(el.eta(), 1)
def test_decoration(self): def test_decoration(self):
event = ROOT.xAOD.TEvent() ROOT.xAOD.TEvent()
factory = ROOT.EgammaFactory() factory = ROOT.EgammaFactory()
runnumber = 10000 runnumber = 10000
...@@ -158,7 +166,6 @@ class TestEgammaFactory(unittest.TestCase): ...@@ -158,7 +166,6 @@ class TestEgammaFactory(unittest.TestCase):
el.auxdecor("double")("mydeco") el.auxdecor("double")("mydeco")
el.auxdataConst("double")("mydeco") el.auxdataConst("double")("mydeco")
#factory.clear()
el = factory.create_electron(1, 2, 3, 4, 5, 6, 7) el = factory.create_electron(1, 2, 3, 4, 5, 6, 7)
el.auxdataConst("double")("mydeco") el.auxdataConst("double")("mydeco")
......
...@@ -31,16 +31,16 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -31,16 +31,16 @@ class TestEgammaResolution(unittest.TestCase):
cls.factory = ROOT.EgammaFactory() cls.factory = ROOT.EgammaFactory()
def loop_combination(self, eta_min=-2.5, eta_max=2.5, eta_step=0.05, def loop_combination(self, eta_min=-2.5, eta_max=2.5, eta_step=0.05,
energy_min=0, energy_max=200E3, energy_step=10E3, energy_min=0, energy_max=200E3, energy_step=10E3,
ptypes=(0, 1, 2, 3), ptypes=(0, 1, 2, 3),
resol_types=(0, 1, 2)): resol_types=(0, 1, 2)):
for ptype in ptypes: for ptype in ptypes:
for resol_type in resol_types: for resol_type in resol_types:
for eta in arange(eta_min, eta_max, eta_step): for eta in arange(eta_min, eta_max, eta_step):
for energy in arange(energy_min, energy_max, energy_step): for energy in arange(energy_min, energy_max, energy_step):
yield ptype, energy, eta, resol_type yield ptype, energy, eta, resol_type
#@unittest.expectedFailure # return nan in the crack (1.37-1.52), inf at >= 2.4 # @unittest.expectedFailure # return nan in the crack (1.37-1.52), inf at >= 2.4
def test_resolution_positive(self): def test_resolution_positive(self):
for ptype, energy, eta, resol_type in self.loop_combination(): for ptype, energy, eta, resol_type in self.loop_combination():
for tool in self.tool_run1, self.tool_run2: for tool in self.tool_run1, self.tool_run2:
...@@ -49,7 +49,8 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -49,7 +49,8 @@ class TestEgammaResolution(unittest.TestCase):
if ptype == 3: if ptype == 3:
continue continue
resolution = tool.getResolution(ptype, energy, eta, resol_type) resolution = tool.getResolution(ptype, energy, eta, resol_type)
self.assertFalse(math.isnan(resolution), msg="resolution is nan for eta=%f" % eta) self.assertFalse(math.isnan(resolution),
msg="resolution is nan for eta=%f" % eta)
self.assertGreater(resolution, 0) self.assertGreater(resolution, 0)
def create_response_run1(self): def create_response_run1(self):
...@@ -57,9 +58,13 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -57,9 +58,13 @@ class TestEgammaResolution(unittest.TestCase):
this should not be executed, to don't invalidate reference file created this should not be executed, to don't invalidate reference file created
on 2015-04-27 with ElectronPhotonFourMomentumCorrection-00-01-23 on 2015-04-27 with ElectronPhotonFourMomentumCorrection-00-01-23
""" """
fout = ROOT.TFile("$ROOTCOREBIN/data/ElectronPhotonFourMomentumCorrection/test_resolution_nonregression_run1_data.root", "recreate") fout = ROOT.TFile(
tree = ROOT.TTree("test_resolution_nonregression_data_run1", "test_resolution_nonregression_data") "$ROOTCOREBIN/data/ElectronPhotonFourMomentumCorrection/test_resolution_nonregression_run1_data.root",
data_ptype, data_resol_type, data_eta, data_energy, data_resolution = map(lambda t: array(t, [0]), ('i', 'i', 'f', 'f', 'f')) "recreate")
tree = ROOT.TTree("test_resolution_nonregression_data_run1",
"test_resolution_nonregression_data")
data_ptype, data_resol_type, data_eta, data_energy, data_resolution = map(
lambda t: array(t, [0]), ('i', 'i', 'f', 'f', 'f'))
tree.Branch("ptype", data_ptype, "ptype/I") tree.Branch("ptype", data_ptype, "ptype/I")
tree.Branch("resol_type", data_resol_type, "resol_type/I") tree.Branch("resol_type", data_resol_type, "resol_type/I")
...@@ -68,7 +73,8 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -68,7 +73,8 @@ class TestEgammaResolution(unittest.TestCase):
tree.Branch("resolution", data_resolution, "resolution/F") tree.Branch("resolution", data_resolution, "resolution/F")
for ptype, energy, eta, resol_type in self.loop_combination_run1(): for ptype, energy, eta, resol_type in self.loop_combination_run1():
resolution = self.tool_run1.getResolution(ptype, energy, eta, resol_type) resolution = self.tool_run1.getResolution(
ptype, energy, eta, resol_type)
data_ptype[0] = ptype data_ptype[0] = ptype
data_energy[0] = energy data_energy[0] = energy
data_eta[0] = eta data_eta[0] = eta
...@@ -86,18 +92,23 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -86,18 +92,23 @@ class TestEgammaResolution(unittest.TestCase):
for eta in arange(0, 1.37, 0.1): for eta in arange(0, 1.37, 0.1):
for e in arange(10E3, 1000E3, 100E3): for e in arange(10E3, 1000E3, 100E3):
for t in 0, 1, 2: for t in 0, 1, 2:
resolution_run1 = self.tool_run1.getResolution(ptype, e, eta, t) resolution_run1 = self.tool_run1.getResolution(
resolution_run2 = tool_run2.getResolution(ptype, e, eta, t) ptype, e, eta, t)
self.assertNotAlmostEqual(resolution_run1, resolution_run2, resolution_run2 = tool_run2.getResolution(
msg="resolution should be different for particle=%d, eta=%f, e=%d, rtype=%d" % (ptype, eta, e, t)) ptype, e, eta, t)
self.assertNotAlmostEqual(resolution_run1,
resolution_run2,
msg="resolution should be different for particle=%d, eta=%f, e=%d, rtype=%d" % (ptype,
eta, e, t))
def test_nonregression_run1(self): def test_nonregression_run1(self):
from PathResolver import PathResolver from PathResolver import PathResolver
rootfile = PathResolver.FindCalibFile("ElectronPhotonFourMomentumCorrection/v8/test_resolution_nonregression_run1_data.root") rootfile = PathResolver.FindCalibFile(
"ElectronPhotonFourMomentumCorrection/v8/test_resolution_nonregression_run1_data.root")
f = ROOT.TFile(rootfile) f = ROOT.TFile(rootfile)
tree = f.Get("test_resolution_nonregression_data_run1") tree = f.Get("test_resolution_nonregression_data_run1")
for ievent in xrange(tree.GetEntries()): for ievent in range(tree.GetEntries()):
tree.GetEntry(ievent) tree.GetEntry(ievent)
resolution = self.tool_run1.getResolution(tree.ptype, resolution = self.tool_run1.getResolution(tree.ptype,
...@@ -105,12 +116,20 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -105,12 +116,20 @@ class TestEgammaResolution(unittest.TestCase):
tree.resol_type) tree.resol_type)
expected_response = tree.resolution expected_response = tree.resolution
if math.isnan(resolution): if math.isnan(resolution):
self.assertTrue(math.isnan(expected_response), msg="resolution is nan, but expected resonse is %f" % expected_response) self.assertTrue(math.isnan(
expected_response), msg="resolution is nan, but expected resonse is %f" % expected_response)
elif math.isnan(expected_response): elif math.isnan(expected_response):
self.assertTrue(math.isnan(resolution), msg="expected response is nan, but resolution is %f" % resolution) self.assertTrue(math.isnan(
resolution), msg="expected response is nan, but resolution is %f" % resolution)
else: else:
self.assertAlmostEqual(resolution, expected_response, self.assertAlmostEqual(resolution, expected_response,
msg="resolution mismatch %f != %f at eta=%f, energy=%f, ptype=%d, resol_type=%d" % (resolution, expected_response, tree.eta, tree.energy, tree.ptype, tree.resol_type)) msg="resolution mismatch %f != %f at eta=%f, energy=%f, ptype=%d, resol_type=%d" % (
resolution,
expected_response,
tree.eta,
tree.energy,
tree.ptype,
tree.resol_type))
@unittest.skip("CHECK") @unittest.skip("CHECK")
def test_resolution_interface(self): def test_resolution_interface(self):
...@@ -125,22 +144,29 @@ class TestEgammaResolution(unittest.TestCase): ...@@ -125,22 +144,29 @@ class TestEgammaResolution(unittest.TestCase):
for energy in energies: for energy in energies:
if particle == 'unconverted': if particle == 'unconverted':
p = self.factory.create_photon(0., 0.1, energy, 0) p = self.factory.create_photon(0., 0.1, energy, 0)
r1 = tool.resolution(energy, 0., 0., ROOT.PATCore.ParticleType.UnconvertedPhoton) r1 = tool.resolution(
energy, 0., 0.,
ROOT.PATCore.ParticleType.UnconvertedPhoton)
r2 = tool.getResolution(p) r2 = tool.getResolution(p)
self.assertAlmostEqual(r1, r2) self.assertAlmostEqual(r1, r2)
elif particle == 'converted': elif particle == 'converted':
p = self.factory.create_photon(0., 0.1, energy, 100) p = self.factory.create_photon(0., 0.1, energy, 100)
r1 = tool.resolution(energy, 0., 0., ROOT.PATCore.ParticleType.ConvertedPhoton) r1 = tool.resolution(
energy, 0., 0.,
ROOT.PATCore.ParticleType.ConvertedPhoton)
r2 = tool.getResolution(p) r2 = tool.getResolution(p)
self.assertAlmostEqual(r1, r2) self.assertAlmostEqual(r1, r2)
elif particle == 'electron': elif particle == 'electron':
p = self.factory.create_electron(0., 0.1, energy) p = self.factory.create_electron(0., 0.1, energy)
r1 = tool.resolution(energy, 0., 0., ROOT.PATCore.ParticleType.Electron) r1 = tool.resolution(
energy, 0., 0.,
ROOT.PATCore.ParticleType.Electron)
r2 = tool.getResolution(p) r2 = tool.getResolution(p)
self.assertAlmostEqual(r1, r2) self.assertAlmostEqual(r1, r2)
else: else:
raise ValueError() raise ValueError()
if __name__ == '__main__': if __name__ == '__main__':
ROOT.gROOT.ProcessLine(".x $ROOTCOREDIR/scripts/load_packages.C") ROOT.gROOT.ProcessLine(".x $ROOTCOREDIR/scripts/load_packages.C")
unittest.main() unittest.main()
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