ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.22
Committed: Mon Apr 8 12:40:01 2013 UTC (12 years ago) by jbrinson
Branch: MAIN
Changes since 1.21: +9 -0 lines
Log Message:
Added printout for crossection

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