6 Calculate FCal Sampling Fractions
7 =================================
9 Calculate the FCal sampling fractions from Geant4 simulation.
22 root.PyConfig.IgnoreCommandLineOptions =
True
26 """Return summary of docstring
28 return docstring.split(
'\n')[4]
if docstring
else ""
32 """Parse command-line arguments
34 parser = argparse.ArgumentParser(description=
_docstring(__doc__))
35 parser.add_argument(
"--version", action=
"version", version=
"%(prog)s 0.1")
36 parser.add_argument(
"-v",
"--verbose", action=
"count", default=0,
37 help=
"print verbose messages; multiple -v result in more verbose messages")
38 parser.add_argument(
"infile", default=
"LArFCalSamplingFraction.aan.root",
39 help=
"Path to input file; wildcards are supported (default: %(default)s)")
40 parser.add_argument(
"-m",
"--module", choices=[
"FCal1",
"FCal2",
"FCal3"],
41 help=
"Calculate the sampling fraction for this FCal module only; "
42 "if unspecified, it will try to parse the module from the file name")
43 parser.add_argument(
"-o",
"--outfile", default=
"out.txt",
44 help=
"Path to output file where sampling fraction results are written "
45 "(default: %(default)s)")
47 args = parser.parse_args()
53 """Read ntuple from the Geant4 simulation and calculate FCal sampling
56 Returns (E_init, samp_frac, samp_frac_err)
59 print(
"Reading tree '{}' from file '{}'...".
format(tree_name, args.infile))
61 aan_chain = root.TChain(tree_name)
62 aan_chain.Add(args.infile)
64 n_event = aan_chain.GetEntries()
67 for event
in aan_chain:
68 E_init =
round(event.E, 2) / 1000
78 print(
"Event Active E [MeV] Total E [MeV]")
81 for event
in aan_chain:
82 if args.module.lower() ==
"fcal1":
83 totalE = event.Calib_FCal1Active + event.Calib_FCal1Inactive
84 activeE = event.FCal1_E
85 elif args.module.lower() ==
"fcal2":
86 totalE = event.Calib_FCal2Active + event.Calib_FCal2Inactive
87 activeE = event.FCal2_E
88 elif args.module.lower() ==
"fcal3":
89 totalE = event.Calib_FCal3Active + event.Calib_FCal3Inactive
90 activeE = event.FCal3_E
92 samp_frac += activeE / totalE
93 samp_frac_sq += (activeE / totalE)**2
95 min_eta=
min(min_eta,event.Vertex_Eta)
96 max_eta=
max(max_eta,event.Vertex_Eta)
99 print(
"{:<6} {:<15g} {:<15g}".
format(event.Event, activeE, totalE))
106 samp_frac_sq /= n_event
109 samp_frac_err = math.sqrt(samp_frac_sq - samp_frac**2) / math.sqrt(n_event)
111 print(
"{} sampling fraction (E = {:g} GeV): {:g} +/- {:g}".
format(args.module, E_init, samp_frac, samp_frac_err))
114 outfile=root.TFile.Open(
"SF_LAr.root",
"UPDATE")
115 func=root.TF1(
"SF_{}_eta_{:4.2f}_{:4.2f}".
format(args.module,min_eta,max_eta),
"pol0",min_eta,max_eta)
116 func.SetParameter(0,samp_frac);
117 func.SetParError(0,samp_frac_err);
121 return E_init, samp_frac, samp_frac_err
125 with open(args.outfile,
"w")
as outfile:
126 outfile.write(
"[{}]\n".
format(args.module))
128 for key
in sorted(results.keys()):
129 outfile.write(
"{:g} GeV: {} +/- {}\n".
format(key, results[key][0], results[key][1]))
135 if args.module
is None:
136 if "fcal1" in args.infile.lower():
137 args.module =
"fcal1"
138 elif "fcal2" in args.infile.lower():
139 args.module =
"fcal2"
140 elif "fcal3" in args.infile.lower():
141 args.module =
"fcal3"
143 raise Exception(
"unknown FCal module")
145 infiles = glob.glob(args.infile)
150 for infile
in infiles:
154 results[E_init] = (samp_frac, samp_frac_err)
156 print(
"Writing results to '{}'".
format(args.outfile))
159 except KeyboardInterrupt:
162 except Exception
as err:
167 if __name__ ==
'__main__':