CMS 3D CMS Logo

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