Exemplo n.º 1
0
def convert_cleo(infilename):
    data = h5hep.initialize()

    groups = [ ['pions','npions',['e','px','py','pz','q','sigpi','sigka','likpi','likka','nphopi','nphoka','depthmu','cluster_energy'] ], 
               ['kaons','nkaons',['e','px','py','pz','q','sigpi','sigka','likpi','likka','nphopi','nphoka','depthmu','cluster_energy'] ], 
               ['muons','nmuons',['e','px','py','pz','q','sigpi','sigka','likpi','likka','nphopi','nphoka','depthmu','cluster_energy'] ], 
               ['electrons','nelectrons',['e','px','py','pz','q','sigpi','sigka','likpi','likka','nphopi','nphoka','depthmu','cluster_energy'] ], 
               ['photons','nphotons',['e','px','py','pz'] ],
               ]

    for group in groups:

        h5hep.create_group(data,group[0],counter=group[1])
        h5hep.create_dataset(data,group[2],group=group[0],dtype=float)

    '''
    h5hep.create_group(data,'pions',counter='npions')
    h5hep.create_dataset(data,['e','px','py','pz','q','beta','dedx'],group='pions',dtype=float)
    '''

    event = h5hep.create_single_event(data)

    collisions = cleo.get_collisions(open(infilename))

    for collision in collisions:

        h5hep.clear_event(event)

        pions,kaons,muons,electrons,photons = collision

        particles = [pions,kaons,muons,electrons,photons]

        for group,particle in zip(groups, particles):
            key = "%s/%s" % (group[0],group[1])
            event[key] = len(particle)
            for p in particle:
                for j in range(len(group[2])):
                    key = '%s/%s' % (group[0],group[2][j])
                    event[key].append(p[j])

        '''
        event['pions/npions'] = len(pions)
        for pion in pions:
            event['pions/e'].append(pion[0])
            event['pions/px'].append(pion[1])
            event['pions/py'].append(pion[2])
            event['pions/pz'].append(pion[3])
            event['pions/q'].append(pion[4])
            event['pions/beta'].append(pion[5])
            event['pions/dedx'].append(pion[6])
        '''

        h5hep.pack(data,event)

    print("Writing the file...")
    outfilename = infilename.split('.')[0] + ".hdf5"
    print(outfilename)
    hdfile = h5hep.write_to_file(outfilename,data,comp_type='gzip',comp_opts=9)
Exemplo n.º 2
0
def convert_babar(infilename,maxentries=None):
    data = h5hep.initialize()

    groups = [ ['pions','npions',['e','px','py','pz','q','beta','dedx'] ], 
               ['kaons','nkaons',['e','px','py','pz','q','beta','dedx'] ], 
               ['protons','nprotons',['e','px','py','pz','q','beta','dedx'] ], 
               ['muons','nmuons',['e','px','py','pz','q','beta','dedx'] ], 
               ['electrons','nelectrons',['e','px','py','pz','q','beta','dedx'] ], 
               ['photons','nphotons',['e','px','py','pz'] ], 
               ]

    for group in groups:

        h5hep.create_group(data,group[0],counter=group[1])
        h5hep.create_dataset(data,group[2],group=group[0],dtype=float)

    '''
    h5hep.create_group(data,'pions',counter='npions')
    h5hep.create_dataset(data,['e','px','py','pz','q','beta','dedx'],group='pions',dtype=float)
    '''

    event = h5hep.create_single_event(data)

    f = ROOT.TFile(infilename)
    tree = f.Get('ntp1')
    #f = uproot.open(infilename)
    #tree = f['ntp1']

    nentries = tree.GetEntries()
    #nentries = tree.numentries

    #'''
    for i in range(nentries):

        if maxentries is not None and i>=maxentries:
            break

        h5hep.clear_event(event)

        tree.GetEvent(i)

        if i%1000==0:
            print(i)

        #pions,kaons,protons,muons,electrons,photons = collision
        #particles = [pions,kaons,protons,muons,electrons,photons]

        event['pions/npions'] = tree.npi
        for j in range(tree.npi):
            e = tree.pienergy[j]
            p3 = tree.pip3[j]
            phi = tree.piphi[j]
            costh = tree.picosth[j]
            px,py,pz = sph2cart(p3,costh,phi)
            lund = tree.piLund[j]
            q = int(lund/np.abs(lund))
            idx = tree.piTrkIdx[j]
            dedx = tree.TRKdedxdch[idx]
            drc = tree.TRKDrcTh[idx]
            beta = 1.0/np.cos(drc)/1.474

            event['pions/e'].append(e)
            event['pions/px'].append(px)
            event['pions/py'].append(py)
            event['pions/pz'].append(pz)
            event['pions/q'].append(q)
            event['pions/beta'].append(beta)
            event['pions/dedx'].append(dedx)

        event['kaons/nkaons'] = tree.nK
        for j in range(tree.nK):
            e = tree.Kenergy[j]
            p3 = tree.Kp3[j]
            phi = tree.Kphi[j]
            costh = tree.Kcosth[j]
            px,py,pz = sph2cart(p3,costh,phi)
            lund = tree.KLund[j]
            q = int(lund/np.abs(lund))
            idx = tree.KTrkIdx[j]
            dedx = tree.TRKdedxdch[idx]
            drc = tree.TRKDrcTh[idx]
            beta = 1.0/np.cos(drc)/1.474

            event['kaons/e'].append(e)
            event['kaons/px'].append(px)
            event['kaons/py'].append(py)
            event['kaons/pz'].append(pz)
            event['kaons/q'].append(q)
            event['kaons/beta'].append(beta)
            event['kaons/dedx'].append(dedx)

        event['protons/nprotons'] = tree.np
        for j in range(tree.np):
            e = tree.penergy[j]
            p3 = tree.pp3[j]
            phi = tree.pphi[j]
            costh = tree.pcosth[j]
            px,py,pz = sph2cart(p3,costh,phi)
            lund = tree.pLund[j]
            q = int(lund/np.abs(lund))
            idx = tree.pTrkIdx[j]
            dedx = tree.TRKdedxdch[idx]
            drc = tree.TRKDrcTh[idx]
            beta = 1.0/np.cos(drc)/1.474

            event['protons/e'].append(e)
            event['protons/px'].append(px)
            event['protons/py'].append(py)
            event['protons/pz'].append(pz)
            event['protons/q'].append(q)
            event['protons/beta'].append(beta)
            event['protons/dedx'].append(dedx)

        event['muons/nmuons'] = tree.nmu
        for j in range(tree.nmu):
            e = tree.muenergy[j]
            p3 = tree.mup3[j]
            phi = tree.muphi[j]
            costh = tree.mucosth[j]
            px,py,pz = sph2cart(p3,costh,phi)
            lund = tree.muLund[j]
            q = int(lund/np.abs(lund))
            idx = tree.muTrkIdx[j]
            dedx = tree.TRKdedxdch[idx]
            drc = tree.TRKDrcTh[idx]
            beta = 1.0/np.cos(drc)/1.474

            event['muons/e'].append(e)
            event['muons/px'].append(px)
            event['muons/py'].append(py)
            event['muons/pz'].append(pz)
            event['muons/q'].append(q)
            event['muons/beta'].append(beta)
            event['muons/dedx'].append(dedx)

        event['electrons/nelectrons'] = tree.ne
        for j in range(tree.ne):
            e = tree.eenergy[j]
            p3 = tree.ep3[j]
            phi = tree.ephi[j]
            costh = tree.ecosth[j]
            px,py,pz = sph2cart(p3,costh,phi)
            lund = tree.eLund[j]
            q = int(lund/np.abs(lund))
            idx = tree.eTrkIdx[j]
            dedx = tree.TRKdedxdch[idx]
            drc = tree.TRKDrcTh[idx]
            beta = 1.0/np.cos(drc)/1.474

            event['electrons/e'].append(e)
            event['electrons/px'].append(px)
            event['electrons/py'].append(py)
            event['electrons/pz'].append(pz)
            event['electrons/q'].append(q)
            event['electrons/beta'].append(beta)
            event['electrons/dedx'].append(dedx)

        event['photons/nphotons'] = tree.ngamma
        for j in range(tree.ngamma):
            e = tree.gammaenergy[j]
            p3 = tree.gammap3[j]
            phi = tree.gammaphi[j]
            costh = tree.gammacosth[j]
            px,py,pz = sph2cart(p3,costh,phi)

            event['photons/e'].append(e)
            event['photons/px'].append(px)
            event['photons/py'].append(py)
            event['photons/pz'].append(pz)

        h5hep.pack(data,event)

    print("Writing the file...")
    outfilename = infilename.split('.')[0] + ".hdf5"
    if maxentries is not None:
        outfilename = infilename.split('.')[0] + "_" + str(maxentries) + "entries.hdf5"
    print(outfilename)
    hdfile = h5hep.write_to_file(outfilename,data,comp_type='gzip',comp_opts=9)
Exemplo n.º 3
0
    event['jet/njet'] = njet

    for n in range(njet):
        event['jet/e'].append(np.random.random())
        event['jet/px'].append(np.random.random())
        event['jet/py'].append(np.random.random())
        event['jet/pz'].append(np.random.random())

    hp.pack(data,event)

#print(data)

print("Writing the file...")
#hdfile = write_to_file('output.hdf5',data)
outputfilename = 'test_singleton_OUTPUT.hdf5'
hdfile = hp.write_to_file(outputfilename,data,comp_type='gzip',comp_opts=9)

################################################################################
# Reading in file
################################################################################
inputfilename = outputfilename

data,event = hp.load(inputfilename)

#print("----------------")
#print(data)
#print("----------------")

nentries = data['nentries']
#print("nentries: ",nentries)
                continue
            ptype = track['@ptype']
            #print(ptype)

            which_charged_track_hypothesis = f"chargedTrack_{ptypes[ptype]}"

            #print(int(track['@candidateId'])-1)
            #print(int(ptypes[ptype]))
            ncharged_tracks[int(
                ptypes[ptype])].append(int(track['@candidateId']) - 1)

            event[which_charged_track_hypothesis + '/candidateId'].append(
                int(track['@candidateId']))
            event[which_charged_track_hypothesis + '/px'].append(
                float(track['trackFit']['@px']))
            event[which_charged_track_hypothesis + '/py'].append(
                float(track['trackFit']['@py']))
            event[which_charged_track_hypothesis + '/pz'].append(
                float(track['trackFit']['@pz']))

    for i in range(len(ncharged_tracks)):
        if len(ncharged_tracks[i]) > 0:
            which_charged_track_hypothesis = f"chargedTrack_{i}"
            #print(ncharged_tracks)
            event[which_charged_track_hypothesis + '/n' +
                  which_charged_track_hypothesis] = max(ncharged_tracks[i]) + 1

    h5hep.pack(data, event)

h5hep.write_to_file('test.h5', data, comp_type='gzip')
Exemplo n.º 5
0
line = None
while 1:

    h5hep.clear_event(event)

    if icount % 100 == 0:
        print(icount)

    line = infile.readline()
    if line is '':
        break
    nhits = int(line)
    #print(nhits)
    event['hits/nhits'] = nhits

    for n in range(nhits):
        line = infile.readline()
        vals = np.array(line.split(',')).astype(float)
        #print(vals)
        event['hits/q'].append(vals[0])
        event['hits/t'].append(vals[1])
        event['hits/x'].append(vals[2])
        event['hits/y'].append(vals[3])
        event['hits/z'].append(vals[4])

    icount += 1
    h5hep.pack(data, event)

outfilename = infilename.split('.')[0] + '.h5'
hdfile = h5hep.write_to_file(outfilename, data, comp_type="gzip", comp_opts=9)
Exemplo n.º 6
0
def convert_babar(infilename,maxentries=None):
    data = h5hep.initialize()

    groups = [ ['pions','npions',['e','px','py','pz','q','beta','dedx'] ], 
               ['kaons','nkaons',['e','px','py','pz','q','beta','dedx'] ], 
               ['protons','nprotons',['e','px','py','pz','q','beta','dedx'] ], 
               ['muons','nmuons',['e','px','py','pz','q','beta','dedx'] ], 
               ['electrons','nelectrons',['e','px','py','pz','q','beta','dedx'] ], 
               ['photons','nphotons',['e','px','py','pz','q','beta','dedx'] ], 
               ]

    for group in groups:

        h5hep.create_group(data,group[0],counter=group[1])
        h5hep.create_dataset(data,group[2],group=group[0],dtype=float)

    '''
    h5hep.create_group(data,'pions',counter='npions')
    h5hep.create_dataset(data,['e','px','py','pz','q','beta','dedx'],group='pions',dtype=float)
    '''

    event = h5hep.create_single_event(data)

    collisions = babar.get_collisions(open(infilename))

    for count,collision in enumerate(collisions):

        if maxentries is not None and count>=maxentries:
            break

        h5hep.clear_event(event)

        pions,kaons,protons,muons,electrons,photons = collision

        particles = [pions,kaons,protons,muons,electrons,photons]

        for group,particle in zip(groups, particles):
            key = "%s/%s" % (group[0],group[1])
            event[key] = len(particle)
            for p in particle:
                for j in range(len(group[2])):
                    key = '%s/%s' % (group[0],group[2][j])
                    event[key].append(p[j])

        '''
        event['pions/npions'] = len(pions)
        for pion in pions:
            event['pions/e'].append(pion[0])
            event['pions/px'].append(pion[1])
            event['pions/py'].append(pion[2])
            event['pions/pz'].append(pion[3])
            event['pions/q'].append(pion[4])
            event['pions/beta'].append(pion[5])
            event['pions/dedx'].append(pion[6])
        '''

        h5hep.pack(data,event)

    print("Writing the file...")
    outfilename = infilename.split('.')[0] + ".hdf5"
    if maxentries is not None:
        outfilename = infilename.split('.')[0] + "_" + str(maxentries) + "entries.hdf5"
    print(outfilename)
    hdfile = h5hep.write_to_file(outfilename,data,comp_type='gzip',comp_opts=9)
Exemplo n.º 7
0
def convert_cms(infilename,maxentries=None):
    data = h5hep.initialize()

    h5hep.create_group(data,'jets',counter='njets')
    h5hep.create_dataset(data,['e','px','py','pz','btag'],group='jets',dtype=float)

    h5hep.create_group(data,'muons',counter='nmuons')
    h5hep.create_dataset(data,['e','px','py','pz','q'],group='muons',dtype=float)
    
    h5hep.create_group(data,'electrons',counter='nelectrons')
    h5hep.create_dataset(data,['e','px','py','pz','q'],group='electrons',dtype=float)
    
    h5hep.create_group(data,'photons',counter='nphotons')
    h5hep.create_dataset(data,['e','px','py','pz'],group='photons',dtype=float)
    
    h5hep.create_dataset(data,['METx','METy'],dtype=float)
    
    event = h5hep.create_single_event(data)

    collisions = cms.get_collisions_from_filename(infilename)

    #'''
    for count,collision in enumerate(collisions):

        if maxentries is not None and count>=maxentries:
            break

        h5hep.clear_event(event)

        jets,muons,electrons,photons,met = collision

        event['jets/njets'] = len(jets)
        for jet in jets:
            event['jets/e'].append(jet[0])
            event['jets/px'].append(jet[1])
            event['jets/py'].append(jet[2])
            event['jets/pz'].append(jet[3])
            event['jets/btag'].append(jet[4])

        event['muons/nmuons'] = len(muons)
        for muon in muons:
            event['muons/e'].append(muon[0])
            event['muons/px'].append(muon[1])
            event['muons/py'].append(muon[2])
            event['muons/pz'].append(muon[3])
            event['muons/q'].append(muon[4])

        event['electrons/nelectrons'] = len(electrons)
        for electron in electrons:
            event['electrons/e'].append(electron[0])
            event['electrons/px'].append(electron[1])
            event['electrons/py'].append(electron[2])
            event['electrons/pz'].append(electron[3])
            event['electrons/q'].append(electron[4])

        event['photons/nphotons'] = len(photons)
        for photon in photons:
            event['photons/e'].append(photon[0])
            event['photons/px'].append(photon[1])
            event['photons/py'].append(photon[2])
            event['photons/pz'].append(photon[3])


        event['METx'] = met[0]
        event['METy'] = met[1]


        h5hep.pack(data,event)

    print("Writing the file...")
    outfilename = infilename.split('.')[0] + ".hdf5"
    if maxentries is not None:
        outfilename = infilename.split('.')[0] + "_" + str(maxentries) + "entries.hdf5"
    print(outfilename)
    hdfile = h5hep.write_to_file(outfilename,data,comp_type='gzip',comp_opts=9)
        py = 300*beta(2,9)
        pz = 300*beta(2,9)
        mass = 0.000511
        energy = calc_energy(mass,px,py,pz)
        event['electron/px'].append(px)
        event['electron/py'].append(py)
        event['electron/pz'].append(pz)
        event['electron/e'].append(energy)
        event['electron/q'].append(2*np.random.randint(2) - 1)

    nphoton = np.random.randint(10)
    event['photon/nphoton'] = nphoton
    for n in range(nphoton):
        px = 300*beta(2,9)
        py = 300*beta(2,9)
        pz = 300*beta(2,9)
        mass = 0.0
        energy = calc_energy(mass,px,py,pz)
        event['photon/px'].append(px)
        event['photon/py'].append(py)
        event['photon/pz'].append(pz)
        event['photon/e'].append(energy)
        
    hp.pack(data,event)

print("Writing the file...")
#hdfile = write_to_file('output.hdf5',data)
hdfile = hp.write_to_file('HEP_random_file_LARGE.hdf5',data,comp_type='gzip',comp_opts=9)
#'''