class FractionJetHistograms (Histograms) : '''eta distribution of the energy fraction per component''' def __init__ (self, name) : self.histos = [] for i in range (8) : # NB here we start from 0 on purpose, for simplicity self.histos.append (TH2F (name + '_' + str (len (self.histos)), '', 240, -6, 6, 100, 0, 1)) super (FractionJetHistograms, self).__init__ (name) # .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... def fillJet (self, jet) : try: for i in range (1, 8) : self.histos[i].Fill (jet.eta (), jet.component (i).fraction ()) except: pass # .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... def fillEvent (self, jets) : for jet in jets: self.fillJet (jet) # .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... def summary (self) : '''to be run after the event loop, before saving''' self.summ = THStack (self.name + '_summ', 'total energy') self.mean = [] for i in range (1, 8) : # self.mean.append (self.histos[i].ProfileX ()) self.mean.append (self.fromProfileToHisto (self.histos[i].ProfileX (), 10 + i * 2)) self.summ.Add (self.mean[len (self.mean) - 1]) # .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... def fromProfileToHisto (self, profile, color = 0) : thename = profile.GetName ().replace('pfx', 'ave') histo = TH1F (thename, '', profile.GetNbinsX (), profile.GetXaxis ().GetXmin (), profile.GetXaxis ().GetXmax ()) histo.SetFillColor (color) for iBin in range (1, profile.GetNbinsX () + 1) : histo.SetBinContent (iBin, profile.GetBinContent (iBin)) histo.SetBinError (iBin, profile.GetBinError (iBin)) return histo # .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... def Write (self, dir) : '''overloads mother function, to save lists contents''' self.dir = dir.mkdir( self.name ) self.dir.cd () self.summ.Write () for i in range (1, 8) : self.histos[i].Write () self.mean[i-1].Write () dir.cd ()
def slice_and_save(sample,histo,outfile): outfile.cd() histo_stack=THStack(histo,'x','Stack_'+histo.GetName(),'') histo_1d=histo_stack.GetHists() histo_stack.Write(histo_stack.GetName()+'_'+sample) nextinlist=TIter(histo_1d) obj=nextinlist() while obj: obj.Write(obj.GetName()+'_'+sample) obj=nextinlist() histo.Write(histo.GetName()+'_'+sample)
real6.SetFillColor(kBlue) hs = THStack("hs", "") hs.Add(real) hs.Add(fake) hs3 = THStack("hs3", "") hs3.Add(real3) hs3.Add(fake3) hs4 = THStack("hs4", "") hs4.Add(real4) hs4.Add(fake4) hs5 = THStack("hs5", "") hs5.Add(real5) hs5.Add(fake5) hs6 = THStack("hs6", "") hs6.Add(real6) hs6.Add(fake6) fout = TFile("fakeTrackD0.root", "recreate") fout.cd() hs.Write("fakeTrackD0") hs6.Write("fakeTrackD0NHits6") hs5.Write("fakeTrackD0NHits5") hs4.Write("fakeTrackD0NHits4") hs3.Write("fakeTrackD0NHits3") fout.Close()
def doPlot(plotName, fn, Nmax=5, Nmin=0, outputSuffix='', inputSuffix='', outputHistogramName='', histogramDescription='', rootFile=None, doStack=True, errfn=None, doSaveHistograms=False, Rebin=None): ''' plotName is the name of the plot, without scale suffix inputSuffix is the suffix of the input histogram ''' N = Nmax - Nmin + 1 fullPlotName = '%s_%s' % (plotName, inputSuffix) if outputHistogramName == '': outputHistogramName = plotName if histogramDescription == '': histogramDescription = 'Merged sample for %s' % plotName print 'Looking for information for plot: ', plotName hjs = [0] * 6 hjexs = [0] * 6 hjborns = [0] * 6 for n in range(Nmin, min(Nmax + 1, 5)): hjs[n] = filesj[n].Get(fullPlotName) htest = filesj[n].Get(fullPlotName + '%%Njets_%s_%s' % (n - 0.5, n + 0.5)) if htest: hjexs[n] = htest else: htest = filesj[n].Get(fullPlotName + '%%%sj' % (n)) if htest: hjexs[n] = htest else: print 'No information about exclusive %s jet for histogram %s (with suffix %s) in file %s' % ( n, plotName, inputSuffix, filesj[n].GetName()) hjborns[n] = filesjborn[n].Get(fullPlotName) if Rebin: def doRebin(x): if x: return HistogramTools.RebinHist(x, Rebin, x.GetName() + "rebinned") else: return x hjexs = [doRebin(h) for h in hjexs] hjs = [doRebin(h) for h in hjs] hjborns = [doRebin(h) for h in hjborns] bins = list() validhjs = [h for h in hjs if h] if not validhjs: print 'No proper histograms found' sys.exit(1) nbrBins = validhjs[0].GetNbinsX() names = [ '%s_%s_%s' % (plotName, outputSuffix, i) for i in range(Nmin, Nmax + 1) ] if HistogramTools.hasVariableBinSize(validhjs[0]): bins = validhjs[0].GetXaxis().GetXbins() bs = [bins[i] for i in range(nbrBins + 1)] b = array.array('d', bs) hist = TH1D(outputHistogramName + '_' + outputSuffix, histogramDescription, nbrBins, b) hists = [ TH1D(name, histogramDescription, nbrBins, b) for name in names ] else: xmin = hjs[2].GetBinLowEdge(1) xmax = hjs[2].GetBinLowEdge(nbrBins + 1) hist = TH1D(outputHistogramName + '_' + outputSuffix, histogramDescription, nbrBins, xmin, xmax) hists = [ TH1D(name, histogramDescription, nbrBins, xmin, xmax) for name in names ] for i in range(1, nbrBins + 1): #if hjs[2].GetBinCenter(i)!=h3j.GetBinCenter(i): #print 'Bins don\'t correspond! h2j: ',h2j.GetBinCenter(i),' h3j:',h3j.GetBinCenter(i) data = container() data.nj = [None] * 6 data.njborn = [None] * 6 data.njex = [None] * 6 data.njerr = [None] * 6 data.njbornerr = [None] * 6 data.njexerr = [None] * 6 for njet in range(Nmin, Nmax + 1): if hjs[njet]: data.nj[njet] = hjs[njet].GetBinContent(i) data.njerr[njet] = hjs[njet].GetBinError(i) else: print 'No info for %s' % njet data.nj[njet] = None data.njerr[njet] = None if hjborns[njet]: data.njborn[njet] = hjborns[njet].GetBinContent(i) data.njbornerr[njet] = hjborns[njet].GetBinError(i) else: print 'No info for %s born' % njet data.njborn[njet] = None data.njbornerr[njet] = None for njet in range(0, 5): if hjexs[njet]: data.njex[njet] = hjexs[njet].GetBinContent(i) data.njexerr[njet] = hjexs[njet].GetBinError(i) else: if data.nj[njet] == None or data.njborn[njet + 1] == None: data.njex[njet] = None data.njexerr[njet] = None else: data.njex[njet] = data.nj[njet] - data.njborn[njet + 1] data.njexerr[njet] = math.sqrt( math.pow(data.njerr[njet], 2) + math.pow(data.njbornerr[njet + 1], 2)) if data.njex[4] and data.nj[4]: data.nj[5] = data.nj[4] - data.njex[4] data.njerr[5] = math.sqrt( math.pow(data.njerr[4], 2) + math.pow(data.njexerr[4], 2)) #print 'nj',data.nj #print 'njex',data.njex #print 'njborn',data.njborn hValues = fn(data) sumv = sum([v for v in hValues if v]) hist.SetBinContent(i, sumv) if errfn: errValues = errfn(data) sumerr = math.sqrt(sum([v * v for v in errValues if v])) hist.SetBinError(i, sumerr) #print hValues for v in range(len(hValues)): if hValues[v]: hists[v].SetBinContent(i, hValues[v]) else: hists[v].SetBinContent(i, 0) if doStack: tsName = 'sh_' + outputHistogramName + '_' + outputSuffix #print tsName stack = THStack(tsName, 'Stacked view of ' + histogramDescription) for i in range(N): hists[i].SetFillColor(colors[i]) stack.Add(hists[i]) if rootFile: rootFile.cd() if doStack: stack.Write() if doSaveHistograms: for h in hists: h.Write() hist.Write() if doStack: return (hist, stack) else: return hist