import numpy as np
import matplotlib.pyplot as plt
import ROOT

class root_to_hist:

    def __init__(self, tfile, hist):

        self.__load_file(tfile)
        self.__load_hist(hist)
        self.__convert()

    def __load_file(self, tfile):

        self.temp_tfile = ROOT.TFile(tfile)

    def __load_hist(self, hist):

        self.temp_hist = self.temp_tfile.Get(hist)

    def get_hist(self):

        return self.temp_hist    

    def __convert(self):
    
        temp_hist = self.temp_hist
        nbins = temp_hist.GetNbinsX()
        xlow = temp_hist.GetXaxis().GetXmin()
        xhigh = temp_hist.GetXaxis().GetXmax()
        self.bins = np.linspace(xlow, xhigh, nbins+1)
        # Get bin content
        self.yields = []
        self.weights = []
        self.errors = []
        for bin in range(nbins+1):

            self.yields.append(temp_hist.GetBinCenter(bin))
            self.weights.append(temp_hist.GetBinContent(bin))
            self.errors.append(temp_hist.GetBinError(bin))   
            
        #self.histogram = plt.hist(self.bins, self.yields, weights=self.weights, log=True, histtype='step')
            



