ATLAS Offline Software
dq_defect_list_iovs.py
Go to the documentation of this file.
1 #!/bin/env python
2 # Author: Steve Farrell <sfarrell@cern.ch>
3 # Part of the DataQuality/DQDefects package
4 # October 2012
5 
6 import os
7 import sys
8 import fnmatch
9 import pickle
10 from collections import defaultdict
11 from datetime import datetime
12 
13 from argparse import ArgumentParser
14 import logging; log = logging.getLogger('dq_defect_list_iovs')
15 
16 from DQDefects import DefectsDB
17 from DQDefects.db import DEFAULT_CONNECTION_STRING
18 
19 from DQUtils import IOVSet, process_iovs, define_iov_type
20 from DQUtils.sugar import RunLumi, RANGEIOV_VAL
21 from DQUtils.periods import fetch_project_period_runs
22 
23 
24 @define_iov_type
25 def DefectIOV(channel, comment):
26  "Stores a defect IOV"
27 
29  """
30  Try to unpickle project dict from file first,
31  unless file doesn't exist or is older than 5 days
32  """
33  filename = 'project_period_runs.pickle'
34  def get_file_age(name):
35  delta = datetime.fromtimestamp(os.path.getmtime(name)) - datetime.now()
36  return delta.days
37  if os.path.exists(filename) and get_file_age(filename) < 5:
38  print('using existing pickled project dict:', filename)
39  with open(filename) as f:
40  project_dict = pickle.load(f)
41  else:
42  # Make a new pickle file
43  print('storing project:period:runs dictionary to', filename)
44  with open(filename, 'w') as f:
45  project_dict = fetch_project_period_runs()
46  pickle.dump(project_dict, f)
47  return project_dict
48 
49 
50 def main():
51  parser = ArgumentParser(description='List defect IOVs')
52  add_arg = parser.add_argument
53 
54  # Specify range to process
55  add_arg('-p', '--project', default='data15_13TeV',
56  help='Data project (default: data15_13TeV)')
57  add_arg('-P', '--period', default=None, nargs='*', help='Data period(s)')
58  #add_arg('-r', '--run', default=None, nargs='*', help='Run number(s) to process')
59  add_arg('-r', '--run', default=None, type=int, help='Run number to process')
60  add_arg('-R', '--range', help='Inclusive run range: e.g. 150000-151000')
61 
62  # Specify defects to process
63  add_arg('-d', '--defects', default=None, nargs='*',
64  help='Defects to process. Use * for wildcard (default: None)')
65 
66  # Other job options
67  add_arg('-c', '--connection-string', default=DEFAULT_CONNECTION_STRING,
68  help='Database connection to use (default: %s)' % DEFAULT_CONNECTION_STRING)
69  #add_arg('-l', '--lumi-tag', default='OflLumi-8TeV-001',
70  #help='Luminosity tag (default: OflLumi-8TeV-001)')
71  add_arg('-t', '--tag', default='HEAD',
72  help='Tag to use (default: HEAD)')
73 
74  # Parse arguments
75  args = parser.parse_args()
76 
77  # Ranges to query
78  since, until = None, None
79  range_iovs = IOVSet()
80 
81  # If specifying runs, use those
82  if args.run:
83  since, until = (args.run, 0), (args.run+1, 0)
84  range_iovs = IOVSet.from_runs([args.run])
85  elif args.range:
86  run1, run2 = map(int, args.range.split('-'))
87  since, until = (run1, 0), (run2, 0)
88  range_iovs = IOVSet([ RANGEIOV_VAL(RunLumi(since), RunLumi(until)) ])
89  # Otherwise, use the project and period settings
90  elif args.project:
91  # Fetch the project:period:runs dictionary
92  project_dict = get_project_dict()
93  period_dict = project_dict[args.project]
94  run_set = set()
95  # Use periods if specified
96  if args.period and len(args.period) > 0:
97  for period in args.period:
98  run_set.update(period_dict[period])
99  # Otherwise use all periods in project
100  else:
101  for period, runs in period_dict.iteritems():
102  run_set.update(runs)
103  since, until = (min(run_set), 0), (max(run_set)+1, 0)
104  range_iovs = IOVSet.from_runs(run_set)
105 
106  # debugging
107  print('range to process:')
108  print('since, until =', since, until)
109 
110  # Instantiate the DB
111  db = DefectsDB(args.connection_string, tag=args.tag)
112 
113  # Fetch the requested defect IOVs
114  defect_iovs = db.retrieve(since, until, channels=args.defects)
115  defect_iovset_list = defect_iovs.by_channel.values()
116 
117  # Get the overlap with the range_iovs
118  # Use a dictionary of defect:iovset
119  result_dict = defaultdict(IOVSet)
120  for since, until, states in process_iovs(range_iovs, *defect_iovset_list):
121  in_range, defects = states[0], states[1:]
122  if in_range:
123  for d in defects:
124  if d:
125  result_dict[d.channel].add(since, until, d.channel, d.comment)
126 
127  # Print the results
128  for channel, result in result_dict.iteritems():
129  result.solidify(DefectIOV)
130  print('\n' + channel + '\n')
131  result.pprint()
132 
133 
134 if __name__ == '__main__':
135  main()
max
#define max(a, b)
Definition: cfImp.cxx:41
python.sugar.iovtype.RANGEIOV_VAL
def RANGEIOV_VAL()
Definition: iovtype.py:153
dq_defect_list_iovs.DefectIOV
def DefectIOV(channel, comment)
Definition: dq_defect_list_iovs.py:25
dq_defect_list_iovs.get_project_dict
def get_project_dict()
Definition: dq_defect_list_iovs.py:28
dq_defect_list_iovs.main
def main()
Definition: dq_defect_list_iovs.py:50
python.sugar.runlumi.RunLumi
RunLumi
Definition: runlumi.py:131
python.periods.fetch_project_period_runs
def fetch_project_period_runs()
Definition: periods.py:64
add
bool add(const std::string &hname, TKey *tobj)
Definition: fastadd.cxx:55
python.events.process_iovs
def process_iovs(*iovsets)
Definition: events.py:30
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:224
min
#define min(a, b)
Definition: cfImp.cxx:40
Trk::open
@ open
Definition: BinningType.h:40
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28