251 lines
8.2 KiB
Python
251 lines
8.2 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
import scipy.optimize as optimization
|
|
import glob
|
|
import h5py
|
|
import os
|
|
import sys
|
|
|
|
def get_h5list(directory, extension='h5'):
|
|
liste = []
|
|
for f in os.listdir(directory):
|
|
if f.endswith('.' + extension):
|
|
liste.append(directory + '/' + f)
|
|
return liste
|
|
|
|
def get_csv_part_reco(inputpath, sim_time, num_E=100, num_r=1, Erange=[1e-5,2e4]):
|
|
|
|
outputpath = inputpath + '/csv'
|
|
try:
|
|
os.mkdir(outputpath)
|
|
except:
|
|
pass
|
|
|
|
# def some constants
|
|
me_eV = 511e3
|
|
mT_eV = 2.8094320212e9
|
|
c_ms = 3e8
|
|
rrange = [0, 0.045]
|
|
num_E = num_E
|
|
num_r = num_r
|
|
Erange=[1e-5,2e4]
|
|
|
|
# calc bin properties
|
|
Ebins = pow(10, np.linspace(np.log10(Erange[0]), np.log10(Erange[1]), (num_E+1)))
|
|
Emids = (Ebins[1:] + Ebins[:-1]/2)
|
|
Emids = pow(10, (np.log10(Ebins[1:]) + np.log10(Ebins[:-1]))/2)
|
|
rbins = np.linspace(0, 0.045, (num_r+1))
|
|
rmids = (rbins[1:] + rbins[:-1])/2
|
|
As = np.pi*rbins[1:]**2 - np.pi*rbins[:-1]**2
|
|
|
|
|
|
|
|
h5list = get_h5list(inputpath)
|
|
|
|
# open irgendeine h5 file
|
|
f = h5py.File(h5list[0], 'r')
|
|
g = f['Particles']['e-']
|
|
|
|
species = list(f['Particles'].keys())
|
|
|
|
zs = list(g.keys())
|
|
f.close()
|
|
|
|
zeros = np.zeros(num_E)
|
|
|
|
df = pd.DataFrame()
|
|
for s in species:
|
|
#print (s)
|
|
if(s=='T+'):
|
|
m = mT_eV
|
|
if(s=='T2+'):
|
|
m = 2*mT_eV
|
|
if(s=='T3+'):
|
|
m = 3*mT_eV
|
|
if(s=='T5+'):
|
|
m = 5*mT_eV
|
|
if(s=='e-'):
|
|
m = me_eV
|
|
|
|
|
|
for z in zs:
|
|
# calc properties and calculate categories
|
|
#print(z)
|
|
a = pd.DataFrame({
|
|
|
|
'species' : s,
|
|
'z' : float(z),
|
|
'Emin' : zeros,
|
|
'Emax' : 0,
|
|
'Emid' : 0,
|
|
'rmin' : 0,
|
|
'rmax' : 0,
|
|
'rmid' : 0,
|
|
'N' : 0,
|
|
'dndE' : 0,
|
|
'djzabsdE' : 0,
|
|
'djzdE' : 0,
|
|
'N+' : 0,
|
|
'N-' : 0,
|
|
'dj+dE' : 0,
|
|
'dj-dE' : 0
|
|
})
|
|
|
|
for h5 in h5list[:]:
|
|
#print(h5)
|
|
f = h5py.File(h5, 'r')
|
|
g = f['Particles'][s]
|
|
df_z = pd.DataFrame(g[z][:])
|
|
#print(df_z)
|
|
#print(df_z['Id'])
|
|
# calc properties and calc categories
|
|
|
|
|
|
|
|
df_z['E_eV'] = 0.5*m*(df_z.vx_ms**2 + df_z.vy_ms**2 + df_z.vz_ms**2)/c_ms**2
|
|
|
|
|
|
|
|
df_z['r_m'] = np.sqrt(df_z['x_m']**2 + df_z['y_m']**2)
|
|
df_z['inv_v_z'] = 1/df_z['vz_ms'].abs()
|
|
df_z['v_sign'] = np.sign(df_z['vz_ms'])
|
|
df_z['v_sign_+'] = df_z['v_sign'].apply(lambda x: 1 if x > 0 else 0)
|
|
df_z['v_sign_-'] = df_z['v_sign'].apply(lambda x: 1 if x < 0 else 0)
|
|
df_z['E_cut'] = pd.cut(df_z['E_eV'], bins=Ebins, labels = False)
|
|
df_z['r_cut'] = pd.cut(df_z['r_m'], bins=rbins, labels = False, include_lowest = True)
|
|
|
|
|
|
# apply categories and fill empty with 0
|
|
t = df_z.groupby([df_z['E_cut'], df_z['r_cut']])['inv_v_z'].agg([('N','size'), ('inv_v_sum','sum')])
|
|
t['v_sign'] = df_z.groupby([df_z['E_cut'], df_z['r_cut']])['v_sign'].sum()
|
|
t['N+'] = df_z.groupby([df_z['E_cut'], df_z['r_cut']])['v_sign_+'].sum()
|
|
t['N-'] = df_z.groupby([df_z['E_cut'], df_z['r_cut']])['v_sign_-'].sum()
|
|
t = t.reindex( pd.MultiIndex.from_product([np.arange(num_E),
|
|
np.arange(num_r)], names=['E_bin', 'r_bin']) ,fill_value=0)
|
|
t = t.reset_index()
|
|
t['z'] = float(z)
|
|
|
|
#print(Ebins[t['E_bin']])
|
|
# transform to df form
|
|
|
|
#a['Emin'] = Ebins[t['E_bin']]
|
|
#a['Emax'] = Ebins[t['E_bin']+1]
|
|
a['Emid'] = Emids[t['E_bin']]
|
|
a['rmin'] = rbins[t['r_bin']]
|
|
a['rmax'] = rbins[t['r_bin']+1]
|
|
a['rmid'] = rmids[t['r_bin']]
|
|
a['N'] += t['N']
|
|
a['dndE'] += t['inv_v_sum']/As[t['r_bin']]/sim_time
|
|
a['dndE'] = a['dndE'].replace(np.inf, 0)
|
|
a['djzabsdE'] += t['N']/As[t['r_bin']]/sim_time
|
|
a['djzdE'] += t['v_sign']/As[t['r_bin']]/sim_time
|
|
a['N+'] += t['N+']
|
|
a['N-'] += t['N-']
|
|
a['dj+dE'] += t['N+']/As[t['r_bin']]/sim_time
|
|
a['dj-dE'] += t['N-']/As[t['r_bin']]/sim_time
|
|
f.close()
|
|
|
|
#append to final
|
|
|
|
df = df.append(a)
|
|
|
|
|
|
|
|
df.to_csv(inputpath + "/csv/df_part_reco.csv")
|
|
print("saved to " + inputpath + "/csv/df_part_reco.csv")
|
|
#return df
|
|
|
|
|
|
def get_csv_pop_reco(inputpath):
|
|
|
|
outputpath = inputpath + '/csv'
|
|
|
|
try:
|
|
os.mkdir(outputpath)
|
|
except:
|
|
pass
|
|
|
|
def append_V_m3 (df_meta):
|
|
A_m2 = np.pi*(df_meta.r_max_m**2-df_meta.r_min_m**2)*(df_meta.phi_max-df_meta.phi_min)/2/np.pi
|
|
V_m3 = A_m2*(df_meta.z_max_m-df_meta.z_min_m)
|
|
df_meta.insert(len(df_meta.columns),'V_m3',V_m3)
|
|
return df_meta
|
|
|
|
def get_V_m3(df_meta):
|
|
r_bins = np.unique(df_meta.r_bin)
|
|
#print(r_bins)
|
|
V_m3 = np.zeros(r_bins.size)
|
|
for i in np.arange(r_bins.size):
|
|
V_m3[i] = np.mean (df_meta[df_meta.r_bin==r_bins[i]].V_m3)
|
|
return V_m3
|
|
|
|
def append_n_m3 (df, V_m3):
|
|
n_m3 = df.cum_time/df.phys_time_s/V_m3[df.r_bin]
|
|
df['n_m3'] = n_m3
|
|
df['n_m3'].fillna(0., inplace=True)
|
|
return df
|
|
|
|
def append_from_meta(df, df_meta):
|
|
df_length = df.shape[0]
|
|
bin_list = ['z_bin','r_bin','phi_bin','E_bin']
|
|
min_list = ['z_min_m', 'r_min_m', 'phi_min', 'E_min_eV']
|
|
max_list = ['z_max_m', 'r_max_m', 'phi_max', 'E_max_eV']
|
|
for i in range(len(bin_list)):
|
|
i_bin = bin_list[i]
|
|
i_min = min_list[i]
|
|
i_max = max_list[i]
|
|
info_df = df_meta.groupby(i_bin, as_index=False).mean()
|
|
mins = np.zeros(df_length)
|
|
maxs = np.zeros(df_length)
|
|
for row in range(0, info_df.shape[0]):
|
|
mins[df[i_bin] == info_df[i_bin][row]] = info_df[i_min][row]
|
|
maxs[df[i_bin] == info_df[i_bin][row]] = info_df[i_max][row]
|
|
df[i_min] = mins
|
|
df[i_max] = maxs
|
|
return df
|
|
|
|
h5list = get_h5list(inputpath)
|
|
|
|
f = h5py.File(h5list[0], 'r')
|
|
|
|
df = pd.DataFrame()
|
|
species = list(f['Populations'].keys())
|
|
for s in species:
|
|
try: # If has been merged
|
|
pop_key = list(f['Populations'][s].keys())[0]
|
|
df_data = pd.DataFrame(f['Populations'][s][pop_key][s][:])
|
|
df_meta = pd.DataFrame(f['Populations'][s][pop_key][s + '_meta_data'][:])
|
|
except: # If not merged
|
|
df_data = pd.DataFrame(f['Populations'][s][s][:])
|
|
df_meta = pd.DataFrame(f['Populations'][s][s + '_meta_data'][:])
|
|
|
|
df_meta = append_V_m3(df_meta)
|
|
V_m3 = get_V_m3(df_meta)
|
|
df_data = append_n_m3(df_data, V_m3)
|
|
df_data = append_from_meta(df_data, df_meta)
|
|
|
|
a = pd.DataFrame({
|
|
'species' : s,
|
|
't' : df_data['phys_time_s'],
|
|
'zmin' : df_data['z_min_m'],
|
|
'zmax' : df_data['z_max_m'],
|
|
'rmin' : df_data['r_min_m'],
|
|
'rmax' : df_data['r_max_m'],
|
|
'phimin' : df_data['phi_min'],
|
|
'phimax' : df_data['phi_max'],
|
|
'Emin' : df_data['E_min_eV'],
|
|
'Emax' : df_data['E_max_eV'],
|
|
'n': df_data['n_m3']
|
|
})
|
|
df = df.append(a) #append to final
|
|
df.to_csv(inputpath + "/csv/df_pop_reco.csv")
|
|
print("saved to " + inputpath + "/csv/df_pop_reco.csv")
|
|
#return df
|
|
|
|
|
|
inputpath = sys.argv[1]
|
|
sim_time = float(sys.argv[2])
|
|
|
|
get_csv_part_reco(inputpath, sim_time)
|
|
get_csv_pop_reco(inputpath) |