ATLAS Offline Software
Input.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2002-2025 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 json
12 
13 from TrigValTools.TrigValSteering.Common import get_logger, find_file_in_path
14 from functools import lru_cache
15 
16 input_json = 'TrigValTools/TrigValInputs.json'
17 
18 
20  '''
21  Input object representing a data sample of a given format. The sample can
22  consist of multiple files.
23  '''
24 
25  def __init__(self, keyword, source, format, paths):
26  self.log = get_logger()
27  self.keyword = keyword
28 
29  allowed_sources = ['data', 'mc']
30  if source not in allowed_sources:
31  self.log.error('source has to be one of %s', allowed_sources)
32  self.source = None
33  else:
34  self.source = source
35 
36  allowed_formats = ['BS', 'HITS', 'RDO', 'ESD', 'AOD']
37  if format not in allowed_formats:
38  self.log.error('format has to be one of %s', allowed_formats)
39  self.format = None
40  else:
41  self.format = format
42 
43  if not isinstance(paths, list):
44  self.log.error('paths have to be provided as a list')
45  self.paths = None
46  else:
47  self.paths = []
48  for path in paths:
49  # for files on EOS, use xrootd rather than fuse mount
50  if '/eos/' in path:
51  self.paths.append(f'root://eosatlas.cern.ch/{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  # use rucio dataset for grid jobs, else rely on EOS/cvmfs inputs (TrigValInputs.json)
102  import os
103  paths = os.getenv("ArtInFile",None)
104 
105  if paths:
106  source = "data" if "data" in paths else "mc"
107  format = None
108  for key,value in {'RAW':'BS', 'HITS':'HITS', 'RDO':'RDO', 'ESD':'ESD', 'AOD':'AOD'}.items():
109  if key in paths:
110  format = value
111  break
112  data_object = {"source":source, "format":format, "paths":[paths]}
113  else:
114  data = load_input_json()
115  if keyword not in data.keys():
116  log.error('Failed to find keyword "%s" in input JSON %s',keyword, input_json)
117  return None
118 
119  data_object = data[keyword]
120 
121  # for ART tests running on RAW data:
122  # - build tests use small files on cvmfs
123  # - grid tests running interactively must copy large files from EOS to the local area
124  if data_object["format"] == "BS":
125  grid = False
126  Nfiles = 0
127  import sys
128  with open(sys.argv[0], 'r') as f:
129  for line in f:
130  if "# art-type:" in line:
131  grid = line.split()[2]=="grid"
132  if "# art-input-nfiles:" in line:
133  Nfiles = int(line.split()[2])
134  if grid:
135  data_object["paths"] = [path for path in data_object["paths"] if "/eos/" in path]
136  import subprocess
137  local_files = []
138  for i in range(Nfiles):
139  f = data_object["paths"][i].split('/')[-1]
140  if not (os.path.exists(f)) and not os.environ.get('TRIGVALSTEERING_DRY_RUN'):
141  print(f'copying {data_object["paths"][i]}')
142  result = subprocess.run(['xrdcp',f'root://eosatlas.cern.ch/{data_object["paths"][i]}','.'])
143  if result.returncode != 0:
144  raise Exception("xrdcp failed, please check you have a valid kerberos ticket")
145  local_files.append(f)
146  data_object["paths"] = local_files
147  else:
148  data_object["paths"] = [path for path in data_object["paths"] if "/cvmfs/" in path]
149 
150  result = TrigValInput(
151  keyword,
152  data_object["source"],
153  data_object["format"],
154  data_object["paths"]
155  )
156 
157  if result.is_valid():
158  return result
159  else:
160  log.error('Failed to create a valid input object')
161  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:25
python.TrigValSteering.Input.TrigValInput.paths
paths
Definition: Input.py:45
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
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
python.TrigValSteering.Input.TrigValInput.format
format
Definition: Input.py:39
python.TrigValSteering.Input.TrigValInput.keyword
keyword
Definition: Input.py:27
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
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.TrigValSteering.Input.TrigValInput.__str__
def __str__(self)
Definition: Input.py:58
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:71
python.TrigValSteering.Input.TrigValInput.is_valid
def is_valid(self)
Definition: Input.py:67
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.TrigValSteering.Input.TrigValInput
Definition: Input.py:19
python.TrigValSteering.Common.get_logger
def get_logger()
Definition: Common.py:33
python.TrigValSteering.Input.TrigValInput.log
log
Definition: Input.py:26
pickleTool.object
object
Definition: pickleTool.py:29
error
Definition: IImpactPoint3dEstimator.h:70
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.TrigValSteering.Input.TrigValInput.source
source
Definition: Input.py:32