Skip to content

Commit

Permalink
Merge pull request #48 from danielhundhausen/dev_restructure_objPerfo…
Browse files Browse the repository at this point in the history
…rmance

Move objectPerformance contents to new structure

authorised by @artlbv via Mattermost.
  • Loading branch information
danielhundhausen authored Jan 17, 2024
2 parents 5146601 + 90de61c commit bb6d9aa
Show file tree
Hide file tree
Showing 215 changed files with 104 additions and 119 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
**/*.png
**/.DS_Store
**/*.parquet
**/rates.py
objectPerformance/**/*.root
objectPerformance/outputs/**/*
**/*.root
rates/table/out/*
rates/table/cache/**/*
rates/table/lib/*
**/tmp/*
outputs
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

A standard venv with Python3.11 can be created on lxplus
via `python3.11 -m venv <name_of_venv>` and all necessary
dependencies installed via `pip install -r requirements.txt`.
dependencies installed via `pip install -r requirements.txt`:

You can then execute the scripts either with `python <path_to_script>.py`
or by modifying the [shebang](https://en.wikipedia.org/wiki/Shebang_%28Unix%29).
(the very first line of the executable `.py` files which starts
with `#!`) to point
to your newly set up Python installation. To find the path run
```bash
python3.11 -m venv <name_of_venv>
source <name_of_venv>/bin/activate
pip install .
```

source <name_of_venv>/bin/activate
which python
You can then execute the tools (e.g. for object performance) via

and replace the current path in the shebang with the output.
```python
python -m menu_tools.object_performance.plotter <path_to_config>
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions objectPerformance/README.md → docs/object-performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
The definition of each object to be tested is detailed in
this [TWiki page](https://twiki.cern.ch/twiki/bin/view/CMS/PhaseIIL1TriggerMenuTools).

A detailed description of each step, together with instructions on how to set up the configuration files for the cache and plotting steps, is given in [the Wiki pages](https://github.com/bonanomi/Phase2-L1MenuTools/wiki/Cache-objects-and-performance-plots).
A detailed description of each step, together with instructions on how to set up the configuration files for the cache and plotting steps, is given in [the Wiki pages](https://github.com/cms-l1-dpg/Phase2-L1MenuTools/wiki).

The following presents the minimal set of commands to be run to produce the standard set of validation plots (cf. [these slides](https://twiki.cern.ch/twiki/pub/CMS/PhaseIIL1TriggerMenuTools/Phase2Menu_validation123x-3.pdf)).

Expand Down Expand Up @@ -71,4 +71,4 @@

* `turnons`: plots of the matching efficiencies and L1 turn-on efficiency curves.

* `scalings`: plots of the scalings, i.e. position of the turnon point (often 95% location) as a function of different threshold cuts on L1 objects.
* `scalings`: plots of the scalings, i.e. position of the turnon point (often 95% location) as a function of different threshold cuts on L1 objects.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/afs/cern.ch/user/d/dhundhau/Phase2-L1MenuTools/pyenv-ak2.0/bin/python
import argparse
import glob
import os
Expand All @@ -9,9 +8,7 @@
import vector
import yaml

from utils import get_pdg_id
from utils import get_branches
from utils import timer
from menu_tools.utils import utils


vector.register_awkward()
Expand All @@ -38,7 +35,7 @@ def __init__(self, version, sample, obj, tree, branches, cfg_file,
self._dryrun = dryrun
# Get Branches
if not isinstance(branches, list):
self._branches = get_branches(self._ntuple_path, tree, obj)
self._branches = utils.get_branches(self._ntuple_path, tree, obj)
else:
self._branches = branches
self.cache_out_path = f"cache/{version}/"
Expand Down Expand Up @@ -169,7 +166,7 @@ def _filter_genpart_branches(self, all_arrays):
Filter genparticle branches by Id.
"""
partId = abs(all_arrays["Id"])
sel_id = (partId == get_pdg_id(self._part_type))
sel_id = (partId == utils.get_pdg_id(self._part_type))
for branch in all_arrays:
all_arrays[branch] = all_arrays[branch][sel_id]
all_arrays[branch] = ak.fill_none(all_arrays[branch], -999)
Expand Down Expand Up @@ -263,7 +260,7 @@ def _ak_array_in_chunk(self, arr, chunk_array, branches):
)
return arr

@timer("Loading objects files")
@utils.timer("Loading objects files")
def _concat_array_from_ntuples(self):
fnames = glob.glob(self._ntuple_path)[:]
bar = IncrementalBar("Progress", max=len(fnames))
Expand Down Expand Up @@ -343,8 +340,7 @@ def load(self):
parser = argparse.ArgumentParser()
parser.add_argument(
"cfg",
default="cfg_caching/V22.yaml",
help=""
help="Path to the config file in yaml format. Defaults in `configs/caching`."
)
parser.add_argument(
"--dry-run",
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/afs/cern.ch/user/d/dhundhau/Phase2-L1MenuTools/pyenv-ak2.0/bin/python
import argparse
import os

Expand All @@ -9,10 +8,10 @@
import yaml
import json

from turnon_collection import TurnOnCollection
from scaling_collection import ScalingCollection
from plotter import Plotter
import utils
from menu_tools.object_performance.turnon_collection import TurnOnCollection
from menu_tools.object_performance.scaling_collection import ScalingCollection
from menu_tools.object_performance.plotter import Plotter
from menu_tools.utils import utils


plt.style.use(hep.style.CMS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/afs/cern.ch/user/d/dhundhau/Phase2-L1MenuTools/pyenv-ak2.0/bin/python
import argparse
import os

Expand All @@ -8,7 +7,7 @@
import yaml
import json

from plotter import Plotter
from menu_tools.object_performance.plotter import Plotter

plt.style.use(hep.style.CMS)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/afs/cern.ch/user/d/dhundhau/Phase2-L1MenuTools/pyenv-ak2.0/bin/python
import argparse
import os

Expand All @@ -9,20 +8,22 @@
import yaml
import json

from turnon_collection import TurnOnCollection
from scaling_collection import ScalingCollection
import utils
from menu_tools.object_performance.turnon_collection import TurnOnCollection
from menu_tools.object_performance.scaling_collection import ScalingCollection
from menu_tools.utils import utils


plt.style.use(hep.style.CMS)


class Plotter():

outdir = "outputs/object_performance/"

def _make_output_dirs(self, version: str):
os.makedirs(f"outputs/{version}/turnons", exist_ok=True)
os.makedirs(f"outputs/{version}/distributions", exist_ok=True)
os.makedirs(f"outputs/{version}/scalings", exist_ok=True)
os.makedirs(f"{self.outdir}/{version}/turnons", exist_ok=True)
os.makedirs(f"{self.outdir}/{version}/distributions", exist_ok=True)
os.makedirs(f"{self.outdir}/{version}/scalings", exist_ok=True)

def _create_new_plot(self):
fig, ax = plt.subplots(figsize=(10, 10))
Expand Down Expand Up @@ -151,7 +152,7 @@ def _plot_efficiency_curve(self):

self._style_plot(fig, ax)
ax.set_ylim(0, 1.1)
plot_fname = f"outputs/{self.version}/turnons/{self.plot_name}_{self.turnon_collection.threshold}_{self.version}"
plot_fname = f"{self.outdir}/{self.version}/turnons/{self.plot_name}_{self.turnon_collection.threshold}_{self.version}"
for ext in [".png",".pdf"]:
plt.savefig(f"{plot_fname}{ext}")
self._save_json(f"{plot_fname}.json")
Expand Down Expand Up @@ -184,7 +185,7 @@ def _plot_iso_vs_efficiency_curve(self):
self._style_plot(fig, ax)


plot_fname = f"outputs/{self.version}/turnons/{self.plot_name}_{self.turnon_collection.threshold}_{self.version}"
plot_fname = f"{self.outdir}/{self.version}/turnons/{self.plot_name}_{self.turnon_collection.threshold}_{self.version}"
for ext in [".png",".pdf"]:
plt.savefig(f"{plot_fname}{ext}")
self._save_json(f"{plot_fname}.json")
Expand Down Expand Up @@ -222,7 +223,7 @@ def _plot_raw_counts(self):
color=test_hist[0].get_color(), **err_kwargs)

self._style_plot(fig, ax)
plot_fname = f"outputs/{self.version}/distributions/{self.plot_name}_{self.turnon_collection.threshold}_dist_{self.version}"
plot_fname = f"{self.outdir}/{self.version}/distributions/{self.plot_name}_{self.turnon_collection.threshold}_dist_{self.version}"
for ext in [".png",".pdf"]:
plt.savefig(f"{plot_fname}{ext}")
#self._save_json(f"{plot_fname}.json")
Expand Down Expand Up @@ -308,7 +309,7 @@ def _set_plot_ranges(self, ax):
ax.set_ylim(0, ymax)

def _save_json(self, file_name):
# file_name = = f"outputs/{self.version}/scalings/{self.plot_name}.json"
# file_name = = f"{self.outdir}/{self.version}/scalings/{self.plot_name}.json"
plot = {}

watermark = f"{self.version}_{self.plot_name}"
Expand Down Expand Up @@ -360,7 +361,7 @@ def plot(self):
self._set_plot_ranges(ax)
fig.tight_layout()

plot_fname = f"outputs/{self.version}/scalings/{self.plot_name}_{self.version}"
plot_fname = f"{self.outdir}/{self.version}/scalings/{self.plot_name}_{self.version}"
for ext in [".png",".pdf"]:
plt.savefig(f"{plot_fname}{ext}")
self._save_json(f"{plot_fname}.json")
Expand All @@ -372,12 +373,14 @@ def plot(self):
plt.close()


class ScalingCentral():
class ScalingCentral:

outdir = "outputs/object_performance/"

def __init__(self, cfg_plots_path):
with open(cfg_plots_path, 'r') as f:
self.cfg_plots = yaml.safe_load(f)
with open("./cfg_plots/scaling_thresholds.yaml", 'r') as f:
with open("./configs/scaling_thresholds.yaml", 'r') as f:
self.scaling_thresholds = yaml.safe_load(f)

def _get_scaling_thresholds(self, cfg_plot, test_obj):
Expand Down Expand Up @@ -411,10 +414,10 @@ def _write_scalings_to_file(self,
plot_name: str,
version: str,
params: dict):
with open(f"outputs/{version}/scalings/{plot_name}_scalings_{version}.txt", 'w+') as f:
with open(f"{self.outdir}/{version}/scalings/{plot_name}_scalings_{version}.txt", 'w+') as f:
f.write('')

with open(f"outputs/{version}/scalings/{plot_name}_scalings_{version}.txt", 'a') as f:
with open(f"{self.outdir}/{version}/scalings/{plot_name}_scalings_{version}.txt", 'a') as f:
for obj, obj_params in params.items():
a, b = obj_params
f.write(self._rate_config_function(obj, a, b) + "\n")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from scipy.optimize import curve_fit
import numpy as np

from plot_config import PlotConfig
import utils
from menu_tools.object_performance.plot_config import PlotConfig
from menu_tools.utils import utils


class ScalingCollection():
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import numpy as np
import vector

from plot_config import PlotConfig
from quality_obj import Quality, L1IsoCut
import utils
from menu_tools.object_performance.plot_config import PlotConfig
from menu_tools.object_performance.quality_obj import Quality, L1IsoCut
from menu_tools.utils import utils


vector.register_awkward()
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions objectPerformance/pyproject.toml

This file was deleted.

56 changes: 56 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# pyproject.toml
[build-system]
build-backend = 'poetry.core.masonry.api'
requires = [
"poetry-core~=1.0"
]

[tool.poetry]
name = "menu-tools"
version = "0.1.0"
description = "Tools to evaluate performance of L1 objects and triggers in the menu for Phase-II."
license = "MIT"
authors = [
"Matteo Bonanomi <[email protected]>",
"Daniel Hundhausen <[email protected]>",
"Artur Lobanov <[email protected]>",
]
readme = "README.md"
packages = [
{ include = "menu_tools" },
]

[tool.poetry.dependencies]
python = "^3.11.0"
awkward = "2.5.2"
fsspec = "2023.12.2"
pyyaml = "6.0.1"
matplotlib = "3.8.2"
mplhep = "0.3.31"
numpy = "^1.23.0"
pandas = "2.1.4"
progress = "1.6"
pyarrow = "14.0.2"
scipy = "1.10.1"
uproot = "5.0.4"
vector = "1.1.1.post1"

[tool.poetry.group.dev.dependencies]
black = "23.12.1"
mypy = "1.8.0"

[tool.poetry.group.test.dependencies]
pytest = "7.4.3"

[tool.pytest.ini_options]
filterwarnings = [
"error",
"ignore::UserWarning",
'ignore:A NumPy version',
]
pythonpath = [
"src"
]
testpaths = [
"tests",
]
Loading

0 comments on commit bb6d9aa

Please sign in to comment.