ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osusub
Revision: 1.11
Committed: Thu Jan 17 20:51:46 2013 UTC (12 years, 3 months ago) by ahart
Branch: MAIN
Changes since 1.10: +33 -14 lines
Log Message:
Allow for a location to be given instead of a dataset name.

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