ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osusub
Revision: 1.13
Committed: Fri Feb 8 09:17:45 2013 UTC (12 years, 2 months ago) by ahart
Branch: MAIN
Changes since 1.12: +18 -10 lines
Log Message:
Added an option for the maximum number of events to process for a dataset. Actual number processed may differ from the target since job splitting is based on the number of files.

File Contents

# User Rev Content
1 ahart 1.1 #!/usr/bin/env perl
2    
3     use strict;
4     use Mysql;
5     use File::Copy;
6     use Getopt::Long;
7 ahart 1.5 use POSIX;
8 ahart 1.2
9 ahart 1.1 sub printHelp;
10     sub outputPset;
11     sub outputRunList;
12     sub getLocation;
13     sub outputCondor;
14    
15 ahart 1.3 our $db = Mysql->connect ("cmshead", "ntuple", "osuT3User");
16 ahart 1.1
17     my %opt;
18     Getopt::Long::Configure ("bundling");
19 ahart 1.13 GetOptions (\%opt, "maxEvents|m=s", "help|h");
20 ahart 1.1 my $argc = @ARGV;
21    
22     printHelp () if $opt{"help"};
23 ahart 1.12 printHelp () if $argc != 3 && $argc != 4;
24     my $dataset;
25     my $config;
26     my $directory;
27     my $nJobs;
28     if ($argc == 3)
29 ahart 1.1 {
30 ahart 1.12 $dataset = "";
31     $config = $ARGV[0];
32     $directory = $ARGV[1];
33     $nJobs = $ARGV[2];
34     }
35     if ($argc == 4)
36     {
37     $dataset = $ARGV[0];
38     $config = $ARGV[1];
39     $directory = $ARGV[2];
40     $nJobs = $ARGV[3];
41     }
42     if (!(-e $config))
43     {
44     print "\"$config\" does not exist!\n";
45 ahart 1.1 exit;
46     }
47 ahart 1.12 if (-e $directory)
48 ahart 1.1 {
49 ahart 1.12 print "Directory \"$directory\" already exists!\n";
50 ahart 1.1 print "Please delete it or specify another working directory.\n";
51     exit;
52     }
53 ahart 1.12 mkdir $directory;
54     my $nFiles = outputRunList ($dataset, $directory);
55 ahart 1.13 my $realNJobs = $nJobs;
56     $realNJobs = ceil ($nFiles / ceil ($nFiles / $realNJobs)) if $nFiles;
57     my $eventsPerJob = -1;
58     $eventsPerJob = ceil ($opt{"maxEvents"} / $realNJobs) if $opt{"maxEvents"} && $opt{"maxEvents"} >= 0;
59     my $realMaxEvents = $eventsPerJob * $realNJobs;
60     outputPset ($directory, $dataset, $opt{"maxEvents"}, $eventsPerJob);
61     outputCondor ($directory, $realNJobs, $dataset);
62 ahart 1.12 copy ($config, "$directory/userConfig_cfg.py");
63     chdir $directory;
64 ahart 1.13 print "Submitting $realNJobs jobs to run on $realMaxEvents events in $nFiles files.\n" if $realMaxEvents >= 0;
65     print "Submitting $realNJobs jobs to run on all events in $nFiles files.\n" if $realMaxEvents < 0;
66 ahart 1.1 system ("condor_submit condor.sub");
67    
68     sub
69     outputPset
70     {
71     my $workingDir = shift;
72 ahart 1.12 my $dataset = shift;
73 ahart 1.13 my $nEvents = shift;
74     my $eventsPerJob = shift;
75 ahart 1.1
76     open (PSET, ">$workingDir/config_cfg.py");
77    
78     print PSET "import FWCore.ParameterSet.Config as cms\n";
79     print PSET "import OSUT3Analysis.DBTools.osusub_cfg as osusub\n";
80     print PSET "import re\n";
81     print PSET "import userConfig_cfg as pset\n";
82     print PSET "\n";
83     print PSET "fileName = pset.process.TFileService.fileName\n";
84     print PSET "fileName = fileName.pythonValue ()\n";
85     print PSET "fileName = fileName[1:(len (fileName) - 1)]\n";
86     print PSET "fileName = re.sub (r'^(.*)\\.([^\\.]*)\$', r'\\1_' + str (osusub.jobNumber) + r'.\\2', fileName)\n";
87     print PSET "pset.process.TFileService.fileName = fileName\n";
88     print PSET "\n";
89 ahart 1.12 if ($dataset)
90     {
91     print PSET "pset.process.source.fileNames = cms.untracked.vstring (osusub.runList)\n";
92 ahart 1.13 print PSET "pset.process.maxEvents.input = cms.untracked.int32 ($eventsPerJob)\n" if $eventsPerJob < 0;
93 ahart 1.12 }
94 ahart 1.13 print PSET "pset.process.maxEvents.input = cms.untracked.int32 ($eventsPerJob)\n" if $eventsPerJob >= 0;
95 ahart 1.1 print PSET "process = pset.process\n";
96    
97     close (PSET);
98     }
99    
100     sub
101     outputRunList
102     {
103     my $dataset = shift;
104     my $workingDir = shift;
105    
106 ahart 1.12 return 0 if !$dataset;
107 ahart 1.11 my $location;
108     my $nFiles;
109     my $status;
110     my $crossSection;
111     my $isLocation = 0;
112     $isLocation = 1 if -e $dataset;
113     if (!$isLocation)
114     {
115     ($location, $nFiles, $status, $crossSection) = getLocation ($dataset);
116     if ($status ne "present")
117     {
118     print "This dataset is not marked as present on the Tier 3!\n";
119     print "Continue anyway? (y/N): ";
120     my $response = <STDIN>;
121     $response =~ s/\n//g;
122     exit if !$response || lc ($response) ne "y";
123     }
124     if (!(-e $location))
125     {
126     print "The database does not know where this dataset is!\n";
127     exit;
128     }
129 ahart 1.1 }
130 ahart 1.11 else
131 ahart 1.1 {
132 ahart 1.11 $location = $dataset;
133 ahart 1.1 }
134     opendir (LOCATION, $location);
135     my @files = readdir (LOCATION);
136     closedir (LOCATION);
137 ahart 1.11 if (!$isLocation && @files - 2 != $nFiles)
138 ahart 1.1 {
139     print "Number of files does not match database entry!\n";
140 ahart 1.9 print "Continue anyway? (y/N): ";
141     my $response = <STDIN>;
142     $response =~ s/\n//g;
143     exit if !$response || lc ($response) ne "y";
144 ahart 1.1 }
145 ahart 1.11 elsif ($isLocation)
146     {
147     $nFiles = @files - 2;
148     }
149 ahart 1.1 open (RUNLIST, ">$workingDir/runList.py");
150     print RUNLIST "runList = [\n";
151     for (my $i = 0; $i < @files; $i++)
152     {
153     next if $files[$i] eq ".";
154     next if $files[$i] eq "..";
155     print RUNLIST "'file:$location/$files[$i]'";
156     print RUNLIST "," if $i + 1 != @files;
157     print RUNLIST "\n";
158     }
159     print RUNLIST "]";
160     close (RUNLIST);
161 ahart 1.6 if ($crossSection && $crossSection >= 0.0)
162     {
163     open (CROSS_SECTION, ">$workingDir/crossSectionInPicobarn.txt");
164     print CROSS_SECTION "$crossSection\n";
165     close (CROSS_SECTION);
166     }
167 ahart 1.5
168     return $nFiles;
169 ahart 1.1 }
170    
171     sub
172     getLocation
173     {
174     my $dataset = shift;
175    
176     my $results;
177 ahart 1.2 my $queryDataset = $dataset;
178     $queryDataset =~ s/\*/%/g;
179     $queryDataset =~ s/(.*)/%$1%/g;
180 ahart 1.9 my $query = "select dataset,user,creationTime,location,nFiles,status,crossSectionInPicobarn from ntuple where dataset like '$queryDataset' order by creationTime";
181 ahart 1.1 $db->selectdb ("ntuple");
182     $results = $db->query ($query);
183     if ($results->numrows () == 1)
184     {
185     my @row = $results->fetchrow ();
186 ahart 1.6 return ($row[3], $row[4], $row[5], $row[6]);
187 ahart 1.1 }
188     if ($results->numrows () == 0)
189     {
190     print "Dataset does not exist on the Tier 3!\n";
191     exit;
192     }
193     my %map;
194     print "Found multiple datasets matching\n";
195     print "\"$dataset\":\n";
196     for (my $i = 1; $i <= $results->numrows (); $i++)
197     {
198     my @row = $results->fetchrow ();
199 ahart 1.6 $map{"$i"} = [$row[3], $row[4], $row[5], $row[6]];
200 ahart 1.2 printf "(%2d) $row[0]\n", $i;
201     print " created by $row[1] on $row[2]\n";
202 ahart 1.1 }
203     print "\nWhich dataset would you like to use?: ";
204     my $response = <STDIN>;
205     $response =~ s/[ \t\n]//g;
206     if (!(exists $map{$response}))
207     {
208     print "Your selection \"$response\" was not a valid option! Quitting.\n";
209     exit;
210     }
211    
212 ahart 1.6 return ($map{$response}[0], $map{$response}[1], $map{$response}[2], $map{$response}[3]);
213 ahart 1.1 }
214    
215     sub
216     outputCondor
217     {
218     my $workingDir = shift;
219     my $nJobs = shift;
220 ahart 1.8 my $dataset = shift;
221 ahart 1.1
222     my $cmsRun = `which cmsRun`;
223     open (SUB, ">$workingDir/condor.sub");
224    
225     print SUB "Executable = $cmsRun\n";
226     print SUB "Universe = vanilla\n";
227     print SUB "Getenv = True\n";
228 ahart 1.13 print SUB "Arguments = config_cfg.py True $nJobs \$(Process) $dataset\n" if $dataset;
229     print SUB "Arguments = config_cfg.py True $nJobs \$(Process) NULL\n" if !$dataset;
230 ahart 1.1 print SUB "\n";
231     print SUB "Output = condor_\$(Process).out\n";
232     print SUB "Error = condor_\$(Process).err\n";
233     print SUB "Log = condor_\$(Process).log\n";
234     print SUB "\n";
235 ahart 1.7 print SUB "+IsLocalJob = true\n";
236     print SUB "Rank = TARGET.IsLocalSlot\n";
237     print SUB "\n";
238 ahart 1.1 print SUB "Queue $nJobs\n";
239    
240     close (SUB);
241     }
242    
243     sub
244     printHelp
245     {
246     my $exeName = $0;
247     $exeName =~ s/^.*\/([^\/]*)$/$1/;
248    
249 ahart 1.12 print "Usage: $exeName [OPTION]... [DATASET | LOCATION] CONFIG DIRECTORY NJOBS\n";
250 ahart 1.1 print "Submits CMSSW jobs to the OSU Tier 3 compute nodes using Condor.\n";
251     print "\n";
252     printf "%-29s%s\n", " -h, --help", "print this help message";
253 ahart 1.13 printf "%-29s%s\n", " -m, --maxEvents N", "only run over N events in the dataset; default is";
254     printf "%-29s%s\n", " ", "to run over all events";
255 ahart 1.1 print "\n";
256 ahart 1.12 print "The optional first argument must be either a DATASET registered in the Tier 3\n";
257     print "ntuple database or a LOCATION which exists on disk. CONFIG must be a valid\n";
258     print "CMSSW python configuration which can be used with cmsRun. DIRECTORY is a\n";
259     print "working directory that is created and in which all output, both from the CMSSW\n";
260     print "jobs and from Condor, is placed. Finally, NJOBS is the number of Condor jobs\n";
261     print "that will be created.\n";
262 ahart 1.1
263     exit;
264     }