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