e15190.neutron_wall.cache.RunCache¶
- class e15190.neutron_wall.cache.RunCache(src_path_fmt, cache_path_fmt, max_workers=8)¶
Functions
__init__(src_path_fmt, cache_path_fmt[, ...])- param src_path_fmt
The format of the path to the ROOT file, e.g.
infer_tree_name(uproot_file)read(runs, branches[, sql_cmd, ...])Read in multiple runs (multiple files) from Daniele's ROOT files.
read_run(run, branches[, sql_cmd, ...])Read in one run (one file) from Daniele's ROOT files.
read_run_from_root(run, branches[, tree_name])Read in one run (one file) from Daniele's ROOT files.
read_run_from_sqlite(run[, sql_cmd])save_run_to_sqlite(run, df)Save the run as a dataframe in sqlite database.
- __init__(src_path_fmt, cache_path_fmt, max_workers=8)¶
- Parameters
src_path_fmt (str with 'run' specifier) – The format of the path to the ROOT file, e.g.
'/home/user/data/CalibratedData_{run:04d}.root'.cache_path_fmt (str with 'run' specifier) – The format of the path to the sqlite3 database file, e.g.
'/mnt/analysis/data/cache/run-{run:04d}.db'.max_workers (int, default 8) – The maximum number of thread pool executor workers to use for decompression and interpretation of ROOT files by Uproot.
- read_run_from_root(run, branches, tree_name=None) pandas.core.frame.DataFrame¶
Read in one run (one file) from Daniele’s ROOT files.
- Parameters
run (int) – The run number.
branches (dict of str:str or list of str) – If a dict, old_br_name -> new_br_name. The keys are the branch names at original path, and the values are the new names given to the dataframe. If a list, the branch names at path are used as the new names.
tree_name (str, default None) – The name of the tree to read. If None, the tree name is inferred from the ROOT file. Infer is guaranteed only if there exists exactly one TTree in the ROOT file.
- save_run_to_sqlite(run, df: pandas.core.frame.DataFrame) pathlib.Path¶
Save the run as a dataframe in sqlite database.
This is primarily used as cache for future use.
- Parameters
run (int) – The run number.
df (pandas.DataFrame) – The dataframe to save.
- Returns
path – The path to the sqlite database file.
- Return type
pathlib.Path
- read_run(run, branches, sql_cmd='', tree_name=None, from_cache=True, save_cache=True) pandas.core.frame.DataFrame¶
Read in one run (one file) from Daniele’s ROOT files.
By default, the run is read from the SQLite cache. If the cache does not exist, the run is read from the ROOT file and saved to the cache.
- Parameters
run (int) – The run number.
branches (dict of str:str or list of str) – If dict, old_br_name -> new_br_name. If list, branch names remain the same. If the function is reading from cache, this option is ignored; if the function is reading from ROOT, this option is required.
sql_cmd (str, default '') – The SQL command to be appended after ‘SELECT * FROM table_name’. A space is automatically added between the table name and the command.
tree_name (str, default None) – The name of the tree to read. If None, the tree name is inferred. Infer is guaranteed only if there exists exactly one TTree in the ROOT file.
from_cache (bool, default True) – If True, the function always attempts to read from the cache, unless the cache does not exist. If False, cache will be ignored, and the run will be read from the ROOT file.
save_cache (bool, default True) – If True, the run is saved to the cache after reading from the ROOT file. If False, the run will not be saved to the cache. This option is ignored if the function is already reading from the cache.
- Returns
df – The dataframe of the run.
- Return type
pandas.DataFrame
- read(runs, branches, sql_cmd='', drop_columns=None, tree_name=None, from_cache=True, save_cache=True, reset_index=False, insert_run_index=True, verbose=False) pandas.core.frame.DataFrame¶
Read in multiple runs (multiple files) from Daniele’s ROOT files.
It is user’s responsibility to ensure that the resultant dataframe fits into memory. Use
sql_cmdto select only the rows of interest. This filter is applied to the single-run dataframes before being concatenated into the final dataframe, keeping the memory usage minimal.The entries and columns that are being cached are not affected by
sql_cmdanddrop_columns. These options only take place when constructing the final dataframe and loading to memory.- Parameters
runs (list of int or int) – The run numbers.
branches (dict of str:str or list of str) – If dict, old_br_name -> new_br_name. If list, branch names remain the same. If the function is reading from cache, this option is ignored.
sql_cmd (str, default '') – The SQL command to be appended after ‘SELECT * FROM table_name’. This option is ignored when reading from ROOT files.
drop_columns (list of str, default None) – The columns to be dropped from the dataframe for each run. This is executed after
pandas_query. This has no effect on the columns that are being cached.tree_name (str, default None) – The name of the tree to read. If None, the tree name is inferred.
from_cache (bool, default True) – If True, the function attempts to read from cache. If False, cache will be ignored, and the runs will be read from ROOT files.
save_cache (bool, default True) – If True, the runs are saved to the cache after reading from ROOT files.
reset_index (bool, default False) – If False, the original index is kept. The original index comes from uproot, where there is a two-level index of (entry, subentry). Entry enumerates the triggered events and is identical to the entry number in the ROOT file. Subentry enumerates the event multiplicity of a single event/entry in a detector, e.g. microBall multiplicity. If True, new single-level index starting from 0 is used.
insert_run_index (bool, default True) – If True, ‘run’ is inserted as the first level index, above entry and subentry.
verbose (bool, default False) – If True, print the progress of the function.
- Returns
df – The concatenated dataframe of all runs.
- Return type
pandas.DataFrame