ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.5
Committed: Sun Sep 9 20:05:12 2012 UTC (12 years, 7 months ago) by ahart
Branch: MAIN
Changes since 1.4: +59 -15 lines
Log Message:
Automatically weight the histograms using the cross section from the database.

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