ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osusub
Revision: 1.15
Committed: Mon Mar 25 18:26:58 2013 UTC (12 years, 1 month ago) by ahart
Branch: MAIN
Changes since 1.14: +5 -3 lines
Log Message:
Select the higher-order cross section from the database when it exists.

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 ahart 1.14 outputCondor ("$ENV{'CMSSW_BASE'}/src/OSUT3Analysis/DBTools/data/condor.sub", $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.15 my $query = "select dataset,user,creationTime,location,nFiles,status,crossSectionInPicobarn,higherOrderCrossSectionInPicobarn 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.15 return ($row[3], $row[4], $row[5], $row[7]) if $row[7];
187     return ($row[3], $row[4], $row[5], $row[6]) if !$row[7];
188 ahart 1.1 }
189     if ($results->numrows () == 0)
190     {
191     print "Dataset does not exist on the Tier 3!\n";
192     exit;
193     }
194     my %map;
195     print "Found multiple datasets matching\n";
196     print "\"$dataset\":\n";
197     for (my $i = 1; $i <= $results->numrows (); $i++)
198     {
199     my @row = $results->fetchrow ();
200 ahart 1.15 $map{"$i"} = [$row[3], $row[4], $row[5], $row[7]] if $row[7];
201     $map{"$i"} = [$row[3], $row[4], $row[5], $row[6]] if !$row[7];
202 ahart 1.2 printf "(%2d) $row[0]\n", $i;
203     print " created by $row[1] on $row[2]\n";
204 ahart 1.1 }
205     print "\nWhich dataset would you like to use?: ";
206     my $response = <STDIN>;
207     $response =~ s/[ \t\n]//g;
208     if (!(exists $map{$response}))
209     {
210     print "Your selection \"$response\" was not a valid option! Quitting.\n";
211     exit;
212     }
213    
214 ahart 1.6 return ($map{$response}[0], $map{$response}[1], $map{$response}[2], $map{$response}[3]);
215 ahart 1.1 }
216    
217     sub
218     outputCondor
219     {
220 ahart 1.14 my $condorFileName = shift;
221 ahart 1.1 my $workingDir = shift;
222     my $nJobs = shift;
223 ahart 1.8 my $dataset = shift;
224 ahart 1.1
225     my $cmsRun = `which cmsRun`;
226 ahart 1.14 my $condorFile = "";
227 ahart 1.1
228 ahart 1.14 if (!(-e $condorFileName))
229     {
230     $condorFile .= "Executable = $cmsRun\n";
231     $condorFile .= "Universe = vanilla\n";
232     $condorFile .= "Getenv = True\n";
233     $condorFile .= "Arguments = config_cfg.py True $nJobs \$(Process) $dataset\n" if $dataset;
234     $condorFile .= "Arguments = config_cfg.py True $nJobs \$(Process) NULL\n" if !$dataset;
235     $condorFile .= "\n";
236     $condorFile .= "Output = condor_\$(Process).out\n";
237     $condorFile .= "Error = condor_\$(Process).err\n";
238     $condorFile .= "Log = condor_\$(Process).log\n";
239     $condorFile .= "\n";
240     $condorFile .= "+IsLocalJob = true\n";
241     $condorFile .= "Rank = TARGET.IsLocalSlot\n";
242     $condorFile .= "\n";
243     $condorFile .= "Queue $nJobs\n";
244     }
245     else
246     {
247     open (SUB, "<$condorFileName");
248     my @condorFile = <SUB>;
249     close (SUB);
250     $condorFile = join ("", @condorFile);
251     $condorFile =~ s/\$cmsRun/$cmsRun/g;
252     $condorFile =~ s/\$nJobs/$nJobs/g;
253     $condorFile =~ s/\$dataset/$dataset/g if $dataset;
254     $condorFile =~ s/\$dataset/NULL/g if !$dataset;
255     }
256 ahart 1.1
257 ahart 1.14 open (SUB, ">$workingDir/condor.sub");
258     print SUB $condorFile;
259 ahart 1.1 close (SUB);
260     }
261    
262     sub
263     printHelp
264     {
265     my $exeName = $0;
266     $exeName =~ s/^.*\/([^\/]*)$/$1/;
267    
268 ahart 1.12 print "Usage: $exeName [OPTION]... [DATASET | LOCATION] CONFIG DIRECTORY NJOBS\n";
269 ahart 1.1 print "Submits CMSSW jobs to the OSU Tier 3 compute nodes using Condor.\n";
270     print "\n";
271     printf "%-29s%s\n", " -h, --help", "print this help message";
272 ahart 1.13 printf "%-29s%s\n", " -m, --maxEvents N", "only run over N events in the dataset; default is";
273     printf "%-29s%s\n", " ", "to run over all events";
274 ahart 1.1 print "\n";
275 ahart 1.12 print "The optional first argument must be either a DATASET registered in the Tier 3\n";
276     print "ntuple database or a LOCATION which exists on disk. CONFIG must be a valid\n";
277     print "CMSSW python configuration which can be used with cmsRun. DIRECTORY is a\n";
278     print "working directory that is created and in which all output, both from the CMSSW\n";
279     print "jobs and from Condor, is placed. Finally, NJOBS is the number of Condor jobs\n";
280     print "that will be created.\n";
281 ahart 1.1
282     exit;
283     }