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.13 by ahart, Thu Jan 3 21:43:31 2013 UTC vs.
Revision 1.27 by ahart, Tue Jun 18 02:00:39 2013 UTC

# Line 8 | Line 8 | sub processArgs;
8   sub printHelp;
9   sub getRunList;
10   sub countEvents;
11 + sub isEDM;
12  
13   my %opt;
14   Getopt::Long::Configure ("bundling");
# Line 20 | Line 21 | my %weights;
21   my @weights;
22   my $nGoodJobs = 0;
23   my $nBadJobs = 0;
24 + my $nIncompleteJobs = 0;
25   my $counting = 0;
26   my %exitCodes;
27   my %signals;
28 + my %partial;
29   my %crossSections;
30 + my %dirs;
31   my $integratedLuminosity = 10000;
32   $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
33   my $cutFlow = "cutFlow";
# Line 43 | Line 47 | foreach my $file (@$files)
47          close (FILE);
48          my $fileContents = join ("", @fileContents);
49          $fileContents =~ s/\n/ /g;
50 +        $counting = 1;
51          if ($fileContents =~ m/return value/)
52            {
53              $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
54              $nGoodJobs++;
55              print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0;
56              $exitCodes{$dir}{$jobNumber} = $fileContents;
52            $counting = 1;
57            }
58 <        if ($fileContents =~ m/signal/)
58 >        elsif ($fileContents =~ m/signal/)
59            {
60              $fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g;
61              $nBadJobs++;
62              print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n";
63              $signals{$dir}{$jobNumber} = $fileContents;
64 <            $counting = 1;
64 >          }
65 >        else
66 >          {
67 >            $nIncompleteJobs++;
68 >            $partial{$dir}{$jobNumber} = 1;
69            }
70        }
71      if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
# Line 75 | Line 83 | foreach my $file (@$files)
83      my $dir = $file;
84      $dir =~ s/^(.*)\/[^\/]*$/$1/;
85      my $badJob = 0;
86 <    if ($file =~ m/^.*_[^_]*\.root$/)
86 >    my $jobNumber;
87 >    my $fileIsEDM = 0;
88 >    $fileIsEDM = isEDM ($file) if $file =~ m/^.*\.root$/;
89 >    if ($file =~ m/^.*_[^_]*\.root$/ && !$fileIsEDM)
90        {
91 <        my $jobNumber = $file;
91 >        $jobNumber = $file;
92          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
93 <        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
93 >        $badJob = (defined $signals{$dir} && defined $signals{$dir}{$jobNumber}) || (defined $partial{$dir} && defined $partial{$dir}{$jobNumber});
94        }
95      next if $badJob;
96 <    if ($file =~ m/^.*\.root$/)
96 >    if ($file =~ m/^.*\.root$/ && !$fileIsEDM)
97        {
98          foreach my $arg (@ARGV)
99            {
100              if (substr ($file, 0, length ($arg)) eq $arg)
101                {
102 +                $dirs{$arg} = $dir;
103 +                if (countEvents ($file, $cutFlow) < 0)
104 +                  {
105 +                    $nGoodJobs--;
106 +                    $nBadJobs++;
107 +                    print "WARNING: Skipping job $jobNumber. (bad ROOT file)\n";
108 +                    last;
109 +                  }
110                  push (@{$rootFiles{$arg}}, $file);
111 <                push (@weights, $opt{"weight"}) if $opt{"weight"};
111 >                if ($opt{"weight"})
112 >                  {
113 >                    push (@weights, $opt{"weight"}) if !($opt{"weight"} =~ m/,/);
114 >                    push (@weights, 1.0) if $opt{"weight"} =~ m/,/;
115 >                  }
116                  push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
117                  push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
118                  push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
# Line 117 | Line 140 | foreach my $arg (@ARGV)
140      system ("cutFlowLimits $tmpName");
141      $nTotalEvents{$arg} = $count;
142      $nTotalEvents += $count;
143 <    push (@mergedFiles, "$tmpName");
144 <    push (@mergedWeights, $weights{$arg} / $count);
143 >    $weights{$arg} /= $count if !$opt{"weight"} && $opt{"xsection"};
144 >    $weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}};
145 >    push (@mergedFiles, $tmpName);
146 >    push (@mergedWeights, $weights{$arg});
147 >    foreach my $rootFile (@{$rootFiles{$arg}})
148 >      {
149 >        my $weight = $weights{$arg};
150 >        system ("weightTrees $rootFile $weight");
151 >      }
152 >    if (defined $crossSections{$dirs{$arg}})
153 >      {
154 >        open (EVENT_COUNT, ">$dirs{$arg}/numberOfEvents.txt");
155 >        print EVENT_COUNT sprintf ("%.0f", $count) . "\n";
156 >        close (EVENT_COUNT);
157 >      }
158    }
159   my $mergedFiles = join (" ", @mergedFiles);
160   my $mergedWeights = join (",", @mergedWeights);
161 + $mergedWeights = $opt{"weight"} if $opt{"weight"} && $opt{"weight"} =~ m/,/;
162   system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
163   foreach my $mergedFile (@mergedFiles)
164    {
165      unlink ("$mergedFile");
166    }
167 < print "Weights:\n";
167 > print "=============================================\n";
168 > my $output = sprintf "Cross-section of samples:\n";
169 > my $printOutput = 0;
170 > foreach my $arg (keys %weights)
171 > {
172 >    my $shortArg = $arg;
173 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
174 >    $output = sprintf "%s  $shortArg: %.5g pb\n", $output, ($crossSections{$dirs{$arg}});
175 >    $printOutput = 1 if $weights{$arg} != 1;
176 > }
177 > print $output if $printOutput;
178 >
179 > my $output = sprintf "Effective luminosities of samples:\n";
180 > my $printOutput = 0;
181 > foreach my $arg (keys %weights)
182 >  {
183 >    my $shortArg = $arg;
184 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
185 >    $output = sprintf "%s  $shortArg: %.5g/fb\n", $output, ($integratedLuminosity / (1000.0 * $weights{$arg})) if $weights{$arg} != 1;
186 >    $printOutput = 1 if $weights{$arg} != 1;
187 >  }
188 > print $output if $printOutput;
189 >
190 > $output = sprintf "Weights for target luminosity of %g/fb:\n", ($integratedLuminosity / 1000.0);
191 > $printOutput = 0;
192   foreach my $arg (keys %weights)
193    {
194      my $shortArg = $arg;
195      $shortArg =~ s/^.*\/([^\/]*)$/$1/;
196 <    printf "  $shortArg: %.5g (%.5g pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity;
196 >    $output = sprintf "%s  $shortArg: %.5g\n", $output, $weights{$arg} if $weights{$arg} != 1;
197 >    $printOutput = 1 if $weights{$arg} != 1;
198    }
199 + print $output if $printOutput;
200 +
201   my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
202 < print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
202 > printf "$nGoodJobs jobs ran successfully over $nTotalEvents (%.1f weighted) events.\n", $goodEvents if $counting;
203   print "$nBadJobs jobs failed to run.\n" if $counting;
204 + print "$nIncompleteJobs jobs have not finished.\n" if $counting;
205 + print "=============================================\n";
206  
207   sub
208   processArgs
# Line 195 | Line 261 | printHelp
261    printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
262    printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
263    printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
264 <  printf "%-29s%s\n", "                     ", "the database";
264 >  printf "%-29s%s\n", "                     ", "the database; WEIGHT may be a single number or a";
265 >  printf "%-29s%s\n", "                     ", "comma-separated list, one for each input file";
266    printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
267    printf "%-29s%s\n", "                     ", "the value in the database";
268  
# Line 228 | Line 295 | countEvents
295    my $cutFlow = shift;
296  
297    my $output = `getEventsFromCutFlow $file $cutFlow`;
298 <  if ($output =~ m/Did not find a histogram named/)
298 >  if ($output =~ m/Did not find a histogram named/ || $output =~ m/appears to be empty/ || $output =~ m/Failed to open/)
299      {
300        print $output;
301        return -1;
# Line 238 | Line 305 | countEvents
305  
306    return $output;
307   }
308 +
309 + sub
310 + isEDM
311 + {
312 +  my $file = shift;
313 +
314 +  my $output = `edmFileUtil $file 2>&1`;
315 +  return 0 if $output =~ m/appears to be missing/ || $output =~ m/not a ROOT file/;
316 +  return 1;
317 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines