Beispiel #1
0
 con.close()
 # Read cached results from tsv file
 e, n = np.loadtxt(path, delimiter=" ").transpose()  # MeV, #
 # Determine bin width
 wvl = 4.135667516e-15 * 299792458.0 / e * 1e3
 wvl_min = min(wvl)
 wvl_max = max(wvl)
 nbins = int((wvl_max - wvl_min) / args.bin_width)
 eff = TEfficiency("%d-pass" % i, "%d-eff" % i, nbins, wvl_min, wvl_max)
 eff.SetUseWeightedEvents()
 eff.SetStatisticOption(TEfficiency.kFNormal)
 for wvli, ni in zip(wvl, n):
     eff.FillWeighted(True, ni, wvli)
     eff.FillWeighted(False, n_particles - ni, wvli)
 x = np.array(
     [eff.GetTotalHistogram().GetBinCenter(i) for i in xrange(nbins)])
 y = np.array([eff.GetEfficiency(i) for i in xrange(nbins)]) * 100.0
 yerr_low = np.array([
     eff.ClopperPearson(int(n_particles),
                        int(eff.GetEfficiency(i) * n_particles), 0.68,
                        False) for i in xrange(nbins)
 ]) * 100.0
 yerr_up = np.array([
     eff.ClopperPearson(int(n_particles),
                        int(eff.GetEfficiency(i) * n_particles), 0.68, True)
     for i in xrange(nbins)
 ]) * 100.0
 # Plot bin statistics.
 label = ("%s +%.1f V" % (name, v_ov * 1e6))
 # Plot PDE
 ax.errorbar(x, y, yerr=(y - yerr_low, yerr_up - y), fmt=".", label=label)
Beispiel #2
0
def RunAnalysis(input_name, output_name=""):
    # Check output name, set by default if not passed to the function
    if not output_name:
        output_name = input_name.split("_")[0] + "_analysed.root"
    print("INPUT:", input_name, "\nOUTPUT:", output_name)

    # Configuring thresholds
    (thr_start, thr_end, thr_step) = (0.3, 8, 0.1)
    thr_range = np.arange(thr_start, thr_end + thr_step, thr_step)
    n_thr = len(thr_range)

    # Get hit dictionary
    hit_dict = GetHitDict(input_name)

    # Open root file to write the results into
    write_file = TFile("data/" + output_name, "recreate")
    write_file.cd()

    # Initialize efficiency and cluster size objects
    eff = TEfficiency("Efficiency", "Efficiency;Threshold [fC];Efficiency",
                      n_thr, thr_start, thr_end)
    clus_graph = TGraphErrors(n_thr)
    clus_graph.SetNameTitle("Average_cluster_size", "Average_cluster_size")
    clus_graph.GetXaxis().SetTitle("Threshold [fC]")
    clus_graph.GetYaxis().SetTitle("Average cluster size")

    # Perform threshold scanning
    for thr in thr_range:
        thrE = thr * 6242.2
        print("Threshold scanning:",
              round(thr / thr_end * 100, 1),
              "%",
              end="\r")

        # Scan a threshold and obtain a list of clusters
        cluster_list = ScanThreshold(hit_dict, thrE)

        # Analyze cluster list and fill TEfficiency object
        for cluster in cluster_list:
            eff.Fill(bool(cluster), thr)

        # Replace zeros with NaN to obtain proper average later
        cluster_list = [
            np.nan if cluster == 0 else cluster for cluster in cluster_list
        ]

        try:
            # Calculate average cluster size and add points to clus_graph
            cluster_size = np.nanmean(cluster_list)
            N = np.count_nonzero(~np.isnan(cluster_list))
            err = np.nanstd(cluster_list) / sqrt(N - 1)
            clus_graph.SetPoint(int(np.where(thr_range == thr)[0][0]), thr,
                                cluster_size)
            clus_graph.SetPointError(int(np.where(thr_range == thr)[0][0]),
                                     ex=0,
                                     ey=err)
        except RuntimeWarning:
            pass

    print("\nDone.")

    # Efficiency fit
    fit_form = "0.5*[0]*TMath::Erfc((x-[1])/(TMath::Sqrt(2)*[2])*(1-0.6*TMath::TanH([3]*(x-[1])/TMath::Sqrt(2)*[2])))"
    fit_func = TF1("Efficiency_fit", fit_form, 0, 8)
    # Set initial parameter values and limits
    fit_func.SetParameters(1, 4, 1, 1, 0.5)
    fit_func.SetParLimits(1, 0, 5)
    fit_func.SetParLimits(2, 0, 2)
    fit_func.SetParLimits(3, 0, 2)
    fit_func.SetParLimits(4, 0.5, 0.7)
    # Perform fit
    eff.Fit(fit_func, "R")

    # Write outputs to file
    eff.Write()
    clus_graph.Write()

    # Collect info
    source = "allpix"
    angle = input_name.split("-")[0]
    descr = input_name.split("_")[0].strip("0deg-")
    n_events = str(int(eff.GetTotalHistogram().GetEntries() / n_thr))
    title = source + "," + angle + "," + descr + "(" + str(n_events) + "ev)"
    vt50 = str(fit_func.GetParameter(1))
    vt50_err = str(fit_func.GetParError(1))
    thr_range = str(thr_start) + ":" + str(thr_end) + ":" + str(thr_step)

    # Write info to Info directory
    info_dir = write_file.mkdir("Info")
    info_dir.cd()
    info_dir.WriteObject(TString(source), "source")
    info_dir.WriteObject(TString(angle), "angle")
    info_dir.WriteObject(TString(descr), "descr")
    info_dir.WriteObject(TString(n_events), "n_events")
    info_dir.WriteObject(TString(title), "title")
    info_dir.WriteObject(TString(vt50), "vt50")
    info_dir.WriteObject(TString(vt50), "vt50_err")
    info_dir.WriteObject(TString(thr_range), "thr_range")
    write_file.Close()