Example #1
0
def ATLAS_2017_I1519428(in_file, out_file):
    """
    Downloaded table:
        1. change indexes [0-8] to [1-9]
        2. provide another output that contains data only i.e. y01
        3. divide luminosity, lumi = 37 fb^{-1} = 37 x 10^{12} mb^{-1}.
    """
    data = yoda.read(in_file)
    lumi = 3.7e4 # in unit of 1/pb
    lumi_err = 0.032*lumi
    new_rivet = []
    new_ref = []
    for key, value in data.iteritems():

        # change Key
        index = int(re.search('d0(.+?)-x', key).group(1))
        new_key = key.replace('d0{}'.format(index), 'd0{}'.format(index+1))
        value.setAnnotation('Path', new_key)

        if index < 2:
            # scale to cross section of di-jet distributions

            # scale each bin only for 
            for point in value.points:
                bin_width = point.xErrs.minus + point.xErrs.plus
                if abs(point.yErrs.minus) < 1E-5:
                    # Use poisson error for observed events
                    y_err = math.sqrt(point.y)

                    # if y-axis is zero, err = 0
                    # else: error propagation
                    if abs(point.y) < 1E-5:
                        xs_err = 0.
                    else:
                        xs_err = point.y/lumi * math.sqrt(1/point.y + lumi_err**2/lumi**2)

                    xs_err /= bin_width
                    point.yErrs = (xs_err, xs_err)
                else:
                    point.yErrs = (point.yErrs.minus/lumi, point.yErrs.plus/lumi)

                point.y = point.y / lumi / bin_width
        else:
            pass
            # scale bin_width for chi2 distributions
            #for p in value.points:
            #    bin_width = p.xErrs.minus + p.xErrs.plus
            #    p.y = p.y / bin_width

        new_rivet.append(value)

        if "y01" in key:
            new_ref.append(value)

    yoda.write(new_rivet, out_file)
    yoda.write(new_ref, "ATLAS_2017_I1519428_ref.yoda")
Example #2
0
def main(arg):
	infile = arg
	outfile = arg[:-5] + '_scatter.yoda'
	aos = yoda.read(infile)
    
	scatter = []
	hs = [h for h in aos.values()]
	for h in hs:
		s = h.mkScatter()
		scatter.append(s)

	yoda.write(scatter, outfile)
	sys.exit(0)
Example #3
0
def ATLAS_2014_I1268975(in_file, out_file):
    data = yoda.read(in_file)
    new_data = []
    pre_fix = '/REF/ATLAS_2014_I1268975'
    for i in range(2):
        # two jet alg. atkt4 and atkt6
        for j in range(6):
            # six y* bins
            index = "{}/d{:0=2}-x01-y0{}".format(pre_fix, i+1, j+1)
            ref_index_number = i*6 + j + 1
            ref_index = "{}/d{:0=2}-x01-y01".format(pre_fix, ref_index_number)
            try:
                scatter2D = data[ref_index]
            except KeyError:
                print(ref_index,"not found")
                continue

            scatter2D.setAnnotation('Path', index)
            new_data.append(scatter2D)

    yoda.write(new_data, out_file)
Example #4
0
def envelope2YODA(fvals,
                  fout_up="envelope_up.yoda",
                  fout_dn="envelope_dn.yoda",
                  wfile=None):
    import apprentice as app
    vals = app.AppSet(fvals)

    Yup = np.array([r.vmax for r in vals._RA])
    Ydn = np.array([r.vmin for r in vals._RA])
    dY = np.zeros_like(Yup)
    hids = np.array([b.split("#")[0] for b in vals._binids])
    hnames = sorted(set(hids))
    observables = sorted(
        [x for x in set(app.io.readObs(wfile))
         if x in hnames]) if wfile is not None else hnames

    with open(fvals) as f:
        import json
        rd = json.load(f)
        xmin = np.array(rd["__xmin"])
        xmax = np.array(rd["__xmax"])

    DX = (xmax - xmin) * 0.5
    X = xmin + DX
    Y2Dup, Y2Ddn = [], []
    import yoda
    for obs in observables:
        idx = np.where(hids == obs)
        P2Dup = [
            yoda.Point2D(x, y, dx, dy)
            for x, y, dx, dy in zip(X[idx], Yup[idx], DX[idx], dY[idx])
        ]
        Y2Dup.append(yoda.Scatter2D(P2Dup, obs, obs))
        P2Ddn = [
            yoda.Point2D(x, y, dx, dy)
            for x, y, dx, dy in zip(X[idx], Ydn[idx], DX[idx], dY[idx])
        ]
        Y2Ddn.append(yoda.Scatter2D(P2Ddn, obs, obs))
    yoda.write(Y2Dup, fout_up)
    yoda.write(Y2Ddn, fout_dn)
Example #5
0
def prediction2YODA(fvals,
                    Peval,
                    fout="predictions.yoda",
                    ferrs=None,
                    wfile=None):
    import apprentice as app
    vals = app.AppSet(fvals)
    errs = app.AppSet(ferrs) if ferrs is not None else None

    P = [Peval[x] for x in vals._SCLR.pnames] if type(Peval) == dict else Peval

    Y = vals.vals(P)
    dY = errs.vals(P) if errs is not None else np.zeros_like(Y)

    hids = np.array([b.split("#")[0] for b in vals._binids])
    hnames = sorted(set(hids))
    observables = sorted(
        [x for x in set(app.io.readObs(wfile))
         if x in hnames]) if wfile is not None else hnames

    with open(fvals) as f:
        import json
        rd = json.load(f)
        xmin = np.array(rd["__xmin"])
        xmax = np.array(rd["__xmax"])

    DX = (xmax - xmin) * 0.5
    X = xmin + DX
    Y2D = []
    import yoda
    for obs in observables:
        idx = np.where(hids == obs)
        P2D = [
            yoda.Point2D(x, y, dx, dy)
            for x, y, dx, dy in zip(X[idx], Y[idx], DX[idx], dY[idx])
        ]
        Y2D.append(yoda.Scatter2D(P2D, obs, obs))
    yoda.write(Y2D, fout)
Example #6
0
def ATLAS_2017_I1635274(in_file, out_file):
    """
    Divided out the bin-width for jet pT distributions
    add Poisson error on data
    """
    data = yoda.read(in_file)
    new_rivet = []

    scaled_by_bin_width = ['d01', 'd03', 'd05', 'd07', 'd09']
    to_be_scaled_by_bin_width = ['d02', 'd04', 'd06', 'd08', 'd10', 'd11']

    for key, value in data.iteritems():
        short_key = key[25:28]
        # print(key, short_key)

        for p in value.points:
            bin_width = p.xErrs.minus + p.xErrs.plus
            if abs(p.yErrs.minus) < 1E-5:
                if short_key in scaled_by_bin_width:
                    ## such distributions have already scaled by bin-width
                    y_err = math.sqrt(p.y*bin_width)/bin_width
                else:
                    y_err = math.sqrt(p.y)
                p.yErrs = (y_err, y_err)
            else:
                p.yErrs = (p.yErrs.minus, p.yErrs.plus)

        if short_key in to_be_scaled_by_bin_width:
            # scale by bin-width
            for p in value.points:
                bin_width = p.xErrs.minus + p.xErrs.plus
                p.yErrs = (p.yErrs.minus/bin_width, p.yErrs.plus/bin_width)
                p.y = p.y / bin_width

        new_rivet.append(value)
    yoda.write(new_rivet, out_file)
#


import sys,re,yoda,os,math

if len(sys.argv) != 4:
    sys.exit("Usage: produce-Qdependence-avg.py generator level output_file")

# open the files
gen = sys.argv[1]
level = sys.argv[2]

observables=["GA_10_05_R6", "GA_10_10_R6", "GA_10_20_R6"]
flavours=["uu", "gg"]

scatters=[]
for flavour in flavours:
    for observable in observables:
        separation_scatter = yoda.Scatter2D(title=observable, path="/Qdependence/"+flavour+"_"+observable)
        for Q in [50,100,200,400,800]:
            hname="/MC_LHQG_EE/"+observable
            dict_all=yoda.read(gen+"/"+level+"/"+flavour+"-"+str(Q)+".yoda", True)
            Qmin=Q/math.sqrt(2.0)
            Qmax=Q*math.sqrt(2.0)
            separation_scatter.addPoint(Q, dict_all[hname].xMean(), xerrs=[Q-Qmin,Qmax-Q])
            # add that for stddev as errorbars: , yerrs=dict_all[hname].xStdDev())
        scatters.append(separation_scatter)

yoda.write(scatters,sys.argv[3])

Example #8
0
import yoda

aosref=yoda.read("emubB_homemade.yoda")
aosHalf=yoda.read("emubB_homemade0.5.yoda")
aosDouble=yoda.read("emubB_homemade2.0.yoda")

aos = [ ao for ao in yoda.read("emubB_homemade.yoda", asdict=False) ]
aosHalf = [ao for ao in yoda.read("emubB_homemade0.5.yoda", asdict=False)]
aosDouble = [ao for ao in yoda.read("emubB_homemade2.0.yoda", asdict=False)]

result = [ ao.mkScatter() for ao in yoda.read("emubB_homemade.yoda", asdict=False)]

for i in range(0, len(aos)-3):
	for p in range(0, aos[i].numBins):
		Half=aosHalf[i].bins[p].sumW
		Double=aosDouble[i].bins[p].sumW

		errP = max(Half, Double) - aos[i].bins[p].sumW
		errM = aos[i].bins[p].sumW - min(Half, Double)
		result[i].points[p].y = aos[i].bins[p].sumW
		result[i].points[p].yErrs = [max(errM, 0), max(errP, 0)]

yoda.write(result, "emubB_homemade_scales.yoda")
Example #9
0
# filename info
base = sys.argv[1]
output_filename = base+".yoda"
sys.stdout.write("Processing: " + output_filename + "\n")

# the R and beta values we shall loop over
Rvalues=["02", "04", "06", "08", "10"]
betas=["05", "10", "20"]

# an array for all the scatter plots
yoda_histograms=[]

# loop over the beta and R values
for beta in betas:
    for Rval in Rvalues:
        # construct the input filename and open it
        input_filename  = base+"-beta"+beta+"-R"+Rval+".txt"
        f = open(input_filename, 'r')

        # declare the yoda 2D scatter
        yoda_histo = yoda.Histo1D(500, 0.0, 1.0, path="/MC_LHQG_EE/GA_10_"+beta+"_R"+str(int(Rval)))

        # parse the input file and construct the scatter plot
        for line in f:
            tokens=line.split()
            yoda_histo.fill((float(tokens[0]) + float(tokens[1]))/2.0, float(tokens[2])*(float(tokens[1])-float(tokens[0])))

        yoda_histograms.append(yoda_histo)

yoda.write(yoda_histograms, output_filename)
Example #10
0
import sys, re, yoda, os

if len(sys.argv) != 3:
    sys.exit(
        "Usage: produce-Rdependence-data.py  separation_table output_file")

# open the files
table = sys.argv[1]

measures = ["grej20", "grej50", "qrej20", "qrej50", "srej", "I", "I2"]
Rvalues = [2, 4, 6, 8, 10]
observables = ["GA_00_00", "GA_20_00", "GA_10_05", "GA_10_10", "GA_10_20"]

scatters = []
for measure in measures:
    for observable in observables:
        separation_scatter = yoda.Scatter2D(title=measure,
                                            path="/Rdependence/" + measure +
                                            "_" + observable)
        for Rval in Rvalues:
            command = "./get-separation.sh " + table + " " + observable + "_R" + str(
                Rval) + " " + measure
            separation_scatter.addPoint(0.1 * Rval,
                                        float(
                                            os.popen(command).read().rstrip()),
                                        xerrs=0.1)
        scatters.append(separation_scatter)

yoda.write(scatters, sys.argv[2])
##add axis labels by default


opts, args = op.parse_args()
name = os.path.splitext(args[0])

aodict = yoda.core.read(args[0], True)

yfiledict = {}

label = re.compile(r"quark_|gluon_|unlabelled_")

for k, ao in aodict.iteritems():
    m = label.search(k)
    if m:
        lab = m.group(0)[:-1]
    else:
        continue

    ao.path = label.sub("", ao.path)
    if lab in yfiledict:
        yfiledict[lab].append(ao)
    else:
        yfiledict[lab] = [ao]

    continue

# loop over rho, aos
for lab, aos in yfiledict.iteritems():
    yoda.write(aos, "%s.yoda" % lab)
                sumerrplus += tmppoint.yErrs[1]
            point.yErrs = (sumerrminus, sumerrplus)
        elif action=="quaderrsum":
            sum = 0.0
            sumerr2minus = 0.0
            sumerr2plus = 0.0
            for scatter in scatters:
                tmppoint = scatter.points[i];
                sum += tmppoint.y
                sumerr2minus += tmppoint.yErrs[0]**2
                sumerr2plus += tmppoint.yErrs[1]**2
            point.yErrs = (sqrt(sumerr2minus), sqrt(sumerr2plus))
        elif action=="envelope":
            upper = float('-inf')
            lower = float('inf')
            for scatter in scatters:
                val = scatter.points[i].y;
                if val > upper:
                    upper = val
                if val < lower:
                    lower = val
            point.yErrs = (point.y-lower, upper-point.y)
        elif action=="transfererrors":
            point.y = scatters[0].points[i].y
            point.yErrs = scatters[1].points[i].yErrs
        else:
            print "Unknown action: ",action
            exit(1)

yoda.write(scatters_out.values(), opts.OUTPUT_FILE)
Example #13
0
##add args to name plot so we have meaningful legends later
##add axis labels by default

opts, args = op.parse_args()
name = os.path.splitext(args[0])

aodict = yoda.core.read(args[0], True)

yfiledict = {}

label = re.compile(r"quark_|gluon_|unlabelled_")

for k, ao in aodict.iteritems():
    m = label.search(k)
    if m:
        lab = m.group(0)[:-1]
    else:
        continue

    ao.path = label.sub("", ao.path)
    if lab in yfiledict:
        yfiledict[lab].append(ao)
    else:
        yfiledict[lab] = [ao]

    continue

# loop over rho, aos
for lab, aos in yfiledict.iteritems():
    yoda.write(aos, "%s.yoda" % lab)
Example #14
0
    def prepare(self, irun):
        """prepare input files for each job"""

        try:
            detector_out = self.tune.update_detector(irun,
                                                     self.detector_hist2D)
            prof_out = self.tune.get_tune(irun)
            pythia_out = self.tune.get_config(irun)
        except IndexError:
            return False

        folder = self.workdir(irun)
        new_irun = irun
        tune_output = os.path.join(folder, "used_params")
        while os.path.exists(folder):
            try:
                with open(tune_output) as f:
                    c = ""
                    for line in f:
                        c += line

                    if prof_out == c:
                        #n_yodas = len(glob.glob(folder+"/out_*.yoda"))
                        n_yodas = len([
                            x for x in glob.glob(folder + "/*.yoda")
                            if "detector" not in x
                        ])
                        if n_yodas > 0:
                            # print new_irun," is already in", folder, n_yodas
                            return False
                        else:
                            break
                    #else:
                    #    print "directory is there, but input is changed"
            except IOError:
                break

            new_irun += 1
            folder = self.workdir(new_irun)
            tune_output = os.path.join(folder, "used_params")

        subprocess.call(['mkdir', '-p', folder])
        with open(tune_output, 'w') as f:
            f.write(prof_out)

        # write global configurations
        out = "Random:setSeed           = on     ! user-set seed\n"
        out += "Main:numberOfEvents     = {}\n".format(str(self.nEventsPerJob))
        out += "Main:timesAllowErrors   = {}\n".format(
            str(max(int(self.nEventsPerJob * 0.002), 1)))
        out += "Next:numberCount        = 10000  ! \n"
        out += '\n'

        # additional options in jason input
        if self.pythia_opt:
            out += '\n'.join(self.pythia_opt)
            out += '\n'

        out += pythia_out + '\n'
        pythia_config_output = folder + "/tune_parameters.cmnd"
        with open(pythia_config_output, 'w') as f:
            f.write(out)

        with open(os.path.join(folder, 'runPythia.cmd'), 'w') as f:
            with open(self.process) as pp:
                f.write(pp.read())
            f.write(out)

        # for value in self.detector_hist2D.values():
        #     for ib, b in enumerate(value.bins):
        #         print ib, b.volume
        detector_output = folder + "/detector_config.yoda"
        yoda.write(detector_out, detector_output)
        del detector_out

        self.submit_folder = folder
        return True
    #"EWWZ_ATLAS_Sherpa_222_NNPDF30NNLO.v1.yoda",
    "EWWZ_ATLAS_Sherpa.yoda",
    "EWWZ_ATLAS_MGPy8_full.yoda",
    #"WZJJ_EW_MG_LHConfig_MaxPtJScale.yoda"
    #"WZJJ_EW_CMSMGSample_LooseFiducial.yoda",
    #"WZJJ_EW_MG_LHConfig_LooseFiducial.yoda",
    #"WZJJ_EW_LHConfig_powheg-pythia8_LooseFiducial.yoda",
]
#"WmZTo1E1Nu2Mu_FixedScaleMW_LHConfig_VBFNLO-Pythia8.yoda",
binning = {
    "Mass3l": [4],
    "dEtajj": [2],
    "mjj": [5],
    "zep3l": [2],
    "zepj3": [2],
    "dRjj": [2],
}

for filename in filenames:
    fullname = "/".join([data_path, filename])
    hists = yoda.read(fullname)
    for name, hist in hists.iteritems():
        name = name.split("/")[-1]
        if not type(hist) is yoda.Histo1D:
            continue
        key = name.split("_")[-1]
        if key not in binning.keys():
            continue
        hist.rebin(*binning[key])
    yoda.write(hists, fullname.replace(".yoda", "_REBIN.yoda"))
Example #16
0
import yoda
import math

fi=yoda.read("Rivet.yoda", asdict=False)

aos = []
for h in fi:
    if h.annotation("Type") == "Histo1D":
	s=yoda.Scatter2D()

	for a in h.annotations:
	  s.setAnnotation(a, h.annotation(a))
	s.setAnnotation("Type", "Scatter2D")

	for b in h.bins:
	  x = b.xMid
	  ex = x - b.xMin
	  y = b.height
	  ey = b.heightErr

	  s.addPoint(x,y,ex,ey)
	  
        aos.append(s)
    else: aos.append(h)

print aos
yoda.write(aos, "Rivet_scatter.yoda")
Example #17
0
import yoda
import math

aos = [ ao for ao in yoda.read("WWbBPOWHEG.yoda", asdict=False) ]
result = [ ao.mkScatter() for ao in yoda.read("WWbBPOWHEG.yoda", asdict=False)]

for i in range(0, len(aos)-3):
	for p in range(0, aos[i].numBins):
		result[i].points[p].y = aos[i].bins[p].sumW
		result[i].points[p].yErrs = [math.sqrt(aos[i].bins[p].sumW2), math.sqrt(aos[i].bins[p].sumW2)]

yoda.write(result, "WWbBPOWHEGScatter.yoda")