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.3 by ahart, Tue Aug 28 12:27:57 2012 UTC vs.
Revision 1.7 by ahart, Tue Sep 11 15:06:18 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;
22 < my %runList;
20 > my $nGoodJobs = 0;
21 > my $nBadJobs = 0;
22   my $counting = 0;
23 + my %exitCodes;
24 + my %crossSections;
25 + my $integratedLuminosity = 10000;
26 + $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
27 + my $cutFlow = "cutFlow";
28 + $cutFlow = $opt{"cutflow"} if $opt{"cutflow"};
29   foreach my $file (@$files)
30    {
31      next if $file eq ".";
32      next if $file eq "..";
33 <    if ($file =~ m/^.*\/condor_.*\.log$/)
33 >    my $dir = $file;
34 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
35 >    if ($file =~ m/^.*\/condor_[^_]*\.log$/)
36        {
37          my $jobNumber = $file;
38          $jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/;
# Line 35 | Line 42 | foreach my $file (@$files)
42          my $fileContents = join ("", @fileContents);
43          $fileContents =~ s/\n/ /g;
44          $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
45 <        push (@goodJobs, $jobNumber) if $fileContents == 0;
45 >        $nGoodJobs++;
46          if ($fileContents != 0)
47            {
48 <            push (@badJobs, $jobNumber);
48 >            $nBadJobs++;
49              print "Skipping job $jobNumber. (return value $fileContents)\n";
50            }
51 +        $exitCodes{$dir}{$jobNumber} = $fileContents;
52 +        $counting = 1;
53        }
54 <    if ($file =~ m/^.*\/runList\.py$/)
54 >    if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
55        {
56 <        my $directory = $file;
57 <        $directory =~ s/^(.*)\/runList\.py$/$1/;
58 <        $runList{$directory} = getRunList ($file);
59 <        $counting = 1;
56 >        open (CROSS_SECTION, "<$file");
57 >        my $crossSection = <CROSS_SECTION>;
58 >        close (CROSS_SECTION);
59 >        $crossSections{$dir} = $crossSection;
60        }
61    }
53 my $nJobs = @goodJobs;
54 $nJobs += @badJobs;
55 my $goodEvents = 0;
56 my $goodLumis = 0;
57 my $goodRuns = 0;
58 my $badEvents = 0;
59 my $badLumis = 0;
60 my $badRuns = 0;
62   foreach my $file (@$files)
63    {
64      next if $file eq ".";
65      next if $file eq "..";
66 +    my $dir = $file;
67 +    $dir =~ s/^(.*)\/[^\/]*$/$1/;
68      my $badJob = 0;
69      if ($file =~ m/^.*_[^_]*\.root$/)
70        {
71          my $jobNumber = $file;
69        my $directory = $file;
72          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
73 <        $directory =~ s/^(.*)\/[^\/]*\.root$/$1/;
72 <        foreach my $badJobNumber (@badJobs)
73 <          {
74 <            $badJob = 1 if $jobNumber = $badJobNumber;
75 <          }
76 <        countEvents ($runList{$directory}, $jobNumber, $nJobs, \$goodEvents, \$goodLumis, \$goodRuns) if !$badJob && defined $runList{$directory};
77 <        countEvents ($runList{$directory}, $jobNumber, $nJobs, \$badEvents, \$badLumis, \$badRuns) if $badJob && defined $runList{$directory};
73 >        $badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
74        }
75      next if $badJob;
76      if ($file =~ m/^.*\.root$/)
77        {
78          push (@rootFiles, $file);
79 <        push (@weights, $opt{"weight"}) if $opt{"weight"};
84 <        push (@weights, 1.0) if !$opt{"weight"};
79 >        push (@weights, 1.0);
80        }
81    }
87 my $nGoodJobs = @goodJobs;
88 my $nBadJobs = @badJobs;
89 print "$nGoodJobs ran successfully over $goodEvents events, $goodLumis lumis, $goodRuns runs\n" if $counting;
90 print "$nBadJobs failed to run over $badEvents events, $badLumis lumis, $badRuns runs\n" if $counting;
82   if (!@rootFiles)
83    {
84      print "Found no ROOT files to merge!\n";
# Line 96 | Line 87 | if (!@rootFiles)
87   my $rootFiles = join (" ", @rootFiles);
88   my $weights = join (",", @weights);
89   system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
90 < system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
90 > my $nTotalEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
91 > unlink ("$opt{'prefix'}.root");
92 > @weights = ();
93 > foreach my $file (@$files)
94 >  {
95 >    next if $file eq ".";
96 >    next if $file eq "..";
97 >    my $dir = $file;
98 >    $dir =~ s/^(.*)\/[^\/]*$/$1/;
99 >    my $badJob = 0;
100 >    if ($file =~ m/^.*_[^_]*\.root$/)
101 >      {
102 >        my $jobNumber = $file;
103 >        $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
104 >        $badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
105 >      }
106 >    next if $badJob;
107 >    if ($file =~ m/^.*\.root$/)
108 >      {
109 >        push (@weights, $opt{"weight"}) if $opt{"weight"};
110 >        push (@weights, ($opt{"xsection"} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && $opt{"xsection"};
111 >        push (@weights, ($crossSections{$dir} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
112 >        push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
113 >      }
114 >  }
115 > my $weights = join (",", @weights);
116 > system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
117 > my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
118 > print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
119 > print "$nBadJobs jobs failed to run.\n" if $counting;
120  
121   sub
122   processArgs
# Line 140 | Line 160 | printHelp
160    my $exeName = $0;
161    $exeName =~ s/^.*\/([^\/]*)$/$1/;
162  
163 <  print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n";
164 <  print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
165 <  print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
166 <  print "Condor logs in the specified directories, checks for nonzero return values.\n";
163 >  print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
164 >  print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
165 >  print "specified directories, checks for nonzero return values. If the directories\n";
166 >  print "where created by \"osusub\", uses the cross section from the database to weight\n";
167 >  print "all histograms.\n";
168    print "\n";
169    print "Mandatory arguments to long options are mandatory for short options too.\n";
170 +  printf "%-29s%s\n", "  -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
171 +  printf "%-29s%s\n", "                  ", "cutFlow)";
172    printf "%-29s%s\n", "  -h, --help", "print this help message";
173 <  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
174 <  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT";
173 >  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
174 >  printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
175 >  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
176 >  printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
177 >  printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
178 >  printf "%-29s%s\n", "                     ", "the database";
179 >  printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
180 >  printf "%-29s%s\n", "                     ", "the value in the database";
181  
182    exit;
183   }
# Line 175 | Line 204 | getRunList
204   sub
205   countEvents
206   {
207 <  my $runList = shift;
208 <  my $jobNumber = shift;
209 <  my $nJobs = shift;
210 <  my $events = shift;
211 <  my $lumis = shift;
183 <  my $runs = shift;
184 <
185 <  my $filesPerJob = ceil (@$runList / $nJobs);
186 <  print "Job $jobNumber ran successfully on $filesPerJob files.\n";
187 <  my @subRunList = @$runList[($jobNumber * $filesPerJob)..($jobNumber * $filesPerJob + $filesPerJob - 1)];
188 <  foreach my $file (@subRunList)
207 >  my $file = shift;
208 >  my $cutFlow = shift;
209 >
210 >  my $output = `getEventsFromCutFlow $file $cutFlow`;
211 >  if ($output =~ m/Did not find a histogram named/)
212      {
213 <      my @output = `edmFileUtil $file`;
214 <      my $output = join (" ", @output);
192 <      $output =~ s/\n//g;
193 <      my $eventsStr = $output;
194 <      my $lumisStr = $output;
195 <      my $runsStr = $output;
196 <      $eventsStr =~ s/^.*\([^ ]* runs, [^ ]* lumis, ([^ ]*) events, [^ ]* bytes\).*$/$1/;
197 <      $lumisStr =~ s/^.*\([^ ]* runs, ([^ ]*) lumis, [^ ]* events, [^ ]* bytes\).*$/$1/;
198 <      $runsStr =~ s/^.*\(([^ ]*) runs, [^ ]* lumis, [^ ]* events, [^ ]* bytes\).*$/$1/;
199 <      $$events += $eventsStr;
200 <      $$lumis += $lumisStr;
201 <      $$runs += $runsStr;
213 >      print $output;
214 >      return -1;
215      }
216 +  $output =~ s/^.*: (.*)$/$1/;
217 +  $output =~ s/\n//g;
218 +
219 +  return $output;
220   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines