File indexing completed on 2025-12-17 09:19:41
0001
0002
0003
0004
0005 use DBI;
0006 use strict;
0007 use Getopt::Long;
0008 use Data::Dumper;
0009
0010 sub creatfilelists;
0011 sub printdatasets;
0012 sub printdsttypes;
0013 sub printtags;
0014 sub printruns;
0015
0016 my $tag;
0017 my $verbose;
0018 my $runnumber;
0019 my $runlist;
0020 my $printdatasets;
0021 my $printdsttypes;
0022 my $printruns;
0023 my $printtags;
0024 my $hpss;
0025 my $noprint;
0026 my $dataset;
0027
0028 GetOptions('dataset:s' => \$dataset, 'hpss' =>\$hpss, 'list:s' => \$runlist, 'printdatasets' => \$printdatasets, 'printdsttypes' => \$printdsttypes, 'printruns' => \$printruns, 'printtags' => \$printtags, 'run:i' => \$runnumber, 'tag:s' => \$tag, "verbose" =>\$verbose);
0029
0030 my %ignore_datasets = (
0031 "mdc2" => 1,
0032 );
0033
0034 my $dbh = DBI->connect("dbi:ODBC:FileCatalog_read") || die $DBI::error;
0035 $dbh->{LongReadLen}=2000;
0036
0037 if (defined $printdatasets)
0038 {
0039 printdatasets();
0040 }
0041
0042 if (defined $printdsttypes)
0043 {
0044 printdsttypes();
0045 }
0046
0047 if (defined $printtags)
0048 {
0049 printtags();
0050 }
0051
0052 if (defined $printruns)
0053 {
0054 printruns();
0055 }
0056
0057 if ($#ARGV < 0)
0058 {
0059 if (! defined $noprint)
0060 {
0061 print "usage:\n";
0062 print "CreateDstList.pl <dsttype1> <dsttype2> ...\n";
0063 print "parameters:\n";
0064 print "--hpss <print files in hpss>\n";
0065 print "--list <file with list of runs>\n";
0066 print "--printdatasets: print existing datasets\n";
0067 print "--printdsttypes: print existing dst types\n";
0068 print " needs --dataset <dataset> flag\n";
0069 print "--printruns: print existing runs (for piping into a runlist)\n";
0070 print " needs --dataset <dataset> flag\n";
0071 print " needs --tag <tag> flag\n";
0072 print "--printtags: print existing tags\n";
0073 print "--run <run number>\n";
0074 print "--tag <tag (build_cdb_version)> (mandatory)\n";
0075 print "--version <version> (only if we have multiple versions for same build/cdb tag)\n";
0076 print "--verbose print stuff\n";
0077 print "\nfor use with --printruns or --printtags\n";
0078 print "--dataset <dataset (run2pp, run2auau, run3auau, run3cosmics,...)\n";
0079 }
0080 exit(0);
0081 }
0082 my %dsttype = ();
0083 while($#ARGV >= 0)
0084 {
0085 $dsttype{$ARGV[0]} = 1;
0086 shift (@ARGV);
0087 }
0088
0089 if (! defined $tag)
0090 {
0091 print "set tag with --tag <tag>\n";
0092 exit(1);
0093 }
0094 if (! defined $runnumber && ! defined $runlist)
0095 {
0096 print "set run number with --run <run number>\n";
0097 exit(1);
0098 }
0099
0100 my $sqlcmd = sprintf("select filename,dsttype,segment from datasets where runnumber=? and tag = '$tag'");
0101 if (defined $hpss)
0102 {
0103 $sqlcmd = sprintf("%s and status = 0",$sqlcmd)
0104 }
0105 else
0106 {
0107 $sqlcmd = sprintf("%s and status > 0",$sqlcmd)
0108 }
0109 my $getfiles = $dbh->prepare($sqlcmd);
0110 if (defined $runlist)
0111 {
0112 if (! -f $runlist)
0113 {
0114 print "$runlist does not exist\n";
0115 exit(1);
0116 }
0117 open(F2,"$runlist");
0118 {
0119 while (my $run = <F2>)
0120 {
0121 creatfilelists($run);
0122 }
0123 }
0124 }
0125 else
0126 {
0127 creatfilelists($runnumber);
0128 }
0129
0130 sub creatfilelists
0131 {
0132 my %files = ();
0133 my $run = shift;
0134 $getfiles->execute($run);
0135 while(my @res = $getfiles->fetchrow_array)
0136 {
0137 $files{$res[2]}{$res[1]}{$res[0]} = $res[1];
0138 }
0139 foreach my $dsttyp (keys %dsttype)
0140 {
0141 my $filename = sprintf("%s-%08d.list",lc $dsttyp, $run);
0142 open(F,">$filename");
0143 my $fcnt = 0;
0144 foreach my $segment (sort { $a <=> $b } keys %files)
0145 {
0146 foreach my $file (keys %{$files{$segment}{$dsttyp}})
0147 {
0148 print F "$file\n";
0149 $fcnt++;
0150 }
0151 }
0152 close(F);
0153 if ($fcnt == 0)
0154 {
0155 print "no dsts for type $dsttyp for run $run\n";
0156 unlink $filename;
0157 }
0158 else
0159 {
0160 print "wrote $filename\n";
0161 }
0162 }
0163
0164 }
0165
0166 sub printtags
0167 {
0168 my $sqlcmd = sprintf("select datasets.tag,datasets.dataset from datasets group by tag,dataset");
0169 my $icnt = 0;
0170 $sqlcmd = sprintf("%s",$sqlcmd);
0171 if (defined $verbose)
0172 {
0173 print "sql cmd: $sqlcmd\n";
0174 }
0175 my $gettags = $dbh->prepare($sqlcmd);
0176 $gettags->execute();
0177 my %tags = ();
0178 while (my @res = $gettags->fetchrow_array)
0179 {
0180 if (defined $dataset)
0181 {
0182 if ($res[1] eq $dataset)
0183 {
0184 $tags{$res[0]} = $res[1];
0185 }
0186 }
0187 else
0188 {
0189 my $addthis = 1;
0190 foreach my $ignore (keys %ignore_datasets)
0191 {
0192 if ($res[1] eq $ignore)
0193 {
0194 $addthis = 0;
0195 last;
0196 }
0197 }
0198 if (! defined $res[0])
0199 {
0200 next;
0201 }
0202 if ($addthis == 1)
0203 {
0204 $tags{$res[0]} = $res[1];
0205 }
0206 }
0207 }
0208 foreach my $tg (sort keys %tags)
0209 {
0210 print "$tg\n";
0211 }
0212 exit(0);
0213 }
0214
0215 sub printruns
0216 {
0217 my %dsttype = ();
0218 while($#ARGV >= 0)
0219 {
0220 $dsttype{$ARGV[0]} = 1;
0221 shift (@ARGV);
0222 }
0223 my $isgood = 1;
0224 if (!defined $dataset)
0225 {
0226 print "printruns needs --dataset <dataset>\n";
0227 $isgood = 0;
0228 }
0229 if (! defined $tag)
0230 {
0231 print "printruns needs --tag <tag>\n";
0232 $isgood = 0;
0233 }
0234 if ($isgood == 0)
0235 {
0236 exit 1;
0237 }
0238 my $sqlcmd = sprintf("select datasets.runnumber,datasets.dsttype from datasets where dataset = '%s' and tag = '%s' group by runnumber,dsttype",$dataset,$tag);
0239 my $getruns = $dbh->prepare($sqlcmd);
0240 $getruns->execute();
0241 my %selruns = ();
0242 if ($getruns->rows == 0)
0243 {
0244 print "no run found for tag $tag and dataset $dataset\n";
0245 }
0246 else
0247 {
0248 my $icnt = 0;
0249 while (my @res = $getruns->fetchrow_array())
0250 {
0251 if (keys %dsttype > 0 && !exists $dsttype{$res[1]})
0252 {
0253 next;
0254 }
0255 $selruns{$res[0]} = 1;
0256 $icnt++;
0257 }
0258 if ($icnt == 0)
0259 {
0260 print "no run for selected dst types\n";
0261 exit 1;
0262 }
0263 foreach my $rn (sort keys %selruns)
0264 {
0265 print "$rn\n";
0266 }
0267 }
0268 exit(0);
0269 }
0270
0271 sub printdatasets
0272 {
0273 my $sqlcmd = sprintf("select datasets.dataset from datasets group by dataset");
0274 my $getdatasets = $dbh->prepare($sqlcmd);
0275 $getdatasets->execute();
0276 my %ds = ();
0277 while (my @res = $getdatasets->fetchrow_array())
0278 {
0279 my $addthis = 1;
0280 foreach my $ignore (keys %ignore_datasets)
0281 {
0282 if ($res[0] eq $ignore)
0283 {
0284 $addthis = 0;
0285 last;
0286 }
0287 }
0288 if ($addthis == 1)
0289 {
0290 $ds{$res[0]} = 1;
0291 }
0292 }
0293 foreach my $dd (sort keys %ds)
0294 {
0295 print "$dd\n";
0296 }
0297 exit(0);
0298 }
0299
0300 sub printdsttypes
0301 {
0302 if (!defined $dataset)
0303 {
0304 print "printruns needs dataset\n";
0305 }
0306 my $sqlcmd = sprintf("select datasets.dsttype,datasets.tag from datasets where dataset = '%s' group by dsttype,tag",$dataset);
0307 my $getdsttypes = $dbh->prepare($sqlcmd);
0308 $getdsttypes->execute();
0309 my %dsts = ();
0310 while (my @res = $getdsttypes->fetchrow_array())
0311 {
0312 if (defined $tag)
0313 {
0314 if ($res[1] ne $tag)
0315 {
0316 next;
0317 }
0318 }
0319 $dsts{$res[0]} = 1;
0320 }
0321 foreach my $dd (sort keys %dsts)
0322 {
0323 print "$dd\n";
0324 }
0325 exit(0);
0326 }