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.8 by ahart, Thu Dec 13 10:51:30 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, "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;
19 > my @weights;
20 > my $nGoodJobs = 0;
21 > my $nBadJobs = 0;
22 > my $counting = 0;
23 > my %exitCodes;
24 > my %signals;
25 > my %crossSections;
26 > my $integratedLuminosity = 10000;
27 > $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
28 > my $cutFlow = "cutFlow";
29 > $cutFlow = $opt{"cutflow"} if $opt{"cutflow"};
30   foreach my $file (@$files)
31    {
32      next if $file eq ".";
33      next if $file eq "..";
34 <    if ($file =~ m/\.root$/)
34 >    my $dir = $file;
35 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
36 >    if ($file =~ m/^.*\/condor_[^_]*\.log$/)
37        {
38 <        push (@rootFiles, $file);
39 <        next;
38 >        my $jobNumber = $file;
39 >        $jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/;
40 >        open (FILE, "<$file");
41 >        my @fileContents = <FILE>;
42 >        close (FILE);
43 >        my $fileContents = join ("", @fileContents);
44 >        $fileContents =~ s/\n/ /g;
45 >        if ($fileContents =~ m/return value/)
46 >          {
47 >            $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
48 >            if ($fileContents != 0)
49 >              {
50 >                $nGoodJobs++;
51 >                print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n";
52 >              }
53 >            $exitCodes{$dir}{$jobNumber} = $fileContents;
54 >            $counting = 1;
55 >          }
56 >        if ($fileContents =~ m/signal/)
57 >          {
58 >            $fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g;
59 >            $nBadJobs++;
60 >            print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n";
61 >            $signals{$dir}{$jobNumber} = $fileContents;
62 >            $counting = 1;
63 >          }
64 >      }
65 >    if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
66 >      {
67 >        open (CROSS_SECTION, "<$file");
68 >        my $crossSection = <CROSS_SECTION>;
69 >        close (CROSS_SECTION);
70 >        $crossSections{$dir} = $crossSection;
71 >      }
72 >  }
73 > foreach my $file (@$files)
74 >  {
75 >    next if $file eq ".";
76 >    next if $file eq "..";
77 >    my $dir = $file;
78 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
79 >    my $badJob = 0;
80 >    if ($file =~ m/^.*_[^_]*\.root$/)
81 >      {
82 >        my $jobNumber = $file;
83 >        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
84 >        #$badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
85 >        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
86        }
87 <    open (FILE, "<$file");
88 <    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)
87 >    next if $badJob;
88 >    if ($file =~ m/^.*\.root$/)
89        {
90 <        printf "Error: return value of %2d in \"$file\"!\n", $fileContents;
91 <        $error = 1;
90 >        push (@rootFiles, $file);
91 >        push (@weights, 1.0);
92        }
93    }
94 < exit if $error;
94 > if (!@rootFiles)
95 >  {
96 >    print "Found no ROOT files to merge!\n";
97 >    exit;
98 >  }
99   my $rootFiles = join (" ", @rootFiles);
100 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root");
101 < system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
100 > my $weights = join (",", @weights);
101 > system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
102 > my $nTotalEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
103 > unlink ("$opt{'prefix'}.root");
104 > @weights = ();
105 > foreach my $file (@$files)
106 >  {
107 >    next if $file eq ".";
108 >    next if $file eq "..";
109 >    my $dir = $file;
110 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
111 >    my $badJob = 0;
112 >    if ($file =~ m/^.*_[^_]*\.root$/)
113 >      {
114 >        my $jobNumber = $file;
115 >        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
116 >        #$badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
117 >        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
118 >      }
119 >    next if $badJob;
120 >    if ($file =~ m/^.*\.root$/)
121 >      {
122 >        push (@weights, $opt{"weight"}) if $opt{"weight"};
123 >        push (@weights, ($opt{"xsection"} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && $opt{"xsection"};
124 >        push (@weights, ($crossSections{$dir} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
125 >        push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
126 >      }
127 >  }
128 > my $weights = join (",", @weights);
129 > system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
130 > my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
131 > print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
132 > print "$nBadJobs jobs failed to run.\n" if $counting;
133  
134   sub
135   processArgs
# Line 80 | Line 173 | printHelp
173    my $exeName = $0;
174    $exeName =~ s/^.*\/([^\/]*)$/$1/;
175  
176 <  print "Usage: $exeName [OPTIONS] -p PREFIX DIRECTORIES_AND_FILES\n";
177 <  print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
178 <  print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
179 <  print "Condor logs in the specified directories, checks for nonzero return values.\n";
176 >  print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
177 >  print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
178 >  print "specified directories, checks for nonzero return values. If the directories\n";
179 >  print "where created by \"osusub\", uses the cross section from the database to weight\n";
180 >  print "all histograms.\n";
181    print "\n";
182    print "Mandatory arguments to long options are mandatory for short options too.\n";
183 +  printf "%-29s%s\n", "  -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
184 +  printf "%-29s%s\n", "                  ", "cutFlow)";
185    printf "%-29s%s\n", "  -h, --help", "print this help message";
186 <  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
186 >  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
187 >  printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
188 >  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
189 >  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
190 >  printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
191 >  printf "%-29s%s\n", "                     ", "the database";
192 >  printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
193 >  printf "%-29s%s\n", "                     ", "the value in the database";
194  
195    exit;
196   }
197 +
198 + sub
199 + getRunList
200 + {
201 +  my $runListFile = shift;
202 +
203 +  open (RUN_LIST, "<$runListFile");
204 +  my @runList0 = <RUN_LIST>;
205 +  close (RUN_LIST);
206 +  my @runList;
207 +  foreach my $file (@runList0)
208 +    {
209 +      next if !($file =~ m/^.*file:.*\.root.*/);
210 +      $file =~ s/.*file:(.*)\.root.*/$1.root/;
211 +      push (@runList, $file);
212 +    }
213 +
214 +  return \@runList;
215 + }
216 +
217 + sub
218 + countEvents
219 + {
220 +  my $file = shift;
221 +  my $cutFlow = shift;
222 +
223 +  my $output = `getEventsFromCutFlow $file $cutFlow`;
224 +  if ($output =~ m/Did not find a histogram named/)
225 +    {
226 +      print $output;
227 +      return -1;
228 +    }
229 +  $output =~ s/^.*: (.*)$/$1/;
230 +  $output =~ s/\n//g;
231 +
232 +  return $output;
233 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines