All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make_etau_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 plane2branches = [
24  "h.p.x", "h.p.y", "h.p.z", "h.time", "h.tpc", "dqdx", "dir.x", "dir.y", "dir.z",
25 ]
26 plane2branches = ["hits2.%s" % s for s in plane2branches]
27 
28 def isTPCE(df):
29  return df.tpc <= 1
30 
31 def reduce_df(df):
32  # use the external input to build the t0
33  ccross_t0_E = df.hit_max_time_p2_tpcE - tcathode_EE
34  ccross_t0_E[df.cryostat==1] = df.hit_max_time_p2_tpcE - tcathode_WE
35 
36  ccross_t0_W = df.hit_max_time_p2_tpcW - tcathode_EW
37  ccross_t0_W[df.cryostat==1] = df.hit_max_time_p2_tpcW - tcathode_WW
38 
39  # Select anode + cathode crossing tracks
40  select_track = df.selected == 1
41 
42  df["ccross_t0"] = ((ccross_t0_E + ccross_t0_W) / 2.) * tick_period
43 
44  df = df[(df.hits2.dqdx > 0) & select_track].copy()
45  df["chunk"] = df.index.get_level_values(1) // 5
46  df["tpcE"] = isTPCE(df.hits2.h)
47  outdf = df.groupby(["entry", "chunk"])[('hits2', 'dqdx', '', ''),].median()
48  outdf = outdf.join(df.groupby(["entry", "chunk"])[[('hits2', 'h', 'p', 'x'),
49  ('hits2', 'h', 'p', 'y'),
50  ('hits2', 'h', 'p', 'z'),
51  ('hits2', 'h', 'time', ''),
52  ('hits2', 'dir', 'x', ''),
53  ('hits2', 'dir', 'y', ''),
54  ('hits2', 'dir', 'z', ''),]].mean())
55 
56  outdf.columns = ["dqdx", "x", "y", "z", "time", "dirx", "diry", "dirz"]
57 
58  # fix the direction normalization
59  norm = np.sqrt(outdf.dirx**2 + outdf.diry**2 + outdf.dirz**2)
60  outdf.dirx = outdf.dirx / norm
61  outdf.diry = outdf.diry / norm
62  outdf.dirz = outdf.dirz / norm
63 
64  # TPC/Cryo info
65  outdf["tpcE"] = df.groupby(["entry", "chunk"]).tpcE.all()
66  outdf["tpcW"] = (~outdf.tpcE) & (df.groupby(["entry", "chunk"]).tpcE.nunique() == 1)
67 
68  # Save T0
69  outdf["ccross_t0"] = df.groupby(["entry", "chunk"]).ccross_t0.first()
70  # also save the cryostat number
71  outdf["cryostat"] = df.groupby(["entry", "chunk"]).cryostat.first()
72  # And run number
73  outdf["run"] = df.groupby(["entry", "chunk"])[[('meta', 'run', '', ''),]].first()
74 
75  # Only save chunks that are all in one TPC
76  outdf = outdf[outdf.tpcE | outdf.tpcW].drop(columns=["tpcW"])
77 
78  return outdf
79 
80 
81 def main(output, inputs):
82  ntuples = NTupleGlob(inputs, branches.trkbranches + plane2branches)
83  df = ntuples.dataframe(nproc="auto", f=reduce_df)
84  df.to_hdf(output, key="df", mode="w")
85 
86 if __name__ == "__main__":
87  printhelp = len(sys.argv) < 3 or sys.argv[1] == "-h"
88  if printhelp:
89  print("Usage: python make_etau_df.py [output.df] [inputs.root,]")
90  else:
91  main(sys.argv[1], sys.argv[2:])
do one_file $F done echo for F in find $TOP name CMakeLists txt print
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
T copy(T const &v)
Definition: glob.py:1
open(RACETRACK) or die("Could not open file $RACETRACK for writing")