ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.7
Committed: Tue Sep 11 15:06:18 2012 UTC (12 years, 7 months ago) by ahart
Branch: MAIN
Changes since 1.6: +5 -6 lines
Log Message:
Do not automatically generate cut flow tables.

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