ATLAS Offline Software
LArMerge_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 
24 
25 import sys, os, pickle, pprint, subprocess
26 
27 
28 
29 # Utility function
30 
31 def getFileMap(fname, dsname, nevts=0, guid='') :
32  if os.path.isfile(fname) :
33  dataMap = { 'lfn': fname,
34  'dataset' : dsname,
35  'events' : nevts
36  }
37  if guid :
38  dataMap['GUID'] = guid
39  else :
40  dataMap = {}
41  return dataMap
42 
43 
44 
45 
46 def larMerge(dataMap) :
47 
48  print ("\n##################################################################")
49  print ( "## ATLAS Tier0 LAr CAF file Merging ##")
50  print ( "##################################################################\n")
51 
52  print ("\nFull Tier-0 run options:\n")
53  pprint.pprint(dataMap)
54 
55  inputList = []
56  inputDSName = None
57 
58  for val in dataMap['inputLArFiles'] :
59  inputList.append(val.split('#')[1])
60  if not inputDSName :
61  inputDSName = val.split('#')[0]
62 
63  print ("\ninputLArFiles list:\n")
64  pprint.pprint(inputList)
65 
66  # output file
67  outputDSName = dataMap['outputLArFile'].split('#')[0]
68  outputFile = dataMap['outputLArFile'].split('#')[1]
69 
70  print ('\nOutput file name:', outputFile)
71 
72  retcode = 0
73  acronym = 'OK'
74 
75  outFiles = []
76  inFiles = []
77  nEvents=0
78 
79  cmdline=["LArSamplesMerge.exe"]
80  for fileName in inputList:
81  if os.access(fileName,os.R_OK):
82  cmdline.append(fileName)
83  else:
84  print ('ERROR : could not open file', fileName)
85  retcode = 74001
86  acronym = 'TRF_LAR_FILE_INPUT_ERROR'
87  break
88  pass
89 
90  if (retcode==0):
91  writepath=os.path.split(outputFile)[0]
92  if writepath=="":
93  writepath="./"
94  if (os.access(writepath,os.W_OK)):
95  cmdline.append(outputFile)
96  else:
97  print ("ERROR, not allowed to write to oututfile", outputFile)
98  retcode = 74002
99  acronym = 'TRF_LAR_MERGE_ERROR'
100  pass
101  pass
102 
103  if (retcode==0):
104 
105  logfile=open("log.LArMerge","w")
106 
107  try:
108  subprocess.check_call(cmdline,stdout=logfile,stderr=subprocess.STDOUT)
109  except Exception as e:
110  print ("ERROR exectution of subprocess failed")
111  print (e)
112  acronym = 'TRF_LAR_MERGE_ERROR'
113  pass
114 
115  logfile.close()
116  logfile=open("log.LArMerge","r")
117 
118  for line in logfile:
119  print (line, end='')
120  if line.startswith("Open file "):
121  linetok=line.split(" ")
122  inFiles.append(getFileMap(linetok[2], None, nevts=int(linetok[4])))
123  pass
124  if line.startswith("Wrote output file "):
125  linetok=line.split(" ")
126  nEvents=int(linetok[5])
127  outFiles=[getFileMap(linetok[3], outputDSName, nevts=nEvents),]
128  pass
129 
130  logfile.close()
131 
132 
133 
134  # assemble job report map, pickle it
135  outMap = { 'prodsys': { 'trfCode': retcode,
136  'trfAcronym': acronym,
137  'jobOutputs': outFiles,
138  'jobInputs': inFiles,
139  'nevents': nEvents,
140  }
141  }
142  f = open('jobReport.gpickle', 'wb')
143  pickle.dump(outMap, f)
144  f.close()
145 
146  print ("\n##################################################################")
147  print ( "## End of job.")
148  print ( "##################################################################\n")
149 
150  print (outMap)
151 
152 
155 
156 if __name__ == "__main__":
157 
158  if len(sys.argv) == 2 and sys.argv[1].startswith('--argdict=') :
159  picklefile = sys.argv[1][len('--argdict='):]
160  # extract parameters from pickle file
161  print ("Using pickled file ", picklefile, " for input parameters")
162  f = open(picklefile, 'rb')
163  dataMap = pickle.load(f)
164  f.close()
165  larMerge(dataMap)
166  elif len(sys.argv) == 3 and sys.argv[1].startswith('--inputFiles=') and sys.argv[2].startswith('--outputFile=') :
167  inputFiles = sys.argv[1][13:].split(',')
168  outputFile = sys.argv[2][13:]
169  dataMap = {}
170  dataMap['inputLArFiles'] = inputFiles
171  dataMap['outputLArFile'] = outputFile
172  larMerge(dataMap)
173  else :
174  print ("Input format wrong --- either have: ")
175  print (" --argdict=<pickled-dictionary containing input info> ")
176  print (" with key/value pairs: ")
177  print (" or (old way): ")
178  print (" --inputFiles=file1,file2,...,fileN --outputFile=file")
179  exit(1)
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
LArMerge_trf.larMerge
def larMerge(dataMap)
Definition: LArMerge_trf.py:46
calibdata.exit
exit
Definition: calibdata.py:236
LArMerge_trf.getFileMap
def getFileMap(fname, dsname, nevts=0, guid='')
Definition: LArMerge_trf.py:31
Trk::open
@ open
Definition: BinningType.h:40
Trk::split
@ split
Definition: LayerMaterialProperties.h:38