ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists
(Generate patch)

Comparing UserCode/OSUT3Analysis/AnaTools/scripts/mergeHists (file contents):
Revision 1.23 by lantonel, Wed Apr 10 13:34:17 2013 UTC vs.
Revision 1.28 by ahart, Wed Jul 3 21:30:14 2013 UTC

# Line 8 | Line 8 | sub processArgs;
8   sub printHelp;
9   sub getRunList;
10   sub countEvents;
11 + sub isEDM;
12  
13   my %opt;
14   Getopt::Long::Configure ("bundling");
# Line 16 | Line 17 | GetOptions (\%opt, "cutflow|c=s", "lumin
17   printHelp () if $opt{"help"} || !$opt{"prefix"};
18   my $files = processArgs (\@ARGV);
19   my %rootFiles;
20 + my %skimEventCounts;
21   my %weights;
22   my @weights;
23   my $nGoodJobs = 0;
# Line 83 | Line 85 | foreach my $file (@$files)
85      $dir =~ s/^(.*)\/[^\/]*$/$1/;
86      my $badJob = 0;
87      my $jobNumber;
88 <    if ($file =~ m/^.*_[^_]*\.root$/)
88 >    my $fileIsEDM = 0;
89 >    $fileIsEDM = isEDM ($file) if $file =~ m/^.*\.root$/;
90 >    if ($file =~ m/^.*_[^_]*\.root$/ && !$fileIsEDM)
91        {
92          $jobNumber = $file;
93          $jobNumber =~ s/^.*_([^_]*)\.root$/$1/;
94          $badJob = (defined $signals{$dir} && defined $signals{$dir}{$jobNumber}) || (defined $partial{$dir} && defined $partial{$dir}{$jobNumber});
95        }
96      next if $badJob;
97 <    if ($file =~ m/^.*\.root$/)
97 >    if ($file =~ m/^.*\.root$/ && !$fileIsEDM)
98        {
99          foreach my $arg (@ARGV)
100            {
# Line 105 | Line 109 | foreach my $file (@$files)
109                      last;
110                    }
111                  push (@{$rootFiles{$arg}}, $file);
112 <                push (@weights, $opt{"weight"}) if $opt{"weight"};
112 >                if ($opt{"weight"})
113 >                  {
114 >                    push (@weights, $opt{"weight"}) if !($opt{"weight"} =~ m/,/);
115 >                    push (@weights, 1.0) if $opt{"weight"} =~ m/,/;
116 >                  }
117                  push (@weights, $opt{"xsection"} * $integratedLuminosity) if !$opt{"weight"} && $opt{"xsection"};
118                  push (@weights, $crossSections{$dir} * $integratedLuminosity) if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dir};
119                  push (@weights, 1.0) if !$opt{"weight"} && !$opt{"xsection"} && !(defined $crossSections{$dir});
# Line 113 | Line 121 | foreach my $file (@$files)
121                }
122            }
123        }
124 +    if ($file =~ m/^.*\.root$/ && $fileIsEDM)
125 +      {
126 +        $fileIsEDM -= $fileIsEDM if $fileIsEDM < 0;
127 +        $skimEventCounts{$dir} += $fileIsEDM if defined $skimEventCounts{$dir};
128 +        $skimEventCounts{$dir} = $fileIsEDM if !(defined $skimEventCounts{$dir});
129 +      }
130    }
131   if (!%rootFiles)
132    {
# Line 126 | Line 140 | my @mergedWeights;
140   foreach my $arg (@ARGV)
141    {
142      my $rootFiles = join (" ", @{$rootFiles{$arg}});
143 <    my $tmpName = $arg . "_" . "$opt{'prefix'}.root";
143 >    my $tmpName = "." . $arg . "_" . "$opt{'prefix'}.root";
144      $tmpName =~ s/\//_/g;
145      system ("mergeTFileServiceHistograms -i $rootFiles -o $tmpName");
146      my $count = countEvents ($tmpName, $cutFlow);
# Line 137 | Line 151 | foreach my $arg (@ARGV)
151      $weights{$arg} /= $count if !$opt{"weight"} && !$opt{"xsection"} && defined $crossSections{$dirs{$arg}};
152      push (@mergedFiles, $tmpName);
153      push (@mergedWeights, $weights{$arg});
154 +    foreach my $rootFile (@{$rootFiles{$arg}})
155 +      {
156 +        my $weight = $weights{$arg};
157 +        system ("weightTrees $rootFile $weight");
158 +      }
159 +    if (defined $crossSections{$dirs{$arg}})
160 +      {
161 +        open (EVENT_COUNT, ">$dirs{$arg}/numberOfEvents.txt");
162 +        print EVENT_COUNT sprintf ("%.0f", $count) . "\n";
163 +        close (EVENT_COUNT);
164 +      }
165    }
166   my $mergedFiles = join (" ", @mergedFiles);
167   my $mergedWeights = join (",", @mergedWeights);
168 + $mergedWeights = $opt{"weight"} if $opt{"weight"} && $opt{"weight"} =~ m/,/;
169   system ("mergeTFileServiceHistograms -i $mergedFiles -o $opt{'prefix'}.root -w $mergedWeights");
170 + foreach my $dir (keys %skimEventCounts)
171 +  {
172 +    open (EDM_EVENTS, ">$dir/skimNumberOfEvents.txt");
173 +    print EDM_EVENTS $skimEventCounts{$dir} . "\n";
174 +    close (EDM_EVENTS);
175 +  }
176   foreach my $mergedFile (@mergedFiles)
177    {
178      unlink ("$mergedFile");
# Line 180 | Line 212 | foreach my $arg (keys %weights)
212   print $output if $printOutput;
213  
214   my $goodEvents = countEvents ("$opt{'prefix'}.root", $cutFlow);
215 < print "$nGoodJobs jobs ran successfully over $nTotalEvents ($goodEvents weighted) events.\n" if $counting;
215 > printf "$nGoodJobs jobs ran successfully over $nTotalEvents (%.1f weighted) events.\n", $goodEvents if $counting;
216   print "$nBadJobs jobs failed to run.\n" if $counting;
217   print "$nIncompleteJobs jobs have not finished.\n" if $counting;
218   print "=============================================\n";
# Line 237 | Line 269 | printHelp
269    printf "%-29s%s\n", "  -c, --cutflow HISTOGRAM", "name of histogram to use for the cutflow (default:";
270    printf "%-29s%s\n", "                  ", "cutFlow)";
271    printf "%-29s%s\n", "  -h, --help", "print this help message";
272 <  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are weighted";
272 >  printf "%-29s%s\n", "  -l, --luminosity", "integrated luminosity to which the histograms are";
273 >  printf "%-29s%s\n", "                  ", "weighted (default: 10000/pb)";
274    printf "%-29s%s\n", "  -p, --prefix PREFIX", "output is  named PREFIX.root";
275    printf "%-29s%s\n", "  -w, --weight WEIGHT", "scale the output by WEIGHT, overriding the";
276    printf "%-29s%s\n", "                     ", "automatic weighting using the cross section from";
277 <  printf "%-29s%s\n", "                     ", "the database";
277 >  printf "%-29s%s\n", "                     ", "the database; WEIGHT may be a single number or a";
278 >  printf "%-29s%s\n", "                     ", "comma-separated list, one for each input file";
279    printf "%-29s%s\n", "  -x, --xsection XSECTION", "use XSECTION to weight the histograms instead of";
280    printf "%-29s%s\n", "                     ", "the value in the database";
281  
# Line 284 | Line 318 | countEvents
318  
319    return $output;
320   }
321 +
322 + sub
323 + isEDM
324 + {
325 +  my $file = shift;
326 +
327 +  my $output = `edmFileUtil $file 2>&1`;
328 +  return 0 if $output =~ m/appears to be missing/ || $output =~ m/not a ROOT file/;
329 +  $output =~ s/[\f\n\r]//g;
330 +  $output =~ s/^.*, ([^,]*) events,.*$/$1/;
331 +  $output += 0;
332 +  $output-- if $output == 0;
333 +  return $output;
334 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines