Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mdolce muon efficiency #154

Merged
merged 27 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6b9b164
sign_distance_efficiency_muon_true_ke.py, starting from the signed_di…
mdolce8 Sep 5, 2024
f394922
sign_distance_efficiency_vs_muon_true_ke.py, we don't use Line_Candid…
mdolce8 Sep 5, 2024
d043da4
sign_distance_efficiency_vs_muon_true_ke.py, update the outdir and ou…
mdolce8 Sep 5, 2024
91ed634
sign_distance_efficiency_vs_muon_true_ke.py, create the hists, set th…
mdolce8 Sep 5, 2024
b7bc891
sign_distance_efficiency_vs_muon_true_ke.py, these are the objects we…
mdolce8 Sep 5, 2024
3e690f6
sign_distance_efficiency_vs_muon_true_ke.py, create the muon KE binni…
mdolce8 Sep 6, 2024
a312635
sign_distance_efficiency_vs_muon_true_ke.py, then address the aesthet…
mdolce8 Sep 6, 2024
ab13a0f
sign_distance_efficiency_vs_muon_true_ke.py, some small typos
mdolce8 Sep 6, 2024
4fc8800
sign_distance_efficiency_vs_muon_true_ke.py, these are hists not list…
mdolce8 Sep 6, 2024
f19ed87
sign_distance_efficiency_vs_muon_true_ke.py, forgot the index starts …
mdolce8 Sep 6, 2024
7aff829
sign_distance_efficiency_vs_muon_true_ke.py, ah we have some garbage …
mdolce8 Sep 6, 2024
cfa5452
sign_distance_efficiency_vs_muon_true_ke.py, And forgot to include th…
mdolce8 Sep 6, 2024
f6041bb
sign_distance_efficiency_vs_muon_true_ke.py, if --n is small, you may…
mdolce8 Sep 6, 2024
fe2cdad
sign_distance_efficiency_vs_muon_true_ke.py, go all the way to the en…
mdolce8 Sep 6, 2024
549c709
sign_distance_efficiency_vs_muon_true_ke.py, go all the way to the en…
mdolce8 Sep 6, 2024
0289fa5
sign_distance_efficiency_vs_muon_true_ke.py, again, these are not lis…
mdolce8 Sep 6, 2024
83825f5
sign_distance_efficiency_vs_muon_true_ke.py, the whole title is a lat…
mdolce8 Sep 9, 2024
1eb16cf
sign_distance_efficiency_vs_muon_true_ke.py, fix title again.
mdolce8 Sep 9, 2024
ab1b7bd
sign_distance_efficiency_vs_muon_true_ke.py, I forgot an important de…
mdolce8 Sep 9, 2024
6416a56
sign_distance_efficiency_vs_muon_true_ke.py, had an issue, the logic …
mdolce8 Sep 9, 2024
57a4557
sign_distance_efficiency_vs_muon_true_ke.py, debug why the efficienci…
mdolce8 Sep 9, 2024
8088dfd
sign_distance_efficiency_vs_muon_true_ke.py, Ohh, I forgot the S.D. i…
mdolce8 Sep 9, 2024
c827001
sign_distance_efficiency_vs_muon_true_ke.py, and looks like some even…
mdolce8 Sep 9, 2024
562a734
sign_distance_efficiency_vs_muon_true_ke.py, it is working! Let's rem…
mdolce8 Sep 9, 2024
7d861ce
sign_distance_efficiency_vs_muon_true_ke.py, here are some TODOs that…
mdolce8 Sep 11, 2024
7e9d406
plot_multiple_truth_efficiencies.py, script to take the output from t…
mdolce8 Sep 11, 2024
4ef7b95
plot_multiple_truth_efficiencies.py, had the labels wrong for the plots.
mdolce8 Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions scripts/Truth/plot_multiple_truth_efficiencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# plot_multiple_truth_signed_distances.py
# this script is used to plot the efficiencies from
# multiple different B-fields.
# Reads in the output ROOT file from
# dune-tms/scripts/Truth/sign_distance_efficiency_vs_muon_true_ke.py.
# So, that script has to be run first for any B field efficiency you want to plot here.

# Note: user may need to install some python packages:
# pip install --user matplotlib uproot
# (--force-reinstall if you have a conflict)

# Sept. 2024.
# M. Dolce, [email protected].


import argparse
import matplotlib.pyplot as plt
import os
import uproot as up

# parse the arguments
parser = argparse.ArgumentParser(description='Plot the sign distance of muons and anti-muons together.')
parser.add_argument('--indir', type=str, help='The input dir of the ROOT files.')
parser.add_argument('--outdir', type=str, help='The output dir name.')
args = parser.parse_args()

def process_args(args_from_argparse):
"""
Process the arguments from the command line.
:param args_from_argparse: the arguments from argparse
:rtype: dictionary of files, string of outdir
:returns: infiles, out_dir
"""
assert os.path.exists(args_from_argparse.indir), f'Directory {args_from_argparse.indir} not found.'
d_infiles = {}
for f in os.listdir(args_from_argparse.indir):
print('file.......', args_from_argparse.indir + '/' + f)
if not f.endswith('.root'):
continue
if not os.path.exists(args_from_argparse.indir + '/' + f):
raise FileNotFoundError(f'File {args_from_argparse.indir}/{f} not found.')
# find the B Field from the filename.
s = f.split('_')
if 'bfield' in s:
bfield = s[s.index('bfield') + 1].strip('.root')
d_infiles[bfield] = args_from_argparse.indir + '/' + f
out_dir = args_from_argparse.outdir or f"/exp/dune/data/users/{os.environ['USER']}/dune-tms_hists/plot_multiple_truth_efficiencies"
os.makedirs(out_dir, exist_ok=True)
print('Outdir.......', out_dir)

return d_infiles, out_dir

infiles, outdir = process_args(args)


# Four separate dictionaries for {LAr,TMS} x {muon, a-muon}.
d_bfield_th1s_muon_lar, b_field_th1s_muon_tms, b_field_th1s_amuon_lar, b_field_th1s_amuon_tms = {}, {}, {}, {}
for bfield, infile in infiles.items():
print('Opening file...', infile)
with up.open(infile) as file:
for key in file.keys():

# clean up the key names
key = key.split(';')[0]

# I saved the canvases to the ROOT file, but I don't want them here (prob. a canvas)
if not file[key].classname.startswith("TH1"):
continue

if 'lar' in key and key.startswith('muon'):
d_bfield_th1s_muon_lar[bfield] = file[key]
elif 'tms' in key and key.startswith('muon'):
b_field_th1s_muon_tms[bfield] = file[key]
elif 'lar' in key and key.startswith('amuon'):
b_field_th1s_amuon_lar[bfield] = file[key]
elif 'tms' in key and key.startswith('amuon'):
b_field_th1s_amuon_tms[bfield] = file[key]
else:
print('I don\'t knw what to do with this key:', key)


# we have 4 histograms for each B field: muon and amuon in TMS and LAr.
list_colors = ['blue', 'red', 'green', 'orange']

# first two are muons, second two are anti-muons.
for i, d in enumerate([d_bfield_th1s_muon_lar, b_field_th1s_muon_tms, b_field_th1s_amuon_lar, b_field_th1s_amuon_tms]):
print(i, ' -- ', d)
fig = plt.figure(figsize=(10, 6))
color_idx = 0
for key_bfield, th1 in d.items():
det = 'lar' if (i == 0 or i == 2) else 'tms'
particle = 'muon' if (i <= 1) else 'amuon'
pdg_string = fr'$\mu^-$' if particle == "muon" else fr'$\mu^+$'
plt.step(th1.axis().edges()[:-1], th1.values(), where='post', label=f'{pdg_string} @ {key_bfield.replace("p", ".")}', color=list_colors[color_idx])

plt.title('Signed Distance Efficiency')
plt.xlabel(f'Muon Kinetic Energy (MeV), in {det.upper().replace("LAR", "LAr")}')
plt.ylabel('Efficiency')
plt.legend(title='B Fields', loc='lower left', ncol=3, fontsize=8, columnspacing=1.0)
plt.xlim(0, 5000)
plt.grid()
for ext in ['png', 'pdf']:
plt.savefig(f'{outdir}/plot_multiple_truth_efficiencies_{particle}_{det}.{ext}', dpi=300, bbox_inches='tight')
# plt.show()
color_idx += 1

print('Done.')
Loading
Loading