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.11 by ahart, Tue Dec 18 09:25:48 2012 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 17 | Line 18 | printHelp () if $opt{"help"} || !$opt{"p
18   my $files = processArgs (\@ARGV);
19   my %rootFiles;
20   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 42 | 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;
51            $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 74 | 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{$arg}}, 1.0);
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});
119 >                $weights{$arg} = $weights[-1];
120                }
121            }
122        }
# Line 100 | Line 128 | if (!%rootFiles)
128    }
129   my %nTotalEvents;
130   my $nTotalEvents = 0;
131 + my @mergedFiles;
132 + my @mergedWeights;
133   foreach my $arg (@ARGV)
134    {
135      my $rootFiles = join (" ", @{$rootFiles{$arg}});
136 <    my $weights = join (",", @{$weights{$arg}});
137 <    system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
138 <    my $count = countEvents ("$opt{'prefix'}.root", $cutFlow);
136 >    my $tmpName = $arg . "_" . "$opt{'prefix'}.root";
137 >    $tmpName =~ s/\//_/g;
138 >    system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
139 >    my $count = countEvents ($tmpName, $cutFlow);
140 >    system ("cutFlowLimits $tmpName");
141      $nTotalEvents{$arg} = $count;
142      $nTotalEvents += $count;
143 <    unlink ("$opt{'prefix'}.root");
144 <  }
145 < my @rootFiles;
146 < my @weights;
147 < %weights = ();
116 < foreach my $file (@$files)
117 <  {
118 <    next if $file eq ".";
119 <    next if $file eq "..";
120 <    my $dir = $file;
121 <    $dir =~ s/^(.*)\/[^\/]*$/$1/;
122 <    my $badJob = 0;
123 <    if ($file =~ m/^.*_[^_]*\.root$/)
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 $jobNumber = $file;
150 <        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
127 <        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
149 >        my $weight = $weights{$arg};
150 >        system ("weightTrees $rootFile $weight");
151        }
152 <    next if $badJob;
130 <    if ($file =~ m/^.*\.root$/)
152 >    if (defined $crossSections{$dirs{$arg}})
153        {
154 <        foreach my $arg (@ARGV)
155 <          {
156 <            if (substr ($file, 0, length ($arg)) eq $arg)
135 <              {
136 <                push (@rootFiles, $file);
137 <                push (@weights, $opt{"weight"}) if $opt{"weight"};
138 <                push (@weights, ($opt{"xsection"} * $integratedLuminosity) / $nTotalEvents{$arg}) if !$opt{"weight"} && $opt{"xsection"};
139 <                push (@weights, ($crossSections{$dir} * $integratedLuminosity) / $nTotalEvents{$arg}) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
140 <                push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
141 <                $weights{$arg} = $weights[-1];
142 <              }
143 <          }
154 >        open (EVENT_COUNT, ">$dirs{$arg}/numberOfEvents.txt");
155 >        print EVENT_COUNT sprintf ("%.0f", $count) . "\n";
156 >        close (EVENT_COUNT);
157        }
158    }
159 < my $rootFiles = join (" ", @rootFiles);
160 < my $weights = join (",", @weights);
161 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
162 < print "Weights:\n";
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 "=============================================\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 <    printf "  $arg: %.1f (%.1f pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity;
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 +    $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 212 | 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 245 | 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 255 | 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