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.5 by ahart, Sun Sep 9 20:05:12 2012 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, "luminosity|l=s", "prefix|p=s", "weight|w=s", "help|h");
15  
16   printHelp () if $opt{"help"} || !$opt{"prefix"};
17   my $files = processArgs (\@ARGV);
18   my @rootFiles;
19 < my $error = 0;
19 > my @weights;
20 > my $nGoodJobs = 0;
21 > my $nBadJobs = 0;
22 > my $counting = 0;
23 > my %exitCodes;
24 > my %crossSections;
25 > my $integratedLuminosity = 10000;
26 > $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
27   foreach my $file (@$files)
28    {
29      next if $file eq ".";
30      next if $file eq "..";
31 <    if ($file =~ m/\.root$/)
31 >    my $dir = $file;
32 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
33 >    if ($file =~ m/^.*\/condor_[^_]*\.log$/)
34        {
35 <        push (@rootFiles, $file);
36 <        next;
35 >        my $jobNumber = $file;
36 >        $jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/;
37 >        open (FILE, "<$file");
38 >        my @fileContents = <FILE>;
39 >        close (FILE);
40 >        my $fileContents = join ("", @fileContents);
41 >        $fileContents =~ s/\n/ /g;
42 >        $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
43 >        $nGoodJobs++;
44 >        if ($fileContents != 0)
45 >          {
46 >            $nBadJobs++;
47 >            print "Skipping job $jobNumber. (return value $fileContents)\n";
48 >          }
49 >        $exitCodes{$dir}{$jobNumber} = $fileContents;
50 >        $counting = 1;
51 >      }
52 >    if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
53 >      {
54 >        open (CROSS_SECTION, "<$file");
55 >        my $crossSection = <CROSS_SECTION>;
56 >        close (CROSS_SECTION);
57 >        $crossSections{$dir} = $crossSection;
58 >      }
59 >  }
60 > my $nTotalEvents = 0;
61 > foreach my $file (@$files)
62 >  {
63 >    next if $file eq ".";
64 >    next if $file eq "..";
65 >    my $dir = $file;
66 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
67 >    my $badJob = 0;
68 >    if ($file =~ m/^.*_[^_]*\.root$/)
69 >      {
70 >        my $jobNumber = $file;
71 >        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
72 >        $badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
73        }
74 <    open (FILE, "<$file");
75 <    my @fileContents = <FILE>;
25 <    close (FILE);
26 <    my $fileContents = join ("", @fileContents);
27 <    next if !($fileContents =~ m/\(return value [^)]*\)/);
28 <    $fileContents =~ s/\n/ /g;
29 <    $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
30 <    if ($fileContents != 0)
74 >    next if $badJob;
75 >    if ($file =~ m/^.*\.root$/)
76        {
77 <        printf "Error: return value of %2d in \"$file\"!\n", $fileContents;
78 <        $error = 1;
77 >        my $nEvents = countEvents ($file);
78 >        $nTotalEvents = -1 if $nTotalEvents < 0 || $nEvents < 0;
79 >        $nTotalEvents += $nEvents if !($nTotalEvents < 0 || $nEvents < 0);
80        }
81    }
82 < exit if $error;
82 > foreach my $file (@$files)
83 >  {
84 >    next if $file eq ".";
85 >    next if $file eq "..";
86 >    my $dir = $file;
87 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
88 >    my $badJob = 0;
89 >    if ($file =~ m/^.*_[^_]*\.root$/)
90 >      {
91 >        my $jobNumber = $file;
92 >        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
93 >        $badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
94 >      }
95 >    next if $badJob;
96 >    if ($file =~ m/^.*\.root$/)
97 >      {
98 >        push (@rootFiles, $file);
99 >        push (@weights, $opt{"weight"}) if $opt{"weight"};
100 >        push (@weights, ($crossSections{$dir} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && defined $crossSections{$dir};
101 >        push (@weights, 1.0) if !$opt{"weight"} && !(defined $crossSections{$dir});
102 >      }
103 >  }
104 > if (!@rootFiles)
105 >  {
106 >    print "Found no ROOT files to merge!\n";
107 >    exit;
108 >  }
109   my $rootFiles = join (" ", @rootFiles);
110 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root");
110 > my $weights = join (",", @weights);
111 > system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
112   system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
113 + my $goodEvents = countEvents ("$opt{'prefix'}.root");
114 + print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
115 + print "$nBadJobs jobs failed to run.\n" if $counting;
116  
117   sub
118   processArgs
# Line 80 | Line 156 | printHelp
156    my $exeName = $0;
157    $exeName =~ s/^.*\/([^\/]*)$/$1/;
158  
159 <  print "Usage: $exeName [OPTIONS] -p PREFIX DIRECTORIES_AND_FILES\n";
159 >  print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n";
160    print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
161    print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
162 <  print "Condor logs in the specified directories, checks for nonzero return values.\n";
162 >  print "Condor logs in the specified directories, checks for nonzero return values. If\n";
163 >  print "the directories where created by \"osusub\", uses the cross section from the\n";
164 >  print "database to weight all histograms.\n";
165    print "\n";
166    print "Mandatory arguments to long options are mandatory for short options too.\n";
167    printf "%-29s%s\n", "  -h, --help", "print this help message";
168 +  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
169 +  printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
170    printf "%-29s%s\n", "  -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
171 +  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
172 +  printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
173 +  printf "%-29s%s\n", "                     ", "the database";
174  
175    exit;
176   }
177 +
178 + sub
179 + getRunList
180 + {
181 +  my $runListFile = shift;
182 +
183 +  open (RUN_LIST, "<$runListFile");
184 +  my @runList0 = <RUN_LIST>;
185 +  close (RUN_LIST);
186 +  my @runList;
187 +  foreach my $file (@runList0)
188 +    {
189 +      next if !($file =~ m/^.*file:.*\.root.*/);
190 +      $file =~ s/.*file:(.*)\.root.*/$1.root/;
191 +      push (@runList, $file);
192 +    }
193 +
194 +  return \@runList;
195 + }
196 +
197 + sub
198 + countEvents
199 + {
200 +  my $file = shift;
201 +
202 +  my $output = `getEventsFromCutFlow $file cutFlow`;
203 +  if ($output =~ m/Did not find a histogram named/)
204 +    {
205 +      print $output;
206 +      return -1;
207 +    }
208 +  $output =~ s/^.*: (.*)$/$1/;
209 +  $output =~ s/\n//g;
210 +
211 +  return $output;
212 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines