CMS 3D CMS Logo

isotrackNtupler.py
Go to the documentation of this file.
1 
7 
8 
9 
10 import uproot
11 import numpy as np
12 import pandas as pd
13 import argparse
14 import matplotlib.pyplot as plt
15 
16 parser = argparse.ArgumentParser()
17 parser.add_argument("-PU", "--filePU",help="input PU file",default="root://eoscms.cern.ch//eos/cms/store/group/dpg_hcal/comm_hcal/ISOTRACK/SinglePion_E-50_Eta-0to3_Run3Winter21_112X_PU.root")
18 parser.add_argument("-NPU", "--fileNPU",help="input no PU file",default="//eoscms.cern.ch//eos/cms/store/group/dpg_hcal/comm_hcal/ISOTRACK/SinglePion_E-50_Eta-0to3_Run3Winter21_112X_PU.root")
19 parser.add_argument("-O", "--opfilename",help="ouput file name",default="isotk_relval")
20 
21 
22 fName1 = parser.parse_args().filePU
23 fName2 = parser.parse_args().fileNPU
24 foutput = parser.parse_args().opfilename
25 
26 
27 # PU
28 tree1 = uproot.open(fName1,xrootdsource=dict(chunkbytes=1024**3, limitbytes=1024**3))['HcalIsoTrkAnalyzer/CalibTree']
29 
30 #no PU
31 tree2 = uproot.open(fName2,xrootdsource=dict(chunkbytes=1024**3, limitbytes=1024**3))['HcalIsoTrkAnalyzer/CalibTree']
32 #tree2.keys()
33 print ("loaded files")
34 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']
35 branchesnpu = ['t_Event','t_ieta','t_iphi','t_eHcal']
36 #dictn = tree.arrays(branches=branches,entrystart=0, entrystop=300)
37 dictpu = tree1.arrays(branches=branchespu)
38 dictnpu = tree2.arrays(branches=branchesnpu)
39 dfspu = pd.DataFrame.from_dict(dictpu)
40 dfspu.columns=branchespu
41 dfsnpu = pd.DataFrame.from_dict(dictnpu)
42 dfsnpu.columns=branchesnpu
43 print ("loaded dicts and dfs")
44 print ("PU sample size:",dfspu.shape[0])
45 print ("noPU sample size:",dfsnpu.shape[0])
46 merged = pd.merge(dfspu, dfsnpu , on=['t_Event','t_ieta','t_iphi'])
47 print ("selected common events before cut:",merged.shape[0])
48 
49 #cuts = (merged['t_selectTk'])&(merged['t_qltyFlag'])&(merged['t_hmaxNearP']<10)&(merged['t_eMipDR_y']<1)
50 keepvars = ['t_nVtx','t_ieta','t_eHcal10','t_eHcal30','t_delta','t_hmaxNearP','t_emaxNearP','t_hAnnular','t_eAnnular','t_rhoh','t_pt','t_eHcal_x','t_eHcal_y','t_p','t_eMipDR']
51 
52 
53 
54 
55 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)
56 merged1=merged.loc[cuts1]
57 merged1 = merged1.reset_index(drop=True)
58 print ("selected events after cut for all ietas:",merged1.shape[0])
59 merged1['t_delta']=merged1['t_eHcal30']-merged1['t_eHcal10']
60 final_df_all = merged1[keepvars]
61 final_df_all.to_pickle(foutput+"_all.pkl")
62 final_df_all.to_csv(foutput+"_all.txt")
63 
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_df_low.to_pickle(foutput+"_lo.pkl")
72 final_df_low.to_csv(foutput+"_lo.txt")
73 
74 
75 
76 cuts3 = (merged['t_selectTk'])&(merged['t_qltyFlag'])&(merged['t_hmaxNearP']<20)&(merged['t_eMipDR']<1)&(abs(merged['t_ieta'])>15)&(abs(merged['t_p'] - 50)<10)&(merged['t_eHcal_x']>10)
77 merged3=merged.loc[cuts3]
78 merged3 = merged3.reset_index(drop=True)
79 print ("selected events after cut for ieta > 15:",merged3.shape[0])
80 merged3['t_delta']=merged3['t_eHcal30']-merged3['t_eHcal10']
81 final_df_hi = merged3[keepvars]
82 final_df_hi.to_pickle(foutput+"_hi.pkl")
83 final_df_hi.to_csv(foutput+"_hi.txt")
84 
Abs< T >::type abs(const T &t)
Definition: Abs.h:22