ATLAS Offline Software
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
python.Hto4lPowhegMerge.Hto4lPowhegMerge Class Reference

Base class for configurable objects in the jobOptions. More...

Inheritance diagram for python.Hto4lPowhegMerge.Hto4lPowhegMerge:
Collaboration diagram for python.Hto4lPowhegMerge.Hto4lPowhegMerge:

Public Member Functions

def __init__ (self, runArgs=None, opts=None)
 
def setUpInput (self)
 Initialise runcard with generic options. More...
 
def merge (self)
 Initialise runcard with generic options. More...
 
def reweight_for_negative_weights (self, powheg_LHE_output)
 
def output_events_file_name (self)
 Get output file name. More...
 
def input_powheg_file_name (self)
 Get input Powheg file name. More...
 
def input_powheg_file_name (self, value)
 Set input Powheg file name. More...
 
def input_powheg_to_hto4l_file_name (self)
 Get input Hto4l file name. More...
 
def output_hto4l4e_file_name (self)
 Get output Hto4l file name for 4e. More...
 
def output_hto4l4mu_file_name (self)
 Get output Hto4l file name for 4mu. More...
 
def output_hto4l2e2mu_file_name (self)
 Get output Hto4l file name for 2e2mu. More...
 
def output_hto4l4e_file_name (self, value)
 Set output Hto4l file name for 4e. More...
 
def output_hto4l4mu_file_name (self, value)
 Set output Hto4l file name for 4mu. More...
 
def output_hto4l2e2mu_file_name (self, value)
 Set output Hto4l file name for 2e2mu. More...
 
def random_seed (self)
 
def random_seed (self, value)
 
def logger (self)
 Get handle to logger. More...
 
def merge_lhe_files (self, listOfFiles, outputFile)
 

Private Attributes

 __output_events_file_name
 This needs to be set so that Generate_trf finds an appropriate file format for showering. More...
 
 __output_hto4l4e_file_name
 Using default output names from PowhegConfig_base and Hto4lConfig. More...
 
 __output_hto4l4mu_file_name
 
 __output_hto4l2e2mu_file_name
 
 __random_seed
 
 __input_powheg_to_hto4l_file_name
 
 __input_powheg_file_name
 

Static Private Attributes

 __run_directory = os.environ['PATH']
 Set up run directory and path to Hto4l. More...
 
 __logger = Logging.logging.getLogger('Hto4lPowhegMerger')
 Setup athena-compatible logger. More...
 
string _merger_executable = 'mergeHto4l4f.exe'
 This must be defined by each derived class - don't change it in the jobOptions! More...
 

Detailed Description

Base class for configurable objects in the jobOptions.

All subprocesses inherit from this class

Definition at line 13 of file Hto4lPowhegMerge.py.

Constructor & Destructor Documentation

◆ __init__()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.__init__ (   self,
  runArgs = None,
  opts = None 
)

Definition at line 23 of file Hto4lPowhegMerge.py.

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 

Member Function Documentation

◆ input_powheg_file_name() [1/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.input_powheg_file_name (   self)

Get input Powheg file name.

Definition at line 216 of file Hto4lPowhegMerge.py.

216  def input_powheg_file_name(self) :
217  return self.__input_powheg_file_name
218 

◆ input_powheg_file_name() [2/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.input_powheg_file_name (   self,
  value 
)

Set input Powheg file name.

Definition at line 221 of file Hto4lPowhegMerge.py.

221  def input_powheg_file_name(self, value) :
222  self.__input_powheg_file_name = value
223 

◆ input_powheg_to_hto4l_file_name()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.input_powheg_to_hto4l_file_name (   self)

Get input Hto4l file name.

Definition at line 226 of file Hto4lPowhegMerge.py.

226  def input_powheg_to_hto4l_file_name(self) :
227  return self.__input_powheg_to_hto4l_file_name
228 

◆ logger()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.logger (   self)

Get handle to logger.

Definition at line 269 of file Hto4lPowhegMerge.py.

269  def logger(self) :
270  return self.__logger
271 

◆ merge()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.merge (   self)

Initialise runcard with generic options.

Definition at line 97 of file Hto4lPowhegMerge.py.

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
117  self.reweight_for_negative_weights(self.output_events_file_name)
118 
119  self.logger.info( 'Fixed weights in file: {}'.format( self.output_events_file_name ) )
120 
121 
122  return
123 

◆ merge_lhe_files()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.merge_lhe_files (   self,
  listOfFiles,
  outputFile 
)

Definition at line 274 of file Hto4lPowhegMerge.py.

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 

◆ output_events_file_name()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_events_file_name (   self)

Get output file name.

Definition at line 211 of file Hto4lPowhegMerge.py.

211  def output_events_file_name(self) :
212  return self.__output_events_file_name
213 

◆ output_hto4l2e2mu_file_name() [1/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l2e2mu_file_name (   self)

Get output Hto4l file name for 2e2mu.

Definition at line 241 of file Hto4lPowhegMerge.py.

241  def output_hto4l2e2mu_file_name(self) :
242  return self.__output_hto4l2e2mu_file_name
243 

◆ output_hto4l2e2mu_file_name() [2/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l2e2mu_file_name (   self,
  value 
)

Set output Hto4l file name for 2e2mu.

Definition at line 256 of file Hto4lPowhegMerge.py.

256  def output_hto4l2e2mu_file_name(self, value) :
257  self.__output_hto4l2e2mu_file_name = value
258 

◆ output_hto4l4e_file_name() [1/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l4e_file_name (   self)

Get output Hto4l file name for 4e.

Definition at line 231 of file Hto4lPowhegMerge.py.

231  def output_hto4l4e_file_name(self) :
232  return self.__output_hto4l4e_file_name
233 

◆ output_hto4l4e_file_name() [2/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l4e_file_name (   self,
  value 
)

Set output Hto4l file name for 4e.

Definition at line 246 of file Hto4lPowhegMerge.py.

246  def output_hto4l4e_file_name(self, value) :
247  self.__output_hto4l4e_file_name = value
248 

◆ output_hto4l4mu_file_name() [1/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l4mu_file_name (   self)

Get output Hto4l file name for 4mu.

Definition at line 236 of file Hto4lPowhegMerge.py.

236  def output_hto4l4mu_file_name(self) :
237  return self.__output_hto4l4mu_file_name
238 

◆ output_hto4l4mu_file_name() [2/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.output_hto4l4mu_file_name (   self,
  value 
)

Set output Hto4l file name for 4mu.

Definition at line 251 of file Hto4lPowhegMerge.py.

251  def output_hto4l4mu_file_name(self, value) :
252  self.__output_hto4l4mu_file_name = value
253 

◆ random_seed() [1/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.random_seed (   self)

Definition at line 260 of file Hto4lPowhegMerge.py.

260  def random_seed(self) :
261  return self.__random_seed
262 

◆ random_seed() [2/2]

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.random_seed (   self,
  value 
)

Definition at line 264 of file Hto4lPowhegMerge.py.

264  def random_seed(self, value) :
265  self.__random_seed = value
266 

◆ reweight_for_negative_weights()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.reweight_for_negative_weights (   self,
  powheg_LHE_output 
)
Post-process the LHE file to update the weights for a negative Hto4l weight.
We do two things:
  1) check if the event weight (XWGTUP) is -1 (can be +/-1). If so, remember this so that the 
 weights in the <rwgt> block can be corrected by multiplying by -1
  2) After correcting the weights in the <rwgt> block, overwrite XWGTUP by the weight at id=0.
 Not completely sure if this step is necessary, but is done for Prophecy4f as well.
Note: we do NOT save the original XWGTUP as an extra weight (i.e. +/-1). Normally, the sign of 
any weight should indicate this. The one exception is when BOTH PowHeg and Hto4l has a negative 
weight and so all weights are positive. This is pretty rare.

#@param powheg_LHE_output  Name of LHE file produced by merge of Hto4l files.

@author RD Schaffer <r.d.schaffer@cern.ch>
#

Definition at line 124 of file Hto4lPowhegMerge.py.

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 

◆ setUpInput()

def python.Hto4lPowhegMerge.Hto4lPowhegMerge.setUpInput (   self)

Initialise runcard with generic options.

Definition at line 51 of file Hto4lPowhegMerge.py.

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"
90  self.merge_lhe_files(allFiles, self.input_powheg_to_hto4l_file_name)
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 

Member Data Documentation

◆ __input_powheg_file_name

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__input_powheg_file_name
private

Definition at line 222 of file Hto4lPowhegMerge.py.

◆ __input_powheg_to_hto4l_file_name

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__input_powheg_to_hto4l_file_name
private

Definition at line 89 of file Hto4lPowhegMerge.py.

◆ __logger

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__logger = Logging.logging.getLogger('Hto4lPowhegMerger')
staticprivate

Setup athena-compatible logger.

Definition at line 18 of file Hto4lPowhegMerge.py.

◆ __output_events_file_name

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_events_file_name
private

This needs to be set so that Generate_trf finds an appropriate file format for showering.

Definition at line 26 of file Hto4lPowhegMerge.py.

◆ __output_hto4l2e2mu_file_name

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_hto4l2e2mu_file_name
private

Definition at line 33 of file Hto4lPowhegMerge.py.

◆ __output_hto4l4e_file_name

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_hto4l4e_file_name
private

Using default output names from PowhegConfig_base and Hto4lConfig.

Definition at line 31 of file Hto4lPowhegMerge.py.

◆ __output_hto4l4mu_file_name

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__output_hto4l4mu_file_name
private

Definition at line 32 of file Hto4lPowhegMerge.py.

◆ __random_seed

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__random_seed
private

Definition at line 34 of file Hto4lPowhegMerge.py.

◆ __run_directory

python.Hto4lPowhegMerge.Hto4lPowhegMerge.__run_directory = os.environ['PATH']
staticprivate

Set up run directory and path to Hto4l.

Definition at line 15 of file Hto4lPowhegMerge.py.

◆ _merger_executable

string python.Hto4lPowhegMerge.Hto4lPowhegMerge._merger_executable = 'mergeHto4l4f.exe'
staticprivate

This must be defined by each derived class - don't change it in the jobOptions!

Definition at line 21 of file Hto4lPowhegMerge.py.


The documentation for this class was generated from the following file:
grepfile.info
info
Definition: grepfile.py:38
mc.random_seed
random_seed
Definition: mc.PhPy8EG_Hto4l_NNLOPS_nnlo_30_ggH125_ZZ4l.py:43
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
skel.merge_lhe_files
def merge_lhe_files(listOfFiles, outputFile)
Check that there is exactly 1 match.
Definition: skel.GENtoEVGEN.py:652
python.processes.powheg.ZZ.logger
logger
Get handle to Athena logging.
Definition: ZZ.py:7
mc.input_powheg_file_name
input_powheg_file_name
Definition: mc.PhPy8EG_Hto4l_NNLOPS_nnlo_30_ggH125_ZZ4l.py:30
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
Trk::open
@ open
Definition: BinningType.h:40
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
python.utility.LHE.merge
def merge(input_file_pattern, output_file)
Merge many input LHE files into a single output file.
Definition: LHE.py:29
str
Definition: BTagTrackIpAccessor.cxx:11
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
ProphecyPowhegCommon.output_events_file_name
output_events_file_name
Definition: ProphecyPowhegCommon.py:18