CMS 3D CMS Logo

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