def book(out_name, hs=[]): bins = binning() h = ROOT.TH1F(out_name, '', len(bins) - 1, bins) hs.append(h) return h
def cutplayMangle(filename): debug = False file = ROOT.TFile.Open(filename) if not file.IsOpen(): raise RuntimeError('could not open %s' % filename) norm = norm_from_file(filename) x = file.Get('SimpleTriggerEfficiency/triggers_pass_num') cutscans = {} nm1 = None # Extract from the big list of bins the numbers separately for each # cut. for i in xrange(1, x.GetNbinsX() + 1): label = x.GetXaxis().GetBinLabel(i) if label == 'nm1': nm1 = x.GetBinContent(i), x.GetBinError(i) continue try: cut, cut_val = label.split('X') except ValueError: print 'warning: could not parse label %s' % label continue if not cutscans.has_key(cut): cutscans[cut] = [] cutscans[cut].append((cut_val, x.GetBinContent(i))) h_intot = file.Get('SimpleTriggerEfficiency/triggers_pass_den') ntot = h_intot.GetBinContent(1), h_intot.GetBinError(1) file.Close() output_fn = filename.replace('.root', '_mangled.root') output_file = ROOT.TFile(output_fn, 'RECREATE') sample_name = os.path.basename(filename).replace('.root', '') try: sample = getattr(Samples, sample_name) except AttributeError: sample = None h_ntot = ROOT.TH1F('ntot', '', 1, 0, 1) scale = ntot[0] / norm if sample is not None else 1. h_ntot.SetBinContent(1, ntot[0] / scale) h_ntot.SetBinError(1, ntot[1] / scale) if nm1 is not None: h_nm1 = ROOT.TH1F('nm1', '', 1, 0, 1) h_nm1.SetBinContent(1, nm1[0]) h_nm1.SetBinError(1, nm1[1]) else: raise ValueError('did not find nm1 value in input') output_hists = [] for cut_name, scan in cutscans.iteritems(): if debug: print 'cut: %s' % cut_name hmin = depize(scan[0][0]) hminplus1 = depize(scan[1][0]) binwidth = hminplus1 - hmin hmaxminus1 = depize(scan[-1][0]) hmax = hmaxminus1 + binwidth if debug: print 'hmin: ', hmin, ' hminplus1: ', hminplus1, ' binwidth: ', binwidth, ' hmaxminus1: ', hmaxminus1, ' hmax: ', hmax hist = ROOT.TH1F(cut_name, '', len(scan), hmin, hmax) output_hists.append(hist) for cut_bin_name, nevents in scan: bin = hist.FindBin(depize(cut_bin_name) + 0.00001) bin_edge = hist.GetBinLowEdge(bin) if debug: print 'cut bin name: %s ibin: %i bin_edge: %s number of events: %f' % ( cut_bin_name, bin, bin_edge, nevents) hist.SetBinContent(bin, nevents) output_file.Write() output_file.Close()
def dphi(v0, v1): phi0 = phi(v0) phi1 = phi(v1) res = phi0 - phi1 while res > pi: res -= 2*pi while res <= -pi: res += 2*pi return res h_y_x = [] for i in xrange(0,10): h_y_x.append(ROOT.TH2F('svdist2d_%i'%i, '%i um <= svdist2d < %i um;x (cm);y (cm)'%(100*i,100*(i+1)), 200, -0.1, 0.1, 200, -0.1, 0.1)) h_dz = ROOT.TH1F('h_dz', '', 100, -0.2, 0.2) h_dphi = ROOT.TH1F('h_phi', '', 100, -pi, pi) h_y_x_g = [[] for x in xrange(10)] h_dzs = [ROOT.TH1F('h_dz_%i' % x, '', 100, -0.2, 0.2) for x in xrange(10)] h_dphis = [ROOT.TH1F('h_dphi_%i' % x, '', 100, -pi, pi) for x in xrange(10)] for sample, ntracks01, svdist2d, v0, v1 in events: svdist2d *= 10000 bin = int(svdist2d / 100) #print svdist2d, bin, event[3][0]-0.244, event[3][1]-0.3928, event[4][0]-0.244, event[4][1]-0.3928 dz = v0[-1] - v1[-1] v0 = debs(v0) v1 = debs(v1) dp = dphi(v0, v1)