Exemple #1
0
def common_make_root_histogram_test():
  x_list = list(range(0,10000))
  w_list = []
  for i in range( len(x_list) ): 
    x_list[i] = float(x_list[i])/10000.
    w_list.append(x_list[i])
  y_list = __sine_list(2., 2.*math.pi, x_list)
  hist1 = common.make_root_histogram('hist sin(x)', y_list, 'hist of x', 100)
  hist2 = common.make_root_histogram('hist sin(x)', y_list, 'hist of x', 50, x_list, 'hist of y', 50)
  hist3 = common.make_root_histogram('hist sin(x)', y_list, 'hist of x', 10, x_list, 'hist of y', 10, w_list)
  testpass = __test_root_hist(hist3,'hist sin(x)','hist of x','hist of y',common.min_max(y_list, w_list, rg.histo_margin)[0], common.min_max(y_list, w_list, rg.histo_margin)[1], 
                                                                          common.min_max(x_list, w_list, rg.histo_margin)[0], common.min_max(x_list, w_list, rg.histo_margin)[1], 
                              rg.line_color, rg.line_style, rg.line_width, rg.fill_color, '')
  hist4 = common.make_root_histogram('hist sin(x)', y_list, 'hist of x', 10, x_list, 'hist of y', 10, w_list, xmin=y_list[7500],xmax=y_list[2500],ymin=x_list[2500],ymax=x_list[7500],
                                     line_color=1, line_style=2, line_width=3, fill_color=4, hist_title_string='title_string')
  testpass = __test_root_hist(hist4,'hist sin(x)', 'hist of x', 'hist of y', y_list[7500], y_list[2500], x_list[2500], x_list[7500], 1, 2, 3, 4, 'title_string')
  canvas1 = common.make_root_canvas('hist_test1')
  hist1.Draw()
  canvas2 = common.make_root_canvas('hist_test2')
  hist2.Draw('cont1z')
  canvas3 = common.make_root_canvas('hist_test3')
  hist3.Draw('lego')
  canvas4 = common.make_root_canvas('hist_test4')
  hist4.Draw('lego')
  if not testpass: return 'fail'
  return 'pass'
Exemple #2
0
    def test_binomial(self):
        """
        Check that we can generate a binomial distribution for event number
        """
        # make a dict of bunches of xboa.Hits separated by event (spill) number
        bunch_dict = Bunch.new_dict_from_read_builtin('maus_json_primary', \
                          BIN_SIM, 'spill')
        bunch_weights = []
        for bunch in bunch_dict.values():
            bunch_weights.append(bunch.bunch_weight())
        canvas = common.make_root_canvas("")
        hist = common.make_root_histogram("generated distribution", \
             bunch_weights, "bunch weights", BIN_N+1, xmin=-0.5, xmax=BIN_N+0.5)
        hist.Fill(0,
                  1000 - hist.GetSumOfWeights())  # xboa ignores empty spills
        hist.Draw()

        test_hist = ROOT.TH1D("test_hist", "reference distribution", BIN_N + 1,
                              -0.5,
                              BIN_N + 0.5)  # pylint: disable = E1101, C0301
        for i in range(BIN_N + 1):
            test_hist.SetBinContent(i, \
                    (ROOT.TMath.BinomialI(BIN_P, BIN_N, i-1)- ROOT.TMath.BinomialI(BIN_P, BIN_N, i))*N_SPILLS) # pylint: disable = E1101, C0301
            print i, test_hist.GetBinContent(i), hist.GetBinContent(i)
        test_hist.SetLineStyle(2)
        test_hist.Draw("SAME")
        ks_value = test_hist.KolmogorovTest(hist)
        canvas.Update()
        canvas.Print(PLOT_DIR + "/binomial_distribution_test.png")
        print "binomial ks_value", ks_value
        self.assertGreater(ks_value, 1e-3)
    def test_binomial(self):
        """
        Check that we can generate a binomial distribution for event number
        """
        # make a dict of bunches of xboa.Hits separated by event (spill) number
        bunch_dict = Bunch.new_dict_from_read_builtin('maus_json_primary', \
                          BIN_SIM, 'spill')
        bunch_weights = []
        for bunch in bunch_dict.values():
            bunch_weights.append(bunch.bunch_weight())
        canvas = common.make_root_canvas("")
        hist = common.make_root_histogram("generated distribution", \
             bunch_weights, "bunch weights", BIN_N+1, xmin=-0.5, xmax=BIN_N+0.5)
        hist.Fill(0, 1000-hist.GetSumOfWeights()) # xboa ignores empty spills
        hist.Draw()

        test_hist = ROOT.TH1D("test_hist", "reference distribution", BIN_N+1, -0.5, BIN_N+0.5)  # pylint: disable = E1101, C0301
        for i in range(BIN_N+1):
            test_hist.SetBinContent(i, \
                    (ROOT.TMath.BinomialI(BIN_P, BIN_N, i-1)- ROOT.TMath.BinomialI(BIN_P, BIN_N, i))*N_SPILLS) # pylint: disable = E1101, C0301
            print i, test_hist.GetBinContent(i), hist.GetBinContent(i)
        test_hist.SetLineStyle(2)
        test_hist.Draw("SAME")
        ks_value = test_hist.KolmogorovTest(hist)
        canvas.Update()
        canvas.Print(PLOT_DIR+"/binomial_distribution_test.png")
        print "binomial ks_value", ks_value
        self.assertGreater(ks_value, 1e-3)
Exemple #4
0
def plot_one_phi(row_data, da_key, axis1, axis2, max_n_points, variables):
    global ROOT_OBJECTS
    hit_data = row_data[da_key]
    name = "phi_" + da_key + "_" + axis1 + " vs " + axis2
    title = get_title(variables)
    name += " " + title
    canvas = None
    tune_data = []
    area_data = []
    da_row = get_da_row(hit_data, max_n_points)
    print("Plot on phi")
    cholesky_canvas = common.make_root_canvas(name + "_cholesky")
    cholesky_canvas.SetCanvasSize(1000, 1000)
    delta = (da_row + 1) * 0.4 + 1.
    hist = common.make_root_histogram("", [-100.],
                                      "u",
                                      100, [-100.],
                                      "u'",
                                      100,
                                      xmin=-delta,
                                      xmax=+delta,
                                      ymin=-delta,
                                      ymax=+delta)
    hist.Draw()
    for i, hit_list in enumerate(hit_data[:da_row + 1]):
        hit_list = [Hit.new_from_dict(hit_dict) for hit_dict in hit_list[1]]
        finder = DPhiTuneFinder()
        finder.u = [hit[axis1] for hit in hit_list[1:]]
        finder.up = [hit[axis2] for hit in hit_list[1:]]
        area_src = numpy.array([[hit[axis1], hit[axis2]] for hit in hit_list])
        try:
            finder.get_tune()
            an_area = get_area(area_src)
        except Exception:
            print("Failed to unpack data for phi da plot")
            continue
        cholesky_canvas, hist, graph = finder.plot_cholesky_space(
            cholesky_canvas, 1. + i * 0.2)
        if i < da_row:
            color = ROOT.TColor(10000 + len(ROOT_OBJECTS), 0.,
                                1. - i * 1. / da_row, 0.)
            ROOT_OBJECTS.append(color)
            graph.SetMarkerColor(ROOT.kGreen)
        ROOT_OBJECTS += [hist, graph]
        tune_data += copy.deepcopy(finder.dphi)
        area_data += [an_area for i in finder.dphi]
        print("    Area", an_area, end=' ')
        print(" tune", numpy.mean(tune_data), "+/-", numpy.std(tune_data))
    canvas = common.make_root_canvas(name)
    canvas.Draw()
    hist, graph = common.make_root_graph(name, area_data, "Amplitude [mm]",
                                         tune_data, "Fractional tune")
    ROOT_OBJECTS += [hist, graph]
    hist.SetTitle(da_key)
    hist.Draw()
    graph.SetMarkerStyle(24)
    graph.Draw("p same")
    return canvas, cholesky_canvas
    def test_apply_weights_bound(self):
        test_bunch = Bunch()
        for i in range(1000):
            x = random.gauss(0., 10.**0.5)
            y = random.gauss(0., 20.**0.5)
            test_hit = Hit.new_from_dict({'x':x, 'y':y})
            test_bunch.append(test_hit)
        limit_ellipse = numpy.zeros([2, 2])
        for i in range(2):
            limit_ellipse[i, i] = 200.
        bound = BoundingEllipse(limit_ellipse, numpy.zeros([2]), 50)

        my_weights = VoronoiWeighting(['x', 'y'],
                                      numpy.array([[20., 0.],[0., 20.]]),
                                      voronoi_bound = bound)
        print("Plotting weights", datetime.datetime.now())
        canvas, hist = test_bunch.root_histogram('x', 'mm', 'y', 'mm')
        hist.Draw("COLZ")
        canvas.Update()
        print('Covariances ["x", "y"] before\n', test_bunch.covariance_matrix(['x', 'y']))
        print("Applying weights", datetime.datetime.now())
        my_weights.apply_weights(test_bunch, False)
        print("Plotting tesselation", datetime.datetime.now())
        #my_weights.plot_two_d_projection(['x', 'y'], 'weight')
        my_weights.plot_two_d_projection(['x', 'y'], 'weight')
        my_weights.plot_two_d_projection(['x', 'y'], 'content')
        print("Plotting weights", datetime.datetime.now())
        canvas, hist = test_bunch.root_histogram('x', 'mm', 'y', 'mm')
        hist.Draw("COLZ")
        canvas.Update()
        test_bunch.root_histogram('local_weight', nx_bins=100)
        canvas = common.make_root_canvas('local_weight')
        hist = common.make_root_histogram('local_weight', test_bunch.list_get_hit_variable(['local_weight'])[0], 'local_weight', 100)
        hist.Draw()
        canvas.Update()
        canvas = common.make_root_canvas('content')
        hist = common.make_root_histogram('content', my_weights.tile_content_list, 'content', 100)
        hist.Draw()
        canvas.Update()
        print('Covariances ["x", "y"] after\n', test_bunch.covariance_matrix(['x', 'y']))
    def plot_areas(self, areas_us, areas_ds):
        assert(len(areas_us) == len(areas_ds))
        # remove None items from the list (failed to calculate area)
        areas = zip(areas_us, areas_ds)
        areas = [item for item in areas if item[0] != None and item[1] != None]
        areas_us = [item[0] for item in areas]
        areas_ds = [item[1] for item in areas]
        # remove residuals
        areas_res = [item[1]-item[0] for item in areas]
        for i in range(3):#len(areas_us)):
            print i, str(round(areas_us[i])).ljust(10), str(round(areas_ds[i])).ljust(10), areas_res[i]
        # plots
        canvas = common.make_root_canvas("content")
        n_bins = 100# max(int((len(bunch_us)/10)**0.5), 10)

        hist_us = common.make_root_histogram("Upstream", areas_us, "cell content [mm MeV/c]", n_bins, xmin=-5000.0, xmax=5000.0)
        hist_us.SetLineColor(2)
        hist_us.Draw()
        hist_ds = common.make_root_histogram("Downstream", areas_ds, "cell content [mm MeV/c]", n_bins, xmin=-5000.0, xmax=5000.0)
        hist_ds.SetLineColor(4)
        hist_ds.Draw("SAME")
        hist_res = common.make_root_histogram("Residuals", areas_res, "cell content [mm MeV/c]", n_bins)
        hist_res.Draw("SAME")
Exemple #7
0
    def plot_areas(self, areas_us, areas_ds):
        assert (len(areas_us) == len(areas_ds))
        # remove None items from the list (failed to calculate area)
        areas = zip(areas_us, areas_ds)
        areas = [item for item in areas if item[0] != None and item[1] != None]
        areas_us = [item[0] for item in areas]
        areas_ds = [item[1] for item in areas]
        # remove residuals
        areas_res = [item[1] - item[0] for item in areas]
        for i in range(3):  #len(areas_us)):
            print i, str(round(areas_us[i])).ljust(10), str(round(
                areas_ds[i])).ljust(10), areas_res[i]
        # plots
        canvas = common.make_root_canvas("content")
        n_bins = 100  # max(int((len(bunch_us)/10)**0.5), 10)

        hist_us = common.make_root_histogram("Upstream",
                                             areas_us,
                                             "cell content [mm MeV/c]",
                                             n_bins,
                                             xmin=-5000.0,
                                             xmax=5000.0)
        hist_us.SetLineColor(2)
        hist_us.Draw()
        hist_ds = common.make_root_histogram("Downstream",
                                             areas_ds,
                                             "cell content [mm MeV/c]",
                                             n_bins,
                                             xmin=-5000.0,
                                             xmax=5000.0)
        hist_ds.SetLineColor(4)
        hist_ds.Draw("SAME")
        hist_res = common.make_root_histogram("Residuals", areas_res,
                                              "cell content [mm MeV/c]",
                                              n_bins)
        hist_res.Draw("SAME")
Exemple #8
0
 def test_sft_accuracy(self):
     # test slow fourier transform
     canvas = common.make_root_canvas("SFT Residuals")
     hist = common.make_root_histogram('', [],
                                       'Residuals',
                                       1000, [],
                                       '',
                                       1000,
                                       xmin=1.,
                                       xmax=1000.0,
                                       ymin=0.0,
                                       ymax=1.0)
     hist.Draw()
     for i in range(3, 4):
         truth_list, sft_list, fft_list, n_list = [], [], [], []
         tune = i * 0.1
         for n in [5, 10, 20, 50, 100, 200, 500, 1000]:
             co, tracking = matrix(tune * 2. * math.pi, n)
             fft = FFTTuneFinder()
             fft.run_tracking('x', 10.0, co, tracking)
             tune_sft, tune_fft, tune_sft_hanning = 0., 0., 0.
             try:
                 tune_fft, tune_sft, tune_sft_hanning = self._ft_test(fft)
             except ValueError:
                 # statistics are low enough that no primary peaks can be
                 # found
                 sys.excepthook(*sys.exc_info())
             if tune > 0.5:
                 tune_sft = 1 - tune_sft
                 tune_fft = 1 - tune_fft
             #print "truth", tune, "sft", tune_sft,
             #print "fft", tune_fft, "hanning", tune_sft_hanning
             truth_list.append(tune)
             sft_list.append(tune_sft)
             fft_list.append(tune_fft)
             n_list.append(n)
         residual_list = []
         for i, n in enumerate(n_list):
             if fft_list[i] - truth_list[i] != 0.:
                 residual_list.append(abs(sft_list[i]-truth_list[i])\
                                     /abs(fft_list[i]-truth_list[i]))
         hist, graph = common.make_root_graph("SFT residuals", n_list,
                                              "Number of points",
                                              residual_list, "Residuals")
         graph.Draw('L')
     canvas.SetLogx()
     canvas.Update()
def peak_hist(peak_list):
    canvas = common.make_root_canvas("projected peaks")
    hist = common.make_root_histogram("projected peaks",
                                      peak_list,
                                      "#deltat [ns]",
                                      200,
                                      xmin=0,
                                      xmax=630.)
    bins = []
    for bin_index in range(1, hist.GetNbinsX() + 1):
        bins.append(hist.GetBinContent(bin_index))
    smoothing = xboa.algorithms.smoothing.GaussianSmoothing(2., 3, True)
    smoothed = smoothing.smooth(bins)
    finder = xboa.algorithms.peak_finder.WindowPeakFinder(10, -1, 1)
    peak_graph_indices, peak_x, peak_y = [], [], []
    peaks = []
    for peak in finder.find_peaks(smoothed):
        a_peak = {
            "peak": hist.GetBinCenter(peak + 1),
            "magnitude": bins[peak],
            "err": 0.
        }
        if a_peak["magnitude"] < 0.5 * max(smoothed):
            continue
        peak_graph_indices.append(peak)
        for bin in range(peak, 0, -1) + range(len(bins) - 1, peak, -1):
            if smoothed[bin] < smoothed[peak] / 2.:
                peak_graph_indices.append(bin)
                a_peak["lower_bound"] = hist.GetBinCenter(bin + 1)
                break
        for bin in range(peak, len(bins), +1) + range(0, peak, 1):
            if smoothed[bin] < smoothed[peak] / 2.:
                peak_graph_indices.append(bin)
                a_peak["upper_bound"] = hist.GetBinCenter(bin + 1)
                break
        peaks.append(a_peak)
    for peak_graph_index in peak_graph_indices:
        peak_x.append(hist.GetBinCenter(peak_graph_index + 1))
        peak_y.append(smoothed[peak_graph_index])
    dummy, graph = xboa.common.make_root_graph("peaks", peak_x, "", peak_y, "")
    graph.SetMarkerStyle(24)
    graph.SetMarkerColor(2)
    canvas.Draw()
    hist.Draw()
    graph.Draw('p')
    canvas.Update()
    return peaks, canvas
def get_period(data):
    rf_peak_indices = data["RF"]["peak_indices"]
    rf_peak_list = [data["RF"]["time_list"][i] / 1e-9 for i in rf_peak_indices]
    rf_peak_deltas_list = [None] * (len(rf_peak_list) - 1)

    for i, rf_time in enumerate(rf_peak_list[0:-1]):
        rf_next_time = rf_peak_list[i + 1]
        rf_peak_deltas_list[i] = rf_next_time - rf_time
    canvas = common.make_root_canvas("rf_period")
    canvas.Draw()
    hist = common.make_root_histogram("times", rf_peak_deltas_list,
                                      "RF period [ns]", 100)
    hist.Draw()
    hist.SetStats(True)
    canvas.Update()
    canvas.Draw()

    return numpy.mean(rf_peak_deltas_list), rf_peak_list[0], canvas
def plot(plot_dir, data_list_pairs):
    tmp_data = []
    for i, data in enumerate(data_list_pairs):
        pos_peak = numpy.mean(
            [x["y"] for x in data["RF"]["pos_peak_with_errors"]])
        neg_peak = numpy.mean(
            [-x["y"] for x in data["RF"]["neg_peak_with_errors"]])
        voltage = (pos_peak - neg_peak)
        data["rf_peak_to_peak_voltage"] = voltage
        rf_list = ["0.2", "0.32", "0.46", "0.62", "0.81", "3.01", "7.18"]
        if True or str(round(voltage, 2)) in rf_list:
            print "Using", voltage
            tmp_data.append(data)
        else:
            print "Not using", voltage
    data_list_pairs = tmp_data
    peak_delta_canvas = common.make_root_canvas("delta rf vs bpm")
    peak_delta_canvas.Draw()
    peak_delta_hist_canvas = common.make_root_canvas("delta rf vs bpm - hist")
    peak_delta_hist_canvas.Draw()
    graph_list = []
    hist_list = []
    dt_mean_list = []
    dt_err_list = []
    measured_voltage_list = []
    max_y = -1e9
    min_y = 1e9
    delta_hist_min = 1e9
    delta_hist_max = -1e9
    for i, data in enumerate(data_list_pairs):
        """
        voltage_str = "V="+str(round(data["rf_peak_to_peak_voltage"], 2))
        print "Plotting measured "+voltage_str
        formats = ["png", "root"]
        times = data["RF"]["time_list"]

        rf_voltage_list = data["RF"]["voltage_list"]
        rf_peak_error_list = data["RF"]["pos_peak_with_errors"]
        rf_peak_list = [x["x"] for x in rf_peak_error_list]
        rf_period = sum([rf_peak_list[j+1] - rf_peak_list[j] for j, index in enumerate(rf_peak_list[:-1])])  
        rf_period = DT*rf_period/float(len(rf_peak_list))
        rf_frequency = 1./rf_period
        print "  RF Period", rf_period, "Frequency", rf_frequency

        print "  Plotting RF magnitude and error"
        canvas_rf_v, hist_rf_v, graph_rf_v = graph_peak_magnitude(data["RF"]["pos_peak_with_errors"], data["RF"]["neg_peak_with_errors"], title=voltage_str)
        for format in formats:
            canvas_rf_v.Print(plot_dir+voltage_str+"_rf_voltage."+format)

        print "  Plotting peak finding errors"
        canvas_err, hist_err, graph_err = graph_peak_errors(data["signal"]["peak_with_errors"], 4, title=voltage_str)
        graph_peak_errors(data["RF"]["pos_peak_with_errors"], 2, canvas_err)
        for format in formats:
            canvas_err.Print(plot_dir+voltage_str+"_peak_errors."+format)

        print "  Plotting peak to peak distance"
        print "    signal",
        canvas_pp, hist_pp, graph_pp = graph_delta_times(data["signal"]["peak_with_errors"], 4, title=voltage_str)
        print "    RF",
        graph_delta_times(data["RF"]["pos_peak_with_errors"], 2, canvas_pp)
        for format in formats:
            canvas_pp.Print(plot_dir+voltage_str+"_peak_deltas."+format)

        print "  Plotting data"
        y_range = common.min_max(data["RF"]["voltage_list"]+data["signal"]["voltage_list"]+data["dc_signal"]["voltage_list"])
        canvas, hist, graph, peak_hist, peak_graph = graph_peak_times(data["RF"], data["RF"]["pos_peak_with_errors"], 2, y_range, title=voltage_str)
        bpm_peak_indices = data["signal"]["peak_indices"]
        bpm_voltage_list = data["signal"]["voltage_list"]
        bpm_peak_list = [index for index in bpm_peak_indices]
        graph_peak_times(data["signal"], data["signal"]["peak_with_errors"], 4, y_range, canvas)
        graph_peak_times(data["dc_signal"], [{"x":0, "y":0}], 6, y_range, canvas)
        canvas.Update()
        for format in formats:
            canvas.Print(plot_dir+voltage_str+"_signals."+format)
        """

        print "  Plotting distance between bpm and rf peaks"
        peak_delta_canvas.cd()
        peak_time_list = data["peak_time_list"]
        peak_delta_list = data["peak_delta_list"]

        hist, graph = common.make_root_graph(voltage_str, peak_time_list,
                                             "time [ns]", peak_delta_list,
                                             "dt [ns]")
        min_y = min([hist.GetYaxis().GetXmin(), min_y])
        max_y = max([hist.GetYaxis().GetXmax(), max_y])
        if len(data_list_pairs) > 1:
            hist.Draw()
            color_fraction = float(i) / float(len(data_list_pairs) - 1)
        else:
            color_fraction = 1
        color = ROOT.TColor.GetColor(color_fraction, 1. - color_fraction, 0)
        graph.SetMarkerColor(color)
        graph.SetLineColor(color)
        graph.SetMarkerStyle(6)
        graph_list.append(graph)

        peak_delta_hist_canvas.cd()
        filtered_list = []
        for j, peak in enumerate(peak_delta_list):
            if peak_time_list[j] > 30.e3 and \
               peak_time_list[j] < 40.e3:
                filtered_list.append(peak)
        if len(filtered_list) > 0:
            dt_mean_list.append(max(filtered_list))
            #dt_err_list.append(numpy.std(filtered_list)/len(filtered_list)**0.5)
            measured_voltage_list.append(data["rf_peak_to_peak_voltage"])
        hist = common.make_root_histogram(voltage_str, filtered_list,
                                          "dt [ns]", 4)
        delta_hist_min = min([hist.GetXaxis().GetXmin(), delta_hist_min])
        delta_hist_max = max([hist.GetXaxis().GetXmax(), delta_hist_max])
        hist.SetLineColor(color)
        hist_list.append(hist)

    peak_delta_canvas.cd()
    hist, graph = common.make_root_graph(voltage_str,
                                         peak_time_list,
                                         "t [ns]",
                                         peak_delta_list,
                                         "dt [ns]",
                                         ymin=0.,
                                         ymax=700.)
    hist.Draw()  # redraw hist to get axes right
    for graph in graph_list:
        graph.Draw("p")
    legend = common.make_root_legend(peak_delta_canvas, graph_list)
    legend.Draw()
    peak_delta_canvas.Update()
    for format in formats:
        peak_delta_canvas.Print(plot_dir + "bpm_to_rf_deltas." + format)

    peak_delta_hist_canvas.cd()
    print delta_hist_min, delta_hist_max
    hist = common.make_root_histogram(voltage_str, [-1],
                                      "dt [ns]",
                                      1000, [-1],
                                      "Counts",
                                      1000,
                                      xmin=delta_hist_min,
                                      xmax=delta_hist_max,
                                      ymin=1e-2,
                                      ymax=20.)
    hist.Draw()  # redraw hist to get axes right
    for a_hist in hist_list:
        a_hist.Draw("same")
    legend = common.make_root_legend(peak_delta_hist_canvas, hist_list)
    legend.Draw()
    peak_delta_hist_canvas.Update()
    for format in formats:
        peak_delta_hist_canvas.Print(plot_dir + "bpm_to_rf_deltas_hist." +
                                     format)

    if len(dt_mean_list) < 2:
        return
    phase_mean_list = [
        2. * math.pi * dt / rf_period for dt in dt_mean_list[1:]
    ]
    measured_voltage_list = measured_voltage_list[1:]
    for popper in []:
        phase_mean_list.pop(popper)
        phase_err_list.pop(popper)
        measured_voltage_list.pop(popper)
    synchronous_phase_canvas = common.make_root_canvas(
        "Synchronous phase vs voltage")
    hist, graph = common.make_root_graph("Synchronous phase vs voltage",
                                         phase_mean_list, "#phi [rad]",
                                         measured_voltage_list, "V [au]")
    graph = ROOT.TGraph(len(phase_mean_list))
    graph.SetMarkerStyle(4)
    for i in range(len(phase_mean_list)):
        graph.SetPoint(i, phase_mean_list[i], measured_voltage_list[i])

    print phase_mean_list
    print measured_voltage_list
    hist.Draw()
    graph.Draw('p')
    print "Doing unweighted fit"
    fit_unweighted = ROOT.TF1("fit", "[0]/sin([1]+x)")
    fit_unweighted.SetParameter(0, 0.1)
    fit_unweighted.SetParameter(1, 0.)
    fit_unweighted.SetLineColor(4)
    graph.Fit(fit_unweighted, "EX0")
    fit_unweighted.Draw("SAME")
    synchronous_phase_canvas.Update()
    for format in formats:
        synchronous_phase_canvas.Print(
            plot_dir + "synchronous_phase_vs_voltage_unweighted." + format)
    def amplitude_scatter_plot(self, suffix):
        """
        Make a scatter plot of upstream versus downstream data
        """
        amp_dict_ds = self.amp.amplitudes[suffix]["amplitude_dict_downstream"]
        amp_dict_us = self.amp.amplitudes[suffix]["amplitude_dict_upstream"]
        amp_list_us = []
        amp_list_ds = []
        amp_list_delta = []

        suffix_label = self.get_suffix_label(suffix)
        for key, amp_ds in amp_dict_ds.iteritems():
            try:
                amp_us = amp_dict_us[key]
            except KeyError:
                sys.excepthook(*sys.exc_info())
                continue
            amp_delta = amp_us - amp_ds
            amp_list_us.append(amp_us)
            amp_list_ds.append(amp_ds)
            amp_list_delta.append(amp_delta)
        canvas = common.make_root_canvas("amplitude_residuals")
        canvas.Draw()
        n_points = min(len(amp_list_us),
                       10000)  # no more than 10k points in the scatter
        hist, graph = common.make_root_graph(
            "delta amplitude scatter",
            amp_list_us,
            "US " + suffix_label + " Amplitude [mm]",
            amp_list_delta,
            suffix_label + " US Amplitude - DS Amplitude [mm]",
            xmin=0.,
            xmax=100.,
            ymin=-50.,
            ymax=50.)
        hist.SetTitle(self.config_anal['name'])
        hist.Draw()
        graph.Draw("P")
        canvas.Update()
        for format in ["eps", "png", "root"]:
            canvas.Print(self.plot_dir + "amplitude_delta_" + suffix +
                         "_scatter." + format)

        canvas = common.make_root_canvas("delta_amplitude_hist")
        canvas.Draw()
        canvas.SetFrameFillColor(utilities.utilities.get_frame_fill())
        hist = common.make_root_histogram("delta amplitude hist",
                                          amp_list_us,
                                          suffix_label + " US Amplitude [mm]",
                                          100,
                                          amp_list_delta,
                                          suffix_label +
                                          " US Amplitude - DS Amplitude [mm]",
                                          100,
                                          xmin=0.,
                                          xmax=100.,
                                          ymin=-50.,
                                          ymax=50.)
        hist.SetTitle(self.config_anal['name'])
        hist.Draw("COLZ")
        canvas.Update()
        for format in ["eps", "png", "root"]:
            canvas.Print(self.plot_dir + "amplitude_delta_" + suffix +
                         "_hist." + format)
def plot(plot_dir, data_list_pairs):
    tmp_data = []
    for i, data in enumerate(data_list_pairs):
        pos_peak = numpy.mean([x["y"] for x in data["RF"]["pos_peak_with_errors"]])
        neg_peak = numpy.mean([-x["y"] for x in data["RF"]["neg_peak_with_errors"]])
        voltage = (pos_peak-neg_peak)
        data["rf_peak_to_peak_voltage"] = voltage
        rf_list = ["0.2", "0.32", "0.46", "0.62", "0.81", "3.01", "7.18"]
        if True or str(round(voltage, 2)) in rf_list:
            print "Using", voltage
            tmp_data.append(data)
        else:
            print "Not using", voltage
    data_list_pairs = tmp_data
    peak_delta_canvas = common.make_root_canvas("delta rf vs bpm")
    peak_delta_canvas.Draw()
    peak_delta_hist_canvas = common.make_root_canvas("delta rf vs bpm - hist")
    peak_delta_hist_canvas.Draw()
    graph_list = []
    hist_list = []
    dt_mean_list = []
    dt_err_list = []
    measured_voltage_list = []
    max_y = -1e9
    min_y = 1e9
    delta_hist_min = 1e9
    delta_hist_max = -1e9
    for i, data in enumerate(data_list_pairs):
        """
        voltage_str = "V="+str(round(data["rf_peak_to_peak_voltage"], 2))
        print "Plotting measured "+voltage_str
        formats = ["png", "root"]
        times = data["RF"]["time_list"]

        rf_voltage_list = data["RF"]["voltage_list"]
        rf_peak_error_list = data["RF"]["pos_peak_with_errors"]
        rf_peak_list = [x["x"] for x in rf_peak_error_list]
        rf_period = sum([rf_peak_list[j+1] - rf_peak_list[j] for j, index in enumerate(rf_peak_list[:-1])])  
        rf_period = DT*rf_period/float(len(rf_peak_list))
        rf_frequency = 1./rf_period
        print "  RF Period", rf_period, "Frequency", rf_frequency

        print "  Plotting RF magnitude and error"
        canvas_rf_v, hist_rf_v, graph_rf_v = graph_peak_magnitude(data["RF"]["pos_peak_with_errors"], data["RF"]["neg_peak_with_errors"], title=voltage_str)
        for format in formats:
            canvas_rf_v.Print(plot_dir+voltage_str+"_rf_voltage."+format)

        print "  Plotting peak finding errors"
        canvas_err, hist_err, graph_err = graph_peak_errors(data["signal"]["peak_with_errors"], 4, title=voltage_str)
        graph_peak_errors(data["RF"]["pos_peak_with_errors"], 2, canvas_err)
        for format in formats:
            canvas_err.Print(plot_dir+voltage_str+"_peak_errors."+format)

        print "  Plotting peak to peak distance"
        print "    signal",
        canvas_pp, hist_pp, graph_pp = graph_delta_times(data["signal"]["peak_with_errors"], 4, title=voltage_str)
        print "    RF",
        graph_delta_times(data["RF"]["pos_peak_with_errors"], 2, canvas_pp)
        for format in formats:
            canvas_pp.Print(plot_dir+voltage_str+"_peak_deltas."+format)

        print "  Plotting data"
        y_range = common.min_max(data["RF"]["voltage_list"]+data["signal"]["voltage_list"]+data["dc_signal"]["voltage_list"])
        canvas, hist, graph, peak_hist, peak_graph = graph_peak_times(data["RF"], data["RF"]["pos_peak_with_errors"], 2, y_range, title=voltage_str)
        bpm_peak_indices = data["signal"]["peak_indices"]
        bpm_voltage_list = data["signal"]["voltage_list"]
        bpm_peak_list = [index for index in bpm_peak_indices]
        graph_peak_times(data["signal"], data["signal"]["peak_with_errors"], 4, y_range, canvas)
        graph_peak_times(data["dc_signal"], [{"x":0, "y":0}], 6, y_range, canvas)
        canvas.Update()
        for format in formats:
            canvas.Print(plot_dir+voltage_str+"_signals."+format)
        """

        print "  Plotting distance between bpm and rf peaks"
        peak_delta_canvas.cd()
        peak_time_list = data["peak_time_list"]
        peak_delta_list = data["peak_delta_list"]

        hist, graph = common.make_root_graph(voltage_str, peak_time_list, "time [ns]", peak_delta_list, "dt [ns]")
        min_y = min([hist.GetYaxis().GetXmin(), min_y])
        max_y = max([hist.GetYaxis().GetXmax(), max_y])
        if len(data_list_pairs) > 1:
            hist.Draw()
            color_fraction = float(i)/float(len(data_list_pairs)-1)
        else:
            color_fraction = 1
        color = ROOT.TColor.GetColor(color_fraction, 1.-color_fraction, 0)
        graph.SetMarkerColor(color)
        graph.SetLineColor(color)
        graph.SetMarkerStyle(6)
        graph_list.append(graph)

        peak_delta_hist_canvas.cd()
        filtered_list = []
        for j, peak in enumerate(peak_delta_list):
            if peak_time_list[j] > 30.e3 and \
               peak_time_list[j] < 40.e3:
                filtered_list.append(peak)
        if len(filtered_list) > 0:
            dt_mean_list.append(max(filtered_list))
            #dt_err_list.append(numpy.std(filtered_list)/len(filtered_list)**0.5)
            measured_voltage_list.append(data["rf_peak_to_peak_voltage"])
        hist = common.make_root_histogram(voltage_str, filtered_list, "dt [ns]", 4)
        delta_hist_min = min([hist.GetXaxis().GetXmin(), delta_hist_min])
        delta_hist_max = max([hist.GetXaxis().GetXmax(), delta_hist_max])
        hist.SetLineColor(color)
        hist_list.append(hist)

    peak_delta_canvas.cd()
    hist, graph = common.make_root_graph(voltage_str, peak_time_list, "t [ns]", peak_delta_list, "dt [ns]", ymin=0., ymax=700.)
    hist.Draw() # redraw hist to get axes right
    for graph in graph_list:
        graph.Draw("p")
    legend = common.make_root_legend(peak_delta_canvas, graph_list)
    legend.Draw()
    peak_delta_canvas.Update() 
    for format in formats:
        peak_delta_canvas.Print(plot_dir+"bpm_to_rf_deltas."+format)

    peak_delta_hist_canvas.cd()
    print delta_hist_min, delta_hist_max
    hist = common.make_root_histogram(voltage_str, [-1], "dt [ns]", 1000, [-1], "Counts", 1000, xmin=delta_hist_min, xmax=delta_hist_max, ymin=1e-2, ymax=20.)
    hist.Draw() # redraw hist to get axes right
    for a_hist in hist_list:
        a_hist.Draw("same")
    legend = common.make_root_legend(peak_delta_hist_canvas, hist_list)
    legend.Draw()
    peak_delta_hist_canvas.Update()
    for format in formats:
        peak_delta_hist_canvas.Print(plot_dir+"bpm_to_rf_deltas_hist."+format)

    if len(dt_mean_list) < 2:
        return
    phase_mean_list = [2.*math.pi*dt/rf_period for dt in dt_mean_list[1:]]
    measured_voltage_list = measured_voltage_list[1:]
    for popper in []:
        phase_mean_list.pop(popper)
        phase_err_list.pop(popper)
        measured_voltage_list.pop(popper)
    synchronous_phase_canvas = common.make_root_canvas("Synchronous phase vs voltage")
    hist, graph = common.make_root_graph("Synchronous phase vs voltage", phase_mean_list, "#phi [rad]", measured_voltage_list, "V [au]")
    graph = ROOT.TGraph(len(phase_mean_list))
    graph.SetMarkerStyle(4)
    for i in range(len(phase_mean_list)):
        graph.SetPoint(i, phase_mean_list[i], measured_voltage_list[i])

    print phase_mean_list 
    print measured_voltage_list
    hist.Draw()
    graph.Draw('p')
    print "Doing unweighted fit"
    fit_unweighted = ROOT.TF1("fit", "[0]/sin([1]+x)")
    fit_unweighted.SetParameter(0, 0.1)
    fit_unweighted.SetParameter(1, 0.)
    fit_unweighted.SetLineColor(4)
    graph.Fit(fit_unweighted, "EX0")
    fit_unweighted.Draw("SAME")
    synchronous_phase_canvas.Update()    
    for format in formats:
        synchronous_phase_canvas.Print(plot_dir+"synchronous_phase_vs_voltage_unweighted."+format)