6 Calculate FCal Sampling Fractions
7 =================================
9 Calculate the FCal sampling fractions from Geant4 simulation.
12 from __future__
import absolute_import, division, print_function
23 root.PyConfig.IgnoreCommandLineOptions =
True
27 """Return summary of docstring
29 return docstring.split(
'\n')[4]
if docstring
else ""
33 """Parse command-line arguments
35 parser = argparse.ArgumentParser(description=
_docstring(__doc__))
36 parser.add_argument(
"--version", action=
"version", version=
"%(prog)s 0.1")
37 parser.add_argument(
"-v",
"--verbose", action=
"count", default=0,
38 help=
"print verbose messages; multiple -v result in more verbose messages")
39 parser.add_argument(
"infile", default=
"LArFCalSamplingFraction.aan.root",
40 help=
"Path to input file; wildcards are supported (default: %(default)s)")
41 parser.add_argument(
"-m",
"--module", choices=[
"FCal1",
"FCal2",
"FCal3"],
42 help=
"Calculate the sampling fraction for this FCal module only; "
43 "if unspecified, it will try to parse the module from the file name")
44 parser.add_argument(
"-o",
"--outfile", default=
"out.txt",
45 help=
"Path to output file where sampling fraction results are written "
46 "(default: %(default)s)")
48 args = parser.parse_args()
54 """Read ntuple from the Geant4 simulation and calculate FCal sampling
57 Returns (E_init, samp_frac, samp_frac_err)
60 print(
"Reading tree '{}' from file '{}'...".
format(tree_name, args.infile))
62 aan_chain = root.TChain(tree_name)
63 aan_chain.Add(args.infile)
65 n_event = aan_chain.GetEntries()
68 for event
in aan_chain:
69 E_init =
round(event.E, 2) / 1000
76 print(
"Event Active E [MeV] Total E [MeV]")
79 for event
in aan_chain:
80 if args.module.lower() ==
"fcal1":
81 totalE = event.Calib_FCal1Active + event.Calib_FCal1Inactive
82 activeE = event.FCal1_E
83 elif args.module.lower() ==
"fcal2":
84 totalE = event.Calib_FCal2Active + event.Calib_FCal2Inactive
85 activeE = event.FCal2_E
86 elif args.module.lower() ==
"fcal3":
87 totalE = event.Calib_FCal3Active + event.Calib_FCal3Inactive
88 activeE = event.FCal3_E
90 samp_frac += activeE / totalE
91 samp_frac_sq += (activeE / totalE)**2
94 print(
"{:<6} {:<15g} {:<15g}".
format(event.Event, activeE, totalE))
101 samp_frac_sq /= n_event
104 samp_frac_err = math.sqrt(samp_frac_sq - samp_frac**2) / math.sqrt(n_event)
106 print(
"{} sampling fraction (E = {:g} GeV): {:g} +/- {:g}".
format(args.module, E_init, samp_frac, samp_frac_err))
108 return E_init, samp_frac, samp_frac_err
112 with open(args.outfile,
"w")
as outfile:
113 outfile.write(
"[{}]\n".
format(args.module))
115 for key
in sorted(results.keys()):
116 outfile.write(
"{:g} GeV: {} +/- {}\n".
format(key, results[key][0], results[key][1]))
122 if args.module
is None:
123 if "fcal1" in args.infile.lower():
124 args.module =
"fcal1"
125 elif "fcal2" in args.infile.lower():
126 args.module =
"fcal2"
127 elif "fcal3" in args.infile.lower():
128 args.module =
"fcal3"
130 raise Exception(
"unknown FCal module")
132 infiles = glob.glob(args.infile)
137 for infile
in infiles:
141 results[E_init] = (samp_frac, samp_frac_err)
143 print(
"Writing results to '{}'".
format(args.outfile))
146 except KeyboardInterrupt:
149 except Exception
as err:
154 if __name__ ==
'__main__':