ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.14
Committed: Fri Jan 4 07:35:41 2013 UTC (12 years, 4 months ago) by ahart
Branch: MAIN
Changes since 1.13: +5 -1 lines
Log Message:
Fix bug with weight calculation.

File Contents

# User Rev Content
1 ahart 1.1 #!/usr/bin/env perl
2    
3     use strict;
4     use Getopt::Long;
5 ahart 1.3 use POSIX;
6    
7     sub processArgs;
8     sub printHelp;
9     sub getRunList;
10     sub countEvents;
11 ahart 1.1
12     my %opt;
13     Getopt::Long::Configure ("bundling");
14 ahart 1.6 GetOptions (\%opt, "cutflow|c=s", "luminosity|l=s", "prefix|p=s", "weight|w=s", "xsection|x=s", "help|h");
15 ahart 1.1
16     printHelp () if $opt{"help"} || !$opt{"prefix"};
17     my $files = processArgs (\@ARGV);
18 ahart 1.11 my %rootFiles;
19     my %weights;
20 ahart 1.13 my @weights;
21 ahart 1.5 my $nGoodJobs = 0;
22     my $nBadJobs = 0;
23 ahart 1.3 my $counting = 0;
24 ahart 1.4 my %exitCodes;
25 ahart 1.8 my %signals;
26 ahart 1.5 my %crossSections;
27 ahart 1.14 my %dirs;
28 ahart 1.5 my $integratedLuminosity = 10000;
29     $integratedLuminosity = $opt{"luminosity"} if $opt{"luminosity"};
30 ahart 1.6 my $cutFlow = "cutFlow";
31     $cutFlow = $opt{"cutflow"} if $opt{"cutflow"};
32 ahart 1.3 foreach my $file (@$files)
33     {
34     next if $file eq ".";
35     next if $file eq "..";
36 ahart 1.5 my $dir = $file;
37     $dir =~ s/^(.*)\/[^\/]*$/$1/;
38 ahart 1.4 if ($file =~ m/^.*\/condor_[^_]*\.log$/)
39 ahart 1.3 {
40     my $jobNumber = $file;
41     $jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/;
42     open (FILE, "<$file");
43     my @fileContents = <FILE>;
44     close (FILE);
45     my $fileContents = join ("", @fileContents);
46     $fileContents =~ s/\n/ /g;
47 ahart 1.8 if ($fileContents =~ m/return value/)
48 ahart 1.3 {
49 ahart 1.8 $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
50 ahart 1.9 $nGoodJobs++;
51     print "WARNING: Nonzero exit code for job $jobNumber. (return value $fileContents)\n" if $fileContents != 0;
52 ahart 1.8 $exitCodes{$dir}{$jobNumber} = $fileContents;
53     $counting = 1;
54     }
55     if ($fileContents =~ m/signal/)
56     {
57     $fileContents =~ s/.*\(signal ([^)]*)\).*/$1/g;
58 ahart 1.5 $nBadJobs++;
59 ahart 1.8 print "WARNING: Skipping job $jobNumber. (signal $fileContents)\n";
60     $signals{$dir}{$jobNumber} = $fileContents;
61     $counting = 1;
62 ahart 1.3 }
63     }
64 ahart 1.5 if ($file =~ m/^.*\/crossSectionInPicobarn\.txt$/)
65     {
66     open (CROSS_SECTION, "<$file");
67     my $crossSection = <CROSS_SECTION>;
68     close (CROSS_SECTION);
69     $crossSections{$dir} = $crossSection;
70     }
71 ahart 1.3 }
72 ahart 1.1 foreach my $file (@$files)
73     {
74     next if $file eq ".";
75     next if $file eq "..";
76 ahart 1.5 my $dir = $file;
77     $dir =~ s/^(.*)\/[^\/]*$/$1/;
78 ahart 1.3 my $badJob = 0;
79     if ($file =~ m/^.*_[^_]*\.root$/)
80 ahart 1.1 {
81 ahart 1.3 my $jobNumber = $file;
82     $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
83 ahart 1.8 $badJob = defined $signals{$dir} && defined $signals{$dir}{$jobNumber};
84 ahart 1.5 }
85     next if $badJob;
86     if ($file =~ m/^.*\.root$/)
87     {
88 ahart 1.11 foreach my $arg (@ARGV)
89     {
90 ahart 1.14 $dirs{$arg} = $dir;
91 ahart 1.11 if (substr ($file, 0, length ($arg)) eq $arg)
92     {
93     push (@{$rootFiles{$arg}}, $file);
94 ahart 1.13 push (@weights, $opt{"weight"}) if $opt{"weight"};
95     push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
96     push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
97     push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
98     $weights{$arg} = $weights[-1];
99 ahart 1.11 }
100     }
101 ahart 1.5 }
102     }
103 ahart 1.11 if (!%rootFiles)
104 ahart 1.6 {
105     print "Found no ROOT files to merge!\n";
106     exit;
107     }
108 ahart 1.11 my %nTotalEvents;
109     my $nTotalEvents = 0;
110 ahart 1.13 my @mergedFiles;
111     my @mergedWeights;
112 ahart 1.11 foreach my $arg (@ARGV)
113     {
114     my $rootFiles = join (" ", @{$rootFiles{$arg}});
115 ahart 1.13 my $tmpName = $arg . "_" . "$opt{'prefix'}.root";
116     $tmpName =~ s/\//_/g;
117     system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
118     my $count = countEvents ($tmpName, $cutFlow);
119     system ("cutFlowLimits $tmpName");
120 ahart 1.11 $nTotalEvents{$arg} = $count;
121     $nTotalEvents += $count;
122 ahart 1.14 $weights{$arg} /= $count if !$opt{"weight"} && $opt{"xsection"};
123     $weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}};
124 ahart 1.13 push (@mergedFiles, "$tmpName");
125 ahart 1.14 push (@mergedWeights, $weights{$arg});
126 ahart 1.11 }
127 ahart 1.13 my $mergedFiles = join (" ", @mergedFiles);
128     my $mergedWeights = join (",", @mergedWeights);
129     system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
130     foreach my $mergedFile (@mergedFiles)
131 ahart 1.5 {
132 ahart 1.13 unlink ("$mergedFile");
133 ahart 1.1 }
134 ahart 1.10 print "Weights:\n";
135     foreach my $arg (keys %weights)
136     {
137 ahart 1.12 my $shortArg = $arg;
138     $shortArg =~ s/^.*\/([^\/]*)$/$1/;
139     printf " $shortArg: %.5g (%.5g pb)\n", $weights{$arg}, $weights{$arg} / $integratedLuminosity;
140 ahart 1.10 }
141 ahart 1.6 my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
142 ahart 1.5 print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
143 ahart 1.4 print "$nBadJobs jobs failed to run.\n" if $counting;
144 ahart 1.1
145     sub
146     processArgs
147     {
148     my $argv = shift;
149    
150     my @files;
151     foreach my $arg (@$argv)
152     {
153     $arg =~ s/\/*$//;
154     if (!(-e $arg))
155     {
156     print "$arg does not exist!\n";
157     exit;
158     }
159     next if ($arg =~ m/\/\.$/ || $arg =~ m/\/\.\.$/);
160     if (-d $arg)
161     {
162     opendir (DIR, $arg);
163     my @dirContents = readdir (DIR);
164     closedir (DIR);
165     for (my $i = 0; $i < @dirContents; $i++)
166     {
167     $dirContents[$i] = "$arg/$dirContents[$i]";
168     }
169     my $newFiles = processArgs (\@dirContents);
170     push (@files, @$newFiles);
171     }
172     else
173     {
174     push (@files, $arg);
175     }
176     }
177    
178     return \@files;
179     }
180    
181     sub
182     printHelp
183     {
184     my $exeName = $0;
185     $exeName =~ s/^.*\/([^\/]*)$/$1/;
186    
187 ahart 1.6 print "Usage: $exeName [OPTION]... -p PREFIX DIRECTORIES_AND_FILES\n";
188 ahart 1.7 print "Merges ROOT files containing histograms. If there are Condor logs in the\n";
189     print "specified directories, checks for nonzero return values. If the directories\n";
190     print "where created by \"osusub\", uses the cross section from the database to weight\n";
191     print "all histograms.\n";
192 ahart 1.1 print "\n";
193     print "Mandatory arguments to long options are mandatory for short options too.\n";
194 ahart 1.6 printf "%-29s%s\n", " -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
195     printf "%-29s%s\n", " ", "cutFlow)";
196 ahart 1.1 printf "%-29s%s\n", " -h, --help", "print this help message";
197 ahart 1.5 printf "%-29s%s\n", " -l, --luminosity", "integrated luminosity to which the histograms are";
198     printf "%-29s%s\n", " ", "weighted (default: 10000/pb)";
199 ahart 1.7 printf "%-29s%s\n", " -p, --prefix PREFIX", "output is named PREFIX.root";
200 ahart 1.5 printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
201     printf "%-29s%s\n", " ", "automatic weighting using the cross section from";
202     printf "%-29s%s\n", " ", "the database";
203 ahart 1.6 printf "%-29s%s\n", " -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
204     printf "%-29s%s\n", " ", "the value in the database";
205 ahart 1.1
206     exit;
207     }
208 ahart 1.3
209     sub
210     getRunList
211     {
212     my $runListFile = shift;
213    
214     open (RUN_LIST, "<$runListFile");
215     my @runList0 = <RUN_LIST>;
216     close (RUN_LIST);
217     my @runList;
218     foreach my $file (@runList0)
219     {
220     next if !($file =~ m/^.*file:.*\.root.*/);
221     $file =~ s/.*file:(.*)\.root.*/$1.root/;
222     push (@runList, $file);
223     }
224    
225     return \@runList;
226     }
227    
228     sub
229     countEvents
230     {
231 ahart 1.4 my $file = shift;
232 ahart 1.6 my $cutFlow = shift;
233 ahart 1.4
234 ahart 1.6 my $output = `getEventsFromCutFlow $file $cutFlow`;
235 ahart 1.5 if ($output =~ m/Did not find a histogram named/)
236     {
237     print $output;
238     return -1;
239     }
240 ahart 1.4 $output =~ s/^.*: (.*)$/$1/;
241     $output =~ s/\n//g;
242    
243     return $output;
244 ahart 1.3 }