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.4 by ahart, Tue Aug 28 17:24:06 2012 UTC vs.
Revision 1.9 by ahart, Sun Dec 16 13:21:27 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines