Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:15:49

0001 from __future__ import absolute_import
0002 import unittest
0003 import os
0004 import shutil
0005 
0006 from .chain import Chain
0007 from PhysicsTools.HeppyCore.utils.testtree import create_tree
0008 
0009 testfname = 'test_tree.root'
0010 
0011 class ChainTestCase(unittest.TestCase):
0012 
0013     def setUp(self):
0014         self.chain = Chain(testfname, 'test_tree')
0015 
0016     def test_file(self):
0017         '''Test that the test file exists'''
0018         self.assertTrue(os.path.isfile(testfname))
0019 
0020     def test_wrong_filename(self):
0021         self.assertRaises(ValueError,
0022                           Chain, 'non_existing_file.root')
0023 
0024     def test_guess_treename(self):
0025         chain = Chain(testfname)
0026         self.assertEqual(len(self.chain), 100)        
0027 
0028     def test_load_1(self):
0029         '''Test that the chain has the correct number of entries'''
0030         self.assertEqual(len(self.chain), 100)
0031 
0032     def test_load_2(self):
0033         '''Test chaining of two files.'''
0034         tmpfile = testfname.replace('test_tree', 'test_tree_2_tmp')
0035         shutil.copyfile(testfname, tmpfile)
0036         chain = Chain(testfname.replace('.root', '*.root'), 'test_tree')
0037         self.assertEqual(len(chain), 200)
0038         os.remove(tmpfile)
0039     
0040     def test_load_3(self):
0041         '''Test LFN/root-fn loading'''
0042         chain = Chain(["root://{0}".format(os.path.abspath(testfname))], 'test_tree')
0043         self.assertEqual(len(chain), 100)
0044 
0045     def test_iterate(self):
0046         '''Test iteration'''
0047         for ev in self.chain:
0048             pass
0049         self.assertTrue(True)
0050 
0051     def test_get(self):
0052         '''Test direct event access'''
0053         event = self.chain[2]
0054         self.assertEqual(event.var1, 2.)
0055 
0056 
0057 if __name__ == '__main__':
0058     create_tree(testfname)
0059     unittest.main()