ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.13
Committed: Thu Jan 3 21:43:31 2013 UTC (12 years, 4 months ago) by ahart
Branch: MAIN
Changes since 1.12: +20 -39 lines
Log Message:
Do not discard the unweighted merged files, but instead calculate upper limits for their cutflow histograms and then weight them.

File Contents

# Content
1 #!/usr/bin/env perl
2
3 use strict;
4 use Getopt::Long;
5 use POSIX;
6
7 sub processArgs;
8 sub printHelp;
9 sub getRunList;
10 sub countEvents;
11
12 my %opt;
13 Getopt::Long::Configure ("bundling");
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 @weights;
21 my $nGoodJobs = 0;
22 my $nBadJobs = 0;
23 my $counting = 0;
24 my %exitCodes;
25 my %signals;
26 my %crossSections;
27 my $integratedLuminosity = 10000;
28 $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
29 my $cutFlow = "cutFlow";
30 $cutFlow = $opt{"cutflow"} if $opt{"cutflow"};
31 foreach my $file (@$files)
32 {
33 next if $file eq ".";
34 next if $file eq "..";
35 my $dir = $file;
36 $dir =~ s/^(.*)\/[^\/]*$/$1/;
37 if ($file =~ m/^.*\/condor_[^_]*\.log$/)
38 {
39 my $jobNumber = $file;
40 $jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/;
41 open (FILE, "<$file");
42 my @fileContents = <FILE>;
43 close (FILE);
44 my $fileContents = join ("", @fileContents);
45 $fileContents =~ s/\n/ /g;
46 if ($fileContents =~ m/return value/)
47 {
48 $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
49 $nGoodJobs++;
50 print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0;
51 $exitCodes{$dir}{$jobNumber} = $fileContents;
52 $counting = 1;
53 }
54 if ($fileContents =~ m/signal/)
55 {
56 $fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g;
57 $nBadJobs++;
58 print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n";
59 $signals{$dir}{$jobNumber} = $fileContents;
60 $counting = 1;
61 }
62 }
63 if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
64 {
65 open (CROSS_SECTION, "<$file");
66 my $crossSection = <CROSS_SECTION>;
67 close (CROSS_SECTION);
68 $crossSections{$dir} = $crossSection;
69 }
70 }
71 foreach my $file (@$files)
72 {
73 next if $file eq ".";
74 next if $file eq "..";
75 my $dir = $file;
76 $dir =~ s/^(.*)\/[^\/]*$/$1/;
77 my $badJob = 0;
78 if ($file =~ m/^.*_[^_]*\.root$/)
79 {
80 my $jobNumber = $file;
81 $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
82 $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
83 }
84 next if $badJob;
85 if ($file =~ m/^.*\.root$/)
86 {
87 foreach my $arg (@ARGV)
88 {
89 if (substr ($file, 0, length ($arg)) eq $arg)
90 {
91 push (@{$rootFiles{$arg}}, $file);
92 push (@weights, $opt{"weight"}) if $opt{"weight"};
93 push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
94 push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
95 push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
96 $weights{$arg} = $weights[-1];
97 }
98 }
99 }
100 }
101 if (!%rootFiles)
102 {
103 print "Found no ROOT files to merge!\n";
104 exit;
105 }
106 my %nTotalEvents;
107 my $nTotalEvents = 0;
108 my @mergedFiles;
109 my @mergedWeights;
110 foreach my $arg (@ARGV)
111 {
112 my $rootFiles = join (" ", @{$rootFiles{$arg}});
113 my $tmpName = $arg . "_" . "$opt{'prefix'}.root";
114 $tmpName =~ s/\//_/g;
115 system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
116 my $count = countEvents ($tmpName, $cutFlow);
117 system ("cutFlowLimits $tmpName");
118 $nTotalEvents{$arg} = $count;
119 $nTotalEvents += $count;
120 push (@mergedFiles, "$tmpName");
121 push (@mergedWeights, $weights{$arg} / $count);
122 }
123 my $mergedFiles = join (" ", @mergedFiles);
124 my $mergedWeights = join (",", @mergedWeights);
125 system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
126 foreach my $mergedFile (@mergedFiles)
127 {
128 unlink ("$mergedFile");
129 }
130 print "Weights:\n";
131 foreach my $arg (keys %weights)
132 {
133 my $shortArg = $arg;
134 $shortArg =~ s/^.*\/([^\/]*)$/$1/;
135 printf " $shortArg: %.5g (%.5g pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity;
136 }
137 my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
138 print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
139 print "$nBadJobs jobs failed to run.\n" if $counting;
140
141 sub
142 processArgs
143 {
144 my $argv = shift;
145
146 my @files;
147 foreach my $arg (@$argv)
148 {
149 $arg =~ s/\/*$//;
150 if (!(-e $arg))
151 {
152 print "$arg does not exist!\n";
153 exit;
154 }
155 next if ($arg =~ m/\/\.$/ || $arg =~ m/\/\.\.$/);
156 if (-d $arg)
157 {
158 opendir (DIR, $arg);
159 my @dirContents = readdir (DIR);
160 closedir (DIR);
161 for (my $i = 0; $i < @dirContents; $i++)
162 {
163 $dirContents[$i] = "$arg/$dirContents[$i]";
164 }
165 my $newFiles = processArgs (\@dirContents);
166 push (@files, @$newFiles);
167 }
168 else
169 {
170 push (@files, $arg);
171 }
172 }
173
174 return \@files;
175 }
176
177 sub
178 printHelp
179 {
180 my $exeName = $0;
181 $exeName =~ s/^.*\/([^\/]*)$/$1/;
182
183 print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
184 print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
185 print "specified directories, checks for nonzero return values. If the directories\n";
186 print "where created by \"osusub\", uses the cross section from the database to weight\n";
187 print "all histograms.\n";
188 print "\n";
189 print "Mandatory arguments to long options are mandatory for short options too.\n";
190 printf "%-29s%s\n", " -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
191 printf "%-29s%s\n", " ", "cutFlow)";
192 printf "%-29s%s\n", " -h, --help", "print this help message";
193 printf "%-29s%s\n", " -l, --luminosity", "integrated luminosity to which the histograms are";
194 printf "%-29s%s\n", " ", "weighted (default: 10000/pb)";
195 printf "%-29s%s\n", " -p, --prefix PREFIX", "output is named PREFIX.root";
196 printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
197 printf "%-29s%s\n", " ", "automatic weighting using the cross section from";
198 printf "%-29s%s\n", " ", "the database";
199 printf "%-29s%s\n", " -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
200 printf "%-29s%s\n", " ", "the value in the database";
201
202 exit;
203 }
204
205 sub
206 getRunList
207 {
208 my $runListFile = shift;
209
210 open (RUN_LIST, "<$runListFile");
211 my @runList0 = <RUN_LIST>;
212 close (RUN_LIST);
213 my @runList;
214 foreach my $file (@runList0)
215 {
216 next if !($file =~ m/^.*file:.*\.root.*/);
217 $file =~ s/.*file:(.*)\.root.*/$1.root/;
218 push (@runList, $file);
219 }
220
221 return \@runList;
222 }
223
224 sub
225 countEvents
226 {
227 my $file = shift;
228 my $cutFlow = shift;
229
230 my $output = `getEventsFromCutFlow $file $cutFlow`;
231 if ($output =~ m/Did not find a histogram named/)
232 {
233 print $output;
234 return -1;
235 }
236 $output =~ s/^.*: (.*)$/$1/;
237 $output =~ s/\n//g;
238
239 return $output;
240 }