ATLAS Offline Software
Input.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 #
4 # This file defines the default input files for trigger validation tests
5 # and keywords to retrieve them in test configuration
6 
7 '''
8 Common way to configure input samples for Trigger ART tests
9 '''
10 
11 import os
12 import json
13 
14 from TrigValTools.TrigValSteering.Common import get_logger, find_file_in_path
15 from functools import lru_cache
16 
17 input_json = 'TrigValTools/TrigValInputs.json'
18 
19 
21  '''
22  Input object representing a data sample of a given format. The sample can
23  consist of multiple files.
24  '''
25 
26  def __init__(self, keyword, source, format, paths):
27  self.log = get_logger()
28  self.keyword = keyword
29 
30  allowed_sources = ['data', 'mc']
31  if source not in allowed_sources:
32  self.log.error('source has to be one of %s', allowed_sources)
33  self.source = None
34  else:
35  self.source = source
36 
37  allowed_formats = ['BS', 'HITS', 'RDO', 'ESD', 'AOD']
38  if format not in allowed_formats:
39  self.log.error('format has to be one of %s', allowed_formats)
40  self.format = None
41  else:
42  self.format = format
43 
44  if not isinstance(paths, list):
45  self.log.error('paths have to be provided as a list')
46  self.paths = None
47  else:
48  self.paths = []
49  for path in paths:
50  if not os.path.isfile(path):
51  self.log.error('Cannot access file: %s', path)
52  else:
53  self.paths.append(path)
54  if len(self.paths) == 0:
55  self.log.error('Failed to parse paths')
56  self.paths = None
57 
58  def __str__(self):
59  str = 'TrigValInput object:\n'
60  str += '---- keyword: {}\n'.format(self.keyword)
61  str += '---- source: {}\n'.format(self.source)
62  str += '---- format: {}\n'.format(self.format)
63  str += '---- paths: {}\n'.format(self.paths)
64  str += '---- is_valid: {}'.format(self.is_valid())
65  return str
66 
67  def is_valid(self):
68  for param in [self.keyword, self.source, self.format, self.paths]:
69  if param is None:
70  return False
71  return True
72 
73 
74 @lru_cache
76  '''Reads the json file with input definitions and returns the data as dictionary'''
77 
78  log = get_logger()
79 
80  input_json_fullpath = find_file_in_path(input_json, 'DATAPATH')
81  if not input_json_fullpath:
82  log.error('Failed to determine full path for input JSON %s', input_json)
83  return None
84 
85  log.debug('Reading %s', input_json_fullpath)
86  with open(input_json_fullpath) as data_file:
87  return json.load(data_file)
88 
89 
90 def is_input_defined(keyword):
91  '''Checks if the keyword exists in the input json file'''
92  data = load_input_json()
93  return keyword in data
94 
95 
96 def get_input(keyword):
97  '''Common getter function to retrieve inputs by keyword'''
98 
99  log = get_logger()
100 
101  data = load_input_json()
102  if keyword not in data.keys():
103  log.error('Failed to find keyword "%s" in input JSON %s',
104  keyword, input_json)
105  return None
106 
107  data_object = data[keyword]
108 
109  result = TrigValInput(
110  keyword,
111  data_object["source"],
112  data_object["format"],
113  data_object["paths"]
114  )
115 
116  if result.is_valid():
117  return result
118  else:
119  log.error('Failed to create a valid input object')
120  return None
python.TrigValSteering.Input.get_input
def get_input(keyword)
Definition: Input.py:96
python.TrigValSteering.Input.TrigValInput.__init__
def __init__(self, keyword, source, format, paths)
Definition: Input.py:26
python.TrigValSteering.Input.TrigValInput.paths
paths
Definition: Input.py:46
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.TrigValSteering.Common.find_file_in_path
def find_file_in_path(filename, path_env_var)
Definition: Common.py:66
python.TrigValSteering.Input.TrigValInput.format
format
Definition: Input.py:40
python.TrigValSteering.Input.TrigValInput.keyword
keyword
Definition: Input.py:28
python.TrigValSteering.Input.is_input_defined
def is_input_defined(keyword)
Definition: Input.py:90
python.TrigValSteering.Input.load_input_json
def load_input_json()
Definition: Input.py:75
python.TrigValSteering.Input.TrigValInput.__str__
def __str__(self)
Definition: Input.py:58
python.TrigValSteering.Input.TrigValInput.is_valid
def is_valid(self)
Definition: Input.py:67
Trk::open
@ open
Definition: BinningType.h:40
python.TrigValSteering.Input.TrigValInput
Definition: Input.py:20
python.TrigValSteering.Common.get_logger
def get_logger()
Definition: Common.py:33
python.TrigValSteering.Input.TrigValInput.log
log
Definition: Input.py:27
pickleTool.object
object
Definition: pickleTool.py:30
error
Definition: IImpactPoint3dEstimator.h:70
python.TrigValSteering.Input.TrigValInput.source
source
Definition: Input.py:33