|
@@ -4,6 +4,7 @@
|
|
|
import argparse
|
|
|
from configparser import ConfigParser
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
+import os
|
|
|
import sqlite3
|
|
|
from typing import Union, Tuple, Optional
|
|
|
|
|
@@ -295,7 +296,7 @@ def calculate_point(comb_df: pd.DataFrame, runs_df: pd.DataFrame, compton_df: pd
|
|
|
'first_run': comb_df.run_first.min(),
|
|
|
'last_run': comb_df.run_last.max(),
|
|
|
'mean_energy': res_df.e_mean.mean(),
|
|
|
- 'mean_energy_stat_err': np.sqrt(1/np.sum(1/(res_df.e_std)**2)), #res_df.e_std.mean()/np.sqrt(len(res_df)),
|
|
|
+ 'mean_energy_stat_err': np.sqrt(1/np.sum(1/(res_df.e_std)**2)),
|
|
|
'mean_energy_sys_err': np.abs(comb_df.iloc[0].at['elabel'] - res_df.e_mean.mean()),
|
|
|
'mean_spread': res_df.spread_mean.mean(),
|
|
|
'mean_spread_stat_err':np.sqrt(1/np.sum(1/(res_df.spread_std)**2)),
|
|
@@ -383,20 +384,20 @@ def process_combined(combined_df: pd.DataFrame, runs_df: pd.DataFrame, compton_d
|
|
|
total_error = np.sqrt(res_dict["mean_energy_stat_err"]**2 + res_dict["mean_energy_sys_err"]**2)
|
|
|
half_timedelta = (plt_table.compton_stop - plt_table.compton_start)/2
|
|
|
time = plt_table.compton_start + half_timedelta
|
|
|
- dlt0 = timedelta(days=1)
|
|
|
+ dlt0, total_time = timedelta(days=1), plt_table.compton_stop.max() - plt_table.compton_stop.min()
|
|
|
+ timelim = [plt_table.compton_stop.min() - 0.05*total_time, plt_table.compton_stop.max() + 0.05*total_time]
|
|
|
|
|
|
fig, ax = plt.subplots(1, 1, dpi=120, tight_layout=True)
|
|
|
ax.errorbar(time, plt_table.e_mean, xerr=half_timedelta, yerr=plt_table.e_std, fmt='.')
|
|
|
ax.axhline(res_dict['mean_energy'], color='black', zorder=3, label='Mean')
|
|
|
- ax.fill_between([plt_table.compton_stop.min(), plt_table.compton_stop.max()],
|
|
|
+ ax.fill_between(timelim,
|
|
|
[res_dict['mean_energy'] - total_error]*2,
|
|
|
[res_dict['mean_energy'] + total_error]*2, color='green', zorder=1, alpha=0.4)
|
|
|
ax.tick_params(axis='x', labelrotation=45)
|
|
|
ax.xaxis.set_major_locator(locator)
|
|
|
ax.xaxis.set_major_formatter(formatter)
|
|
|
- ax.set(title=f'{res_dict["energy_point"]}, E = {res_dict["mean_energy"]:.3f} ± {res_dict["mean_energy_stat_err"]:.3f} ± {res_dict["mean_energy_sys_err"]:.3f} MeV', xlabel='Time, NSK', ylabel='Energy, [MeV]',
|
|
|
- xlim=[plt_table.compton_stop.min(), plt_table.compton_stop.max()])
|
|
|
- plt.savefig(f'{pics_folder}/{res_dict["first_run"]}_{res_dict["energy_point"]}.png')
|
|
|
+ ax.set(title=f'{res_dict["energy_point"]}, E = {res_dict["mean_energy"]:.3f} ± {res_dict["mean_energy_stat_err"]:.3f} ± {res_dict["mean_energy_sys_err"]:.3f} MeV', xlabel='Time, NSK', ylabel='Energy, [MeV]', xlim=timelim)
|
|
|
+ plt.savefig(f'{pics_folder}/{res_dict["first_run"]}_{res_dict["energy_point"]}.png', transparent=True)
|
|
|
plt.close()
|
|
|
|
|
|
return result_df
|
|
@@ -417,13 +418,25 @@ def final_table_to_clbrdb(df: pd.DataFrame, clbrdb: CalibrdbHandler, runs_df: pd
|
|
|
'mean_energy_stat_err', 'mean_energy_sys_err', 'mean_spread', 'mean_spread_stat_err']].values.tolist()
|
|
|
clbrdb.insert(df_clbrdb, 'Misc', 'RunHeader', 'Compton_run_avg', 'Default', comment = season)
|
|
|
clbrdb.commit()
|
|
|
+
|
|
|
+def save_csv(df: pd.DataFrame, filepath: str):
|
|
|
+ """Saves csv file. Updates current file in filepath if exists"""
|
|
|
+
|
|
|
+ if os.path.isfile(filepath):
|
|
|
+ df_current = pd.read_csv(filepath)
|
|
|
+ df_current = df_current.append(df, ignore_index=True)
|
|
|
+ df_current = df_current.drop_duplicates(subset=['energy_point', 'first_run'], keep='last')
|
|
|
+ df = df_current
|
|
|
+
|
|
|
+ df.to_csv(filepath, index=False, float_format='%g')
|
|
|
+ return
|
|
|
|
|
|
-# python scripts/compton_combiner.py -s NNBAR2021 -c database.ini --csv --clbrdb
|
|
|
+# python scripts/compton_combiner.py -s NNBAR2021 -c database.ini --csv_dir . --clbrdb
|
|
|
def main():
|
|
|
parser = argparse.ArgumentParser(description = 'Mean compton energy measurements from clbrdb')
|
|
|
parser.add_argument('-s', '--season', help = 'Name of the season')
|
|
|
parser.add_argument('-c', '--config', help = 'Config file containing information for access to databases')
|
|
|
- parser.add_argument('--csv', action = 'store_true', help = 'Save csv file with data or not')
|
|
|
+ parser.add_argument('--csv_dir', help = 'Save csv file with data in the folder or not if skip it')
|
|
|
parser.add_argument('--clbrdb', action = 'store_true', help = 'Update Compton_run_avg clbrdb or not')
|
|
|
parser.add_argument('--pics_folder', help = 'Path to the directory for saving the pictures')
|
|
|
parser.add_argument('--only_last', action = 'store_true', help = 'Compute values of the last (in Compton_run_avg clbrdb) and new points only')
|
|
@@ -461,8 +474,10 @@ def main():
|
|
|
|
|
|
cdf = process_combined(comb_df, runs_df, compton_df, args.pics_folder, rdb)
|
|
|
|
|
|
- if args.csv:
|
|
|
- cdf.to_csv(f'{args.season}.csv', index=False, float_format='%g')
|
|
|
+ if args.csv_dir is not None:
|
|
|
+ csv_path = os.path.join(args.csv_dir, f'{args.season}.csv')
|
|
|
+ save_csv(cdf, csv_path)
|
|
|
+ # cdf.to_csv(f'{args.season}.csv', index=False, float_format='%g')
|
|
|
|
|
|
if args.clbrdb:
|
|
|
final_table_to_clbrdb(cdf, clbrdb, runs_df, args.season)
|