Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-01 23:40:22

0001 #!/usr/bin/env python3
0002 
0003 import logging
0004 import argparse
0005 import sys
0006 import os
0007 import re
0008 
0009 
0010 def number_events(input_file, output_file=None, offset=0):
0011     if output_file is None:
0012         output_file = input_file
0013     if not os.path.exists(os.path.dirname(os.path.realpath(output_file))):
0014         os.makedirs(os.path.dirname(os.path.realpath(output_file)))
0015 
0016     nevent = offset
0017     with open('tmp.txt', 'w') as fw:
0018         with open(input_file, 'r') as ftmp:
0019             for line in ftmp:
0020                 if re.search('\s*</event>', line):
0021                     nevent += 1
0022                     fw.write('<event_num num="' + str(nevent) +  '"> ' + str(nevent) + '</event_num>\n')
0023                 fw.write(line)
0024     if output_file is not None:
0025         os.rename("tmp.txt", output_file)
0026     else:
0027         os.rename("tmp.txt", input_file)
0028     return nevent
0029 
0030 
0031 if __name__=="__main__":
0032 
0033     parser = argparse.ArgumentParser(
0034         description="Add numbers to lhe")
0035     parser.add_argument("input_file", type=str,
0036                         help="Input LHE file path.")
0037     parser.add_argument("-o", "--output-file", default=None, type=str,
0038                         help="Output LHE file path. If not specified, output to input file")
0039     args = parser.parse_args()
0040 
0041     logging.info('>>> launch addLHEnumbers.py in %s' % os.path.abspath(os.getcwd()))
0042 
0043     logging.info('>>> Input file: [%s]' % args.input_file)
0044     logging.info('>>> Write to output: %s ' % args.output_file)
0045 
0046     number_events(args.input_file, args.output_file)