ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.13
Committed: Thu Jan 3 21:43:31 2013 UTC (12 years, 4 months ago) by ahart
Branch: MAIN
Changes since 1.12: +20 -39 lines
Log Message:
Do not discard the unweighted merged files, but instead calculate upper limits for their cutflow histograms and then weight them.

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