ATLAS Offline Software
Hto4lPowhegMerge.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 
3 import os, subprocess, tarfile
4 from AthenaCommon import Logging
5 from PowhegControl.utility import LHE
6 # from ...utility import LHE
7 # from xml.etree import ElementTree
8 import shutil
9 
10 
14 
15  __run_directory = os.environ['PATH']
16 
17 
18  __logger = Logging.logging.getLogger('Hto4lPowhegMerger')
19 
20 
21  _merger_executable = 'mergeHto4l4f.exe'
22 
23  def __init__( self, runArgs=None, opts=None ) :
24 
25 
26  self.__output_events_file_name = 'Hto4lPowhegMergedOTF._1.events'
27  # self.__output_events_file_name = 'events.lhe'
28 
29 
30 
31  self.__output_hto4l4e_file_name = 'Hto4lOTF4e._1.events'
32  self.__output_hto4l4mu_file_name = 'Hto4lOTF4mu._1.events'
33  self.__output_hto4l2e2mu_file_name = 'Hto4lOTF2e2mu._1.events'
34  self.__random_seed = 0
35 
36 
37  if runArgs is None :
38  self.logger.warning( 'No run arguments found! Using defaults.' )
39  else :
40  # Read values from runArgs
41  # if hasattr(runArgs,'outputTXTFile') :
42  # self.logger.info( 'outputTXTFile {0} {1}'.format (runArgs.outputTXTFile, type(runArgs.outputTXTFile) ) )
43  # self.__output_events_file_name = runArgs.outputTXTFile
44  # self.output_events_file_name = runArgs.outputTXTFile.split('.tar.gz')[0]+'.events'
45  # Set inputGeneratorFile to match output events file. Otherwise Generate_trf check will fail.
46 
47  runArgs.inputGeneratorFile = self.output_events_file_name
48  # runArgs.inputGeneratorFile = "Hto4lOTF4mu._1.events"
49 
50 
51  def setUpInput(self) :
52 
53  self.logger.info( 'entering setUpInput')
54 
55  # we require four input files, 1 for 4e, 1 for 4mu, and 2 for 2e2mu. The two for 2e2mu will be merged
56  myinputfiles = self.input_powheg_file_name
57  genInputFiles = myinputfiles.split(',')
58  numberOfFiles = len(genInputFiles)
59  # if there is a single file, make a symlink. If multiple files, merge them into one output eventsFile
60  if numberOfFiles > 0:
61  allFiles = []
62  for file in genInputFiles:
63  file_1 = file
64  # untar as needed
65  if tarfile.is_tarfile(file):
66  tar = tarfile.open(file)
67  tar.extractall()
68  tar.close()
69  file_1 = file.replace("tar.gz.1","events")
70  self.logger.info( 'Extracted tar file, and renaming {0} to {1}'.format ( file, file_1 ) )
71  pass
72 
73  # The only input format where merging is permitted is LHE
74  allFiles.append(file_1)
75  with open(file_1, 'r') as f:
76  first_line = f.readline()
77  self.logger.info( 'first_line {0}'.format ( first_line ) )
78  if(not ("LesHouche" in first_line)):
79  raise RuntimeError("%s is NOT a LesHouche file" % file)
80  pass
81  pass
82 
83  self.logger.info( 'Found files: nf {0}'.format( len(allFiles) ) )
84  for file in allFiles:
85  self.logger.info( ' {0}'.format( file ) )
86  pass
87 
88  # Merge files for 4e, 4mu and 2e2mu
89  self.__input_powheg_to_hto4l_file_name = "merged_hto4l_events.lhe"
91  self.logger.info( 'Merged: {0} to {1}'.format ( allFiles, self.input_powheg_to_hto4l_file_name ) )
92  else:
93  self.__input_powheg_to_hto4l_file_name = myinputfiles
94  self.logger.info( 'Using single input file: {0}'.format ( self.input_powheg_to_hto4l_file_name ) )
95 
96 
97  def merge(self) :
98 
99  self.logger.info( 'Starting merge' )
100 
101  hto4lLHE4e = self.output_hto4l4e_file_name
102  hto4lLHE4mu = self.output_hto4l4mu_file_name
103  hto4lLHE2e2mu = self.output_hto4l2e2mu_file_name
104 # powhegLHE_output = self.output_events_file_name
105 
106  self.logger.info( 'Input files: {0} {1} {2}'.format( hto4lLHE4e, hto4lLHE4mu, hto4lLHE2e2mu ) )
107 
108  allFiles = []
109  allFiles += [ hto4lLHE4e ]
110  allFiles += [ hto4lLHE4mu ]
111  allFiles += [ hto4lLHE2e2mu ]
112  self.merge_lhe_files(allFiles, self.output_events_file_name)
113 
114  self.logger.info( 'Merged to output file: {0}'.format( self.output_events_file_name ) )
115 
116  # Now fix the output file for negative weights
118 
119  self.logger.info( 'Fixed weights in file: {}'.format( self.output_events_file_name ) )
120 
121 
122  return
123 
124  def reweight_for_negative_weights(self, powheg_LHE_output):
125  """Post-process the LHE file to update the weights for a negative Hto4l weight.
126  We do two things:
127  1) check if the event weight (XWGTUP) is -1 (can be +/-1). If so, remember this so that the
128  weights in the <rwgt> block can be corrected by multiplying by -1
129  2) After correcting the weights in the <rwgt> block, overwrite XWGTUP by the weight at id=0.
130  Not completely sure if this step is necessary, but is done for Prophecy4f as well.
131  Note: we do NOT save the original XWGTUP as an extra weight (i.e. +/-1). Normally, the sign of
132  any weight should indicate this. The one exception is when BOTH PowHeg and Hto4l has a negative
133  weight and so all weights are positive. This is pretty rare.
134 
135  #@param powheg_LHE_output Name of LHE file produced by merge of Hto4l files.
136 
137  @author RD Schaffer <r.d.schaffer@cern.ch>
138  #"""
139 
140  self.logger.info( 'Starting reweight_for_negative_weights' )
141 
142  # Get opening and closing strings
143  preamble = LHE.preamble(powheg_LHE_output)
144  postamble = LHE.postamble(powheg_LHE_output)
145 
146  # open new file for updates
147  powheg_LHE_updated = "{}.updated".format(powheg_LHE_output)
148  with open(powheg_LHE_updated, "wb") as f_output:
149  f_output.write("{}".format(preamble))
150  # for each event, check for an -1 Hto4l weight in XWGTUP. If it is -1, correct the <rwgt>
151  # weights (-1 * wgt), and save the id=0 weight in XWGTUP.
152  for input_event in LHE.event_iterator(powheg_LHE_output):
153 
154  output_event = ""
155  has_neg_wgt = False
156 
157  # First check for negative weights
158  input_lines = input_event.splitlines(True)
159 
160  try: # interpret 2nd line as a general event info line
161  NUP, IDPRUP, XWGTUP, SCALUP, AQEDUP, AQCDUP = input_lines[1].split()
162  has_neg_wgt = float(XWGTUP) < 0.
163 
164  self.logger.info( 'has_neg_wgt {}, XWGTUP {}'.format(has_neg_wgt, XWGTUP))
165 
166  except ValueError: # this is not a general event info line
167  self.logger.warning( 'could not get first line of event')
168  pass
169 
170  # Now correct weights
171  output_event = ""
172  if has_neg_wgt:
173  for input_line in input_event.splitlines(True):
174  self.logger.info( 'input line {}'.format(input_line))
175  output_line = None
176  if input_line.find("<wgt") >= 0:
177  weights = input_line.split()
178  self.logger.info( 'weightsinput {}'.format(weights))
179  try:
180  wgt_tag, wid, wgt, wgt_tag_end = input_line.split()
181  # We 'multiply' the two weights - if wgt is negative, it now becomes positive.
182  # Otherwise, set it to negative
183  if wgt[0] == '-':
184  output_line = "{} {} {} {} \n".format(wgt_tag, wid, wgt[1:], wgt_tag_end)
185  else:
186  output_line = "{} {} -{} {} \n".format(wgt_tag, wid, wgt, wgt_tag_end)
187  except Exception:
188  pass
189  self.logger.info( 'output line {}'.format(output_line))
190 
191  # save event lines
192  output_event += output_line if output_line is not None else input_line
193  else:
194  output_event = input_event
195 
196  # Overwrite (XWGTUP)
197  output_event_updated = LHE.update_XWGTUP_with_reweighted_nominal(output_event)
198  f_output.write(output_event_updated)
199 
200  f_output.write(postamble)
201  # f_output.close()
202 
203  # Make a backup of the original events
204  shutil.move(powheg_LHE_output, "{}.lhe_nominal_weight_updater_backup".format(powheg_LHE_output))
205  shutil.move(powheg_LHE_updated, powheg_LHE_output)
206 
207  self.logger.info( 'wrote file {}'.format(powheg_LHE_output))
208 
209 
210  @property
212  return self.__output_events_file_name
213 
214 
215  @property
217  return self.__input_powheg_file_name
218 
219 
220  @input_powheg_file_name.setter
221  def input_powheg_file_name(self, value) :
223 
224 
225  @property
228 
229 
230  @property
232  return self.__output_hto4l4e_file_name
233 
234 
235  @property
237  return self.__output_hto4l4mu_file_name
238 
239 
240  @property
243 
244 
245  @output_hto4l4e_file_name.setter
246  def output_hto4l4e_file_name(self, value) :
247  self.__output_hto4l4e_file_name = value
248 
249 
250  @output_hto4l4mu_file_name.setter
251  def output_hto4l4mu_file_name(self, value) :
252  self.__output_hto4l4mu_file_name = value
253 
254 
255  @output_hto4l2e2mu_file_name.setter
256  def output_hto4l2e2mu_file_name(self, value) :
257  self.__output_hto4l2e2mu_file_name = value
258 
259  @property
260  def random_seed(self) :
261  return self.__random_seed
262 
263  @random_seed.setter
264  def random_seed(self, value) :
265  self.__random_seed = value
266 
267 
268  @property
269  def logger(self) :
270  return self.__logger
271 
272  # This function merges a list of input LHE file to make one outputFile. The header is taken from the first
273  # file, but the number of events is updated to equal the total number of events in all the input files
274  def merge_lhe_files(self, listOfFiles, outputFile):
275  if(os.path.exists(outputFile)):
276  self.logger.info( 'outputFile {0} already exists. Will rename to {1}.OLD'.format ( outputFile, outputFile ) )
277  os.rename(outputFile,outputFile+".OLD")
278  output = open(outputFile,'w')
279  holdHeader = ""
280  nevents=0
281  for file in listOfFiles:
282  cmd = "grep /event "+file+" | wc -l"
283  nevents+=int(subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True))
284 
285  for file in listOfFiles:
286  inHeader = True
287  header = ""
288  self.logger.info( '*** Starting to merge file {0}'.format ( file ) )
289  for line in open(file,"r"):
290 
295  if("<event" in line and inHeader):
296  inHeader = False
297  if(len(holdHeader)<1):
298  holdHeader = header
299  output.write(header)
300  output.write(line)
301 
303  elif(not inHeader and not ("</LesHouchesEvents>" in line)):
304  output.write(line)
305  if(inHeader):
306 
307  if("nevents" in line):
308 
309  tmp = line.split("=")
310  line = line.replace(tmp[0],str(nevents))
311  elif("numevts" in line):
312 
313  tmp = line.split(" ")
314  nnn = str(nevents)
315  line = line.replace(tmp[1],nnn)
316  header+=line
317  output.write("</LesHouchesEvents>\n")
318  output.close()
319 
grepfile.info
info
Definition: grepfile.py:38
python.Hto4lPowhegMerge.Hto4lPowhegMerge.reweight_for_negative_weights
def reweight_for_negative_weights(self, powheg_LHE_output)
Definition: Hto4lPowhegMerge.py:124
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_hto4l2e2mu_file_name
__output_hto4l2e2mu_file_name
Definition: Hto4lPowhegMerge.py:33
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.Hto4lPowhegMerge.Hto4lPowhegMerge.merge
def merge(self)
Initialise runcard with generic options.
Definition: Hto4lPowhegMerge.py:97
python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l4e_file_name
def output_hto4l4e_file_name(self)
Get output Hto4l file name for 4e.
Definition: Hto4lPowhegMerge.py:231
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__input_powheg_file_name
__input_powheg_file_name
Definition: Hto4lPowhegMerge.py:222
python.Hto4lPowhegMerge.Hto4lPowhegMerge.input_powheg_file_name
def input_powheg_file_name(self)
Get input Powheg file name.
Definition: Hto4lPowhegMerge.py:216
python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l2e2mu_file_name
def output_hto4l2e2mu_file_name(self)
Get output Hto4l file name for 2e2mu.
Definition: Hto4lPowhegMerge.py:241
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_hto4l4e_file_name
__output_hto4l4e_file_name
Using default output names from PowhegConfig_base and Hto4lConfig.
Definition: Hto4lPowhegMerge.py:31
python.Hto4lPowhegMerge.Hto4lPowhegMerge.merge_lhe_files
def merge_lhe_files(self, listOfFiles, outputFile)
Definition: Hto4lPowhegMerge.py:274
python.Hto4lPowhegMerge.Hto4lPowhegMerge.setUpInput
def setUpInput(self)
Initialise runcard with generic options.
Definition: Hto4lPowhegMerge.py:51
python.Hto4lPowhegMerge.Hto4lPowhegMerge.input_powheg_to_hto4l_file_name
def input_powheg_to_hto4l_file_name(self)
Get input Hto4l file name.
Definition: Hto4lPowhegMerge.py:226
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__random_seed
__random_seed
Definition: Hto4lPowhegMerge.py:34
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__init__
def __init__(self, runArgs=None, opts=None)
Definition: Hto4lPowhegMerge.py:23
python.Hto4lPowhegMerge.Hto4lPowhegMerge
Base class for configurable objects in the jobOptions.
Definition: Hto4lPowhegMerge.py:13
python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_events_file_name
def output_events_file_name(self)
Get output file name.
Definition: Hto4lPowhegMerge.py:211
python.Hto4lPowhegMerge.Hto4lPowhegMerge.logger
def logger(self)
Get handle to logger.
Definition: Hto4lPowhegMerge.py:269
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__logger
__logger
Setup athena-compatible logger.
Definition: Hto4lPowhegMerge.py:18
Trk::open
@ open
Definition: BinningType.h:40
python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l4mu_file_name
def output_hto4l4mu_file_name(self)
Get output Hto4l file name for 4mu.
Definition: Hto4lPowhegMerge.py:236
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
pickleTool.object
object
Definition: pickleTool.py:30
str
Definition: BTagTrackIpAccessor.cxx:11
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__input_powheg_to_hto4l_file_name
__input_powheg_to_hto4l_file_name
Definition: Hto4lPowhegMerge.py:89
python.Hto4lPowhegMerge.Hto4lPowhegMerge.random_seed
def random_seed(self)
Definition: Hto4lPowhegMerge.py:260
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_events_file_name
__output_events_file_name
This needs to be set so that Generate_trf finds an appropriate file format for showering.
Definition: Hto4lPowhegMerge.py:26
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_hto4l4mu_file_name
__output_hto4l4mu_file_name
Definition: Hto4lPowhegMerge.py:32