CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
chain_test.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 import unittest
3 import os
4 import shutil
5 
6 from .chain import Chain
7 from PhysicsTools.HeppyCore.utils.testtree import create_tree
8 
9 testfname = 'test_tree.root'
10 
11 class ChainTestCase(unittest.TestCase):
12 
13  def setUp(self):
14  self.chain = Chain(testfname, 'test_tree')
15 
16  def test_file(self):
17  '''Test that the test file exists'''
18  self.assertTrue(os.path.isfile(testfname))
19 
21  self.assertRaises(ValueError,
22  Chain, 'non_existing_file.root')
23 
25  chain = Chain(testfname)
26  self.assertEqual(len(self.chain), 100)
27 
28  def test_load_1(self):
29  '''Test that the chain has the correct number of entries'''
30  self.assertEqual(len(self.chain), 100)
31 
32  def test_load_2(self):
33  '''Test chaining of two files.'''
34  tmpfile = testfname.replace('test_tree', 'test_tree_2_tmp')
35  shutil.copyfile(testfname, tmpfile)
36  chain = Chain(testfname.replace('.root', '*.root'), 'test_tree')
37  self.assertEqual(len(chain), 200)
38  os.remove(tmpfile)
39 
40  def test_load_3(self):
41  '''Test LFN/root-fn loading'''
42  chain = Chain(["root://{0}".format(os.path.abspath(testfname))], 'test_tree')
43  self.assertEqual(len(chain), 100)
44 
45  def test_iterate(self):
46  '''Test iteration'''
47  for ev in self.chain:
48  pass
49  self.assertTrue(True)
50 
51  def test_get(self):
52  '''Test direct event access'''
53  event = self.chain[2]
54  self.assertEqual(event.var1, 2.)
55 
56 
57 if __name__ == '__main__':
58  create_tree(testfname)
59  unittest.main()
def create_tree
Definition: testtree.py:6