ATLAS Offline Software
validation.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 
3 # Author: Abhishek Nag (TU Dresden)
4 # email: abhishek.nag@cern.ch
5 
6 import os
7 import sys
8 import optparse
9 from itertools import chain
10 import readDatabase
11 sys.path.append('$TestArea/../')
12 
13 
14 def file_is_empty(path):
15  return os.stat(path).st_size == 0
16 
17 
18 def getDAOD(list_DAOD):
19  DAOD_types = []
20  for d in list_DAOD:
21  DAOD_types.append(d.split('.')[4])
22  short_DAOD = list(set(DAOD_types))
23  return short_DAOD
24 
25 
26 def validate(testSampleDir, thisSampleName, testSamplePath, weight_database, outputSamples):
27  for dirName in os.listdir(testSampleDir):
28  if thisSampleName.split(":")[1] in dirName:
29  testSamplePath = testSampleDir + "/" + dirName
30  if (testSamplePath is None):
31  print("[INFO] do not currently have a test sample for ", thisSampleName)
32  rucioCommand = "rucio download --nrandom 1 %s --dir %s " % (thisSampleName, opts.testSampleDir)
33  print("[INFO] --> downloading one using this command \n ", rucioCommand)
34  os.system(rucioCommand)
35  testSamplePath = testSampleDir + "/" + thisSampleName.split(":")[1]
36  if not os.path.exists(testSamplePath):
37  print("[ERROR INFO] No such directory: %s OR downloading Failed" % (testSamplePath))
38  return
39  if not os.listdir(testSamplePath):
40  print("[ERROR INFO] Downloading Failed for ", testSamplePath)
41  return
42  for fileName in os.listdir(testSamplePath):
43  if fileName.endswith('part'):
44  print("[ERROR INFO] Downloading Failed for ", testSamplePath)
45  rmdir = "rm -r %s" % (testSamplePath)
46  os.system(rmdir)
47  return
48  testSamplePath = testSamplePath + "/" + fileName
49  break
50  print("[SUCCESS] found test file ", testSamplePath)
51  athenaCommand = "athena --filesInput=%s --evtMax = 1 MyPackage/MyPackageAlgJobOptions.py" % (testSamplePath)
52  print("[INFO] running athena weight retrieving tool using \n", athenaCommand)
53  os.system(athenaCommand)
54  if not os.path.exists('weight.txt'):
55  print('[INFO] Athena Tool failed to retrieve weights')
56  return
57  wfile = open('weight.txt', 'r')
58  ofile = open(outputSamples, 'a+')
59  weightFile = wfile.read().splitlines()
60  if set(weight_database) == set(weightFile):
61  print('%s is Validated' % (thisSampleName))
62  ofile.write(thisSampleName.split(":")[1] + ' ' + 'PASSED' + '\n')
63  else:
64  print('%s is NOT Validated' % (thisSampleName))
65  ofile.write(thisSampleName.split(":")[1] + ' ' + 'FAILED')
66  wfile.close()
67  ofile.close()
68  rmcommand = "rm weight.txt"
69  os.system(rmcommand)
70  rmsample = "rm -r %s" % (testSampleDir + "/" + thisSampleName.split(":")[1])
71  os.system(rmsample)
72 
73 
74 parser = optparse.OptionParser(usage="%prog [options]")
75 parser.add_option("-i", "--inputSamples", help="list of samples. One per line", dest="inputSamples", default="../toValidate_samples.txt")
76 parser.add_option("--testSampleDir", help="where to store the test samples.", dest="testSampleDir", default="../validSamples")
77 parser.add_option("-o", "--outputSamples", help="list of validated samples. One per line, with tag as PASSED/FAILED", dest="outputSamples", default="../validated_samples.txt")
78 (opts, args) = parser.parse_args()
79 
80 os.system("mkdir -p %s" % opts.testSampleDir)
81 f = open(opts.inputSamples)
82 for line in f.readlines():
83  weight_database = []
84  if line[0] == "#" or line == '': continue
85  SampleName = line.split()[0]
86  dsid = SampleName.split(".")[1]
87  dictionary, keys = readDatabase.getWeights(dsid)
88  if keys == []:
89  print('[FAIL] DSID %s not found in Database' % (dsid))
90  continue
91  for key in keys:
92  weight_database.append(dictionary[key]['weights'])
93  weight_database = list(chain.from_iterable(weight_database))
94  testSamplePath = None
95  if 'DAOD' in SampleName:
96  ofile = open(opts.outputSamples, 'a+')
97  if any(SampleName.split(":")[1] in oline for oline in ofile.readlines()):
98  print('[INFO] sample already checked')
99  continue
100  ofile.close()
101  validate(opts.testSampleDir, SampleName, testSamplePath, weight_database, opts.outputSamples)
102  continue
103  ruciolist = "rucio list-dids %s*DAOD* --filter type = CONTAINER --short > list_DAOD.txt" % (SampleName.split("_")[0] + '_' + SampleName.split("_")[1] + '_' + SampleName.split("_")[2])
104  ruciolist = ruciolist.replace('mc15', 'mc16')
105  print("[INFO] rucio listing DAODs using this command \n ", ruciolist)
106  os.system(ruciolist)
107  if file_is_empty('list_DAOD.txt'):
108  print('No DAODs found for the given sample: ', SampleName)
109  continue
110  lfile = open('list_DAOD.txt', 'r')
111  list_DAOD = lfile.readlines()
112  shortDAOD = getDAOD(list_DAOD)
113  for DAOD in shortDAOD:
114  d_list = list(element for element in list_DAOD if DAOD in element)
115  d_list.sort()
116  thisSampleName = d_list[-1].split()[0]
117  testSamplePath = None
118  ofile = open(opts.outputSamples, 'a+')
119  if any(thisSampleName.split(":")[1] in oline for oline in ofile.readlines()):
120  print('[INFO] sample already checked')
121  continue
122  ofile.close()
123  validate(opts.testSampleDir, thisSampleName, testSamplePath, weight_database, opts.outputSamples)
124 f.close()
validation.validate
def validate(testSampleDir, thisSampleName, testSamplePath, weight_database, outputSamples)
Definition: validation.py:26
validation.getDAOD
def getDAOD(list_DAOD)
Definition: validation.py:18
validation.file_is_empty
def file_is_empty(path)
Definition: validation.py:14
histSizes.list
def list(name, path='/')
Definition: histSizes.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
Trk::open
@ open
Definition: BinningType.h:40
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
readDatabase.getWeights
def getWeights(dsid)
Definition: readDatabase.py:22
Trk::split
@ split
Definition: LayerMaterialProperties.h:38