SplitEvents: split.pl.txt

File split.pl.txt, 4.9 KB (added by anonymous, 7 years ago)
Line 
1#!/usr/bin/perl -w
2
3################################################################################
4#                                                                             ##
5#                    MadGraph/MadEvent                                        ##
6#                                                                             ##
7# FILE : split.pl                                                             ##
8# VERSION : 1.0                                                               ##
9# DATE : 23 December 2007                                                     ##
10# AUTHOR : Michel Herquet (UCL-CP3)                                           ##
11#                                                                             ##
12# DESCRIPTION : script to split LHE events files                              ##
13# USAGE :                                                                     ##
14# ./split.pl percentage events1.lhe.gz events2.lhe.gz events.lhe.gz            ##                                                   ##
15################################################################################
16
17use POSIX qw(ceil);
18
19if ( $#ARGV < 3 ) {
20     die "This script must be called with at least four arguments!\n";
21}
22
23my $perc=$ARGV[0];
24my $outfile1=$ARGV[1];
25my $outfile2=$ARGV[2];
26my $infile=$ARGV[3];
27
28if ( $perc > 100 || $perc < 0) {
29     die "The first argument should range from 0 to 100\n";
30}
31
32print "Unzipping input file...\n";
33
34system("gunzip $infile")==0 || die "Error while unzipping $infile, stopping\n";
35$infile=~ s/\.gz//;
36
37print "Reading input file...\n";
38
39open(INFILE,"$infile") || die "Cannot open input file called $infile, stopping\n";
40
41while(<INFILE>)
42{
43    $fulltext .= $_;
44}
45
46close(INFILE);
47
48print "Rezipping input file...\n";
49
50system("gzip $infile")==0 || die "Error while rezipping $infile, stopping\n";
51
52if ($fulltext =~ m/<header>/) {
53my $begin_tag='<header>'."\n";
54my $end_tag='</header>';
55} else {
56my $begin_tag='<!--'."\n";
57my $end_tag='-->';
58}
59
60my $begin_init='<init>'."\n";
61my $end_init='</init>';
62my $begin_events='</init>'."\n";
63my $end_events='</LesHouchesEvents>';
64
65($banner)= $fulltext=~ m/$begin_tag(.*)$end_tag/s;
66($init)= $fulltext=~ m/$begin_init(.*)$end_init/s;
67($events)= $fulltext=~ m/$begin_events(.*)$end_events/s;
68
69($num_events)= $banner=~ m/#  Number of Events\s*:(.*)\n/;
70($xsec)= $banner=~ m/#  Integrated weight \(pb\)\s*:(.*)\n/;
71$num_events =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
72$xsec =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
73$disp_xsec=sprintf('%0.5E',$xsec);
74
75print "File $infile read with $num_events events and $xsec xsec\n";
76
77$num_events1=ceil($perc*$num_events/100);
78$num_events2=$num_events-$num_events1;
79
80print "Now outputting $outfile1 with $num_events1 events and $outfile2 with $num_events2 events\n";
81$outfile1=~ s/\.gz//;
82$outfile2=~ s/\.gz//;
83open(OUTFILE1,">$outfile1") || die "Cannot open output file called $outfile1, stopping\n";
84open(OUTFILE2,">$outfile2") || die "Cannot open output file called $outfile2, stopping\n";
85
86
87$banner1=$banner;
88
89$uwgt1=sprintf('%0.5E',$xsec/$num_events1);
90
91$banner1=~ s/#  Integrated weight \(pb\)\s*:(.*)\n/#  Integrated weight (pb)  :  $disp_xsec\n/;
92$banner1=~ s/#  Number of Events\s*:(.*)\n/#  Number of Events        :  $num_events1\n/;
93$banner1=~ s/#  Unit wgt\s*:(.*)\n/#  Unit wgt                :  $uwgt1\n/;
94
95$banner2=$banner;
96
97$uwgt2=sprintf('%0.5E',$xsec/$num_events2);
98
99$banner2=~ s/#  Integrated weight \(pb\)\s*:(.*)\n/#  Integrated weight (pb)  :  $disp_xsec\n/;
100$banner2=~ s/#  Number of Events\s*:(.*)\n/#  Number of Events        :  $num_events2\n/;
101$banner2=~ s/#  Unit wgt\s*:(.*)\n/#  Unit wgt                :  $uwgt2\n/;
102
103print OUTFILE1 "<LesHouchesEvents version=\"1.0\">\n";
104print OUTFILE1 $begin_tag;
105print OUTFILE1 $banner1;
106print OUTFILE1 $end_tag."\n";
107print OUTFILE1 "<init>\n";
108print OUTFILE1 $init;
109print OUTFILE1 "</init>\n";
110
111print OUTFILE2 "<LesHouchesEvents version=\"1.0\">\n";
112print OUTFILE2 $begin_tag;
113print OUTFILE2 $banner2;
114print OUTFILE2 $end_tag."\n";
115print OUTFILE2 "<init>\n";
116print OUTFILE2 $init;
117print OUTFILE2 "</init>\n";
118
119@events = split('\n',$events);
120
121$newblock = 0;
122$i=0;
123
124foreach (@events) {
125
126        $curr=$_;
127       
128        if($newblock==1) {
129                $curr =~ s/^\s+//;
130                @param=split(/\s+/,$curr);
131                if ($#param != 5) { die "Not right number of param in first line of event"; }
132                if ($i<=$num_events1) {
133                        $curr=" $param[0] $param[1] $uwgt1 $param[3] $param[4] $param[5]";
134                } else {
135                        $curr=" $param[0] $param[1] $uwgt2 $param[3] $param[4] $param[5]";
136                }
137                $newblock=0;
138        }
139       
140        if($curr =~ m/<event>/) {$newblock=1;$i++;}
141       
142        if ($i<=$num_events1) {
143                print OUTFILE1 $curr."\n";
144        } else {
145                print OUTFILE2 $curr."\n";
146        }
147
148}
149
150print OUTFILE1 "</LesHouchesEvents>\n";
151print OUTFILE2 "</LesHouchesEvents>\n";
152
153close(OUTFILE1);
154close(OUTFILE2);
155
156print "Zipping output files...\n";
157
158system("gzip -f $outfile1")==0 || die "Error while zipping $outfile1, stopping\n";
159system("gzip -f $outfile2")==0 || die "Error while zipping $outfile2, stopping\n";
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177