All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make_calib_df.py
Go to the documentation of this file.
1 import sys
2 import datetime as dt
3 from lib.glob import NTupleGlob
4 from lib import branches
5 import numpy as np
6 
7 # load constants
8 from lib.constants import *
9 
10 # EXTERNAL INPUT: The drift window in each TPC
11 tcathode_EE = 3198.5279397664003
12 tcathode_EW = 3207.147982327826
13 tcathode_WE = 3200.883742841676
14 tcathode_WW = 3199.9763136348492
15 
16 # Get external information on run timing
17 run_times = {}
18 with open("/icarus/app/users/gputnam/calib/rundata") as f:
19  for line in f:
20  dat = line.split(" ")
21  run_times[int(dat[0])] = dt.datetime.strptime(dat[1].rstrip("\n"), "%Y-%m-%dT%H:%M:%S").date()
22 
23 # Get external information on electron lifetime
24 run_etaus = {}
25 with open("/icarus/app/users/gputnam/calib/plots2/etau_run_data.txt") as f:
26  next(f) # Skip first (header) line
27  for line in f:
28  dat = line.split(" ")
29  run_etaus[int(dat[0])] = [float(d) for d in dat[1:]]
30 
31 plane2branches = [
32  "h.time", "h.width", "h.tpc", "dqdx", "pitch", "rr", "dir.x",
33 ]
34 plane2branches = ["hits2.%s" % s for s in plane2branches]
35 
36 ray_branches = [
37  "daughter_nsp",
38  "daughter_pdg",
39  "daughter_sp_toend_dist"
40 ]
41 
42 t0 = 0
43 def exp(t, *p):
44  A,tau = p
45  return A*np.exp(-(t - t0)/tau)
46 
47 def isTPCE(df):
48  return df.tpc <= 1
49 
50 def reduce_df(df, raydf=None):
51  # use the external input to build the t0
52  ccross_t0_E = df.hit_max_time_p2_tpcE - tcathode_EE
53  ccross_t0_E[df.cryostat==1] = df.hit_max_time_p2_tpcE - tcathode_WE
54 
55  ccross_t0_W = df.hit_max_time_p2_tpcW - tcathode_EW
56  ccross_t0_W[df.cryostat==1] = df.hit_max_time_p2_tpcW - tcathode_WW
57 
58  # Select stopping tracks
59  select_track = df.selected == 0
60 
61  df["ccross_t0"] = ((ccross_t0_E + ccross_t0_W) / 2.) * tick_period
62  df["tpcE"] = isTPCE(df.hits2.h)
63 
64  # What to save
65  outdf = df.loc[(df.hits2.dqdx > 0) & (df.hits2.rr < 200.) & select_track,
66  [ ("hits2", "h", "time"),
67  ("tpcE", "", ""),
68  ("hits2", "dqdx", ""),
69  ("hits2", "pitch", ""),
70  ("hits2", "rr", ""),
71  ("hits2", "dir", "x"),
72  ("hits2", "h", "width"),
73  ("ccross_t0", "", ""),
74  ("meta", "run", ""),
75  ("cryostat", "", ""),
76  ("dir", "y", ""),
77  ("hit_min_time_p2_tpcE", "", ""),
78  ("hit_max_time_p2_tpcE", "", ""),
79  ("hit_min_time_p2_tpcW", "", ""),
80  ("hit_max_time_p2_tpcW", "", ""),
81  ]
82  ].copy()
83 
84  # Simplify column names
85  outdf.columns = ["time", "tpcE", "dqdx_nocorr", "pitch", "rr", "dir_x", "width", "ccross_t0", "run", "cryostat", "tdir_y", "mint_tpcE", "maxt_tpcE", "mint_tpcW", "maxt_tpcW"]
86 
87  # Correct for electron lifetime
88  outdf["thit"] = (outdf.time * tick_period - outdf.ccross_t0 - tanode*tick_period) / 1000.
89  if len(outdf):
90  thisrun = outdf.run.iloc[0]
91  # Correct in each TPC
92  outdf["dqdx_corr"] = outdf.dqdx_nocorr * exp(outdf.thit, 1., -run_etaus[thisrun][0]*1e3)
93  outdf.loc[~outdf.tpcE & (outdf.cryostat==0), "dqdx_corr"] = (outdf.dqdx_nocorr * exp(outdf.thit, 1., -run_etaus[thisrun][1]*1e3))[~outdf.tpcE & (outdf.cryostat==0)]
94  outdf.loc[outdf.tpcE & (outdf.cryostat==1), "dqdx_corr"] = (outdf.dqdx_nocorr * exp(outdf.thit, 1., -run_etaus[thisrun][2]*1e3))[outdf.tpcE & (outdf.cryostat==1)]
95  outdf.loc[~outdf.tpcE & (outdf.cryostat==1), "dqdx_corr"] = (outdf.dqdx_nocorr * exp(outdf.thit, 1., -run_etaus[thisrun][3]*1e3))[~outdf.tpcE & (outdf.cryostat==1)]
96 
97  # Save information on PFP daughters
98  if raydf is not None:
99  closest_tdaughter = raydf.groupby(level=0).daughter_sp_toend_dist.min()
100  outdf = outdf.join(closest_tdaughter.rename("closest_tdaughter"), on="entry", how="left")
101 
102  return outdf
103 
104 def main(output, inputs):
105  ntuples = NTupleGlob(inputs, branches.trkbranches + plane2branches + ray_branches)
106  df = ntuples.dataframe(nproc="auto", f=reduce_df)
107  df.to_hdf(output, key="df", mode="w")
108 
109 if __name__ == "__main__":
110  printhelp = len(sys.argv) < 3 or sys.argv[1] == "-h"
111  if printhelp:
112  print("Usage: python make_calib_df.py [output.df] [inputs.root,]")
113  else:
114  main(sys.argv[1], sys.argv[2:])
do one_file $F done echo for F in find $TOP name CMakeLists txt print
T copy(T const &v)
Definition: glob.py:1
open(RACETRACK) or die("Could not open file $RACETRACK for writing")