Exemple #1
0
def compareShapes(h1,h2,outFile,xTitle="",yTitle="",yRange=[],xRange=[],log=False,label1="",label2="",labelR="Data/MC",data=False):
    hRpf_mj = createRatio(h1,h2)
    hRatio = []
    hRatioErrors = []
    hDataErrors = []
    hData = []
    hDataCentres = []
    for i in range(1,hRpf_mj.GetNbinsX()+1):
        hRatio.append(hRpf_mj.GetBinContent(i))
        hRatioErrors.append(hRpf_mj.GetBinError(i))
    for i in range(1,h2.GetNbinsX()+1):
        hData.append(h2.GetBinContent(i))
        hDataCentres.append(h2.GetBinCenter(i))
        hDataErrors.append(h2.GetBinError(i))
    #numpy histograms
    h1, h1Edges = hist2array(h1,return_edges=True)
    h2, h2Edges = hist2array(h2,return_edges=True)
    hRatio = np.asarray(hRatio)
    hRatioErrors = np.asarray(hRatioErrors)
    centresRatio = (h1Edges[0][:-1] + h1Edges[0][1:])/2.

    plt.style.use([hep.style.CMS])
    f, axs = plt.subplots(2,1, sharex=True, sharey=False,gridspec_kw={'height_ratios': [4, 1],'hspace': 0.05})
    axs = axs.flatten()
    plt.sca(axs[0])
    if(log):
        axs[0].set_yscale("log")
    hep.histplot(h1,h1Edges[0],stack=False,ax=axs[0],label = label1, histtype="step",color="r")

    if(label2=="data_obs"):
        print("Plotting data")
        plt.errorbar(hDataCentres,hData, yerr=hDataErrors, fmt='o',color="k",label = label2)
    else:
        hep.histplot(h2,h2Edges[0],stack=False,ax=axs[0],label = label2, histtype="step",color="k")
    plt.ylabel(yTitle, horizontalalignment='right', y=1.0)
    axs[0].legend()
    axs[1].set_xlabel(xTitle)
    #axs[0].set_ylabel(yTitle)
    axs[1].set_ylabel(labelR)
    if(yRange):
        axs[0].set_ylim(yRange)
    if(xRange):
        axs[0].set_xlim(xRange)
    if data:
        hep.cms.text("WiP",loc=1)
        hep.cms.lumitext(text='59.8 $fb^{-1} (13 TeV)$', ax=axs[0], fontname=None, fontsize=None)
    else:
        hep.cms.lumitext(text='2018', ax=axs[0], fontname=None, fontsize=None)
        hep.cms.text("Simulation WiP",loc=1)
    plt.legend(loc="best")#loc = (0.4,0.2))

    plt.sca(axs[1])#switch to lower pad
    axs[1].axhline(y=1.0, xmin=0, xmax=1, color="r")
    axs[1].set_ylim([0.0,2.0])
    plt.xlabel(xTitle, horizontalalignment='right', x=1.0)

    plt.errorbar(centresRatio,hRatio, yerr=hRatioErrors, fmt='o',color="k")    

    print("Saving {0}".format(outFile))
    plt.savefig(outFile)
Exemple #2
0
def check_array2hist(hist):
    shape = np.array([hist.GetNbinsX(), hist.GetNbinsY(), hist.GetNbinsZ()])
    shape = shape[:hist.GetDimension()]
    arr = RNG.randint(0, 10, size=shape)
    rnp.array2hist(arr, hist)
    arr_hist = rnp.hist2array(hist)
    assert_array_equal(arr_hist, arr)

    shape_overflow = shape + 2
    arr_overflow = RNG.randint(0, 10, size=shape_overflow)
    hist_overflow = hist.Clone()
    hist_overflow.Reset()
    rnp.array2hist(arr_overflow, hist_overflow)
    arr_hist_overflow = rnp.hist2array(hist_overflow, include_overflow=True)
    assert_array_equal(arr_hist_overflow, arr_overflow)

    if len(shape) == 1:
        return

    # overflow not specified on all axes
    arr_overflow2 = arr_overflow[1:-1]
    hist_overflow2 = hist.Clone()
    hist_overflow2.Reset()
    rnp.array2hist(arr_overflow2, hist_overflow2)
    arr_hist_overflow2 = rnp.hist2array(hist_overflow2, include_overflow=True)
    assert_array_equal(arr_hist_overflow2[1:-1], arr_overflow2)
Exemple #3
0
def getMiniGroupHists(lpgbt_hists, minigroups_swap, root=False):

    minigroup_hists = []

    minigroup_hists_inclusive = {}
    minigroup_hists_phi60 = {}

    for minigroup, lpgbts in minigroups_swap.items():

        inclusive = ROOT.TH1D(
            "minigroup_ROverZ_silicon_" + str(minigroup) + "_0", "", 42, 0.076,
            0.58)
        phi60 = ROOT.TH1D("minigroup_ROverZ_silicon_" + str(minigroup) + "_1",
                          "", 42, 0.076, 0.58)

        for lpgbt in lpgbts:

            inclusive.Add(lpgbt_hists[0][lpgbt])
            phi60.Add(lpgbt_hists[1][lpgbt])

        inclusive_array = hist2array(inclusive)
        phi60_array = hist2array(phi60)

        if (root):
            minigroup_hists_inclusive[minigroup] = inclusive
            minigroup_hists_phi60[minigroup] = phi60
        else:
            minigroup_hists_inclusive[minigroup] = inclusive_array
            minigroup_hists_phi60[minigroup] = phi60_array

    minigroup_hists.append(minigroup_hists_inclusive)
    minigroup_hists.append(minigroup_hists_phi60)

    return minigroup_hists
Exemple #4
0
def compare(shared_paths, pr_flat_dict, base_flat_dict, paths_to_save_in_pr, paths_to_save_in_base):
   # Collect paths that have to be written to both output files
   for path in shared_paths:
      pr_item = pr_flat_dict[path]
      base_item = base_flat_dict[path]

      if pr_item == None or base_item == None:
         continue

      are_different=False

      if pr_item.InheritsFrom('TProfile2D') and base_item.InheritsFrom('TProfile2D'):
         # Compare TProfile (content, entries and errors)
         are_different = not compare_TProfile(pr_item, base_item)
      
      elif pr_item.InheritsFrom('TProfile') and base_item.InheritsFrom('TProfile'):
         # Compare TProfile (content, entries and errors)
         are_different = not compare_TProfile(pr_item, base_item)

      elif pr_item.InheritsFrom('TH1') and base_item.InheritsFrom('TH1'):
         # Compare bin by bin
         pr_array = root_numpy.hist2array(hist=pr_item, include_overflow=True, copy=False)
         base_array = root_numpy.hist2array(hist=base_item, include_overflow=True, copy=False)

         if pr_array.shape != base_array.shape or not np.allclose(pr_array, base_array, equal_nan=True):
            are_different = True
      else:
         # Compare non histograms
         if pr_item != base_item:
            are_different = True

      if are_different:
         paths_to_save_in_pr.append(path)
         paths_to_save_in_base.append(path)
Exemple #5
0
def root_to_template(root_name,
                     file_name,
                     histogram_names=None,
                     metadata={
                         "version": "0.0",
                         "date": datetime.now().strftime('%Y%m%d_%H:%M:%S')
                     }):
    if not HAVE_ROOT:
        raise NotImplementedError("root_to_template requires ROOT, root_numpy")
    froot = rt.TFile(root_name)
    if histogram_names is None:
        histogram_names = []
        for k in froot.GetListOfKeys():
            if froot.Get(k.GetName()).InheritsFrom("TH1"):
                histogram_names.append(k.GetName())
    _, bins = root_numpy.hist2array(froot.Get(histogram_names[0]),
                                    return_edges=True)
    axis_names = get_root_hist_axis_labels(froot.Get(histogram_names[0]))
    histograms = []
    for histogram_name in histogram_names:
        histogram, _ = root_numpy.hist2array(froot.Get(histogram_name),
                                             return_edges=True)
        histograms.append(histogram)
    numpy_to_template(bins,
                      histograms,
                      file_name,
                      histogram_names=histogram_names,
                      axis_names=axis_names,
                      metadata=metadata)
Exemple #6
0
def check_array2hist(hist):
    shape = np.array([hist.GetNbinsX(), hist.GetNbinsY(), hist.GetNbinsZ()])
    shape = shape[:hist.GetDimension()]
    arr = RNG.randint(0, 10, size=shape)
    rnp.array2hist(arr, hist)
    arr_hist = rnp.hist2array(hist)
    assert_array_equal(arr_hist, arr)

    shape_overflow = shape + 2
    arr_overflow = RNG.randint(0, 10, size=shape_overflow)
    hist_overflow = hist.Clone()
    hist_overflow.Reset()
    rnp.array2hist(arr_overflow, hist_overflow)
    arr_hist_overflow = rnp.hist2array(hist_overflow, include_overflow=True)
    assert_array_equal(arr_hist_overflow, arr_overflow)

    if len(shape) == 1:
        return

    # overflow not specified on all axes
    arr_overflow2 = arr_overflow[1:-1]
    hist_overflow2 = hist.Clone()
    hist_overflow2.Reset()
    rnp.array2hist(arr_overflow2, hist_overflow2)
    arr_hist_overflow2 = rnp.hist2array(hist_overflow2, include_overflow=True)
    assert_array_equal(arr_hist_overflow2[1:-1], arr_overflow2)
    def correct_bbb(hist, vorig, funcname, normname=''):
        if vorig == 'mllVSmth_4x3':
            # 4 mth x 3 mll ([10, 40, 55, 210])
            vfolding = [range(i, i + 12, 3) for i in range(3)]
            vmapping = [0, 0, 1, 1, 2, 2]
        elif vorig == 'mllVSmth_3x3':
            # [10, 40, 70, 210]
            vfolding = [range(i, i + 9, 3) for i in range(3)]
            vmapping = [0, 0, 1, 1, 2, 2]
        elif vorig == 'mllVSmth_2x2':
            # [10, 50, 210]
            vfolding = [range(i, i + 4, 2) for i in range(2)]
            vmapping = [0, 0, 0, 1, 1, 1]

        postfit_full = common.make_roofit_histogram('postfit',
                                                    postfit_ws,
                                                    funcname,
                                                    normname=normname,
                                                    fold=vfolding)
        postfit_nobbb = common.make_roofit_histogram('prefit',
                                                     nobbb_ws,
                                                     funcname,
                                                     normname=normname,
                                                     fold=vfolding)

        cont = rnp.hist2array(hist, copy=False)
        cont *= (rnp.hist2array(postfit_full, copy=False) /
                 rnp.hist2array(postfit_nobbb, copy=False))[np.array(vmapping)]

        postfit_full.Delete()
        postfit_nobbb.Delete()
Exemple #8
0
def makeMCTruthDataset(data, etas, pts, masses):

    #add charge for mc truth events
    charge1 = np.ones_like(data['eta1'])
    charge2 = -np.ones_like(data['eta1'])

    datasetMCTruth = np.array([np.concatenate((data['eta1'],data['eta2']),axis=None),np.concatenate((charge1*data['mcpt1'],charge2*data['mcpt2']),axis=None),np.concatenate((data['res1'], data['res2']),axis=None)])
    histoMCTruth, edges = np.histogramdd(datasetMCTruth.T, bins = [etas,pts,masses])

    good_idx = np.nonzero(np.sum(histoMCTruth,axis=-1)>100.)
    histoMCTruth = histoMCTruth[good_idx]

    #compute mean in each bin (integrating over mass) for pt-dependent terms
    massesfull = np.array([masses[0],masses[-1]])
    histoden = np.histogramdd(datasetMCTruth.T, bins = [etas,pts,massesfull])[0][good_idx]

    k = 1./datasetMCTruth[1]
    eta = datasetMCTruth[0]
    sEta = np.sin(2*np.arctan(np.exp(-eta)))
    L = computeTrackLength(eta)

    fIn = ROOT.TFile.Open("resolutionMCtruth.root")
    bHisto = fIn.Get("b")
    dHisto = fIn.Get("d")

    bterm = hist2array(bHisto)
    dterm = hist2array(dHisto)

    terms_etas = np.linspace(-2.4, 2.4, bterm.shape[0]+1, dtype='float64')
    findBin = np.digitize(eta, terms_etas)-1
    b = bterm[findBin]
    d = dterm[findBin]

    terms = []
    terms.append(1./k)
    terms.append(np.abs(sEta*k))
    terms.append(1./np.square(k))
    terms.append(np.square(L))
    #terms.append(b*np.square(L)*np.reciprocal(1.+d/np.square(pt)/np.square(L)))
    terms.append(np.square(k))

    means = []
    for term in terms:
        histoterm = np.histogramdd(datasetMCTruth.T, bins = [etas,pts,massesfull], weights=term)[0][good_idx]
        ret = histoterm/histoden
        #remove spurious mass dimension
        ret = np.squeeze(ret, axis=-1)
        
        means.append(ret.flatten())
            
    mean = np.stack(means,axis=-1)
    
    pkg = {}
    pkg['dataset'] = histoMCTruth
    pkg['edges'] = edges
    pkg['binCenters'] = mean
    pkg['good_idx'] = good_idx

    return pkg
Exemple #9
0
def check_hist2array_THnSparse(hist):
    hist_thnsparse = ROOT.THnSparse.CreateSparse("", "", hist)
    array = rnp.hist2array(hist)
    array_thnsparse = rnp.hist2array(hist_thnsparse)
    # non-zero elements
    assert_true(np.any(array))
    # arrays should be identical
    assert_array_equal(array, array_thnsparse)
Exemple #10
0
def check_hist2array_THn(hist):
    hist_thn = ROOT.THn.CreateHn("", "", hist)
    array = rnp.hist2array(hist)
    array_thn = rnp.hist2array(hist_thn)
    # non-zero elements
    assert_true(np.any(array))
    # arrays should be identical
    assert_array_equal(array, array_thn)
Exemple #11
0
def check_hist2array_THn(hist):
    hist_thn = ROOT.THn.CreateHn("", "", hist)
    array = rnp.hist2array(hist)
    array_thn = rnp.hist2array(hist_thn)
    # non-zero elements
    assert_true(np.any(array))
    # arrays should be identical
    assert_array_equal(array, array_thn)
Exemple #12
0
def check_hist2array_THnSparse(hist):
    hist_thnsparse = ROOT.THnSparse.CreateSparse("", "", hist)
    array = rnp.hist2array(hist)
    array_thnsparse = rnp.hist2array(hist_thnsparse)
    # non-zero elements
    assert_true(np.any(array))
    # arrays should be identical
    assert_array_equal(array, array_thnsparse)
Exemple #13
0
def plotSidebandsMJY_mpl(h_3d,outFile,xTitle="",yTitle="",yRange=[],xRange=[],log=False):
    nBinsX = h_3d.GetNbinsX()
    nBinsY = h_3d.GetNbinsY()
    nBinsZ = h_3d.GetNbinsZ()
    wp = 0.8
    point = 0.0
    taggerLowBin = h_3d.GetXaxis().FindBin(point)
    taggerHighBin = h_3d.GetXaxis().FindBin(wp) - 1

    h_mj_fail  = h_3d.ProjectionZ("mj_failProjection" ,taggerLowBin,taggerHighBin,1,nBinsY)
    h_mj_pass  = h_3d.ProjectionZ("mj_passProjection" ,taggerHighBin,nBinsX,1,nBinsY)
    h_mj_fail.Scale(1/h_mj_fail.Integral())
    h_mj_pass.Scale(1/h_mj_pass.Integral())
    h_mj_pass.Rebin(2)
    h_mj_fail.Rebin(2)
    hRpf_mj = createRatio(h_mj_pass,h_mj_fail)
    hRatio = []
    hRatioErrors = []
    for i in range(1,hRpf_mj.GetNbinsX()+1):
        hRatio.append(hRpf_mj.GetBinContent(i))
        hRatioErrors.append(hRpf_mj.GetBinError(i))
    #numpy histograms
    hMJYFail, hMJYFailEdges = hist2array(h_mj_fail,return_edges=True)
    hMJYPass, hMJYPassEdges = hist2array(h_mj_pass,return_edges=True)
    hRatio = np.asarray(hRatio)
    hRatioErrors = np.asarray(hRatioErrors)
    centresRatio = (hMJYPassEdges[0][:-1] + hMJYPassEdges[0][1:])/2.

    plt.style.use([hep.style.CMS])
    f, axs = plt.subplots(2,1, sharex=True, sharey=False,gridspec_kw={'height_ratios': [4, 1],'hspace': 0.05})
    axs = axs.flatten()
    plt.sca(axs[0])
    if(log):
        axs[0].set_yscale("log")
    hep.histplot(hMJYPass,hMJYPassEdges[0],stack=False,ax=axs[0],label = "Pass", histtype="step",color="r")
    hep.histplot(hMJYFail,hMJYFailEdges[0],stack=False,ax=axs[0],label = "Fail", histtype="step",color="b")
    axs[0].legend()
    axs[1].set_xlabel(xTitle)
    axs[0].set_ylabel(yTitle)
    axs[1].set_ylabel("Data/MC")
    plt.ylabel(yTitle, horizontalalignment='right', x=1.0)
    if(yRange):
        axs[0].set_ylim(yRange)
    if(xRange):
        axs[0].set_xlim(xRange)
    hep.cms.lumitext(text='59.8 $fb^{-1} (13 TeV)$', ax=axs[0], fontname=None, fontsize=None)
    hep.cms.text("Simulation WiP",loc=1)
    plt.legend(loc="best")#loc = (0.4,0.2))

    plt.sca(axs[1])#switch to lower pad
    axs[1].axhline(y=1.0, xmin=0, xmax=1, color="r")
    axs[1].set_ylim([0.0,2.0])
    plt.xlabel(xTitle, horizontalalignment='right', x=1.0)

    plt.errorbar(centresRatio,hRatio, yerr=hRatioErrors, fmt='o',color="k")    

    print("Saving {0}".format(outFile))
    plt.savefig(outFile)
 def add_hist(total, hist):
     if args.combination_sideways:
         nx = hist.GetNbinsX()
         first, last = nx * isource, nx * (isource + 1)
         tc = rnp.hist2array(total, copy=False)
         tc[first:last] += rnp.hist2array(hist, copy=False)
         te2 = rnp.array(total.GetSumw2(), copy=False)[1:-1]
         te2[first:last] += rnp.array(hist.GetSumw2(), copy=False)[1:-1]
     else:
         total.Add(hist)
 def addHist(self, histname):
     histogram = self.file.Get(histname)
     if len(self.data) == 0:
         self.data = np.atleast_2d(rp.hist2array(histogram))
         self.bin_edges = getBinEdges(histogram)
     else:
         data_new = np.empty((self.data.shape[0] + 1, self.data.shape[1]))
         data_new[-1, :] = rp.hist2array(histogram)
         data_new[:-1, :] = self.data
         self.data = data_new
Exemple #16
0
def get_data(folder, fname, drift_margin = 0, crop = True, blur = None, 
             white_noise = 0, coherent_noise = 0):
    print 'Reading', fname
    try:
        if isinstance(folder, TFile):  # read from ROOT file
            A_raw     = hist2array(folder.Get(fname + '_raw'))
            A_deposit = hist2array(folder.Get(fname + '_deposit'))
            A_pdg     = hist2array(folder.Get(fname + '_pdg'))
        else:                # read text files
            A_raw     = np.genfromtxt(folder+'/'+fname + '.raw', delimiter=' ', 
                                      dtype=np.float32)
            A_deposit = np.genfromtxt(folder+'/'+fname + '.deposit', 
                                      delimiter=' ', dtype=np.float32)
            A_pdg     = np.genfromtxt(folder+'/'+fname + '.pdg', delimiter=' ', 
                                      dtype=np.int32)
    except:
        print 'Bad event, return empty arrays'
        return None, None, None, None, None

    if A_raw.shape[0] < 8 or A_raw.shape[1] < 8: 
        return None, None, None, None, None

    test_pdg = np.sum(A_pdg)
    test_dep = np.sum(A_deposit)
    test_raw = np.sum(A_raw)
    if test_raw == 0.0 or test_dep == 0.0 or test_pdg == 0: 
        return None, None, None, None, None

    print test_raw, test_dep, test_pdg
    #assert np.sum(A_deposit) > 0
    # get main event body (99% signal)
    if crop:
        evt_start_ind, evt_stop_ind = get_event_bounds(A_deposit, drift_margin)
        A_raw     = A_raw[:,evt_start_ind:evt_stop_ind]
        A_deposit = A_deposit[:,evt_start_ind:evt_stop_ind]
        A_pdg     = A_pdg[:,evt_start_ind:evt_stop_ind]
    else:
        evt_start_ind = 0
        evt_stop_ind = A_raw.shape[1]
    print evt_start_ind, evt_stop_ind

    A_raw = applyBlur(A_raw, blur)
    A_raw = addWhiteNoise(A_raw, white_noise)
    A_raw = addCoherentNoise(A_raw, coherent_noise)

    deposit_th_ind = A_deposit < 2.0e-5
    A_pdg[deposit_th_ind] = 0
    tracks = A_pdg.copy()
    showers = A_pdg.copy()
    tracks[(A_pdg & 0x0FFF) == 11] = 0
    tracks[tracks > 0]   = 1
    showers[(A_pdg & 0x0FFF) != 11] = 0
    showers[showers > 0] = 1
    return A_raw, A_deposit, A_pdg, tracks, showers
Exemple #17
0
    def read_decorr(self):
        myfile = root_open(self.filename, 'read')
        awesome = myfile.Get(self.directory)

        cumu_dW2TwoTwoD = rnp.hist2array(
            awesome.Get('cumulants').Get('mixed').Get('dW2TwoTwoD').Get(
                'cumu_dW2TwoTwoD'))
        cumu_dW2TwoTwoN = rnp.hist2array(
            awesome.Get('cumulants').Get('mixed').Get('dW2TwoTwoN').Get(
                'cumu_dW2TwoTwoN'))

        self.differential_mixedn[..., dW2TwoTwoD] = cumu_dW2TwoTwoD
        self.differential_mixedn[..., dW2TwoTwoN] = cumu_dW2TwoTwoN
def get_efficiency(title1, title2):

    #nPV_reconstructed
    nums1, edges1 = rn.hist2array(title1, return_edges=True, copy=True)
    n_ed = np.sum(nums1)

    #nPV_reconstructible
    nums2, edges2 = rn.hist2array(title2, return_edges=True, copy=True)
    n_ible = np.sum(nums2)

    efficiency = n_ed / n_ible

    return efficiency
Exemple #19
0
def get_hists(filename):
    import sys
    sys.argv.append('-b')
    from ROOT import TFile
    from root_numpy import hist2array

    f = TFile(filename, 'READ')
    h_ep = f.Get('gem_efficiency_ep')
    h_ee = f.Get('gem_efficiency_ee')

    hist_ep, edges = hist2array(h_ep, return_edges=True)
    hist_ee, edges = hist2array(h_ee, return_edges=True)

    return hist_ep[1:], hist_ee[1:], edges[0][1:]
Exemple #20
0
def Saver(bkgfilename, sgfilename, B_events, S_events):

    sig = ROOT.TFile.Open(sgfilename)
    bkg = ROOT.TFile.Open(bkgfilename)

    Nbkg = len(bkg.GetListOfKeys())
    Nsig = len(sig.GetListOfKeys())

    bkgArray = []
    sigArray = []

    bkg_iteration = random.sample(xrange(0, Nbkg), B_events)
    sig_iteration = random.sample(xrange(0, Nsig), S_events)
    #print(bkg_iteration)

    countBKG = 0
    countSig = 0

    RangeSig = [0, Nsig, int(math.floor(Nsig / S_events))]

    for i in range(0, Nbkg, int(math.floor(Nbkg / B_events))):
        histoname = "histo" + str(i)
        aHisto = bkg.Get(histoname)
        aHisto.Print()  #This prevents all the array elements being 0.
        #print("BKG")
        anArrayfromHisto = root_numpy.hist2array(aHisto)
        anArrayfromHisto = np.rot90(anArrayfromHisto)
        bkgArray.append(anArrayfromHisto)
        countBKG = countBKG + 1
        print(countBKG)

    for i in range(0, Nsig, int(math.floor(Nsig / S_events))):
        histoname = "histo" + str(i)
        aHisto = sig.Get(histoname)
        aHisto.Print()  #This prevents all the array elements being 0.
        #print("SIG")
        anArrayfromHisto = root_numpy.hist2array(aHisto)
        anArrayfromHisto = np.rot90(anArrayfromHisto)
        if np.sum(anArrayfromHisto) != 0.0:
            sigArray.append(anArrayfromHisto)
            countSig = countSig + 1
            print(countSig)

    bkgArray = np.array(bkgArray)
    sigArray = np.array(sigArray)

    print(countBKG)
    print(countSig)

    return bkgArray, sigArray
def extract_hist(directory, process_dict, total_signal_list, total_background_list, total_list):
    for key in directory.GetListOfKeys():
        print(key.GetName())
        subdir = directory.Get(key.GetName())
        for subkey in subdir.GetListOfKeys():
            print(subkey.GetName())
            print(subkey.GetName() == "total_signal")
            if subkey.GetName() == "data": continue
            hist = subdir.Get(subkey.GetName())
            if "total" not in subkey.GetName():
                if subkey.GetName() not in process_dict.keys(): process_dict[subkey.GetName()] = []
                process_dict[subkey.GetName()] = np.append(process_dict[subkey.GetName()], root_numpy.hist2array(hist))
            elif subkey.GetName() == "total_signal": total_signal_list.append(list(root_numpy.hist2array(hist)))
            elif subkey.GetName() == "total_background": total_background_list.append(list(root_numpy.hist2array(hist)))
            elif subkey.GetName() == "total": total_list.append(list(root_numpy.hist2array(hist)))
def getErrors(default, others):
    dfarr = root_numpy.hist2array(default)
    errs = np.zeros(len(dfarr))
    for other in others:
        if type(other) == list:
            err1 = root_numpy.hist2array(other[0]) - dfarr
            err1 = err1**2
            err2 = root_numpy.hist2array(other[1]) - dfarr
            err2 = err2**2
            err = np.maximum(err1, err2)
            errs += err
        else:
            err = root_numpy.hist2array(other) - dfarr
            err = err**2
            errs += err
    return errs
Exemple #23
0
def get_max_res_map(fname):
    """Get the histogram showing the acceptance of the detector at maximal resolution"""
    with root_open(fname) as f:
        h = list(f.c2_correlations)[0][-1]
        if h.GetName() != "etaPhiZvtx_max_res":
            raise ValueError("Histogram order is different than expected")
        return root_numpy.hist2array(h, return_edges=True)
def makeForwardPlotsSum(awesome, Zinit, mydata, doedges, dofill):
    histname = 'NUAforward'
    z = hist2array(awesome.FindObject('EventInfo').FindObject('Vertex'))
    zbins = 10
    Zinit = np.nansum(Zinit, axis=2)
    z = np.nansum(z)

    Znorm = nua_norm(Zinit, z)

    myplot(Znorm[:, :], mydata + '/forward/nua_norm', '',
           r'$dN / d\varphi d\eta$', False, -1, 'FMD')
    myplot(Znorm[:, :], mydata + '/forward/nua_norm_wfill', 'w. fills',
           r'$dN_{ch} / d\eta d\varphi$', False, -1, 'FMD')

    Zmean = nua_meanSum(Znorm)
    Znew = nua_frac(Zmean, Znorm)
    Zmean[Zmean == 0] = np.nan

    myplot(Zmean[:, :], mydata + '/forward/nua_mean_wfill', '',
           r'$\langle N_{ch} \rangle / d\eta$', False, -1, 'FMD')
    myplot(Znew[:, :], mydata + '/forward/nua_frac', '',
           r'$\langle N \rangle / N$', False, -1, 'FMD')
    plt.show()

    Zres = nua_result(Znew, Znorm)
    Zforward = Znew

    myplot(Zres[:, :], mydata + '/forward/nua_res', '',
           r'$\langle N \rangle / N$', False, -1, 'FMD')

    return Zforward
Exemple #25
0
    def get_curve(self, observable=None, norm=1, npoints=1000):
        """ Get projection of the pdf curve

        Args:
            observable (str, optional): Name of the observable
            norm (float, optional): normalisation for the pdf, default=1
            npoints (int): number of points, default=1000

        Returns:
            hx, hy : numpy arrays of x and y points of the pdf projection


        Examples:
            >>> import matplotlib.pyplot as plt
            >>> plt.plot(*pdf.get_curve())

        """
        import root_numpy
        import numpy as np

        if observable is not None:
            assert isinstance(observable,
                              str), "please specify the name of the observable"
            assert observable in self.observables, "observable not found"
        else:
            observable = self.get_observable().GetName()
        h = self.roo_pdf.createHistogram(observable, npoints)
        hy, hx = root_numpy.hist2array(h, False, False, True)
        # Normalise y
        hy = npoints * hy / np.sum(hy)
        # center x
        hx = (hx[0][:-1] + hx[0][1:]) / 2.

        return hx, np.multiply(hy, norm)
Exemple #26
0
def getErrors(default, others, keys):
    dfarr = root_numpy.hist2array(default)
    errs = {}
    for other, key in zip(others, keys):
        if type(other) == list:
            err1 = root_numpy.hist2array(other[0]) - dfarr
            err1 = abs(err1)
            err2 = root_numpy.hist2array(other[1]) - dfarr
            err2 = abs(err2)
            err = numpy.maximum(err1, err2)
            errs[key] = err
        else:
            err = root_numpy.hist2array(other) - dfarr
            err = abs(err)
            errs[key] = err
    return errs
def convert_histos(root_files, hist):

    global input_dir

    # Set histogram directory prefix based on ECAL subdetector
    if hist.startswith("EB"):
        hist_name = 'EcalBarrel/Run summary/%s' % hist
    else:
        hist_name = 'EcalEndcap/Run summary/%s' % hist

    hists = []
    for root_file in root_files:
        histo = root_file.Get(
            'DQMData/Run %d/%s' % (run, hist_name)
        )  # Get list of associated ROOT histograms (ROOT::TH2F or ROOT::TProfile2D)
        try:
            histo.Scale(1 / histo.GetBinContent(
                histo.GetMaximumBin()))  #Normalize Histograms by its max
            hists.append(histo)
        except ZeroDivisionError:
            print "Empty histogram: ", hist, " in file: ", root_file
        except AttributeError:
            print "Empty root filr: ", root_file
    X = np.stack([hist2array(hist).T for hist in hists
                  ])  # Convert ROOT histograms to NumPy arrays and stack

    return X
Exemple #28
0
    def __init__(self, options):
        self.rebin = options.rebin
        self.options = options
        self.pedfile_fullres_name = options.pedfile_fullres_name
        self.tmpname = options.tmpname
        geometryPSet = open(
            'modules_config/geometry_{det}.txt'.format(det=options.geometry),
            'r')
        geometryParams = eval(geometryPSet.read())
        self.cg = cameraGeometry(geometryParams)
        self.xmax = self.cg.npixx

        if not os.path.exists(self.pedfile_fullres_name):
            print("WARNING: pedestal file with full resolution ",
                  self.pedfile_fullres_name,
                  " not existing. First calculate them...")
            self.calcPedestal(options, 1)
        if not options.justPedestal:
            print("Pulling pedestals...")
            # first the one for clustering with rebin
            ctools = cameraTools(self.cg)
            # then the full resolution one
            pedrf_fr = ROOT.TFile.Open(self.pedfile_fullres_name)
            self.pedmap_fr = pedrf_fr.Get('pedmap').Clone()
            self.pedmap_fr.SetDirectory(0)
            self.pedarr_fr = hist2array(self.pedmap_fr).T
            self.noisearr_fr = ctools.noisearray(self.pedmap_fr).T
            pedrf_fr.Close()
            if options.vignetteCorr:
                self.vignmap = ctools.loadVignettingMap()
            else:
                self.vignmap = np.ones((self.xmax, self.xmax))
Exemple #29
0
def magnify_dump(ctx, out, filename):
    '''Dump magnify histograms into Numpy .npz files.

    The underlying histogram array is dumped into an array of the same
    name.  For every dimension of histogram an array of bin edges is
    dumped as <name>_<dim>edges where <dim> is "x", "y" or "z".
    '''
    import ROOT
    import numpy
    from root_numpy import hist2array
    from time import time

    print "Reading ROOT file"
    t1 = time()
    arrs = dict()
    tfile = ROOT.TFile.Open(filename)
    for key in tfile.GetListOfKeys():
        cname = key.GetClassName()
        if cname[:3] not in ["TH1", "TH2", "TH3"]:
            continue

        th = key.ReadObj()
        hname = th.GetName()
        arr, edges = hist2array(th, return_edges=True)
        arrs[hname] = arr
        for dim, edge in enumerate(edges):
            arrs["%s_%sedge" % (hname, "xyz"[dim])] = edge
    t2 = time()
    print t2 - t1  # takes 5.7 seconds compared to 5.3 seconds loading npz
    print "Writing NPZ file"
    numpy.savez_compressed(out, **arrs)
    t3 = time()
    print t3 - t2
Exemple #30
0
def channel_responses(ctx, tscale, scale, name, infile, outfile):
    '''Produce the per-channel calibrated response JSON file from a TH2D
    of the given name in the input ROOT file provided by the analysis.

    - tscale :: a number to multiply to the time axis of the histogram
      in order to bring the result into the WCT system of units.  It
      may be expressed as a string of an algebraic expression which
      includes symbols, eg "0.5*us".

    - scale :: a number multiplied to all samples in the histogram in
      order to make the sample value a unitless relative measure.  It
      may be expressed as a string of an algebraic expression which
      includes symbols, eg "0.5*us". For uBoone's initial
      20171006_responseWaveforms.root the appropriate scale is
      1e-9*0.5/1.13312 = 4.41267e-10
    '''
    import json
    import ROOT
    import numpy
    from root_numpy import hist2array

    tscale = eval(tscale, units.__dict__)
    scale = eval(scale, units.__dict__)

    tf = ROOT.TFile.Open(str(infile))
    assert(tf)
    h = tf.Get(str(name))
    if not h:
        click.echo('Failed to get histogram "%s" from %s' % (name, infile))
        sys.exit(1)

    arr,edges = hist2array(h, return_edges=True)

    arr *= scale
    tedges = edges[1]
    t0,t1 = tscale*(tedges[0:2])
    tick = t1-t0

    nchans, nticks = arr.shape
    channels = list()
    for ch in range(nchans):
        # reduce down to ~32 bit float precision to save file space
        res = [float("%.6g"%x) for x in arr[ch,:].tolist()]
        one = [ch, res]
        channels.append(one)

    dat = dict(tick=tick, t0=t0, channels=channels)

    jtext = json.dumps(dat, indent=4)
    if outfile.endswith(".json.bz2"):
        import bz2
        bz2.BZ2File(outfile, 'w').write(jtext)
        return
    if outfile.endswith(".json.gz"):
        import gzip
        gzip.open(outfile, 'wb').write(jtext) # wb?
        return

    open(outfile, 'w').write(jtext)
    return
Exemple #31
0
 def loadVignettingMap(self):
     print("Loading vignette map from: {vf}...".format(
         vf=self.geometry.vignette))
     det = self.geometry.name
     if det == 'lemon':  # not implemented (we were taking the efficienct region within the FC)
         return self.vignetteMap[det]
     elif det == 'lime' or 'Mango_full':
         if not self.vignetteMap[det].any():
             tf = ROOT.TFile.Open(self.geometry.vignette)
             hmap = tf.Get('normmap_lime')
             vignetteMapRebinned = hist2array(hmap)
             tf.Close()
             rebinx = int(self.geometry.npixx /
                          vignetteMapRebinned.shape[0])
             rebiny = int(self.geometry.npixx /
                          vignetteMapRebinned.shape[1])
             macroPixel = np.zeros((rebinx, rebiny))
             print("Macro-pixel of the vignetting map has size = ",
                   macroPixel.shape)
             for ibx in range(vignetteMapRebinned.shape[0]):
                 for iby in range(vignetteMapRebinned.shape[1]):
                     macroPixel[:, :] = 1. / vignetteMapRebinned[iby, ibx]
                     (self.vignetteMap[det]
                      )[int(ibx * rebinx):int((ibx + 1) * rebinx),
                        int(iby * rebiny):int((iby + 1) *
                                              rebiny)] = macroPixel
             return self.vignetteMap[det]
         else:
             return self.vignetteMap[det]
     else:
         print('WARNING! Geometry ', det,
               ' not foreseen. Return correction 1')
         return self.vignetteMap[det]
Exemple #32
0
def getProfile2DataRaw(root_file, root_folder, coll_name):
    print "opening...", root_file
    # open root file
    profile_file = TFile(root_file)
    # get data
    profile_data = profile_file.Get(root_folder + coll_name)  #print histdata
    # get dimension, sums and x,y-positions. Note: using hist2array for Th2profile data
    sums, edges = hist2array(profile_data,
                             include_overflow=False,
                             return_edges=True)  #print counts, edge
    # get contents
    contents = np.zeros(shape=np.shape(sums))
    for y_index, y_value in enumerate(sums[0]):
        for x_index, x_value in enumerate(sums.T[0]):
            contents[x_index][y_index] = profile_data.GetBinContent(
                x_index + 1, y_index + 1)
    # get errors
    errors = np.zeros(shape=np.shape(sums))
    for y_index, y_value in enumerate(sums[0]):
        for x_index, x_value in enumerate(sums.T[0]):
            errors[x_index][y_index] = profile_data.GetBinError(
                x_index + 1, y_index + 1)
    # calculate counts
    counts = np.divide(sums, contents)
    # shift hist data by half of the binwidth
    binwidth_x = abs(edges[0][1] - edges[0][0])
    bincenters_x = np.array(edges[0][:-1]) + binwidth_x / 2.
    binwidth_y = abs(edges[1][1] - edges[1][0])
    bincenters_y = np.array(edges[1][:-1]) + binwidth_y / 2.
    # return counts (x, y)-array, x_edges and y_edges data
    return contents, counts, bincenters_x, bincenters_y, edges[0], edges[
        1], errors
def nua_raw(awesome, hist):
    nuahist = awesome.FindObject(hist)

    Zinit = hist2array(nuahist)
    Zinit[Zinit == 0] = np.nan

    return Zinit
Exemple #34
0
def check_array2hist(hist):
    shape = np.array([hist.GetNbinsX(), hist.GetNbinsY(), hist.GetNbinsZ()])
    shape = shape[:hist.GetDimension()]
    arr = RNG.randint(0, 10, size=shape)
    rnp.array2hist(arr, hist)
    arr_hist = rnp.hist2array(hist)
    assert_array_equal(arr_hist, arr)

    # Check behaviour if errors are supplied
    errors = arr * 0.1
    _hist = hist.Clone()
    _hist.Reset()
    rnp.array2hist(arr, _hist, errors=errors)
    arr_hist = rnp.hist2array(_hist)
    assert_array_equal(arr_hist, arr)
    if hist.GetDimension() == 1:
        errors_from_hist = np.array([_hist.GetBinError(ix)
                                     for ix in range(1, _hist.GetNbinsX() + 1)])
    if hist.GetDimension() == 2:
        errors_from_hist = np.array([[_hist.GetBinError(ix, iy)
                                      for iy in range(1, _hist.GetNbinsY() + 1)]
                                     for ix in range(1, _hist.GetNbinsX() + 1)])
    if hist.GetDimension() == 3:
        errors_from_hist = np.array([[[_hist.GetBinError(ix, iy, iz)
                                       for iz in range(1, _hist.GetNbinsZ() + 1)]
                                      for iy in range(1, _hist.GetNbinsY() + 1)]
                                     for ix in range(1, _hist.GetNbinsX() + 1)])
    assert_array_equal(errors, errors_from_hist)

    shape_overflow = shape + 2
    arr_overflow = RNG.randint(0, 10, size=shape_overflow)
    hist_overflow = hist.Clone()
    hist_overflow.Reset()
    rnp.array2hist(arr_overflow, hist_overflow)
    arr_hist_overflow = rnp.hist2array(hist_overflow, include_overflow=True)
    assert_array_equal(arr_hist_overflow, arr_overflow)

    if len(shape) == 1:
        return

    # overflow not specified on all axes
    arr_overflow2 = arr_overflow[1:-1]
    hist_overflow2 = hist.Clone()
    hist_overflow2.Reset()
    rnp.array2hist(arr_overflow2, hist_overflow2)
    arr_hist_overflow2 = rnp.hist2array(hist_overflow2, include_overflow=True)
    assert_array_equal(arr_hist_overflow2[1:-1], arr_overflow2)
Exemple #35
0
 def __init__(self, working_points, functions, efficiency_map):
     efficiency_array = hist2array(efficiency_map)
     working_points_indices = find_closest(working_points, efficiency_array)
     self.function_index_map = efficiency_map.empty_clone()
     array2hist(working_points_indices, self.function_index_map)
     self.indices = working_points_indices
     self.functions = functions
     self.dim = len(efficiency_array.shape)
def hist2d_to_matrix( hist2d ):
    '''
    hist2d to matrix + bin edges
    '''
    from root_numpy import hist2array
    values = hist2array(hist2d)
    xedges = list(hist2d.xedges())
    yedges = list(hist2d.yedges())
    return values, xedges, yedges
Exemple #37
0
def get_hist(rfile, histname, get_overflow=False):
    """Read a 1D Histogram."""
    import root_numpy as rnp

    rfile = open_rfile(rfile)
    hist = rfile[histname]
    xlims = np.array(list(hist.xedges()))
    bin_values = rnp.hist2array(hist, include_overflow=get_overflow)
    rfile.close()
    return bin_values, xlims
Exemple #38
0
def check_hist2array(hist, include_overflow, copy):
    array = rnp.hist2array(hist, include_overflow=include_overflow, copy=copy)
    assert_equal(hist.GetDimension(), array.ndim)
    for iaxis, axis in enumerate('XYZ'[:array.ndim]):
        if include_overflow:
            assert_equal(array.shape[iaxis],
                         getattr(hist, 'GetNbins{0}'.format(axis))() + 2)
        else:
            assert_equal(array.shape[iaxis],
                         getattr(hist, 'GetNbins{0}'.format(axis))())
    # non-zero elements
    assert_true(np.any(array))
Exemple #39
0
def check_hist2array(hist, include_overflow, copy):
    array = rnp.hist2array(hist, include_overflow=include_overflow, copy=copy)
    assert_equal(hist.GetDimension(), array.ndim)
    for iaxis, axis in enumerate('XYZ'[:array.ndim]):
        if include_overflow:
            assert_equal(array.shape[iaxis],
                         getattr(hist, 'GetNbins{0}'.format(axis))() + 2)
        else:
            assert_equal(array.shape[iaxis],
                         getattr(hist, 'GetNbins{0}'.format(axis))())
    hist_sum = hist.Integral()
    assert_true(hist_sum > 0)
    assert_equal(hist_sum, np.sum(array))
Exemple #40
0
def test_hist2array():
    assert_raises(TypeError, rnp.hist2array, object())
    for ndim in (1, 2, 3):
        for hist_type in 'DFISC':
            hist = make_histogram(hist_type, shape=(5,) * ndim)
            yield check_hist2array, hist, False, False
            yield check_hist2array, hist, False, True
            yield check_hist2array, hist, True, False
            yield check_hist2array, hist, True, True
            yield check_hist2array_THn, hist
            # check that the memory was copied
            arr = rnp.hist2array(hist, copy=True)
            hist_sum = hist.Integral()
            assert_true(hist_sum > 0)
            hist.Reset()
            assert_equal(np.sum(arr), hist_sum)
            # check that the memory is shared
            hist = make_histogram(hist_type, shape=(5,) * ndim)
            arr = rnp.hist2array(hist, copy=False)
            hist_sum = hist.Integral()
            assert_true(hist_sum > 0)
            assert_true(np.sum(arr) == hist_sum)
            hist.Reset()
            assert_true(np.sum(arr) == 0)
Exemple #41
0
def th2_to_arrays(th2):

    # NOTE: man how much I hate ROOT!
    # So much effort to do the simplest thing...

    # Get first all the lower edges of the bins, then the last edge is the upper edge of the last bin
    # (so these edges can be used in np.histogram)
    n_x = th2.GetNbinsX()
    xax = th2.GetXaxis()
    x_edges = _get_edges(xax, n_x)

    # Same for y
    n_y = th2.GetNbinsY()
    yax = th2.GetYaxis()
    y_edges = _get_edges(yax, n_y)

    # Finally get the array of values
    matrix = root_numpy.hist2array(th2)

    return x_edges, y_edges, matrix
Exemple #42
0
def check_hist2array_edges(hist, ndim, bins):
    _, edges = rnp.hist2array(hist, return_edges=True)
    assert_equal(len(edges), ndim)
    for axis_edges in edges:
        assert_array_equal(axis_edges, np.arange(bins + 1, dtype=np.double))
Exemple #43
0
  if checkExtension( inFile, "root" ):
    cOutputName = changeExtension( inFile, '.npz' )
    if args.change_output_folder:
      import os.path
      cOutputName = os.path.join( os.path.abspath(args.change_output_folder) , os.path.basename(cOutputName) )
    f = ROOT.TFile( inFile, 'r' )
    data = {}
    for keyName, obj in getall(f):
      mainLogger.debug("Reading key: %s", keyName)
      shortKey = keyName.split('/')[-1]
      if not issubclass(type(obj), ROOT.TH1): 
        mainLogger.verbose("Ignoring key: %s", shortKey )
        continue
      hist = obj
      if all( [wanted not in shortKey for wanted in args.filter_keys] ):
        mainLogger.debug("key <%s> does not match any filter", shortKey )
        continue
      if all( [exclude in shortKey for exclude in args.exclude_keys] ):
        mainLogger.debug("key <%s> matches exclude pattern", shortKey )
        continue
      if not hist:
        mainLogger.warning("Couldn't retrieve histogram with key: %s", shortKey )
      data[ shortKey ] = rnp.hist2array( hist, include_overflow=True, return_edges=True )
    savedPath = save(data, cOutputName, protocol = 'savez_compressed')
    mainLogger.info("Successfully created numpy file: %s", cOutputName)
  else:
    mainLogger.error("Cannot transform file '%s' to numpy format." % inFile)
# end of (for fileCollection)


Exemple #44
0
        for (ioff, off) in enumerate(offMC):
            print off
            for (izen, zen) in enumerate(zenMC):
                print zen
                filehistoarea = results.directory + "/" + results.config + "/gFixedEnergy_paris_0-8-8-8_CamOptimal_hwtrig_eff" + str(
                    eff) + "/CollectionArea.root"
                filehistoresol = results.directory + "/" + results.config + "/gFixedEnergy_paris_0-8-8-8_CamOptimal_hwtrig_eff" + str(
                    eff) + "/EnergyResolution.root"
                TFileArea = ROOT.TFile(filehistoarea)
                TFileResol = ROOT.TFile(filehistoresol)
                if (izen == 0):
                    name_hist_area = "EffArea_00deg_" + str(off) + "off_eff" + str(eff) + "_FixedE"
                    name_hist_biais = "Resol_Biais_00deg_" + str(off) + "off_eff" + str(eff) + "_FixedE"
                    name_hist_sigma = "Resol_Sigma_00deg_" + str(off) + "off_eff" + str(eff) + "_FixedE"
                else:
                    name_hist_area = "EffArea_" + str(zen) + "deg_" + str(off) + "off_eff" + str(eff) + "_FixedE"
                    name_hist_biais = "Resol_Biais_" + str(zen) + "deg_" + str(off) + "off_eff" + str(eff) + "_FixedE"
                    name_hist_sigma = "Resol_Sigma_" + str(zen) + "deg_" + str(off) + "off_eff" + str(eff) + "_FixedE"
                histoarea = TFileArea.Get(name_hist_area)
                histobiais = TFileResol.Get(name_hist_biais)
                histosigma = TFileResol.Get(name_hist_sigma)
                AreaArray = hist2array(histoarea)[ind_area]
                AreaBiais = hist2array(histobiais)[ind_resol]
                AreaSigma = hist2array(histosigma)[ind_resol]
                TableArea[:, ioff, izen, ieff] = AreaArray
                TableBiais[:, ioff, izen, ieff] = AreaBiais
                TableSigma[:, ioff, izen, ieff] = AreaSigma
    outdir="output_4Dnumpyarrays"
    np.savez(outdir+"/IRF_"+results.config+".npz", TableArea=TableArea, TableBiais=TableBiais, TableSigma=TableSigma, enMC=enMC, lnenMC=lnenMC,
             zenMC=zenMC, offMC=offMC, effMC=effMC)