|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (C) 2023 luca.baldini@pi.infn.it |
| 4 | +# |
| 5 | +# For the license terms see the file LICENSE, distributed along with this |
| 6 | +# software. |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or modify it |
| 9 | +# under the terms of the GNU General Public License as published by the |
| 10 | +# Free Software Foundation; either version 2 of the License, or (at your |
| 11 | +# option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, but |
| 14 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | +# General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License along |
| 19 | +# with this program; if not, write to the Free Software Foundation Inc., |
| 20 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + |
| 22 | +"""Event file viewer comparing reconstructed quantities with MC truth. |
| 23 | +""" |
| 24 | +from ast import literal_eval |
| 25 | + |
| 26 | +import numpy as np |
| 27 | + |
| 28 | +from hexsample.app import ArgumentParser |
| 29 | +from hexsample.fileio import ReconInputFile |
| 30 | +from hexsample.hist import Histogram1d, Histogram2d |
| 31 | +from hexsample.plot import plt, setup_gca |
| 32 | +from hexsample.analysis import create_histogram |
| 33 | + |
| 34 | + |
| 35 | +__description__ = \ |
| 36 | +"""Simple viewer for reconstructed event lists. |
| 37 | +""" |
| 38 | + |
| 39 | +# Parser object. |
| 40 | +HXVIEW_ARGPARSER = ArgumentParser(description=__description__) |
| 41 | +HXVIEW_ARGPARSER.add_infile() |
| 42 | + |
| 43 | +def hxview(**kwargs): |
| 44 | + """View the file content. |
| 45 | + Shows histograms of energy and cluster_size of recon events vs their MC truth. |
| 46 | + """ |
| 47 | + input_file = ReconInputFile(kwargs['infile']) |
| 48 | + # Plotting the reconstructed energy and the true energy |
| 49 | + histo = create_histogram(input_file, 'energy', mc = False) |
| 50 | + mc_histo = create_histogram(input_file, 'energy', mc = True) |
| 51 | + plt.figure('Photons energy') |
| 52 | + histo.plot(label='Reconstructed') |
| 53 | + mc_histo.plot(label='MonteCarlo') |
| 54 | + plt.xlabel('Energy [eV]') |
| 55 | + plt.legend() |
| 56 | + |
| 57 | + # Plotting the reconstructed x and y position and the true position. |
| 58 | + plt.figure('Reconstructed photons position') |
| 59 | + binning = np.linspace(-5. * 0.1, 5. * 0.1, 100) |
| 60 | + x = input_file.column('posx') |
| 61 | + y = input_file.column('posy') |
| 62 | + histo = Histogram2d(binning, binning).fill(x, y) |
| 63 | + histo.plot() |
| 64 | + setup_gca(xlabel='x [cm]', ylabel='y [cm]') |
| 65 | + plt.figure('True photons position') |
| 66 | + x_mc = input_file.mc_column('absx') |
| 67 | + y_mc = input_file.mc_column('absy') |
| 68 | + histo_mc = Histogram2d(binning, binning).fill(x_mc, y_mc) |
| 69 | + histo_mc.plot() |
| 70 | + setup_gca(xlabel='x [cm]', ylabel='y [cm]') |
| 71 | + #Closing the file and showing the figures. |
| 72 | + plt.figure('x-direction resolution') |
| 73 | + binning = np.linspace((x-x_mc).min(), (x-x_mc).max(), 100) |
| 74 | + histx = Histogram1d(binning, xlabel=r'$x - x_{MC}$ [cm]').fill(x-x_mc) |
| 75 | + histx.plot() |
| 76 | + plt.figure('y-direction resolution') |
| 77 | + binning = np.linspace((y-y_mc).min(), (y-y_mc).max(), 100) |
| 78 | + histy = Histogram1d(binning, xlabel=r'$y - y_{MC}$ [cm]').fill(y-y_mc) |
| 79 | + histy.plot() |
| 80 | + |
| 81 | + input_file.close() |
| 82 | + plt.show() |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + hxview(**vars(HXVIEW_ARGPARSER.parse_args())) |
0 commit comments