Ejemplo n.º 1
0
def makePage(histos, fileName, fileDescr, separateFiles, logscale):
    """
    Prepares a canvas with one histogram per pad
    """
    import rootpy
    from rootpy.plotting import Hist, Canvas
    from ROOT import kBlue, gPad
    log = logging.getLogger('pyroplot')
    cans = {}
    log.info("Drawing histograms ..")
    for idx, name in enumerate(sorted(histos.keys())):
        if separateFiles:
            log.debug("Creating new canvas with index %d." % (idx))
            c = Canvas(600, 800)
            cans[name] = c
            markCanvas(c, fileName, 0.05, y=0.009, size=0.025, color=2)
        if not separateFiles and (idx) % 6 == 0:
            log.debug("Creating new canvas with index %d." % (idx / 6))
            # start a new canvas
            c = Canvas(600, 800)
            cans[fileName + '/' + name] = c
            c.Divide(2, 3)
            markCanvas(c, fileName, 0.05, y=0.009, size=0.025, color=2)
        # draw the histogram
        hist = histos[name]
        log.debug("Drawing histogram #" + str(idx % 6 + 1) + ": " +
                  hist.GetName() + " (" + hist.__class__.__name__ +
                  ") in canvas #" + str(int(idx / 6)))
        hist.color = kBlue
        if not separateFiles:
            c.cd(idx % 6 + 1)
        if logscale:
            gPad.SetLogy()
        plotHistos(histos=[hist], text=name)
        if fileDescr:
            markPad(text=fileDescr, x=.14, y=.8, size=0.041, color=2)
    return cans
Ejemplo n.º 2
0
=================================================

This is an example of drawing a quantile-quantile plot with a confidence level
(CL) band.
"""
print(__doc__)
import ROOT
from rootpy.interactive import wait
from rootpy.plotting import Hist, Canvas, Legend, set_style
from rootpy.plotting.contrib.quantiles import qqgraph
from rootpy.extern.six.moves import range

set_style('ATLAS')

c = Canvas(width=1200, height=600)
c.Divide(2, 1, 1e-3, 1e-3)

rand = ROOT.TRandom3()
h1 = Hist(100,
          -5,
          5,
          name="h1",
          title="Histogram 1",
          linecolor='red',
          legendstyle='l')
h2 = Hist(100,
          -5,
          5,
          name="h2",
          title="Histogram 2",
          linecolor='blue',
Ejemplo n.º 3
0
h_t2.SetLineColor(8)
h_t3.SetLineColor(6)
h_t4.SetLineColor(7)
h_data.SetLineColor(1)
h_data.SetMarkerColor(1)

ymax = getMax([h_data, h_t1, h_t2, h_t3])
ymax = ymax * 1.1
h_data.GetYaxis().SetRangeUser(0, ymax)
h_t1.GetYaxis().SetRangeUser(0, ymax)
h_t2.GetYaxis().SetRangeUser(0, ymax)
h_t3.GetYaxis().SetRangeUser(0, ymax)
h_t4.GetYaxis().SetRangeUser(0, ymax)

c = Canvas()
c.Divide(2)
c.cd(1)
h_data.Draw('PE')
h_t1.Draw('SAME HIST')
h_t2.Draw('SAME HIST')
h_t3.Draw('SAME HIST')
h_t4.Draw('SAME HIST')

templates = {}
if useT1: templates['t1'] = h_t1
if useT2: templates['t2'] = h_t2
if useT3: templates['t3'] = h_t3
if useT4: templates['t4'] = h_t4

fit_data = FitData(h_data, templates, fit_boundaries=(0, h_data.nbins()))
Ejemplo n.º 4
0
    def duration_in_seconds(self):
        return self.__finish - self.__start


data0 = "normal_small", np.random.normal(0.5, 1, 200)
data1 = "normal", np.random.normal(0.5, 1, 100000)
data2 = "uniform", np.random.random(100000)
data3 = "normal+uniform", np.concatenate((data1[1], 10 * data2[1]))
data4 = "normal+normal", np.concatenate((data1[1], np.random.normal(2.5, 0.1, 100000)))

datas = (data0, data1, data2, data3, data4)
recipes = "manual1", "sturges", "sturges-doane", "scott", "sqrt", \
    "doane", "freedman-diaconis", "risk", "knuth"
objs = []
canvas = Canvas()
canvas.Divide(len(recipes), len(datas))
print '-' * 80
print '\t\t{:<20s}{:>10s}   {:<6s}'.format('method', 'bins', 'time/s')
print '-' * 80
for id, (dataname, d) in enumerate(datas):
    print dataname
    for ir, r in enumerate(recipes):
        canvas.cd(id * len(recipes) + ir + 1)
        timer = Timer()
        if r == "manual1":
            with timer:
                bins, h = FillHistogram(d, 50, np.min(d), np.max(d), drawstyle='hist')
        else:
            with timer:
                bins, h = FillHistogram(d, binning=r, drawstyle='hist')
        print '\t\t{:<20s}{:>10d}   {:<6.2f}'.format(r, h.GetNbinsX(), timer.duration_in_seconds())
Ejemplo n.º 5
0
    def ln_likelihood_full_matching(self,
                                    w_value,
                                    alpha,
                                    zeta,
                                    beta,
                                    gamma,
                                    delta,
                                    kappa,
                                    eta,
                                    lamb,
                                    g1_value,
                                    extraction_efficiency,
                                    gas_gain_value,
                                    gas_gain_width,
                                    spe_res,
                                    s1_acc_par0,
                                    s1_acc_par1,
                                    s2_acc_par0,
                                    s2_acc_par1,
                                    scale_par,
                                    d_gpu_local_info,
                                    draw_fit=False):

        # -----------------------------------------------
        # -----------------------------------------------
        # determine prior likelihood and variables
        # -----------------------------------------------
        # -----------------------------------------------

        prior_ln_likelihood = 0
        matching_ln_likelihood = 0

        # get w-value prior lieklihood
        prior_ln_likelihood += self.get_ln_prior_w_value(w_value)

        # priors of detector variables
        prior_ln_likelihood += self.get_ln_prior_g1(g1_value)
        prior_ln_likelihood += self.get_ln_prior_extraction_efficiency(
            extraction_efficiency)
        prior_ln_likelihood += self.get_ln_prior_gas_gain_value(gas_gain_value)
        prior_ln_likelihood += self.get_ln_prior_gas_gain_width(gas_gain_width)
        prior_ln_likelihood += self.get_ln_prior_spe_res(spe_res)

        prior_ln_likelihood += self.get_ln_prior_s1_acc_pars(
            s1_acc_par0, s1_acc_par1)
        prior_ln_likelihood += self.get_ln_prior_s2_acc_pars(
            s2_acc_par0, s2_acc_par1)

        # get priors from lindhard parameters
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(alpha)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(beta)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(gamma)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(kappa)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(eta)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(lamb)

        prior_ln_likelihood += self.get_ln_prior_zeta(zeta)
        prior_ln_likelihood += self.get_ln_prior_delta(delta)

        # if prior is -inf then don't bother with MC
        #print 'removed prior infinity catch temporarily'
        if not np.isfinite(prior_ln_likelihood) and not draw_fit:
            return -np.inf

        # -----------------------------------------------
        # -----------------------------------------------
        # run MC
        # -----------------------------------------------
        # -----------------------------------------------

        num_trials = np.asarray(self.num_mc_events, dtype=np.int32)
        num_repetitions = np.asarray(d_gpu_local_info['num_repetitions'],
                                     dtype=np.int32)
        mean_field = np.asarray(self.d_data_parameters['mean_field'],
                                dtype=np.float32)

        w_value = np.asarray(w_value, dtype=np.float32)
        alpha = np.asarray(alpha, dtype=np.float32)
        zeta = np.asarray(zeta, dtype=np.float32)
        beta = np.asarray(beta, dtype=np.float32)
        gamma = np.asarray(gamma, dtype=np.float32)
        delta = np.asarray(delta, dtype=np.float32)
        kappa = np.asarray(kappa, dtype=np.float32)
        eta = np.asarray(eta, dtype=np.float32)
        lamb = np.asarray(lamb, dtype=np.float32)

        g1_value = np.asarray(g1_value, dtype=np.float32)
        extraction_efficiency = np.asarray(extraction_efficiency,
                                           dtype=np.float32)
        gas_gain_value = np.asarray(gas_gain_value, dtype=np.float32)
        gas_gain_width = np.asarray(gas_gain_width, dtype=np.float32)
        spe_res = np.asarray(spe_res, dtype=np.float32)

        s1_acc_par0 = np.asarray(s1_acc_par0, dtype=np.float32)
        s1_acc_par1 = np.asarray(s1_acc_par1, dtype=np.float32)

        s2_acc_par0 = np.asarray(s2_acc_par0, dtype=np.float32)
        s2_acc_par1 = np.asarray(s2_acc_par1, dtype=np.float32)

        # for histogram binning
        num_bins_s1 = np.asarray(self.s1_settings[0], dtype=np.int32)
        num_bins_s2 = np.asarray(self.s2_settings[0], dtype=np.int32)

        a_hist_2d = np.zeros(self.s1_settings[0] * self.s2_settings[0],
                             dtype=np.int32)

        #print d_gpu_local_info['d_gpu_energy'][degree_setting]

        l_gpu_args = (d_gpu_local_info['rng_states'], drv.In(num_trials),
                      drv.In(num_repetitions), drv.In(mean_field),
                      d_gpu_local_info['gpu_energies'], drv.In(w_value),
                      drv.In(alpha), drv.In(zeta), drv.In(beta), drv.In(gamma),
                      drv.In(delta), drv.In(kappa), drv.In(eta), drv.In(lamb),
                      drv.In(g1_value), drv.In(extraction_efficiency),
                      drv.In(gas_gain_value), drv.In(gas_gain_width),
                      drv.In(spe_res), drv.In(s1_acc_par0),
                      drv.In(s1_acc_par1), drv.In(s2_acc_par0),
                      drv.In(s2_acc_par1), drv.In(num_bins_s1),
                      d_gpu_local_info['gpu_bin_edges_s1'],
                      drv.In(num_bins_s2),
                      d_gpu_local_info['gpu_bin_edges_s2'],
                      drv.InOut(a_hist_2d))

        #print '\n\n\nBefore call...'
        #print d_gpu_local_info
        d_gpu_local_info['function_to_call'](*l_gpu_args, **self.d_gpu_scale)
        #print 'After call...\n\n\n'

        a_s1_s2_mc = np.reshape(a_hist_2d,
                                (self.s2_settings[0], self.s1_settings[0])).T

        #print list(a_s1_s2_mc)

        sum_mc = np.sum(a_s1_s2_mc, dtype=np.float32)
        if sum_mc == 0:
            #print 'sum mc == 0'
            return -np.inf

        # this forces our scale to be close to 1 (difference comes from acceptance)
        a_s1_s2_mc = np.multiply(
            a_s1_s2_mc,
            float(scale_par) * len(self.a_data_s1) /
            float(self.num_mc_events * self.num_repetitions))

        #'ml'
        if draw_fit:

            f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)

            ax1.set_xlabel('S1 [PE]')
            ax1.set_ylabel('log(S2/S1)')
            ax2.set_xlabel('S1 [PE]')
            ax2.set_ylabel('log(S2/S1)')

            s1_s2_data_plot = np.rot90(self.d_coincidence_data_information[
                self.l_cathode_settings_in_use[0]][degree_setting]
                                       ['a_log_s2_s1'])
            s1_s2_data_plot = np.flipud(s1_s2_data_plot)
            ax1.pcolormesh(self.a_s1_bin_edges, self.a_log_bin_edges,
                           s1_s2_data_plot)

            s1_s2_mc_plot = np.rot90(a_s1_s2_mc)
            s1_s2_mc_plot = np.flipud(s1_s2_mc_plot)
            #print self.l_s1_settings
            #print self.l_log_settings
            #print self.d_coincidence_data_information[self.l_cathode_settings_in_use[0]][self.l_degree_settings_in_use[0]]['a_log_s2_s1'].shape
            #print a_s1_s2_mc.shape
            #print s1_s2_data_plot.shape
            #print s1_s2_mc_plot.shape
            ax2.pcolormesh(self.a_s1_bin_edges, self.a_log_bin_edges,
                           s1_s2_mc_plot)
            #plt.colorbar()

            c1 = Canvas(1400, 400)
            c1.Divide(2)

            h_s1_data = Hist(*self.l_s1_settings, name='hS1_draw_data')
            root_numpy.array2hist(
                self.d_coincidence_data_information[
                    self.l_cathode_settings_in_use[0]][degree_setting]
                ['a_log_s2_s1'].sum(axis=1), h_s1_data)

            hS1MC = Hist(*self.l_s1_settings, name='hS1_draw_mc')
            root_numpy.array2hist(a_s1_s2_mc.sum(axis=1), hS1MC)

            s1_scale_factor = h_s1_data.Integral() / float(hS1MC.Integral())

            g_s1_data = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                h_s1_data)
            g_s1_mc = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                hS1MC, scale=s1_scale_factor)

            g_s1_mc.SetFillColor(root.kBlue)
            g_s1_mc.SetMarkerColor(root.kBlue)
            g_s1_mc.SetLineColor(root.kBlue)
            g_s1_mc.SetFillStyle(3005)

            g_s1_data.SetTitle('S1 Comparison')
            g_s1_data.GetXaxis().SetTitle('S1 [PE]')
            g_s1_data.GetYaxis().SetTitle('Counts')

            g_s1_data.SetLineColor(root.kRed)
            g_s1_data.SetMarkerSize(0)
            g_s1_data.GetXaxis().SetRangeUser(self.l_s1_settings[1],
                                              self.l_s1_settings[2])
            g_s1_data.GetYaxis().SetRangeUser(
                0, 1.2 * max(h_s1_data.GetMaximum(), hS1MC.GetMaximum()))

            c1.cd(1)
            g_s1_data.Draw('ap')
            g_s1_mc.Draw('same')
            g_s1_mc_band = g_s1_mc.Clone()
            g_s1_mc_band.Draw('3 same')

            h_s2_data = Hist(*self.l_log_settings, name='h_s2_draw_data')
            root_numpy.array2hist(
                self.d_coincidence_data_information[
                    self.l_cathode_settings_in_use[0]][degree_setting]
                ['a_log_s2_s1'].sum(axis=0), h_s2_data)

            h_s2_mc = Hist(*self.l_log_settings, name='h_s2_draw_mc')
            root_numpy.array2hist(a_s1_s2_mc.sum(axis=0), h_s2_mc)

            s2_scale_factor = h_s2_data.Integral() / float(h_s2_mc.Integral())

            g_s2_data = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                h_s2_data)
            g_s2_mc = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                h_s2_mc, scale=s2_scale_factor)

            g_s2_mc.SetFillColor(root.kBlue)
            g_s2_mc.SetMarkerColor(root.kBlue)
            g_s2_mc.SetLineColor(root.kBlue)
            g_s2_mc.SetFillStyle(3005)

            g_s2_data.SetTitle('Log(S2/S1) Comparison')
            g_s2_data.GetXaxis().SetTitle('Log(S2/S1)')
            g_s2_data.GetYaxis().SetTitle('Counts')

            g_s2_data.SetLineColor(root.kRed)
            g_s2_data.SetMarkerSize(0)
            g_s2_data.GetXaxis().SetRangeUser(self.l_log_settings[1],
                                              self.l_log_settings[2])
            g_s2_data.GetYaxis().SetRangeUser(
                0, 1.2 * max(h_s2_data.GetMaximum(), h_s2_mc.GetMaximum()))

            c1.cd(2)
            g_s2_data.Draw('ap')
            g_s2_mc.Draw('same')
            g_s2_mc_band = g_s2_mc.Clone()
            g_s2_mc_band.Draw('3 same')

            c1.Update()

            neriX_analysis.save_plot(['temp_results'],
                                     c1,
                                     '%d_deg_1d_hists' % (degree_setting),
                                     batch_mode=True)
            f.savefig('./temp_results/%d_deg_2d_hist.png' % (degree_setting))

        flat_s1_s2_data = np.asarray(self.a_data_hist_s1_s2.flatten(),
                                     dtype=np.float32)
        flat_s1_s2_mc = np.asarray(a_s1_s2_mc.flatten(), dtype=np.float32)
        logLikelihoodMatching = c_log_likelihood(
            flat_s1_s2_data, flat_s1_s2_mc, len(flat_s1_s2_data),
            int(self.num_mc_events * self.num_repetitions), 0.95)

        #print prior_ln_likelihood
        #print logLikelihoodMatching
        #print max(flat_s1_s2_data)
        #print max(flat_s1_s2_mc)
        #print '\n\n'

        if np.isnan(logLikelihoodMatching):
            return -np.inf
        else:
            matching_ln_likelihood += logLikelihoodMatching

        if self.b_suppress_likelihood:
            matching_ln_likelihood /= self.ll_suppression_factor

        total_ln_likelihood = prior_ln_likelihood + matching_ln_likelihood
        #print total_ln_likelihood

        if np.isnan(total_ln_likelihood):
            return -np.inf
        else:
            return total_ln_likelihood
Ejemplo n.º 6
0
            i, modules_hist[j].GetBinContent(i) / args.bw)
        modules_hist[j].SetBinError(i,
                                    modules_hist[j].GetBinError(i) / args.bw)
print ' - drawing ... '
y_max = 0
for i in xrange(25):
    if modules_hist[i].GetMaximum() > y_max:
        y_max = modules_hist[i].GetMaximum()
for i in xrange(25):
    modules_hist[i].SetMaximum(y_max * 1.1)

canvas_trigger = Canvas(1000,
                        800,
                        name="canvas_trigger",
                        title="rate of event trigger")
canvas_trigger.ToggleEventStatus()
canvas_trigger.cd()
trigger_hist.Draw('EH')
canvas_modules = Canvas(1500,
                        1000,
                        name="canvas_modules",
                        title="rate of 25 modules")
canvas_modules.ToggleEventStatus()
canvas_modules.Divide(5, 5)
for i in xrange(25):
    canvas_modules.cd(5 * (i % 5) + i / 5 + 1)
    modules_hist[i].Draw('EH')

sci_trigger_r_obj.close_file()
wait(True)
Ejemplo n.º 7
0
def makeComparisionPage(histodicts, fileNames, fileDescr, separateFiles):
    """
    Prepares a canvas comparing multiple histograms: plotting all in one pad and their ratios in the second
    """
    import rootpy
    from rootpy.plotting import Hist, Canvas, Legend
    import ROOT
    from ROOT import gPad
    log = logging.getLogger('pyroplot')
    cans = {}
    colors = [
        ROOT.kBlue, ROOT.kRed + 1, ROOT.kViolet - 1, ROOT.kOrange + 7,
        ROOT.kGreen - 7, ROOT.kOrange - 6, ROOT.kPink - 9, ROOT.kTeal - 6,
        ROOT.kBlue + 4, ROOT.kAzure + 2
    ]
    log.info("Drawing histograms ..")
    # prepare set of histograms to compare to the reference on (the first)
    # loop over the reference set of histos (sorted by key):
    for hidx, refname in enumerate(sorted(histodicts[0].keys())):
        # prepare canvas
        if separateFiles:
            log.debug("Creating new canvas with index %d." % (hidx))
            c = Canvas(600, 270)
            cans[refname] = c
            c.Divide(3, 1)
            c.cd(1)
        if not separateFiles and (hidx) % 4 == 0:
            log.debug("Creating new canvas with index %d." % (hidx / 3))
            # start a new canvas
            c = Canvas(600, 800)
            cans[refname] = c
            c.Divide(3, 4)
        # prepare histograms for drawing
        log.debug("Drawing histogram #" + str(hidx + 1) + " (" + refname +
                  ") on canvas #" + str(len(cans)))
        hists = []
        ratiohists = []
        hiter = iter(histodicts)
        # special treatment for tprofile: prepare the reference projection for the ratio
        if histodicts[0][refname].__class__.__name__ == "Profile":
            refProj = histodicts[0][refname].ProjectionX()
            refProj.SetName("reference_proj")
        for idx, h in enumerate(hiter):
            # make sure we have this histogram loaded:
            if not refname in h:
                continue
            # access the corresponding histogram of the other files at the same hidx as used for ref
            h[refname].color = colors[idx]
            h[refname].linestyle = idx
            hists.append(h[refname])
            # prepare ratio is this is not the first (i.e. reference) histogram
            if idx:
                # special treatment for TProfile:
                if h[refname].__class__.__name__ == "Profile":
                    myratio = Hist(h[refname].nbins(), h[refname].lowerbound(),
                                   h[refname].upperbound())  #dummy hist
                    myratioproj = h[refname].ProjectionX()
                    myratioproj.SetName("cmp_hist_proj" + str(idx))
                    try:
                        myratio.divide(myratioproj, refProj)
                    except rootpy.ROOTError, e:
                        log.error(
                            "Calculation of ratio for histogram %s caused ROOTError exception ('%s')"
                            % (h[refname].GetName(), e.msg))
                        break
                    myratio.color = colors[idx]
                else:
                    myratio = h[refname].clone(
                    )  # make sure that the ratio has the right type
                    try:
                        myratio.Divide(
                            h[refname],
                            histodicts[0][refname])  # divide by reference hist
                    except rootpy.ROOTError, e:
                        log.error(
                            "Calculation of ratio for histogram %s caused ROOTError exception ('%s')"
                            % (h[refname].GetName(), e.msg))
                        break
                myratio.yaxis.SetTitle("(h_{cmp} - h_{ref})/h_{ref}")
                myratio.SetTitle("ratio to reference")
                myratio.SetMaximum(2)
                myratio.SetMinimum(0)
                myratio.SetStats(0)
                ratiohists.append(myratio)