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.9 by ahart, Sun Dec 16 13:21:27 2012 UTC vs.
Revision 1.28 by ahart, Wed Jul 3 21:30:14 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 15 | Line 16 | GetOptions (\%opt, "cutflow|c=s", "lumin
16  
17   printHelp () if $opt{"help"} || !$opt{"prefix"};
18   my $files = processArgs (\@ARGV);
19 < my @rootFiles;
19 > my %rootFiles;
20 > my %skimEventCounts;
21 > my %weights;
22   my @weights;
23   my $nGoodJobs = 0;
24   my $nBadJobs = 0;
25 + my $nIncompleteJobs = 0;
26   my $counting = 0;
27   my %exitCodes;
28   my %signals;
29 + my %partial;
30   my %crossSections;
31 + my %dirs;
32   my $integratedLuminosity = 10000;
33   $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
34   my $cutFlow = "cutFlow";
# Line 42 | Line 48 | foreach my $file (@$files)
48          close (FILE);
49          my $fileContents = join ("", @fileContents);
50          $fileContents =~ s/\n/ /g;
51 +        $counting = 1;
52          if ($fileContents =~ m/return value/)
53            {
54              $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
55              $nGoodJobs++;
56              print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0;
57              $exitCodes{$dir}{$jobNumber} = $fileContents;
51            $counting = 1;
58            }
59 <        if ($fileContents =~ m/signal/)
59 >        elsif ($fileContents =~ m/signal/)
60            {
61              $fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g;
62              $nBadJobs++;
63              print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n";
64              $signals{$dir}{$jobNumber} = $fileContents;
65 <            $counting = 1;
65 >          }
66 >        else
67 >          {
68 >            $nIncompleteJobs++;
69 >            $partial{$dir}{$jobNumber} = 1;
70            }
71        }
72      if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
# Line 74 | Line 84 | foreach my $file (@$files)
84      my $dir = $file;
85      $dir =~ s/^(.*)\/[^\/]*$/$1/;
86      my $badJob = 0;
87 <    if ($file =~ m/^.*_[^_]*\.root$/)
87 >    my $jobNumber;
88 >    my $fileIsEDM = 0;
89 >    $fileIsEDM = isEDM ($file) if $file =~ m/^.*\.root$/;
90 >    if ($file =~ m/^.*_[^_]*\.root$/ && !$fileIsEDM)
91        {
92 <        my $jobNumber = $file;
92 >        $jobNumber = $file;
93          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
94 <        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
94 >        $badJob = (defined $signals{$dir} && defined $signals{$dir}{$jobNumber}) || (defined $partial{$dir} && defined $partial{$dir}{$jobNumber});
95        }
96      next if $badJob;
97 <    if ($file =~ m/^.*\.root$/)
97 >    if ($file =~ m/^.*\.root$/ && !$fileIsEDM)
98        {
99 <        push (@rootFiles, $file);
100 <        push (@weights, 1.0);
99 >        foreach my $arg (@ARGV)
100 >          {
101 >            if (substr ($file, 0, length ($arg)) eq $arg)
102 >              {
103 >                $dirs{$arg} = $dir;
104 >                if (countEvents ($file, $cutFlow) < 0)
105 >                  {
106 >                    $nGoodJobs--;
107 >                    $nBadJobs++;
108 >                    print "WARNING: Skipping job $jobNumber. (bad ROOT file)\n";
109 >                    last;
110 >                  }
111 >                push (@{$rootFiles{$arg}}, $file);
112 >                if ($opt{"weight"})
113 >                  {
114 >                    push (@weights, $opt{"weight"}) if !($opt{"weight"} =~ m/,/);
115 >                    push (@weights, 1.0) if $opt{"weight"} =~ m/,/;
116 >                  }
117 >                push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
118 >                push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
119 >                push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
120 >                $weights{$arg} = $weights[-1];
121 >              }
122 >          }
123 >      }
124 >    if ($file =~ m/^.*\.root$/ && $fileIsEDM)
125 >      {
126 >        $fileIsEDM -= $fileIsEDM if $fileIsEDM < 0;
127 >        $skimEventCounts{$dir} += $fileIsEDM if defined $skimEventCounts{$dir};
128 >        $skimEventCounts{$dir} = $fileIsEDM if !(defined $skimEventCounts{$dir});
129        }
130    }
131 < if (!@rootFiles)
131 > if (!%rootFiles)
132    {
133      print "Found no ROOT files to merge!\n";
134      exit;
135    }
136 < my $rootFiles = join (" ", @rootFiles);
137 < my $weights = join (",", @weights);
138 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
139 < my $nTotalEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
140 < unlink ("$opt{'prefix'}.root");
100 < @weights = ();
101 < foreach my $file (@$files)
136 > my %nTotalEvents;
137 > my $nTotalEvents = 0;
138 > my @mergedFiles;
139 > my @mergedWeights;
140 > foreach my $arg (@ARGV)
141    {
142 <    next if $file eq ".";
143 <    next if $file eq "..";
144 <    my $dir = $file;
145 <    $dir =~ s/^(.*)\/[^\/]*$/$1/;
146 <    my $badJob = 0;
147 <    if ($file =~ m/^.*_[^_]*\.root$/)
142 >    my $rootFiles = join (" ", @{$rootFiles{$arg}});
143 >    my $tmpName = "." . $arg . "_" . "$opt{'prefix'}.root";
144 >    $tmpName =~ s/\//_/g;
145 >    system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
146 >    my $count = countEvents ($tmpName, $cutFlow);
147 >    system ("cutFlowLimits $tmpName");
148 >    $nTotalEvents{$arg} = $count;
149 >    $nTotalEvents += $count;
150 >    $weights{$arg} /= $count if !$opt{"weight"} && $opt{"xsection"};
151 >    $weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}};
152 >    push (@mergedFiles, $tmpName);
153 >    push (@mergedWeights, $weights{$arg});
154 >    foreach my $rootFile (@{$rootFiles{$arg}})
155        {
156 <        my $jobNumber = $file;
157 <        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
112 <        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
156 >        my $weight = $weights{$arg};
157 >        system ("weightTrees $rootFile $weight");
158        }
159 <    next if $badJob;
115 <    if ($file =~ m/^.*\.root$/)
159 >    if (defined $crossSections{$dirs{$arg}})
160        {
161 <        push (@weights, $opt{"weight"}) if $opt{"weight"};
162 <        push (@weights, ($opt{"xsection"} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && $opt{"xsection"};
163 <        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});
161 >        open (EVENT_COUNT, ">$dirs{$arg}/numberOfEvents.txt");
162 >        print EVENT_COUNT sprintf ("%.0f", $count) . "\n";
163 >        close (EVENT_COUNT);
164        }
165    }
166 < my $weights = join (",", @weights);
167 < system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
166 > my $mergedFiles = join (" ", @mergedFiles);
167 > my $mergedWeights = join (",", @mergedWeights);
168 > $mergedWeights = $opt{"weight"} if $opt{"weight"} && $opt{"weight"} =~ m/,/;
169 > system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
170 > foreach my $dir (keys %skimEventCounts)
171 >  {
172 >    open (EDM_EVENTS, ">$dir/skimNumberOfEvents.txt");
173 >    print EDM_EVENTS $skimEventCounts{$dir} . "\n";
174 >    close (EDM_EVENTS);
175 >  }
176 > foreach my $mergedFile (@mergedFiles)
177 >  {
178 >    unlink ("$mergedFile");
179 >  }
180 > print "=============================================\n";
181 > my $output = sprintf "Cross-section of samples:\n";
182 > my $printOutput = 0;
183 > foreach my $arg (keys %weights)
184 > {
185 >    my $shortArg = $arg;
186 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
187 >    $output = sprintf "%s  $shortArg: %.5g pb\n", $output, ($crossSections{$dirs{$arg}});
188 >    $printOutput = 1 if $weights{$arg} != 1;
189 > }
190 > print $output if $printOutput;
191 >
192 > my $output = sprintf "Effective luminosities of samples:\n";
193 > my $printOutput = 0;
194 > foreach my $arg (keys %weights)
195 >  {
196 >    my $shortArg = $arg;
197 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
198 >    $output = sprintf "%s  $shortArg: %.5g/fb\n", $output, ($integratedLuminosity / (1000.0 * $weights{$arg})) if $weights{$arg} != 1;
199 >    $printOutput = 1 if $weights{$arg} != 1;
200 >  }
201 > print $output if $printOutput;
202 >
203 > $output = sprintf "Weights for target luminosity of %g/fb:\n", ($integratedLuminosity / 1000.0);
204 > $printOutput = 0;
205 > foreach my $arg (keys %weights)
206 >  {
207 >    my $shortArg = $arg;
208 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
209 >    $output = sprintf "%s  $shortArg: %.5g\n", $output, $weights{$arg} if $weights{$arg} != 1;
210 >    $printOutput = 1 if $weights{$arg} != 1;
211 >  }
212 > print $output if $printOutput;
213 >
214   my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
215 < print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
215 > printf "$nGoodJobs jobs ran successfully over $nTotalEvents (%.1f weighted) events.\n", $goodEvents if $counting;
216   print "$nBadJobs jobs failed to run.\n" if $counting;
217 + print "$nIncompleteJobs jobs have not finished.\n" if $counting;
218 + print "=============================================\n";
219  
220   sub
221   processArgs
# Line 183 | Line 274 | printHelp
274    printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
275    printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
276    printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
277 <  printf "%-29s%s\n", "                     ", "the database";
277 >  printf "%-29s%s\n", "                     ", "the database; WEIGHT may be a single number or a";
278 >  printf "%-29s%s\n", "                     ", "comma-separated list, one for each input file";
279    printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
280    printf "%-29s%s\n", "                     ", "the value in the database";
281  
# Line 216 | Line 308 | countEvents
308    my $cutFlow = shift;
309  
310    my $output = `getEventsFromCutFlow $file $cutFlow`;
311 <  if ($output =~ m/Did not find a histogram named/)
311 >  if ($output =~ m/Did not find a histogram named/ || $output =~ m/appears to be empty/ || $output =~ m/Failed to open/)
312      {
313        print $output;
314        return -1;
# Line 226 | Line 318 | countEvents
318  
319    return $output;
320   }
321 +
322 + sub
323 + isEDM
324 + {
325 +  my $file = shift;
326 +
327 +  my $output = `edmFileUtil $file 2>&1`;
328 +  return 0 if $output =~ m/appears to be missing/ || $output =~ m/not a ROOT file/;
329 +  $output =~ s/[\f\n\r]//g;
330 +  $output =~ s/^.*, ([^,]*) events,.*$/$1/;
331 +  $output += 0;
332 +  $output-- if $output == 0;
333 +  return $output;
334 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines