Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:50

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