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.5 by ahart, Sun Sep 9 20:05:12 2012 UTC vs.
Revision 1.12 by ahart, Sun Dec 23 14:45:56 2012 UTC

# Line 11 | Line 11 | sub countEvents;
11  
12   my %opt;
13   Getopt::Long::Configure ("bundling");
14 < GetOptions (\%opt, "luminosity|l=s", "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;
18 > my %rootFiles;
19 > my %weights;
20   my $nGoodJobs = 0;
21   my $nBadJobs = 0;
22   my $counting = 0;
23   my %exitCodes;
24 + my %signals;
25   my %crossSections;
26   my $integratedLuminosity = 10000;
27   $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
28 + my $cutFlow = "cutFlow";
29 + $cutFlow = $opt{"cutflow"} if $opt{"cutflow"};
30   foreach my $file (@$files)
31    {
32      next if $file eq ".";
# Line 39 | Line 42 | foreach my $file (@$files)
42          close (FILE);
43          my $fileContents = join ("", @fileContents);
44          $fileContents =~ s/\n/ /g;
45 <        $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
46 <        $nGoodJobs++;
47 <        if ($fileContents != 0)
45 >        if ($fileContents =~ m/return value/)
46 >          {
47 >            $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
48 >            $nGoodJobs++;
49 >            print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0;
50 >            $exitCodes{$dir}{$jobNumber} = $fileContents;
51 >            $counting = 1;
52 >          }
53 >        if ($fileContents =~ m/signal/)
54            {
55 +            $fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g;
56              $nBadJobs++;
57 <            print "Skipping job $jobNumber. (return value $fileContents)\n";
57 >            print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n";
58 >            $signals{$dir}{$jobNumber} = $fileContents;
59 >            $counting = 1;
60            }
49        $exitCodes{$dir}{$jobNumber} = $fileContents;
50        $counting = 1;
61        }
62      if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
63        {
# Line 57 | Line 67 | foreach my $file (@$files)
67          $crossSections{$dir} = $crossSection;
68        }
69    }
60 my $nTotalEvents = 0;
70   foreach my $file (@$files)
71    {
72      next if $file eq ".";
# Line 69 | Line 78 | foreach my $file (@$files)
78        {
79          my $jobNumber = $file;
80          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
81 <        $badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
81 >        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
82        }
83      next if $badJob;
84      if ($file =~ m/^.*\.root$/)
85        {
86 <        my $nEvents = countEvents ($file);
87 <        $nTotalEvents = -1 if $nTotalEvents < 0 || $nEvents < 0;
88 <        $nTotalEvents += $nEvents if !($nTotalEvents < 0 || $nEvents < 0);
86 >        foreach my $arg (@ARGV)
87 >          {
88 >            if (substr ($file, 0, length ($arg)) eq $arg)
89 >              {
90 >                push (@{$rootFiles{$arg}}, $file);
91 >                push (@{$weights{$arg}}, 1.0);
92 >              }
93 >          }
94        }
95    }
96 + if (!%rootFiles)
97 +  {
98 +    print "Found no ROOT files to merge!\n";
99 +    exit;
100 +  }
101 + my %nTotalEvents;
102 + my $nTotalEvents = 0;
103 + foreach my $arg (@ARGV)
104 +  {
105 +    my $rootFiles = join (" ", @{$rootFiles{$arg}});
106 +    my $weights = join (",", @{$weights{$arg}});
107 +    system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
108 +    my $count = countEvents ("$opt{'prefix'}.root", $cutFlow);
109 +    $nTotalEvents{$arg} = $count;
110 +    $nTotalEvents += $count;
111 +    unlink ("$opt{'prefix'}.root");
112 +  }
113 + my @rootFiles;
114 + my @weights;
115 + %weights = ();
116   foreach my $file (@$files)
117    {
118      next if $file eq ".";
# Line 90 | Line 124 | foreach my $file (@$files)
124        {
125          my $jobNumber = $file;
126          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
127 <        $badJob = defined $exitCodes{$dir} && defined $exitCodes{$dir}{$jobNumber} && $exitCodes{$dir}{$jobNumber};
127 >        $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
128        }
129      next if $badJob;
130      if ($file =~ m/^.*\.root$/)
131        {
132 <        push (@rootFiles, $file);
133 <        push (@weights, $opt{"weight"}) if $opt{"weight"};
134 <        push (@weights, ($crossSections{$dir} * $integratedLuminosity) / $nTotalEvents) if !$opt{"weight"} && defined $crossSections{$dir};
135 <        push (@weights, 1.0) if !$opt{"weight"} && !(defined $crossSections{$dir});
132 >        foreach my $arg (@ARGV)
133 >          {
134 >            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 >          }
144        }
145    }
104 if (!@rootFiles)
105  {
106    print "Found no ROOT files to merge!\n";
107    exit;
108  }
146   my $rootFiles = join (" ", @rootFiles);
147   my $weights = join (",", @weights);
148   system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
149 < system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
150 < my $goodEvents = countEvents ("$opt{'prefix'}.root");
149 > print "Weights:\n";
150 > foreach my $arg (keys %weights)
151 >  {
152 >    my $shortArg = $arg;
153 >    $shortArg =~ s/^.*\/([^\/]*)$/$1/;
154 >    printf "  $shortArg: %.5g (%.5g pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity;
155 >  }
156 > my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
157   print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
158   print "$nBadJobs jobs failed to run.\n" if $counting;
159  
# Line 156 | Line 199 | printHelp
199    my $exeName = $0;
200    $exeName =~ s/^.*\/([^\/]*)$/$1/;
201  
202 <  print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n";
203 <  print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
204 <  print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
205 <  print "Condor logs in the specified directories, checks for nonzero return values. If\n";
206 <  print "the directories where created by \"osusub\", uses the cross section from the\n";
164 <  print "database to weight all histograms.\n";
202 >  print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
203 >  print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
204 >  print "specified directories, checks for nonzero return values. If the directories\n";
205 >  print "where created by \"osusub\", uses the cross section from the database to weight\n";
206 >  print "all histograms.\n";
207    print "\n";
208    print "Mandatory arguments to long options are mandatory for short options too.\n";
209 +  printf "%-29s%s\n", "  -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
210 +  printf "%-29s%s\n", "                  ", "cutFlow)";
211    printf "%-29s%s\n", "  -h, --help", "print this help message";
212    printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
213    printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
214 <  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
214 >  printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
215    printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
216    printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
217    printf "%-29s%s\n", "                     ", "the database";
218 +  printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
219 +  printf "%-29s%s\n", "                     ", "the value in the database";
220  
221    exit;
222   }
# Line 198 | Line 244 | sub
244   countEvents
245   {
246    my $file = shift;
247 +  my $cutFlow = shift;
248  
249 <  my $output = `getEventsFromCutFlow $file cutFlow`;
249 >  my $output = `getEventsFromCutFlow $file $cutFlow`;
250    if ($output =~ m/Did not find a histogram named/)
251      {
252        print $output;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines