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.17 by ahart, Tue Jan 29 00:23:24 2013 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;
18 > my %rootFiles;
19 > my %weights;
20   my @weights;
21 < my @goodJobs;
22 < my @badJobs;
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 +    my $dir = $file;
39 +    $dir =~ s/^(.*)\/[^\/]*$/$1/;
40      if ($file =~ m/^.*\/condor_[^_]*\.log$/)
41        {
42          my $jobNumber = $file;
# Line 34 | Line 46 | foreach my $file (@$files)
46          close (FILE);
47          my $fileContents = join ("", @fileContents);
48          $fileContents =~ s/\n/ /g;
49 <        $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
38 <        push (@goodJobs, $jobNumber) if $fileContents == 0;
39 <        if ($fileContents != 0)
49 >        if ($fileContents =~ m/return value/)
50            {
51 <            push (@badJobs, $jobNumber);
52 <            print "Skipping job $jobNumber. (return value $fileContents)\n";
51 >            $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
52 >            $nGoodJobs++;
53 >            print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0;
54 >            $exitCodes{$dir}{$jobNumber} = $fileContents;
55 >            $counting = 1;
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 >            $counting = 1;
64 >          }
65 >        else
66 >          {
67 >            $nIncompleteJobs++;
68 >            $partial{$dir}{$jobNumber} = 1;
69            }
70 <        $exitCodes{$jobNumber} = $fileContents;
71 <        $counting = 1;
70 >      }
71 >    if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
72 >      {
73 >        open (CROSS_SECTION, "<$file");
74 >        my $crossSection = <CROSS_SECTION>;
75 >        close (CROSS_SECTION);
76 >        $crossSections{$dir} = $crossSection;
77        }
78    }
48 my $nJobs = @goodJobs;
49 $nJobs += @badJobs;
79   foreach my $file (@$files)
80    {
81      next if $file eq ".";
82      next if $file eq "..";
83 +    my $dir = $file;
84 +    $dir =~ s/^(.*)\/[^\/]*$/$1/;
85      my $badJob = 0;
86      if ($file =~ m/^.*_[^_]*\.root$/)
87        {
88          my $jobNumber = $file;
89          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
90 <        $badJob = defined $exitCodes{$jobNumber} && $exitCodes{$jobNumber};
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 <        push (@weights, $opt{"weight"}) if $opt{"weight"};
97 <        push (@weights, 1.0) if !$opt{"weight"};
95 >        foreach my $arg (@ARGV)
96 >          {
97 >            $dirs{$arg} = $dir;
98 >            if (substr ($file, 0, length ($arg)) eq $arg && countEvents ($file) >= 0)
99 >              {
100 >                push (@{$rootFiles{$arg}}, $file);
101 >                push (@weights, $opt{"weight"}) if $opt{"weight"};
102 >                push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
103 >                push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
104 >                push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
105 >                $weights{$arg} = $weights[-1];
106 >              }
107 >          }
108        }
109    }
110 < my $nGoodJobs = @goodJobs;
70 < my $nBadJobs = @badJobs;
71 < if (!@rootFiles)
110 > if (!%rootFiles)
111    {
112      print "Found no ROOT files to merge!\n";
113      exit;
114    }
115 < my $rootFiles = join (" ", @rootFiles);
116 < my $weights = join (",", @weights);
117 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
118 < system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
119 < my $goodEvents = countEvents ("$opt{'prefix'}.root");
120 < print "$nGoodJobs jobs ran successfully over $goodEvents events.\n" if $counting;
115 > my %nTotalEvents;
116 > my $nTotalEvents = 0;
117 > my @mergedFiles;
118 > my @mergedWeights;
119 > foreach my $arg (@ARGV)
120 >  {
121 >    my $rootFiles = join (" ", @{$rootFiles{$arg}});
122 >    my $tmpName = $arg . "_" . "$opt{'prefix'}.root";
123 >    $tmpName =~ s/\//_/g;
124 >    system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
125 >    my $count = countEvents ($tmpName, $cutFlow);
126 >    system ("cutFlowLimits $tmpName");
127 >    $nTotalEvents{$arg} = $count;
128 >    $nTotalEvents += $count;
129 >    $weights{$arg} /= $count if !$opt{"weight"} && $opt{"xsection"};
130 >    $weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}};
131 >    push (@mergedFiles, "$tmpName");
132 >    push (@mergedWeights, $weights{$arg});
133 >  }
134 > my $mergedFiles = join (" ", @mergedFiles);
135 > my $mergedWeights = join (",", @mergedWeights);
136 > system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}_Unweighted.root");
137 > system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
138 > foreach my $mergedFile (@mergedFiles)
139 >  {
140 >    unlink ("$mergedFile");
141 >  }
142 > print "Weights:\n";
143 > foreach my $arg (keys %weights)
144 >  {
145 >    my $shortArg = $arg;
146 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
147 >    printf "  $shortArg: %.5g (%.5g pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity;
148 >  }
149 > my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
150 > print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
151   print "$nBadJobs jobs failed to run.\n" if $counting;
152 + print "$nIncompleteJobs jobs have not finished.\n" if $counting;
153  
154   sub
155   processArgs
# Line 123 | Line 193 | printHelp
193    my $exeName = $0;
194    $exeName =~ s/^.*\/([^\/]*)$/$1/;
195  
196 <  print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n";
197 <  print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
198 <  print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
199 <  print "Condor logs in the specified directories, checks for nonzero return values.\n";
196 >  print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
197 >  print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
198 >  print "specified directories, checks for nonzero return values. If the directories\n";
199 >  print "where created by \"osusub\", uses the cross section from the database to weight\n";
200 >  print "all histograms.\n";
201    print "\n";
202    print "Mandatory arguments to long options are mandatory for short options too.\n";
203 +  printf "%-29s%s\n", "  -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
204 +  printf "%-29s%s\n", "                  ", "cutFlow)";
205    printf "%-29s%s\n", "  -h, --help", "print this help message";
206 <  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
207 <  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT";
206 >  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
207 >  printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
208 >  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
209 >  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
210 >  printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
211 >  printf "%-29s%s\n", "                     ", "the database";
212 >  printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
213 >  printf "%-29s%s\n", "                     ", "the value in the database";
214  
215    exit;
216   }
# Line 159 | Line 238 | sub
238   countEvents
239   {
240    my $file = shift;
241 +  my $cutFlow = shift;
242  
243 <  my $output = `getEventsFromCutFlow $file cutFlow`;
243 >  my $output = `getEventsFromCutFlow $file $cutFlow`;
244 >  if ($output =~ m/Did not find a histogram named/ || $output =~ m/appears to be empty/ || $output =~ m/Failed to open/)
245 >    {
246 >      print $output;
247 >      return -1;
248 >    }
249    $output =~ s/^.*: (.*)$/$1/;
250    $output =~ s/\n//g;
251  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines