ATLAS Offline Software
Loading...
Searching...
No Matches
periods.py
Go to the documentation of this file.
1#! /usr/bin/env python
2
3# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4
5
6from os import listdir, makedirs
7from os.path import basename, exists, join as pjoin
8
9from .sugar import define_iov_type, IOVSet
10
11import logging
12log = logging.getLogger("DQUtils.periods")
13
14DATA_PERIODS_PATH = "/afs/cern.ch/atlas/www/GROUPS/DATAPREPARATION/DataPeriods/"
15
16@define_iov_type
17def PERIODIOV_VAL(project_tag, period):
18 pass
19
21 """
22 Warning: Being deprecated
23 """
24 for period_file in listdir(DATA_PERIODS_PATH):
25 if not period_file.endswith(".runs.list"):
26 continue
27
28def get_period_iovset_from_file(project_tag, period):
29 runs_file = "%s.%s.runs.list" % (project_tag, period)
30 with open(pjoin(DATA_PERIODS_PATH, runs_file)) as fd:
31 period_data = fd.read()
32
33 try:
34 period_data = map(int, period_data.strip().split("\n"))
35 except ValueError:
36 raise RuntimeError("Invalid period data")
37
38 return IOVSet.from_runs(period_data)
39
41 #from pyAMI.pyAMI import AMI
42 #import pyAMI
43
44 # This import needed to ensure correct python environment since pyAMI 4.0.3
45 # Yuriy Ilchenko: commented out (not sure needed since ATHENA 17.X)
46 #import setup_pyAMI
47 import pyAMI
48 from pyAMI.client import Client
49 amiclient = Client('atlas')
50 amiclient.config.read()
51
52 args = ['GetRunsForDataPeriod',
53 '-projectName=%s' % ptag,
54 '-period=%s' % period]
55 try:
56 runs = amiclient.execute(args, format='dict_object').get_rows()
57 except pyAMI.exceptions.AMI_Error:
58 raise
59
60 #return IOVSet.from_runs(int(x['runNumber']) for x in runs.values())
61 return sorted(int(x['runNumber']) for x in runs.values())
62
64 """
65 Returns a dictionary {project_tag: {period: [runs]}}
66
67 retrieved from the coma database.
68 """
69
70 from sqlalchemy import (select, Table, Column,
71 String, Integer, ForeignKey)
72
73 from re import compile
74
75 LETTER_PART = compile(r"^(\D+)")
76
77 from DQUtils.oracle import make_oracle_connection
78
79 engine, metadata = make_oracle_connection("oracle://atlas_dd/atlasdd")
80
81 schema = "ATLAS_TAGS_METADATA"
82
83 coma_runs = Table("coma_runs", metadata,
84 Column("run_index", Integer),
85 Column("run_number", Integer),
86 schema=schema
87 )
88
89 coma_period_definitions = Table("coma_period_defs", metadata,
90 Column("p_index", Integer),
91 Column("p_project_period", String),
92 schema=schema
93 )
94 project_period = coma_period_definitions.c.p_project_period
95
96 coma_period_p1_to_runs = Table("coma_period_p1_to_runs", metadata,
97 Column("p2r_index", Integer),
98 Column("p_index", Integer, ForeignKey(coma_period_definitions.c.p_index)),
99 Column("run_index", Integer, ForeignKey(coma_runs.c.run_index)),
100 schema=schema
101 )
102 joined = coma_period_p1_to_runs.join(coma_runs).join(coma_period_definitions)
103
104 smt = (select([project_period, coma_runs.c.run_number], from_obj=joined)
105 .order_by(coma_runs.c.run_number))
106
107 projectperiod_runs = smt.execute().fetchall()
108
109 mapping = {}
110 for projectperiod, run in projectperiod_runs:
111 project, period = projectperiod.split(".")
112 period = period.replace("period", "")
113
114 project_dict = mapping.setdefault(project, {})
115 period_runs = project_dict.setdefault(period, [])
116 if not period_runs or period_runs[-1] != run:
117 period_runs.append(run)
118
119 period_letter = LETTER_PART.search(period)
120 if period_letter:
121 (period_letter,) = period_letter.groups()
122 period_runs = project_dict.setdefault(period_letter, [])
123 if not period_runs or period_runs[-1] != run:
124 period_runs.append(run)
125
126 return mapping
127
128def split_grl(project_tag, periods, grl_file, out_directory):
129 grl = IOVSet.from_grl(grl_file)
130
131 period_info = fetch_project_period_runs()
132 if project_tag not in period_info:
133 raise RuntimeError("Unknown period {0}".format(project_tag))
134 ptag_periods = period_info[project_tag]
135
136 if periods is None:
137 # If periods are unspecified, use all
138 periods = sorted(ptag_periods.keys())
139
140 name_left, _, extension = basename(grl_file).rpartition(".")
141 assert name_left, "No '.' in filename?"
142
143 if not exists(out_directory):
144 makedirs(out_directory)
145
146 for period in periods:
147 iovs = grl & IOVSet.from_runs(ptag_periods[period])
148
149 output_name = "{0}_period{1}.{2}".format(name_left, period, extension)
150 log.info("{0:4s} : {1:3} runs, {2:5} lumiblocks".format(
151 period, len(iovs.runs), iovs.lb_counts))
152
153 output_path = pjoin(out_directory, output_name)
154 iovs.to_grl(output_path)
STL class.
bool exists(const std::string &filename)
does a file exist
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
split_grl(project_tag, periods, grl_file, out_directory)
Definition periods.py:128
fetch_project_period_runs()
Definition periods.py:63
PERIODIOV_VAL(project_tag, period)
Definition periods.py:17
get_period_iovset_from_file(project_tag, period)
Definition periods.py:28
get_period_iovset_from_ami(ptag, period)
Definition periods.py:40
get_periods_from_file()
Definition periods.py:20
std::string basename(std::string name)
Definition utils.cxx:207