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; |
23 |
< |
my %runList; |
21 |
> |
my $nGoodJobs = 0; |
22 |
> |
my $nBadJobs = 0; |
23 |
> |
my $nIncompleteJobs = 0; |
24 |
|
my $counting = 0; |
25 |
+ |
my %exitCodes; |
26 |
+ |
my %signals; |
27 |
+ |
my %partial; |
28 |
+ |
my %crossSections; |
29 |
+ |
my %dirs; |
30 |
+ |
my $integratedLuminosity = 10000; |
31 |
+ |
$integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"}; |
32 |
+ |
my $cutFlow = "cutFlow"; |
33 |
+ |
$cutFlow = $opt{"cutflow"} if $opt{"cutflow"}; |
34 |
|
foreach my $file (@$files) |
35 |
|
{ |
36 |
|
next if $file eq "."; |
37 |
|
next if $file eq ".."; |
38 |
< |
if ($file =~ m/^.*\/condor_.*\.log$/) |
38 |
> |
my $dir = $file; |
39 |
> |
$dir =~ s/^(.*)\/[^\/]*$/$1/; |
40 |
> |
if ($file =~ m/^.*\/condor_[^_]*\.log$/) |
41 |
|
{ |
42 |
|
my $jobNumber = $file; |
43 |
|
$jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/; |
46 |
|
close (FILE); |
47 |
|
my $fileContents = join ("", @fileContents); |
48 |
|
$fileContents =~ s/\n/ /g; |
49 |
< |
$fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g; |
50 |
< |
push (@goodJobs, $jobNumber) if $fileContents == 0; |
51 |
< |
if ($fileContents != 0) |
49 |
> |
$counting = 1; |
50 |
> |
if ($fileContents =~ m/return value/) |
51 |
> |
{ |
52 |
> |
$fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g; |
53 |
> |
$nGoodJobs++; |
54 |
> |
print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0; |
55 |
> |
$exitCodes{$dir}{$jobNumber} = $fileContents; |
56 |
> |
} |
57 |
> |
elsif ($fileContents =~ m/signal/) |
58 |
|
{ |
59 |
< |
push (@badJobs, $jobNumber); |
60 |
< |
print "Skipping job $jobNumber. (return value $fileContents)\n"; |
59 |
> |
$fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g; |
60 |
> |
$nBadJobs++; |
61 |
> |
print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n"; |
62 |
> |
$signals{$dir}{$jobNumber} = $fileContents; |
63 |
> |
} |
64 |
> |
else |
65 |
> |
{ |
66 |
> |
$nIncompleteJobs++; |
67 |
> |
$partial{$dir}{$jobNumber} = 1; |
68 |
|
} |
69 |
|
} |
70 |
< |
if ($file =~ m/^.*\/runList\.py$/) |
70 |
> |
if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/) |
71 |
|
{ |
72 |
< |
my $directory = $file; |
73 |
< |
$directory =~ s/^(.*)\/runList\.py$/$1/; |
74 |
< |
$runList{$directory} = getRunList ($file); |
75 |
< |
$counting = 1; |
72 |
> |
open (CROSS_SECTION, "<$file"); |
73 |
> |
my $crossSection = <CROSS_SECTION>; |
74 |
> |
close (CROSS_SECTION); |
75 |
> |
$crossSections{$dir} = $crossSection; |
76 |
|
} |
77 |
|
} |
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; |
78 |
|
foreach my $file (@$files) |
79 |
|
{ |
80 |
|
next if $file eq "."; |
81 |
|
next if $file eq ".."; |
82 |
+ |
my $dir = $file; |
83 |
+ |
$dir =~ s/^(.*)\/[^\/]*$/$1/; |
84 |
|
my $badJob = 0; |
85 |
+ |
my $jobNumber; |
86 |
|
if ($file =~ m/^.*_[^_]*\.root$/) |
87 |
|
{ |
88 |
< |
my $jobNumber = $file; |
69 |
< |
my $directory = $file; |
88 |
> |
$jobNumber = $file; |
89 |
|
$jobNumber =~ s/^.*_([^_]*)\.root$/$1/; |
90 |
< |
$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}; |
90 |
> |
$badJob = (defined $signals{$dir} && defined $signals{$dir}{$jobNumber}) || (defined $partial{$dir} && defined $partial{$dir}{$jobNumber}); |
91 |
|
} |
92 |
|
next if $badJob; |
93 |
|
if ($file =~ m/^.*\.root$/) |
94 |
|
{ |
95 |
< |
push (@rootFiles, $file); |
96 |
< |
push (@weights, $opt{"weight"}) if $opt{"weight"}; |
97 |
< |
push (@weights, 1.0) if !$opt{"weight"}; |
95 |
> |
foreach my $arg (@ARGV) |
96 |
> |
{ |
97 |
> |
if (substr ($file, 0, length ($arg)) eq $arg) |
98 |
> |
{ |
99 |
> |
$dirs{$arg} = $dir; |
100 |
> |
if (countEvents ($file, $cutFlow) < 0) |
101 |
> |
{ |
102 |
> |
$nGoodJobs--; |
103 |
> |
$nBadJobs++; |
104 |
> |
print "WARNING: Skipping job $jobNumber. (bad ROOT file)\n"; |
105 |
> |
last; |
106 |
> |
} |
107 |
> |
push (@{$rootFiles{$arg}}, $file); |
108 |
> |
push (@weights, $opt{"weight"}) if $opt{"weight"}; |
109 |
> |
push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"}; |
110 |
> |
push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir}; |
111 |
> |
push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir}); |
112 |
> |
$weights{$arg} = $weights[-1]; |
113 |
> |
} |
114 |
> |
} |
115 |
|
} |
116 |
|
} |
117 |
< |
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) |
117 |
> |
if (!%rootFiles) |
118 |
|
{ |
119 |
|
print "Found no ROOT files to merge!\n"; |
120 |
|
exit; |
121 |
|
} |
122 |
< |
my $rootFiles = join (" ", @rootFiles); |
123 |
< |
my $weights = join (",", @weights); |
124 |
< |
system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights"); |
125 |
< |
system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex"); |
122 |
> |
my %nTotalEvents; |
123 |
> |
my $nTotalEvents = 0; |
124 |
> |
my @mergedFiles; |
125 |
> |
my @mergedWeights; |
126 |
> |
foreach my $arg (@ARGV) |
127 |
> |
{ |
128 |
> |
my $rootFiles = join (" ", @{$rootFiles{$arg}}); |
129 |
> |
my $tmpName = $arg . "_" . "$opt{'prefix'}.root"; |
130 |
> |
$tmpName =~ s/\//_/g; |
131 |
> |
system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName"); |
132 |
> |
my $count = countEvents ($tmpName, $cutFlow); |
133 |
> |
system ("cutFlowLimits $tmpName"); |
134 |
> |
$nTotalEvents{$arg} = $count; |
135 |
> |
$nTotalEvents += $count; |
136 |
> |
$weights{$arg} /= $count if !$opt{"weight"} && $opt{"xsection"}; |
137 |
> |
$weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}}; |
138 |
> |
push (@mergedFiles, $tmpName); |
139 |
> |
push (@mergedWeights, $weights{$arg}); |
140 |
> |
} |
141 |
> |
my $mergedFiles = join (" ", @mergedFiles); |
142 |
> |
my $mergedWeights = join (",", @mergedWeights); |
143 |
> |
system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights"); |
144 |
> |
foreach my $mergedFile (@mergedFiles) |
145 |
> |
{ |
146 |
> |
unlink ("$mergedFile"); |
147 |
> |
} |
148 |
> |
print "=============================================\n"; |
149 |
> |
my $output = sprintf "Effective luminosities of samples:\n"; |
150 |
> |
my $printOutput = 0; |
151 |
> |
foreach my $arg (keys %weights) |
152 |
> |
{ |
153 |
> |
my $shortArg = $arg; |
154 |
> |
$shortArg =~ s/^.*\/([^\/]*)$/$1/; |
155 |
> |
$output = sprintf "%s $shortArg: %.5g/fb\n", $output, ($integratedLuminosity / (1000.0 * $weights{$arg})) if $weights{$arg} != 1; |
156 |
> |
$printOutput = 1 if $weights{$arg} != 1; |
157 |
> |
} |
158 |
> |
|
159 |
> |
foreach my $arg (keys %weights) |
160 |
> |
{ |
161 |
> |
my $shortArg = $arg; |
162 |
> |
$shortArg =~ s/^.*\/([^\/]*)$/$1/; |
163 |
> |
$output = sprintf "%s $shortArg: CrossSection = %.5g/pb\n", $output, ($crossSections{$dirs{$arg}}); |
164 |
> |
$printOutput = 1 if $weights{$arg} != 1; |
165 |
> |
} |
166 |
> |
|
167 |
> |
print $output if $printOutput; |
168 |
> |
$output = sprintf "Weights for target luminosity of %g/fb:\n", ($integratedLuminosity / 1000.0); |
169 |
> |
$printOutput = 0; |
170 |
> |
foreach my $arg (keys %weights) |
171 |
> |
{ |
172 |
> |
my $shortArg = $arg; |
173 |
> |
$shortArg =~ s/^.*\/([^\/]*)$/$1/; |
174 |
> |
$output = sprintf "%s $shortArg: %.5g\n", $output, $weights{$arg} if $weights{$arg} != 1; |
175 |
> |
$printOutput = 1 if $weights{$arg} != 1; |
176 |
> |
} |
177 |
> |
print $output if $printOutput; |
178 |
> |
my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow); |
179 |
> |
print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting; |
180 |
> |
print "$nBadJobs jobs failed to run.\n" if $counting; |
181 |
> |
print "$nIncompleteJobs jobs have not finished.\n" if $counting; |
182 |
> |
print "=============================================\n"; |
183 |
|
|
184 |
|
sub |
185 |
|
processArgs |
223 |
|
my $exeName = $0; |
224 |
|
$exeName =~ s/^.*\/([^\/]*)$/$1/; |
225 |
|
|
226 |
< |
print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n"; |
227 |
< |
print "Merges ROOT files containing histograms and produces a cutflow table in the\n"; |
228 |
< |
print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n"; |
229 |
< |
print "Condor logs in the specified directories, checks for nonzero return values.\n"; |
226 |
> |
print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n"; |
227 |
> |
print "Merges ROOT files containing histograms. If there are Condor logs in the\n"; |
228 |
> |
print "specified directories, checks for nonzero return values. If the directories\n"; |
229 |
> |
print "where created by \"osusub\", uses the cross section from the database to weight\n"; |
230 |
> |
print "all histograms.\n"; |
231 |
|
print "\n"; |
232 |
|
print "Mandatory arguments to long options are mandatory for short options too.\n"; |
233 |
+ |
printf "%-29s%s\n", " -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:"; |
234 |
+ |
printf "%-29s%s\n", " ", "cutFlow)"; |
235 |
|
printf "%-29s%s\n", " -h, --help", "print this help message"; |
236 |
< |
printf "%-29s%s\n", " -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex"; |
237 |
< |
printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT"; |
236 |
> |
printf "%-29s%s\n", " -l, --luminosity", "integrated luminosity to which the histograms are"; |
237 |
> |
printf "%-29s%s\n", " ", "weighted (default: 10000/pb)"; |
238 |
> |
printf "%-29s%s\n", " -p, --prefix PREFIX", "output is named PREFIX.root"; |
239 |
> |
printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the"; |
240 |
> |
printf "%-29s%s\n", " ", "automatic weighting using the cross section from"; |
241 |
> |
printf "%-29s%s\n", " ", "the database"; |
242 |
> |
printf "%-29s%s\n", " -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of"; |
243 |
> |
printf "%-29s%s\n", " ", "the value in the database"; |
244 |
|
|
245 |
|
exit; |
246 |
|
} |
267 |
|
sub |
268 |
|
countEvents |
269 |
|
{ |
270 |
< |
my $runList = shift; |
271 |
< |
my $jobNumber = shift; |
272 |
< |
my $nJobs = shift; |
273 |
< |
my $events = shift; |
274 |
< |
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) |
270 |
> |
my $file = shift; |
271 |
> |
my $cutFlow = shift; |
272 |
> |
|
273 |
> |
my $output = `getEventsFromCutFlow $file $cutFlow`; |
274 |
> |
if ($output =~ m/Did not find a histogram named/ || $output =~ m/appears to be empty/ || $output =~ m/Failed to open/) |
275 |
|
{ |
276 |
< |
my @output = `edmFileUtil $file`; |
277 |
< |
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; |
276 |
> |
print $output; |
277 |
> |
return -1; |
278 |
|
} |
279 |
+ |
$output =~ s/^.*: (.*)$/$1/; |
280 |
+ |
$output =~ s/\n//g; |
281 |
+ |
|
282 |
+ |
return $output; |
283 |
|
} |