ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.18
Committed: Thu Jan 31 17:32:03 2013 UTC (12 years, 3 months ago) by ahart
Branch: MAIN
Changes since 1.17: +5 -2 lines
Log Message:
Do not output the weights when they are all one.

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