# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
import random 
from matplotlib import cm
from matplotlib.pyplot import figure

event = pd.read_hdf('~/gnnfiles/data/ntuple_PU200_numEvent1000/ntuple_PU200_event1.h5')

p660 = event[event['particle_id']==476]

plt.plot(p660['z'], p660['r'], "kx")

i = pd.read_csv('inference_resuts.csv')

i

p476 = i[i['particle_id']==-18987]
plt.plot(p476['mc_z'], p476['mc_r'], "kx")
plt.plot(p476['pred_z'], p476['pred_r'], "b*")

event.particle_id.values[::100]

event.describe()

r = np.loadtxt('g_r.csv')
z = np.loadtxt('g_z.csv')
pids = np.loadtxt('g_pids.csv')
reward = np.loadtxt('g_reward.csv')
files = np.loadtxt('g_filenumber.csv')
alt_files = np.loadtxt('g_files.csv')
a0 = np.loadtxt('g_a0.csv')
a1 = np.loadtxt('g_a1.csv')

# !stat g_a0.csv


len(np.where(np.abs(a1) > 1)[0])

a1

plt.plot(range(len(a1)), a1)

a0[:-100]

# !stat g_filenumber.csv

rf = pd.DataFrame({'file':files, 
                   'particle_id':pids,
                   'r':r, 
                  'z':z, 
                  #'reward': reward
                  })

rf


# !stat g_filenumber.csv

def plot_random_track(filenumber): 
    rf_file = rf[rf['file']==filenumber]
    
    event_r = pd.read_hdf('~/gnnfiles/data/ntuple_PU200_numEvent1000/ntuple_PU200_event' + str(filenumber)+'.h5')
    random_pid = random.choice(rf_file.particle_id.values)
    print(random_pid)

    true_hits = event_r[event_r['particle_id']==random_pid]
    #print(true_hits)
    rf_hits = rf_file[rf_file['particle_id']==random_pid]
    #print(rf_hits.reward)
    
    plt.plot(true_hits.z, true_hits.r, "kx", label="True hits")
    plt.plot(rf_hits.z, rf_hits.r, "b*", label="Reinforcement predictions")
    plt.xlabel("z (cm)")
    plt.ylabel("r (cm)")
    #plt.legend() 
    
    
    plt.show() 

# +
plot_random_track(10)


# -

rf

np.mean(rf.groupby(['file', 'particle_id']).count().values[:,0])

p353 = event[event['particle_id']==416]
p353

# +
i =  1000


plt.plot(p353.z[:7], p353.r[:7], "kx")
rf353 = rf[i*7:(i*7+7)]
plt.plot(rf353.z, rf353.r, "b*")#
cumulative_rewards = reward.reshape(-1, 7).sum(1)
plt.text(0,0, s="reward "+ str(cumulative_rewards[i]))
# -

max(a0)

# +

smol1 = rf[i*7:i*7+7]
# -

smol2 = p353.iloc[:7, ][['z', 'r']]

plt.plot(smol2.z, smol2.r, "kx")
plt.plot(smol1.z, smol1.r, "b*")

# +

np.sqrt((smol1.z.values - smol2.z.values)**2 + (smol1.r.values - smol2.r.values)**2)
# -

reward[i*7:i*7+7]

reward

a0[180:186]

smol1.z.values + a0[180:187]

smol1.z

reward[:-(len(reward)%7)]

# +
n = 100
  
# calculates the average
avgResult = np.average(reward.reshape(-1, n), axis=1)


plt.plot(range(len(avgResult)), avgResult)
plt.xlabel("Number of ephocs in 100s")
plt.ylabel("Average reward over 100 epochs")
plt.savefig("trainingplot.png")
# -





plt.plot(range(len(reward)), reward)

rf[:-20]

a0[:-20]

a1[:-20]

np.std(reward)

event.groupby('particle_id').agg('count').iloc[:,0].describe()

a1[:-10]

pd.read_csv('/home/lhv14/garage/src/garage/examples/tf/data/local/experiment/ddpg_partcile_189/progress.csv')

pd.read_pickle('/home/lhv14/garage/src/garage/examples/tf/data/local/experiment/ddpg_partcile_191/params.pkl')

event.groupby('particle_id').count()

 obs = np.array([[ 2.56000004e+01,  3.92771077e+00,  3.99999619e-01,
         6.42023087e-02,  0.00000000e+00, -7.24246346e-04],
       [ 2.52000008e+01,  3.86350846e+00, -3.99999619e-01,
        -8.12386036e-01,  1.51199985e+00,  2.13940407e-01],
       [ 3.25760002e+01,  4.96691322e+00, -1.99999809e-01,
        -3.99995804e-01, -1.96524173e+00, -3.94052538e-03],
       [ 4.02989998e+01,  6.12514687e+00,  2.19199944e+00,
         1.29535198e-02,  5.18526535e-01,  6.72745549e-03],
       [ 4.06990013e+01,  6.18270874e+00,  3.67474985e+00,
         2.99136817e-01, -1.83498792e+00, -3.07377931e-02],
       [ 5.17109985e+01,  7.84998941e+00,  2.93979988e+00,
         2.50772047e-01, -2.24658865e+00, -1.10327255e-02],
       [ 5.21110001e+01,  7.91252232e+00,  4.35183303e+00,
         4.86736576e-01,  1.33371169e+00, -2.92752863e-02],
       [ 6.71080017e+01,  1.02038422e+01,  3.73014259e+00,
         4.26426649e-01,  2.44075081e+00, -9.30227549e-03],
       [ 6.75080032e+01,  1.02619343e+01,  5.08849978e+00,
         6.59344763e-01, -2.57759163e+00, -3.71289431e-02],
       [ 8.53779984e+01,  1.29735317e+01,  4.52311092e+00,
         5.92013650e-01, -2.41459239e+00, -5.72700358e-03],
       [ 8.53779984e+01,  1.29735317e+01,  5.93779964e+00,
         8.04044700e-01,  3.11376608e+00, -8.82741025e-03],
       [ 8.53779984e+01,  1.29735317e+01,  5.39799968e+00,
         7.30151415e-01, -9.67534367e-01,  7.26834476e-04],
       [ 8.53779984e+01,  1.29735317e+01,  4.94816637e+00,
         6.70037250e-01,  9.67534367e-01, -7.26834476e-04],
       [ 8.53779984e+01,  1.29735317e+01,  4.56753819e+00,
         6.17820428e-01, -9.67534367e-01,  7.26834476e-04]])

plt.plot(obs[:,0:2][:,0], obs[:,0:2][:,1], "kx")

obs[:,0:2][:,0]

obs[:,0:2][:,1]

e101 = pd.read_hdf('~/gnnfiles/data/ntuple_PU200_numEvent1000/ntuple_PU200_event101.h5')

rf_hits = np.array([[ 7.87616444e+00,  2.75680518e+00, -1.04751768e+01,
        -3.64632201e+00,  0.00000000e+00, -2.97137772e-03],
       [ 1.83513412e+01,  6.40312719e+00, -9.66324806e-01,
        -6.28757477e-03,  1.18089921e+00, -1.20474423e-01],
       [ 1.82203674e+01,  6.40524960e+00,  4.73660040e+00,
         1.57162535e+00,  6.98936462e-01,  1.47723593e-01],
       [ 1.75683918e+01,  6.42296886e+00,  3.14822292e+00,
         1.04773291e+00,  2.05846757e+00, -1.54979721e-03],
       [ 2.43999996e+01,  8.99099064e+00,  2.16018605e+00,
         7.85817444e-01,  2.70489293e+00, -4.48842673e-02],
       [ 2.52000008e+01,  9.29278946e+00,  3.30476704e+00,
         1.04642987e+00,  2.04761702e+00,  8.24126197e-02],
       [ 2.52000008e+01,  9.18471050e+00,  2.75397253e+00,
         9.24365401e-01, -5.54223299e-01, -3.65727031e-02],
       [ 2.56000004e+01,  9.30591774e+00,  2.36054788e+00,
         7.74861097e-01,  7.49343753e-01,  1.40431502e-02],
       [ 2.56000004e+01,  9.44733524e+00,  2.11547935e+00,
         6.93795472e-01,  1.77843261e+00,  8.87106752e-04],
       [ 3.25760002e+01,  1.20897913e+01,  1.83598169e+00,
         6.32305596e-01,  2.02841389e+00, -3.12558303e-02],
       [ 3.25760002e+01,  9.12926769e+00,  2.42998343e+00,
         8.33348107e-01, -1.88308001e-01,  4.20155265e-03],
       [ 3.17759991e+01,  1.17876062e+01,  2.17271224e+00,
         4.88367449e-01, -1.57679653e+00,  2.85655844e-01],
       [ 4.14990005e+01,  9.08602333e+00,  1.95831958e+00,
         7.43695279e-01,  2.86297208e+00, -3.58221124e-01],
       [ 4.06990013e+01,  1.51095638e+01,  2.49406426e+00,
         4.10202191e-01, -5.14467065e-01,  5.94648565e-01],
       [ 4.02989998e+01,  1.49603157e+01,  2.31591681e+00,
         8.10981086e-01,  1.82866806e+00, -5.36957829e-01],
       [ 5.17109985e+01,  9.03945065e+00,  2.16152236e+00,
         7.46562084e-01, -2.39865035e+00,  1.04517178e-02]])

p = e101[e101['particle_id']==-16982]

# +
plt.plot(p.z, p.r, "kx")

plt.plot(rf_hits[:,0:2][:,0], rf_hits[:,0:2][:,1], "b*")
# -

inf = pd.read_csv('/home/lhv14/garage/src/garage/examples/tf/test_inference_results.csv')

# +
p = -8808.0

ep = e101[e101['particle_id']==p]
ip = inf[inf['particle_id']==p]

plt.plot(ep.z, ep.r, "kx")
plt.plot(ip.observation_z, ip.observation_r, "b*")

# -

inf[inf['particle_id']==-7617]

tevent = event.copy()

tevent['test_phi'] = np.arctan(tevent.y.values/tevent.x.values)

event[event['particle_id']==-3394
     ]

np.mean(np.abs(tevent['sim_phi'] - tevent['test_phi']))

tevent.describe()

np.pi/2

0%pt_min': config['selection']['pt_min'],
            'z0_max': config['selection']['z0_max'],
            'phi_max': config['selection']['phi_slope_max'],
            'total time': t1-t0,
            'number of events': len(all_files),
            'mean time per event

l = pd.read_csv('/home/lhv14/graph_building_timing.csv', header=None)

l.columns = ['pt_min','z0 max', 'phi max', 'total time', 'number of events', 'mean time per event']

l995 = l[l['number of events']==995]

times = l995.groupby('pt_min').mean()
times

# +
from matplotlib.pyplot import figure

fig, ax = plt.subplots(figsize=(8, 6))

ax.plot(times.index, times.iloc[:,2], "kx", markersize=10)
ax.set_xlabel("Minimum $p_T$", fontsize=16)
ax.set_ylabel("Average time to build graph \n (one thread CPU) (s)", fontsize=16)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
#leg = ax.legend(prop={"size":16})
# -

event

tracks = event.groupby('particle_id').count().iloc[:,0].values

len(tracks)

len(np.where(tracks==1)[0])

1723/18049*100

# +
goal=np.array((1., 1.), dtype=np.float32),

goal = np.array(goal, dtype=np.float32)
# -

goal

one = event.iloc[0, :]

np.array([one.z, one.r])


p353 = event[event['particle_id']==353]

p353

np.sqrt(3**2 + 3**2)

np.linalg.norm(np.array([5,5]) - 
               
               np.array([2,2]))

np.sqrt((5 - 2)**2 + (5 - 2)**2)

a = np.array([-5.        ,  4.55663623])
b = np.array([2,2])

np.linalg.norm(a-b)

rf_file = pd.read_csv('/home/lhv14/garage/src/garage/examples/tf/garage_outputs.csv')


pids = rf_file.particle_id.values[::100]

n_trackhits = 6

p1 = rf_file[(rf_file['particle_id']==pids[0]) &(rf_file['filenumber']==1) ]
p2 = p1.iloc[:n_trackhits-1, ]

for i in range(n_trackhits*2-1): 
    #print(i)
    if i < (n_trackhits-1): 
        hit = p2.iloc[i, :]
        print(hit.mc_z, hit.mc_r)
    elif (i > (n_trackhits-2)) & (i < (n_trackhits*2-2)): 
        hit = p2.iloc[i-(n_trackhits-1), :]
        print(hit.pred_z, hit.pred_r)
    else: 
        break

p2

pids

files

np.unique(event.particle_id.values)[:10]

small = event[event['particle_id'].isin(np.unique(event.particle_id.values)[:10])]

# small




# +
import seaborn as sns
colors = cm.Paired.colors

figure(figsize=(8, 6), dpi=80)
plt.style.use('seaborn-notebook')

plt.scatter(small.z, small.r, c=small.particle_id)

plt.axvline(x=0, linewidth=3, c='k')
plt.xlabel('z (cm)', fontsize=20)
plt.ylabel('r (cm)', fontsize=20)
plt.tick_params(axis='both', which='major', labelsize=20)
# -

small2 = small.copy()
small2['z'] = np.abs(small['z'])

# +
figure(figsize=(8, 6), dpi=80)
plt.style.use('seaborn-notebook')

plt.scatter(small2.z, small2.r, c=small.particle_id)

#plt.axvline(x=0, linewidth=3, c='k')
plt.xlabel('z (cm)', fontsize=20)
plt.ylabel('r (cm)', fontsize=20)
plt.tick_params(axis='both', which='major', labelsize=20)
# -



StarcoderdataJupyterScriptsDedupFiltered