CMS 3D CMS Logo

isotrackRootTreeMaker.py
Go to the documentation of this file.
1 
7 
8 import uproot3
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 
17 parser.add_argument("-PU", "--filePU",help="input PU file")
18 parser.add_argument("-NPU", "--fileNPU",help="input no PU file")
19 parser.add_argument("-O", "--opfilename",help="ouput file name")
20 parser.add_argument("-s", "--start", help="start entry for input PU file")
21 parser.add_argument("-e", "--end", help="end entry for input PU file")
22 
23 
24 fName1 = parser.parse_args().filePU
25 fName2 = parser.parse_args().fileNPU
26 foutput = parser.parse_args().opfilename
27 start = parser.parse_args().start
28 stop = parser.parse_args().end
29 
30 # PU
31 tree1 = uproot3.open(fName1)['hcalIsoTrkAnalyzer/CalibTree']
32 
33 #no PU
34 tree2 = uproot3.open(fName2)['hcalIsoTrkAnalyzer/CalibTree']
35 
36 #tree2.keys()
37 print ("loaded files")
38 
39 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']
40 
41 branchesnpu = ['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']
42 
43 dictpu = tree1.arrays(branchespu, entrystart=int(start), entrystop=int(stop))
44 
45 npu_entries = tree2.numentries
46 
47 scale = 5000000
48 npu_start = 0
49 i = 0
50 
51 for index in range(0,npu_entries, scale):
52  npu_stop = index+scale
53  if (npu_stop > npu_entries):
54  npu_stop = npu_entries
55  dictnpu = tree2.arrays(branchesnpu, entrystart=npu_start, entrystop=npu_stop)
56  npu_start = npu_stop
57  dfspu = pd.DataFrame.from_dict(dictpu)
58  dfspu.columns=branchespu
59  dfsnpu = pd.DataFrame.from_dict(dictnpu)
60  dfsnpu.columns=branchesnpu
61  print("loaded % of nopile file is =",(npu_stop/npu_entries)*100)
62  print ("PU sample size:",dfspu.shape[0])
63  print ("noPU sample size:",dfsnpu.shape[0])
64 
65  cuts_pu = (dfspu['t_selectTk'])&(dfspu['t_qltyFlag'])&(dfspu['t_hmaxNearP']<20)&(dfspu['t_eMipDR']<1)&(abs(dfspu['t_p'] - 50)<10)&(dfspu['t_eHcal']>10)
66 
67  cuts_npu = (dfsnpu['t_selectTk'])&(dfsnpu['t_qltyFlag'])&(dfsnpu['t_hmaxNearP']<20)&(dfsnpu['t_eMipDR']<1)&(abs(dfsnpu['t_p'] - 50)<10)&(dfsnpu['t_eHcal']>10)
68 
69  dfspu = dfspu.loc[cuts_pu]
70  dfspu = dfspu.reset_index(drop=True)
71 
72  dfsnpu = dfsnpu.loc[cuts_npu]
73  dfsnpu = dfsnpu.reset_index(drop=True)
74  branches_skim = ['t_Event','t_ieta','t_iphi','t_p','t_eHcal','t_eHcal10','t_eHcal30']
75  dfsnpu = dfsnpu[branches_skim]
76 
77  merged = pd.merge(dfspu, dfsnpu , on=['t_Event','t_ieta','t_iphi'])
78  print ("selected common events before cut:",merged.shape[0])
79  #print(merged)
80  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']
81 
82  merged3 = merged
83  #merged3 = merged3.reset_index(drop=True)
84  print ("selected events after cut:",merged3.shape[0])
85  merged3['t_delta_x']=merged3['t_eHcal30_x']-merged3['t_eHcal10_x']
86  merged3['t_delta_y']=merged3['t_eHcal30_y']-merged3['t_eHcal10_y']
87 
88  final_df_hi = merged3[keepvars]
89  final_df_hi.to_parquet(foutput+'_'+str(i)+"_"+start+"_"+stop+".parquet")
90  final_df_hi.to_csv(foutput+'_'+str(i)+"_"+start+"_"+stop+".txt")
91 
92  print(merged3['t_ieta'].dtype)
93 
94  with uproot3.recreate(foutput+'_'+str(i)+"_"+start+"_"+stop+".root") as f:
95 
96  f["tree"] = uproot3.newtree({"t_Event": np.int32,
97  "t_p_PU": np.float64,
98  "t_eHcal_PU":np.float64,
99  "t_delta_PU":np.float64,
100  "t_p_NoPU": np.float64,
101  "t_eHcal_noPU":np.float64,
102  "t_delta_NoPU":np.float64,
103  "t_ieta":np.int32})
104 
105 
106  f["tree"].extend({"t_Event": merged3['t_Event'],
107  "t_p_PU": merged3['t_p_x'].to_numpy(),
108  "t_eHcal_PU": merged3['t_eHcal_x'].to_numpy(),
109  "t_delta_PU": merged3['t_delta_x'].to_numpy(),
110  "t_p_NoPU": merged3['t_p_y'].to_numpy(),
111  "t_eHcal_noPU": merged3['t_eHcal_y'].to_numpy(),
112  "t_delta_NoPU": merged3['t_delta_y'].to_numpy(),
113  "t_ieta": merged3['t_ieta'].to_numpy()})
114  i += 1
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
#define str(s)