Belle II Software  release-05-01-25
b2file-normalize.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import tempfile
5 import subprocess
6 import ROOT
7 import hashlib
8 
9 testFile = tempfile.NamedTemporaryFile()
10 fileName = testFile.name
11 
12 rootfile = ROOT.TFile.Open(fileName, 'RECREATE')
13 hist = ROOT.TH1F('hist', 'TextHistogram', 100, 0, 1)
14 ROOT.gRandom.SetSeed(42)
15 for i in range(1000):
16  hist.Fill(ROOT.gRandom.Uniform())
17 hist.Write()
18 oldHist = hist.Clone()
19 oldHist.SetDirectory(0)
20 del(rootfile)
21 
22 subprocess.run(['b2file-normalize', '-i', '-n', 'NormalizedRootFile', '-r', '61408', fileName], check=True)
23 
24 newRootFile = ROOT.TFile.Open(fileName)
25 newHist = newRootFile.Get('hist')
26 assert 0 == newHist.Chi2Test(oldHist, 'PCHI2')
27 del(newRootFile)
28 
29 checksum = hashlib.md5()
30 checksum.update(open(fileName, 'rb').read())
31 print(checksum.digest().hex())
32 assert checksum.digest().hex() == '4b616ad813c35498f6f8aa09f2bcebda'
33 del(testFile)