ATLAS Offline Software
merge_lhe_files.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 # This function merges a list of input LHE file to make one outputFile. The header is taken from the first
3 # file, but the number of events is updated to equal the total number of events in all the input files
4 def merge_lhe_files(listOfFiles,outputFile):
5  if(os.path.exists(outputFile)):
6  print ("outputFile ",outputFile," already exists. Will rename to ",outputFile,".OLD")
7  os.rename(outputFile,outputFile+".OLD")
8  output = open(outputFile,'w')
9  holdHeader = ""
10  nevents=0
11  for file in listOfFiles:
12  cmd = "grep /event "+file+" | wc -l"
13  nevents+=int(subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True))
14 
15  for file in listOfFiles:
16  inHeader = True
17  header = ""
18  print ("*** Starting file ",file)
19  for line in open(file,"r"):
20 
25  if("<event" in line and inHeader):
26  inHeader = False
27  if(len(holdHeader)<1):
28  holdHeader = header
29  output.write(header)
30  output.write(line)
31 
33  elif(not inHeader and not ("</LesHouchesEvents>" in line)):
34  output.write(line)
35  if(inHeader):
36 
37  if("nevents" in line and "Required accuracy" not in line):
38 
39  tmp = line.split("=")
40  line = line.replace(tmp[0],str(nevents))
41  elif("numevts" in line):
42 
43  tmp = line.split(" ")
44  nnn = str(nevents)
45  line = line.replace(tmp[1],nnn)
46  header+=line
47  output.write("</LesHouchesEvents>\n")
48  output.close()
49 
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Trk::open
@ open
Definition: BinningType.h:40
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
merge_lhe_files.merge_lhe_files
def merge_lhe_files(listOfFiles, outputFile)
Definition: merge_lhe_files.py:4
str
Definition: BTagTrackIpAccessor.cxx:11