MultiRun: merge.pl.txt

File merge.pl.txt, 5.3 KB (added by anonymous, 7 years ago)
Line 
1#!/usr/bin/perl -w
2
3################################################################################
4#                                                                             ##
5#                    MadGraph/MadEvent                                        ##
6#                                                                             ##
7# FILE : merge.pl                                                             ##
8# VERSION : 1.2                                                               ##
9# DATE : 12 April 2007                                                        ##
10# AUTHOR : Michel Herquet (UCL-CP3)                                           ##
11#                                                                             ##
12# DESCRIPTION : script to merge to LHE events files                           ##
13# USAGE :                                                                     ##
14# ./merge.pl eventfile1.lhe.gz eventfile2.lhe.gz ... result.lhe.gz banner.txt ##
15# Note that result can be eventfile1, eventfile2, ...                         ##
16# OUTPUT : merged file, banner                                                ##
17################################################################################
18
19
20# Parse the command line arguments
21
22if ( $#ARGV < 3 ) {
23     die "This script must be called with at least four filenames as arguments!\n";
24}
25
26# Start by copying the first input file as result
27
28print "Copying first file on output file ... \n";
29system("cp ".$ARGV[0]." ".$ARGV[$#ARGV-1]);
30
31my $infile2=$ARGV[$#ARGV-1];
32my $outfile=$ARGV[$#ARGV-1];
33my $bannerfile=$ARGV[$#ARGV];
34
35# Unzip and open the input files
36print "Unzipping input files ... \n";
37system("gunzip $infile2")==0 || die "Error while unzipping $infile2, stopping\n";
38$infile2=~ s/\.gz//;
39
40# Loop over files starting from the second one, and merge each time with the result (initially the first)
41for($count=1;$count<=$#ARGV-2;$count++) {
42
43my $infile1=$ARGV[$count];
44
45system("gunzip $infile1")==0 || die "Error while unzipping $infile1, stopping\n";
46$infile1=~ s/\.gz//;
47
48print "Reading input files ... \n";
49open(INFILE1,"$infile1") || die "Cannot open input file called $infile1, stopping\n";
50open(INFILE2,"$infile2") || die "Cannot open input file called $infile2, stopping\n";
51
52# Extract banner, init and events from event files
53
54my $begin_tag='<!--'."\n";
55my $end_tag='-->';
56my $begin_init='<init>'."\n";
57my $end_init='</init>';
58my $begin_events='</init>'."\n";
59my $end_events='</LesHouchesEvents>';
60
61my $fulltext1;
62my $fulltext2;
63
64while(<INFILE1>)
65{
66    $fulltext1 .= $_;
67}
68($banner1)= $fulltext1=~ m/$begin_tag(.*)$end_tag/s;
69($init1)= $fulltext1=~ m/$begin_init(.*)$end_init/s;
70($events1)= $fulltext1=~ m/$begin_events(.*)$end_events/s;
71
72while(<INFILE2>)
73{
74    $fulltext2 .= $_;
75}
76
77($banner2)= $fulltext2=~ m/$begin_tag(.*)$end_tag/s;
78#($init2)= $fulltext1=~ m/$begin_init(.*)$end_init/s;
79($events2)= $fulltext2=~ m/$begin_events(.*)$end_events/s;
80
81# Close input files
82close(INFILE1);
83close(INFILE2);
84
85# Scan banners to extract number of events and cross sections
86
87($num_events1)= $banner1=~ m/#  Number of Events\s*:(.*)\n/;
88($xsec1)= $banner1=~ m/#  Integrated weight \(pb\)\s*:(.*)\n/;
89$num_events1 =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
90$xsec1 =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
91
92($num_events2)= $banner2=~ m/#  Number of Events\s*:(.*)\n/;
93($xsec2)= $banner2=~ m/#  Integrated weight \(pb\)\s*:(.*)\n/;
94$num_events2 =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
95$xsec2 =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
96
97print "File $infile1 read with $num_events1 events and $xsec1 xsec\n";
98print "File $infile2 read with $num_events2 events and $xsec2 xsec\n";
99
100# Check that cross sections do not differ too much
101if( abs(($xsec1-$xsec2)/$xsec1) > 0.05 ) { print "\nWARNING Cross sections do not agree with a 5\% precision!\n";}
102
103# Compute total number of events and new unit weight
104$tot_events=$num_events1+$num_events2;
105$xsec=($num_events1*$xsec1+$num_events2*$xsec2)/$tot_events;
106$uwgt=sprintf('%0.5E',$xsec/$tot_events);
107$disp_xsec=sprintf('%0.5E',$xsec);
108
109# Print out the merged file
110print "Creating the merged file with xsec=$disp_xsec, $tot_events events and $uwgt unit wgt ... \n";
111
112my $banner=$banner1;
113my $init=$init1;
114
115
116$banner=~ s/#  Integrated weight \(pb\)\s*:(.*)\n/#  Integrated weight (pb)  :  $disp_xsec\n/;
117$banner=~ s/#  Number of Events\s*:(.*)\n/#  Number of Events        :  $tot_events\n/;
118$banner=~ s/#  Unit wgt\s*:(.*)\n/#  Unit wgt                :  $uwgt\n/;
119
120$outfile=~ s/\.gz//;
121
122open(OUTFILE,">$outfile") || die "Cannot open output file called $outfile for writing, stopping\n";
123
124print OUTFILE "<LesHouchesEvents version=\"1.0\">\n";
125print OUTFILE "<!--\n";
126print OUTFILE $banner;
127print OUTFILE "-->\n";
128print OUTFILE "<init>\n";
129print OUTFILE $init;
130print OUTFILE "</init>\n";
131print OUTFILE $events1;
132print OUTFILE $events2;
133print OUTFILE "</LesHouchesEvents>\n";
134
135close(OUTFILE);
136
137open(BANNER,">$bannerfile") || die "Cannot open output file called $bannerfile for writing, stopping\n";
138print BANNER $banner;
139close(BANNER);
140
141# Zip all remaining unzipped files
142print "Zipping files ... \n";
143if( (-e $infile1) && !($infile1 =~ m/\.gz/) ) { system("gzip $infile1")==0 || die "Error while zipping $infile1\n"; }
144
145}
146if( (-e $infile2) && !($infile2 =~ m/\.gz/) ) { system("gzip $infile2")==0 || die "Error while zipping $infile2\n"; }
147if( (-e $outfile) && !($outfile =~ m/\.gz/) ) { system("gzip $outfile")==0 || die "Error while zipping $outfile\n"; }