ATLAS Offline Software
oracle.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 
5 from sqlalchemy import (select, create_engine, MetaData, Table, Column, String,
6  Integer)
7 from sqlalchemy.orm import sessionmaker
8 from sqlalchemy.sql import and_
9 
10 from DQUtils.sugar import IOVSet
11 from xml.dom.minidom import parse
12 from os.path import exists
13 
14 from future.standard_library import install_aliases
15 install_aliases()
16 from urllib.parse import urlparse # noqa: E402
17 
18 def parse_auth_file(file_name, connection):
19 
20  dom = parse(file_name)
21 
22  connections = dom.getElementsByTagName("connection")
23  desired_conn = lambda c: c.attributes.get("name").value == connection
24 
25  connections = list(filter(desired_conn, connections))
26 
27  if len(connections) < 1:
28  return None
29 
30  info = {}
31  for node in connections[0].childNodes:
32  if node.nodeName == "parameter":
33  info[node.getAttribute("name")] = str(node.getAttribute("value"))
34 
35  authentication = info["user"], info["password"]
36  return authentication
37 
38 def get_authentication(connection="oracle://ATLAS_COOLPROD/ATLAS_COOLONL_GLOBAL"):
39  """
40  Retrieves authentication information from CORAL_AUTH_PATH authentication.xml
41  """
42 
43  from os import environ
44  from os.path import join as pjoin
45  assert "CORAL_AUTH_PATH" in environ, "CORAL_AUTH_PATH environment var not set"
46 
47  auth_paths = environ["CORAL_AUTH_PATH"].split(":")
48 
49  for auth_path in auth_paths + ["."]:
50  file_name = pjoin(auth_path, "authentication.xml")
51  if exists(file_name):
52  authentication = parse_auth_file(file_name, connection)
53  if authentication:
54  return authentication
55 
56  raise RuntimeError("Unable to locate credentials for %s."
57  % connection)
58 
59 def make_oracle_connection(connection_string):
60  "oracle://ATLAS_COOLPROD/ATLAS_COOLONL_GLOBAL"
61  assert connection_string.startswith("oracle://"), "Not a connection string"
62  host = urlparse (connection_string[len("oracle:"):]).netloc
63  username, password = get_authentication(connection_string)
64  conn_str = "oracle://%s:%s@%s" % (username, password, host)
65  engine = create_engine(conn_str, pool_recycle=10*60)
66  metadata = MetaData()
67  metadata.reflect(engine)
68  return engine, metadata
69 
70 #conn_str = "oracle://%s:%s@ATLAS_COOLPROD" % get_authentication()
71 
72 # Recycle the connection every 10 minutes
73 #engine = create_engine(conn_str, pool_recycle=10*60)
74 #metadata = MetaData(bind=engine)
75 
76 engine, metadata = make_oracle_connection("oracle://ATLAS_COOLPROD"
77  "/ATLAS_COOLONL_GLOBAL")
78 Session = sessionmaker(engine)
79 
80 run_table = Table("ATLAS_RUN_NUMBER.RUNNUMBER", metadata,
81  Column("NAME", String),
82  Column("RUNNUMBER", String),
83  Column("STARTAT", String),
84  Column("DURATION", Integer),
85  Column("CREATEDBY", String),
86  Column("HOST", String),
87  Column("PARTITIONNAME", String),
88  Column("CONFIGSCHEMA", Integer),
89  Column("CONFIGDATA", String),
90  Column("COMMENTS", String),
91  quote=False
92 )
93 
94 ONE_WEEK = 7*24*3600
95 
96 def fetch_recent_runs(how_recent=ONE_WEEK, ascending=False):
97  """
98  Retrieve a list of ATLAS runs from the database, since first_run
99  """
100  from time import time, strftime, gmtime
101  t = run_table
102 
103  ordering = t.c.RUNNUMBER.asc() if ascending else t.c.RUNNUMBER.desc()
104 
105  this_recent = strftime("%Y%m%dT%H%M%S", gmtime(time()-how_recent))
106  condition = and_(t.c.STARTAT >= this_recent, t.c.PARTITIONNAME == "ATLAS")
107  rows = select(run_table).where(condition).order_by(ordering)
108  with Session() as session:
109  return session.execute(rows).fetchall()
110 
111 def fetch_runs_since(first_run=140000, ascending=False):
112  """
113  Retrieve a list of ATLAS runs from the database, since first_run
114  """
115  t = run_table
116 
117  ordering = t.c.RUNNUMBER.asc() if ascending else t.c.RUNNUMBER.desc()
118 
119  condition = and_(t.c.RUNNUMBER > first_run, t.c.PARTITIONNAME == "ATLAS")
120  rows = select(run_table).where(condition).order_by(ordering)
121  with Session() as session:
122  return session.execute(rows).fetchall()
123 
125  return (select(run_table.c.RUNNUMBER)
126  .where(run_table.c.PARTITIONNAME == "ATLAS")
127  .order_by(run_table.c.RUNNUMBER))
128 
130  rows = (select(run_table.c.RUNNUMBER)
131  .where(run_table.c.PARTITIONNAME == "ATLAS")
132  .order_by(run_table.c.RUNNUMBER.desc()).limit(n))
133 
134  with Session() as session:
135  return [row.RUNNUMBER for row in reversed(session.execute(rows).fetchall())]
136 
139  with Session() as session:
140  return session.execute(rows).fetchall()
141 
143  return set(x.RUNNUMBER for x in fetch_atlas_runs())
144 
145 def atlas_runs_between(first, last):
146 
148  rows = rows.where(run_table.c.RUNNUMBER.between(first, last))
149 
150  with Session() as session:
151  return [row.RUNNUMBER for row in session.execute(rows).fetchall()]
152 
154 
155  iov_runs = set(iov.since.run for iov in iovs)
156  first, last = min(iov_runs), max(iov_runs)
157 
159  rows = rows.where(run_table.c.RUNNUMBER.between(first, last))
160 
161  with Session() as session:
162  atlas_runs = set(row.RUNNUMBER for row in session.execute(rows).fetchall())
163  keep_runs = atlas_runs.intersection(iov_runs)
164 
165  return IOVSet(iov for iov in iovs if iov.since.run in keep_runs)
166 
python.oracle.fetch_recent_runs
def fetch_recent_runs(how_recent=ONE_WEEK, ascending=False)
Definition: oracle.py:96
python.oracle.atlas_runs_between
def atlas_runs_between(first, last)
Definition: oracle.py:145
max
#define max(a, b)
Definition: cfImp.cxx:41
python.oracle.fetch_last_n_atlas_runs
def fetch_last_n_atlas_runs(n=10)
Definition: oracle.py:129
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
python.oracle.fetch_runs_since
def fetch_runs_since(first_run=140000, ascending=False)
Definition: oracle.py:111
python.oracle.make_atlas_partition_query
def make_atlas_partition_query()
Definition: oracle.py:124
python.oracle.Session
Session
Definition: oracle.py:78
python.oracle.make_oracle_connection
def make_oracle_connection(connection_string)
Definition: oracle.py:59
covarianceTool.filter
filter
Definition: covarianceTool.py:514
python.Utils.unixtools.where
def where(filename, prepath=[])
"which" for python files -------------------------------------------------—
Definition: unixtools.py:53
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.oracle.atlas_runs_set
def atlas_runs_set()
Definition: oracle.py:142
python.oracle.parse_auth_file
def parse_auth_file(file_name, connection)
Definition: oracle.py:18
python.oracle.get_authentication
def get_authentication(connection="oracle://ATLAS_COOLPROD/ATLAS_COOLONL_GLOBAL")
Definition: oracle.py:38
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
min
#define min(a, b)
Definition: cfImp.cxx:40
python.oracle.fetch_atlas_runs
def fetch_atlas_runs()
Definition: oracle.py:137
str
Definition: BTagTrackIpAccessor.cxx:11
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9
DerivationFramework::ClustersInCone::select
void select(const xAOD::IParticle *particle, const float coneSize, const xAOD::CaloClusterContainer *clusters, std::vector< bool > &mask)
Definition: ClustersInCone.cxx:14
updateCoolNtuple.limit
int limit
Definition: updateCoolNtuple.py:45
python.oracle.filter_atlas_runs
def filter_atlas_runs(iovs)
Definition: oracle.py:153
Trk::split
@ split
Definition: LayerMaterialProperties.h:38