ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
(Generate patch)

Comparing UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists (file contents):
Revision 1.1 by ahart, Thu Jun 14 17:42:30 2012 UTC vs.
Revision 1.25 by ahart, Tue Jun 4 23:46:12 2013 UTC

# Line 2 | Line 2
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, "prefix|p=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 $error = 0;
18 > my %rootFiles;
19 > my %weights;
20 > my @weights;
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/\.root$/)
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/;
44 >        open (FILE, "<$file");
45 >        my @fileContents = <FILE>;
46 >        close (FILE);
47 >        my $fileContents = join ("", @fileContents);
48 >        $fileContents =~ s/\n/ /g;
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 >            $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/^.*\/crossSectionInPicobarn\.txt$/)
71 >      {
72 >        open (CROSS_SECTION, "<$file");
73 >        my $crossSection = <CROSS_SECTION>;
74 >        close (CROSS_SECTION);
75 >        $crossSections{$dir} = $crossSection;
76 >      }
77 >  }
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 >        $jobNumber = $file;
89 >        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
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 <        next;
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 >                if ($opt{"weight"})
109 >                  {
110 >                    push (@weights, $opt{"weight"}) if !($opt{"weight"} =~ m/,/);
111 >                    push (@weights, 1.0) if $opt{"weight"} =~ m/,/;
112 >                  }
113 >                push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
114 >                push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
115 >                push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
116 >                $weights{$arg} = $weights[-1];
117 >              }
118 >          }
119        }
120 <    open (FILE, "<$file");
121 <    my @fileContents = <FILE>;
122 <    close (FILE);
123 <    my $fileContents = join ("", @fileContents);
124 <    next if !($fileContents =~ m/\(return value [^)]*\)/);
125 <    $fileContents =~ s/\n/ /g;
126 <    $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
127 <    if ($fileContents != 0)
120 >  }
121 > if (!%rootFiles)
122 >  {
123 >    print "Found no ROOT files to merge!\n";
124 >    exit;
125 >  }
126 > my %nTotalEvents;
127 > my $nTotalEvents = 0;
128 > my @mergedFiles;
129 > my @mergedWeights;
130 > foreach my $arg (@ARGV)
131 >  {
132 >    my $rootFiles = join (" ", @{$rootFiles{$arg}});
133 >    my $tmpName = $arg . "_" . "$opt{'prefix'}.root";
134 >    $tmpName =~ s/\//_/g;
135 >    system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
136 >    my $count = countEvents ($tmpName, $cutFlow);
137 >    system ("cutFlowLimits $tmpName");
138 >    $nTotalEvents{$arg} = $count;
139 >    $nTotalEvents += $count;
140 >    $weights{$arg} /= $count if !$opt{"weight"} && $opt{"xsection"};
141 >    $weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}};
142 >    push (@mergedFiles, $tmpName);
143 >    push (@mergedWeights, $weights{$arg});
144 >    foreach my $rootFile (@{$rootFiles{$arg}})
145        {
146 <        printf "Error: return value of %2d in \"$file\"!\n", $fileContents;
147 <        $error = 1;
146 >        my $weight = $weights{$arg};
147 >        system ("weightTrees $rootFile $weight");
148        }
149    }
150 < exit if $error;
151 < my $rootFiles = join (" ", @rootFiles);
152 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root");
153 < system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
150 > my $mergedFiles = join (" ", @mergedFiles);
151 > my $mergedWeights = join (",", @mergedWeights);
152 > $mergedWeights = $opt{"weight"} if $opt{"weight"} && $opt{"weight"} =~ m/,/;
153 > system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
154 > foreach my $mergedFile (@mergedFiles)
155 >  {
156 >    unlink ("$mergedFile");
157 >  }
158 > print "=============================================\n";
159 > my $output = sprintf "Cross-section of samples:\n";
160 > my $printOutput = 0;
161 > foreach my $arg (keys %weights)
162 > {
163 >    my $shortArg = $arg;
164 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
165 >    $output = sprintf "%s  $shortArg: %.5g pb\n", $output, ($crossSections{$dirs{$arg}});
166 >    $printOutput = 1 if $weights{$arg} != 1;
167 > }
168 > print $output if $printOutput;
169 >
170 > my $output = sprintf "Effective luminosities of samples:\n";
171 > my $printOutput = 0;
172 > foreach my $arg (keys %weights)
173 >  {
174 >    my $shortArg = $arg;
175 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
176 >    $output = sprintf "%s  $shortArg: %.5g/fb\n", $output, ($integratedLuminosity / (1000.0 * $weights{$arg})) if $weights{$arg} != 1;
177 >    $printOutput = 1 if $weights{$arg} != 1;
178 >  }
179 > print $output if $printOutput;
180 >
181 > $output = sprintf "Weights for target luminosity of %g/fb:\n", ($integratedLuminosity / 1000.0);
182 > $printOutput = 0;
183 > foreach my $arg (keys %weights)
184 >  {
185 >    my $shortArg = $arg;
186 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
187 >    $output = sprintf "%s  $shortArg: %.5g\n", $output, $weights{$arg} if $weights{$arg} != 1;
188 >    $printOutput = 1 if $weights{$arg} != 1;
189 >  }
190 > print $output if $printOutput;
191 >
192 > my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
193 > print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
194 > print "$nBadJobs jobs failed to run.\n" if $counting;
195 > print "$nIncompleteJobs jobs have not finished.\n" if $counting;
196 > print "=============================================\n";
197  
198   sub
199   processArgs
# Line 80 | Line 237 | printHelp
237    my $exeName = $0;
238    $exeName =~ s/^.*\/([^\/]*)$/$1/;
239  
240 <  print "Usage: $exeName [OPTIONS] -p PREFIX DIRECTORIES_AND_FILES\n";
241 <  print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
242 <  print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
243 <  print "Condor logs in the specified directories, checks for nonzero return values.\n";
240 >  print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
241 >  print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
242 >  print "specified directories, checks for nonzero return values. If the directories\n";
243 >  print "where created by \"osusub\", uses the cross section from the database to weight\n";
244 >  print "all histograms.\n";
245    print "\n";
246    print "Mandatory arguments to long options are mandatory for short options too.\n";
247 +  printf "%-29s%s\n", "  -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
248 +  printf "%-29s%s\n", "                  ", "cutFlow)";
249    printf "%-29s%s\n", "  -h, --help", "print this help message";
250 <  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
250 >  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
251 >  printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
252 >  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
253 >  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
254 >  printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
255 >  printf "%-29s%s\n", "                     ", "the database; WEIGHT may be a single number or a";
256 >  printf "%-29s%s\n", "                     ", "comma-separated list, one for each input file";
257 >  printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
258 >  printf "%-29s%s\n", "                     ", "the value in the database";
259  
260    exit;
261   }
262 +
263 + sub
264 + getRunList
265 + {
266 +  my $runListFile = shift;
267 +
268 +  open (RUN_LIST, "<$runListFile");
269 +  my @runList0 = <RUN_LIST>;
270 +  close (RUN_LIST);
271 +  my @runList;
272 +  foreach my $file (@runList0)
273 +    {
274 +      next if !($file =~ m/^.*file:.*\.root.*/);
275 +      $file =~ s/.*file:(.*)\.root.*/$1.root/;
276 +      push (@runList, $file);
277 +    }
278 +
279 +  return \@runList;
280 + }
281 +
282 + sub
283 + countEvents
284 + {
285 +  my $file = shift;
286 +  my $cutFlow = shift;
287 +
288 +  my $output = `getEventsFromCutFlow $file $cutFlow`;
289 +  if ($output =~ m/Did not find a histogram named/ || $output =~ m/appears to be empty/ || $output =~ m/Failed to open/)
290 +    {
291 +      print $output;
292 +      return -1;
293 +    }
294 +  $output =~ s/^.*: (.*)$/$1/;
295 +  $output =~ s/\n//g;
296 +
297 +  return $output;
298 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines