pueoAnalysisTools
Loading...
Searching...
No Matches
initialise.py
Go to the documentation of this file.
1r"""!
2@file initialise.py
3@brief `main()` shows how to use [load_pueoEvent_Dataset()](#initialise.load_pueoEvent_Dataset),
4 **start here!**.
5
6* The script will first load the library `libpueoEvent.so` in `$PUEO_UTIL_INSTALL_DIR` as soon as you
7 `import` it, even if you don't call the function
8 [load_pueoEvent_Dataset()](#initialise.load_pueoEvent_Dataset),
9 since the loading is done at the global scope.
10 @note no need to set `LD_LIBRARY_PATH`
11
12* As the script tries to load the library, it could error out if the library cannot be found,
13 or if `$PUEO_UTIL_INSTALL_DIR` is not set
14
15* The checks are done via `assert`s, which attempt to provide more meaningful error messages.
16 You can turn them off by running
17 ```
18 python3 -O initialise.py
19 ``` instead of
20 ```
21 python3 initialise.py
22 ```
23 which turns on optimzation.
24
25* P.S. `initialise.py` with an "s" because I like to be annoying.
26"""
27
28import os
29import ROOT
30from pathlib import Path
31
32puid = Path(os.environ["PUEO_UTIL_INSTALL_DIR"])
33assert puid.exists() and not puid == Path("."), "$PUEO_UTIL_INSTALL_DIR has not been set"
34
35# could be in lib or lib64, get the first match
36library_path = next(Path(puid).rglob("lib*/libpueoEvent.so"), None)
37assert library_path is not None, "failed to find libpueoEvent.so in your $PUEO_UTIL_INSTALL_DIR"
38
39ROOT.gSystem.Load(str(library_path))
40
41
42def load_pueoEvent_Dataset(pueo_mc_data: Path, run_number: int) -> ROOT.pueo.Dataset:
43 """!Loads the "dual output" dataset whose interface is defined in
44 [pueoEvent](https://github.com/PUEOCollaboration/pueoEvent) repository.
45
46 @param[in] pueo_mc_data The [directory](#effective_area.PUEO_MC_DATA)
47 that contains all the `run<run_number>/` subdirectories.
48
49 @param[in] run_number **Must** be discoverable inside @p pueo_mc_data,
50 else an assertion will cause the function to error out.
51 @retval dataset An instance of the class
52 [Dataset](https://github.com/PUEOCollaboration/pueoEvent/blob/main/src/pueo/Dataset.h)
53
54 * Some other functions (eg. #waveform_plots.load_waveforms) require you to
55 **call this function first** to retrive the Dataset instance.
56
57 @note
58 * The "dual output" parallels the "main output" of `simulatePueo`, namely the IceFinal files
59 `IceFinal_<run_number>_allTree.root`.
60 * The dual ouput files mimic the real PUEO data format.
61 * ie. they are the `SimulatedPueo*File<run_number>.root` that live alongside the IceFinal file
62 in the `run<run_number>/` directory.
63 * It might help to check out the
64 [examples](https://github.com/PUEOCollaboration/pueoEvent/tree/main/examples)
65 in the pueoEvent repository.
66
67 * Details:
68 * Technically, as intended by pueoEvent, `PUEO_MC_DATA` should be an environment variable.
69 * However, `pueo_mc_data` is a `Path` in this function.
70 * It is first used to verify whether
71 `run<run_number>/SimulatedPueoEventFile<run_number>.root` exists.
72 * Then it is used to define a temporary environment variable within the python process,
73 which will not persist beyond the program.
74 * In short, by using this script you are **not** actually using, nor affecting,
75 the environment variable `PUEO_MC_DATA` which you might have defined in your `.bashrc`.
76
77 """
78 # bare minimum error handling :)
79 assert isinstance(pueo_mc_data, Path), "parameter pueo_mc_data needs to be a pathlib.Path object"
80 _root_file = pueo_mc_data / f"run{run_number}" / f"SimulatedPueoEventFile{run_number}.root"
81
82 assert _root_file.exists(), f"{_root_file} does not exist"
83 os.environ["PUEO_MC_DATA"] = str(pueo_mc_data.resolve())
84 print(f"The environment variable $PUEO_MC_DATA has been temporarily set to {pueo_mc_data}")
85
86 dataset = ROOT.pueo.Dataset(run_number, ROOT.pueo.Dataset.PUEO_MC_DATA)
87 return dataset
88
89
90if __name__ == "__main__":
91 d = load_pueoEvent_Dataset(pueo_mc_data=Path("/tmp"), run_number=0)