Ejemplo n.º 1
0
def test_na_union():
    one = awkward1.from_iter([1, None, 3], highlevel=False)
    two = awkward1.from_iter([[], [1], None, [3, 3, 3]], highlevel=False)
    tags = awkward1.layout.Index8(numpy.array([0, 1, 1, 0, 0, 1, 1], dtype=numpy.int8))
    index = awkward1.layout.Index64(numpy.array([0, 0, 1, 1, 2, 2, 3], dtype=numpy.int64))
    array = awkward1.Array(awkward1.layout.UnionArray8_64(tags, index, [one, two]), check_valid=True)
    assert awkward1.to_list(array) == [1, [], [1], None, 3, None, [3, 3, 3]]

    assert awkward1.to_list(awkward1.is_none(array)) == [False, False, False, True, False, True, False]
Ejemplo n.º 2
0
 def num(ar):
     return ak.num(ak.fill_none(ar[~ak.is_none(ar)], 0), axis=0)
Ejemplo n.º 3
0
    def process(self, events):
        
        output = self.accumulator.identity()
        
        # we can use a very loose preselection to filter the events. nothing is done with this presel, though
        presel = ak.num(events.Jet)>=2
        
        ev = events[presel]
        dataset = ev.metadata['dataset']

        # load the config - probably not needed anymore
        cfg = loadConfig()
        
        output['totalEvents']['all'] += len(events)
        output['skimmedEvents']['all'] += len(ev)
        
        
        ## Electrons
        electron = Collections(ev, "Electron", "tightFCNC", 0, self.year).get()
        electron = electron[(electron.pt > 15) & (np.abs(electron.eta) < 2.4)]

        electron = electron[(electron.genPartIdx >= 0)]
        electron = electron[(np.abs(electron.matched_gen.pdgId)==11)]  #from here on all leptons are gen-matched
        electron = electron[( (electron.genPartFlav==1) | (electron.genPartFlav==15) )] #and now they are all prompt
     
        
        leading_electron_idx = ak.singletons(ak.argmax(electron.pt, axis=1))
        leading_electron = electron[leading_electron_idx]
        
        trailing_electron_idx = ak.singletons(ak.argmin(electron.pt, axis=1))
        trailing_electron = electron[trailing_electron_idx]
        
        leading_parent = find_first_parent(leading_electron.matched_gen)
        trailing_parent = find_first_parent(trailing_electron.matched_gen)
        
       
        is_flipped = ( ( (electron.matched_gen.pdgId*(-1) == electron.pdgId) | (find_first_parent(electron.matched_gen)*(-1) == electron.pdgId) ) & (np.abs(electron.pdgId) == 11) )
        
        
        flipped_electron = electron[is_flipped]
        flipped_electron = flipped_electron[(ak.fill_none(flipped_electron.pt, 0)>0)]
        flipped_electron = flipped_electron[~(ak.is_none(flipped_electron))]
        n_flips = ak.num(flipped_electron)
                
        ##Muons
        muon     = Collections(ev, "Muon", "tightFCNC").get()
        muon = muon[(muon.pt > 15) & (np.abs(muon.eta) < 2.4)]
        
        muon = muon[(muon.genPartIdx >= 0)]
        muon = muon[(np.abs(muon.matched_gen.pdgId)==13)] #from here, all muons are gen-matched
        muon = muon[( (muon.genPartFlav==1) | (muon.genPartFlav==15) )] #and now they are all prompt
       
        
        ##Leptons

        lepton   = ak.concatenate([muon, electron], axis=1)
        SSlepton = (ak.sum(lepton.charge, axis=1) != 0) & (ak.num(lepton)==2)
        OSlepton = (ak.sum(lepton.charge, axis=1) == 0) & (ak.num(lepton)==2)
        
        emulepton = (ak.num(electron) == 1) & (ak.num(muon) == 1)
        no_mumu = (ak.num(muon) <= 1)
        
        
        leading_lepton_idx = ak.singletons(ak.argmax(lepton.pt, axis=1))
        leading_lepton = lepton[leading_lepton_idx]
        
        trailing_lepton_idx = ak.singletons(ak.argmin(lepton.pt, axis=1))
        trailing_lepton = lepton[trailing_lepton_idx]
        
        
        
        #jets
        jet       = getJets(ev, minPt=40, maxEta=2.4, pt_var='pt')
        jet       = jet[ak.argsort(jet.pt, ascending=False)] # need to sort wrt smeared and recorrected jet pt
        jet       = jet[~match(jet, muon, deltaRCut=0.4)] # remove jets that overlap with muons
        jet       = jet[~match(jet, electron, deltaRCut=0.4)] 
        
        ## MET -> can switch to puppi MET
        met_pt  = ev.MET.pt
        met_phi = ev.MET.phi

        # setting up the various weights
        weight = Weights( len(ev) )
        weight2 = Weights( len(ev))
        
        if not dataset=='MuonEG':
            # generator weight
            weight.add("weight", ev.genWeight)
            weight2.add("weight", ev.genWeight)
            
        weight2.add("charge flip", self.charge_flip_ratio.flip_weight(electron))
                                   
                      
        #selections    
        filters   = getFilters(ev, year=self.year, dataset=dataset)
        ss = (SSlepton)
        os = (OSlepton)
        jet_all = (ak.num(jet) >= 2)
        diele = (ak.num(electron) == 2)
        emu = (emulepton)
        flips = (n_flips == 1)
        no_flips = (n_flips == 0)
        nmm = no_mumu
        
        
        selection = PackedSelection()
        selection.add('filter',      (filters) )
        selection.add('ss',          ss )
        selection.add('os',          os )
        selection.add('jet',         jet_all )
        selection.add('ee',          diele)
        selection.add('emu',         emu)
        selection.add('flip',        flips)
        selection.add('nflip',       no_flips)
        selection.add('no_mumu',     nmm)
        
        bl_reqs = ['filter'] + ['jet']

        bl_reqs_d = { sel: True for sel in bl_reqs }
        baseline = selection.require(**bl_reqs_d)
        
        f_reqs = bl_reqs + ['flip'] + ['ss'] + ['ee']
        f_reqs_d = {sel: True for sel in f_reqs}
        flip_sel = selection.require(**f_reqs_d)
        
        f2_reqs = bl_reqs + ['flip'] + ['ss'] + ['emu']
        f2_reqs_d = {sel: True for sel in f2_reqs}
        flip_sel2 = selection.require(**f2_reqs_d)
        
        f3_reqs = bl_reqs + ['flip'] + ['ss'] + ['no_mumu']
        f3_reqs_d = {sel: True for sel in f3_reqs}
        flip_sel3 = selection.require(**f3_reqs_d)
        
        nf_reqs = bl_reqs + ['nflip'] + ['os'] + ['ee']
        nf_reqs_d = {sel: True for sel in nf_reqs}
        n_flip_sel = selection.require(**nf_reqs_d)
        
        nf2_reqs = bl_reqs + ['nflip'] + ['os'] + ['emu']
        nf2_reqs_d = {sel: True for sel in nf2_reqs}
        n_flip_sel2 = selection.require(**nf2_reqs_d)
        
        nf3_reqs = bl_reqs + ['nflip'] + ['os'] + ['no_mumu']
        nf3_reqs_d = {sel: True for sel in nf3_reqs}
        n_flip_sel3 = selection.require(**nf3_reqs_d)
        
        s_reqs = bl_reqs + ['ss'] + ['no_mumu']
        s_reqs_d = { sel: True for sel in s_reqs }
        ss_sel = selection.require(**s_reqs_d)
        
        o_reqs = bl_reqs + ['os'] + ['no_mumu']
        o_reqs_d = {sel: True for sel in o_reqs }
        os_sel = selection.require(**o_reqs_d)
        
        ees_reqs = bl_reqs + ['ss'] + ['ee']
        ees_reqs_d = { sel: True for sel in ees_reqs }
        eess_sel = selection.require(**ees_reqs_d)
        
        eeo_reqs = bl_reqs + ['os'] + ['ee']
        eeo_reqs_d = {sel: True for sel in eeo_reqs }
        eeos_sel = selection.require(**eeo_reqs_d)
        
        ems_reqs = bl_reqs + ['ss'] + ['emu']
        ems_reqs_d = { sel: True for sel in ems_reqs }
        emss_sel = selection.require(**ems_reqs_d)
        
        emo_reqs = bl_reqs + ['os'] + ['emu']
        emo_reqs_d = {sel: True for sel in emo_reqs }
        emos_sel = selection.require(**emo_reqs_d)
        
       
        #outputs
        output['N_jet'].fill(dataset=dataset, multiplicity=ak.num(jet)[baseline], weight=weight.weight()[baseline])
        
        output['N_ele'].fill(dataset=dataset, multiplicity=ak.num(lepton)[ss_sel], weight=weight.weight()[ss_sel])
                      
        output['N_ele2'].fill(dataset=dataset, multiplicity=ak.num(lepton)[os_sel], weight=weight2.weight()[os_sel])
        
        output['electron_flips'].fill(dataset=dataset, multiplicity = n_flips[flip_sel], weight=weight.weight()[flip_sel])

        output['electron_flips2'].fill(dataset=dataset, multiplicity = n_flips[n_flip_sel], weight=weight2.weight()[n_flip_sel])
        
        output['electron_flips3'].fill(dataset=dataset, multiplicity = n_flips[flip_sel2], weight=weight.weight()[flip_sel2])

        output['electron_flips4'].fill(dataset=dataset, multiplicity = n_flips[n_flip_sel2], weight=weight2.weight()[n_flip_sel2])
        

        output["electron"].fill(
            dataset = dataset,
            pt  = ak.to_numpy(ak.flatten(leading_electron[flip_sel3].pt)),
            eta = np.abs(ak.to_numpy(ak.flatten(leading_electron[flip_sel3].eta))),
            weight = weight.weight()[flip_sel3]
        )
        
        output["electron2"].fill(
            dataset = dataset,
            pt  = ak.to_numpy(ak.flatten(leading_electron[n_flip_sel3].pt)),
            eta = np.abs(ak.to_numpy(ak.flatten(leading_electron[n_flip_sel3].eta))),
            weight = weight2.weight()[n_flip_sel3]
        )
        
        output["flipped_electron"].fill(
            dataset = dataset,
            pt  = ak.to_numpy(ak.flatten(leading_electron[flip_sel].pt)),
            eta = np.abs(ak.to_numpy(ak.flatten(leading_electron[flip_sel].eta))),
            weight = weight.weight()[flip_sel]
        )
        
        output["flipped_electron2"].fill(
            dataset = dataset,
            pt  = ak.to_numpy(ak.flatten(leading_electron[n_flip_sel].pt)),
            eta = np.abs(ak.to_numpy(ak.flatten(leading_electron[n_flip_sel].eta))),
            weight = weight2.weight()[n_flip_sel]
        )
        
        output["flipped_electron3"].fill(
            dataset = dataset,
            pt  = ak.to_numpy(ak.flatten(leading_electron[flip_sel2].pt)),
            eta = np.abs(ak.to_numpy(ak.flatten(leading_electron[flip_sel2].eta))),
            weight = weight.weight()[flip_sel2]
        )
        
        output["flipped_electron4"].fill(
            dataset = dataset,
            pt  = ak.to_numpy(ak.flatten(leading_electron[n_flip_sel2].pt)),
            eta = np.abs(ak.to_numpy(ak.flatten(leading_electron[n_flip_sel2].eta))),
            weight = weight2.weight()[n_flip_sel2]
        )
        
        #output["lepton_parent"].fill(
        #    dataset = dataset,
        #    pdgID = np.abs(ak.to_numpy(ak.flatten(leading_parent[ss_sel]))),
        #    weight = weight.weight()[ss_sel]
        #)
        #
        #output["lepton_parent2"].fill(
        #    dataset = dataset,
        #    pdgID = np.abs(ak.to_numpy(ak.flatten(trailing_parent[ss_sel]))),
        #    weight = weight.weight()[ss_sel]
        #)

        return output
Ejemplo n.º 4
0
					weightString = re.sub("_[0-9]", "", weight)
					if isData:
						continue
					if not weightString in weightDict.keys():
						weightDict[weightString] = currentTree[weightString].array(library="ak")
					if re.search("_[1-9]", weight):
						indexOfInterest = int(weight[-1]) - 1
						currentWeight = currentWeight * weightDict[weightString].mask[ak.num(weightDict[weightString]) > indexOfInterest][:,indexOfInterest]
					else:
						currentWeight = currentWeight * weightDict[weightString]
				for cut, condition in plotConfig[quantity]["cutvariables"]:
					currentQuantity = common.MaskQuantity(currentTree, currentQuantity, cutDict, cut, condition)

				# Flatten and strip all None type elements for fill to properly process
				currentQuantity = ak.flatten(currentQuantity)
				currentQuantity = currentQuantity[~ak.is_none(currentQuantity)]
				currentWeight = currentWeight[~ak.is_none(currentQuantity)]

				if isData:
					hist.fill(currentQuantity)
				else:
					hist.fill(currentQuantity, weight=currentWeight)#[~ak.is_none(currentQuantity)], weight=currentWeight[~ak.is_none(currentQuantity)])
					hist = hist * xSection * 1000 * luminosity / nGenEvents[re.sub("_ext[0-9]*", "", directory)]

				histPerQuantity[quantity] += hist
			currentTree.close()
		histPerSample[sample] = histPerQuantity

	# Create the plots
	if args.unblind and "data" in args.samples:
		mcHist   = [bh.Histogram(common.ConstructHistogram(plotConfig, quantity)) for quantity in args.quantities]
Ejemplo n.º 5
0
    def process(self, events):

        output = self.accumulator.identity()

        # we can use a very loose preselection to filter the events. nothing is done with this presel, though
        presel = ak.num(events.Jet) > 0

        ev = events[presel]
        dataset = ev.metadata['dataset']
        dataset_weight = ev.metadata['dataset']

        # load the config - probably not needed anymore
        cfg = loadConfig()

        output['totalEvents']['all'] += len(events)
        output['skimmedEvents']['all'] += len(ev)

        ## Electrons
        electron = Collections(ev, "Electron", "tight").get()
        electron = electron[(electron.miniPFRelIso_all < 0.12)
                            & (electron.pt > 20) & (abs(electron.eta) < 2.4)]

        gen_matched_electron = electron[((electron.genPartIdx >= 0) & (abs(
            electron.matched_gen.pdgId) == 11))]
        n_gen = ak.num(gen_matched_electron)

        is_flipped = ((gen_matched_electron.matched_gen.pdgId *
                       (-1) == gen_matched_electron.pdgId) &
                      (abs(gen_matched_electron.pdgId) == 11))

        flipped_electron = gen_matched_electron[is_flipped]
        flipped_electron = flipped_electron[(ak.fill_none(
            flipped_electron.pt, 0) > 0)]
        flipped_electron = flipped_electron[~(ak.is_none(flipped_electron))]
        n_flips = ak.num(flipped_electron)

        dielectron = choose(electron, 2)
        SSelectron = ak.any(
            (dielectron['0'].charge * dielectron['1'].charge) > 0, axis=1)

        leading_electron_idx = ak.singletons(ak.argmax(electron.pt, axis=1))
        leading_electron = electron[leading_electron_idx]

        ## MET -> can switch to puppi MET
        met_pt = ev.MET.pt
        met_phi = ev.MET.phi

        # setting up the various weights
        weight = Weights(len(ev))
        weight2 = Weights(len(ev))

        if not dataset == 'MuonEG':
            # generator weight
            weight.add("weight", ev.genWeight)
            weight2.add("weight", ev.genWeight)

        weight2.add("charge flip",
                    self.charge_flip_ratio.flip_weight(electron))

        #selections
        filters = getFilters(ev, year=self.year, dataset=dataset)
        electr = ((ak.num(electron) >= 1))
        ss = (SSelectron)
        gen = (n_gen >= 1)
        flip = (n_flips >= 1)
        not_flip = (n_flips == 0)

        selection = PackedSelection()
        selection.add('filter', (filters))
        selection.add('electr', electr)
        selection.add('ss', ss)
        selection.add('flip', flip)
        selection.add('nflip', not_flip)
        selection.add('gen', gen)

        bl_reqs = ['filter', 'electr', 'gen']

        bl_reqs_d = {sel: True for sel in bl_reqs}
        baseline = selection.require(**bl_reqs_d)

        s_reqs = bl_reqs + ['ss']
        s_reqs_d = {sel: True for sel in s_reqs}
        ss_sel = selection.require(**s_reqs_d)

        f_reqs = bl_reqs + ['flip']
        f_reqs_d = {sel: True for sel in f_reqs}
        flip_sel = selection.require(**f_reqs_d)

        nf_reqs = bl_reqs + ['nflip']
        nf_reqs_d = {sel: True for sel in nf_reqs}
        nflip_sel = selection.require(**nf_reqs_d)

        #outputs
        output['N_ele'].fill(dataset=dataset,
                             multiplicity=ak.num(electron)[flip_sel],
                             weight=weight.weight()[flip_sel])
        output['electron_flips'].fill(dataset=dataset,
                                      multiplicity=n_flips[flip_sel],
                                      weight=weight.weight()[flip_sel])

        output['N_ele2'].fill(dataset=dataset_weight,
                              multiplicity=ak.num(electron)[nflip_sel],
                              weight=weight2.weight()[nflip_sel])
        output['electron_flips2'].fill(dataset=dataset_weight,
                                       multiplicity=n_flips[nflip_sel],
                                       weight=weight2.weight()[nflip_sel])

        output["electron"].fill(
            dataset=dataset,
            pt=ak.to_numpy(ak.flatten(leading_electron[flip_sel].pt)),
            eta=ak.to_numpy(ak.flatten(abs(leading_electron[flip_sel].eta))),
            #phi = ak.to_numpy(ak.flatten(leading_electron[baseline].phi)),
            weight=weight.weight()[flip_sel])

        output["electron2"].fill(
            dataset=dataset_weight,
            pt=ak.to_numpy(ak.flatten(leading_electron[nflip_sel].pt)),
            eta=ak.to_numpy(ak.flatten(abs(leading_electron[nflip_sel].eta))),
            #phi = ak.to_numpy(ak.flatten(leading_electron[baseline].phi)),
            weight=weight2.weight()[nflip_sel])

        return output
Ejemplo n.º 6
0
		def skim_weight(arr):
			mask1 = ~ak.is_none(arr)
			subarr = arr[mask1]
			mask2 = subarr !=0
			return ak.to_numpy(subarr[mask2])
Ejemplo n.º 7
0
		def drop_na(arr):

			mask = ~ak.is_none(arr)

			return arr[mask]
Ejemplo n.º 8
0
		def flat_dim(arr):

			sub_arr = ak.flatten(arr)
			mask = ~ak.is_none(sub_arr)

			return ak.to_numpy(sub_arr[mask])
Ejemplo n.º 9
0
def runOneFile(filename):
    #print ("filename: ", filename)
    #inputfile=filename
    outputfile = "output/" + inputfile.split("/")[-1]
    #outputfile = "tmp.root"
    mycache = uproot4.LRUArrayCache("1 MB")
    file_ = uproot4.open(inputfile, num_workers=10)
    #print ("root file opened: ", filename)
    nevents = ak.to_list(file_["h_total_mcweight"].values())[2]
    #nevents = 1000000
    print("histogram opened: ", nevents)

    #tree_ = uproot4.open(inputfile, num_workers=10)["outTree"].arrays(array_cache=mycache)
    tree_ = file_["outTree"].arrays(array_cache=mycache)
    print("tree length", len(tree_))

    #tree_ = uproot4.open(inputfile)[trees[0]].arrays()
    #tree_ = uproot4.open(inputfile)["outTree"].arrays(array_cache=mycache)
    #tree_ = uproot4.open("Merged_WJetsInclusiveSkim.root")["outTree"].arrays(array_cache=mycache)
    #tree_ = uproot4.open("/eos/cms/store/group/phys_exotica/bbMET/2016_SkimmedFiles/skim_setup_2016_v16_07-00/crab_DYJetsToLL_M-50_HT-400to600_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_200918_215129_0000_0.root")["outTree"].arrays(array_cache=mycache)
    #tree_ = uproot4.open("/eos/cms/store/group/phys_exotica/bbMET/2016_SkimmedFiles/skim_setup_2016_v16_07-00/crab_ttHTobb_M125_13TeV_powheg_pythia8_200918_215950_0000_0.root")["outTree"].arrays(array_cache=mycache)
    #print ((tree_))

    cms_events = ak.zip(
        {
            "run":
            tree_["st_runId"],
            "lumi":
            tree_["st_lumiSection"],
            "event":
            tree_["st_eventId"],
            "jetpx":
            tree_["st_THINjetPx"],
            "jetpy":
            tree_["st_THINjetPy"],
            "jetpz":
            tree_["st_THINjetPz"],
            "jete":
            tree_["st_THINjetEnergy"],
            "jetpt":
            getpt(tree_["st_THINjetPx"], tree_["st_THINjetPy"]),
            "jeteta":
            geteta(tree_["st_THINjetPx"], tree_["st_THINjetPy"],
                   tree_["st_THINjetPz"]),
            "jetphi":
            getphi(tree_["st_THINjetPx"], tree_["st_THINjetPy"]),
            "jetcsv":
            tree_["st_THINjetDeepCSV"],
            "jetflav":
            tree_["st_THINjetHadronFlavor"],
            "metpt":
            tree_["st_pfMetCorrPt"],
            "metphi":
            tree_["st_pfMetCorrPhi"],
            "mettrig":
            tree_["st_mettrigdecision"],
            "elepx":
            tree_["st_elePx"],
            "elepy":
            tree_["st_elePy"],
            "elepz":
            tree_["st_elePz"],
            "elee":
            tree_["st_eleEnergy"],
            "eleidL":
            tree_["st_eleIsPassLoose"],
            "eleidT":
            tree_["st_eleIsPassTight"],
            "eleq":
            tree_["st_eleCharge"],
            "elept":
            getpt(tree_["st_elePx"], tree_["st_elePy"]),
            "eleeta":
            geteta(tree_["st_elePx"], tree_["st_elePy"], tree_["st_elePz"]),
            "elephi":
            getphi(tree_["st_elePx"], tree_["st_elePy"]),
            "mupx":
            tree_["st_muPx"],
            "mupy":
            tree_["st_muPy"],
            "mupz":
            tree_["st_muPz"],
            "mue":
            tree_["st_muEnergy"],
            "muidT":
            tree_["st_isTightMuon"],
            "muq":
            tree_["st_muCharge"],
            "mupt":
            getpt(tree_["st_muPx"], tree_["st_muPy"]),
            "mueta":
            geteta(tree_["st_muPx"], tree_["st_muPy"], tree_["st_muPz"]),
            "muphi":
            getphi(tree_["st_muPx"], tree_["st_muPy"]),
            "ntau":
            tree_["st_nTau_discBased_TightEleTightMuVeto"],
            "npho":
            tree_["st_nPho"],
            "phopx":
            tree_["st_phoPx"],
            "phopy":
            tree_["st_phoPy"],
            "phopz":
            tree_["st_phoPz"],
            "phoe":
            tree_["st_phoEnergy"],
            "phopt":
            getpt(tree_["st_phoPx"], tree_["st_phoPy"]),
            "phoeta":
            geteta(tree_["st_phoPx"], tree_["st_phoPy"], tree_["st_phoPz"]),
            "nTrueInt":
            tree_["st_pu_nTrueInt"],
            "nPUVert":
            tree_["st_pu_nPUVert"],
            "genpt":
            tree_["st_genParPt"]
        },
        depth_limit=1)

    out_events = ak.zip(
        {
            "run": tree_["st_runId"],
            "lumi": tree_["st_lumiSection"],
            "event": tree_["st_eventId"]
        },
        depth_limit=1)

    print("event loading done")
    print("# of events: ", len(cms_events))

    ## add more columns/properties to the event
    cms_events["mu_sel_tight"] = (cms_events.mupt > 30) & (
        cms_events.muidT == True) & (numpy.abs(cms_events.mueta) < 2.4)
    cms_events["mu_sel_tight0"] = ak.Array(getN(cms_events.mu_sel_tight, 0))
    cms_events["nMuTight"] = ak.sum(cms_events.mu_sel_tight, axis=-1)
    cms_events["nMuLoose"] = ak.sum((cms_events.mupt > 10), axis=-1)
    cms_events["mu_q0"] = ak.Array(getN(cms_events.muq, 0))
    cms_events["mu_q1"] = ak.Array(getN(cms_events.muq, 1))

    cms_events["ele_sel_tight"] = (cms_events.eleidT == True) & (
        cms_events.elept > 30) & (numpy.abs(cms_events.eleeta) < 2.5)
    cms_events["ele_sel_tight0"] = ak.Array(getN(cms_events.ele_sel_tight, 0))
    cms_events["nEleTight"] = ak.sum(cms_events.ele_sel_tight, axis=-1)
    cms_events["nEleLoose"] = ak.sum((cms_events.elept > 10), axis=-1)
    cms_events["ele_q0"] = ak.Array(getN(cms_events.eleq, 0))
    cms_events["ele_q1"] = ak.Array(getN(cms_events.eleq, 1))

    cms_events["recoil_Wmunu"] = getrecoil(cms_events.nMuTight,
                                           cms_events.mupt, cms_events.muphi,
                                           cms_events.mupx, cms_events.mupy,
                                           cms_events.metpt, cms_events.metphi)
    cms_events["recoil_Wmunu0"] = ak.firsts(cms_events.recoil_Wmunu)
    cms_events["recoil_Wenu"] = getrecoil(cms_events.nEleTight,
                                          cms_events.elept, cms_events.elephi,
                                          cms_events.elepx, cms_events.elepy,
                                          cms_events.metpt, cms_events.metphi)
    cms_events["recoil_Wenu0"] = ak.firsts(cms_events.recoil_Wenu)

    elepx0 = ak.Array(getN(cms_events.elepx, 0))
    elepx1 = ak.Array(getN(cms_events.elepx, 1))

    elepy0 = ak.Array(getN(cms_events.elepy, 0))
    elepy1 = ak.Array(getN(cms_events.elepy, 1))

    elepz0 = ak.Array(getN(cms_events.elepz, 0))
    elepz1 = ak.Array(getN(cms_events.elepz, 1))

    elee0 = ak.Array(getN(cms_events.elee, 0))
    elee1 = ak.Array(getN(cms_events.elee, 1))

    cms_events["Zee_mass"] = numpy.sqrt((elee0 + elee1)**2 -
                                        (elepx0 + elepx1)**2 -
                                        (elepy0 + elepy1)**2 -
                                        (elepz0 + elepz1)**2)
    cms_events["Zee_pt"] = numpy.sqrt((elepx0 + elepx1)**2 +
                                      (elepy0 + elepy1)**2)
    cms_events["Zee_recoil"] = getrecoil1((elepx0 + elepx1), (elepy0 + elepy1),
                                          cms_events.metpt, cms_events.metphi)

    mupx0 = ak.Array(getN(cms_events.mupx, 0))
    mupx1 = ak.Array(getN(cms_events.mupx, 1))

    mupy0 = ak.Array(getN(cms_events.mupy, 0))
    mupy1 = ak.Array(getN(cms_events.mupy, 1))

    mupz0 = ak.Array(getN(cms_events.mupz, 0))
    mupz1 = ak.Array(getN(cms_events.mupz, 1))

    mue0 = ak.Array(getN(cms_events.mue, 0))
    mue1 = ak.Array(getN(cms_events.mue, 1))

    cms_events["Zmumu_mass"] = numpy.sqrt((mue0 + mue1)**2 -
                                          (mupx0 + mupx1)**2 -
                                          (mupy0 + mupy1)**2 -
                                          (mupz0 + mupz1)**2)
    cms_events["Zmumu_pt"] = numpy.sqrt((mupx0 + mupx1)**2 +
                                        (mupy0 + mupy1)**2)
    cms_events["Zmumu_recoil"] = getrecoil1(
        (mupx0 + mupx1), (mupy0 + mupy1), cms_events.metpt, cms_events.metphi)

    #cms_events["recoil_Zmumu"] = getrecoil
    cms_events["recoil_WmunuPhi"] = getRecoilPhi(
        cms_events.nMuTight, cms_events.mupt, cms_events.muphi,
        cms_events.mupx, cms_events.mupy, cms_events.metpt, cms_events.metphi)
    cms_events["recoil_WmunuPhi0"] = ak.firsts(cms_events.recoil_WmunuPhi)

    cms_events["recoil_WenuPhi"] = getRecoilPhi(
        cms_events.nEleTight, cms_events.elept, cms_events.elephi,
        cms_events.elepx, cms_events.elepy, cms_events.metpt,
        cms_events.metphi)
    cms_events["recoil_WenuPhi0"] = ak.firsts(cms_events.recoil_WenuPhi)

    cms_events["mt_Wmunu"] = getMT(cms_events.nMuTight, cms_events.mupt,
                                   cms_events.muphi, cms_events.mupx,
                                   cms_events.mupy, cms_events.metpt,
                                   cms_events.metphi)
    cms_events["mt_Wmunu0"] = ak.firsts(cms_events.mt_Wmunu)

    cms_events["mt_Wenu"] = getMT(cms_events.nEleTight, cms_events.elept,
                                  cms_events.elephi, cms_events.elepx,
                                  cms_events.elepy, cms_events.metpt,
                                  cms_events.metphi)
    cms_events["mt_Wenu0"] = ak.firsts(cms_events.mt_Wenu)

    cms_events["jet_sel_loose"] = (cms_events.jetpt > 30.0) & (numpy.abs(
        cms_events.jeteta) < 2.5)
    cms_events["jet_sel_tight"] = (cms_events.jetpt > 50.0) & (numpy.abs(
        cms_events.jeteta) < 2.5)
    #cms_events["jet_sel_b"]     = (cms_events.jetcsv > 0.6321) & (numpy.abs(cms_events.jeteta)<2.4)
    cms_events["jet_sel_b"] = (
        cms_events.jetcsv[cms_events.jet_sel_loose == True] > 0.6321
    ) & (numpy.abs(cms_events.jeteta[cms_events.jet_sel_loose == True]) < 2.4)

    cms_events["jetptTight"] = cms_events.jetpt[cms_events.jet_sel_tight ==
                                                True]
    cms_events["jetetaTight"] = cms_events.jeteta[cms_events.jet_sel_tight ==
                                                  True]
    cms_events["jetphiTight"] = cms_events.jetphi[cms_events.jet_sel_tight ==
                                                  True]

    cms_events["jetptLoose"] = cms_events.jetpt[cms_events.jet_sel_loose ==
                                                True]
    cms_events["jetetaLoose"] = cms_events.jeteta[cms_events.jet_sel_loose ==
                                                  True]
    cms_events["jetphiLoose"] = cms_events.jetphi[cms_events.jet_sel_loose ==
                                                  True]

    cms_events["jet_sel_tight0"] = ak.Array(
        getN(cms_events.jet_sel_tight[cms_events.jet_sel_loose == True], 0))
    cms_events["jet_sel_b_0"] = ak.Array(getN(cms_events.jet_sel_b, 0))
    cms_events["jet_sel_b_1"] = ak.Array(getN(cms_events.jet_sel_b, 1))

    cms_events["nJetLoose"] = ak.sum(cms_events.jet_sel_loose, axis=-1)
    cms_events["nJetTight"] = ak.sum(cms_events.jet_sel_tight, axis=-1)
    cms_events["nJetb"] = ak.sum(cms_events.jet_sel_b, axis=-1)

    cms_events["dphi_jet_met"] = DeltaPhi(
        cms_events.jetphi[cms_events.jet_sel_loose == True], cms_events.metphi)
    cms_events["min_dphi_jet_met"] = ak.min(cms_events.dphi_jet_met, axis=-1)

    #--------------------------------------------------------------------------------------------------
    ## W --> lepton + nu
    #--------------------------------------------------------------------------------------------------
    from regions import get_mask_wmunu1b, get_mask_wmunu2b, get_mask_wenu1b, get_mask_wenu2b, get_mask_topmunu1b, get_mask_topmunu2b, get_mask_topenu1b, get_mask_topenu2b, get_mask_Zmumu1b, get_mask_Zmumu2b, get_mask_Zee1b, get_mask_Zee2b, get_mask_SR1b, get_mask_SR2b

    cms_events["mask_wmunu1b"] = get_mask_wmunu1b(cms_events)
    cms_events["mask_wmunu2b"] = get_mask_wmunu2b(cms_events)
    cms_events["mask_wenu1b"] = get_mask_wenu1b(cms_events)
    cms_events["mask_wenu2b"] = get_mask_wenu2b(cms_events)
    cms_events["mask_topmunu1b"] = get_mask_topmunu1b(cms_events)
    cms_events["mask_topmunu2b"] = get_mask_topmunu2b(cms_events)
    cms_events["mask_topenu1b"] = get_mask_topenu1b(cms_events)
    cms_events["mask_topenu2b"] = get_mask_topenu2b(cms_events)
    cms_events["mask_Zmumu1b"] = get_mask_Zmumu1b(cms_events)
    cms_events["mask_Zmumu2b"] = get_mask_Zmumu2b(cms_events)
    cms_events["mask_Zee1b"] = get_mask_Zee1b(cms_events)
    cms_events["mask_Zee2b"] = get_mask_Zee2b(cms_events)
    cms_events["mask_SR1b"] = get_mask_SR1b(cms_events)
    cms_events["mask_SR2b"] = get_mask_SR2b(cms_events)
    '''
    wm = cms_events.event[mask_SR2b]
    wm[~ak.is_none(wm)]
    '''

    ###############
    out_events["metpt"] = cms_events["metpt"]
    out_events["metphi"] = cms_events["metphi"]
    out_events["nTrueInt"] = cms_events["nTrueInt"]
    out_events["nJetLoose"] = cms_events["nJetLoose"]

    out_events["mu_sel_tight0"] = cms_events["mu_sel_tight0"]
    out_events["nMuTight"] = cms_events["nMuTight"]
    out_events["nMuLoose"] = cms_events["nMuLoose"]
    out_events["mu_q0"] = cms_events["mu_q0"]
    out_events["mu_q1"] = cms_events["mu_q1"]
    out_events["mupt0"] = ak.Array(getN(cms_events.mupt, 0))
    out_events["mupt1"] = ak.Array(getN(cms_events.mupt, 1))
    out_events["mueta0"] = ak.Array(getN(cms_events.mueta, 0))
    out_events["mueta1"] = ak.Array(getN(cms_events.mueta, 1))
    out_events["muphi0"] = ak.Array(getN(cms_events.muphi, 0))
    out_events["muphi1"] = ak.Array(getN(cms_events.muphi, 1))

    out_events["ele_sel_tight0"] = cms_events["ele_sel_tight0"]
    out_events["nEleTight"] = cms_events["nEleTight"]
    out_events["nEleLoose"] = cms_events["nEleLoose"]
    out_events["ele_q0"] = cms_events["ele_q0"]
    out_events["ele_q1"] = cms_events["ele_q1"]
    out_events["elept0"] = ak.Array(getN(cms_events.elept, 0))
    out_events["elept1"] = ak.Array(getN(cms_events.elept, 1))
    out_events["eleeta0"] = ak.Array(getN(cms_events.eleeta, 0))
    out_events["eleeta1"] = ak.Array(getN(cms_events.eleeta, 1))
    out_events["elephi0"] = ak.Array(getN(cms_events.elephi, 0))
    out_events["elephi1"] = ak.Array(getN(cms_events.elephi, 1))

    out_events["recoil_Wmunu0"] = cms_events["recoil_Wmunu0"]
    out_events["recoil_Wenu0"] = cms_events["recoil_Wenu0"]
    out_events["recoil_WmunuPhi0"] = cms_events["recoil_WmunuPhi0"]
    out_events["recoil_WenuPhi0"] = cms_events["recoil_WenuPhi0"]
    out_events["mt_Wmunu0"] = cms_events["mt_Wmunu0"]
    out_events["mt_Wenu0"] = cms_events["mt_Wenu0"]

    out_events["Zee_mass"] = cms_events["Zee_mass"]
    out_events["Zee_pt"] = cms_events["Zee_pt"]
    out_events["Zee_recoil"] = cms_events["Zee_recoil"]
    out_events["Zmumu_mass"] = cms_events["Zmumu_mass"]
    out_events["Zmumu_pt"] = cms_events["Zmumu_pt"]
    out_events["Zmumu_recoil"] = cms_events["Zmumu_recoil"]

    out_events["nJetLoose"] = cms_events["nJetLoose"]
    out_events["nJetTight"] = cms_events["nJetTight"]
    out_events["nJetb"] = cms_events["nJetb"]
    out_events["min_dphi_jet_met"] = cms_events["min_dphi_jet_met"]
    cms_events["jet_sel_tight0"] = cms_events["jet_sel_tight0"]
    cms_events["jet_sel_b_0"] = cms_events["jet_sel_b_0"]
    cms_events["jet_sel_b_1"] = cms_events["jet_sel_b_1"]

    out_events["jetpt0"] = ak.Array(getN(cms_events.jetptTight, 0))
    out_events["jetpt1"] = ak.Array(getN(cms_events.jetptLoose, 1))
    out_events["jetpt2"] = ak.Array(getN(cms_events.jetptLoose, 2))
    out_events["jetpt3"] = ak.Array(getN(cms_events.jetptLoose, 3))
    out_events["jetpt4"] = ak.Array(getN(cms_events.jetptLoose, 4))
    out_events["jetpt5"] = ak.Array(getN(cms_events.jetptLoose, 5))
    out_events["jetpt6"] = ak.Array(getN(cms_events.jetptLoose, 6))

    out_events["jeteta0"] = ak.Array(getN(cms_events.jetetaTight, 0))
    out_events["jeteta1"] = ak.Array(getN(cms_events.jetetaLoose, 1))
    out_events["jeteta2"] = ak.Array(getN(cms_events.jetetaLoose, 2))
    out_events["jeteta3"] = ak.Array(getN(cms_events.jetetaLoose, 3))
    out_events["jeteta4"] = ak.Array(getN(cms_events.jetetaLoose, 4))
    out_events["jeteta5"] = ak.Array(getN(cms_events.jetetaLoose, 5))
    out_events["jeteta6"] = ak.Array(getN(cms_events.jetetaLoose, 6))

    out_events["jetphi0"] = ak.Array(getN(cms_events.jetphiTight, 0))
    out_events["jetphi1"] = ak.Array(getN(cms_events.jetphiLoose, 1))
    out_events["jetphi2"] = ak.Array(getN(cms_events.jetphiLoose, 2))

    out_events["jetflav0"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_tight == True], 0))
    out_events["jetflav1"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_loose == True], 1))
    out_events["jetflav2"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_loose == True], 2))
    out_events["jetflav3"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_loose == True], 3))
    out_events["jetflav4"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_loose == True], 4))
    out_events["jetflav5"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_loose == True], 5))
    out_events["jetflav6"] = ak.Array(
        getN(cms_events.jetflav[cms_events.jet_sel_loose == True], 6))

    out_events["csv0"] = ak.Array(
        getN(cms_events.jetcsv[cms_events.jet_sel_tight == True], 0))
    out_events["csv1"] = ak.Array(
        getN(cms_events.jetcsv[cms_events.jet_sel_loose == True], 1))
    out_events["csv2"] = ak.Array(
        getN(cms_events.jetcsv[cms_events.jet_sel_loose == True], 2))
    out_events["csv3"] = ak.Array(
        getN(cms_events.jetcsv[cms_events.jet_sel_loose == True], 3))

    out_events["SR_2b"] = cms_events["mask_SR2b"]
    out_events["SR_1b"] = cms_events["mask_SR1b"]
    out_events["ZeeCR_2b"] = cms_events["mask_Zee2b"]
    out_events["ZeeCR_1b"] = cms_events["mask_Zee1b"]
    out_events["ZmumuCR_2b"] = cms_events["mask_Zmumu2b"]
    out_events["ZmumuCR_1b"] = cms_events["mask_Zmumu1b"]
    out_events["TopenuCR_2b"] = cms_events["mask_topenu2b"]
    out_events["TopenuCR_1b"] = cms_events["mask_topenu1b"]
    out_events["TopmunuCR_2b"] = cms_events["mask_topmunu2b"]
    out_events["TopmunuCR_1b"] = cms_events["mask_topmunu1b"]
    out_events["WenuCR_1b"] = cms_events["mask_wenu1b"]
    out_events["WenuCR_2b"] = cms_events["mask_wenu2b"]
    out_events["WmunuCR_1b"] = cms_events["mask_wmunu1b"]
    out_events["WmunuCR_2b"] = cms_events["mask_wmunu2b"]

    ## btagging SFs
    from read_sfs import btag_sf
    from read_sfs import evaluator

    out_events["btagsf0"] = btag_sf.eval("central", out_events.jetflav0,
                                         abs(out_events.jeteta0),
                                         out_events.jetpt0)
    out_events["btagsf1"] = btag_sf.eval("central", out_events.jetflav1,
                                         abs(out_events.jeteta1),
                                         out_events.jetpt1)
    out_events["btagsf2"] = btag_sf.eval("central", out_events.jetflav2,
                                         abs(out_events.jeteta2),
                                         out_events.jetpt2)
    out_events["btagsf3"] = btag_sf.eval("central", out_events.jetflav3,
                                         abs(out_events.jeteta3),
                                         out_events.jetpt3)
    out_events["btagsf4"] = btag_sf.eval("central", out_events.jetflav4,
                                         abs(out_events.jeteta4),
                                         out_events.jetpt4)
    out_events["btagsf5"] = btag_sf.eval("central", out_events.jetflav5,
                                         abs(out_events.jeteta5),
                                         out_events.jetpt5)
    out_events["btagsf6"] = btag_sf.eval("central", out_events.jetflav6,
                                         abs(out_events.jeteta6),
                                         out_events.jetpt6)

    ## btag efficiency
    out_events["btag_eff_lwp_0"] = evaluator["btag_eff_lwp"](
        out_events.jeteta0, out_events.jetpt0)
    out_events["btag_eff_lwp_1"] = evaluator["btag_eff_lwp"](
        out_events.jeteta1, out_events.jetpt1)

    out_events["ctag_eff_lwp_0"] = evaluator["ctag_eff_lwp"](
        out_events.jeteta0, out_events.jetpt0)
    out_events["ctag_eff_lwp_1"] = evaluator["ctag_eff_lwp"](
        out_events.jeteta1, out_events.jetpt1)

    out_events["ltag_eff_lwp_0"] = evaluator["ltag_eff_lwp"](
        out_events.jeteta0, out_events.jetpt0)
    out_events["ltag_eff_lwp_1"] = evaluator["ltag_eff_lwp"](
        out_events.jeteta1, out_events.jetpt1)

    out_events["btag_eff_mwp_0"] = evaluator["btag_eff_mwp"](
        out_events.jeteta0, out_events.jetpt0)
    out_events["btag_eff_mwp_1"] = evaluator["btag_eff_mwp"](
        out_events.jeteta1, out_events.jetpt1)

    out_events["ctag_eff_mwp_0"] = evaluator["ctag_eff_mwp"](
        out_events.jeteta0, out_events.jetpt0)
    out_events["ctag_eff_mwp_1"] = evaluator["ctag_eff_mwp"](
        out_events.jeteta1, out_events.jetpt1)

    out_events["ltag_eff_mwp_0"] = evaluator["ltag_eff_mwp"](
        out_events.jeteta0, out_events.jetpt0)
    out_events["ltag_eff_mwp_1"] = evaluator["ltag_eff_mwp"](
        out_events.jeteta1, out_events.jetpt1)

    ## ele sfs
    out_events["eleTightSF0"] = evaluator["EGamma_SF2D_T"](out_events.eleeta0,
                                                           out_events.elept0)
    out_events["eleLooseSF1"] = evaluator["EGamma_SF2D_L"](out_events.eleeta1,
                                                           out_events.elept1)
    out_events["eleTrigSF0"] = evaluator["EGamma_SF2D_Trig"](
        out_events.eleeta0, out_events.elept0)
    out_events["eleRecoSF0"] = evaluator["EGamma_SF2D_Reco"](
        out_events.eleeta0, out_events.elept0)

    eleRecoSF1_hi = evaluator["EGamma_SF2D_Reco"](out_events.eleeta1,
                                                  out_events.elept1)
    eleRecoSF1_lo = evaluator["EGamma_SF2D_Reco_lowpt"](out_events.eleeta1,
                                                        out_events.elept1)

    eleRecoSF1_hi_ = ak.fill_none(
        ak.mask(eleRecoSF1_hi, out_events.elept1 > 20.), 0)
    eleRecoSF1_lo_ = ak.fill_none(
        ak.mask(eleRecoSF1_lo, out_events.elept1 > 20.), 0)
    out_events["eleRecoSF1"] = eleRecoSF1_hi_ + eleRecoSF1_lo_

    ## muon sfs
    bcdef_lumi = 19.554725529
    gh_lumi = 16.224846377
    total_lumi = bcdef_lumi + gh_lumi

    ##--------low pt Loose
    muonLooseIDSF_lowpt1 = (
        (bcdef_lumi * evaluator["muon_lowpt_BCDEF_LooseID"]
         (out_events.mupt1, abs(out_events.mueta1))) +
        (gh_lumi * evaluator["muon_lowpt_GH_LooseID"]
         (out_events.mupt1, abs(out_events.mueta1)))) / total_lumi

    ##----------- medium pt Loose
    muonLooseIDSF1 = ((bcdef_lumi * evaluator["muon_highpt_BCDEF_LooseID"]
                       (out_events.mueta1, out_events.mupt1)) +
                      (gh_lumi * evaluator["muon_highpt_GH_LooseID"]
                       (out_events.mueta1, out_events.mupt1))) / total_lumi

    muonLooseISOSF1 = ((bcdef_lumi * evaluator["muon_highpt_BCDEF_LooseISO"]
                        (out_events.mueta1, out_events.mupt1)) +
                       (gh_lumi * evaluator["muon_highpt_GH_LooseISO"]
                        (out_events.mueta1, out_events.mupt1))) / total_lumi

    muon_loose_ID_low_SF_1 = ak.fill_none(
        ak.mask(muonLooseIDSF_lowpt1, out_events.mupt1 < 20.), 0)
    muon_loose_ID_high_SF_1 = ak.fill_none(
        ak.mask(muonLooseIDSF1, out_events.mupt1 > 20.), 0)

    muon_loose_ID_SF_1 = muon_loose_ID_low_SF_1 + muon_loose_ID_high_SF_1

    out_events["muLooseSF1"] = muon_loose_ID_SF_1 * muonLooseISOSF1

    ##------------medium pt tight
    muonTightIDSF0 = ((bcdef_lumi * evaluator["muon_highpt_BCDEF_TightID"]
                       (out_events.mueta0, out_events.mupt0)) +
                      (gh_lumi * evaluator["muon_highpt_GH_TightID"]
                       (out_events.mueta0, out_events.mupt0))) / total_lumi

    muonTightISOSF0 = ((bcdef_lumi * evaluator["muon_highpt_BCDEF_TightISO"]
                        (out_events.mueta0, out_events.mupt0)) +
                       (gh_lumi * evaluator["muon_highpt_GH_TightISO"]
                        (out_events.mueta0, out_events.mupt0))) / total_lumi

    out_events["muTightSF0"] = muonTightIDSF0 * muonTightISOSF0

    out_events["puweight"] = evaluator["pu_weight"](cms_events.nTrueInt)

    ## trigger sfs
    out_events["mettrigWeight"] = evaluator["met_trig"](cms_events.metpt)
    out_events["recoilWmunutrigWeight"] = evaluator["met_trig"](
        cms_events.recoil_Wmunu0)
    out_events["recoilWenutrigWeight"] = evaluator["met_trig"](
        cms_events.recoil_Wenu0)
    out_events["recoilZmumutrigWeight"] = evaluator["met_trig"](
        cms_events.Zmumu_recoil)
    out_events["recoilZeetrigWeight"] = evaluator["met_trig"](
        cms_events.Zee_recoil)

    ## Fill weights for each CR so that we don't need to worry later

    out_events["weight_SR_2b"] = out_events.puweight * out_events.mettrigWeight
    out_events["weight_SR_1b"] = out_events.puweight * out_events.mettrigWeight

    out_events["weight_ZeeCR_2b"] = out_events.puweight * out_events.eleTrigSF0
    out_events["weight_ZeeCR_1b"] = out_events.puweight * out_events.eleTrigSF0

    out_events[
        "weight_ZmumuCR_2b"] = out_events.puweight * out_events.recoilZmumutrigWeight
    out_events[
        "weight_ZmumuCR_1b"] = out_events.puweight * out_events.recoilZmumutrigWeight

    out_events[
        "weight_TopenuCR_2b"] = out_events.puweight * out_events.eleTrigSF0
    out_events[
        "weight_TopenuCR_1b"] = out_events.puweight * out_events.eleTrigSF0

    out_events[
        "weight_TopmunuCR_2b"] = out_events.puweight * out_events.recoilWmunutrigWeight
    out_events[
        "weight_TopmunuCR_1b"] = out_events.puweight * out_events.recoilWmunutrigWeight

    out_events[
        "weight_WenuCR_1b"] = out_events.puweight * out_events.eleTrigSF0
    out_events[
        "weight_WenuCR_2b"] = out_events.puweight * out_events.eleTrigSF0

    out_events[
        "weight_WmunuCR_1b"] = out_events.puweight * out_events.recoilWmunutrigWeight
    out_events[
        "weight_WmunuCR_2b"] = out_events.puweight * out_events.recoilWmunutrigWeight

    ## Fill Histograms
    from variables import vardict, regions, variables_common
    from binning import binning

    f = TFile(outputfile, "RECREATE")
    for ireg in regions:
        thisregion = out_events[out_events[ireg] == True]
        thisregion_ = thisregion[~(ak.is_none(thisregion))]
        weight_ = "weight_" + ireg

        for ivar in variables_common[ireg]:
            hist_name_ = "h_reg_" + ireg + "_" + vardict[ivar]
            h = VarToHist(thisregion_[ivar], thisregion_[weight_], hist_name_,
                          binning[ireg][ivar])
            f.cd()
            h.Write()

    h_total = TH1F("h_total_mcweight", "h_total_mcweight", 2, 0, 2)
    h_total.SetBinContent(1, nevents)
    f.cd()
    h_total.Write()

    write_parquet = False
    if write_parquet:
        ak.to_parquet(out_events, "analysis_wjets_allevents.parquet")