11 |
|
|
12 |
|
my %opt; |
13 |
|
Getopt::Long::Configure ("bundling"); |
14 |
< |
GetOptions (\%opt, "prefix|p=s", "weight|w=s", "help|h"); |
14 |
> |
GetOptions (\%opt, "cutflow|c=s", "luminosity|l=s", "prefix|p=s", "weight|w=s", "xsection|x=s", "help|h"); |
15 |
|
|
16 |
|
printHelp () if $opt{"help"} || !$opt{"prefix"}; |
17 |
|
my $files = processArgs (\@ARGV); |
18 |
|
my @rootFiles; |
19 |
|
my @weights; |
20 |
< |
my @goodJobs; |
21 |
< |
my @badJobs; |
22 |
< |
my %runList; |
20 |
> |
my $nGoodJobs = 0; |
21 |
> |
my $nBadJobs = 0; |
22 |
|
my $counting = 0; |
23 |
+ |
my %exitCodes; |
24 |
+ |
my %signals; |
25 |
+ |
my %crossSections; |
26 |
+ |
my $integratedLuminosity = 10000; |
27 |
+ |
$integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"}; |
28 |
+ |
my $cutFlow = "cutFlow"; |
29 |
+ |
$cutFlow = $opt{"cutflow"} if $opt{"cutflow"}; |
30 |
|
foreach my $file (@$files) |
31 |
|
{ |
32 |
|
next if $file eq "."; |
33 |
|
next if $file eq ".."; |
34 |
< |
if ($file =~ m/^.*\/condor_.*\.log$/) |
34 |
> |
my $dir = $file; |
35 |
> |
$dir =~ s/^(.*)\/[^\/]*$/$1/; |
36 |
> |
if ($file =~ m/^.*\/condor_[^_]*\.log$/) |
37 |
|
{ |
38 |
|
my $jobNumber = $file; |
39 |
|
$jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/; |
42 |
|
close (FILE); |
43 |
|
my $fileContents = join ("", @fileContents); |
44 |
|
$fileContents =~ s/\n/ /g; |
45 |
< |
$fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g; |
38 |
< |
push (@goodJobs, $jobNumber) if $fileContents == 0; |
39 |
< |
if ($fileContents != 0) |
45 |
> |
if ($fileContents =~ m/return value/) |
46 |
|
{ |
47 |
< |
push (@badJobs, $jobNumber); |
48 |
< |
print "Skipping job $jobNumber. (return value $fileContents)\n"; |
47 |
> |
$fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g; |
48 |
> |
$nGoodJobs++; |
49 |
> |
print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0; |
50 |
> |
$exitCodes{$dir}{$jobNumber} = $fileContents; |
51 |
> |
$counting = 1; |
52 |
> |
} |
53 |
> |
if ($fileContents =~ m/signal/) |
54 |
> |
{ |
55 |
> |
$fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g; |
56 |
> |
$nBadJobs++; |
57 |
> |
print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n"; |
58 |
> |
$signals{$dir}{$jobNumber} = $fileContents; |
59 |
> |
$counting = 1; |
60 |
|
} |
61 |
|
} |
62 |
< |
if ($file =~ m/^.*\/runList\.py$/) |
62 |
> |
if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/) |
63 |
|
{ |
64 |
< |
my $directory = $file; |
65 |
< |
$directory =~ s/^(.*)\/runList\.py$/$1/; |
66 |
< |
$runList{$directory} = getRunList ($file); |
67 |
< |
$counting = 1; |
64 |
> |
open (CROSS_SECTION, "<$file"); |
65 |
> |
my $crossSection = <CROSS_SECTION>; |
66 |
> |
close (CROSS_SECTION); |
67 |
> |
$crossSections{$dir} = $crossSection; |
68 |
|
} |
69 |
|
} |
53 |
– |
my $nJobs = @goodJobs; |
54 |
– |
$nJobs += @badJobs; |
55 |
– |
my $goodEvents = 0; |
56 |
– |
my $goodLumis = 0; |
57 |
– |
my $goodRuns = 0; |
58 |
– |
my $badEvents = 0; |
59 |
– |
my $badLumis = 0; |
60 |
– |
my $badRuns = 0; |
70 |
|
foreach my $file (@$files) |
71 |
|
{ |
72 |
|
next if $file eq "."; |
73 |
|
next if $file eq ".."; |
74 |
+ |
my $dir = $file; |
75 |
+ |
$dir =~ s/^(.*)\/[^\/]*$/$1/; |
76 |
|
my $badJob = 0; |
77 |
|
if ($file =~ m/^.*_[^_]*\.root$/) |
78 |
|
{ |
79 |
|
my $jobNumber = $file; |
69 |
– |
my $directory = $file; |
80 |
|
$jobNumber =~ s/^.*_([^_]*)\.root$/$1/; |
81 |
< |
$directory =~ s/^(.*)\/[^\/]*\.root$/$1/; |
72 |
< |
foreach my $badJobNumber (@badJobs) |
73 |
< |
{ |
74 |
< |
$badJob = 1 if $jobNumber = $badJobNumber; |
75 |
< |
} |
76 |
< |
countEvents ($runList{$directory}, $jobNumber, $nJobs, \$goodEvents, \$goodLumis, \$goodRuns) if !$badJob && defined $runList{$directory}; |
77 |
< |
countEvents ($runList{$directory}, $jobNumber, $nJobs, \$badEvents, \$badLumis, \$badRuns) if $badJob && defined $runList{$directory}; |
81 |
> |
$badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber}; |
82 |
|
} |
83 |
|
next if $badJob; |
84 |
|
if ($file =~ m/^.*\.root$/) |
85 |
|
{ |
86 |
|
push (@rootFiles, $file); |
87 |
< |
push (@weights, $opt{"weight"}) if $opt{"weight"}; |
84 |
< |
push (@weights, 1.0) if !$opt{"weight"}; |
87 |
> |
push (@weights, 1.0); |
88 |
|
} |
89 |
|
} |
87 |
– |
my $nGoodJobs = @goodJobs; |
88 |
– |
my $nBadJobs = @badJobs; |
89 |
– |
print "$nGoodJobs ran successfully over $goodEvents events, $goodLumis lumis, $goodRuns runs\n" if $counting; |
90 |
– |
print "$nBadJobs failed to run over $badEvents events, $badLumis lumis, $badRuns runs\n" if $counting; |
90 |
|
if (!@rootFiles) |
91 |
|
{ |
92 |
|
print "Found no ROOT files to merge!\n"; |
95 |
|
my $rootFiles = join (" ", @rootFiles); |
96 |
|
my $weights = join (",", @weights); |
97 |
|
system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights"); |
98 |
< |
system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex"); |
98 |
> |
my $nTotalEvents = countEvents ("$opt{'prefix'}.root", $cutFlow); |
99 |
> |
unlink ("$opt{'prefix'}.root"); |
100 |
> |
@weights = (); |
101 |
> |
my %weights = (); |
102 |
> |
foreach my $file (@$files) |
103 |
> |
{ |
104 |
> |
next if $file eq "."; |
105 |
> |
next if $file eq ".."; |
106 |
> |
my $dir = $file; |
107 |
> |
$dir =~ s/^(.*)\/[^\/]*$/$1/; |
108 |
> |
my $badJob = 0; |
109 |
> |
if ($file =~ m/^.*_[^_]*\.root$/) |
110 |
> |
{ |
111 |
> |
my $jobNumber = $file; |
112 |
> |
$jobNumber =~ s/^.*_([^_]*)\.root$/$1/; |
113 |
> |
$badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber}; |
114 |
> |
} |
115 |
> |
next if $badJob; |
116 |
> |
if ($file =~ m/^.*\.root$/) |
117 |
> |
{ |
118 |
> |
push (@weights, $opt{"weight"}) if $opt{"weight"}; |
119 |
> |
push (@weights, ($opt{"xsection"} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && $opt{"xsection"}; |
120 |
> |
push (@weights, ($crossSections{$dir} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir}; |
121 |
> |
push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir}); |
122 |
> |
foreach my $arg (@ARGV) |
123 |
> |
{ |
124 |
> |
if (substr ($file, 0, length ($arg)) eq $arg) |
125 |
> |
{ |
126 |
> |
$arg =~ s/^.*\/([^\/]*)$/$1/; |
127 |
> |
$weights{$arg} = $weights[-1] |
128 |
> |
} |
129 |
> |
} |
130 |
> |
} |
131 |
> |
} |
132 |
> |
my $weights = join (",", @weights); |
133 |
> |
system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights"); |
134 |
> |
print "Weights:\n"; |
135 |
> |
foreach my $arg (keys %weights) |
136 |
> |
{ |
137 |
> |
printf " $arg: %.1f (%.1f pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity; |
138 |
> |
} |
139 |
> |
my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow); |
140 |
> |
print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting; |
141 |
> |
print "$nBadJobs jobs failed to run.\n" if $counting; |
142 |
|
|
143 |
|
sub |
144 |
|
processArgs |
182 |
|
my $exeName = $0; |
183 |
|
$exeName =~ s/^.*\/([^\/]*)$/$1/; |
184 |
|
|
185 |
< |
print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n"; |
186 |
< |
print "Merges ROOT files containing histograms and produces a cutflow table in the\n"; |
187 |
< |
print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n"; |
188 |
< |
print "Condor logs in the specified directories, checks for nonzero return values.\n"; |
185 |
> |
print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n"; |
186 |
> |
print "Merges ROOT files containing histograms. If there are Condor logs in the\n"; |
187 |
> |
print "specified directories, checks for nonzero return values. If the directories\n"; |
188 |
> |
print "where created by \"osusub\", uses the cross section from the database to weight\n"; |
189 |
> |
print "all histograms.\n"; |
190 |
|
print "\n"; |
191 |
|
print "Mandatory arguments to long options are mandatory for short options too.\n"; |
192 |
+ |
printf "%-29s%s\n", " -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:"; |
193 |
+ |
printf "%-29s%s\n", " ", "cutFlow)"; |
194 |
|
printf "%-29s%s\n", " -h, --help", "print this help message"; |
195 |
< |
printf "%-29s%s\n", " -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex"; |
196 |
< |
printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT"; |
195 |
> |
printf "%-29s%s\n", " -l, --luminosity", "integrated luminosity to which the histograms are"; |
196 |
> |
printf "%-29s%s\n", " ", "weighted (default: 10000/pb)"; |
197 |
> |
printf "%-29s%s\n", " -p, --prefix PREFIX", "output is named PREFIX.root"; |
198 |
> |
printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the"; |
199 |
> |
printf "%-29s%s\n", " ", "automatic weighting using the cross section from"; |
200 |
> |
printf "%-29s%s\n", " ", "the database"; |
201 |
> |
printf "%-29s%s\n", " -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of"; |
202 |
> |
printf "%-29s%s\n", " ", "the value in the database"; |
203 |
|
|
204 |
|
exit; |
205 |
|
} |
226 |
|
sub |
227 |
|
countEvents |
228 |
|
{ |
229 |
< |
my $runList = shift; |
230 |
< |
my $jobNumber = shift; |
231 |
< |
my $nJobs = shift; |
232 |
< |
my $events = shift; |
233 |
< |
my $lumis = shift; |
183 |
< |
my $runs = shift; |
184 |
< |
|
185 |
< |
my $filesPerJob = ceil (@$runList / $nJobs); |
186 |
< |
print "Job $jobNumber ran successfully on $filesPerJob files.\n"; |
187 |
< |
my @subRunList = @$runList[($jobNumber * $filesPerJob)..($jobNumber * $filesPerJob + $filesPerJob - 1)]; |
188 |
< |
foreach my $file (@subRunList) |
229 |
> |
my $file = shift; |
230 |
> |
my $cutFlow = shift; |
231 |
> |
|
232 |
> |
my $output = `getEventsFromCutFlow $file $cutFlow`; |
233 |
> |
if ($output =~ m/Did not find a histogram named/) |
234 |
|
{ |
235 |
< |
my @output = `edmFileUtil $file`; |
236 |
< |
my $output = join (" ", @output); |
192 |
< |
$output =~ s/\n//g; |
193 |
< |
my $eventsStr = $output; |
194 |
< |
my $lumisStr = $output; |
195 |
< |
my $runsStr = $output; |
196 |
< |
$eventsStr =~ s/^.*\([^ ]* runs, [^ ]* lumis, ([^ ]*) events, [^ ]* bytes\).*$/$1/; |
197 |
< |
$lumisStr =~ s/^.*\([^ ]* runs, ([^ ]*) lumis, [^ ]* events, [^ ]* bytes\).*$/$1/; |
198 |
< |
$runsStr =~ s/^.*\(([^ ]*) runs, [^ ]* lumis, [^ ]* events, [^ ]* bytes\).*$/$1/; |
199 |
< |
$$events += $eventsStr; |
200 |
< |
$$lumis += $lumisStr; |
201 |
< |
$$runs += $runsStr; |
235 |
> |
print $output; |
236 |
> |
return -1; |
237 |
|
} |
238 |
+ |
$output =~ s/^.*: (.*)$/$1/; |
239 |
+ |
$output =~ s/\n//g; |
240 |
+ |
|
241 |
+ |
return $output; |
242 |
|
} |