Exemple #1
0
def neut_eta():

    #neutron pseudorapidity

    etabin = 0.05
    etamin = -20
    etamax = 20

    hEta = ut.prepare_TH1D("hEta", etabin, etamin, etamax)

    can = ut.box_canvas()

    particles = TClonesArray("TParticle", 200)
    tree.SetBranchAddress("particles", particles)

    nev = tree.GetEntriesFast()
    #nev = 24
    for iev in xrange(nev):
        tree.GetEntry(iev)
        for imc in xrange(particles.GetEntriesFast()):
            part = particles.At(imc)
            if part.GetPdgCode() != 2112: continue
            hEta.Fill(part.Eta())

    ut.put_yx_tit(hEta, "Events", "#eta", 1.4, 1.2)

    hEta.Draw()

    gPad.SetGrid()

    gPad.SetLogy()

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")
Exemple #2
0
    def make_root(self, parse):

        #ROOT output

        nam = parse.get("main", "nam").strip("\"'") + ".root"
        print("ROOT output name:", nam)

        self.out_root = TFile(nam, "recreate")
        #tree variables, all Double_t
        tlist = ["phot_en", "phot_theta", "phot_phi"]
        tlist += ["el_en", "el_theta", "el_phi"]
        #C structure holding the variables
        struct = "struct tree_out { Double_t "
        for i in tlist:
            struct += i + ", "
        struct = struct[:-2] + ";};"
        gROOT.ProcessLine(struct)
        #create the output tree
        self.tree_out = rt.tree_out()  # instance of the C structure
        self.ltree = TTree("ltree", "ltree")
        for i in tlist:
            exec("self.tree_out." + i + "=0")
            self.ltree.Branch(i, addressof(self.tree_out, i), i + "/D")

        #particles array
        self.particles_out = TClonesArray("TParticle")
        self.particles_out.SetOwner(True)
        self.ltree.Branch("particles", self.particles_out)

        atexit.register(self.close_root)
Exemple #3
0
def pion(args, figname):
    datatype = args[0]
    label = args[1]
    test = get_options(args, 'test')
    batch = get_options(args, 'batch')

    if batch:
        cmd = create_batch_cmd()
        bashname = '%s.sh' %figname
        bashfile = create_bashfile_cmd(cmd, bashname, label, test=test)
        logfile = set_logfile('fig', datatype, label, figname)
        jobname = 'figptpion'
        bsub_jobs(logfile, jobname, bashfile, test)
        return

    figfile = set_figfile(figname, label, '.pdf', test=test)
    rootfile = atr.rootfile(datatype, label, test=test)
    obj = atr.root_tree_obj(datatype, label)
    chain = root_chain(rootfile, obj)

    canvas = TCanvas("aCanvas", "Canvas", 600, 600)
    hist = TH1F('pionpt', '#pi p_{T}', 100, 0, 20)
 
    Gen_Pion_P4_ = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('Gen_Pion_P4', AddressOf(Gen_Pion_P4_))

    PionPP4_ = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('PionPP4', AddressOf(PionPP4_))

    ntot = chain.GetEntries()
    if test:
        ntot = 1000

    sys.stdout.write('Processing %s events ...\n' %ntot)
    sys.stdout.flush()
    nfill = 0 
    for i in xrange(ntot):
        chain.LoadTree(i)
        chain.GetEntry(i)

        if 'GEN' in label:
            pionp4 = chain.Gen_Pion_P4[0]
        else:
            if chain.nXcand <= 0:
                continue
            pionp4 = pionp4 = chain.PionPP4[0]
        hist.Fill(pionp4.Pt())
        nfill += 1
        
    sys.stdout.write('Filled %s events. \n' %nfill)
    hist.GetXaxis().SetTitle('p_{T} (GeV/c)')
    hist.Draw()
    canvas.SaveAs(figfile)
    hist.Delete()
        
Exemple #4
0
def neut_en():

    #neutron energy

    ebin = 0.5
    emin = 50
    #emax = 200
    emax = 1000
    #ebin = 10
    #emin = 500
    #emax = 3000

    hE = ut.prepare_TH1D("hE", ebin, emin, emax)

    can = ut.box_canvas()

    particles = TClonesArray("TParticle", 200)
    tree.SetBranchAddress("particles", particles)

    nev = tree.GetEntriesFast()
    #nev = 24
    for iev in xrange(nev):
        tree.GetEntry(iev)

        esum = 0.

        for imc in xrange(particles.GetEntriesFast()):

            part = particles.At(imc)
            if part.GetPdgCode() != 2112: continue

            esum += part.Energy()

        hE.Fill(esum)

    ut.put_yx_tit(hE, "Events", "E (GeV)", 1.4, 1.2)

    hE.Draw()

    gPad.SetGrid()

    gPad.SetLogy()

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")
Exemple #5
0
def neut_abs_eta():

    #neutron absolute pseudorapidity

    etabin = 0.05
    etamin = 5
    etamax = 20

    hEta = ut.prepare_TH1D("hEta", etabin, etamin, etamax)

    can = ut.box_canvas()

    particles = TClonesArray("TParticle", 200)
    tree.SetBranchAddress("particles", particles)

    nev = tree.GetEntriesFast()
    #nev = 24
    for iev in xrange(nev):
        tree.GetEntry(iev)
        for imc in xrange(particles.GetEntriesFast()):
            part = particles.At(imc)
            if part.GetPdgCode() != 2112: continue
            #hEta.Fill( TMath.Abs(part.Eta()) )
            hEta.Fill(abs(part.Eta()))

    ytit = "Events / {0:.2f}".format(etabin)
    ut.put_yx_tit(hEta, ytit, "Neutron |#kern[0.3]{#eta}|", 1.4, 1.2)

    ut.set_H1D_col(hEta, rt.kRed)

    ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.01, 0.02)

    hEta.Draw()

    gPad.SetGrid()

    gPad.SetLogy()

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")
Exemple #6
0
    def __init__(self, infile):

        #energy in event at positive and negative rapidity
        self.epos = 0.
        self.eneg = 0.

        #number of neutrons at positive and negative rapidity
        self.npos = 0
        self.nneg = 0

        #flag for XnXn event
        self.is_XnXn = False

        #flag for central event
        self.is_Cen = False

        #J/psi kinematics
        self.pT = 0.
        self.y = 0.
        self.m = 0.

        #absolute eta for electron and positron
        self.aeta_max = 1.

        #minimal electron and positron for central trigger
        #self.p_min = 1.014

        #open the input
        self.inp = TFile.Open(infile)
        self.tree = self.inp.Get("slight_tree")

        #connect the input tree
        self.particles = TClonesArray("TParticle", 200)
        self.tree.SetBranchAddress("particles", self.particles)

        #number of events in input tree
        self.nev = self.tree.GetEntriesFast()
Exemple #7
0
def read_particles():

    #particles clones array

    #tree.Print()

    particles = TClonesArray("TParticle", 200)
    tree.SetBranchAddress("particles", particles)

    #nev = tree.GetEntriesFast()
    nev = 12
    for iev in xrange(nev):
        tree.GetEntry(iev)

        print iev

        nmc = particles.GetEntriesFast()

        for imc in xrange(nmc):

            part = particles.At(imc)

            print " ", imc, part.GetPdgCode(), part.Px(), part.Py(), part.Pz(
            ), part.Energy()
Exemple #8
0
class file_output:
    #output from the generator
    #_____________________________________________________________________________
    def __init__(self, parse):

        #create the individual outputs
        self.set_write_tx = False
        if parse.has_option("main", "write_tx"):
            self.set_write_tx = parse.getboolean("main", "write_tx")

        self.set_write_root = True
        if parse.has_option("main", "write_root"):
            self.set_write_root = parse.getboolean("main", "write_root")

        self.set_write_hepmc = False
        if parse.has_option("main", "write_hepmc"):
            self.set_write_hepmc = parse.getboolean("main", "write_hepmc")

        if self.set_write_tx: self.make_tx(parse)

        self.ltree = None
        if self.set_write_root: self.make_root(parse)

        self.hepmc_attrib = {}
        if self.set_write_hepmc: self.make_hepmc(parse)

    #_____________________________________________________________________________
    def make_tx(self, parse):

        #TX output

        nam = parse.get("main", "nam").strip("\"'") + ".tx"
        print("TX output name:", nam)

        self.tx_out = open(nam, "w")
        self.tx_ievt = 1

    #_____________________________________________________________________________
    def make_root(self, parse):

        #ROOT output

        nam = parse.get("main", "nam").strip("\"'") + ".root"
        print("ROOT output name:", nam)

        self.out_root = TFile(nam, "recreate")
        #tree variables, all Double_t
        tlist = ["phot_en", "phot_theta", "phot_phi"]
        tlist += ["el_en", "el_theta", "el_phi"]
        #C structure holding the variables
        struct = "struct tree_out { Double_t "
        for i in tlist:
            struct += i + ", "
        struct = struct[:-2] + ";};"
        gROOT.ProcessLine(struct)
        #create the output tree
        self.tree_out = rt.tree_out()  # instance of the C structure
        self.ltree = TTree("ltree", "ltree")
        for i in tlist:
            exec("self.tree_out." + i + "=0")
            self.ltree.Branch(i, addressof(self.tree_out, i), i + "/D")

        #particles array
        self.particles_out = TClonesArray("TParticle")
        self.particles_out.SetOwner(True)
        self.ltree.Branch("particles", self.particles_out)

        atexit.register(self.close_root)

    #_____________________________________________________________________________
    def make_hepmc(self, parse):

        #HepMC3 output

        global hepmc
        from pyHepMC3 import HepMC3 as hepmc

        nam = parse.get("main", "nam").strip("\"'") + ".hepmc"
        print("HepMC3 output name:", nam)

        self.hepmc_out = hepmc.WriterAscii(nam, hepmc.GenRunInfo())
        self.hepmc_ievt = 0

    #_____________________________________________________________________________
    def write_tx(self, tracks):

        #TX Starlight format

        if not self.set_write_tx: return

        #tracks and vertex position in cm
        tracks_tx = []
        vx = 0.
        vy = 0.
        vz = 0.
        #tracks loop
        for t in tracks:
            #only final particles
            if t.stat != 1: continue

            vx = t.vx / 10.
            vy = t.vy / 10.
            vz = t.vz / 10.

            t.write_tx(tracks_tx)

        #number of tracks for event and vertex lines
        ntrk = str(len(tracks_tx))

        #event line
        evtlin = "EVENT: " + str(self.tx_ievt) + " " + ntrk + " 1"
        self.tx_out.write(evtlin + "\n")

        #vertex line
        vtxlin = "VERTEX:"
        vtx_prec = 9
        if abs(vx) < 1e-9 and abs(vy) < 1e-9 and abs(vz) < 1e-9:
            vtx_prec = 0
        vtx_form = " {0:." + str(vtx_prec) + "f}"
        vtxlin += vtx_form.format(vx)
        vtxlin += vtx_form.format(vy)
        vtxlin += vtx_form.format(vz)
        vtxlin += " 0 1 0 0 " + ntrk
        self.tx_out.write(vtxlin + "\n")

        #track lines
        for tlin in tracks_tx:
            self.tx_out.write(tlin + "\n")

        self.tx_ievt += 1

    #_____________________________________________________________________________
    def write_root(self, tracks):

        #ROOT output

        if not self.set_write_root: return

        #initialize the particles array
        ipos = 0
        self.particles_out.Clear("C")

        t = self.tree_out

        for i in tracks:
            #select the final photon and electron
            if i.stat != 1: continue

            #put the particles to TParticles clones array
            i.write_tparticle(self.particles_out, ipos)
            ipos += 1

            #final photon
            if i.pdg == 22:

                t.phot_en = i.vec.Energy()
                t.phot_theta = i.vec.Theta()
                t.phot_phi = i.vec.Phi()

            #final electron
            if i.pdg == 11:

                t.el_en = i.vec.Energy()
                t.el_theta = i.vec.Theta()
                t.el_phi = i.vec.Phi()

        #fill the tree
        self.ltree.Fill()

    #_____________________________________________________________________________
    def write_hepmc(self, tracks):

        #HepMC3 format

        if not self.set_write_hepmc: return

        #hepmc event
        evt = hepmc.GenEvent(hepmc.Units.GEV, hepmc.Units.MM)
        evt.set_event_number(self.hepmc_ievt)

        #event attributes
        for i in self.hepmc_attrib:
            attr = hepmc.DoubleAttribute(self.hepmc_attrib[i])
            evt.add_attribute(i, attr)

        #vertex position
        if len(tracks) > 0:
            evt.shift_position_to(
                hepmc.FourVector(tracks[0].vx, tracks[0].vy, tracks[0].vz, 0))

        #tracks loop
        for t in tracks:
            #only final particles
            if t.stat != 1: continue

            evt.add_particle(t.make_hepmc_particle(hepmc))

        self.hepmc_out.write_event(evt)

        self.hepmc_ievt += 1

    #_____________________________________________________________________________
    def close_root(self):

        self.out_root.Write()
        self.out_root.Close()
Exemple #9
0
def output_evt_mass_b0(chain, datatype, label, job, test=False, pbar=False):
    skmfile = set_skmfile(datatype, label, 'skim', job)
    if test:
        skmfile = skmfile + '.test'

    f = TFile.Open(skmfile, 'RECREATE')
    t = TTree('skm', 'skm')

    s = SkimTree(t)

    nskm = 0
    ntot = 0

    xp4_ = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('xP4', AddressOf(xp4_))

    kstarp4_ = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('KstarP4', AddressOf(kstarp4_))

    entries = chain.GetEntries()

    if pbar:
        pb = get_progressbar(maxval=entries)

    for i in xrange(entries):
        ntot += 1
        if pbar:
            pb.update(i + 1)
        ientry = chain.LoadTree(i)
        if ientry < 0:
            break

        nb = chain.GetEntry(i)
        if nb <= 0:
            continue

        if not pass_trigger(chain, label):
            continue

        if chain.nXcand < 1:
            continue

        for nx in range(chain.nXcand):
            b0p4 = chain.xP4[nx]
            b0mass = b0p4.M()
            lxysig = chain.xlxySig[nx]
            input_var = (b0mass, lxysig)
            if not pass_b0_skim(input_var, label):
                continue

            fill_skim_tree(s, chain, nx)

        t.Fill()
        nskm += 1
        if nskm > 100 and test:
            break

    f.Write()
    f.Close()

    if pbar:
        pb.finish()

    return ntot, nskm, skmfile
Exemple #10
0
class file_io:
    #output from lgen
    #_____________________________________________________________________________
    def __init__(self, parse):

        #create the individual outputs
        self.set_write_dat = False
        if parse.has_option("lgen", "write_dat"):
            self.set_write_dat = parse.getboolean("lgen", "write_dat")

        self.set_write_tx = False
        if parse.has_option("lgen", "write_tx"):
            self.set_write_tx = parse.getboolean("lgen", "write_tx")

        self.set_write_root = True
        if parse.has_option("lgen", "write_root"):
            self.set_write_root = parse.getboolean("lgen", "write_root")

        if self.set_write_dat: self.make_dat(parse)
        if self.set_write_tx: self.make_tx(parse)

        self.ltree = None
        if self.set_write_root: self.make_root(parse)

    #_____________________________________________________________________________
    def make_dat(self, parse):

        #dat output, Pythia6 format

        #name for the output file
        nam = parse.get("lgen", "nam").strip("\"'") + "_evt.dat"
        print "Dat output name:", nam

        #lgen ascii output
        self.out = open(nam, "w")
        #event counter
        self.ievt = 1
        #header for the ascii output
        header = []
        header.append("  LGEN EVENT FILE ")  # LGEN
        header.append(" ============================================")
        header.append(
            "I, ievent, IChannel, process, subprocess, nucleon, struckparton, partontrck, Y, Q2, X, W2, NU, trueY, trueQ2, trueX, trueW2,trueNu, SIGtot, errSIGtot, D, F1NC, F3NC, G1NC,G3NC, A1NC, F1CC, F3CC, G1CC, G5CC, nrTracks"
        )
        header.append(" ============================================")
        header.append(
            "  I  K(I,1)  K(I,2)  K(I,3)  K(I,4)  K(I,5) P(I,1)  P(I,2)  P(I,3)  P(I,4)  P(I,5)  V(I,1)  V(I,2)  V(I,3)"
        )
        header.append(" ============================================")
        for i in header:
            self.out.write(i + "\n")

    #_____________________________________________________________________________
    def make_tx(self, parse):

        #TX output

        nam = parse.get("lgen", "nam").strip("\"'") + ".tx"
        print "TX output name:", nam

        self.tx_out = open(nam, "w")
        self.tx_ievt = 1

    #_____________________________________________________________________________
    def make_root(self, parse):

        #ROOT output

        nam = parse.get("lgen", "nam").strip("\"'") + ".root"
        print "ROOT output name:", nam

        self.out_root = TFile(nam, "recreate")
        #tree variables, all Double_t
        tlist = ["phot_en", "phot_theta", "phot_phi"]
        tlist += ["el_en", "el_theta", "el_phi"]
        #C structure holding the variables
        struct = "struct tree_out { Double_t "
        for i in tlist:
            struct += i + ", "
        struct = struct[:-2] + ";};"
        gROOT.ProcessLine(struct)
        #create the output tree
        self.tree_out = rt.tree_out()  # instance of the C structure
        self.ltree = TTree("ltree", "ltree")
        for i in tlist:
            exec("self.tree_out." + i + "=0")
            self.ltree.Branch(i, AddressOf(self.tree_out, i), i + "/D")

        #particles array
        self.particles_out = TClonesArray("TParticle")
        self.particles_out.SetOwner(True)
        self.ltree.Branch("particles", self.particles_out)

        atexit.register(self.close_root)

    #_____________________________________________________________________________
    def write_dat(self, tracks):
        #ascii output for the event, pythia6 format

        if not self.set_write_dat: return

        #write event header to the output
        self.out.write("   0")
        #event number
        self.out.write("{0:11d}".format(self.ievt))
        #placeholder for integer variables
        self.out.write("     15      4      2      1      0      0")
        #placeholder for event kinematics and cross section, 22 parameters, 10 decimal digits, 19 characters
        for ii in xrange(22):
            self.out.write("{0:19.10E}".format(0.))
        #number of tracks in the event
        self.out.write("{0:13d}".format(len(tracks)))
        self.out.write("\n")
        self.out.write(" ============================================\n")

        #put tracks in the event
        for i in tracks:
            i.write(self.out)

        #place event footer to the output
        self.out.write(" =============== Event finished ===============\n")
        #increment event count after writing the event
        self.ievt += 1

    #_____________________________________________________________________________
    def write_tx(self, tracks):

        #TX Starlight format

        if not self.set_write_tx: return

        #tracks and vertex position in cm
        tracks_tx = []
        vx = 0.
        vy = 0.
        vz = 0.
        #tracks loop
        for t in tracks:
            #only final particles
            if t.stat != 1: continue

            vx = t.vx / 10.
            vy = t.vy / 10.
            vz = t.vz / 10.

            t.write_tx(tracks_tx)

        #number of tracks for event and vertex lines
        ntrk = str(len(tracks_tx))

        #event line
        evtlin = "EVENT: " + str(self.tx_ievt) + " " + ntrk + " 1"
        self.tx_out.write(evtlin + "\n")

        #vertex line
        vtxlin = "VERTEX:"
        vtx_prec = 9
        if abs(vx) < 1e-9 and abs(vy) < 1e-9 and abs(vz) < 1e-9:
            vtx_prec = 0
        vtx_form = " {0:." + str(vtx_prec) + "f}"
        vtxlin += vtx_form.format(vx)
        vtxlin += vtx_form.format(vy)
        vtxlin += vtx_form.format(vz)
        vtxlin += " 0 1 0 0 " + ntrk
        self.tx_out.write(vtxlin + "\n")

        #track lines
        for tlin in tracks_tx:
            self.tx_out.write(tlin + "\n")

        self.tx_ievt += 1

    #_____________________________________________________________________________
    def write_root(self, tracks):

        #ROOT output

        if not self.set_write_root: return

        #initialize the particles array
        ipos = 0
        self.particles_out.Clear("C")

        t = self.tree_out

        for i in tracks:
            #select the final photon and electron
            if i.stat != 1: continue

            #put the particles to TParticles clones array
            i.write_tparticle(self.particles_out, ipos)
            ipos += 1

            #final photon
            if i.pdg == 22:

                t.phot_en = i.vec.Energy()
                t.phot_theta = i.vec.Theta()
                t.phot_phi = i.vec.Phi()

            #final electron
            if i.pdg == 11:

                t.el_en = i.vec.Energy()
                t.el_theta = i.vec.Theta()
                t.el_phi = i.vec.Phi()

        #fill the tree
        self.ltree.Fill()

    #_____________________________________________________________________________
    def close_root(self):

        self.out_root.Write()
        self.out_root.Close()
Exemple #11
0
def q2mumu(args, figname):
    datatype = args[0]
    label = args[1]
    test = get_options(args, 'test')
    batch = get_options(args, 'batch')

    if batch:
        cmd = create_batch_cmd()
        bashname = '%s.sh' % figname
        bashfile = create_bashfile_cmd(cmd, bashname, label, test=test)
        logfile = set_logfile('fig', datatype, label, figname)
        jobname = 'figq2mm'
        bsub_jobs(logfile, jobname, bashfile, test)
        return

    figfile = set_figfile(figname, label, '.pdf', test=test)

    #rootfile = get_rootfile(datatype, label)
    rootfile = atr.rootfile(datatype, label)

    obj = atr.root_tree_obj(datatype, label)
    chain = root_chain(rootfile, obj)

    canvas = TCanvas("aCanvas", "Canvas", 600, 600)
    hist = TH1F('mumumass', '#mu^{+} #mu^{-} mass', 100, 0, 25)

    if label in [
            'B2KstarMuMu/RECO_1M_v2.2', 'B2KstarMuMu/RECO_1M_v2.3',
            'B2KstarMuMu/RECO_100M_v1.1'
    ]:
        MuPP4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('MuPP4', AddressOf(MuPP4_))
        MuMP4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('MuMP4', AddressOf(MuMP4_))

        Gen_muonPos_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonPos_P4', AddressOf(Gen_muonPos_P4_))
        Gen_muonNeg_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonNeg_P4', AddressOf(Gen_muonNeg_P4_))

    elif 'B2KstarMuMu/RECO_100M_v1.2' in label or 'B2KstarMuMu/RECO_100M_v1.4' in label:
        Gen_muonPos_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonPos_P4', AddressOf(Gen_muonPos_P4_))
        Gen_muonNeg_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonNeg_P4', AddressOf(Gen_muonNeg_P4_))

        reco_mup_p4_ = TLorentzVector()
        chain.SetBranchAddress('reco_mup_p4', AddressOf(reco_mup_p4_))

        reco_mum_p4_ = TLorentzVector()
        chain.SetBranchAddress('reco_mum_p4', AddressOf(reco_mum_p4_))

    else:
        raise NameError(label)

    ntot = chain.GetEntries()

    if test:
        ntot = 1000

    if  'B2KstarMuMu/RECO_100M_v1.1' in label or \
        'B2KstarMuMu/RECO_100M_v1.4' in label:
        cuts_label = '5ifbv2.6.2'
        cuts = select_b0s(cuts_label)

    sys.stdout.write('Processing %s events ...\n' % ntot)
    sys.stdout.flush()

    nfill = 0
    for i in xrange(ntot):
        chain.LoadTree(i)
        chain.GetEntry(i)

        if label == 'B2KstarMuMu/GEN_1M_v1.1':
            mup4 = fourvecs_xyzm(chain.genMupPx, chain.genMupPy,
                                 chain.genMupPz, atr.muon_mass)
            mum4 = fourvecs_xyzm(chain.genMumPx, chain.genMumPy,
                                 chain.genMumPz, atr.muon_mass)

            mumu = mup4 + mum4
            hist.Fill(mumu.M2())
            nfill += 1

        elif label in [
                'B2KstarMuMu/RECO_1M_v2.2', 'B2KstarMuMu/RECO_1M_v2.3',
                'B2KstarMuMu/RECO_100M_v1.1'
        ]:
            if chain.nXcand > 0:
                mup4 = chain.MuPP4[0]
                mum4 = chain.MuMP4[0]
                mumu = mup4 + mum4
                hist.Fill(mumu.M2())
                nfill += 1

        elif label in [
                'B2KstarMuMu/RECO_1M_v2.2/GEN', 'B2KstarMuMu/RECO_1M_v2.3/GEN',
                'B2KstarMuMu/RECO_100M_v1.1/GEN',
                'B2KstarMuMu/RECO_100M_v1.1/GEN_HLT'
        ]:

            if label in ['B2KstarMuMu/RECO_100M_v1.1/GEN_HLT']:
                if not cuts.pass_trigger(chain):
                    continue

            if len(chain.Gen_muonPos_P4) > 0:
                mup4 = chain.Gen_muonPos_P4[0]
                mum4 = chain.Gen_muonNeg_P4[0]

                try:
                    mumu = mup4 + mum4
                except TypeError:
                    continue
                    # mup4 = chain.Gen_muonPos_P4[1]
                    # mum4 = chain.Gen_muonNeg_P4[1]
                    # mumu = mup4 + mum4
                hist.Fill(mumu.M2())
                nfill += 1

        elif 'B2KstarMuMu/RECO_100M_v1.2' in label or \
            'B2KstarMuMu/RECO_100M_v1.4' in label:

            if 'MCmatched' in label and not chain.mc_matched:
                continue

            mup4 = chain.reco_mup_p4
            mum4 = chain.reco_mum_p4

            if '/GEN' in label and len(chain.Gen_muonPos_P4) > 0:
                mup4 = chain.Gen_muonPos_P4[0]
                mum4 = chain.Gen_muonNeg_P4[0]

            mumu = mup4 + mum4
            hist.Fill(mumu.M2())
            nfill += 1

        else:
            raise NameError(label)

    sys.stdout.write('Filled %s events. \n' % nfill)
    hist.GetXaxis().SetTitle('q^{2} (GeV^{2}/c^{2})')
    hist.Draw()
    canvas.SaveAs(figfile)
    hist.Delete()
Exemple #12
0
def output_evt_mass_b0(chain, datatype, label, job, test=False, pbar=False):
    selfile = set_selfile(datatype, label, 'tree', job=job, test=test)

    f = TFile.Open(selfile, 'RECREATE')
    t = TTree('sel', 'sel')

    s = SelTree(t)

    cuts = select_b0s(label)

    pass_trigger = cuts.pass_trigger  
    pass_b0s = cuts.pass_b0s
    pass_jpsimass = cuts.pass_jpsimass
    pass_psi2smass = cuts.pass_psi2smass

    nsel = 0 
    ntot = 0

    xp4_ = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('xP4', AddressOf(xp4_))

    kstarp4_ = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('KstarP4', AddressOf(kstarp4_))

    entries = chain.GetEntries()
    if test:
        entries = 3000

    if pbar:
        pb = get_progressbar(maxval=entries)

    for i in xrange(entries):
        ntot += 1
        if pbar:
            pb.update(i+1)
            
        ientry = chain.LoadTree(i)
        if ientry < 0:
            break

        nb = chain.GetEntry(i)
        if nb <= 0:
            continue

        if not pass_trigger(chain):
            continue

        chain.LoadTree(i)
        chain.GetEntry(i)

        b0s = pass_b0s(chain)
        if b0s == []:
            continue

        s.run[0] = chain.runNb
        s.event[0] = chain.eventNb

        #nb0 = 0
        #nj = 0
        #np = 0 
        #nout = 0 
        for i in b0s:
            #nb0 += 1 
            b0p4 = chain.xP4[i]
            kstarp4 = chain.KstarP4[i]
            oniap4 = b0p4 - kstarp4

            s.b0mass[0] = b0p4.M()
            s.oniamass[0] = oniap4.M()

            s.b0massinjpsi[0] = 0 
            s.b0massinpsi2s[0] = 0
            s.b0massout[0] = 0
            
            if pass_jpsimass(oniap4.M()):
                s.b0massinjpsi[0] = b0p4.M()
                #nj += 1 
            elif pass_psi2smass(oniap4.M()):
                s.b0massinpsi2s[0] = b0p4.M()
                #np += 1
            else:
                s.b0massout[0] = b0p4.M()
                #nout += 1

        #print '%s + %s + %s = %s' %(nj, np, nout, nb0)

        t.Fill()
        nsel += 1
        if nsel > 100 and test:
            break

    f.Write()
    f.Close()

    if pbar:
        pb.finish()

    return ntot, nsel, selfile 
Exemple #13
0
class STnOOnRead:
    #_____________________________________________________________________________
    def __init__(self, infile):

        #energy in event at positive and negative rapidity
        self.epos = 0.
        self.eneg = 0.

        #number of neutrons at positive and negative rapidity
        self.npos = 0
        self.nneg = 0

        #flag for XnXn event
        self.is_XnXn = False

        #flag for central event
        self.is_Cen = False

        #J/psi kinematics
        self.pT = 0.
        self.y = 0.
        self.m = 0.

        #absolute eta for electron and positron
        self.aeta_max = 1.

        #minimal electron and positron for central trigger
        #self.p_min = 1.014

        #open the input
        self.inp = TFile.Open(infile)
        self.tree = self.inp.Get("slight_tree")

        #connect the input tree
        self.particles = TClonesArray("TParticle", 200)
        self.tree.SetBranchAddress("particles", self.particles)

        #number of events in input tree
        self.nev = self.tree.GetEntriesFast()

    #__init__

    #_____________________________________________________________________________
    def read(self, iev):

        #read a given event

        if iev >= self.nev: return False

        self.tree.GetEntry(iev)

        #initialize event variables
        self.epos = 0.
        self.eneg = 0.

        self.npos = 0
        self.nneg = 0

        self.is_XnXn = False
        self.is_Cen = True

        vec = TLorentzVector()

        #particle loop
        for imc in xrange(self.particles.GetEntriesFast()):
            part = self.particles.At(imc)

            #central electron and positron
            if TMath.Abs(part.GetPdgCode()) == 11:
                if TMath.Abs(part.Eta()) > self.aeta_max: self.is_Cen = False
                #if part.P() < self.p_min: self.is_Cen = False
                pv = TLorentzVector()
                part.Momentum(pv)
                vec += pv

            #select the neutrons
            if part.GetPdgCode() != 2112: continue

            #energy at positive and negative rapidity
            if part.Eta() > 0:
                self.epos += part.Energy()
                self.npos += 1
            else:
                self.eneg += part.Energy()
                self.nneg += 1

        #particle loop

        #flag for XnXn event
        if self.npos > 0 and self.nneg > 0: self.is_XnXn = True

        #J/psi kinematics
        self.pT = vec.Pt()
        self.y = vec.Rapidity()
        self.m = vec.M()

        return True
Exemple #14
0
def neut_en_pn():

    #neutron energy and positive and negative rapidity

    #plot range
    ebin = 3
    #emin = 1
    emin = 30
    #emax = 1400
    emax = 710

    #analysis cuts
    eta_max = 6.6  # absolute eta
    en_max = 1250  # energy
    en_min = 20

    hE = ut.prepare_TH2D("hE", ebin, emin, emax, ebin, emin, emax)

    can = ut.box_canvas()

    particles = TClonesArray("TParticle", 200)
    tree.SetBranchAddress("particles", particles)

    nev = tree.GetEntriesFast()
    #nev = int(1e4)

    nall = 0.
    nsel = 0.

    for iev in xrange(nev):
        tree.GetEntry(iev)

        epos = 0.
        eneg = 0.

        for imc in xrange(particles.GetEntriesFast()):

            part = particles.At(imc)
            if part.GetPdgCode() != 2112: continue

            #ZDC eta
            if abs(part.Eta()) < eta_max: continue

            if part.Eta() > 0:
                epos += part.Energy()
            else:
                eneg += part.Energy()

        if epos < en_min or eneg < en_min: continue

        nall += 1.

        if epos > en_max or eneg > en_max: continue

        #if epos < en_min or epos > en_max: continue
        #if eneg < en_min or eneg > en_max: continue

        nsel += 1.

        hE.Fill(eneg, epos)

    print nall, nsel, nsel / nall

    ut.put_yx_tit(hE, "#it{E}_{#it{n}} (GeV),  #it{#eta} > 0",
                  "#it{E}_{#it{n}} (GeV), #it{#eta} < 0", 1.7, 1.2)
    ut.set_margin_lbtr(gPad, 0.12, 0.09, 0.02, 0.11)

    hE.SetMinimum(0.98)
    hE.SetContour(300)

    hE.Draw()

    gPad.SetGrid()

    gPad.SetLogz()

    #ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")
Exemple #15
0
def q2mumu(args, figname):
    datatype = args[0]
    label = args[1]
    test = get_options(args, 'test')
    batch = get_options(args, 'batch')

    if batch:
        cmd = create_batch_cmd()
        bashname = '%s.sh' % figname
        bashfile = create_bashfile_cmd(cmd, bashname, label, test=test)
        logfile = set_logfile('fig', datatype, label, figname)
        jobname = 'effq2mm'
        bsub_jobs(logfile, jobname, bashfile, test)
        return

    figfile = set_figfile(figname, label, '.pdf', test=test)
    rootfile = atr.rootfile(datatype, label, test=test)
    obj = atr.root_tree_obj(datatype, label)
    chain = root_chain(rootfile, obj)

    canvas = TCanvas("aCanvas", "Canvas", 600, 600)
    #h_mm_gen = TH1F('mumumass_gen', '#mu^{+} #mu^{-} mass', 100, 0, 25)
    #h_mm_reco = TH1F('mumumass_reco', '#mu^{+} #mu^{-} mass', 100, 0, 25)
    lower = array('f', [0, 2, 4.3, 8.68, 10.09, 12.86, 14.18, 16, 19, 25])

    h_mm_gen = TH1F('mumumass_gen', '#mu^{+} #mu^{-} mass', 9, lower)
    h_mm_reco = TH1F('mumumass_reco', '#mu^{+} #mu^{-} mass', 9, lower)

    if 'B2KstarMuMu/RECO_100M_v1.1' in label:
        Gen_muonPos_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonPos_P4', AddressOf(Gen_muonPos_P4_))
        Gen_muonNeg_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonNeg_P4', AddressOf(Gen_muonNeg_P4_))

        MuPP4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('MuPP4', AddressOf(MuPP4_))
        MuMP4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('MuMP4', AddressOf(MuMP4_))

        KstarP4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('KstarP4', AddressOf(KstarP4_))

    elif 'B2KstarMuMu/RECO_100M_v1.2' in label or \
        'B2KstarMuMu/RECO_100M_v1.4' in label or \
        'B2KstarMuMu/RECO_100M_v1.5' in label:
        Gen_muonPos_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonPos_P4', AddressOf(Gen_muonPos_P4_))
        Gen_muonNeg_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonNeg_P4', AddressOf(Gen_muonNeg_P4_))

        reco_mup_p4_ = TLorentzVector()
        chain.SetBranchAddress('reco_mup_p4', AddressOf(reco_mup_p4_))

        reco_mum_p4_ = TLorentzVector()
        chain.SetBranchAddress('reco_mum_p4', AddressOf(reco_mum_p4_))

    else:
        raise NameError(label)

    ntot = chain.GetEntries()
    if test:
        ntot = 1000

    if  'B2KstarMuMu/RECO_100M_v1.2' in label or \
        'B2KstarMuMu/RECO_100M_v1.4' in label or \
        'B2KstarMuMu/RECO_100M_v1.5' in label:
        cuts_label = '5ifbv2.6.2'
        cuts = select_b0s(cuts_label)

    sys.stdout.write('Processing %s events ...\n' % ntot)
    sys.stdout.flush()

    nfill_gen = 0
    nfill_reco = 0

    for i in xrange(ntot):
        chain.LoadTree(i)
        chain.GetEntry(i)

        if len(chain.Gen_muonPos_P4) > 0:
            mup4_gen = chain.Gen_muonPos_P4[0]
            mum4_gen = chain.Gen_muonNeg_P4[0]
            try:
                mumu_gen = mup4_gen + mum4_gen
            except TypeError:

                continue
            h_mm_gen.Fill(mumu_gen.M2())
            nfill_gen += 1

        if '/HLT' in label and not cuts.pass_trigger(chain):
            continue

        if 'OfflineHLT' in label and not chain.offline_hlt_passed:
            continue

        if 'MCmatched' in label and not chain.mc_matched:
            continue

        if 'B2KstarMuMu/RECO_100M_v1.1' in label and chain.nXcand > 0:
            if label in ['B2KstarMuMu/RECO_100M_v1.1/Kstar'] and \
                not cuts.pass_kstarmass(chain, 0):
                continue

            if label in ['B2KstarMuMu/RECO_100M_v1.1/lxysig'] and \
                not cuts.pass_lxysig(chain, 0):
                continue

            mup4 = chain.MuPP4[0]
            mum4 = chain.MuMP4[0]
            mumu = mup4 + mum4
            h_mm_reco.Fill(mumu.M2())
            nfill_reco += 1

        if 'B2KstarMuMu/RECO_100M_v1.2' in label:
            mup4 = chain.reco_mup_p4
            mum4 = chain.reco_mum_p4
            mumu = mup4 + mum4
            h_mm_reco.Fill(mumu.M2())
            nfill_reco += 1

        if 'B2KstarMuMu/RECO_100M_v1.4' in label or \
            'B2KstarMuMu/RECO_100M_v1.5' in label:
            h_mm_reco.Fill(mumu_gen.M2())
            nfill_reco += 1

    sys.stdout.write('Filled events: GEN: %s, RECO: %s. \n' %
                     (nfill_gen, nfill_reco))

    hist = h_mm_reco
    hist.Divide(h_mm_gen)

    hist.SetTitle('RECO Efficiency')
    hist.GetXaxis().SetTitle('q^{2} (GeV^{2}/c^{2})')
    hist.Draw()
    canvas.SaveAs(figfile)
    hist.Delete()
Exemple #16
0
def mup(args, figname):
    datatype = args[0]
    label = args[1]
    test = get_options(args, 'test')
    batch = get_options(args, 'batch')

    if batch:
        cmd = create_batch_cmd()
        bashname = '%s.sh' %figname
        bashfile = create_bashfile_cmd(cmd, bashname, label, test=test)
        logfile = set_logfile('fig', datatype, label, figname)
        jobname = 'figptmu'
        bsub_jobs(logfile, jobname, bashfile, test)
        return

    figfile = set_figfile(figname, label, '.pdf', test=test)
    rootfile = atr.rootfile(datatype, label, test=test)

    obj = atr.root_tree_obj(datatype, label)
    chain = root_chain(rootfile, obj)

    canvas = TCanvas("aCanvas", "Canvas", 600, 600)
    hist = TH1F('mupt', '#mu^{+} p_{T}', 100, 0, 20)

    if  'B2KstarMuMu/RECO_100M_v1.1' in label or \
        'B2KstarMuMu/RECO_100M_v1.2' in label:
        Gen_muonPos_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonPos_P4', AddressOf(Gen_muonPos_P4_))
        
        MuPP4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('MuPP4', AddressOf(MuPP4_))

    if 'B2KstarMuMu/RECO_100M_v1.3' in label or \
        'B2KstarMuMu/RECO_100M_v1.4' in label or \
        'B2KstarMuMu/RECO_100M_v1.5' in label:
        Gen_muonPos_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonPos_P4', AddressOf(Gen_muonPos_P4_))
        Gen_muonNeg_P4_ = TClonesArray('TLorentzVector')
        chain.SetBranchAddress('Gen_muonNeg_P4', AddressOf(Gen_muonNeg_P4_))

        reco_mup_p4_ = TLorentzVector()
        chain.SetBranchAddress('reco_mup_p4', AddressOf(reco_mup_p4_))

        reco_mum_p4_ = TLorentzVector()
        chain.SetBranchAddress('reco_mum_p4', AddressOf(reco_mum_p4_))
    
    ntot = chain.GetEntries()
    if test:
        ntot = 1000

    if label in ['B2KstarMuMu/RECO_100M_v1.1/HLT',
                 'B2KstarMuMu/RECO_100M_v1.2/HLT',
                 'B2KstarMuMu/RECO_100M_v1.3/HLT',
                 'B2KstarMuMu/RECO_100M_v1.3/MCmatched/HLT',
                 'B2KstarMuMu/RECO_100M_v1.4/MCmatched/HLT',
                 'B2KstarMuMu/RECO_100M_v1.5/MCmatched/OfflineHLT',
                 ]:
        cuts_label = '5ifbv2.6.2'
        cuts = select_b0s(cuts_label)
        
    sys.stdout.write('Processing %s events ...\n' %ntot)
    sys.stdout.flush()
    nfill = 0 
    for i in xrange(ntot):
        chain.LoadTree(i)
        chain.GetEntry(i)

        if label in ['B2KstarMuMu/RECO_100M_v1.1/HLT',
                     'B2KstarMuMu/RECO_100M_v1.2/HLT',
                     'B2KstarMuMu/RECO_100M_v1.3/HLT',
                     'B2KstarMuMu/RECO_100M_v1.3/MCmatched/HLT', 
                     'B2KstarMuMu/RECO_100M_v1.4/MCmatched/HLT',
                     ] and \
                     not cuts.pass_trigger(chain):
           continue

        if label in ['B2KstarMuMu/RECO_100M_v1.5/MCmatched/OfflineHLT',
                     ] and \
                     not chain.offline_hlt_passed:
           continue

        if 'GEN' in label:
            if 'B2KstarMuMu/RECO_100M_v1.5' in label:
                # need others if necessary for backward compatibility.
                mup4 = chain.Gen_muonPos_P4[0]
            elif 'B2KstarMuMu/RECO_100M_v1.6' in label:
                mup4 = TVector3(chain.genMupPx, chain.genMupPy, chain.genMupPz)

            else:
                raise NameError(label)
                
            
        if 'MCmatched' in label and not chain.mc_matched:
            continue

        if 'B2KstarMuMu/RECO_100M_v1.1' in label:
            if chain.nXcand <= 0:
                continue
            mup4 = chain.MuPP4[0]
            
        if 'B2KstarMuMu/RECO_100M_v1.2' in label or \
            'B2KstarMuMu/RECO_100M_v1.3' in label or \
            'B2KstarMuMu/RECO_100M_v1.4' in label or \
            'B2KstarMuMu/RECO_100M_v1.5' in label:
            mup4 = chain.reco_mup_p4

        hist.Fill(mup4.Pt())
        nfill += 1
        
    sys.stdout.write('Filled %s events. \n' %nfill)
    hist.GetXaxis().SetTitle('p_{T} (GeV/c)')
    hist.Draw()
    canvas.SaveAs(figfile)
    hist.Delete()