Example #1
0
def test_Pythonization_FourVector():
    evt = hm.GenEvent()
    m = hm.FourVector(0.5 - random.random(), 0.5 - random.random(),
                      0.5 - random.random(), 0.5 - random.random())
    print(m[0])
    m[1] = 10
    return 0
Example #2
0
    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
Example #3
0
    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 test_Pythonization_Search():
    print(dir(hmsearch))
    inputA = hm.ReaderAsciiHepMC2("inputPythonization_Search.hepmc")
    if inputA.failed():
        print("No input")
        sys.exit(1)
    while not inputA.failed():
        evt = hm.GenEvent()
        inputA.read_event(evt)
        if inputA.failed():
            print("End of file reached. Exit.\n")
            break
        v = evt.vertices()[2]
        evt.clear()
    inputA.close()
    return 0
def test_Pythonization_GenRunInfo():
    ri = hm.GenRunInfo()
    a = hm.GenRunInfo.ToolInfo()
    a.name = "aaa"
    b = hm.GenRunInfo.ToolInfo()
    c = hm.GenRunInfo.ToolInfo()
    b.name = "foo"
    c = ("foo", "1.0", "bar")
    return 0
Example #6
0
def test_Pythia8():
    try:
        pythia = p8.Pythia()
    except ImportError:
        return 0
    pythia.readString("WeakSingleBoson:ffbar2gmZ = on")
    pythia.readString("Beams:idA =  11")
    pythia.readString("Beams:idB = -11")
    pythia.readString("Beams:eCM =  91.2")
    pythia.init()
    p8tohm = Pythia8ToHepMC3()
    p8tohm.m_store_pdf = True
    out = hm.WriterAscii(python_label() + "test_Pythia.hepmc")
    for iEvent in range(0, 100):
        if not pythia.next():
            continue
        nCharged = 0
        evt = hm.GenEvent()
        p8tohm.fill_next_event1(pythia, evt, iEvent)
        out.write_event(evt)
        evt.clear()
    pythia.stat()
    out.close()
    return 0
Example #7
0
def test_IO1():
    inputA = hm.ReaderAsciiHepMC2("inputIO1.hepmc")
    if inputA.failed():
        sys.exit(1)
    outputA = hm.WriterAscii(python_label() + "frominputIO1.hepmc")
    if outputA.failed():
        sys.exit(2)
    while not inputA.failed():
        evt = hm.GenEvent()
        inputA.read_event(evt)
        if inputA.failed():
            print("End of file reached. Exit.\n")
            break
        outputA.write_event(evt)
        evt.clear()
    inputA.close()
    outputA.close()

    inputB = hm.ReaderAscii(python_label() + "frominputIO1.hepmc")
    if inputB.failed():
        sys.exit(3)
    outputB = hm.WriterAsciiHepMC2(python_label() + "fromfrominputIO1.hepmc")
    if outputB.failed():
        sys.exit(4)
    while not inputB.failed():
        evt = hm.GenEvent()
        inputB.read_event(evt)
        if inputB.failed():
            print("End of file reached. Exit.\n")
            break
        outputB.write_event(evt)
        evt.clear()
    inputB.close()
    outputB.close()
    assert 0 == COMPARE_ASCII_FILES(python_label() + "fromfrominputIO1.hepmc",
                                    "inputIO1.hepmc")
    return 0
Example #8
0
def conversion_factor(from_, to_):
    m = hm.FourVector(0.5 - random.random(), 0.5 - random.random(),
                      0.5 - random.random(), 0.5 - random.random())
    msave = hm.FourVector(m)
    hm.Units.convert(m, from_, to_)
    return m.e() / msave.e()
Example #9
0
    def fill_next_event(self, pyev, evt, ievnum, pyinfo, pyset):
        # 1. Error if no event passed.
        if evt is None:
            print(
                "Pythia8ToHepMC3::fill_next_event error - passed null event.")
            return False
        # Event number counter.
        if ievnum >= 0:
            evt.set_event_number(ievnum)
            self.m_internal_event_number = ievnum
        else:
            evt.set_event_number(self.m_internal_event_number)
            self.m_internal_event_number = self.m_internal_event_number + 1
        evt.set_units(hm.Units.GEV, hm.Units.MM)
        #        // 2. Fill particle information
        hepevt_particles = []
        for i in range(0, pyev.size()):
            hepevt_particles.append(
                hm.GenParticle(
                    hm.FourVector(pyev[i].px(), pyev[i].py(), pyev[i].pz(),
                                  pyev[i].e()),
                    pyev[i].id(),
                    pyev[i].statusHepMC(),
                ))
            hepevt_particles[i].set_generated_mass(pyev[i].m())
        #        // 3. Fill vertex information and find beam particles.
        # For type compatibility
        vertex_cache = hm.GenEvent().vertices()
        beam_particles = hm.GenEvent().particles()
        for i in range(0, pyev.size()):
            mothers = pyev[i].motherList()
            if len(mothers) != 0:
                prod_vtx = hepevt_particles[mothers[0]].end_vertex()
                if prod_vtx is None:
                    prod_vtx = hm.GenVertex()
                    vertex_cache.append(prod_vtx)
                    for j in range(0, len(mothers)):
                        prod_vtx.add_particle_in(hepevt_particles[mothers[j]])
                prod_pos = hm.FourVector(pyev[i].xProd(), pyev[i].yProd(),
                                         pyev[i].zProd(), pyev[i].tProd())
                #                // Update vertex position if necessary
                if (not prod_pos.is_zero()) and prod_vtx.position().is_zero():
                    prod_vtx.set_position(prod_pos)
                prod_vtx.add_particle_out(hepevt_particles[i])
            else:
                beam_particles.append(hepevt_particles[i])
        #        // Add particles and vertices in topological order
        if len(beam_particles) < 2:
            print("There are  ", len(beam_particles),
                  "!=2 particles without mothers")
            if self.m_crash_on_problem:
                sys.exit(1)
        evt.add_tree(beam_particles)
        #        //Attributes should be set after adding the particles to event
        for i in range(0, pyev.size()):
            #            // Colour flow uses index 1 and 2.
            colType = pyev[i].colType()
            if colType == -1 or colType == 1 or colType == 2:
                flow1 = 0
                flow2 = 0
                if colType == 1 or colType == 2:
                    flow1 = pyev[i].col()
                if colType == -1 or colType == 2:
                    flow2 = pyev[i].acol()
                hepevt_particles[i].add_attribute("flow1",
                                                  hm.IntAttribute(flow1))
                hepevt_particles[i].add_attribute("flow2",
                                                  hm.IntAttribute(flow2))
        #        // If hadronization switched on then no final coloured particles.
        if pyset == None:
            doHadr = self.m_free_parton_warnings and pyset.flag(
                "HadronLevel:Hadronize")
        else:
            doHadr = pyset.flag("HadronLevel:all") and pyset.flag(
                "HadronLevel:Hadronize")

        #        // 4. Check for particles which come from nowhere, i.e. are without
        #        // mothers or daughters. These need to be attached to a vertex, or else
        #        // they will never become part of the event.
        for i in range(1, pyev.size()):

            #            // Check for particles not added to the event
            #            // NOTE: We have to check if this step makes any sense in HepMC event standard
            if not hepevt_particles[i]:
                print("hanging particle ", i)
                prod_vtx = hm.GenVertex()
                prod_vtx.add_particle_out(hepevt_particles[i])
                evt.add_vertex(prod_vtx)

            #            // Also check for free partons (= gluons and quarks; not diquarks?).
            if doHadr and self.m_free_parton_warnings:
                if hepevt_particles[i].pid() == 21 and (
                        hepevt_particles[i].end_vertex() is None):
                    print("gluon without end vertex ", i)
                    if self.m_crash_on_problem:
                        sys.exit(1)
                if abs(hepevt_particles[i].pid()) <= 6 and (
                        hepevt_particles[i].end_vertex() is None):
                    print("quark without end vertex ", i)
                    if self.m_crash_on_problem:
                        sys.exit(1)

        #        // 5. Store PDF, weight, cross section and other event information.
        #        // Flavours of incoming partons.
        if self.m_store_pdf and pyinfo is not None:
            id1pdf = pyinfo.id1pdf()
            id2pdf = pyinfo.id2pdf()
            if self.m_convert_gluon_to_0:
                if id1pdf == 21:
                    id1pdf = 0
                if id2pdf == 21:
                    id2pdf = 0
            pdfinfo = hm.GenPdfInfo()
            pdfinfo.set(id1pdf, id2pdf, pyinfo.x1pdf(), pyinfo.x2pdf(),
                        pyinfo.QFac(), pyinfo.pdf1(), pyinfo.pdf2())
            #            // Store PDF information.
            evt.set_pdf_info(pdfinfo)

        #        // Store process code, scale, alpha_em, alpha_s.
        if self.m_store_proc and pyinfo is not None:
            evt.add_attribute("mpi", hm.IntAttribute(pyinfo.nMPI()))
            evt.add_attribute("signal_process_id",
                              hm.IntAttribute(pyinfo.code()))
            evt.add_attribute("event_scale", hm.DoubleAttribute(pyinfo.QRen()))
            evt.add_attribute("alphaQCD", hm.DoubleAttribute(pyinfo.alphaS()))
            evt.add_attribute("alphaQED", hm.DoubleAttribute(pyinfo.alphaEM()))

        #        // Store cross-section information in pb.
        if self.m_store_xsec and pyinfo is not None:
            xsec = hm.GenCrossSection()
            xsec.set_cross_section(pyinfo.sigmaGen() * 1e9,
                                   pyinfo.sigmaErr() * 1e9)
            evt.set_cross_section(xsec)

        #        // Store event weights.
        if self.m_store_weights and pyinfo is not None:
            evt.weights().clear()
            for iweight in range(0, pyinfo.nWeights()):
                evt.weights().append(pyinfo.weight(iweight))

        #        // Done.
        return True
Example #10
0
def test_Attribute():
    # First create the event container, with Signal Process 20, event number 1
    #
    evt = hm.GenEvent(hm.Units.GEV, hm.Units.MM)
    evt.set_event_number(1)
    evt.add_attribute("signal_process_id", hm.IntAttribute(20))
    x1 = hm.VectorIntAttribute()
    evt.add_attribute("somevector1", x1)
    x2 = hm.VectorDoubleAttribute(std.vector_double([1.0, 2.0, 3.0, 4.0]))
    evt.add_attribute("somevector2", x2)
    v1 = hm.GenVertex()
    evt.add_vertex(v1)
    p1 = hm.GenParticle(hm.FourVector(0, 0, 7000, 7000), 2212, 3)
    evt.add_particle(p1)
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("flow1", hm.IntAttribute(233))
    random1 = random.random() * math.pi
    p1.add_attribute("theta", hm.DoubleAttribute(random1))
    random2 = random.random() * math.pi * 2
    p1.add_attribute("phi", hm.DoubleAttribute(random2))

    v2 = hm.GenVertex()
    evt.add_vertex(v2)
    p2 = hm.GenParticle(hm.FourVector(0, 0, -7000, 7000), 2212, 3)
    evt.add_particle(p2)
    p2.add_attribute("flow1", hm.IntAttribute(243))
    random3 = random.random() * math.pi
    p2.add_attribute("theta", hm.DoubleAttribute(random3))
    random4 = random.random() * math.pi * 2
    p2.add_attribute("phi", hm.DoubleAttribute(random4))
    v2.add_particle_in(p2)

    evt_spid_back = hm.IntAttribute()
    evt_spid_back.from_string(evt.attribute("signal_process_id"))
    assert evt_spid_back.value() == 20

    p1_flow1_back = hm.IntAttribute()
    p1_flow1_back.from_string(p1.attribute("flow1"))
    assert p1_flow1_back.value() == 233

    p2_flow1_back = hm.IntAttribute()
    p2_flow1_back.from_string(p2.attribute("flow1"))
    assert p2_flow1_back.value() == 243

    p1_theta_back = hm.DoubleAttribute()
    p1_theta_back.from_string(p1.attribute("theta"))

    assert fuse_equal(p1_theta_back.value(), random1)

    p2_phi_back = hm.DoubleAttribute()
    p2_phi_back.from_string(p2.attribute("phi"))

    assert fuse_equal(p2_phi_back.value(), random4)

    evt.clear()

    return 0
Example #11
0
def test_Boost():
    evt = hm.GenEvent(hm.Units.MomentumUnit.GEV, hm.Units.LengthUnit.CM)
    evt.set_event_number(1)
    evt.add_attribute("signal_process_id", hm.IntAttribute(20))
    #     create vertex 1
    v1 = hm.GenVertex()
    evt.add_vertex(v1)
    p1 = hm.GenParticle(hm.FourVector(1.0, 1.0, 7000, 7000), 2212, 3)
    evt.add_particle(p1)
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p1.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))

    v2 = hm.GenVertex()
    evt.add_vertex(v2)
    p2 = hm.GenParticle(hm.FourVector(1.0, 1.0, -7000, 7000), 2212, 3)
    evt.add_particle(p2)
    p2.add_attribute("flow1", hm.IntAttribute(243))
    p2.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p2.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_in(p2)
    #
    #     create the outgoing particles of v1 and v2
    p3 = hm.GenParticle(hm.FourVector(0.750, -1.569, 32.191, 32.238), 1, 3)
    evt.add_particle(p3)
    p3.add_attribute("flow1", hm.IntAttribute(231))
    p3.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p3.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v1.add_particle_out(p3)
    p4 = hm.GenParticle(hm.FourVector(-3.047, -19.0, -54.629, 57.920), -2, 3)
    evt.add_particle(p4)
    p4.add_attribute("flow1", hm.IntAttribute(243))
    p4.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p4.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_out(p4)
    #
    #     create v3
    v3 = hm.GenVertex()
    evt.add_vertex(v3)
    v3.add_particle_in(p3)
    v3.add_particle_in(p4)
    p6 = hm.GenParticle(hm.FourVector(-3.813, 0.113, -1.833, 4.233), 22, 1)
    evt.add_particle(p6)
    p6.add_attribute("flow1", hm.IntAttribute(231))
    p6.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p6.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p6)
    p5 = hm.GenParticle(hm.FourVector(1.517, -20.68, -20.605, 85.925), -24, 3)
    evt.add_particle(p5)
    p5.add_attribute("flow1", hm.IntAttribute(243))
    p5.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p5.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p5)
    #
    #     create v4
    v4 = hm.GenVertex(hm.FourVector(0.12, -0.3, 0.05, 0.004))
    evt.add_vertex(v4)
    v4.add_particle_in(p5)
    p7 = hm.GenParticle(hm.FourVector(-2.445, 28.816, 6.082, 29.552), 1, 1)
    evt.add_particle(p7)
    v4.add_particle_out(p7)
    p8 = hm.GenParticle(hm.FourVector(3.962, -49.498, -26.687, 56.373), -2, 1)
    evt.add_particle(p8)
    v4.add_particle_out(p8)
    #
    #     tell the event which vertex is the signal process vertex
    evt.add_attribute("signal_process_vertex", hm.IntAttribute(v3.id()))

    bwrong1 = hm.FourVector(-1.1, -0.3, 0.2, 0)
    #  Test that wrong boost will not work
    print(dir(evt))
    assert False == evt.boost(bwrong1)
    bwrong2 = hm.FourVector(-1.0, -0.0, 0.0, 0)
    # Test that boost with v=c will not work
    assert False == evt.boost(bwrong2)
    bwrong3 = hm.FourVector(sys.float_info.epsilon * 0.9, 0.0, 0.0, 0)
    #  Test that boost with v=0 will be OK
    assert True == evt.boost(bwrong3)
    rz = hm.FourVector(0.0, 0.0, -0.9, 0)
    rzinv = hm.FourVector(0.0, 0.0, 0.9, 0)
    evt.rotate(rz)
    evt.rotate(rzinv)
    evt.clear()
    return 0
Example #12
0
def test_Units():
    err = 0
    evt = hm.GenEvent()

    print("Default hm.Units: ", hm.Units.name(evt.momentum_unit()), " ",
          hm.Units.name(evt.length_unit()))
    cf = conversion_factor(hm.Units.MomentumUnit.GEV,
                           hm.Units.MomentumUnit.GEV)
    print(cf)
    if neq(cf, 1.0):

        err = err + 1
        print("wrong conversion factor ", cf,
              " for GEV to GEV - should be 1 \n")

    cf = conversion_factor(hm.Units.MomentumUnit.MEV,
                           hm.Units.MomentumUnit.MEV)
    if neq(cf, 1.0):

        err = err + 1
        print("wrong conversion factor ", cf,
              " for MEV to MEV - should be 1 \n")

    cf = conversion_factor(hm.Units.MomentumUnit.MEV,
                           hm.Units.MomentumUnit.GEV)
    if neq(cf, 0.001):

        err = err + 1
        print("wrong conversion factor ", cf,
              " for MEV to GEV - should be 0.001 \n")

    cf = conversion_factor(hm.Units.MomentumUnit.GEV,
                           hm.Units.MomentumUnit.MEV)
    if neq(cf, 1000.0):

        err = err + 1
        print("wrong conversion factor ", cf,
              " for GEV to MEV - should be 1000 \n")

    cf = conversion_factor(hm.Units.LengthUnit.MM, hm.Units.LengthUnit.MM)
    if neq(cf, 1.0):

        err = err + 1
        print("wrong conversion factor ", cf, " for MM to MM - should be 1 \n")

    cf = conversion_factor(hm.Units.LengthUnit.CM, hm.Units.LengthUnit.CM)
    if neq(cf, 1.0):

        err = err + 1
        print("wrong conversion factor ", cf, " for CM to CM - should be 1 \n")

    cf = conversion_factor(hm.Units.LengthUnit.CM, hm.Units.LengthUnit.MM)
    if neq(cf, 10.0):

        err = err + 1
        print("wrong conversion factor ", cf,
              " for CM to MM - should be 10 \n")

    cf = conversion_factor(hm.Units.LengthUnit.MM, hm.Units.LengthUnit.CM)
    if neq(cf, 0.1):

        err = err + 1
        print("wrong conversion factor ", cf,
              " for MM to CM - should be 0.1 \n")

    print(err)
    assert 0 == err
    return 0
Example #13
0
def test_HEPEVT():

    # Build the graph, which will look like
    # Please note this is not physically meaningful event.
    #                       p7                   #
    # p1                   /                     #
    #   \v1__p3      p5---v4                     #
    #         \_v3_/       \                     #
    #         /    \        p8                   #
    #    v2__p4     \                            #
    #   /            p6                          #
    # p3                                         #
    #
    # define a flow pattern as  p1 . p3 . p6
    #                       and p2 . p4 . p5
    #

    # First create the event container, with Signal Process 20, event number 1
    #
    evt = hm.GenEvent(hm.Units.GEV, hm.Units.MM)
    evt.set_event_number(1)
    evt.add_attribute("signal_process_id", hm.IntAttribute(20))

    v1 = hm.GenVertex()
    evt.add_vertex(v1)
    p1 = hm.GenParticle(hm.FourVector(0, 0, 7000, 7000), 2212, 3)
    evt.add_particle(p1)
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p1.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v1.add_particle_in(p1)

    v2 = hm.GenVertex()
    evt.add_vertex(v2)
    p2 = hm.GenParticle(hm.FourVector(0, 0, -7000, 7000), 2212, 3)
    evt.add_particle(p2)
    p2.add_attribute("flow1", hm.IntAttribute(243))
    p2.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p2.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_in(p2)

    p3 = hm.GenParticle(hm.FourVector(0.751, -1.569, 32.191, 32.238), 1, 3)
    evt.add_particle(p3)
    p3.add_attribute("flow1", hm.IntAttribute(231))
    p3.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p3.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v1.add_particle_out(p3)
    p4 = hm.GenParticle(hm.FourVector(-3.047, -19.0, -54.629, 57.920), -2, 3)
    evt.add_particle(p4)
    p4.add_attribute("flow1", hm.IntAttribute(243))
    p4.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p4.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_out(p4)

    v3 = hm.GenVertex()
    evt.add_vertex(v3)
    v3.add_particle_in(p3)
    v3.add_particle_in(p4)
    p6 = hm.GenParticle(hm.FourVector(-3.813, 0.113, -1.833, 4.233), 22, 1)
    evt.add_particle(p6)
    p6.add_attribute("flow1", hm.IntAttribute(231))
    p6.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p6.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p6)
    p5 = hm.GenParticle(hm.FourVector(1.517, -20.68, -20.605, 85.925), -24, 3)
    evt.add_particle(p5)
    p5.add_attribute("flow1", hm.IntAttribute(243))
    p5.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p5.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p5)
    # create v4
    v4 = hm.GenVertex(hm.FourVector(0.12, -0.3, 0.05, 0.004))
    evt.add_vertex(v4)
    v4.add_particle_in(p5)
    p7 = hm.GenParticle(hm.FourVector(-2.445, 28.816, 6.082, 29.552), 1, 1)
    evt.add_particle(p7)
    v4.add_particle_out(p7)
    p8 = hm.GenParticle(hm.FourVector(3.962, -49.498, -26.687, 56.373), -2, 1)
    evt.add_particle(p8)
    v4.add_particle_out(p8)

    evt.add_attribute("signal_process_vertex", hm.IntAttribute(v3.id()))
    evt.set_beam_particles(p1, p2)
    # The event is complete, we now print it out

    hew = hm.HEPEVT_Wrapper_Runtime()
    hew.set_max_number_entries(200)
    hew.allocate_internal_storage()
    hew.GenEvent_to_HEPEVT(evt)
    hew.print_hepevt()
    hew.print_hepevt_particle(2)
    evt.clear()
    return 0
Example #14
0
def create_df():

    #input hepmc
    #inp = "/home/jaroslav/sim/GETaLM_data/beam_gas/beam_gas_ep_10GeV_emin0p1_10Mevt.hepmc"
    inp = "/home/jaroslav/sim/GETaLM/cards/bg.hepmc"
    read = hepmc.ReaderAscii(inp)

    #output dataframe
    col = [
        "vtx_x", "vtx_y", "vtx_z", "phot_en", "phot_theta", "phot_phi",
        "el_en", "el_theta", "el_phi"
    ]
    val = []

    nmax = 3000000
    iev = 0

    #event loop
    while (True):

        iev += 1
        if iev > nmax: break

        mc = hepmc.GenEvent(hepmc.Units.GEV, hepmc.Units.MM)
        read.read_event(mc)
        if (read.failed()): break

        lin = []

        pos = mc.event_pos()
        lin.append(pos.x())
        lin.append(pos.y())
        lin.append(pos.z())

        #photon and electron particles
        phot = None
        el = None
        for i in mc.particles():
            if i.pid() == 22:
                phot = i
            if i.pid() == 11:
                el = i

        lin.append(phot.momentum().e())
        lin.append(phot.momentum().theta())
        lin.append(phot.momentum().phi())

        lin.append(el.momentum().e())
        lin.append(el.momentum().theta())
        lin.append(el.momentum().phi())

        val.append(lin)

    df = DataFrame(val, columns=col)

    print(df)

    out = HDFStore("bg.h5")
    out["bg"] = df
    out.close()

    read.close()
Example #15
0
def test_Print():
    evt = hm.GenEvent(hm.Units.MomentumUnit.GEV, hm.Units.LengthUnit.CM)
    evt.set_event_number(1)
    evt.add_attribute("signal_process_id", hm.IntAttribute(20))
    #     create vertex 1
    v1 = hm.GenVertex()
    evt.add_vertex(v1)
    p1 = hm.GenParticle(hm.FourVector(1.0, 1.0, 7000, 7000), 2212, 3)
    evt.add_particle(p1)
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p1.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))

    v2 = hm.GenVertex()
    evt.add_vertex(v2)
    p2 = hm.GenParticle(hm.FourVector(1.0, 1.0, -7000, 7000), 2212, 3)
    evt.add_particle(p2)
    p2.add_attribute("flow1", hm.IntAttribute(243))
    p2.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p2.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_in(p2)
    #
    #     create the outgoing particles of v1 and v2
    p3 = hm.GenParticle(hm.FourVector(0.750, -1.569, 32.191, 32.238), 1, 3)
    evt.add_particle(p3)
    p3.add_attribute("flow1", hm.IntAttribute(231))
    p3.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p3.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v1.add_particle_out(p3)
    p4 = hm.GenParticle(hm.FourVector(-3.047, -19.0, -54.629, 57.920), -2, 3)
    evt.add_particle(p4)
    p4.add_attribute("flow1", hm.IntAttribute(243))
    p4.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p4.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_out(p4)
    #
    #     create v3
    v3 = hm.GenVertex()
    evt.add_vertex(v3)
    v3.add_particle_in(p3)
    v3.add_particle_in(p4)
    p6 = hm.GenParticle(hm.FourVector(-3.813, 0.113, -1.833, 4.233), 22, 1)
    evt.add_particle(p6)
    p6.add_attribute("flow1", hm.IntAttribute(231))
    p6.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p6.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p6)
    p5 = hm.GenParticle(hm.FourVector(1.517, -20.68, -20.605, 85.925), -24, 3)
    evt.add_particle(p5)
    p5.add_attribute("flow1", hm.IntAttribute(243))
    p5.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p5.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p5)
    #
    #     create v4
    v4 = hm.GenVertex(hm.FourVector(0.12, -0.3, 0.05, 0.004))
    evt.add_vertex(v4)
    v4.add_particle_in(p5)
    p7 = hm.GenParticle(hm.FourVector(-2.445, 28.816, 6.082, 29.552), 1, 1)
    evt.add_particle(p7)
    v4.add_particle_out(p7)
    p8 = hm.GenParticle(hm.FourVector(3.962, -49.498, -26.687, 56.373), -2, 1)
    evt.add_particle(p8)
    v4.add_particle_out(p8)
    #
    #     tell the event which vertex is the signal process vertex

    evt.add_attribute("signal_process_vertex", hm.IntAttribute(v3.id()))
    print(dir(hm))
    print(hm.Print.content(evt))
    #    we now print it out in old format
    print(hm.Print.listing(evt, 8))
    #     print each particle so we can see the polarization
    for ip in evt.particles():
        print(hm.Print.line(ip, True))

    xout1 = hm.WriterAscii(python_label() + "testBoost1.out")
    xout1.set_precision(6)
    xout1.write_event(evt)
    xout1.close()
    # different outputs
    ff = io.StringIO()
    hm.Print.listing(ff, evt)
    print(ff.getvalue())
    # different outputs
    for ip in evt.particles():
        print(hm.Print.line(ip, True))
    for ip in evt.particles():
        print(hm.Print.line(ip, True))
    xout2 = hm.WriterAscii(python_label() + "testBoost2.out")
    xout2.set_precision(6)
    xout2.write_event(evt)
    xout2.close()
    assert COMPARE_ASCII_FILES(python_label() + "testBoost1.out",
                               python_label() + "testBoost2.out") == 0
    evt.clear()
    return 0
Example #16
0
def test_Pythonization_GenEvent():
    evt = hm.GenEvent()
    evt.add_particle(hm.GenParticle())
    print(evt.particles)
    evt.add_vertex(hm.GenVertex())
    return 0
Example #17
0
def test_Polarization():
    xout1 = hm.WriterAscii(python_label() + "testPolarization1.dat")
    xout2 = hm.WriterAscii(python_label() + "testPolarization2.dat")
    xout4 = hm.WriterAsciiHepMC2(python_label() + "testPolarization4.dat")
    xout5 = hm.WriterAscii(python_label() + "testPolarization5.dat")

    # Build the graph, which will look like
    # Please note this is not physically meaningful event.
    #                       p7                   #
    # p1                   /                     #
    #   \v1__p3      p5---v4                     #
    #         \_v3_/       \                     #
    #         /    \        p8                   #
    #    v2__p4     \                            #
    #   /            p6                          #
    # p3                                         #
    #
    # define a flow pattern as  p1 . p3 . p6
    #                       and p2 . p4 . p5
    #

    # First create the event container, with Signal Process 20, event number 1
    #
    evt = hm.GenEvent(hm.Units.GEV, hm.Units.MM)
    evt.set_event_number(1)
    evt.add_attribute("signal_process_id", hm.IntAttribute(20))

    v1 = hm.GenVertex()
    evt.add_vertex(v1)
    p1 = hm.GenParticle(hm.FourVector(0, 0, 7000, 7000), 2212, 3)
    evt.add_particle(p1)
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("flow1", hm.IntAttribute(231))
    p1.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p1.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v1.add_particle_in(p1)

    v2 = hm.GenVertex()
    evt.add_vertex(v2)
    p2 = hm.GenParticle(hm.FourVector(0, 0, -7000, 7000), 2212, 3)
    evt.add_particle(p2)
    p2.add_attribute("flow1", hm.IntAttribute(243))
    p2.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p2.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_in(p2)

    p3 = hm.GenParticle(hm.FourVector(0.751, -1.569, 32.191, 32.238), 1, 3)
    evt.add_particle(p3)
    p3.add_attribute("flow1", hm.IntAttribute(231))
    p3.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p3.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v1.add_particle_out(p3)
    p4 = hm.GenParticle(hm.FourVector(-3.047, -19.0, -54.629, 57.920), -2, 3)
    evt.add_particle(p4)
    p4.add_attribute("flow1", hm.IntAttribute(243))
    p4.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p4.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v2.add_particle_out(p4)

    v3 = hm.GenVertex()
    evt.add_vertex(v3)
    v3.add_particle_in(p3)
    v3.add_particle_in(p4)
    p6 = hm.GenParticle(hm.FourVector(-3.813, 0.113, -1.833, 4.233), 22, 1)
    evt.add_particle(p6)
    p6.add_attribute("flow1", hm.IntAttribute(231))
    p6.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p6.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p6)
    p5 = hm.GenParticle(hm.FourVector(1.517, -20.68, -20.605, 85.925), -24, 3)
    evt.add_particle(p5)
    p5.add_attribute("flow1", hm.IntAttribute(243))
    p5.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p5.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi * 2))
    v3.add_particle_out(p5)
    # create v4
    v4 = hm.GenVertex(hm.FourVector(0.12, -0.3, 0.05, 0.004))
    evt.add_vertex(v4)
    v4.add_particle_in(p5)
    p7 = hm.GenParticle(hm.FourVector(-2.445, 28.816, 6.082, 29.552), 1, 1)
    evt.add_particle(p7)
    v4.add_particle_out(p7)
    p8 = hm.GenParticle(hm.FourVector(3.962, -49.498, -26.687, 56.373), -2, 1)
    evt.add_particle(p8)
    v4.add_particle_out(p8)

    evt.add_attribute("signal_process_vertex", hm.IntAttribute(v3.id()))
    evt.set_beam_particles(p1,p2)
    # The event is complete, we now print it out
    hm.Print.content(evt)
    hm.Print.listing(evt, 8)
    print(hm.version())
    print(hm.Print.line(v4, True))
    a = 0
    print(evt.particles())
    print(len(evt.particles()))
    print(evt.particles()[0])
    for ip in evt.particles():
        print(hm.Print.line(ip, True))
    xout1.write_event(evt)

    # write event in old format
    xout4.write_event(evt)
    # make a copy and write it
    xout5.write_event(hm.GenEvent(evt))
    # try changing polarization
    p2.add_attribute("theta", hm.DoubleAttribute(random.random() * math.pi))
    p2.add_attribute("phi", hm.DoubleAttribute(random.random() * math.pi))
    xout2.write_event(evt)
    xout1.close()
    xout2.close()
    xout4.close()
    xout5.close()
    # now clean-up by deleteing all objects from memory
    #
    # deleting the event deletes all contained vertices, and all particles
    # contained in those vertices
    evt.clear()

    assert (
        COMPARE_ASCII_FILES(python_label() + "testPolarization1.dat", python_label() + "testPolarization5.dat") == 0
    ) and (COMPARE_ASCII_FILES(python_label() + "testPolarization1.dat", python_label() + "testPolarization2.dat") != 0)

    inputA1 = hm.ReaderAscii(python_label() + "testPolarization1.dat")
    if inputA1.failed():
        return 1
    while not inputA1.failed():
        evt = hm.GenEvent()
        inputA1.read_event(evt)
        if inputA1.failed():
            print("End of file reached. Exit.\n")
            break
        evt.clear()
    inputA1.close()
    inputA2 = hm.ReaderAscii(python_label() + "testPolarization2.dat")
    if inputA2.failed():
        return 2
    while not inputA2.failed():
        evt = hm.GenEvent()
        inputA2.read_event(evt)
        if inputA2.failed():
            print("End of file reached. Exit.\n")
            break
        evt.clear()
    inputA2.close()

    inputA4 = hm.ReaderAsciiHepMC2(python_label() + "testPolarization4.dat")
    if inputA4.failed():
        return 4
    while not inputA4.failed():
        evt = hm.GenEvent()
        inputA4.read_event(evt)
        if inputA4.failed():
            print("End of file reached. Exit.\n")
            break
        evt.clear()
    inputA4.close()

    inputA5 = hm.ReaderAscii(python_label() + "testPolarization5.dat")
    if inputA5.failed():
        return 4
    while not inputA5.failed():
        evt = hm.GenEvent()
        inputA5.read_event(evt)
        if inputA5.failed():
            print("End of file reached. Exit.\n")
            break
        evt.clear()
    inputA5.close()

    return 0