def _convert_th1(root_obj: TH1) -> dict: """ Take the values from the histogram and add them to a series of arrays. Parameters ---------- root_obj : TH1 ROOT histogram Returns ------- dict Dictionary with x, y, xerr, yerr points """ assert(isinstance(root_obj, TH1)) x, y, x_err, y_err = [], [], [], [] for n in range(1, root_obj.GetNbinsX() + 1): x.append(root_obj.GetXaxis().GetBinCenter(n)) y.append(root_obj.GetBinContent(n)) x_err.append(root_obj.GetXaxis().GetBinWidth(n) * 0.5) y_err.append(root_obj.GetBinError(n)) return {'x': np.array(x), 'y': np.array(y), 'xerr': np.array(x_err), 'yerr': np.array(y_err)}
def __init__(self, name, projection, nx, xmin, xmax, ny, ymin, ymax, side=0): '''arguments @name name = title name for the pane @param projection = name of the projection (string) eg one of 'xy', 'yz', 'xz' ,'ECAL_thetaphi', 'HCAL_thetaphi' @param nx: points on x axis @param xmin: lower left corner x for ROOT hists @param xmax: upper right corner x for ROOT hists @param nx: points on x axis @param ymin: lower left corner y for ROOT hists @param ymax: upper right corner y for ROOT hists @param side: documents which subscreen we are in 0 = main or left, 1 = right ''' self.side = side self.projection = projection TH1.AddDirectory(False) self.hist = TH2F(name, name, nx, xmin, xmax, ny, ymin, ymax) TH1.AddDirectory(True) self.hist.Draw() self.hist.SetStats(False) self.registered = dict() self.locked = dict()
def __init__(self, name, projection, nx, xmin, xmax, ny, ymin, ymax, dx=600, dy=600): self.projection = projection tx = 50 + self.__class__.nviews * (dx+10) ty = 50 self.canvas = TCanvas(name, name, tx, ty, dx, dy) TH1.AddDirectory(False) self.hist = TH2F(name, name, nx, xmin, xmax, ny, ymin, ymax) TH1.AddDirectory(True) self.hist.Draw() self.hist.SetStats(False) self.registered = dict() self.locked = dict() self.__class__.nviews += 1
def SigmaPull(data, postfit, show=True): pull = data.Clone("pull") pull.Add(postfit, -1) TH1.StatOverflows(1) addedsqrt = 0 mean = 0 sigma = 0 chi2 = 0 for ibin in range(1, pull.GetNbinsX() + 1): if postfit[ibin] <= 0: continue addedsqrt += (pull.GetBinContent(ibin)**2) / (postfit.GetBinError(ibin) **2) sigma = TMath.Sqrt( postfit.GetBinError(ibin)**2 + data.GetBinError(ibin)**2) pull.SetBinContent(ibin, pull.GetBinContent(ibin) / sigma) pull.SetBinError(ibin, 0) mean += pull.GetBinContent(ibin) chi2 += pull.GetBinContent(ibin)**2 if show: print "MEAN: ", mean / pull.GetNbinsX() print "CHI2: ", TMath.Sqrt(chi2) / pull.GetNbinsX() print "Added", TMath.Sqrt( addedsqrt), "divided: ", TMath.Sqrt(addedsqrt) / pull.GetNbinsX() print "Added2", addedsqrt, "divided: ", addedsqrt / pull.GetNbinsX() return pull
def __init__(self): TH1.SetDefaultSumw2() self.histJetPT = TH1F("jet_pt", "jet p_{T}", 25, 0, 500) self.histJet0PT = TH1F("jet0_pt", "Leading jet p_{T}", 25, 0, 500) self.histJet1PT = TH1F("jet1_pt", "Sub-leading jet p_{T}", 25, 0, 500) self.histNJets = TH1F("njets", "Number of Jets", 15, -0.5, 14.5) self.histMET = TH1F("met", "Missing E_{T}", 25, 0, 500) #self.histLepPT = TH1F("lep_pt", "lepton p_{T}", 25, 0, 500) self.histElePT = TH1F("ele_pt", "Electron p_{T}", 25, 0, 500) self.histMuPT = TH1F("mu_pt", "Muon p_{T}", 25, 0, 500) # Parton self.histTopPT = TH1F("t_pt_mc", "Top p_{T} (truth)", 25, 0, 500) self.histTopY = TH1F("t_y_mc", "Top rapidity (truth)", 20, -5, 5) self.histTbarPT = TH1F("tbar_pt_mc", "Anti-top p_{T} (truth)", 25, 0, 500) self.histTbarY = TH1F("tbar_y_mc", "Anti-top rapidity (truth)", 20, -5, 5) #self.histTopHPT = TH1F("th_pt_mc", "Hadronic top p_{T} (truth)", 25, 0, 500) #self.histTopHY = TH1F("th_y_mc", "Hadronic top rapidity (truth)", 20, -5, 5) #self.histTopLPT = TH1F("tl_pt_mc", "Leptonic top p_{T} (truth)", 25, 0, 500) #self.histTopLY = TH1F("tl_y_mc", "Leptonic top rapidity (truth)", 20, -5, 5) self.histTTbarM = TH1F("ttbar_m_mc", "TTbar mass (truth)", 25, 0, 1500) self.histTTbarPT = TH1F("ttbar_pt_mc", "TTbar p_{T} (truth)", 25, 0, 500) self.histTTbarDPHI = TH1F("ttbar_dphi_mc", "TTbar dPhi (truth)", 20, 0, 3.15) # decay mode self.histTTbarMode = TH2F("ttbar_decaymode", "Decay Mode", 2, -0.5, 1.5, 2, -0.5, 1.5) self.histTTbarMode.GetXaxis().SetTitle("hadronic top") self.histTTbarMode.GetYaxis().SetTitle("hadronic anti-top")
def getHistoFromFile(infilename,inhistoname,outhistoname,outhistorebin=None, addOverflow=True): TH1.SetDefaultSumw2() rfile = TFile.Open(infilename, 'r') if not rfile: raise RuntimeError('file {} could not be opened, please check'.format(infilename)) htemp = rfile.Get(inhistoname) if htemp==None: print 'Did not find histo {} in file {}'.format(inhistoname,infilename) return -1,None histo = htemp.Clone(outhistoname) if outhistorebin!=None: histo.Rebin(outhistorebin) if addOverflow: hret = getOverflowedHisto(histo) else: hret = histo hret.SetDirectory(0) # stupid ROOT memory handling... return 0,hret
def makeHistoFromNtuple(infilename, intreename, outhistoname, outhistobinning, outhistoquantity, outhistoweight, outhistoselection="(1)", outhistosmooth=False, addOverflow=True): TH1.SetDefaultSumw2() histo = TH1F(outhistoname, outhistoname, *(outhistobinning)) chain = TChain(intreename) chain.Add(infilename) ret = chain.Project(histo.GetName(), outhistoquantity, '(' + outhistoselection + ')*(' + outhistoweight + ')') if outhistosmooth: histo.Scale(1, 'width') if addOverflow: hret = getOverflowedHisto(histo) else: hret = histo return ret, hret
def AddRatio(can, hist, ref_hist, divide=''): from ROOT import TH1 TH1.SetDefaultSumw2(True) ratioplot = hist.Clone() ratioplot.SetName(hist.GetName() + '_ratio') ratioplot.Divide(hist, ref_hist, 1., 1., divide) AddHistogram(can.GetPrimitive('pad_top'), hist) AddHistogram(can.GetPrimitive('pad_bot'), ratioplot) return
def scale_factors(forward, central, output): TH1.AddDirectory(ROOT.kFALSE); #rootfile = TFile(ROOTFile,"READ") #forward = rootfile.Get(forward_name).Clone() #central = rootfile.Get(central_name).Clone() #print "loaded", forward_name, "and", central_name central_norm = central.GetSumOfWeights() forward_norm = forward.GetSumOfWeights() #for i in xrange(1,forward.GetNcells()-1): # forward.SetBinError(i,forward.GetBinError(i)/forward.GetBinContent(i)) # central.SetBinError(i,central.GetBinError(i)/central.GetBinContent(i)) #central.Scale(1/central_norm) #forward.Scale(1/forward_norm) #for i in xrange(1,forward.GetNcells()-1): # print 'bin',i,'content',central.GetBinContent(i),'\pm',central.GetBinError(i) #central = central.Rebin(4) #forward = forward.Rebin(4) can = ROOT.TCanvas() #fit_scale = get_scale(central, forward, central_norm/forward_norm,700) #forward.Scale(fit_scale) correction_hist = forward.Clone('corrections') for mc_bin in xrange(1,forward.GetNcells()-1): a = forward.GetBinContent(mc_bin) b = central.GetBinContent(mc_bin) if b ==0. or a==0.: correction_hist.SetBinContent(mc_bin,-1) correction_hist.SetBinError(mc_bin,0) continue b_err = central.GetBinError(mc_bin) a_err = forward.GetBinError(mc_bin) factor = a/b error = ((1/b*a_err)**2 + (a/(b*b) * b_err)**2) # print 'b',b,'+-',b_err # print 'a',a,'+-',a_err # print 'bin',mc_bin,'factor',factor,'\pm',math.sqrt(error) correction_hist.SetBinContent(mc_bin,factor) correction_hist.SetBinError(mc_bin,math.sqrt(error)) correction_hist.Scale((correction_hist.GetNcells()-2)/correction_hist.GetSumOfWeights()) correction_hist.SetAxisRange(0.5, 4,"Y"); correction_hist.Draw(); #can.SaveAs(output) return correction_hist
def makeHistosFromFriends(infilename1, infilename2, intreename1, intreename2, outhistoname, outhistobinning, outhistoquantity, outhistoweight, outhistoselection="(1)", outhistosmooth=False, index='evt', friendname='new'): TH1.SetDefaultSumw2() if os.path.isfile(infilename1) == False or os.path.isfile( infilename2) == False: raise RuntimeError('wrong files') fref = TFile(infilename1, 'READ') fnew = TFile(infilename2, 'READ') tref = fref.Get(intreename1) tnew = fnew.Get(intreename2) # FIXME: find a way to index on the same variable #tref.BuildIndex(index) #tnew.BuildIndex(index) tref.AddFriend(tnew, friendname) htemp = TH1F('htemp', 'htemp', *(outhistobinning)) htemp2 = TH1F('htemp2', 'htemp2', *(outhistobinning)) print str(outhistobinning) #ret1=tref.Draw('%s>>%s%s' % (outhistoquantity, outhistoname,outhistobinning), outhistoselection, 'goff') ret1 = tref.Draw('%s>>htemp' % (outhistoquantity), outhistoselection, 'goff') href = tref.GetHistogram().Clone('htemp') href.SetDirectory(0) href.SetTitle(href.GetName()) #ret2=tref.Draw('%s.%s>>%s%s' % (friendname, outhistoquantity, outhistoname, outhistobinning), outhistoselection, 'goff') #ret2=tref.Draw('%s.%s>>%s%s' % (friendname, outhistoquantity, outhistoname, outhistobinning), outhistoselection, 'goff') ret2 = tref.Draw('%s.%s>>htemp2' % (friendname, outhistoquantity), outhistoselection, 'goff') #hnew = tref.GetHistogram().Clone('%s_new' % outhistoname) hnew = tref.GetHistogram().Clone('htemp2') hnew.SetDirectory(0) hnew.SetTitle(hnew.GetName() + '_') print ret1, ret2, href.GetEntries(), hnew.GetEntries() return ret1, ret2, href, hnew
def ReadHistogram(fitdir, region, process): #hist_ = [] ## construct histogram name histname = fitdir + '/' + region + '/' + process TH1.AddDirectory(0) ## get histo from the rootfile hist_ = fin.Get(histname) TH1.AddDirectory(0) ## This will take into account of underfow and overflow ## "width" will multiply the bin-content with bin-width. This is to get the correct integral for bin-width normalised histogram. integral_ = 0.0 if type(hist_) is TObject: integral_ = 0.0 elif type(hist_) is TH1F: integral_ = hist_.Integral(0, -1, "width") print 'histname = ', histname, 'integral =', integral_ return integral_
def calculate_pull(hist: TH1, ff: TF1) -> tuple: """ Calculate a pull plot between a histogram and a fitted function. Parameters ---------- hist : TH1 ROOT TH1 histogram ff : TF1 ROOT TF1 function used to fit. Returns ------- x, y : np.array, np.array points for pull plot """ x, y = [], [] for n in range(1, hist.GetNbinsX() + 1): x.append(hist.GetBinCenter(n)) if not hist.GetBinError(n): y.append(0) continue y.append((hist.GetBinContent(n) - ff.Eval(hist.GetBinCenter(n))) / ( hist.GetBinError(n))) return x, y
def fixXExponent(self, canvas): ''' If there's an exponent on the Y axis, it will either be in a weird place or it will overlap with the axis title. We fix the placement in __init__(), but we still have to move the title if need be. Recursive, so we find histograms in pads in pads. ''' for obj in canvas.GetListOfPrimitives(): if obj.InheritsFrom(TH1.Class()) or obj.InheritsFrom( THStack.Class()): axis = obj.GetXaxis() if axis.GetXmax() >= 10**TGaxis.GetMaxDigits(): # has exponent axis.CenterTitle() if obj.InheritsFrom(TPad.Class()): self.fixXExponent(obj)
def main(extension, network_name, luminosity, output_flag, output_mod, veto_all_jets, generator, extra_text, fudge, text, Independant_Variable_List, Variable_list, WP_list, MC_list, Real_Data): gROOT.SetStyle("ATLAS") gROOT.ForceStyle() gStyle.SetErrorX(0.5) TH1.SetDefaultSumw2() OUTPUT_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Output") ## Changing the name of the input file to match the generator if generator == "PowhegPythia": pass elif generator == "Sherpa": MC_list[0].file = "S" + MC_list[0].file elif generator == "MadGraph": MC_list[0].file = "MG" + MC_list[0].file else: print("\n\n\nUnknown Generator option\n\n\n") return 0 ## Changing the name of the input files to match the jet selection if veto_all_jets: for mc in MC_list: mc.file = mc.file + "_NOJETS" Real_Data.file = Real_Data.file + "_NOJETS" extra_text += "0 Jets" ########## Plotting the independant variables ########## for ivar in Independant_Variable_List: STACK(extension, OUTPUT_dir, network_name, luminosity, output_flag, output_mod, generator, extra_text, fudge, text, MC_list, Real_Data, ivar, None) ########## Loading and Plotting the TH1Ds ########## for var in Variable_list: for wp in WP_list: STACK(extension, OUTPUT_dir, network_name, luminosity, output_flag, output_mod, generator, extra_text, fudge, text, MC_list, Real_Data, var, wp) return 0
def main(argv=None): if argv is None: argv = sys.argv[1:] args = arg_parser().parse_args(argv) from ROOT import TChain, gROOT, gSystem, TH1 gSystem.SetBuildDir(".rootbuild") gROOT.SetBatch(True) TH1.AddDirectory(False) chain = TChain("particles") for fname in args.file: chain.Add(fname) load_ok = gROOT.LoadMacro("RunFemto.C+") assert load_ok >= 0, load_ok from ROOT import RunFemto RunFemto(chain, args.output, args.limit)
def invert_col(self, pad): bgcol = rt.kBlack fgcol = rt.kOrange - 3 pad.SetFillColor(bgcol) pad.SetFrameLineColor(fgcol) next = TIter(pad.GetListOfPrimitives()) obj = next() while obj != None: #TText if obj.InheritsFrom(TText.Class()) == True: if obj.GetTextColor() == rt.kBlack: obj.SetTextColor(fgcol) #H1 if obj.InheritsFrom(TH1.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) if obj.GetMarkerColor() == rt.kBlack: obj.SetMarkerColor(fgcol) obj.SetAxisColor(fgcol, "X") obj.SetAxisColor(fgcol, "Y") obj.SetLabelColor(fgcol, "X") obj.SetLabelColor(fgcol, "Y") obj.GetXaxis().SetTitleColor(fgcol) obj.GetYaxis().SetTitleColor(fgcol) #TFrame if obj.InheritsFrom(TFrame.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) #print obj.GetName(), obj.ClassName() #move to the next item obj = next()
def printDirectory(directory, prefix, args): for key in directory.GetListOfKeys(): classname = key.GetClassName() cl = gROOT.GetClass(classname) if (not cl): continue if (cl.InheritsFrom(TDirectory.Class())): print prefix, key.GetName() printDirectory(key.ReadObj(), prefix + " ", args) elif (cl.InheritsFrom(TH1.Class())): hist = key.ReadObj() if args.systematics or hist.GetName( ) == "nominal" or hist.GetTitle() == "nominal": if args.full: print prefix, hist.GetName(), hist.GetSumOfWeights() for i in range(0, hist.GetNbinsX() + 2): print prefix, " ", hist.GetBinContent( i), " +/- ", hist.GetBinError(i) else: print prefix, key.GetName(), hist.GetSumOfWeights() else: print "Unknown", key.GetName()
def plotHistograms(histname, filenames, labels, canvas, legend, normalize=False): assert (len(filenames) == len(labels)) TH1.SetDefaultSumw2() # get histograms from the files histograms = [] for fname in filenames: f = TFile.Open(fname, "READ") h = f.Get(histname) h.SetDirectory(0) histograms.append(h) f.Close() # plot canvas.cd() hs = THStack() hs.Draw() norm = 1. for i, (hist, label) in enumerate(zip(histograms, labels)): hist.SetStats(0) hist.SetLineColor(i + 1) hist.SetMarkerColor(i + 1) # normalize to the first histogram if needed if normalize: if i == 0: norm = hist.Integral() else: norm_i = hist.Integral() hist.Scale(norm / norm_i) hs.Add(hist) legend.AddEntry(hist, label) return hs
def __init__(self): # ROOT gROOT.LoadMacro("tdrstyle.C") gROOT.LoadMacro("../interface/HelperMath.h") gROOT.LoadMacro("../../AMSimulationIO/src/AMSimulationIOLinkDef.h") gROOT.ProcessLine("setTDRStyle()") gStyle.SetEndErrorSize(2) gStyle.SetPadRightMargin(0.05) gStyle.SetTitleOffset(1.1, "Y") gStyle.SetLabelSize(0.04, "Y") gStyle.SetLabelSize(0.04, "Z") gStyle.SetNdivisions(505, "XY") gStyle.SetPalette(55) # rainbow color map gStyle.SetNumberContours(100) gStyle.SetOptStat(111110) gStyle.SetStatX(0.94) gStyle.SetStatY(0.93) gStyle.SetStatH(0.30) gStyle.SetStatW(0.28) TH1.SetDefaultSumw2()
elif (ZTree): treeDATA = inputDATAtree.Get("ZTree/candTree") treeMCDY = inputMCDYtree.Get("ZTree/candTree") treeMCTTbar = inputMCTTbartree.Get("ZTree/candTree") treeText = "ZTree" else: print("Error: wrong option!") else: print("Error: choose a period!") # ******************** # do DATA histos # ******************** if (redoDATAHistos): TH1.SetDefaultSumw2( ) # set sumw2 = true for all the histograms created from now on # ****************************** # Define data histograms Z->ee # ****************************** if (period == "data2017"): Z_ele_1st_LepBDT_hist = TH1F('Z_ele_1st_LepBDT_hist', 'LepBDT leading ele', 100, 0.75, 1.25) Z_ele_1st_LepBDT_hist_EB = TH1F('Z_ele_1st_LepBDT_hist_EB', 'LepBDT leading ele in ECAL Barrel', 100, 0.75, 1.25) Z_ele_1st_LepBDT_hist_EE = TH1F('Z_ele_1st_LepBDT_hist_EE', 'LepBDT leading ele in ECAL Endcap', 100, 0.75, 1.25) elif (period == "data2016" or period == "data2018"): Z_ele_1st_LepBDT_hist = TH1F('Z_ele_1st_LepBDT_hist',
* Redistribution and use in source and binary forms, with or without * * modification, are permitted according to the terms listed in the file * * LICENSE. * ********************************************************************************** """ import ROOT from ROOT import TFile, TMath, RooRandom, TH1, TH1F from ROOT import kBlack, kWhite, kGray, kRed, kPink, kMagenta, kViolet, kBlue, kAzure, kCyan, kTeal, kGreen, kSpring, kYellow, kOrange, kDashed, kSolid, kDotted from math import fabs from logger import Logger from systematic import SystematicBase log = Logger('Sample') TH1.SetDefaultSumw2(True) from copy import deepcopy from configManager import configMgr class Sample(object): """ Defines a Sample belonging to a Channel """ def __init__(self, name, color=1): """ Store configuration, set sample name, and if to normalize by theory Scales histograms to luminosity set in configuration
from array import array import math from ROOT import gPad, TCanvas, TH2D, TH1D, TFile, THStack, TLegend, gSystem, TLatex, TH1, gStyle, TGaxis import os, sys import CMS_lumi #import tdrstyle #TGaxis.SetMaxDigits(2) TH1.SetDefaultSumw2() gStyle.SetPaintTextFormat(".5f") def setBotStyle(h, r=4, fixRange=True): h.GetXaxis().SetLabelSize(h.GetXaxis().GetLabelSize() * (r - 1)) h.GetXaxis().SetLabelOffset(h.GetXaxis().GetLabelOffset() * (r - 1)) h.GetXaxis().SetTitleSize(h.GetXaxis().GetTitleSize() * (r - 1)) h.GetYaxis().SetLabelSize(h.GetYaxis().GetLabelSize() * (r - 1)) h.GetYaxis().SetNdivisions(505) h.GetYaxis().SetTitleSize(h.GetYaxis().GetTitleSize() * (r - 1)) h.GetYaxis().SetTitleOffset(h.GetYaxis().GetTitleOffset() / (r - 1)) if fixRange: h.GetYaxis().SetRangeUser(0., 2.) for i in range(1, h.GetNbinsX() + 1): if h.GetBinContent(i) < 1.e-6: h.SetBinContent(i, -1.e-6) pass def drawCMS(lumi, text, onTop=False):
from ROOT import TFile, TH1 import collections TH1.AddDirectory(0) for year in ['2016', '2017', '2018']: print year ############################### # Calculate top QCDscale norm # ############################### samples = collections.OrderedDict() handle = open('../Full%s_v7/njets/NormFact/samples.py' % (year), 'r') exec(handle) handle.close() f0 = TFile( "../Full{year}_v7/njets/NormFact/rootFile/plots_WW{year}_v7_njets_NormFact.root" .format(year=year)) print "QCDscale_top" nnom = [0] * 4 nup = [0] * 4 ndown = [0] * 4 for i in xrange(4): hnom = f0.Get("ww2l2v_13TeV_B%d/events/histo_top" % i) hup = f0.Get("ww2l2v_13TeV_B%d/events/histo_top_QCDscale_topUp" % i) hdown = f0.Get("ww2l2v_13TeV_B%d/events/histo_top_QCDscale_topDown" %
#!/usr/bin/env python from ROOT import gRandom, TH1, TH1D, gROOT, cout, TMath, gStyle gROOT.LoadMacro("$HOME/Downloads/RooUnfold/libRooUnfold.so") from ROOT import RooUnfoldResponse from ROOT import RooUnfoldBayes from ROOT import RooUnfoldSvd from ROOT import RooUnfoldTUnfold from ROOT import RooUnfoldInvert from ROOT import RooUnfoldBasisSplines from ROOT import TProfile, TCanvas, TF1, TMatrixD, TH2D, TVectorD TH1.SetDefaultSumw2(False) # Gaussian smearing, systematic translation, and variable inefficiency: class Measurement: def __init__(self, mean, sigma, leff): self.mean = mean self.sigma = sigma self.leff = leff return def getMean(self): return self.mean def getSigma(self):
def AddPIDCalibWeights( infname, intreename, outfname, magtype, year='2016', perfhistpath='/disk/lhcb_data2/amathad/Lb2Lclnu_analysis/perfhist/RLc', nentries_to_read=1000000000, chunksize=10000): """ Function: --------- Adds PIDCalib weights for following cuts Proton: [DLLp>0] Kaon : [DLLK>4] Pion : [DLLK<2] Muon : [DLLmu>2&&DLLmu-DLLK>2&&DLLmu-DLLp>2&&IsMuon==1&&MC15TuneV1_ProbNNghost<0.2] (NB: This is an 'offline' cut on Muon not Stripping cut). Arguments: --------- infname: full path to input file tfile including it's name intreename: ttree name in the input file outfname: full path to the output file including it's name where it will be written magtype : MagDown or MagUp year : 2016 perfhistpath: path where the perfomance histogram are stored (these are created using custom binning scheme). Default is set to '/disk/lhcb_data2/amathad/Lb2Lclnu_analysis/perfhist/RLc'. nentries_to_read: number of candidates to read. Set to root default i.e. 1000000000 chunksize: Pandas data frame chunksize to read Output: ------ Stores a root file with the same TTree name as the input. The file contains eventNumber, runNumber and PIDCalib weights (Event_PIDCalibEffWeight). """ TH1.AddDirectory(kFALSE) yr = "Turbo" + year[-2:] varsdf = ['runNumber', 'eventNumber', 'nTracks'] perfHist = {} for trck_PIDCalib in trcks_PIDCalib: if trck_PIDCalib == 'K': varsdf += [trck_PIDCalib + "_P", trck_PIDCalib + "_PT"] else: varsdf += [ trck_PIDCalib.lower() + "_P", trck_PIDCalib.lower() + "_PT" ] prefix = perfhistpath + "/PerfHists_" + trck_PIDCalib + "_" + yr + "_" + magtype binningname = "binning-" + trck_PIDCalib + "-" + yr + "-" + magtype suffix = "_".join(leafs_PIDCalib) perfname = prefix + "_" + binningname + "_" + suffix + ".root" File = TFile.Open(perfname, "read") Histg = File.Get(perfhistname[trck_PIDCalib]) perfHist[trck_PIDCalib] = Histg.Clone(trck_PIDCalib + "new") File.Close() varstoStore = ['runNumber', 'eventNumber', 'Event_PIDCalibEffWeight'] if os.path.exists(outfname): os.remove(outfname) if nentries_to_read <= chunksize: chunksize = nentries_to_read events_read = 0 for df_data in read_root(infname, intreename, chunksize=chunksize, columns=varsdf): print('Events read', events_read) if events_read >= nentries_to_read: break for trck_PIDCalib in trcks_PIDCalib: Xmin = perfHist[trck_PIDCalib].GetXaxis().GetXmin() Xmax = perfHist[trck_PIDCalib].GetXaxis().GetXmax() Ymin = perfHist[trck_PIDCalib].GetYaxis().GetXmin() Ymax = perfHist[trck_PIDCalib].GetYaxis().GetXmax() Zmin = perfHist[trck_PIDCalib].GetZaxis().GetXmin() Zmax = perfHist[trck_PIDCalib].GetZaxis().GetXmax() Limits = (Xmin, Xmax, Ymin, Ymax, Zmin, Zmax) if trck_PIDCalib == 'K': applyvars = [ trck_PIDCalib + '_P', trck_PIDCalib + '_PT', 'nTracks' ] df_data[trck_PIDCalib + '_PIDCalibeff'] = df_data[applyvars].apply( storeeff, args=[perfHist[trck_PIDCalib], Limits], axis=1) else: applyvars = [ trck_PIDCalib.lower() + '_P', trck_PIDCalib.lower() + '_PT', 'nTracks' ] df_data[trck_PIDCalib.lower() + '_PIDCalibeff'] = df_data[applyvars].apply( storeeff, args=[perfHist[trck_PIDCalib], Limits], axis=1) df_data['Event_PIDCalibEffWeight'] = df_data[ 'K_PIDCalibeff'] * df_data['p_PIDCalibeff'] * df_data[ 'pi_PIDCalibeff'] * df_data['mu_PIDCalibeff'] df_data[varstoStore].to_root(outfname, key=intreename, mode='a', store_index=False) events_read += df_data.shape[0]
def invert_col(pad, fgcol=rt.kOrange - 3, bgcol=rt.kBlack): #set foreground and background color #fgcol = rt.kAzure #fgcol = rt.kGreen #fgcol = rt.kOrange-3 pad.SetFillColor(bgcol) pad.SetFrameLineColor(fgcol) next = TIter(pad.GetListOfPrimitives()) obj = next() while obj != None: #H1 if obj.InheritsFrom(TH1.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) if obj.GetMarkerColor() == rt.kBlack: obj.SetMarkerColor(fgcol) obj.SetAxisColor(fgcol, "X") obj.SetAxisColor(fgcol, "Y") obj.SetLabelColor(fgcol, "X") obj.SetLabelColor(fgcol, "Y") obj.GetXaxis().SetTitleColor(fgcol) obj.GetYaxis().SetTitleColor(fgcol) #Legend if obj.InheritsFrom(TLegend.Class()) == True: obj.SetTextColor(fgcol) #obj.SetFillStyle(1000) #obj.SetFillColor(fgcol) #obj.SetTextColor(bgcol) #ln = TIter(obj.GetListOfPrimitives()) #lo = ln.Next() #while lo != None: #if lo.GetObject() == None: #lo = ln.Next() #continue #if lo.GetObject().InheritsFrom(TH1.Class()) == True: #hx = lo.GetObject() #hx.SetFillColor(bgcol) #if hx.GetMarkerColor() == rt.kBlack: #hx.SetMarkerColor(fgcol) #hx.SetLineColor(fgcol) #pass #lo = ln.Next() #RooHist if obj.InheritsFrom(RooHist.Class()) == True: if obj.GetMarkerColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetMarkerColor(fgcol) #H2 if obj.InheritsFrom(TH2.Class()) == True: obj.SetAxisColor(fgcol, "Z") obj.SetLabelColor(fgcol, "Z") obj.GetZaxis().SetTitleColor(fgcol) #obj.SetLineColor(fgcol) #obj.SetMarkerColor(fgcol) #TLatex if obj.InheritsFrom(TLatex.Class()) == True: if obj.GetTextColor() == rt.kBlack: obj.SetTextColor(fgcol) #F2 if obj.InheritsFrom(TF2.Class()) == True: axes = [obj.GetXaxis(), obj.GetYaxis(), obj.GetZaxis()] for i in range(len(axes)): axes[i].SetAxisColor(fgcol) axes[i].SetLabelColor(fgcol) axes[i].SetTitleColor(fgcol) #F1 if obj.InheritsFrom(TF1.Class()) == True: axes = [obj.GetXaxis(), obj.GetYaxis()] for i in range(len(axes)): axes[i].SetAxisColor(fgcol) axes[i].SetLabelColor(fgcol) axes[i].SetTitleColor(fgcol) #TGraph if obj.InheritsFrom(TGraph.Class()) == True: obj.SetFillColor(bgcol) ax = obj.GetXaxis() ay = obj.GetYaxis() ax.SetAxisColor(fgcol) ay.SetAxisColor(fgcol) ax.SetLabelColor(fgcol) ay.SetLabelColor(fgcol) ax.SetTitleColor(fgcol) ay.SetTitleColor(fgcol) if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetMarkerColor(fgcol) #TGaxis if obj.InheritsFrom(TGaxis.Class()) == True: obj.SetLineColor(fgcol) obj.SetLabelColor(fgcol) obj.SetTitleColor(fgcol) #TFrame if obj.InheritsFrom(TFrame.Class()) == True: if obj.GetLineColor() == rt.kBlack: obj.SetLineColor(fgcol) obj.SetFillColor(bgcol) #move to next item obj = next()
__version__ = "$Id" try: import numpy as np import ROOT from ROOT import gDirectory, TChain, TCanvas, TFile, TProfile, TNtuple from ROOT import TH1, TH1D, TH2F, TF1, TH1F, TGraph, TGraphErrors, TLine from ROOT import gROOT, gStyle, gBenchmark, gRandom, gSystem, gDirectory from ROOT import gPad, TText, TLatex, TMarker, TColor, TNamed except ImportError as e: print e raise ImportError ROOT.gROOT.SetBatch(True) TH1.AddDirectory(False) def objExists(infile, objname, subdir='', verbose=False): """ Test if object exists in input file objExists(input file, object name, sub-directory='', verbose=False) """ # Try to Open File fin = ROOT.TFile(infile, "READ") file_open = fin.IsOpen() err_mess = "\n***Can not open ROOT file %s***\n" % (infile) assert (file_open), err_mess if (verbose): print "\n============= Opening %s\n" % infile
#!/usr/bin/python from ROOT import gSystem, gROOT, gStyle, TFile, TCanvas, TH1, TH1D import ROOT, sys sys.path.append('..') sys.path.append('../Polarization') from pyUtils import * from polarization import * TH1.SetDefaultSumw2( True ) #Cosi` in automatico per tutti gli isto dovrebbe abilitare il calcolo degli errori come si deve (come somma dei pesi) ptBins = [7, 11, 16, 20, 40] inFile_name = 'agreementDataMC_2S_plots.root' inFile = TFile(inFile_name, 'read') h_dimuon_pt = inFile.Get('dimuon_pt_signal') for ptBin in range(1, len(ptBins)): binMin = h_dimuon_pt.FindBin(ptBins[ptBin - 1]) binMax = h_dimuon_pt.FindBin(ptBins[ptBin]) - 1 num = 0 den = 0 for i in range(binMin, binMax + 1): num += h_dimuon_pt.GetBinCenter(i) * h_dimuon_pt.GetBinContent(i) den += h_dimuon_pt.GetBinContent(i) print num / den
trivialConst = [1.0] * maxDim * maxDim * maxDepth trivialConst[0] = 1.0 if len(opts.initCalib) > 0: trivialConst = loadCalibration(opts.initCalib) trivialConstHO = [1.] * maxDim * maxDim if len(opts.initCalibHO) > 0: trivialConstHO = loadCalibration(opts.initCalibHO) HBdepths = 2 if (opts.depths): HBdepths = maxDepth beamE = opts.beamE TH1.SetDefaultBufferSize(10000) prelim = TH1F('prelimE', 'prelim energy', 100, 0., -1.) passed = 0 radius = 1 resolution = beamE * sqrt(1 / beamE + 0.01) ieta = opts.eta iphi = opts.phi ecalXtalieta = ieta * 5 - 2 ecalXtaliphi = ebMaxPhi - (iphi * 5 - 2) (ie, ip, xie, xip) = findBeamCaloCoords(dataTree) print 'assumed resolution: {0:0.2f}'.format(resolution)
def main(network_name, do_weights, general_weights, electron_weights, muon_weights, data_files, no_true_comp, nbins, veto_all_jets, require_jets, WorkingP_List, Independant_Variable_List, Histogram_Variable_List, Profile_List, TH2D_List): TH1.SetDefaultSumw2() ## First we check that you havent vetoed and requested jets if veto_all_jets and require_jets: print("\n\n\nYOU CANT ASK FOR NONE YET SOME JETS\n\n\n") return 0 for data_dir, data_set_name in data_files: OUTPUT_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Output") DATA_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Data") data_base_dir = os.path.join(DATA_dir, data_dir) print("\n\n" + data_set_name) """ ____ ____ _____ ____ _ ____ _ _____ ___ ___ _ _ | _ \ | _ \ | ____| | _ \ / \ | _ \ / \ |_ _| |_ _| / _ \ | \ | | | |_) | | |_) | | _| | |_) | / _ \ | |_) | / _ \ | | | | | | | | | \| | | __/ | _ < | |___ | __/ / ___ \ | _ < / ___ \ | | | | | |_| | | |\ | |_| |_| \_\ |_____| |_| /_/ \_\ |_| \_\ /_/ \_\ |_| |___| \___/ |_| \_| """ ## The list of matrices Indp_output = [None for var in Independant_Variable_List] TH1D_output = [[None for wp in WorkingP_List] for var in Histogram_Variable_List] Tail_output = [[None for wp in WorkingP_List] for var in Histogram_Variable_List] Prof_output = [[None for wp in WorkingP_List] for var in Profile_List] TH2D_output = [[None for wp in WorkingP_List] for var in TH2D_List] ## Open the root file root_file = TFile.Open(os.path.join(data_base_dir, data_set_name)) ## Loading the tree containing the working point information wpt_tree = root_file.Get("wpt_tree") ## Making the wpt_tree and the other trees friends so that they can be compared wpt_tree.AddFriend(network_name, root_file) wpt_tree.AddFriend("alt_tree", root_file) wpt_tree.AddFriend("var_tree", root_file) ## Creating a string of all the weights to be applied event_weights = [] if "SR" in data_dir: event_weights += general_weights if "ee" in data_dir: event_weights += electron_weights if "mumu" in data_dir: event_weights += muon_weights if veto_all_jets: event_weights += ["(var_tree.Jets_Loose_SumET==0)"] if require_jets: event_weights += ["(var_tree.Jets_Loose_SumET>0)"] if do_weights: weight_string = " * ".join(event_weights) else: weight_string = "" if len(weight_string): print("Events are weighted using: {}".format(weight_string)) ## The strings to call up the Truth Values if no_true_comp: True_Et = "0" True_Ex = "0" True_Ey = "0" True_Phi = "0" else: True_Et = "WP_Truth_ET" True_Ex = "WP_Truth_X" True_Ey = "WP_Truth_Y" True_Phi = "WP_Truth_Phi" """ ____ ____ _ __ __ ___ _ _ ____ | _ \ | _ \ / \ \ \ / / |_ _| | \ | | / ___| | | | | | |_) | / _ \ \ \ /\ / / | | | \| | | | _ | |_| | | _ < / ___ \ \ V V / | | | |\ | | |_| | |____/ |_| \_\ /_/ \_\ \_/\_/ |___| |_| \_| \____| """ ## Before the workingpoint loop, the independant variables are drawn for v, var in enumerate(Independant_Variable_List): print(" -- {}".format(var.name)) ## Creating the histogram which will be filled hist_name = var.name myhist = TH1D(hist_name, hist_name, var.nbins, var.xmin, var.xmax) myhist.SetStats(True) myhist.StatOverflows(True) ## Get Maths Function from variable maths_string = var.tree + "." + var.branch ## Drawing the tree and saving the hist to the matrix execution = "{}>>{}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) Indp_output[v] = myhist ## First we select the working point and create the correct strings for w, wp in enumerate(WorkingP_List): print(" -- {}:".format(wp.name)) Rec_Et = wp.Et Rec_Ex = wp.Ex Rec_Ey = wp.Ey Rec_Phi = wp.Phi if wp.tree == "ann_tree": Rec_Et = network_name + "." + Rec_Et Rec_Ex = network_name + "." + Rec_Ex Rec_Ey = network_name + "." + Rec_Ey rec_and_truth_vars = [ Rec_Et, Rec_Ex, Rec_Ey, Rec_Phi, True_Et, True_Ex, True_Ey, True_Phi ] ## Drawing the 1D histograms for v, var in enumerate(Histogram_Variable_List): print(" -- -- {}".format(var.name)) ## Creating the histogram which will be filled hist_name = "{}_{}".format(var.name, wp.name) myhist = TH1D(hist_name, hist_name, var.nbins, var.xmin, var.xmax) myhist.SetStats(True) myhist.StatOverflows(True) ## Individual special plots if var.name == "XY": ## Plot the X histogram maths_string = Evaluation.TH1D_Maths_String( "X", *rec_and_truth_vars) execution = "{} >> {}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Add the y histogram maths_string = Evaluation.TH1D_Maths_String( "Y", *rec_and_truth_vars) execution = "{} >> +{}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") else: ## Get Maths Function from variable maths_string = Evaluation.TH1D_Maths_String( var.name, *rec_and_truth_vars) ## Drawing the tree and saving the hist to the matrix execution = "{} >> {}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) TH1D_output[v][w] = myhist ## Drawing the Profiles for v, (vx, vy) in enumerate(Profile_List): print(" -- -- {} vs {}".format(vx.name, vy.name)) ## Creating the profile which will be filled hist_name = "{}_vs_{}_{}".format(vx.name, vy.name, wp.name) myhist = TProfile(hist_name, hist_name, vx.nbins, vx.xmin, vx.xmax) if vy.reso: myhist.SetErrorOption('s') ## The x variable is called from its branch in the correct tree x_string = vx.tree + "." + vx.branch ## Individual special plots if vy.name == "XY": y_string = Evaluation.TH1D_Maths_String( "X", *rec_and_truth_vars) execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") y_string = Evaluation.TH1D_Maths_String( "Y", *rec_and_truth_vars) execution = "{}:{} >>+{}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") elif vy.name == "AZ": z_x = "(alt_tree.ll_px)" z_y = "(alt_tree.ll_py)" z_pt = "(alt_tree.ll_pt)" x_string = z_pt y_string = "({rx}*{zx} + {ry}*{zy})/{zpt}".format( rx=Rec_Ex, ry=Rec_Ey, zx=z_x, zy=z_y, zpt=z_pt) execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") else: y_string = Evaluation.TH1D_Maths_String( vy.name, *rec_and_truth_vars) execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) Prof_output[v][w] = myhist if wp.name not in ["Network", "Tight"]: continue ## Drawing the TH2Ds for v, (vx, vy) in enumerate(TH2D_List): print(" -- -- {} vs {}".format(vx.name, vy.name)) ## Creating the profile which will be filled hist_name = "2D_{}_vs_{}_{}".format(vx.name, vy.name, wp.name) myhist = TH2D(hist_name, hist_name, vx.nbins2d, vx.xmin2d, vx.xmax2d, vy.nbins2d, vy.xmin2d, vy.xmax2d) x_string = Evaluation.TH1D_Maths_String( vx.name, *rec_and_truth_vars) y_string = Evaluation.TH1D_Maths_String( vy.name, *rec_and_truth_vars) ## The x variable is called from its branch in the correct tree if x_string is None: x_string = vx.tree + "." + vx.branch execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) TH2D_output[v][w] = myhist root_file.Close() """ _____ ____ ___ _____ ___ _ _ ____ | ____| | _ \ |_ _| |_ _| |_ _| | \ | | / ___| | _| | | | | | | | | | | | \| | | | _ | |___ | |_| | | | | | | | | |\ | | |_| | |_____| |____/ |___| |_| |___| |_| \_| \____| """ ## We now go through the 1D histograms and make them include overflow and underflow for v, var in enumerate(Histogram_Variable_List): for w, wp in enumerate(WorkingP_List): ## Include the overflow last_bin = TH1D_output[v][w].GetBinContent(nbins) overflow = TH1D_output[v][w].GetBinContent(nbins + 1) TH1D_output[v][w].SetBinContent(nbins, last_bin + overflow) TH1D_output[v][w].SetBinContent(nbins + 1, 0) ## Include the underflow first_bin = TH1D_output[v][w].GetBinContent(1) underflow = TH1D_output[v][w].GetBinContent(0) TH1D_output[v][w].SetBinContent(1, first_bin + underflow) TH1D_output[v][w].SetBinContent(0, 0) ## We create a tail distrobution if it is requested if var.tail: tail_temp = TH1D_output[v][w].GetCumulative(False) tail_temp.Scale(1 / tail_temp.GetBinContent(1)) Tail_output[v][w] = tail_temp ## We go through the resolution profiles and replace their entries with the RMSE for each bin for v, (vx, vy) in enumerate(Profile_List): if not vy.reso: continue for w, wp in enumerate(WorkingP_List): old_prof = Prof_output[v][w] bin_width = old_prof.GetBinWidth(1) name = "{}_vs_{}_{}_res".format(vx.name, vy.name, wp.name) new_prof = TGraphErrors(vx.nbins) new_prof.SetName(name) new_prof.SetTitle(name) new_prof.SetLineWidth(2) for b_idx in range(vx.nbins): new_prof.SetPoint(b_idx, old_prof.GetBinCenter(b_idx + 1), old_prof.GetBinError(b_idx + 1)) new_prof.SetPointError(b_idx, bin_width / 2, 0) Prof_output[v][w] = new_prof """ ____ _ __ __ ___ _ _ ____ / ___| / \ \ \ / / |_ _| | \ | | / ___| \___ \ / _ \ \ \ / / | | | \| | | | _ ___) | / ___ \ \ V / | | | |\ | | |_| | |____/ /_/ \_\ \_/ |___| |_| \_| \____| """ ## Creating the output directory output_dir = os.path.join(OUTPUT_dir, network_name, data_set_name[:-5]) if veto_all_jets: output_dir = output_dir + "_NOJETS" if require_jets: output_dir = output_dir + "_SOMEJETS" ## Check that the file can be saved if not os.path.exists(output_dir): os.system("mkdir -p " + output_dir) ## We create an output file for the histograms output_file = TFile(os.path.join(output_dir, "histograms.root"), "update") gFile = output_file ## We save the independants for v, var in enumerate(Independant_Variable_List): Indp_output[v].Write("", TObject.kOverwrite) ## We save the TH1Ds and tails for v, var in enumerate(Histogram_Variable_List): for w in range(len(WorkingP_List)): TH1D_output[v][w].Write("", TObject.kOverwrite) if var.tail: Tail_output[v][w].Write("", TObject.kOverwrite) ## We save the profiles for v in range(len(Profile_List)): for w in range(len(WorkingP_List)): Prof_output[v][w].Write("", TObject.kOverwrite) ## We save the TH2Ds for v in range(len(TH2D_List)): for w in range(len(WorkingP_List[:2])): TH2D_output[v][w].Write("", TObject.kOverwrite) output_file.Close() return 0