def UCNyield(x, p):
    scale = p[0] if x[0] >= 0 else p[5]
    T = UCN.HeTemperature(abs(x[0]))
    tau1 = 1. / (1. / p[1] + p[2] * (1. - p[3] * T) * T**7)
    tau2 = 1. / (1. + p[4] * T**7)
    #  print(tau1, tau2)
    if tau1 <= 0 or tau2 <= 0:
        return 0.
    return scale * tau1 * tau2 * (
        1. - math.exp(-x[2] * p[6] / tau1)) * math.exp(-x[1] / tau1)
def TauVsTemp(experiments,
              parameter,
              variable,
              color=ROOT.kBlack,
              convert=False):
    exs = [ex for ex in experiments if ex[variable] > 0.]
    if not convert:
        x = numpy.array([
            (max(ex['max' + parameter]) + min(ex['min' + parameter])) / 2
            for ex in exs
        ])
        xerr = numpy.array([
            (max(ex['max' + parameter]) - min(ex['min' + parameter])) / 2
            for ex in exs
        ])
    else:
        x = numpy.array([(UCN.HeTemperature(max(ex['max' + parameter])) +
                          UCN.HeTemperature(min(ex['min' + parameter]))) / 2
                         for ex in exs])
        xerr = numpy.array([(UCN.HeTemperature(max(ex['max' + parameter])) -
                             UCN.HeTemperature(min(ex['min' + parameter]))) / 2
                            for ex in exs])
    gr = ROOT.TGraphErrors(len(exs), x,
                           numpy.array([ex[variable] for ex in exs]), xerr,
                           numpy.array([ex[variable + 'err'] for ex in exs]))
    gr.SetTitle('')

    if parameter == 'temperature':
        gr.GetXaxis().SetTitle('Temperature (K)')
    elif parameter == 'vaporpressure' and not convert:
        gr.GetXaxis().SetTitle('Vapor pressure (Torr)')
    elif parameter == 'vaporpressure' and convert:
        gr.GetXaxis().SetTitle('Temperature (K)')
    else:
        assert (True)
    gr.GetYaxis().SetTitle('Storage lifetime (s)')
    gr.SetLineColor(color)
    gr.SetMarkerColor(color)
    return gr
mg.Add(TauVsTemp(beforefilling, 'temperature', 'tau',
                 ROOT.kRed))  # before refill
mg.Add(TauVsTemp(spoiling, 'temperature', 'tau',
                 ROOT.kGreen))  # source spoiling
mg.Draw('AP')
canvas.Print('tauvstemp.pdf(')

canvas.SetLogx()
mg = ROOT.TMultiGraph('mg', ';Vapor pressure (Torr);Storage lifetime (s)')
mg.Add(TauVsTemp(initialgeometry, 'vaporpressure', 'tau', ROOT.kBlack))
mg.Add(TauVsTemp(finalgeometry, 'vaporpressure', 'tau', ROOT.kBlack))
mg.Add(TauVsTemp(afterfilling, 'vaporpressure', 'tau', ROOT.kBlue))
mg.Add(TauVsTemp(beforefilling, 'vaporpressure', 'tau', ROOT.kRed))
mg.Add(TauVsTemp(spoiling, 'vaporpressure', 'tau', ROOT.kGreen))
mg.Draw('AP')
fHeTemperature = ROOT.TF1('HeTemperature', lambda x: UCN.HeTemperature(x[0]),
                          UCN.HeTemperature(mg.GetXaxis().GetXmin()),
                          UCN.HeTemperature(mg.GetXaxis().GetXmax()))
Taxis = ROOT.TGaxis(mg.GetXaxis().GetXmin(),
                    mg.GetHistogram().GetMaximum(),
                    mg.GetXaxis().GetXmax(),
                    mg.GetHistogram().GetMaximum(), 'HeTemperature', 510, '-')
FormatTaxis(Taxis)
Taxis.Draw()
canvas.Print('tauvstemp.pdf')

mg = ROOT.TMultiGraph('mg', ';Vapor pressure (Torr);Wall-storage lifetime (s)')
mg.Add(TauVsTemp(initialgeometry, 'vaporpressure', 'tau_wall', ROOT.kBlack))
mg.Add(TauVsTemp(finalgeometry, 'vaporpressure', 'tau_wall', ROOT.kBlack))
mg.Add(TauVsTemp(afterfilling, 'vaporpressure', 'tau_wall', ROOT.kBlue))
mg.Add(TauVsTemp(beforefilling, 'vaporpressure', 'tau_wall', ROOT.kRed))