|
| 1 | +# Copyright (c) 2025, IRIS-HEP |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright notice, this |
| 8 | +# list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# * Neither the name of the copyright holder nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from |
| 16 | +# this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +# pydantic 2 API |
| 30 | + |
| 31 | +import pydantic |
| 32 | +from pathlib import Path |
| 33 | + |
| 34 | +from typing import Optional, Union |
| 35 | +from ..query_core import QueryStringGenerator |
| 36 | + |
| 37 | + |
| 38 | +@pydantic.dataclasses.dataclass |
| 39 | +class TopCPQuery(QueryStringGenerator): |
| 40 | + yaml_tag = "!TopCP" |
| 41 | + default_codegen = "topcp" |
| 42 | + |
| 43 | + reco: Optional[Union[Path, str]] = None |
| 44 | + """Path to the reco.yaml""" |
| 45 | + parton: Optional[Union[Path, str]] = None |
| 46 | + """Path to the parton.yaml""" |
| 47 | + particle: Optional[Union[Path, str]] = None |
| 48 | + """Path to the particle.yaml""" |
| 49 | + max_events: Optional[int] = -1 |
| 50 | + """Number of events to process""" |
| 51 | + no_systematics: Optional[bool] = True |
| 52 | + """Toggles off the computation of systematics""" |
| 53 | + no_filter: Optional[bool] = False |
| 54 | + """Save all events regardless of analysis filters (still saves the decision)""" |
| 55 | + |
| 56 | + @pydantic.model_validator(mode="after") |
| 57 | + def no_input_yaml(self): |
| 58 | + if self.reco is None and self.parton is None and self.particle is None: |
| 59 | + raise ValueError("No yaml provided!") |
| 60 | + return self |
| 61 | + |
| 62 | + def generate_selection_string(self): |
| 63 | + import json |
| 64 | + |
| 65 | + recoYaml = None |
| 66 | + if self.reco: |
| 67 | + with open(Path(self.reco), "r") as reco_file: |
| 68 | + recoYaml = reco_file.read() |
| 69 | + |
| 70 | + partonYaml = None |
| 71 | + if self.parton: |
| 72 | + with open(Path(self.parton), "r") as parton_file: |
| 73 | + partonYaml = parton_file.read() |
| 74 | + |
| 75 | + particleYaml = None |
| 76 | + if self.particle: |
| 77 | + with open(Path(self.particle), "r") as particle_file: |
| 78 | + particleYaml = particle_file.read() |
| 79 | + |
| 80 | + query = { |
| 81 | + "reco": recoYaml, |
| 82 | + "parton": partonYaml, |
| 83 | + "particle": particleYaml, |
| 84 | + "max_events": self.max_events, |
| 85 | + "no_systematics": self.no_systematics, |
| 86 | + "no_filter": self.no_filter, |
| 87 | + } |
| 88 | + return json.dumps(query) |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def from_yaml(cls, _, node): |
| 92 | + code = node.value |
| 93 | + import re |
| 94 | + |
| 95 | + # Use regex to split key-value pairs |
| 96 | + matches = re.findall(r'(\w+)="?(.*?)"?(?:,|$)', code) |
| 97 | + |
| 98 | + # Convert to dictionary |
| 99 | + result = {key: value for key, value in matches} |
| 100 | + |
| 101 | + q = cls(**result) |
| 102 | + return q |
0 commit comments