Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IT container semi-automatic scripts #170

Closed
wants to merge 63 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
3000de3
Update coincident_events.py
Elisa-Visentin Oct 4, 2023
aaf230a
Merging script onlyMAGIC and onlyMC options
ranieremenezes Oct 5, 2023
99f02d8
stereo_events with "onlyReal" and "onlyMC" options
ranieremenezes Oct 5, 2023
f6fed35
cinfig file 4LST test
ranieremenezes Oct 6, 2023
4ff2608
Info on high level analysis on README
ranieremenezes Oct 6, 2023
63a4c09
Update setup.py
Elisa-Visentin Oct 24, 2023
40d8d82
Create placeholder.txt
Elisa-Visentin Oct 24, 2023
d8b8e35
Add files via upload
Elisa-Visentin Oct 24, 2023
e06d19d
Delete magicctapipe/scripts/lst1_magic/IT_container_data_nsb_bash_scr…
Elisa-Visentin Oct 24, 2023
803b07a
Update ci.yml
Elisa-Visentin Oct 24, 2023
ea9e565
Update ci.yml
Elisa-Visentin Oct 24, 2023
8db42bc
Update environment.yml
Elisa-Visentin Oct 24, 2023
86e4c64
Merge branch 'master' into Torino_IT_Container
Elisa-Visentin Oct 25, 2023
02f0792
Merge branch 'Torino' into Torino_IT_Container
Elisa-Visentin Oct 25, 2023
c04afe1
minor fixes
Elisa-Visentin Oct 25, 2023
28c2733
Bug + minor fixes
Elisa-Visentin Oct 25, 2023
b18fac9
config bug
Elisa-Visentin Oct 25, 2023
375e87b
variable name
Elisa-Visentin Oct 25, 2023
887249f
minor fixes
Elisa-Visentin Oct 26, 2023
0e2edad
bug
Elisa-Visentin Oct 30, 2023
5a8e773
median
Elisa-Visentin Oct 30, 2023
bc574b4
Adding condition to split proton sample
ranieremenezes Oct 30, 2023
7ea644b
minor fixes
Elisa-Visentin Oct 30, 2023
57ca47f
Merge branch 'Torino_IT_Container' of https://github.com/cta-observat…
Elisa-Visentin Oct 30, 2023
30b6edf
Minor fixes + nsb
Elisa-Visentin Oct 31, 2023
5d888d6
writelines
Elisa-Visentin Oct 31, 2023
1a24e9c
Documentation
Elisa-Visentin Oct 31, 2023
b1e8ebf
Update setting_up_config_and_dir.py
ranieremenezes Nov 3, 2023
34f0f0a
Update LSTnsb_MC.py
Elisa-Visentin Nov 3, 2023
6238270
Update setting_up_config_and_dir.py
Elisa-Visentin Nov 3, 2023
6a4d4d0
Update nsb_level_MC.py
ranieremenezes Nov 3, 2023
57c3219
Update LSTnsb.py
Elisa-Visentin Nov 3, 2023
3ff5e98
Update LSTnsb_MC.py
ranieremenezes Nov 3, 2023
6e7dee2
Updated nsb_avg function
ranieremenezes Nov 3, 2023
a744f21
Updated documentation
ranieremenezes Nov 3, 2023
f0468b2
bug + black
Elisa-Visentin Nov 3, 2023
d48ae98
Merge branch 'master' into Torino_IT_Container
Elisa-Visentin Nov 3, 2023
fcd4c33
Bugs (merge)
Elisa-Visentin Nov 3, 2023
1dcc70f
merge bug
Elisa-Visentin Nov 3, 2023
cd1aca2
Update environment.yml
Elisa-Visentin Nov 3, 2023
a8c23e6
Update README.md
Elisa-Visentin Nov 3, 2023
7f0652e
Delete magicctapipe/utils/utils.py
Elisa-Visentin Nov 3, 2023
c940806
Update ci.yml
Elisa-Visentin Nov 3, 2023
99b6ac9
Update ci.yml
Elisa-Visentin Nov 3, 2023
eeaf140
Merge branch 'master' into Torino_IT_Container
Elisa-Visentin Nov 3, 2023
75da719
Minor fixes + doc
Elisa-Visentin Nov 3, 2023
4f49ade
Documentation
Elisa-Visentin Nov 3, 2023
1ee386b
Doc fixes
Elisa-Visentin Nov 3, 2023
a84ba4f
pre-commit
Elisa-Visentin Nov 3, 2023
4851460
Minor Doc fixes
Elisa-Visentin Nov 4, 2023
a841b70
Minor fixes (logging, varables' names)
Elisa-Visentin Nov 4, 2023
7054e89
Merge branch 'master' into Torino_IT_Container
Elisa-Visentin Nov 5, 2023
3de134b
Pass run and date as separate args. Precommit doc
Elisa-Visentin Nov 5, 2023
59c41fc
Doc + minor fixes
Elisa-Visentin Nov 5, 2023
ca638db
Minor fixes (typo, warning, LST version, subrun n)
Elisa-Visentin Nov 6, 2023
5da030f
Minor fixes
Elisa-Visentin Nov 6, 2023
582051f
Merge the analyses
Elisa-Visentin Nov 13, 2023
b69e000
Bug (new pandas version?)
Elisa-Visentin Nov 14, 2023
80fb837
Bug (mod)
Elisa-Visentin Nov 17, 2023
ba59177
error fixes
Elisa-Visentin Nov 27, 2023
2d51b50
Update setting_up_config_and_dir.py
Elisa-Visentin Mar 12, 2024
5e46cce
Bug fix (printed string)
Elisa-Visentin Mar 14, 2024
27e02a6
minor fix
Elisa-Visentin Mar 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/user-guide/IT_analysis_NSB.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.. _IT_data_NSB:

IT Cluster analysis: NSB_matching
=================================

TODO
-----

1. Database (joint observations): choose format and share the needed file

Quick start tutorial
--------------------

Update the config_h5.yaml file with the time range, target name and bad runs.

``list_from_h5.py`` to create the lists with the runs

``nsb_level.py`` If there is no file called "config_general" or if you want to set another name to the config file, you can use the option "-c PG1553_config_general.yaml". This is actually valid for all scripts in the NSB branch. This script computes the NSB level for each LST run found by the first script. For a single LST run, it takes around 50 min to compute the NSB level, so we launch the jobs in parallel. For each job, it creates one txt file with information about the NSB.

``collect_nsb.py`` This script stacks the information from the txt files created above separated by NSB level.

``nsb_setting_up_config_and_dir.py`` Creates the directories for DL1 MAGIC data separated by observation period and processes MAGIC data up to DL1.

To merge the subruns into runs, the M1 and M2 runs, and then runs into nights, we do:

``nsb_merge_subruns.py``

``nsb_merge_M1_M2_runs.py``

``nsb_merge_M1_M2_night.py``

``nsb_coincident_events.py`` Find the MAGIC-LST coincident events and organize them by NSB level.

``nsb_stereo_events.py`` Computes the stereo parameters for the coincident runs.





228 changes: 228 additions & 0 deletions docs/user-guide/IT_analysis_data_MC.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
.. _IT_data_MC:

IT Cluster analysis: data + MC reduction tutorial
=================================================

1) The very first step to reduce MAGIC-LST data in IT Container is to have remote access/credentials to this cluster, so provide one. Once you have it, the connection steps are the following:

| Authorized institute server (Client) → ssh connection to CTALaPalma → ssh connection to cp01/02

2) Once connected to the IT Container, install MAGIC-CTA-PIPE (e.g. in your home directory in the IT Container) following the tutorial here: https://github.com/cta-observatory/magic-cta-pipe

3) Do not forget to open the magic-lst environment with the command `conda activate magic-lst` before starting the analysis

Preparations and DL0 to DL1
---------------------------

In this step, we will convert the MAGIC and Monte Carlo (MC) Data Level (DL) 0 to DL1 (our goal is to reach DL3).

Now copy all the python scripts available here to your preferred directory (e.g. /fefs/aswg/workspace/yourname/yourprojectname) in the IT Container, as well as the files `config_general.yaml`, `MAGIC_runs.txt` and `LST_runs.txt`.

The file ``config_general.yaml`` must contain the telescope IDs and the directories with the MC data, as shown below:

mc_tel_ids:
| LST-1: 1
| LST-2: 0
| LST-3: 0
| LST-4: 0
| MAGIC-I: 2
| MAGIC-II: 3

directories:
| workspace_dir : "/fefs/aswg/workspace/yourname/yourprojectname/"
| target_name : "CrabTeste"
| MC_gammas : "/fefs/aswg/data/mc/DL0/LSTProd2/TestDataset/sim_telarray"
| MC_electrons : "/fefs/aswg/data/mc/DL0/LSTProd2/TestDataset/Electrons/sim_telarray/"
| MC_helium : "/fefs/aswg/data/mc/DL0/LSTProd2/TestDataset/Helium/sim_telarray/"
| MC_protons : "/fefs/aswg/data/mc/DL0/LSTProd2/TrainingDataset/Protons/dec_2276/sim_telarray"
| MC_gammadiff : "/fefs/aswg/data/mc/DL0/LSTProd2/TrainingDataset/GammaDiffuse/dec_2276/sim_telarray/"

general:
| target_RA_deg : 83.633083 #RA in degrees
| target_Dec_deg : 22.0145 #Dec in degrees
| SimTel_version : "v1.4"
| LST_version : "v0.9"
| focal_length : "effective" #effective #nominal
| MAGIC_runs : "MAGIC_runs.txt" #If there is no MAGIC data, please fill this file with "0, 0"
| LST_runs : "LST_runs.txt"
| proton_train_fraction : 0.8 # 0.8 means that 80% of the DL1 protons will be used for training the Random Forest
| env_name : magic-lst



The file ``MAGIC_runs.txt`` looks like that:

| 2020_11_19,5093174
| 2020_11_19,5093175
| 2020_12_08,5093491
| 2020_12_08,5093492




The columns here represent the night and run in which you want to select data. Please do not add blank spaces in the rows, as these names will be used to i) find the MAGIC data in the IT Container and ii) create the subdirectories in your working directory. If there is no MAGIC data, please fill this file with "0,0". Similarly, the `LST_runs.txt` file looks like this:

| 2020_11_18,2923
| 2020_11_18,2924
| 2020_12_07,3093


Note that the LST nights appear as being one day before MAGIC's!!! This is because LST saves the date at the beginning of the night, while MAGIC saves it at the end. If there is no LST data, please fill this file with "0,0". These files are the only ones we need to modify in order to convert DL0 into DL1 data.

In this analysis, we use a wobble of 0.4°.

Night sky background estimation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Before processing the Monte Carlo simulations, we need to estimate the NSB level of our data. We do it by calling the following script:

.. code-block:: console

$ python nsb_level_MC.py -c config.yaml

Process name: nsb
To check the jobs submitted to the cluster, type: squeue -n nsb

If the file config.yaml is not provided, this script will automatically search for the file "config_general.yaml" in the same directory where you call the script.
This script will save a series of files named TARGETNAME_LST_nsb_RUNNUMBER.txt with information about the NSB level of each run, and usually takes 50 min to run.

DL0 to DL1
^^^^^^^^^^

To convert the MAGIC and SimTelArray MCs data into DL1 format, you first do the following:

.. code-block:: console

$ python setting_up_config_and_dir.py


***** Linking MC paths - this may take a few minutes ******
*** Reducing DL0 to DL1 data - this can take many hours ***
Process name: yourprojectnameCrabTeste
To check the jobs submitted to the cluster, type: squeue -n yourprojectnameCrabTeste

Note that this script can be run as

.. code-block:: console

$ python setting_up_config_and_dir.py --analysis-type onlyMAGIC

or

.. code-block:: console

$ python setting_up_config_and_dir.py --analysis-type onlyMC

if you want to convert only MAGIC or only MC DL0 files to DL1, respectively.


The script ``setting_up_config_and_dir.py`` does a series of things:
- Evaluates the average NSB level over all runs based on the files generated by the script nsb_level_MC.py.
- Creates a directory with your source name within the directory ``yourprojectname`` and several subdirectories inside it that are necessary for the rest of the data reduction.
- Generates a configuration file called config_step1.yaml with telescope ID information, adopted imaging/cleaning cuts, average NSB level. It then puts this configuration file in the directory created in the previous step.
- Links the MAGIC and MC data addresses to their respective subdirectories defined in the previous steps.
- Runs the scripts ``lst1_magic_mc_dl0_to_dl1.py`` and ``magic_calib_to_dl1.py`` for each one of the linked data files.

In the file ``config_general.yaml``, the sequence of telescopes is always LST1, LST2, LST3, LST4, MAGIC-I, MAGIC-II. So in this tutorial, we have

| LST-1 ID = 1
| LST-2 ID = 0
| LST-3 ID = 0
| LST-4 ID = 0
| MAGIC-I ID = 2
| MAGIC-II ID = 3

If the telescope ID is set to 0, this means that the telescope is not used in the analysis.

You can check if this process is done by typing

.. code-block:: console

$ squeue -n yourprojectnameCrabTeste

or

.. code-block:: console

$ squeue -u your_user_name

in the terminal. Once it is done, all of the subdirectories in ``/fefs/aswg/workspace/yourname/yourprojectname/CrabTeste/DL1/`` will be filled with files of the type `dl1_[...]_LST1_MAGIC1_MAGIC2_runXXXXXX.h5` for the MCs and `dl1_MX.RunXXXXXX.0XX.h5` for the MAGIC runs. The next step of the conversion of DL0 to DL1 is to split the DL1 MC proton sample into "train" and "test" datasets (these will be used later in the Random Forest event classification and to do some diagnostic plots) and to merge all the MAGIC data files such that in the end, we have only one datafile per night. To do so, we run the following script:

.. code-block:: console

$ python merging_runs_and_splitting_training_samples.py



***** Splitting protons into 'train' and 'test' datasets...
***** Generating merge bashscripts...
***** Running merge_hdf_files.py in the MAGIC data files...
Process name: merging_CrabTeste
To check the jobs submitted to the cluster, type: squeue -n merging_CrabTeste


This script will slice the proton MC sample according to the entry "proton_train_fraction" in the "config_general.yaml" file, and then it will merge the MAGIC data files in the following order:
- MAGIC subruns are merged into single runs.
- MAGIC I and II runs are merged (only if both telescopes are used, of course).
- All runs in specific nights are merged, such that in the end we have only one datafile per night.
- Proton MC training data is merged.
- Proton MC testing data is merged.
- Diffuse MC gammas are merged.
- MC gammas are merged.

Coincident events and stereo parameters on DL1
----------------------------------------------

To find coincident events between MAGIC and LST, starting from DL1 data, we run the following script:

.. code-block:: console

$ python coincident_events.py

This script creates the file config_coincidence.yaml containing the telescope IDs and the following parameters:

mc_tel_ids:
| LST-1: 1
| LST-2: 0
| LST-3: 0
| LST-4: 0
| MAGIC-I: 2
| MAGIC-II: 3

event_coincidence:
| timestamp_type_lst: "dragon_time" # select "dragon_time", "tib_time" or "ucts_time"
| window_half_width: "300 ns"
| pre_offset_search: true
| n_pre_offset_search_events: 100

time_offset:
| start: "-10 us"
| stop: "0 us"



It then links the real LST data files to the output directory [...]DL1/Observations/Coincident, and runs the script lst1_magic_event_coincidence.py in all of them.

Once it is done, we add stereo parameters to the MAGIC+LST coincident DL1 data by running:

.. code-block:: console

$ python stereo_events.py

This script creates the file config_stereo.yaml with the follwoing parameters:

mc_tel_ids:
| LST-1: 1
| LST-2: 0
| LST-3: 0
| LST-4: 0
| MAGIC-I: 2
| MAGIC-II: 3

stereo_reco:
| quality_cuts: "(intensity > 50) & (width > 0)"
| theta_uplim: "6 arcmin"


It then creates the output directories for the DL1 with stereo parameters [...]DL1/Observations/Coincident_stereo/SEVERALNIGHTS and [...]/DL1/MC/GAMMAorPROTON/Merged/StereoMerged, and then runs the script lst1_magic_stereo_reco.py in all of the coincident DL1 files. The stereo DL1 files for MC and real data are then saved in these directories.

1 change: 1 addition & 0 deletions docs/user-guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ User Guide

getting-started
magic-lst-scripts

3 changes: 0 additions & 3 deletions magicctapipe/image/tests/test_calib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def tel_id_MAGIC():


def test_calibrate_LST(dl0_gamma, config_calib, tel_id_LST):

assigned_tel_ids = [1, 2, 3]
for input_file in dl0_gamma:
event_source = EventSource(
Expand Down Expand Up @@ -67,7 +66,6 @@ def test_calibrate_LST(dl0_gamma, config_calib, tel_id_LST):


def test_calibrate_MAGIC(dl0_gamma, config_calib, tel_id_MAGIC):

assigned_tel_ids = [1, 2, 3]
for input_file in dl0_gamma:
event_source = EventSource(
Expand All @@ -89,7 +87,6 @@ def test_calibrate_MAGIC(dl0_gamma, config_calib, tel_id_MAGIC):
config_extractor_magic = {extractor_type_magic: config_magic["image_extractor"]}
magic_clean = {}
for k in [1, 2]:

magic_clean[k] = MAGICClean(camera_geoms[k], config_magic["magic_clean"])
calibrator_magic = CameraCalibrator(
image_extractor_type=extractor_type_magic,
Expand Down
2 changes: 0 additions & 2 deletions magicctapipe/io/tests/test_io_monly.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ def test_load_mc_dl2_data_file_opt(p_dl2_monly, gamma_dl2_monly):
"""
dl2_mc = [p for p in gamma_dl2_monly.glob("*")] + [p for p in p_dl2_monly.glob("*")]
for file in dl2_mc:

data_m, _, _ = load_mc_dl2_data_file(
str(file), "width>0", "magic_only", "simple"
)
Expand Down Expand Up @@ -434,7 +433,6 @@ def test_load_dl2_data_file_opt(real_dl2_monly):
Check on event_type
"""
for file in real_dl2_monly.glob("*"):

data_m, _, _ = load_dl2_data_file(str(file), "width>0", "magic_only", "simple")

assert np.all(data_m["combo_type"] == 0)
Expand Down
18 changes: 18 additions & 0 deletions magicctapipe/resources/test_config_general_4LST.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,25 @@ mc_tel_ids:
LST-4: 5
MAGIC-I: 0
MAGIC-II: 0

directories:
workspace_dir : "/fefs/aswg/workspace/raniere/"
target_name : "CrabTeste"
MC_gammas : "/fefs/aswg/data/mc/DL0/LSTProd2/TestDataset/sim_telarray"
MC_electrons : "/fefs/aswg/data/mc/DL0/LSTProd2/TestDataset/Electrons/sim_telarray/"
MC_helium : "/fefs/aswg/data/mc/DL0/LSTProd2/TestDataset/Helium/sim_telarray/"
MC_protons : "/fefs/aswg/data/mc/DL0/LSTProd2/TrainingDataset/Protons/dec_2276/sim_telarray"
MC_gammadiff : "/fefs/aswg/data/mc/DL0/LSTProd2/TrainingDataset/GammaDiffuse/dec_2276/sim_telarray/"

general:
target_RA_deg : 83.633083 #RA in degrees
target_Dec_deg : 22.0145 #Dec in degrees
SimTel_version : "v1.4"
LST_version : "v0.9"
LST_tailcut : "tailcut84"
focal_length : "effective" #effective #nominal
MAGIC_runs : "MAGIC_runs.txt" #If there is no MAGIC data, please fill this file with "0, 0"
LST_runs : "LST_runs.txt"
proton_train_fraction : 0.8 # 0.8 means that 80% of the DL1 protons will be used for training the Random Forest
env_name : magic-lst

Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ def event_coincidence(input_file_lst, input_dir_magic, output_dir, config):
tel_ids = np.unique(event_data_magic.index.get_level_values("tel_id"))

for tel_id in tel_ids:

tel_name = TEL_NAMES[tel_id]
df_magic = event_data_magic.query(f"tel_id == {tel_id}").copy()

Expand Down
4 changes: 1 addition & 3 deletions magicctapipe/scripts/lst1_magic/lst1_magic_stereo_reco.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def stereo_reconstruction(input_file, output_dir, config, magic_only_analysis=Fa
Two_arrays_are_used = Number_of_LSTs_in_use * Number_of_MAGICs_in_use > 0

if (not is_simulation) and (Two_arrays_are_used):

logger.info(
"\nChecking the angular distances of "
"the LST and MAGIC pointing directions..."
Expand Down Expand Up @@ -238,7 +237,6 @@ def stereo_reconstruction(input_file, output_dir, config, magic_only_analysis=Fa
multi_indices = event_data.groupby(["obs_id", "event_id"]).size().index

for i_evt, (obs_id, event_id) in enumerate(multi_indices):

if i_evt % 100 == 0:
logger.info(f"{i_evt} events")

Expand All @@ -256,7 +254,6 @@ def stereo_reconstruction(input_file, output_dir, config, magic_only_analysis=Fa
tel_ids = df_evt.index.get_level_values("tel_id")

for tel_id in tel_ids:

df_tel = df_evt.loc[tel_id]

# Assign the telescope information
Expand Down Expand Up @@ -361,6 +358,7 @@ def stereo_reconstruction(input_file, output_dir, config, magic_only_analysis=Fa

def main():
"""Main function."""

start_time = time.time()

parser = argparse.ArgumentParser()
Expand Down
Loading