ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
Revision: 1.4
Committed: Tue Aug 28 17:24:06 2012 UTC (12 years, 8 months ago) by ahart
Branch: MAIN
CVS Tags: mytag_0_0_0, V00-00-03
Changes since 1.3: +14 -49 lines
Log Message:
Get number of events from cut flow histogram instead of edmFileUtil.

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.3 GetOptions (\%opt, "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     my @goodJobs;
21     my @badJobs;
22     my $counting = 0;
23 ahart 1.4 my %exitCodes;
24 ahart 1.3 foreach my $file (@$files)
25     {
26     next if $file eq ".";
27     next if $file eq "..";
28 ahart 1.4 if ($file =~ m/^.*\/condor_[^_]*\.log$/)
29 ahart 1.3 {
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 ahart 1.4 $exitCodes{$jobNumber} = $fileContents;
45 ahart 1.3 $counting = 1;
46     }
47     }
48     my $nJobs = @goodJobs;
49     $nJobs += @badJobs;
50 ahart 1.1 foreach my $file (@$files)
51     {
52     next if $file eq ".";
53     next if $file eq "..";
54 ahart 1.3 my $badJob = 0;
55     if ($file =~ m/^.*_[^_]*\.root$/)
56 ahart 1.1 {
57 ahart 1.3 my $jobNumber = $file;
58     $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
59 ahart 1.4 $badJob = defined $exitCodes{$jobNumber} && $exitCodes{$jobNumber};
60 ahart 1.1 }
61 ahart 1.3 next if $badJob;
62     if ($file =~ m/^.*\.root$/)
63 ahart 1.1 {
64 ahart 1.3 push (@rootFiles, $file);
65     push (@weights, $opt{"weight"}) if $opt{"weight"};
66     push (@weights, 1.0) if !$opt{"weight"};
67 ahart 1.1 }
68     }
69 ahart 1.3 my $nGoodJobs = @goodJobs;
70     my $nBadJobs = @badJobs;
71     if (!@rootFiles)
72     {
73     print "Found no ROOT files to merge!\n";
74     exit;
75     }
76 ahart 1.1 my $rootFiles = join (" ", @rootFiles);
77 ahart 1.3 my $weights = join (",", @weights);
78     system ("mergeTFileServiceHistograms -i $rootFiles -o $opt{'prefix'}.root -w $weights");
79 ahart 1.1 system ("cutFlowTable $opt{'prefix'}.root cutFlow >& $opt{'prefix'}.tex");
80 ahart 1.4 my $goodEvents = countEvents ("$opt{'prefix'}.root");
81     print "$nGoodJobs jobs ran successfully over $goodEvents events.\n" if $counting;
82     print "$nBadJobs jobs failed to run.\n" if $counting;
83 ahart 1.1
84     sub
85     processArgs
86     {
87     my $argv = shift;
88    
89     my @files;
90     foreach my $arg (@$argv)
91     {
92     $arg =~ s/\/*$//;
93     if (!(-e $arg))
94     {
95     print "$arg does not exist!\n";
96     exit;
97     }
98     next if ($arg =~ m/\/\.$/ || $arg =~ m/\/\.\.$/);
99     if (-d $arg)
100     {
101     opendir (DIR, $arg);
102     my @dirContents = readdir (DIR);
103     closedir (DIR);
104     for (my $i = 0; $i < @dirContents; $i++)
105     {
106     $dirContents[$i] = "$arg/$dirContents[$i]";
107     }
108     my $newFiles = processArgs (\@dirContents);
109     push (@files, @$newFiles);
110     }
111     else
112     {
113     push (@files, $arg);
114     }
115     }
116    
117     return \@files;
118     }
119    
120     sub
121     printHelp
122     {
123     my $exeName = $0;
124     $exeName =~ s/^.*\/([^\/]*)$/$1/;
125    
126 ahart 1.3 print "Usage: $exeName -p PREFIX [OPTION]... DIRECTORIES_AND_FILES\n";
127 ahart 1.1 print "Merges ROOT files containing histograms and produces a cutflow table in the\n";
128     print "form of a LaTeX document from the histogram named \"cutFlow\". If there are\n";
129     print "Condor logs in the specified directories, checks for nonzero return values.\n";
130     print "\n";
131     print "Mandatory arguments to long options are mandatory for short options too.\n";
132     printf "%-29s%s\n", " -h, --help", "print this help message";
133     printf "%-29s%s\n", " -p, --prefix PREFIX", "output files are named PREFIX.root and PREFIX.tex";
134 ahart 1.3 printf "%-29s%s\n", " -w, --weight WEIGHT", "scale the output by WEIGHT";
135 ahart 1.1
136     exit;
137     }
138 ahart 1.3
139     sub
140     getRunList
141     {
142     my $runListFile = shift;
143    
144     open (RUN_LIST, "<$runListFile");
145     my @runList0 = <RUN_LIST>;
146     close (RUN_LIST);
147     my @runList;
148     foreach my $file (@runList0)
149     {
150     next if !($file =~ m/^.*file:.*\.root.*/);
151     $file =~ s/.*file:(.*)\.root.*/$1.root/;
152     push (@runList, $file);
153     }
154    
155     return \@runList;
156     }
157    
158     sub
159     countEvents
160     {
161 ahart 1.4 my $file = shift;
162    
163     my $output = `getEventsFromCutFlow $file cutFlow`;
164     $output =~ s/^.*: (.*)$/$1/;
165     $output =~ s/\n//g;
166    
167     return $output;
168 ahart 1.3 }