ATLAS Offline Software
LArHistMerge_trf.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 
7 import sys, os, pickle, pprint, subprocess
8 
9 from LArCafJobs.GetLBsToIgnore import getLBsToIgnore
10 
11 
12 
13 
14 # Utility function
15 
16 def getFileMap(fname, dsname, nevts=0, guid='') :
17  if os.path.isfile(fname) :
18  dataMap = { 'lfn': fname,
19  'dataset' : dsname,
20  'events' : nevts
21  }
22  if guid :
23  dataMap['GUID'] = guid
24  else :
25  dataMap = {}
26  return dataMap
27 
28 
29 
30 
31 def getPFN(filespec):
32  if isinstance(filespec,dict):
33  if "svcclass" in filespec:
34  os.environ["STAGE_SVCCLASS"]=filespec["svcclass"]
35  pass
36 
37  if 'pfn' in filespec:
38  return filespec['pfn']
39  else:
40  print ("ERROR: Unknown file specification (dict):")
41  print (filespec)
42  sys.exit(-1)
43 
44 
45  if isinstance(filespec,str):
46  names=filespec.split('#')
47  if len(names)!=2:
48  print ("ERROR: Unexpected file specification (string):")
49  print (filespec)
50  sys.exit(-1)
51  else:
52  return names[1]
53 
54 
55 
56 
57 def getRunLB(fname):
58  filename=fname.split("/")[-1] #Strip off path
59  elem=filename.split(".")
60  #File name example:
61  #data11_7TeV.00190618.physics_CosmicCalo.recon.HIST.f411._lb0826._SFO-ALL._0001.1
62  if (len(elem) <6):
63  print ("ERROR: Invalid file name format. Expected at least six subfileds, got",filename)
64  sys.exit(-1)
65  pass
66 
67  step=elem[3]
68  if step != 'recon':
69  return (-1,-1)
70 
71  rnStr=elem[1]
72 
73  if not rnStr.isdigit():
74  print ("ERROR: Can't get run number, got",rnStr)
75  sys.exit(-1)
76  pass
77  run=int(rnStr,10)
78 
79  LBStr=elem[6]
80  if (len(LBStr)<4):
81  print ("ERROR: Can't get LB number, got",LBStr)
82  sys.exit(-1)
83  pass
84 
85  LBStr=LBStr[3:]
86  if not LBStr.isdigit():
87  print ("ERROR: Can't get LB number, got",LBStr)
88  sys.exit(-1)
89  pass
90 
91  LB=int(LBStr,10)
92  return (run,LB)
93 
94 
95 def larMerge(dataMap) :
96 
97  print ("\n##################################################################")
98  print ( "## ATLAS Tier0 LAr CAF file Merging ##")
99  print ( "##################################################################\n")
100 
101  print ("\nFull Tier-0 run options:\n")
102  pprint.pprint(dataMap)
103 
104  inputList = []
105 
106  badLBs=set()
107 
108  inFiles=dataMap['inputHistFiles']
109 
110 
111  if len(inFiles)>0:
112  firstFile=getPFN(inFiles[0])
113  runnumber=getRunLB(firstFile)[0]
114  if runnumber==-1:
115  print ("Encountered pre-merged file, no bad-LB checking done" )
116  else:
117  print ("Found run number",runnumber)
118  badLBs=getLBsToIgnore(runnumber)
119 
120 
121  for val in inFiles:
122  pfn=getPFN(val)
123  if len(badLBs)>0:
124  LB=getRunLB(pfn)[1]
125  if LB in badLBs:
126  print ("Ignoring bad LumiBlock",LB)
127  continue
128  inputList.append(pfn)
129  sys.stdout.flush()
130  #print ("\ninputLArFiles list:\n")
131  #pprint.pprint(inputList)
132 
133  #Write input file list to temporary file:
134  templist=open("inputfiles.txt","w")
135  for infile in inputList:
136  templist.write(infile+"\n")
137  pass
138  templist.close()
139 
140 
141  # output file
142  outputDSName = dataMap['outputLArHistFile'].split('#')[0]
143  outputFile = dataMap['outputLArHistFile'].split('#')[1]
144 
145  print ('\nOutput file name:', outputFile)
146 
147  retcode = 0
148 
149  cmd="LArQuickHistMerge.exe -i inputfiles.txt " + outputFile
150 
151  try:
152  retcode = subprocess.call(cmd, shell=True)
153  print ('retcode =',retcode)
154  if retcode != 0 :
155  retcode = 62601
156  acronym = "LARQUICKHISTMEGE PROBLEM"
157  except OSError as e :
158  retcode = 62600
159  print (e)
160  acronym = "SUBPROCESS EXECUTION PROBLEM"
161  pass
162 
163  if retcode==0:
164  cmd ="DQWebDisplay.py " + outputFile +" LArDisplay 111"
165  print ("Attempt to run",cmd)
166  try:
167  retcodeDQM = subprocess.call(cmd, shell=True)
168  print ('retcode =',retcodeDQM)
169  except Exception as e:
170  print ("Attempt failed with exception")
171  print (e)
172 
173 
174  # get info for report gpickle file
175  if retcode == 0 :
176  outputMap = getFileMap(outputFile, outputDSName, nevts=0)
177  outFiles = [ outputMap ]
178  acronym = 'OK'
179  else:
180  outFiles = []
181  outputMap = {}
182  print ("ERROR: problem in LAr Histogram merging!")
183  if retcode == 62600 :
184  acronym = 'TRF_LAR_FILE_INPUT_ERROR'
185  elif retcode == 62601 :
186  acronym = 'TRF_LAR_MERGE_ERROR'
187 
188 
189 
190  # assemble job report map, pickle it
191  outMap = { 'prodsys': { 'trfCode': retcode,
192  'trfAcronym': acronym,
193  'jobOutputs': outFiles,
194  'jobInputs': inFiles,
195  }
196  }
197  f = open('jobReport.gpickle', 'wb')
198  pickle.dump(outMap, f)
199  f.close()
200 
201  print ("\n##################################################################")
202  print ( "## End of job.")
203  print ( "##################################################################\n")
204 
205 
206 
207 
210 
211 if __name__ == "__main__":
212 
213  if len(sys.argv) == 2 and sys.argv[1].startswith('--argdict=') :
214  picklefile = sys.argv[1][len('--argdict='):]
215  # extract parameters from pickle file
216  print ("Using pickled file ", picklefile, " for input parameters")
217  f = open(picklefile, 'rb')
218  dataMap = pickle.load(f)
219  f.close()
220  larMerge(dataMap)
221  elif len(sys.argv) == 3 and sys.argv[1].startswith('--inputFiles=') and sys.argv[2].startswith('--outputFile=') :
222  inputFiles = sys.argv[1][13:].split(',')
223  outputFile = sys.argv[2][13:]
224  dataMap = {}
225  dataMap['inputHistFiles'] = inputFiles
226  dataMap['outputLArHistFile'] = outputFile
227  larMerge(dataMap)
228  else :
229  print ("Input format wrong --- either have: ")
230  print (" --argdict=<pickled-dictionary containing input info> ")
231  print (" with key/value pairs: ")
232  print (" or (old way): ")
233  print (" --inputFiles=file1,file2,...,fileN --outputFile=file")
234  exit(1)
LArHistMerge_trf.getRunLB
def getRunLB(fname)
Definition: LArHistMerge_trf.py:57
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
GetLBsToIgnore.getLBsToIgnore
def getLBsToIgnore(runnum, burstsFromCosmic=True, bulkProcessing=False, dropNonReady=True)
Definition: GetLBsToIgnore.py:21
LArHistMerge_trf.getPFN
def getPFN(filespec)
Definition: LArHistMerge_trf.py:31
calibdata.exit
exit
Definition: calibdata.py:236
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
LArHistMerge_trf.getFileMap
def getFileMap(fname, dsname, nevts=0, guid='')
Definition: LArHistMerge_trf.py:16
LArHistMerge_trf.larMerge
def larMerge(dataMap)
Definition: LArHistMerge_trf.py:95
Trk::open
@ open
Definition: BinningType.h:40
Trk::split
@ split
Definition: LayerMaterialProperties.h:38