CMS 3D CMS Logo

isotrackRootTreeMaker.py
Go to the documentation of this file.
1 
6 
7 import uproot
8 import numpy as np
9 import pandas as pd
10 import matplotlib.pyplot as plt
11 import argparse
12 from mpl_toolkits.mplot3d import Axes3D
13 
14 parser = argparse.ArgumentParser()
15 parser.add_argument("-PU", "--filePU",help="input PU file",default="2021PU.root")
16 parser.add_argument("-NPU", "--fileNPU",help="input no PU file",default="2021noPU.root")
17 parser.add_argument("-O", "--opfilename",help="ouput file name",default="isotk_relval")
18 
19 fName1 = parser.parse_args().filePU
20 fName2 = parser.parse_args().fileNPU
21 foutput = parser.parse_args().opfilename
22 
23 # PU
24 tree1 = uproot.open(fName1,xrootdsource=dict(chunkbytes=1024**3, limitbytes=1024**3))['HcalIsoTrkAnalyzer/CalibTree']
25 
26 #no PU
27 tree2 = uproot.open(fName2,xrootdsource=dict(chunkbytes=1024**3, limitbytes=1024**3))['HcalIsoTrkAnalyzer/CalibTree']
28 
29 #tree2.keys()
30 print ("loaded files")
31 branchespu = ['t_Run','t_Event','t_nVtx','t_ieta','t_iphi','t_p','t_pt','t_gentrackP','t_eMipDR','t_eHcal','t_eHcal10','t_eHcal30','t_hmaxNearP','t_emaxNearP','t_hAnnular','t_eAnnular','t_rhoh','t_selectTk','t_qltyFlag']
32 branchesnpu = ['t_Event','t_ieta','t_iphi','t_p','t_eHcal','t_eHcal10','t_eHcal30']
33 #dictn = tree.arrays(branches=branches,entrystart=0, entrystop=300)
34 dictpu = tree1.arrays(branches=branchespu)
35 dictnpu = tree2.arrays(branches=branchesnpu)
36 dfspu = pd.DataFrame.from_dict(dictpu)
37 dfspu.columns=branchespu
38 dfsnpu = pd.DataFrame.from_dict(dictnpu)
39 dfsnpu.columns=branchesnpu
40 print ("loaded dicts and dfs")
41 print ("PU sample size:",dfspu.shape[0])
42 print ("noPU sample size:",dfsnpu.shape[0])
43 #dfspu
44 merged = pd.merge(dfspu, dfsnpu , on=['t_Event','t_ieta','t_iphi'])
45 print ("selected common events before cut:",merged.shape[0])
46 #print(merged)
47 keepvars = ['t_nVtx','t_ieta','t_eHcal10_x','t_eHcal30_x','t_delta_x','t_eHcal10_y','t_eHcal30_y','t_delta_y','t_hmaxNearP','t_emaxNearP','t_hAnnular','t_eAnnular','t_rhoh','t_pt','t_eHcal_x','t_eHcal_y','t_p_x','t_p_y','t_eMipDR']
48 
49 '''
50 #########################all ietas
51 cuts1 = (merged['t_selectTk'])&(merged['t_qltyFlag'])&(merged['t_hmaxNearP']<20)&(merged['t_eMipDR']<1)&(abs(merged['t_p'] - 50)<10)&(merged['t_eHcal_x']>10)
52 merged1=merged.loc[cuts1]
53 merged1 = merged1.reset_index(drop=True)
54 print ("selected events after cut for all ietas:",merged1.shape[0])
55 merged1['t_delta']=merged1['t_eHcal30']-merged1['t_eHcal10']
56 final_df_all = merged1[keepvars]
57 #final_dfnp = final_df.values
58 #np.save('isotk_relval_all.npy',final_df_all.values)
59 #np.save('isotk_relval_all.npy',final_df_all)
60 final_df_all.to_pickle(foutput+"_all.pkl")
61 final_df_all.to_csv(foutput+"_all.txt")
62 #########################split ieta < 16
63 
64 cuts2 = (merged['t_selectTk'])&(merged['t_qltyFlag'])&(merged['t_hmaxNearP']<20)&(merged['t_eMipDR']<1)&(abs(merged['t_ieta'])<16)&(abs(merged['t_p'] - 50)<10)&(merged['t_eHcal_x']>10)
65 merged2=merged.loc[cuts2]
66 merged2 = merged2.reset_index(drop=True)
67 print ("selected events after cut for ieta < 16:",merged2.shape[0])
68 merged2['t_delta']=merged2['t_eHcal30']-merged2['t_eHcal10']
69 final_df_low = merged2[keepvars]
70 #final_dfnp = final_df.values
71 #np.save('isotk_relval_lo.npy',final_df_low.values)
72 #np.save('isotk_relval_lo.npy',final_df_low)
73 final_df_low.to_pickle(foutput+"_lo.pkl")
74 final_df_low.to_csv(foutput+"_lo.txt")
75 '''
76 
77 
78 
79 cuts3 = (merged['t_selectTk'])&(merged['t_qltyFlag'])&(merged['t_hmaxNearP']<20)&(merged['t_eMipDR']<1)&(abs(merged['t_p_x'] - 50)<10)&(merged['t_eHcal_x']>10)
80 merged3=merged.loc[cuts3]
81 merged3 = merged3.reset_index(drop=True)
82 print ("selected events after cut for ieta > 24:",merged3.shape[0])
83 merged3['t_delta_x']=merged3['t_eHcal30_x']-merged3['t_eHcal10_x']
84 merged3['t_delta_y']=merged3['t_eHcal30_y']-merged3['t_eHcal10_y']
85 
86 final_df_hi = merged3[keepvars]
87 final_df_hi.to_pickle(foutput+"_hi.pkl")
88 final_df_hi.to_csv(foutput+"_hi.txt")
89 
90 '''
91 threedee = plt.figure().gca(projection='3d')
92 threedee.scatter(final_df_hi['t_eHcal_x'], final_df_hi['t_eHcal_y'], final_df_hi['t_delta'])
93 threedee.set_xlabel('Corrected Energy')
94 threedee.set_ylabel('Uncorrected Energy')
95 threedee.set_zlabel('delta')
96 fig = threedee.get_figure()
97 fig.show()
98 fig.savefig('debu.png')
99 
100 print(type(merged3['t_p']))
101 print(merged3['t_p'])
102 print(merged3['t_p'].to_numpy())
103 
104 a=merged3['t_p'].to_numpy()
105 print(type(a))
106 print(a.ndim)
107 print(a.shape)
108 '''
109 
110 print(merged3['t_ieta'].dtype)
111 
112 with uproot.recreate(foutput+".root") as f:
113 
114  f["tree"] = uproot.newtree({"t_Event": np.int32,
115  "t_p_PU": np.float64,
116  "t_eHcal_PU":np.float64,
117  "t_delta_PU":np.float64,
118  "t_p_NoPU": np.float64,
119  "t_eHcal_noPU":np.float64,
120  "t_delta_NoPU":np.float64,
121  "t_ieta":np.int32})
122 
123 
124  f["tree"].extend({"t_Event": merged3['t_Event'],
125  "t_p_PU": merged3['t_p_x'].to_numpy(),
126  "t_eHcal_PU": merged3['t_eHcal_x'].to_numpy(),
127  "t_delta_PU": merged3['t_delta_x'].to_numpy(),
128  "t_p_NoPU": merged3['t_p_y'].to_numpy(),
129  "t_eHcal_noPU": merged3['t_eHcal_y'].to_numpy(),
130  "t_delta_NoPU": merged3['t_delta_y'].to_numpy(),
131  "t_ieta": merged3['t_ieta'].to_numpy()})
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Abs< T >::type abs(const T &t)
Definition: Abs.h:22