All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make_equalibriate_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", "h.wire", "dqdx", "pitch",
33 ]
34 
35 plane2branches = ["hits2.%s" % s for s in plane2branches]
36 
37 
38 t0 = 0
39 def exp(t, *p):
40  A,tau = p
41  return A*np.exp(-(t - t0)/tau)
42 
43 def isTPCE(df):
44  return df.tpc <= 1
45 
46 def reduce_df(df):
47  # use the external input to build the t0
48  ccross_t0_E = df.hit_max_time_p2_tpcE - tcathode_EE
49  ccross_t0_E[df.cryostat==1] = df.hit_max_time_p2_tpcE - tcathode_WE
50 
51  ccross_t0_W = df.hit_max_time_p2_tpcW - tcathode_EW
52  ccross_t0_W[df.cryostat==1] = df.hit_max_time_p2_tpcW - tcathode_WW
53 
54  # Select anode + cathode crossing tracks
55  select_track = df.selected == 1
56 
57  df["ccross_t0"] = ((ccross_t0_E + ccross_t0_W) / 2.) * tick_period
58  df["tpcE"] = isTPCE(df.hits2.h)
59 
60  # What to save
61  outdf = df.loc[(df.hits2.dqdx > 0) & select_track,
62  [ ("hits2", "h", "time"),
63  ("hits2", "h", "wire"),
64  ("tpcE", "", ""),
65  ("hits2", "dqdx", ""),
66  ("hits2", "pitch", ""),
67  ("ccross_t0", "", ""),
68  ("meta", "run", ""),
69  ("cryostat", "", ""),
70  ]
71  ].copy()
72 
73  # Simplify column names
74  outdf.columns = ["time", "wire", "tpcE", "dqdx_nocorr", "width", "ccross_t0", "run", "cryostat"]
75 
76  # Correct for electron lifetime
77  outdf["thit"] = (outdf.time * tick_period - outdf.ccross_t0 - tanode*tick_period) / 1000.
78  if len(outdf):
79  thisrun = outdf.run.iloc[0]
80  # Correct in each TPC
81  outdf["dqdx_corr"] = outdf.dqdx_nocorr * exp(outdf.thit, 1., -run_etaus[thisrun][0]*1e3)
82  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)]
83  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)]
84  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)]
85 
86  return outdf
87 
88 def main(output, inputs):
89  ntuples = NTupleGlob(inputs, branches.trkbranches + plane2branches)
90  df = ntuples.dataframe(nproc="auto", f=reduce_df)
91  df.to_hdf(output, key="df", mode="w")
92 
93 if __name__ == "__main__":
94  printhelp = len(sys.argv) < 3 or sys.argv[1] == "-h"
95  if printhelp:
96  print("Usage: python make_equalibriate_df.py [output.df] [inputs.root,]")
97  else:
98  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")