diff --git a/src/nectarchain/trr_verification_package/charge_resolution.py b/src/nectarchain/trr_verification_package/charge_resolution.py index 51a97d21c..e846f6c34 100644 --- a/src/nectarchain/trr_verification_package/charge_resolution.py +++ b/src/nectarchain/trr_verification_package/charge_resolution.py @@ -148,6 +148,8 @@ def main(): args = parser.parse_args() camera = args.camera + temperature = args.temperature + nevents = args.evts output_dir = os.path.join( os.path.abspath(args.output), @@ -170,6 +172,33 @@ def main(): runs_list = df["runs"].tolist() ff_v_list = df["ff_v"].tolist() + run_charge_resolution( + NSB=NSB, + runs_list=runs_list, + ff_v_list=ff_v_list, + temperature=temperature, + nevents=nevents, + camera=camera, + output_dir=output_dir, + temp_output_args=args.temp_output, + ) + + +def run_charge_resolution( + NSB, + runs_list, + ff_v_list, + temperature=14, + nevents=1000, + camera="NectarCAMQM", + output_dir="./", + temp_output_args=None, +): + temp_output = os.path.abspath(temp_output_args) if temp_output_args else None + + log.debug(f"Output directory: {output_dir}") + log.debug(f"Temporary output directory: {temp_output}") + color = ["black", "red", "blue", "green", "yellow"] log.info("NSB Run FF", NSB, runs_list, ff_v_list) @@ -179,8 +208,6 @@ def main(): mean_resolution_nsb_err = [] mean_charge_err = [] log.info(f"NSB: length {len(NSB)}, NSB rate {NSB} MHz") - temperature = args.temperature - nevents = args.evts window_shift = 4 window_width = 16 @@ -214,7 +241,7 @@ def main(): gain_run = int(get_gain_run(temperature)) gain_file_name = ( - "FlatFieldSPENominalStdNectarCAM_run{}_maxevents{}_" + "resources/FlatFieldSPENominalStdNectarCAM_run{}_maxevents{}_" "{}_window_shift_{}_window_width_{}.h5".format( gain_run, max_events, method, window_shift, window_width ) @@ -297,12 +324,6 @@ def main(): plot_path = os.path.join(output_dir, f"{fig_name}.png") plt.savefig(plot_path) - if temp_output: - with open( - os.path.join(args.temp_output, f"plot_{fig_name}.pkl"), "wb" - ) as f: - pickle.dump(fig, f) - ratio_lghg_nsb.append(ratio_hglg) ff_volt = np.array(ff_volt) @@ -391,7 +412,7 @@ def main(): plt.savefig(plot_path) if temp_output: - with open(os.path.join(args.temp_output, f"plot_{fig_name}.pkl"), "wb") as f: + with open(os.path.join(temp_output_args, f"plot_{fig_name}.pkl"), "wb") as f: pickle.dump(fig, f) charge_plot = np.linspace(20, 1000) @@ -441,11 +462,19 @@ def main(): plt.savefig(plot_path) if temp_output: - with open(os.path.join(args.temp_output, f"plot_{fig_name}.pkl"), "wb") as f: + with open(os.path.join(temp_output, f"plot_{fig_name}.pkl"), "wb") as f: pickle.dump(fig, f) plt.close("all") + return ( + mean_charge, + mean_charge_err, + mean_resolution_nsb, + mean_resolution_nsb_err, + ratio_hglg, + ) + if __name__ == "__main__": main() diff --git a/src/nectarchain/trr_verification_package/deadtime.py b/src/nectarchain/trr_verification_package/deadtime.py index 6f9fcefd4..cb5f16b2e 100644 --- a/src/nectarchain/trr_verification_package/deadtime.py +++ b/src/nectarchain/trr_verification_package/deadtime.py @@ -554,6 +554,127 @@ def run_deadtime_test_tool_process( ) +def run_deadtime( + nevents, + runlist, + ids, + camera, + output_dir=None, + temp_output=None, + test_type="trr", +): + ( + _, + _, + event_counter, + busy_counter, + collected_trigger_rates, + time_tot, + deadtime_us, + deadtime_pc, + ) = run_deadtime_test_tool_process( + runlist=runlist, nevents=nevents, ids=ids, camera=camera, test_type=test_type + ) + + log.info(f"The plots will be saved at: {output_dir}") + + results = fit_rate_per_run( + runlist=runlist, + deadtime_us=deadtime_us, + )[-1] + + avg_deadtime_pc = [] + + log.info(f"Output directory: {output_dir}") + log.info(f"Temporary output file: {temp_output}") + log.info(f"N max events to be considered: {nevents}") + log.info("-" * 40) + for ii, (key, values) in enumerate(results.items()): + log.info(f"For run {key}, source: {ids[ii]},") + log.info( + "Dead-Time extracted from the tool process: " + f"{np.min(deadtime_us[ii]):.3f}" + ) + log.info("Dead-Time from the fit: " f"{values[0]:.3f} us") + log.info(f"Collected rate: {collected_trigger_rates[ii]:.2f} Hz") + log.info(f"Rate from the fit: {values[2]:.2f} Hz") + log.info(f"Dead-Time percentage from the tool process: {deadtime_pc[ii]} %") + log.info(f"Expected run duration from the fit: {values[4]:.2f} s") + log.info("-" * 40) + if collected_trigger_rates[ii] < 8500 or collected_trigger_rates[ii] > 6500: + avg_deadtime_pc.append(deadtime_pc[ii]) + # compute average deadtime % with only runs at 7 kHz for the thermal test + log.info( + "Average Dead-Time percentage" + + f"for runs at ~7 kHz: {np.mean(avg_deadtime_pc):.3f} %" + ) + log.info("-" * 40) + + ids = np.array(ids) + runlist = np.array(runlist) + + error_deadtime_pc = [] + for run_id in range(np.array(busy_counter).shape[0]): + error_deadtime_pc.append( + np.sqrt( + (busy_counter[run_id][-1] * event_counter[run_id][-1]) + / ((busy_counter[run_id][-1] + event_counter[run_id][-1]) ** 3) + ) + ) + error_deadtime_pc = np.array(error_deadtime_pc) + + deadtime, deadtime_err = [], [] + fitted_trigger_rates, fitted_trigger_rates_err = [], [] + + for ii, run_num in enumerate(runlist): + plot_results = plot_deadtime_and_expo_fit( + total_delta_t_for_busy_time=time_tot[ii], + deadtime_us=np.array(deadtime_us[ii].value), + run=run_num, + output_plot=output_dir, + temp_output=temp_output, + ) + deadtime.append(plot_results[0]) + deadtime_err.append(np.abs(plot_results[2]) * 1e-3) + fit_trigger_rate = results[run_num][2] / 1e3 # convert to kHz + fit_trigger_rate_plot = ((-1 * plot_results[6]) * (1 / u.us)).to(u.kHz).value + if np.abs(fit_trigger_rate - collected_trigger_rates[ii] / 1e3) < np.abs( + fit_trigger_rate_plot - collected_trigger_rates[ii] / 1e3 + ): + fitted_trigger_rates.append(fit_trigger_rate) + else: + fitted_trigger_rates.append(fit_trigger_rate_plot) + fitted_trigger_rates_err.append( + ((plot_results[8]) * (1 / u.us)).to(u.kHz).value + ) + plt.close() + + deadtime = np.array(deadtime) + fitted_trigger_rates = np.array(fitted_trigger_rates) + fitted_trigger_rates_err = np.array(fitted_trigger_rates_err) + + deadtime_pc_fit = np.array( + [ + # the parameter_lambda is a rate value in kHz, + # so one needs to compare the deadtime in mus with the rate in kHz + # and finally make it a percentage value + deadtime[ii] * rate * 1e2 * 1e-3 + for ii, rate in enumerate(fitted_trigger_rates) + ] + ) + + return ( + collected_trigger_rates, + fitted_trigger_rates, + fitted_trigger_rates_err, + deadtime, + deadtime_err, + deadtime_pc, + error_deadtime_pc, + deadtime_pc_fit, + ) + + def get_args(): """Parses command-line arguments for the deadtime test script. @@ -735,83 +856,28 @@ def main(): f"{test_type}_camera_{camera}/{Path(__file__).stem}", ) os.makedirs(output_dir, exist_ok=True) - temp_output = os.path.abspath(args.temp_output) if args.temp_output else None # Drop arguments from the script after they are parsed, for the GUI to work properly sys.argv = sys.argv[:1] ( + collected_trigger_rates, + fitted_trigger_rates, + fitted_trigger_rates_err, _, _, - event_counter, - busy_counter, - collected_trigger_rates, - time_tot, - deadtime_us, deadtime_pc, - ) = run_deadtime_test_tool_process( - runlist=runlist, camera=camera, nevents=nevents, ids=ids, test_type=test_type - ) - - results = fit_rate_per_run(runlist=runlist, deadtime_us=deadtime_us)[-1] - - log.info(f"Output directory: {output_dir}") - log.info(f"Temporary output file: {temp_output}") - log.info(f"N max events to be considered: {nevents}") - log.info("-" * 40) - for ii, (key, values) in enumerate(results.items()): - log.info(f"For run {key}, source: {ids[ii]},") - log.info( - "Dead-Time extracted from the tool process: " - f"{np.min(deadtime_us[ii]):.3f}" - ) - log.info(f"Dead-Time from the fit: {values[0]:.3f} +- " f"{values[1]:.3f} µs") - log.info(f"Rate from the fit: {values[2]:.2f} +- " f"{values[3]:.2f} Hz") - log.info("Expected run duration from the fit: " f"{values[4]:.2f} s") - log.info("-" * 40) - - ids = np.array(ids) - runlist = np.array(runlist) - - error_deadtime_pc = [] - for run_id in range(np.array(busy_counter).shape[0]): - error_deadtime_pc.append( - np.sqrt( - (busy_counter[run_id][-1] * event_counter[run_id][-1]) - / ((busy_counter[run_id][-1] + event_counter[run_id][-1]) ** 3) - ) - ) - error_deadtime_pc = np.array(error_deadtime_pc) - - deadtime, fitted_trigger_rates, fitted_trigger_rates_err = [], [], [] - - for ii, run_num in enumerate(runlist): - results = plot_deadtime_and_expo_fit( - total_delta_t_for_busy_time=time_tot[ii], - deadtime_us=np.array(deadtime_us[ii].value), - run=run_num, - output_plot=output_dir, - run_type=EventType(ids[ii]).name, - temp_output=temp_output, - ) - deadtime.append(results[0]) - fitted_trigger_rates.append(((-1 * results[6]) * (1 / u.us)).to(u.kHz).value) - fitted_trigger_rates_err.append(((results[8]) * (1 / u.us)).to(u.kHz).value) - plt.close() - - deadtime = np.array(deadtime) - fitted_trigger_rates = np.array(fitted_trigger_rates) - fitted_trigger_rates_err = np.array(fitted_trigger_rates_err) - - deadtime_pc_fit = np.array( - [ - # the parameter_lambda is a rate value in kHz, - # so one needs to compare the deadtime in mus with the rate in kHz - # and finally make it a percentage value - deadtime[ii] * rate * 1e2 * 1e-3 - for ii, rate in enumerate(fitted_trigger_rates) - ] + error_deadtime_pc, + deadtime_pc_fit, + ) = run_deadtime( + nevents=nevents, + runlist=runlist, + camera=camera, + ids=ids, + output_dir=output_dir, + temp_output=temp_output, + test_type=test_type, ) if len(runlist) > 1: diff --git a/src/nectarchain/trr_verification_package/linearity.py b/src/nectarchain/trr_verification_package/linearity.py index f4545725a..8a7f45d08 100644 --- a/src/nectarchain/trr_verification_package/linearity.py +++ b/src/nectarchain/trr_verification_package/linearity.py @@ -21,7 +21,7 @@ plot_parameters, transmission_390ns, ) -from nectarchain.utils.constants import ALLOWED_CAMERAS +from nectarchain.utils.constants import ALLOWED_CAMERAS, GAIN_LINEAR_RANGE logging.basicConfig( format="%(asctime)s %(name)s %(levelname)s %(message)s", @@ -30,15 +30,6 @@ ) log = logging.getLogger(__name__) -try: - plt.style.use( - os.path.join( - os.path.abspath(os.path.dirname(__file__)), "../utils/plot_style.mpltstyle" - ) - ) -except FileNotFoundError as e: - raise e - def get_args(): """Parses command-line arguments for the linearity test script. @@ -81,13 +72,22 @@ def get_args(): ) parser.add_argument( "-t", - "--trans", + "--transmission", type=float, nargs="+", help="List of corresponding transmission for each run", required=False, default=transmission_390ns, ) + parser.add_argument( + "-tt", + "--temperature", + type=int, + help="Temperature, default 14", + required=False, + default=14, + ) + parser.add_argument( "-e", "--evts", @@ -100,7 +100,8 @@ def get_args(): "-o", "--output", type=str, - help="Output directory", + help="Output directory.", + required=False, default=f"{os.environ.get('NECTARCHAIN_FIGURES', f'/tmp/{os.getpid()}')}", ) parser.add_argument( @@ -140,25 +141,44 @@ def main(): parser = get_args() args = parser.parse_args() - camera = args.camera - runlist = args.runlist - transmission = args.trans # corresponding transmission for above data + transmission = args.transmission # corresponding transmission for above data nevents = args.evts - + camera = args.camera + temperature = args.temperature output_dir = os.path.join( os.path.abspath(args.output), f"trr_camera_{camera}/{Path(__file__).stem}", ) - os.makedirs(output_dir, exist_ok=True) - log.debug(f"Output directory: {output_dir}") - temp_output = os.path.abspath(args.temp_output) if args.temp_output else None - log.debug(f"Temporary output directory: {temp_output}") + + log.info(f"Output directory: {output_dir}") # Debug print + # log.info(f"Temporary output file: {args.temp_output}") # Debug print sys.argv = sys.argv[:1] # runlist = [3441] + run_linearity( + runlist=runlist, + transmission=transmission, + temperature=temperature, + camera=camera, + nevents=nevents, + output_dir=output_dir, + temp_output_args=args.temp_output, + ) + + +def run_linearity( + runlist, + transmission, + temperature=14, + camera="NectarCAMQM", + nevents=500, + output_dir="./", + temp_output_args=None, +): + temp_output = os.path.abspath(temp_output_args) if temp_output_args else None charge = np.zeros((len(runlist), 2)) std = np.zeros((len(runlist), 2)) @@ -166,12 +186,12 @@ def main(): index = 0 for run in runlist: - log.info(f"PROCESSING RUN {run}") + log.info("PROCESSING RUN {}".format(run)) pedestal_tool = PedestalNectarCAMCalibrationTool( progress_bar=True, run_number=run, + max_events=nevents, camera=camera, - max_events=12000, events_per_slice=5000, log_level=20, output_path=output_dir + f"/pedestal_{run}.h5", @@ -183,12 +203,12 @@ def main(): tool = LinearityTestTool( progress_bar=True, run_number=run, - camera=camera, - events_per_slice=999, + events_per_slice=5000, max_events=nevents, + camera=camera, log_level=20, method="LocalPeakWindowSum", - extractor_kwargs={"window_width": 14, "window_shift": 6}, + extractor_kwargs={"window_width": 16, "window_shift": 4}, pedestal_file=output_dir + f"/pedestal_{run}.h5", overwrite=True, ) @@ -206,9 +226,11 @@ def main(): norm_factor_hg = charge[ np.argwhere((transmission < 1.1e-2) & (transmission > 9e-3)), 0 ][0] + norm_factor_lg = charge[ np.argwhere((transmission < 1.1e-2) & (transmission > 9e-3)), 1 ][0] + charge_norm_hg = charge[:, 0] / norm_factor_hg charge_norm_lg = charge[:, 1] / norm_factor_lg std_norm_hg = std[:, 0] / norm_factor_hg @@ -243,6 +265,7 @@ def main(): axs[2].axvspan(10, 1000, alpha=0.2, color="orange") axs[2].set_xlabel("Illumination charge [p.e.]") + fit_parameters = [] for _, (channel_charge, channel_std, name) in enumerate( zip( [charge_norm_hg, charge_norm_lg], @@ -258,38 +281,36 @@ def main(): # linearity model = Model(linear_fit_function) - params = model.make_params(a=100, b=0) + params = model.make_params(a=1, b=0) true = ch_sorted[:, 0] ch_charge = ch_sorted[:, 1] * norm_factor_hg[0] ch_std = ch_sorted[:, 2] * norm_factor_hg[0] ch_err = ch_std / np.sqrt(npixels) - ch_fit = model.fit( - ch_charge[ - plot_parameters[name]["linearity_range"][0] : plot_parameters[name][ - "linearity_range" - ][1] - ], - params, - weights=1 - / ch_err[ - plot_parameters[name]["linearity_range"][0] : plot_parameters[name][ - "linearity_range" - ][1] - ], - x=true[ - plot_parameters[name]["linearity_range"][0] : plot_parameters[name][ - "linearity_range" - ][1] - ], - ) + mask_LG = (ch_charge > 10) & (ch_charge < 3000) # LG + mask_HG = (ch_charge > 1) & (ch_charge < 200) # HG + + if str(plot_parameters[name]["initials"]) == "HG": + ch_charge_inp = ch_charge[mask_HG] + true_inp = true[mask_HG] + ch_err_inp = ch_err[mask_HG] + + else: + ch_charge_inp = ch_charge[mask_LG] + true_inp = true[mask_LG] + ch_err_inp = ch_err[mask_LG] + + ch_fit = model.fit(ch_charge_inp, params, x=true_inp, weights=1.0 / ch_err_inp) + + log.debug(ch_fit.fit_report()) a = ch_fit.params["a"].value b = ch_fit.params["b"].value a_err = ch_fit.params["a"].stderr b_err = ch_fit.params["b"].stderr + fit_parameters.append((a, b, a_err, b_err)) axs[0].errorbar( true, ch_charge, @@ -299,21 +320,10 @@ def main(): marker="o", color=plot_parameters[name]["color"], ) + axs[0].plot( - true[ - plot_parameters[name]["linearity_range"][0] : plot_parameters[name][ - "linearity_range" - ][1] - ], - linear_fit_function( - true[ - plot_parameters[name]["linearity_range"][0] : plot_parameters[name][ - "linearity_range" - ][1] - ], - a, - b, - ), + true_inp, + linear_fit_function(true_inp, a, b), color=plot_parameters[name]["color"], ) @@ -388,19 +398,33 @@ def main(): ratio = ratio_sorted[:, 1] ratio_std = ratio_sorted[:, 2] - model = model = Model(linear_fit_function) - params = model.make_params(a=100, b=0) - ratio_fit = model.fit( - ratio[10:-4], params, weights=1 / ratio_std[10:-4], x=true[10:-4] - ) + model = Model(linear_fit_function) + params = model.make_params(a=10, b=0) axs[2].set_ylabel("hg/lg") axs[2].set_xlabel("charge(p.e.)") axs[2].errorbar(true, ratio, yerr=ratio_std, ls="", color="C1", marker="o") + + mask_linear = (true > GAIN_LINEAR_RANGE[0]) & (true < 300) + ratio_fit = model.fit( + ratio[mask_linear], + params, + weights=1 / ratio_std[mask_linear], + x=true[mask_linear], + ) + + log.debug( + "true === ", + true[mask_linear], + linear_fit_function( + true[mask_linear], ratio_fit.params["a"].value, ratio_fit.params["b"].value + ), + ) + axs[2].plot( - true[10:-4], + true[mask_linear], linear_fit_function( - true[10:-4], ratio_fit.params["a"].value, ratio_fit.params["b"].value + true[mask_linear], ratio_fit.params["a"].value, ratio_fit.params["b"].value ), ls="-", color="C1", @@ -432,13 +456,12 @@ def main(): color="k", alpha=0.9, ) - - fig_name = "linearity_test" + fig_name = f"linearity_test_T{temperature}" plot_path = os.path.join(output_dir, f"{fig_name}.png") plt.savefig(plot_path) if temp_output: - with open(os.path.join(args.temp_output, f"plot_{fig_name}.pkl"), "wb") as f: + with open(os.path.join(temp_output_args, f"plot_{fig_name}.pkl"), "wb") as f: pickle.dump(fig, f) # charge resolution @@ -499,16 +522,17 @@ def main(): plt.xlim(3e-2, 4e3) plt.legend(frameon=False) - fig_name = "charge_resolution" plot_path = os.path.join(output_dir, f"{fig_name}.png") plt.savefig(plot_path) if temp_output: - with open(os.path.join(args.temp_output, f"plot_{fig_name}.pkl"), "wb") as f: + with open(os.path.join(temp_output_args, f"plot_{fig_name}.pkl"), "wb") as f: pickle.dump(fig, f) plt.close("all") + return fit_parameters, ratio, ratio_std + if __name__ == "__main__": main() diff --git a/src/nectarchain/trr_verification_package/observation_temperature.py b/src/nectarchain/trr_verification_package/observation_temperature.py new file mode 100644 index 000000000..ed1e92aa9 --- /dev/null +++ b/src/nectarchain/trr_verification_package/observation_temperature.py @@ -0,0 +1,971 @@ +import argparse +import logging +import os +import sys +from pathlib import Path + +import matplotlib +import matplotlib.pyplot as plt +import numpy as np +import pandas as pd +from lmfit.models import Model +from scipy import stats + +from nectarchain.trr_verification_package.charge_resolution import run_charge_resolution +from nectarchain.trr_verification_package.deadtime import run_deadtime +from nectarchain.trr_verification_package.linearity import run_linearity +from nectarchain.trr_verification_package.utils import linear_fit_function +from nectarchain.utils.constants import ALLOWED_CAMERAS + +matplotlib.use("Qt5Agg") # interactive Qt5 backend for showing plots + +logging.basicConfig( + format="%(asctime)s %(name)s %(levelname)s %(message)s", + level=logging.INFO, + handlers=[logging.getLogger("__main__").handlers], +) +log = logging.getLogger(__name__) + +try: + plt.style.use( + os.path.join( + os.path.abspath(os.path.dirname(__file__)), + "../utils/plot_style.mpltstyle", + ) + ) +except FileNotFoundError as e: + raise e + + +def get_args(): + """Parses command-line arguments for the linearity test script. + + Returns: + argparse.ArgumentParser: The parsed command-line arguments. + """ + + parser = argparse.ArgumentParser( + description="""Intensity resolution B-TEL-1010 using FF+NSB runs. + +According to the nectarchain component interface, you have to set a NECTARCAMDATA +environment variable in the folder where you have the data from your runs or where +you want them to be downloaded. + + +If the data are not in `$NECTARCAMDATA`, the files will be downloaded through DIRAC. + +For the purposes of testing this script, default data are from the runs used for this +test in the TRR document. + +You can optionally specify the number of events to be processed (default 500). +""" + ) + parser.add_argument( + "-r", + "--run_file", + type=str, + help="Run file path and name", + required=False, + default="resources/observation_temperature_run_list.json", + ) + + parser.add_argument( + "-m", + "--run_module", + type=str, + help="Module to run", + choices=["all", "linearity", "charge_resolution", "deadtime"], + required=False, + default="all", + ) + parser.add_argument( + "-c", + "--camera", + choices=ALLOWED_CAMERAS, + default=[camera for camera in ALLOWED_CAMERAS if "QM" in camera][0], + help="Process data for a specific NectarCAM camera.", + type=str, + ) + + parser.add_argument( + "-e", + "--evts", + type=int, + help="Number of events to process from each run. Default is 500", + required=False, + default=500, + ) + parser.add_argument( + "-o", + "--output", + type=str, + help="Output base directory", + required=False, + default=f"{os.environ.get('NECTARCHAIN_FIGURES', f'/tmp/{os.getpid()}')}", + ) + parser.add_argument( + "--temp_output", help="Temporary output directory for GUI", default=None + ) + + return parser + + +class ObservationTemperaturePipeline: + """Generates the temperature dependent plots""" + + def __init__( + self, run_module, run_file, camera, max_events, output_dir, temp_output + ): + # Initialise parameters by reading run_file + + self.run_module = run_module + self.run_file = run_file + self.max_events = max_events + self.output_dir = output_dir + self.temp_output = temp_output + self.camera = camera + + self.df = pd.read_json(run_file) + self.df_module = self.df.copy() + + def run(self): + """ + Dispatches processing to the correct module functions + based on self.run_module or all modules in the metadata. + """ + # Determine which modules to run + if self.run_module.lower() == "all": + modules_to_run = self.df_module["module"].unique() + else: + modules_to_run = [self.run_module] + + # Loop over each module and call the corresponding method + for module in modules_to_run: + logging.info(f"Running module: {module}") + df_module_filtered = self.df_module[self.df_module["module"] == module] + + if module == "charge_resolution": + self.plot_charge_resolution_temperature(df_module_filtered) + elif module == "linearity": + self.plot_linearity_temperature(df_module_filtered) + elif module == "deadtime": + self.plot_deadtime_temperature(df_module_filtered) + else: + logging.warning(f"No function defined for module: {module}") + + def plot_charge_resolution_temperature(self, df_module_filtered): + """Plots + 1. Charge as function of FF_v for different temperatures + 2. Charge resolution as function of temperature for different NSB levels + Computed percentage change in charge resolution with temperature. + """ + + temperatures = sorted(df_module_filtered["temperature"].unique()) + + outputs_comb = [] + + for t in temperatures: + df_temp = df_module_filtered[df_module_filtered["temperature"] == t] + + NSB_list = df_temp["NSB"].unique() + + runs_list = df_temp["runs"].tolist() + ff_v_list = df_temp["ff_v"].tolist() + log.info("parameters =============== ", t, NSB_list, runs_list, ff_v_list) + + output = run_charge_resolution( + NSB=NSB_list, + runs_list=runs_list, + ff_v_list=ff_v_list, + temperature=t, + nevents=self.max_events, + output_dir=self.output_dir, + ) + ( + mean_charge, + mean_charge_err, + mean_resolution_nsb, + mean_resolution_nsb_err, + ratio_hglg, + ) = output + for nsb_index, nsb in enumerate(NSB_list): + outputs_comb.append( + { + "temperature": t, + "NSB": nsb, + "ff_v": np.array(ff_v_list[nsb_index]), + "mean_charge": mean_charge[nsb_index], + "mean_charge_err": mean_charge_err[nsb_index], + "mean_charge_resolution": mean_resolution_nsb[nsb_index], + "mean_charge_resolution_err": mean_resolution_nsb_err[ + nsb_index + ], + } + ) + + df_output = pd.DataFrame(outputs_comb) + df_output = df_output.explode( + [ + "ff_v", + "mean_charge", + "mean_charge_err", + "mean_charge_resolution", + "mean_charge_resolution_err", + ] + ) + + colors = plt.cm.viridis(np.linspace(0, 1, len(df_output["ff_v"].unique()))) + ff_list = sorted(df_output["ff_v"].unique()) + + marker = ["o", "s", "d", "^", "*"] + + # Calibration curves dependent on temperature + df_nsb0 = df_output[df_output["NSB"] == 0] + + plt.figure() + for temp_fixed in df_nsb0["temperature"].unique(): + df_nsb_temp = df_nsb0[df_nsb0["temperature"] == temp_fixed] + ff_v = df_nsb_temp["ff_v"] + mean_charge = df_nsb_temp["mean_charge"] + + plt.errorbar( + ff_v, + mean_charge, + yerr=df_nsb_temp["mean_charge_err"], + marker="o", + label=rf"T={temp_fixed}$^\circ$C", + ) + + plt.legend() + plt.xlabel("FF voltage (V)") + plt.ylabel("Charge (p.e)") + fig_name = "Charge_ff_v_temp" + plot_path = os.path.join(self.output_dir, f"{fig_name}.png") + plt.savefig(plot_path) + + # Charge resolution as function of + # temperature for different FF_voltages and NSBs. + + plt.figure(figsize=(8, 6)) + + for i, v_fixed in enumerate(ff_list): + df_v = df_output[df_output["ff_v"] == v_fixed].sort_values("temperature") + for j, nsb in enumerate(sorted(df_v["NSB"].unique())): + df_v_nsb = df_v[df_v["NSB"] == nsb] + plt.plot( + df_v_nsb["temperature"], + df_v_nsb["mean_charge_resolution"], + marker=marker[j], + color=colors[i], + label=f"FF={v_fixed}V, NSB={nsb}", + ) + + plt.xlabel(r"Temperature ($^\circ$C)") + plt.ylabel("Charge Resolution") + # plt.legend(fontsize=8, ncol=2) + fig_name = "charge_resolution_T_nsb" + plot_path = os.path.join(self.output_dir, f"{fig_name}.png") + plt.savefig(plot_path) + + slope = [] + intercept = [] + slope_err = [] + intercept_err = [] + for i, v_fixed in enumerate(ff_list): + df_v = df_output[df_output["ff_v"] == v_fixed] + mean_res = [] + min_res = [] + max_res = [] + temp_list = [] + + for j, t_fixed in enumerate(sorted(df_v["temperature"].unique())): + df_v_t = df_v[df_v["temperature"] == t_fixed] + temp_list.append(t_fixed) + res = df_v_t["mean_charge_resolution"] + mean_res.append(np.mean(res)) + min_res.append(np.min(res)) + max_res.append(np.max(res)) + + temp_list = np.array(temp_list) + min_res = np.array(min_res) + max_res = np.array(max_res) + + plt.plot(temp_list, mean_res, label=f"{v_fixed} V", marker="o", ls="") + plt.fill_between(temp_list, min_res, max_res, alpha=0.6) + + model = Model(linear_fit_function) + params = model.make_params(a=0, b=np.mean(mean_res)) + charge_res_fit = model.fit( + mean_res, + params, + weights=1 / (max_res - min_res), + x=temp_list, + ) + slope.append(charge_res_fit.params["a"].value) + intercept.append(charge_res_fit.params["b"].value) + slope_err.append(charge_res_fit.params["a"].stderr) + intercept_err.append(charge_res_fit.params["b"].stderr) + + plt.xlabel(r"Temperature ($^\circ$C)") + plt.ylabel("Charge Resolution") + plt.ylim(0.04, 0.22) + plt.legend(fontsize=8, ncol=4) + fig_name = "ChargeResolution_T_nsb_paper" + plot_path = os.path.join(self.output_dir, f"{fig_name}.png") + plt.savefig(plot_path) + + fig, ax1 = plt.subplots() + + color = "tab:red" + + ax1.errorbar(ff_list, slope, yerr=slope_err, marker="o", label="slope") + ax1.set_xlabel("Voltage (V)") + ax1.set_ylabel("Slope") + ax2 = ax1.twinx() + ax2.errorbar( + ff_list, + intercept, + intercept_err, + marker="^", + color=color, + label="intercept", + ) + ax2.set_ylabel("Mean charge resolution", color=color) + mean_slope = np.mean(slope) + mean_slope_err = np.mean(slope_err) + ax1.text( + 0.05, + 0.95, + f"Mean slope: {mean_slope:.5f} ± {mean_slope_err:.5f}", + transform=ax1.transAxes, + verticalalignment="top", + ) + fig.tight_layout() + fig_name = "ChargeResolution_parameters" + plot_path = os.path.join(self.output_dir, f"{fig_name}.png") + plt.savefig(plot_path) + plt.close("all") + + def plot_linearity_temperature(self, df_module_filtered): + """Plots + 1. a, b coefficients with temperature + 2. HG/LG with temperature + """ + + results = [] + + for _, row in df_module_filtered.iterrows(): + t = row["temperature"] + runlist = row["runs"] + transmission = row["transmissions"] + + log.info(row, "====", t, runlist, transmission) + + fit_parameters, ratio, ratio_std = run_linearity( + runlist, + transmission, + temperature=t, + nevents=self.max_events, + output_dir=self.output_dir, + ) + + # unpack HG and LG + (a_HG, b_HG, a_err_HG, b_err_HG) = fit_parameters[0] + (a_LG, b_LG, a_err_LG, b_err_LG) = fit_parameters[1] + + results.append( + { + "temperature": t, + "a_HG": a_HG, + "b_HG": b_HG, + "a_err_HG": a_err_HG, + "b_err_HG": b_err_HG, + "a_LG": a_LG, + "b_LG": b_LG, + "a_err_LG": a_err_LG, + "b_err_LG": b_err_LG, + "ratio": ratio, + "ratio_std": ratio_std, + "transmission": transmission, + } + ) + + df_lin = pd.DataFrame(results) + df_lin = df_lin.sort_values("temperature") # sort values with temperature + + # PLOT 1 : Parameters vs temperature + fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True, figsize=(6, 6)) + + # Parameter a with temperature + ax1.errorbar( + df_lin["temperature"], + df_lin["a_HG"], + yerr=df_lin["a_err_HG"], + marker="o", + ls=" ", + label="$a_{HG}$", + ) + model = Model(linear_fit_function) + params = model.make_params(a=10, b=0) + a_HG_fit = model.fit( + df_lin["a_HG"], + params, + weights=1 / df_lin["a_err_HG"], + x=df_lin["temperature"], + ) + ax1.plot( + df_lin["temperature"], + linear_fit_function( + df_lin["temperature"], + a_HG_fit.params["a"].value, + a_HG_fit.params["b"].value, + ), + ls="--", + label=r" Fit line, slope: $(%2.4f\pm%2.4f)$ $^{\circ} C^{-1}$ " + % ( + a_HG_fit.params["a"].value, + a_HG_fit.params["a"].stderr, + ), + ) + ax1.errorbar( + df_lin["temperature"], + df_lin["a_LG"], + yerr=df_lin["a_err_LG"], + marker="s", + ls=" ", + label="$a_{LG}$", + ) + model = Model(linear_fit_function) + params = model.make_params(a=10, b=0) + a_LG_fit = model.fit( + df_lin["a_LG"], + params, + weights=1 / df_lin["a_err_LG"], + x=df_lin["temperature"], + ) + ax1.plot( + df_lin["temperature"], + linear_fit_function( + df_lin["temperature"], + a_LG_fit.params["a"].value, + a_LG_fit.params["b"].value, + ), + ls="--", + label=r" Fit line, slope: $(%2.4f\pm%2.4f)$ $^{\circ} C^{-1}$ " + % ( + a_LG_fit.params["a"].value, + a_LG_fit.params["a"].stderr, + ), + ) + + ax1.legend(fontsize=8) + ax1.set_xlim(-7, 28) + # Parameter b vs temperature + ax2.errorbar( + df_lin["temperature"], + df_lin["b_HG"], + yerr=df_lin["b_err_HG"], + marker="o", + ls=" ", + label="$b_{HG}$", + ) + model = Model(linear_fit_function) + params = model.make_params(a=10, b=0) + b_HG_fit = model.fit( + df_lin["b_HG"], + params, + weights=1 / df_lin["b_err_HG"], + x=df_lin["temperature"], + ) + ax2.plot( + df_lin["temperature"], + linear_fit_function( + df_lin["temperature"], + b_HG_fit.params["a"].value, + b_HG_fit.params["b"].value, + ), + ls="--", + label=r" Fit line, slope: $(%2.3f\pm%2.3f)$ $^{\circ} p.e. C^{-1}$ " + % ( + b_HG_fit.params["a"].value, + b_HG_fit.params["a"].stderr, + ), + ) + ax2.errorbar( + df_lin["temperature"], + df_lin["b_LG"], + yerr=df_lin["b_err_LG"], + marker="s", + ls=" ", + label="$b_{LG}$", + ) + + model = Model(linear_fit_function) + params = model.make_params(a=10, b=0) + b_LG_fit = model.fit( + df_lin["b_LG"], + params, + weights=1 / df_lin["b_err_LG"], + x=df_lin["temperature"], + ) + ax2.plot( + df_lin["temperature"], + linear_fit_function( + df_lin["temperature"], + b_LG_fit.params["a"].value, + b_LG_fit.params["b"].value, + ), + ls="--", + label=r" Fit line, slope: $(%2.3f\pm%2.3f)$ $^{\circ} p.e. C^{-1}$ " + % ( + b_LG_fit.params["a"].value, + b_LG_fit.params["a"].stderr, + ), + ) + + ax2.set_xlabel("Temperature ($deg C$)") + ax2.legend(fontsize=8) + ax2.set_xlim(-7, 28) + + fig_name = "Linearity_params_temp" + plot_path = os.path.join(self.output_dir, f"{fig_name}.png") + plt.savefig(plot_path) + + # PLOT2 : HG/LG as function of different transmissions + slope_hglg = [] + + df_lin = df_lin.explode(["transmission", "ratio", "ratio_std"]) + + for transmission in sorted(df_lin["transmission"].unique()): + df_lin_transmission = df_lin[ + df_lin["transmission"] == transmission + ].sort_values("temperature") + + plt.errorbar( + df_lin_transmission["temperature"], + df_lin_transmission["ratio"], + marker="s", + alpha=0.8, + label=f"Tr = {transmission}", + ) + + x = df_lin_transmission["temperature"].to_numpy(dtype=float) + y = df_lin_transmission["ratio"].to_numpy(dtype=float) + yerr = df_lin_transmission["ratio_std"].to_numpy(dtype=float) + + y1 = y - yerr + y2 = y + yerr + + plt.fill_between(x, y1, y2, alpha=0.3) + + model = Model(linear_fit_function) + params = model.make_params(a=10, b=0) + ratio_fit = model.fit( + df_lin_transmission["ratio"], + params, + weights=1 / df_lin_transmission["ratio_std"], + x=df_lin_transmission["temperature"], + ) + slope_hglg.append(ratio_fit.params["a"].value) + slope_hglg = np.array(slope_hglg) + plt.text( + 0.05, + 0.98, + r"Avg slope : $(%2.3f\pm%2.3f)$" + % (np.mean(slope_hglg), np.std(slope_hglg) / np.sqrt(len(slope_hglg))), + transform=plt.gca().transAxes, + ha="left", + va="top", + fontsize=6, + bbox=dict( + boxstyle="round", facecolor="white", edgecolor="black", alpha=0.9 + ), + ) + plt.xlabel(r"Temperature ($^\circ$C)") + plt.ylabel("HG/LG") + plt.legend(fontsize=5, ncol=3) + plt.xlim(-10, 30) + plt.ylim(-1, 20) + + fig_name = "HGLGRatio_Linearity_temp" + plot_path = os.path.join(self.output_dir, f"{fig_name}.png") + plt.savefig(plot_path) + + plt.close("all") + + def plot_deadtime_temperature(self, df_module_filtered): + """ + Plots + + 1. Deadtime as function of run temperatures + 2. Relative difference between collected and fitted trigger rates + 3. Deadtime as function of run temperatures and fit to get the slope + and intercept of the linear dependence of deadtime with temperature, + only for the runs selected for the fit and the paper + 4. Relative difference between collected and fitted trigger rates + vs temperature, with shaded area for the paper, + only for the runs selected for the fit and the paper + 5. Deadtime percentage as function of run temperatures and fit + to get the slope and intercept of the linear dependence + of deadtime percentage with temperature, + only for the runs selected for the fit and the paper + + """ + + def select_runs(runs, trigger_rates, intensities, temperatures): + choice_mask = {} + unique_temperatures, counts = np.unique(temperatures, return_counts=True) + multiple_temperatures = unique_temperatures[counts > 1] + for ii, temperature in enumerate(temperatures): + if temperature < -5: + choice_mask[runs[ii]] = False + elif temperature >= -5 and temperature not in multiple_temperatures: + choice_mask[runs[ii]] = True + else: + multiple_temp_indexes = np.where(temperatures == temperature)[0] + min_trigger_rate = np.min( + np.abs(np.array(trigger_rates)[multiple_temp_indexes] - 7000) + ) + best_indexes_tr = np.where( + (np.array(trigger_rates)[multiple_temp_indexes] - 7000) + == min_trigger_rate + )[0] + + min_intensity = np.min( + np.abs(np.array(intensities)[multiple_temp_indexes] - 30) + ) + best_indexes_int = np.where( + (np.array(intensities)[multiple_temp_indexes] - 30) + == min_intensity + )[0] + + best_index = np.intersect1d( + best_indexes_tr, best_indexes_int, assume_unique=True + ) + if len(best_index) == 0: + best_index = best_indexes_int + + choice_mask[ + np.array(runs)[multiple_temp_indexes[best_index]][0] + ] = True + choice_mask[ + np.setdiff1d( + np.array(runs)[multiple_temp_indexes], + np.array(runs)[multiple_temp_indexes[best_index]], + )[0] + ] = False + return choice_mask + + def plot_deadtime_per_temperature( + temperatures, + dt_or_dt_pc, # deadtime or deadtime % + dt_err_or_dt_pc_err, # error on deadtime or on deadtime % + y_label=r"Minimum Deadtime ($\mu s$)", + y_lims=(0.7, 0.92), + ): + plt.figure() + plt.errorbar( + x=temperatures, + y=dt_or_dt_pc, + yerr=dt_err_or_dt_pc_err, + ls="", + marker="o", + color="blue", + ) + + plt.xlabel("Temperature (°C)") + plt.ylabel(y_label) + plt.ylim(y_lims) + plt.xlim(temperatures.min() - 2, temperatures.max() + 2) + plt.grid() + + def plot_relative_trigger_difference( + temperatures, + collected_trigger_rates, + fitted_trigger_rates, + fitted_trigger_rates_err=None, + ylims=(-11, 11), + ): + plt.figure() + collected_trigger_rates = np.array(collected_trigger_rates) / 1000 + fitted_trigger_rates = np.array(fitted_trigger_rates) + relative = ( + (fitted_trigger_rates - collected_trigger_rates) + / collected_trigger_rates + * 100 + ) + + if fitted_trigger_rates_err is not None: + rate_err = np.array(fitted_trigger_rates_err) + rate_err = np.abs(relative) * ( + (rate_err) / np.abs(fitted_trigger_rates - collected_trigger_rates) + ) + else: + rate_err = None + + plt.errorbar( + x=temperatures, + y=relative, + yerr=rate_err, + ls="", + marker="o", + color="red", + ) + + plt.xlabel("Temperature (°C)") + plt.ylabel( + r"$\frac{\text{Rate from fit} - " + r"\text{Camera rate}}{\text{Camera rate}}$ (%)" + ) + plt.ylim(ylims) + plt.xlim(temperatures.min() - 2, temperatures.max() + 2) + plt.grid() + + return relative + + temperatures = np.sort(df_module_filtered["temperatures"].tolist()[0]) + argsorted_temps = np.argsort(df_module_filtered["temperatures"].tolist()[0]) + run_list = np.array(df_module_filtered["runs"].tolist()[0])[ + argsorted_temps + ].tolist() + source_ids = np.array(df_module_filtered["source_ids"].tolist()[0])[ + argsorted_temps + ] + intensities = np.array(df_module_filtered["intensity"].tolist()[0])[ + argsorted_temps + ].tolist() + + ( + collected_trigger_rates, + fitted_trigger_rates, + fitted_trigger_rates_err, + deadtime, + deadtime_err, + deadtime_pc, + error_deadtime_pc, + _, + ) = run_deadtime( + nevents=self.max_events, + runlist=run_list, + camera=self.camera, + ids=source_ids, + output_dir=self.output_dir, + temp_output=self.temp_output, + ) + + choices = select_runs( + run_list, collected_trigger_rates, intensities, temperatures + ) + + # Deadtime [mus] vs Temperature [°C] + plot_deadtime_per_temperature( + temperatures=temperatures, + dt_or_dt_pc=deadtime, + dt_err_or_dt_pc_err=deadtime_err, + ) + for ii, intensity in enumerate(intensities): + plt.text( + x=temperatures[ii] + 0.1, + y=deadtime[ii] + 0.02, + s=f"{intensity} mA", + ha="left", + va="bottom", + fontsize=9, + color="blue", + ) + plot_path = os.path.join(self.output_dir, "deadtime_vs_temperature.png") + plt.savefig(plot_path) + + # Relative difference between fitted + # and collected trigger rates vs Temperature [°C] + relative = plot_relative_trigger_difference( + temperatures=temperatures, + collected_trigger_rates=collected_trigger_rates, + fitted_trigger_rates=fitted_trigger_rates, + fitted_trigger_rates_err=fitted_trigger_rates_err, + ) + + for ii, intensity in enumerate(intensities): + plt.text( + x=temperatures[ii] + 0.1, + y=relative[ii] + 0.02, + s=f"{intensity} mA", + ha="left", + va="bottom", + fontsize=9, + color="red", + ) + + plt.axhline(10, ls="--", lw=2, color="grey") + plt.text( + x=temperatures.min() + 0.1, + y=10 + 0.3, + s="CTA requirement", + ha="left", + va="center", + fontsize=10, + color="grey", + ) + plt.axhline(-10, ls="--", lw=2, color="grey") + plt.text( + x=temperatures.min() + 0.1, + y=-10 - 0.3, + s="CTA requirement", + ha="left", + va="center", + fontsize=10, + color="grey", + ) + plot_path = os.path.join( + self.output_dir, "relative_trigger_difference_vs_temperature.png" + ) + plt.savefig(plot_path) + + # Deadtime [mus] vs Temperature [°C] + # only selected runs for the fit and the paper + selected_temperatures = np.array(temperatures)[list(choices.values())] + selected_deadtime = np.array(deadtime)[list(choices.values())] + selected_deadtime_err = np.array(deadtime_err)[list(choices.values())] + slope, intercept, _, _, std_err = stats.linregress( + selected_temperatures, selected_deadtime + ) + std_err_intercept = std_err * np.sqrt( + 1 + / len(selected_temperatures) + * np.mean(selected_temperatures**2) + / np.var(selected_temperatures) + ) + plot_deadtime_per_temperature( + temperatures=selected_temperatures, + dt_or_dt_pc=selected_deadtime, + dt_err_or_dt_pc_err=selected_deadtime_err, + ) + x_fit = np.linspace( + min(selected_temperatures) - 2, max(selected_temperatures) + 2, 100 + ) + y_fit = slope * x_fit + intercept + plt.plot(x_fit, y_fit, color="orange", lw=3, ls="--") + plt.text( + x=0.05, + y=0.95, + s=f"m: ({slope:.4f} ± {std_err:.4f}) μs/°C" + + f"\nc: ({intercept:.4f} ± {std_err_intercept:.4f}) μs", + transform=plt.gca().transAxes, + ha="left", + va="top", + fontsize=16, + bbox=dict( + boxstyle="round", facecolor="white", edgecolor="black", alpha=0.9 + ), + ) + plot_path = os.path.join(self.output_dir, "deadtime_vs_temperature_fit.png") + plt.savefig(plot_path) + + # Relative difference between fitted + # and collected trigger rates vs Temperature [°C], + # with shaded area for the paper + selected_collected_tr = np.array(collected_trigger_rates)[ + list(choices.values()) + ] + selected_fitted_tr = np.array(fitted_trigger_rates)[list(choices.values())] + selected_fitted_tr_err = np.array(fitted_trigger_rates_err)[ + list(choices.values()) + ] + relative = plot_relative_trigger_difference( + temperatures=selected_temperatures, + collected_trigger_rates=selected_collected_tr, + fitted_trigger_rates=selected_fitted_tr, + fitted_trigger_rates_err=selected_fitted_tr_err, + ylims=(-10.5, 10.5), + ) + plt.fill_between( + x=np.linspace( + np.min(selected_temperatures) - 2, + np.max(selected_temperatures) + 2, + num=100, + ), + y1=-10, + y2=10, + color="grey", + alpha=0.3, + ) + plt.axhline(0, ls="--", lw=2, color="grey") + plot_path = os.path.join( + self.output_dir, "relative_trigger_difference_vs_temperature_shaded.png" + ) + plt.savefig(plot_path) + + # Deadtime percentage per temperature + selected_deadtime_pc = np.array(deadtime_pc)[list(choices.values())] + selected_error_deadtime_pc = np.array(error_deadtime_pc)[list(choices.values())] + slope, intercept, _, _, std_err = stats.linregress( + selected_temperatures, selected_deadtime_pc + ) + std_err_intercept = std_err * np.sqrt( + 1 + / len(selected_temperatures) + * np.mean(selected_temperatures**2) + / np.var(selected_temperatures) + ) + plot_deadtime_per_temperature( + temperatures=selected_temperatures, + dt_or_dt_pc=selected_deadtime_pc, + dt_err_or_dt_pc_err=selected_error_deadtime_pc, + y_label=r"Deadtime %", + y_lims=(0, 1), + ) + x_fit = np.linspace( + min(selected_temperatures) - 2, max(selected_temperatures) + 2, 100 + ) + y_fit = slope * x_fit + intercept + plt.plot(x_fit, y_fit, color="orange", lw=3, ls="--") + plt.text( + x=0.05, + y=0.85, + s=f"m: ({slope:.4f} ± {std_err:.4f}) %/°C" + + f"\nc: ({intercept:.4f} ± {std_err_intercept:.4f}) %", + transform=plt.gca().transAxes, + ha="left", + va="top", + fontsize=16, + bbox=dict( + boxstyle="round", facecolor="white", edgecolor="black", alpha=0.9 + ), + ) + plot_path = os.path.join( + self.output_dir, "deadtime_percentage_vs_temperature.png" + ) + plt.savefig(plot_path) + + plt.show() + + +def main(): + parser = get_args() + args = parser.parse_args() + + run_file = args.run_file + + run_module = args.run_module + max_events = args.evts + camera = args.camera + output_dir = os.path.join( + os.path.abspath(args.output), + f"trr_camera_{camera}/{Path(__file__).stem}", + ) + os.makedirs(output_dir, exist_ok=True) + log.debug(f"Output directory: {output_dir}") + + temp_output = args.temp_output + + sys.argv = sys.argv[:1] + + pipeline = ObservationTemperaturePipeline( + run_module, run_file, camera, max_events, output_dir, temp_output + ) + + pipeline.run() + + +if __name__ == "__main__": + main() diff --git a/src/nectarchain/trr_verification_package/resources/observation_temperature_run_list.json b/src/nectarchain/trr_verification_package/resources/observation_temperature_run_list.json new file mode 100644 index 000000000..81f6a93e4 --- /dev/null +++ b/src/nectarchain/trr_verification_package/resources/observation_temperature_run_list.json @@ -0,0 +1,63 @@ +[ + { + "module" : "charge_resolution", + "temperature" : 10, + "NSB": 0, + "runs" : [7144, 7145, 7146, 7147, 7148, 7149, 7150, 7151, 7152], + "ff_v" : [8,9,10,11,12,13,14,15, 16 ] + }, + { + "module" : "charge_resolution", + "temperature" : 10, + "NSB": 125, + "runs" : [7153, 7154, 7155, 7156, 7157, 7158, 7159, 7160, 7161], + "ff_v" : [8,9,10,11,12,13,14,15, 16 ] + }, + { + "module" : "charge_resolution", + "temperature" : 10, + "NSB": 250, + "runs" : [7162, 7163, 7164, 7165, 7166, 7167, 7168, 7169, 7170], + "ff_v" : [8,9,10,11,12,13,14,15, 16 ] + }, + { + "module" : "charge_resolution", + "temperature" : 10, + "NSB": 500, + "runs" : [7171, 7172, 7173, 7174, 7175, 7176, 7177 , 7178, 7179], + "ff_v" : [8,9,10,11,12,13,14,15, 16 ] + }, + { + "module" : "charge_resolution", + "temperature" : 10, + "NSB": 1000, + "runs" : [7180, 7181, 7182, 7183, 7184, 7185, 7186, 7187 , 7188], + "ff_v" : [8,9,10,11,12,13,14,15, 16 ] + }, + { + "module" : "charge_resolution", + "temperature" : 5, + "NSB": 250, + "runs" : [6561, 6562, 6563, 6564, 6565, 6566, 6567, 6568, 6569], + "ff_v" : [8,9,10,11,12,13,14,15,16] + }, + { + "module" : "linearity", + "temperature": 5, + "runs" : [6629, 6630, 6631, 6632, 6633, 6634, 6635, 6636], + "transmissions" : [1, 0.182201, 0.0003674, 0.002935, 0.009844, 0.04523, 1.164112e-5, 1] + }, + { + "module" : "linearity", + "temperature": 10, + "runs" : [7010, 7011, 7012, 7013, 7014, 7015, 7016, 7017], + "transmissions" : [1, 0.182201, 0.0003674, 0.002935, 0.009844, 0.04523, 1.164112e-5, 1] + }, + { + "module" : "deadtime", + "runs" : [6644, 6664, 6787, 6805, 6880, 6952, 7018, 7141, 7212, 7213], + "temperatures" : [5, 0, -5, -10, -5, 14, 25, 20, 10, 10], + "intensity" : [35, 35, 30, 30, 30, 30, 30, 30, 30, 60], + "source_ids" : [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] + } +] \ No newline at end of file diff --git a/src/nectarchain/trr_verification_package/tools_components.py b/src/nectarchain/trr_verification_package/tools_components.py index 6a953487a..d1950adea 100644 --- a/src/nectarchain/trr_verification_package/tools_components.py +++ b/src/nectarchain/trr_verification_package/tools_components.py @@ -1,3 +1,4 @@ +import logging import os import pathlib @@ -16,6 +17,13 @@ from nectarchain.trr_verification_package.utils import get_bad_pixels_list from nectarchain.utils.constants import GAIN_DEFAULT +logging.basicConfig( + format="%(asctime)s %(name)s %(levelname)s %(message)s", + level=logging.INFO, + handlers=[logging.getLogger("__main__").handlers], +) +log = logging.getLogger(__name__) + # overriding so we can have maxevents in the path def _init_output_path(self): @@ -165,15 +173,14 @@ def finish(self, *args, **kwargs): Ratio of high-gain to low-gain mean charges. """ output = super().finish(return_output_component=True, *args, **kwargs) - # print("output ",output,args,kwargs,kwargs["gain_file"]) + log.debug("output ", output, args, kwargs, kwargs["gain_file"]) charge_container = output[0].containers[EventType.FLATFIELD] # Read gain try: - # print(kwargs["gain_file"]) gain_data = next(SPEfitContainer.from_hdf5(kwargs["gain_file"])) - # print(gain_data) + log.debug(gain_data) adc_to_pe = gain_data.high_gain[:, 0] except Exception: adc_to_pe = GAIN_DEFAULT @@ -187,7 +194,7 @@ def finish(self, *args, **kwargs): charge_lg = charge_container["charges_lg"] tom = charge_container["peak_hg"] npixels = charge_container["npixels"] - # print("charge hg ",charge_hg, len(charge_hg), len(charge_hg[0])) + charge_hg = np.array(charge_hg, dtype=float) charge_lg = np.array(charge_lg, dtype=float) @@ -203,9 +210,9 @@ def finish(self, *args, **kwargs): charge_lg[:, bad_pix] = np.nan charge_hg[:, bad_pix] = np.nan - print("adc_to_pe", adc_to_pe) + log.debug("adc_to_pe", adc_to_pe) - # print("bad pix list ", bad_pix) + log.debug("bad pix list ", bad_pix) charge_lg = np.array(charge_lg) charge_hg = np.array(charge_hg) @@ -219,28 +226,10 @@ def finish(self, *args, **kwargs): charge_pe_hg = charge_hg / (adc_to_pe) charge_pe_lg = charge_lg / (adc_to_pe) - n_events = len(charge_pe_hg) - print("n_events", n_events) - - """ - print( - charge_pe_lg, - len(charge_pe_lg), - len(charge_pe_hg), - np.nanmean(charge_pe_hg, axis=0), - np.nanmean(charge_pe_lg, axis=0), - ) - """ - # print("min ", np.min(np.concatenate(charge_pe_lg)), - # np.min(np.concatenate(charge_pe_hg))) - ratio_hglg = np.nanmean(np.nanmean(charge_pe_hg / charge_pe_lg, axis=0)) - print("ratio ", ratio_hglg) for channel, charge in enumerate([charge_pe_hg, charge_pe_lg]): - # print(channel,charge) pix_mean_charge = np.nanmean(charge, axis=0) # in pe - # print(pix_mean_charge) pix_std_charge = np.nanstd(charge, axis=0) @@ -251,9 +240,6 @@ def finish(self, *args, **kwargs): mean_resolution[channel] = np.nanmean(pix_resolution) - # print("pix ",npixels,channel,pix_resolution,min(pix_resolution), - # max(pix_resolution),np.where(pix_mean_charge<0),max(pix_std_charge)) - # mean_res_std[channel] = np.std(pix_resolution[pix_resolution>-500]) std_charge[channel] = np.nanmean(pix_std_charge) # for the charge resolution @@ -348,8 +334,8 @@ def finish(self, bootstrap=False, *args, **kwargs): tom_no_fit = np.array(tom_no_fit_all[good_evts]).reshape( len(good_evts), npixels ) - # print(tom_no_fit) - # print(tom_no_fit) + # log.info(tom_no_fit) + # log.info(tom_no_fit) # rms_mu = np.zeros(output[0].npixels) rms_no_fit = np.zeros(npixels) @@ -367,7 +353,7 @@ def finish(self, bootstrap=False, *args, **kwargs): boot_rms = [] sample = tom_pos[tom_pos < 32] - # print(sample) + # log.info(sample) bins = np.linspace(20, 32, 50) hist_values, bin_edges = np.histogram(sample, bins=bins) @@ -375,13 +361,13 @@ def finish(self, bootstrap=False, *args, **kwargs): bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2 if bootstrap: - # print(len(sample)) + # log.info(len(sample)) if len(sample) != 0: for _ in range(1000): bootsample = np.random.choice( sample, size=int(3 / 4 * (len(sample))), replace=True ) - # print(len(bootsample), bootsample.mean(), + # log.info(len(bootsample), bootsample.mean(), # bootsample.std()) boot_rms.append(bootsample.std()) # simulated mean of rms @@ -391,33 +377,33 @@ def finish(self, bootstrap=False, *args, **kwargs): else: bootrms_std = 0 bootrms_mean = 0 - # print(bootrms_std) + # log.info(bootrms_std) err[pix] = bootrms_std rms[pix] = bootrms_mean else: try: weighted_mean = np.average(bin_centers, weights=hist_values) - # print("Weighted Mean:", weighted_mean) + # log.info("Weighted Mean:", weighted_mean) # Compute weighted variance weighted_variance = np.average( (bin_centers - weighted_mean) ** 2, weights=hist_values ) - # print("Weighted Variance:", weighted_variance) + # log.info("Weighted Variance:", weighted_variance) # Compute RMS value (Standard deviation) rms[pix] = np.sqrt(weighted_variance) - # print("RMS:", rms[pix]) + # log.info("RMS:", rms[pix]) # Compute the total number of data points (sum of histogram # values, i.e. N) N = np.sum(hist_values) - # print("Total number of events (N):", N) + # log.info("Total number of events (N):", N) # Error on the standard deviation err[pix] = rms[pix] / np.sqrt(2 * N) - # print("Error on RMS:", err[pix]) + # log.info("Error on RMS:", err[pix]) except Exception: # no data rms[pix] = np.nan @@ -713,25 +699,25 @@ def finish(self, *args, **kwargs): # Compute bin centers bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2 weighted_mean = np.average(bin_centers, weights=hist_values) - # print("Weighted Mean:", weighted_mean) + # log.info("Weighted Mean:", weighted_mean) # Compute weighted variance weighted_variance = np.average( (bin_centers - weighted_mean) ** 2, weights=hist_values ) - # print("Weighted Variance:", weighted_variance) + # log.info("Weighted Variance:", weighted_variance) # Compute RMS value (Standard deviation) rms = np.sqrt(weighted_variance) - # print("RMS:", rms[pix]) + # log.info("RMS:", rms[pix]) # Compute the total number of data points (sum of histogram values, i.e. N) N = np.sum(hist_values) - # print("Total number of events (N):", N) + # log.info("Total number of events (N):", N) # Error on the standard deviation err = rms / np.sqrt(2 * N) - # print("Error on RMS:", err[pix]) + # log.info("Error on RMS:", err[pix]) # charge per run charge_per_run = np.mean(charge_per_event)