ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.11
Committed: Tue Dec 18 09:25:48 2012 UTC (12 years, 4 months ago) by ahart
Branch: MAIN
Changes since 1.10: +33 -18 lines
Log Message:
Count the number of events for each argument.

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