ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.3
Committed: Tue Aug 28 12:27:57 2012 UTC (12 years, 8 months ago) by ahart
Branch: MAIN
Changes since 1.2: +128 -18 lines
Log Message:
Add a weight option, and print the number of events processed at the end.

File Contents

# Content
1 #!/usr/bin/env perl
2
3 use strict;
4 use Getopt::Long;
5 use POSIX;
6
7 sub processArgs;
8 sub printHelp;
9 sub getRunList;
10 sub countEvents;
11
12 my %opt;
13 Getopt::Long::Configure ("bundling");
14 GetOptions (\%opt, "prefix|p=s", "weight|w=s", "help|h");
15
16 printHelp () if $opt{"help"} || !$opt{"prefix"};
17 my $files = processArgs (\@ARGV);
18 my @rootFiles;
19 my @weights;
20 my @goodJobs;
21 my @badJobs;
22 my %runList;
23 my $counting = 0;
24 foreach my $file (@$files)
25 {
26 next if $file eq ".";
27 next if $file eq "..";
28 if ($file =~ m/^.*\/condor_.*\.log$/)
29 {
30 my $jobNumber = $file;
31 $jobNumber =~ s/^.*\/condor_([^_]*)\.log$/$1/;
32 open (FILE, "<$file");
33 my @fileContents = <FILE>;
34 close (FILE);
35 my $fileContents = join ("", @fileContents);
36 $fileContents =~ s/\n/ /g;
37 $fileContents =~ s/.*\(return value ([^)]*)\).*/$1/g;
38 push (@goodJobs, $jobNumber) if $fileContents == 0;
39 if ($fileContents != 0)
40 {
41 push (@badJobs, $jobNumber);
42 print "Skipping job $jobNumber. (return value $fileContents)\n";
43 }
44 }
45 if ($file =~ m/^.*\/runList\.py$/)
46 {
47 my $directory = $file;
48 $directory =~ s/^(.*)\/runList\.py$/$1/;
49 $runList{$directory} = getRunList ($file);
50 $counting = 1;
51 }
52 }
53 my $nJobs = @goodJobs;
54 $nJobs += @badJobs;
55 my $goodEvents = 0;
56 my $goodLumis = 0;
57 my $goodRuns = 0;
58 my $badEvents = 0;
59 my $badLumis = 0;
60 my $badRuns = 0;
61 foreach my $file (@$files)
62 {
63 next if $file eq ".";
64 next if $file eq "..";
65 my $badJob = 0;
66 if ($file =~ m/^.*_[^_]*\.root$/)
67 {
68 my $jobNumber = $file;
69 my $directory = $file;
70 $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
71 $directory =~ s/^(.*)\/[^\/]*\.root$/$1/;
72 foreach my $badJobNumber (@badJobs)
73 {
74 $badJob = 1 if $jobNumber = $badJobNumber;
75 }
76 countEvents ($runList{$directory}, $jobNumber, $nJobs, \$goodEvents, \$goodLumis, \$goodRuns) if !$badJob && defined $runList{$directory};
77 countEvents ($runList{$directory}, $jobNumber, $nJobs, \$badEvents, \$badLumis, \$badRuns) if $badJob && defined $runList{$directory};
78 }
79 next if $badJob;
80 if ($file =~ m/^.*\.root$/)
81 {
82 push (@rootFiles, $file);
83 push (@weights, $opt{"weight"}) if $opt{"weight"};
84 push (@weights, 1.0) if !$opt{"weight"};
85 }
86 }
87 my $nGoodJobs = @goodJobs;
88 my $nBadJobs = @badJobs;
89 print "$nGoodJobs ran successfully over $goodEvents events, $goodLumis lumis, $goodRuns runs\n" if $counting;
90 print "$nBadJobs failed to run over $badEvents events, $badLumis lumis, $badRuns runs\n" if $counting;
91 if (!@rootFiles)
92 {
93 print "Found no ROOT files to merge!\n";
94 exit;
95 }
96 my $rootFiles = join (" ", @rootFiles);
97 my $weights = join (",", @weights);
98 system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
99 system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
100
101 sub
102 processArgs
103 {
104 my $argv = shift;
105
106 my @files;
107 foreach my $arg (@$argv)
108 {
109 $arg =~ s/\/*$//;
110 if (!(-e $arg))
111 {
112 print "$arg does not exist!\n";
113 exit;
114 }
115 next if ($arg =~ m/\/\.$/ || $arg =~ m/\/\.\.$/);
116 if (-d $arg)
117 {
118 opendir (DIR, $arg);
119 my @dirContents = readdir (DIR);
120 closedir (DIR);
121 for (my $i = 0; $i < @dirContents; $i++)
122 {
123 $dirContents[$i] = "$arg/$dirContents[$i]";
124 }
125 my $newFiles = processArgs (\@dirContents);
126 push (@files, @$newFiles);
127 }
128 else
129 {
130 push (@files, $arg);
131 }
132 }
133
134 return \@files;
135 }
136
137 sub
138 printHelp
139 {
140 my $exeName = $0;
141 $exeName =~ s/^.*\/([^\/]*)$/$1/;
142
143 print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n";
144 print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
145 print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
146 print "Condor logs in the specified directories, checks for nonzero return values.\n";
147 print "\n";
148 print "Mandatory arguments to long options are mandatory for short options too.\n";
149 printf "%-29s%s\n", " -h, --help", "print this help message";
150 printf "%-29s%s\n", " -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
151 printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT";
152
153 exit;
154 }
155
156 sub
157 getRunList
158 {
159 my $runListFile = shift;
160
161 open (RUN_LIST, "<$runListFile");
162 my @runList0 = <RUN_LIST>;
163 close (RUN_LIST);
164 my @runList;
165 foreach my $file (@runList0)
166 {
167 next if !($file =~ m/^.*file:.*\.root.*/);
168 $file =~ s/.*file:(.*)\.root.*/$1.root/;
169 push (@runList, $file);
170 }
171
172 return \@runList;
173 }
174
175 sub
176 countEvents
177 {
178 my $runList = shift;
179 my $jobNumber = shift;
180 my $nJobs = shift;
181 my $events = shift;
182 my $lumis = shift;
183 my $runs = shift;
184
185 my $filesPerJob = ceil (@$runList / $nJobs);
186 print "Job $jobNumber ran successfully on $filesPerJob files.\n";
187 my @subRunList = @$runList[($jobNumber * $filesPerJob)..($jobNumber * $filesPerJob + $filesPerJob - 1)];
188 foreach my $file (@subRunList)
189 {
190 my @output = `edmFileUtil $file`;
191 my $output = join (" ", @output);
192 $output =~ s/\n//g;
193 my $eventsStr = $output;
194 my $lumisStr = $output;
195 my $runsStr = $output;
196 $eventsStr =~ s/^.*\([^ ]* runs, [^ ]* lumis, ([^ ]*) events, [^ ]* bytes\).*$/$1/;
197 $lumisStr =~ s/^.*\([^ ]* runs, ([^ ]*) lumis, [^ ]* events, [^ ]* bytes\).*$/$1/;
198 $runsStr =~ s/^.*\(([^ ]*) runs, [^ ]* lumis, [^ ]* events, [^ ]* bytes\).*$/$1/;
199 $$events += $eventsStr;
200 $$lumis += $lumisStr;
201 $$runs += $runsStr;
202 }
203 }