ATLAS Offline Software
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 
6 from os import listdir, makedirs
7 from os.path import basename, exists, join as pjoin
8 
9 from .sugar import define_iov_type, IOVSet
10 
11 import logging
12 log = logging.getLogger("DQUtils.periods")
13 
14 DATA_PERIODS_PATH = "/afs/cern.ch/atlas/www/GROUPS/DATAPREPARATION/DataPeriods/"
15 
16 @define_iov_type
17 def 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 
28 def 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 
40 def get_period_iovset_from_ami(ptag, period):
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 
128 def 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)
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
vtune_athena.format
format
Definition: vtune_athena.py:14
python.dummyaccess.listdir
def listdir(dirname)
Definition: dummyaccess.py:6
FakeBkgTools::Client
Client
Definition: FakeBkgInternals.h:141
python.oracle.make_oracle_connection
def make_oracle_connection(connection_string)
Definition: oracle.py:59
python.periods.PERIODIOV_VAL
def PERIODIOV_VAL(project_tag, period)
Definition: periods.py:17
python.periods.fetch_project_period_runs
def fetch_project_period_runs()
Definition: periods.py:63
python.periods.get_periods_from_file
def get_periods_from_file()
Definition: periods.py:20
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.periods.split_grl
def split_grl(project_tag, periods, grl_file, out_directory)
Definition: periods.py:128
python.periods.get_period_iovset_from_ami
def get_period_iovset_from_ami(ptag, period)
Definition: periods.py:40
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9
python.periods.get_period_iovset_from_file
def get_period_iovset_from_file(project_tag, period)
Definition: periods.py:28
DerivationFramework::ClustersInCone::select
void select(const xAOD::IParticle *particle, const float coneSize, const xAOD::CaloClusterContainer *clusters, std::vector< bool > &mask)
Definition: ClustersInCone.cxx:14
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
beamspotman.basename
basename
Definition: beamspotman.py:638