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?' )
# 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)) # create a plot using the TreePlot class treePlotTest = TreePlot('TreePlot Test', sizeVar, Variable('Entries'), tree=t) treePlotTest.addCut('No Cut') treePlotTest.addCut('Energy > 15 MeV', 'energy > 15') treePlotTest.draw() # create a plot using the BasicPlot class
def testSystematicsCalculator(): from plotting.AtlasStyle import Style from plotting.Cut import Cut from plotting.Dataset import Dataset from plotting.DataMcRatioPlot import DataMcRatioPlot from plotting.DoublePlot import DoublePlot from plotting.HistogramStore import HistogramStore from plotting.Systematics import Systematics from plotting.Tools import divideGraphs from plotting.Variable import Binning, Variable from ROOT import TF1, kViolet, kCyan, kBlack, kDashed cut = Cut() luminosity = 36.2 f_zpeak = TF1('z-peak', 'TMath::Voigt((1./[0])*x-91.2, [1], 2.5)') f_zpeak.SetParNames('TES', 'TER') f_zpeak.SetParameter('TES', 1.0) f_zpeak.SetParameter('TER', 10.) f_cont = TF1('continuum', '[0]*1./x') f_cont.SetParNames('TES') f_cont.SetParameter('TES', 1.0) f_zpeak.SetParameter('TER', 10.) var = Variable('dimuon_mass', title='#it{m_{#tau#tau}}', unit='GeV', binning=Binning(20, 50., 150.)) store = HistogramStore('testHistogramStore.root') z_jets = Dataset('z_jets', '#it{Z}+jets', style=Style(kCyan), crossSection=0.0004) continuum = Dataset('continuum', 'Others', style=Style(kViolet), crossSection=0.002) sysLumi = Systematics.scaleSystematics('sysLumi', 'Lumi', 1.031, 0.968, 1.0) sysTES = Systematics.treeSystematics('sysTES', 'TES', 'tes_up', 'tes_down') sysTER = Systematics.treeSystematics('sysTER', 'TER', 'ter_up', 'ter_down') mcStat = 500000 datasets = [continuum, z_jets] functions = {z_jets: f_zpeak, continuum: f_cont} for dataset in datasets: dataset.addSystematics(sysTES) dataset.addSystematics(sysLumi) h = var.createHistogram(dataset.title) f = functions[dataset] h.FillRandom(f.GetName(), mcStat) store.putHistogram(dataset, dataset.nominalSystematics, var, cut, h) h.Reset() f.SetParameter('TES', 1.02) h.FillRandom(f.GetName(), mcStat) store.putHistogram(dataset, sysTES.up, var, cut, h) h.Reset() f.SetParameter('TES', 0.98) h.FillRandom(f.GetName(), mcStat) store.putHistogram(dataset, sysTES.down, var, cut, h) f.SetParameter('TES', 1.0) z_jets.addSystematics(sysTER) h.Reset() f_zpeak.SetParameter('TER', 13) h.FillRandom(f_zpeak.GetName(), mcStat) store.putHistogram(z_jets, sysTER.up, var, cut, h) h.Reset() f.SetParameter('TER', 7) h.FillRandom(f_zpeak.GetName(), mcStat) store.putHistogram(z_jets, sysTER.down, var, cut, h) p = DataMcRatioPlot('testPlot', var) for dataset in datasets: dataset.histogramStore = store p.addHistogram( dataset.getHistogram(var, cut=cut, luminosity=luminosity)) systGraph = calculateSystematicsGraph(datasets, var, luminosity=luminosity) systGraph.SetTitle('Combined Systematics') p.defaultCombinedErrorStyle.apply(systGraph) p.setMCSystematicsGraph(systGraph) p.draw() p.saveAs('test.pdf') p1 = DoublePlot('testPlot1', var, yVariableDown=Variable('Ratio', binning=Binning(low=0.8, up=1.2))) p1.plotDown.yVariable.binning.nDivisions = 205 nominalHist = None systematicsSet = SystematicsSet() for dataset in datasets: systematicsSet |= dataset.combinedSystematicsSet h = dataset.getHistogram(var, cut=cut, luminosity=luminosity) if nominalHist: nominalHist.Add(h) else: nominalHist = h nominalHist.SetLineColor(kBlack) nominalHist.SetTitle('Nominal') p1.plotUp.addGraph(systGraph, '2') p1.plotUp.addHistogram(nominalHist) rSystGraph = systGraph.Clone() rSystGraph.SetTitle() p1.plotDown.addGraph(rSystGraph, '2') divideGraphs(rSystGraph, nominalHist) lineColor = 1 for systematics in systematicsSet: lineColor += 1 upHist = None downHist = None # collect all up and down histograms for all datasets for dataset in datasets: upH = dataset.getHistogram(var, cut=cut, luminosity=luminosity, systematicVariation=systematics.up) downH = dataset.getHistogram(var, cut=cut, luminosity=luminosity, systematicVariation=systematics.down) downH.SetLineColor(lineColor) downH.SetLineStyle(kDashed) if upH: if upHist: upHist.Add(upH) else: upHist = upH if downH: if downHist: downHist.Add(downH) else: downHist = downH upHist.SetLineColor(lineColor) upHist.SetTitle(systematics.title) downHist.SetLineColor(lineColor) downHist.SetLineStyle(kDashed) downHist.SetTitle('') rUpHist = upHist.Clone() rUpHist.Divide(nominalHist) rUpHist.SetTitle('') rDownHist = downHist.Clone() rDownHist.Divide(nominalHist) rDownHist.SetTitle('') p1.plotUp.addHistogram(upHist) p1.plotUp.addHistogram(downHist) p1.plotDown.addHistogram(rUpHist) p1.plotDown.addHistogram(rDownHist) p1.draw() raw_input()
h2.FillRandom('gaus', 500) h2.SetLineColor( kBlue ) # if no fill color is defined, the line color is used to fill stacked histograms h3 = TH1D('hTest3', 'Data', 20, -4, 4) h3.FillRandom('gaus', 1500) h3.SetLineColor(kBlack) hSum = TH1D('hSum', 'Stat', 20, -4, 4) mcErrorStyle.apply(hSum) hRatio = TH1D('hRatio', 'Ratio', 20, -4, 4) hRatio.Sumw2() xVar = Variable('Invariant Mass', unit='GeV', binning=Binning(20, -4, 4)) yVarDown = Variable('Ratio', binning=Binning(low=0.5, up=1.5)) # Add the ATLAS label to each plot BasicPlot.defaultTitleDecorator = AtlasTitleDecorator('work in progress') # create a double plot with ratios by hand doubleTest = DoublePlot('testDoublePlot', xVar, yVariableDown=yVarDown) doubleTest.plotUp.stacked = True doubleTest.plotUp.addHistogram(hSum, 'E2') doubleTest.plotUp.addHistogram(h3, 'E0') hSum.Add(doubleTest.plotUp.addHistogram(h1, stacked=True)) hSum.Add(doubleTest.plotUp.addHistogram(h2, stacked=True, copy=True)) hRatio.Add(h3) hRatio.Divide(hSum) doubleTest.plotDown.addHistogram(hRatio, 'E0')
from plotting.Variable import Binning, Variable, VariableBinning from plotting.Systematics import Systematics, SystematicsSet, SystematicVariation #===================# # Weight Expression # #===================# weight_total = "weight_total" # acutally used weight expression: weight_expression = weight_total #==========# # Binnings # #==========# bin_pt = VariableBinning([20, 25, 30, 40, 60, 120]) bin_pt_lino = VariableBinning([50, 80, 120]) bin_pt_equal = Binning(10, 20, 120) bin_pt_fine = Binning(24, 0, 120) bin_eta = VariableBinning( [-2.5, -1.52, -1.37, -1.1, -0.05, 0.05, 1.1, 1.37, 1.52, 2.5]) bin_eta_equal = Binning(15, -3, 3) bin_phi = Binning(11, -3.15, 3.15) bin_bdt = Binning(20, 0, 1) #===========# # Variables # #===========# # Variable( name, variable, x-axis title, unit, binning ) var_run_number = Variable('run_number', 'run_number', 'Run Number', '', Binning(100, 250000, 350000)) var_pull = Variable('pull', 'pull', 'Pull', '', Binning(70, -3.5, 3.5))
from plotting.AtlasStyle import blackLine, redLine, blueLine from ROOT import TFile, TTree, TRandom3 from array import array rndm = TRandom3() pT0 = array('f', [0]) pT1 = array('f', [0]) t = TTree('tree', 'tree') t.Branch('tau_0_pt', pT0, 'tau_0_pt/F') t.Branch('tau_1_pt', pT1, 'tau_1_pt/F') sumOfWeights = 0. for entry in xrange(10000): pT0[0] = rndm.Gaus(60, 15) pT1[0] = rndm.Gaus(50, 10) t.Fill() binning = Binning(50, 0, 100) var_tau_0_pt = Variable('tau_0_pt', title='p_{T}(#tau_{0})', unit='GeV', binning=binning) var_tau_1_pt = Variable('tau_1_pt', title='p_{T}(#tau_{1})', unit='GeV', binning=binning) var_tau_pt = CombinedVariable('tau_0_pt', 'p_{T}(#tau)', 'GeV', binning, variables=[var_tau_0_pt, var_tau_1_pt]) p = BasicPlot('Test Plot', var_tau_pt)
nBlocks = int(round(barLength * fraction)) t = '\r[{0}] ({1}/{2}) {3:50}'.format( '#' * nBlocks + '-' * (barLength - nBlocks), current, total, text) sys.stdout.write(t) sys.stdout.flush() if fraction >= 1: print '' if __name__ == '__main__': from plotting.Variable import Variable, Binning from plotting.BasicPlot import BasicPlot var = Variable( 'ditau_tau0_decay_mode', 'ditau_tau0_decay_mode', 'Decay Mode (#tau_{0})', '', Binning(6, 0, 6, ['1p0n', '1p1n', '1pXn', '3p0n', '3pXn', 'Other'])) h = var.createHistogram('h_test') h.Fill(2) h.Fill(3) h.Fill(2) h.Fill(0) h.Fill(2) g = histToGraph(h, 'g_test') g.SetLineColor(2) #g = addGraphs( g, g, True ) p = BasicPlot('test', var) p.addHistogram(h)
from plotting.BasicPlot import BasicPlot, LegendElement from plotting.DoublePlot import DoublePlot from plotting.Variable import Binning, Variable, var_Events from plotting.Tools import histToGraph, combineStatsAndSystematics, addGraphs, divideGraphs, resetGraph,\ divideHistograms, createOutOfRangeArrows import uuid from copy import copy var_dataMc = Variable('Data / Model', binning=Binning(low=0.7, up=1.3)) var_dataMc.binning.nDivisions = 205 class DataMcRatioPlot(DoublePlot): from AtlasStyle import Style from ROOT import kRed, kSpring, kGray, TColor defaultStatErrorTitle = 'Stat. Uncertainty' defaultSystErrorTitle = 'Syst. Uncertainty' defaultCombinedErrorTitle = 'Stat #oplus Syst' defaultStatErrorStyle = Style(TColor.GetColorTransparent(kSpring, 0.3), lineWidth=0, fillStyle=1000, markerStyle=0) defaultSystErrorStyle = Style(kGray + 3, lineWidth=0, fillStyle=3354, markerStyle=0) defaultCombinedErrorStyle = Style(kGray + 3, lineWidth=0, fillStyle=3354, markerStyle=0)