def storeHistogram(var, lumi, cut, histo, filename): # Plotting Data # ------------------ bp = BasicPlot(filename, var) bp.addHistogram(histo, 'E', copy=True) bp.titles.append( "#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}". format(lumi / 1000.)) bp.yVariable = Variable("Events") bp.normalizeByBinWidth = False bp.showBinWidthY = False bp.normalized = False bp.logY = False bp.yVariable.binning.low = 0 # start y-axis at 0 bp.draw() bp.saveAsAll(os.path.join(outFolder, bp.title)) # storing histogram in root file # ------------------------------ from ROOT import TFile, TObject rootfile = TFile.Open(outFolder + '/' + filename + '.root', 'update') if rootfile.IsOpen(): print "writing in file {}".format(outFolder + '/' + filename + '.root') rootfile.cd() histogram = histo.Clone('h_' + var.name) histogram.SetTitle('h_' + var.name) histogram.Write('h_' + var.name, TObject.kOverwrite) rootfile.Close() else: print "Problem opening file {}".format(outFolder + '/' + filename + '.root')
def PlotAndStore(var, filename, lumi, h_1, h_2=None, h_3=None, drawOneLine=False, simulation=False, yLabel="Fake Rate", yLow=0, yHigh=None): bp = BasicPlot(filename, var) # Add histos SetHistStyle(h_1, 0) bp.addHistogram(h_1, 'E', copy=True) if h_2: SetHistStyle(h_2, 1) bp.addHistogram(h_2, 'E', copy=True) if h_3: SetHistStyle(h_3, 2) bp.addHistogram(h_3, 'E', copy=True) # Draw a line at 1 e.g. for SF plots if drawOneLine: h_tmp = var.createHistogram('') for i_bin in range(h_tmp.GetNbinsX() + 2): h_tmp.SetBinContent(i_bin, 1) h_tmp.SetBinError(i_bin, 0) h_tmp.SetLineColor(kGray) bp.addHistogram(h_tmp, 'E') # Some further settings bp.titles.append( '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'. format(lumi / 1000.)) bp.legendDecorator.textSize = 0.045 if simulation: bp.titles.append('Simulation') bp.showBinWidthY = False bp.logY = False bp.normalized = False bp.normalizeByBinWidth = False bp.yVariable = Variable(yLabel) bp.yVariable.binning.low = yLow bp.yVariable.binning.up = yHigh # Plot and store bp.draw() bp.saveAsAll(os.path.join(outFolder, bp.title))
if __name__ == '__main__': from ROOT import TTree, TRandom3 from array import array from plotting.Variable import Variable, Binning from AtlasStyle import redLine, blueLine, greenLine # create some dummy tree rndm = TRandom3() size = array( 'f', [0] ) energy = array( 'f', [0] ) t = TTree( 'tree', 'My Tree' ) t.Branch( 'size', size, 'size/F' ) t.Branch( 'energy', energy, 'energy/F' ) for entry in xrange( 10000 ): size[0] = rndm.Poisson( 3 ) energy[0] = rndm.Landau( size[0]*2.5, size[0] ) t.Fill() # define a variable object for each branch sizeVar = Variable( 'size', title='Cluster Size', binning=Binning(10, 1, 11, range(1,11)) ) energyVar = Variable( 'energy', title='Hit Energy', unit='MeV', binning=Binning(50, 0, 100) ) p = BasicPlot( 'test', sizeVar, Variable( 'RMS(E)', unit = 'MeV' ) ) measure = HalfWidth() measure.selection = Truncate( 0.68 ) g = p.addGraph( resolutionGraph( t, sizeVar, energyVar, '', measure ) ) p.draw() g.Draw() raw_input( 'Continue?' )
cut=selection, weightExpression=weight_expression) sys.stdout.write('.') sys.stdout.flush() h_data.Add(h_tmp, -1) print "read all histograms for {0}".format(var.name) # calculating scale factor data/MC for the given Variable h_sf = var.createHistogram('SF') h_sf.Divide(h_data, h_mc, 1, 1, 'B') # plotting the scale factor bp_sf = BasicPlot('SF_' + var.name, var, Variable('Scale Factor')) bp_sf.titles.append( '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}, ' .format(lumi / 1000.)) bp_sf.addHistogram(h_sf, 'E', copy=True) bp_sf.draw() bp_sf.saveAsAll(os.path.join(outFolder, bp_sf.title)) # storing histogram in root file from ROOT import TFile, TObject filename = outFolder + '/SF_' + var.name + '.root' rootfile = TFile.Open(filename, 'update') if rootfile.IsOpen(): print "writing in file {}".format(filename) rootfile.cd() histogram = h_sf.Clone(var.name) histogram.SetTitle(var.name) histogram.Write(var.name, TObject.kOverwrite) rootfile.Close() else: print "Problem opening file {}".format(filename)
for i_set in range(n_sets): # data or mc plot ? if i_type == 2: # SF plots have a different naming convention bp = BasicPlot( 'FakeRate_'+var.name+name_type[i_type]+sel_p_suffix[p], var ) else: bp = BasicPlot( 'FakeRate_'+var.name+name_type[i_type]+sel_p_suffix[p]+name_set[i_set], var ) bp.showBinWidthY = False if name_type[i_type] == '_SF': # Draw a line at 1 for SF plots h_tmp = var.createHistogram( '' ) for i_bin in range( h_tmp.GetNbinsX() + 2 ): h_tmp.SetBinContent( i_bin, 1 ) h_tmp.SetBinError( i_bin, 0 ) h_tmp.SetLineColor( kGray ) bp.addHistogram( h_tmp, 'E' ) for i in range(id_range[i_type]): # Loop over different IDs histo_set[len(name_set)*i_type+i_set][i].SetLineColor( sel_colors[i] ) histo_set[len(name_set)*i_type+i_set][i].SetMarkerColor( sel_colors[i] ) histo_set[len(name_set)*i_type+i_set][i].SetMarkerStyle( sel_marker[i] ) bp.addHistogram( histo_set[len(name_set)*i_type+i_set][i], 'E', copy=True ) bp.titles.append( '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'.format(lumi/1000.) ) bp.legendDecorator.textSize = 0.045 bp.normalizeByBinWidth = normByWidth[i_type] bp.yVariable = Variable( yLabel[i_type] ) bp.logY = logScale[i_type] bp.normalized = False if i_type == 1 or i_type == 2: # For FR and SF plots start y-axis at 0 bp.yVariable.binning.low = 0 bp.draw() bp.saveAsAll( os.path.join( "plots/", bp.title ) ) # -------------------- print 'everything is done!'
energyVar, 'Size > 1', Cut('size > 1'), style=redLine), 'E0') testBasicPlot.addHistogram( createHistogramFromTree(t, energyVar, 'Size > 3', Cut('size > 3'), style=blueLine), 'E0') testBasicPlot.addHistogram( createHistogramFromTree(t, energyVar, 'Size > 6', Cut('size > 6'), style=greenLine), 'E0') testBasicPlot.logY = True testBasicPlot.draw() # create correlation plot using the BasicPlot class testBasicPlot2D = BasicPlot('BasicPlot test 2D', sizeVar, energyVar, Variable('Entries')) testBasicPlot2D.addHistogram( create2DHistogramFromTree(t, sizeVar, energyVar), 'COLZ') testBasicPlot2D.addHistogram( create2DHistogramFromTree(t, sizeVar, energyVar, profile=True), 'E0') testBasicPlot2D.logZ = True testBasicPlot2D.draw() raw_input('Continue?')
h_var_r1.SetMarkerColor(kRed) h_var_r2.SetLineColor(kBlue) h_var_r2.SetMarkerColor(kBlue) bp = BasicPlot( 'Q-G-Separation' + sel_p_suffix[i_p] + '_' + var.name + cut_option_suffix, var) bp.yVariable = Variable("Events with quark match") bp.yVariable.binning.low = 0 bp.yVariable.binning.up = None bp.showBinWidthY = False bp.addHistogram(h_var_r1, 'E', copy=True) bp.addHistogram(h_var_r2, 'E', copy=True) bp.titles.append( '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}, ' .format(lumi / 1000.)) bp.draw() bp.saveAsAll(os.path.join("plots/", bp.title)) # print quark rate h_var_tot_r1 = process.getHistogram(var, "Region 1", cut=selections[3] + sel_p[i_p] + cut_option_region1, luminosity=lumi, weight=tmp_weight) sys.stdout.write('.') sys.stdout.flush() h_var_tot_r2 = process.getHistogram(var, "Region 2", cut=selections[3] + sel_p[i_p] +
def TemplateFit(filename, data, mc_q, mc_g, w_q=None, w_g=None, pull_widths=[1, 1], doPlots=True): n_mc = 2 # Define MC samples mc = TObjArray(n_mc) mc.Add(mc_q) mc.Add(mc_g) # Names mc_type = [] mc_type.append('Quarks') mc_type.append('Gluons') # Perform Fit fit = TFractionFitter(data, mc, "q") if w_q and w_g: fit.SetWeight(0, w_q) fit.SetWeight(1, w_g) fit.Constrain(0, 0.0, 1.0) #quarks fit.Constrain(1, 0.0, 1.0) #gluons fit.Fit() # Printing results val_q = ROOT.Double() err_q = ROOT.Double() val_g = ROOT.Double() err_g = ROOT.Double() fit.GetResult(0, val_q, err_q) fit.GetResult(1, val_g, err_g) err_q = err_q * pull_widths[ 0] # correcting the error for the width of the pull plot err_g = err_g * pull_widths[ 1] # correcting the error for the width of the pull plot for i in range(n_mc): val = ROOT.Double() err = ROOT.Double() fit.GetResult(i, val, err) out_file.write("fit result {}: {}, {}\n".format(mc_type[i], val, err)) out_file.write("X2 = {}\n".format(fit.GetChisquare())) out_file.write("NDF = {}\n".format(fit.GetNDF())) red_chi = fit.GetChisquare() / fit.GetNDF() out_file.write("red. X2 = {}\n".format(red_chi)) out_file.write("\n") if doPlots: print "storing plot as {}".format(filename) # Preparing Histograms h_fit = fit.GetPlot() h_q = fit.GetMCPrediction(0) h_g = fit.GetMCPrediction(1) h_fit.SetTitle("Fit") h_q.SetTitle("Quarks") h_g.SetTitle("Gluons") h_fit.SetLineColor(kGreen) h_q.SetLineColor(kRed) h_g.SetLineColor(kBlue) h_q.SetMarkerColor(kRed) h_g.SetMarkerColor(kBlue) # Scaling to the fit result int_fit = h_fit.Integral() int_q = h_q.Integral() int_g = h_g.Integral() h_q.Sumw2() h_g.Sumw2() h_q.Scale(val_q * int_fit / int_q) h_g.Scale(val_g * int_fit / int_g) # Checking fit vs. templates h_check = h_fit.Clone('Fit - Templates Check') h_check.Add(h_q, -1) h_check.Add(h_g, -1) check_maxBin = h_check.GetMaximumBin() out_file.write( "Maximal bin of fit minus templates divided by content of that bin in fit: {} / {}\n\n" .format(h_fit.GetBinContent(check_maxBin), h_check.GetBinContent(check_maxBin))) # Plotting results bp = BasicPlot(filename, var_tau0_width) bp.addHistogram(h_q, 'E', copy=True) bp.addHistogram(h_g, 'E', copy=True) bp.addHistogram(h_fit, 'HIST', copy=True) bp.addHistogram(data, 'E', copy=True) bp.titles.append( 'Quarks: {:.3} #pm {:.2}, Gluons: {:.3} #pm {:.2}'.format( val_q, err_q, val_g, err_g)) bp.titles.append( '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'. format(lumi / 1000.)) bp.titles.append('#chi^{{2}}/ndf: {:.3}'.format(red_chi)) bp.yVariable = Variable("Events") bp.legendDecorator.textSize = 0.045 bp.normalizeByBinWidth = False bp.showBinWidthY = False bp.normalized = False bp.logY = False bp.yVariable.binning.low = 0 # start y-axis at 0 bp.draw() bp.saveAsAll(os.path.join(outFolder, bp.title)) return fit
def PullPlot(filename, h_data, h_quarks, h_gluons, w_quarks=None, w_gluons=None, pull_widths=[1, 1]): # Creating a pull-plot # -------------------- ratio_q = [] error_q = [] ratio_g = [] error_g = [] n_runs = 10000 # getting fit results for toy experiments for i in range(1, n_runs + 1): if i == 1: print " starting loop" h_tmp = var.createHistogram(str(i)) h_tmp.FillRandom(h_data) filename_tmp = filename + '_' + str(i) out_file.write(str(i) + ":\n") fit = TemplateFit(filename, h_tmp, h_quarks, h_gluons, w_quarks, w_gluons, pull_widths, doPlots=False) val_q = ROOT.Double() err_q = ROOT.Double() val_g = ROOT.Double() err_g = ROOT.Double() fit.GetResult(0, val_q, err_q) fit.GetResult(1, val_g, err_g) del fit err_q = err_q * pull_widths[ 0] # correcting the error for the width of the pull plot err_g = err_g * pull_widths[ 1] # correcting the error for the width of the pull plot ratio_q.append(val_q) error_q.append(err_q) ratio_g.append(val_g) error_g.append(err_g) if i % (n_runs / 10) == 0 or i == 1 or i == n_runs: print " {}\tQ: {} +- {}\tG: {} +- {}".format( i, val_q, err_q, val_g, err_g) # creating pull histogram for quarks h_pull = var_pull.createHistogram() ratio_q_mean = sum(ratio_q) / len(ratio_q) for i in range(n_runs): tmp = (ratio_q[i] - ratio_q_mean) / error_q[i] h_pull.Fill(tmp) fit_result = h_pull.Fit('gaus', 'QS') f_gaus = h_pull.GetFunction('gaus') v_q_mu = fit_result.Parameter(1) v_q_mu_err = fit_result.ParError(1) v_q_sigma = fit_result.Parameter(2) v_q_sigma_err = fit_result.ParError(2) chi = fit_result.Chi2() ndf = fit_result.Ndf() #red_chi = chi / ndf filename_tmp = filename + '_PullPlot_q' bp = BasicPlot(filename_tmp, var_pull) bp.titles.append( 'Fit Results: #mu = {:05.3f}#pm{:05.3f}, #sigma = {:05.3f}#pm{:05.3f}'. format(v_q_mu, v_q_mu_err, v_q_sigma, v_q_sigma_err)) bp.titles.append('#chi^{{2}}/ndf: {:.3}/{}'.format(chi, ndf)) bp.addHistogram(h_pull, 'E', copy=True) bp.yVariable = Variable("Events") bp.normalizeByBinWidth = False bp.showBinWidthY = False bp.normalized = False bp.logY = False bp.yVariable.binning.low = 0 # start y-axis at 0 bp.draw() bp.saveAsAll(os.path.join(outFolder, bp.title)) del h_pull del bp # creating pull histogram for gluons h_pull = var_pull.createHistogram() ratio_g_mean = sum(ratio_g) / len(ratio_g) for i in range(n_runs): tmp = (ratio_g[i] - ratio_g_mean) / error_g[i] h_pull.Fill(tmp) fit_result = h_pull.Fit('gaus', 'QS') f_gaus = h_pull.GetFunction('gaus') v_g_mu = fit_result.Parameter(1) v_g_mu_err = fit_result.ParError(1) v_g_sigma = fit_result.Parameter(2) v_g_sigma_err = fit_result.ParError(2) chi = fit_result.Chi2() ndf = fit_result.Ndf() #red_chi = chi / ndf filename_tmp = filename + '_PullPlot_g' bp = BasicPlot(filename_tmp, var_pull) bp.titles.append( 'Fit Results: #mu = {:05.3f}#pm{:05.3f}, #sigma = {:05.3f}#pm{:05.3f}'. format(v_g_mu, v_g_mu_err, v_g_sigma, v_g_sigma_err)) bp.titles.append('#chi^{{2}}/ndf: {:.3}/{}'.format(chi, ndf)) bp.addHistogram(h_pull, 'E', copy=True) bp.yVariable = Variable("Events") bp.normalizeByBinWidth = False bp.showBinWidthY = False bp.normalized = False bp.logY = False bp.yVariable.binning.low = 0 # start y-axis at 0 bp.draw() bp.saveAsAll(os.path.join(outFolder, bp.title)) return [v_q_sigma, v_g_sigma]