#
# Simple pyROOT script to plot the "trend" of bytes generated by an 8-bit Linear-Feedback Shift Register (LFSR).
# The "random" sequence repeats after 256 values, fill the histogram to show only the first 4 replicated sequences.
#
# Luca Pacher - pacher@to.infn.it
# Spring 2020
#


from ROOT import TH1F, gStyle, gPad ;

## don't show the statistics pane
gStyle.SetOptStat(0)


## histogram the "trend" of shift-register outputs (not a true histogram indeed, just a trend)
histoBytes = TH1F("histo", "LFSR output-code distribution", 256*4, -0.5, 256*4 + 0.5)   ## extra values simply goes into overflow bin

## open the ASCII file containing shift-register output bytes
file = open("bytes.txt","r")


## **NOTE: in a ROOT histogram bin zero contains underflows, bin N+1 overflows
bin = 1

## enjoy the Python magic ...
for line in file :

	histoBytes.SetBinContent(bin, int(line.split("\n")[0]))
	bin = bin + 1


histoBytes.Draw()

## the "random" sequence repeats after 256 values, show only 4 repeated sequences
histoBytes.GetXaxis().SetRangeUser(0, 256*4)


## cosmetics
histoBytes.GetXaxis().SetTitle("tick")
histoBytes.GetYaxis().SetTitle("LFSR[7:0] integer value")
histoBytes.GetYaxis().CenterTitle()

gPad.SetGridx()
gPad.SetGridy()


## set y-axis log scale for easier pattern recognition
gPad.SetLogy()
gPad.Modified()
gPad.Update()

