Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:50

0001 #!/usr/bin/env perl
0002 
0003 # SELMA - Saclay Ecal Laser Monitoring and Analysis
0004 # contacts: Julie Malcles, Gautier Hamel de Monchenault, Serguei Ganjour, Patrice Verrecchia
0005 # last modified: Sat Jul 28 09:58:19 CEST 2007
0006 
0007 use Term::ANSIColor;
0008 #use Date::Manip; 
0009 use Cwd;
0010 
0011 $firstRun     = @ARGV[0];
0012 $lastRun      = @ARGV[1];
0013 $fed          = @ARGV[2];
0014 $ecalmod      = @ARGV[3];
0015 $useShape     = @ARGV[4];
0016 $fitAB        = @ARGV[5];
0017 $linkname     = @ARGV[6];
0018 $user         = @ARGV[7];
0019 $nmaxjobshead = @ARGV[8];
0020 $debug        = @ARGV[9];
0021  
0022 #  Load some usefull functions
0023 #==============================
0024 
0025 
0026 die unless( $ecalmod =~ /E(E|B)(\+|-)(\d*)/ );
0027 
0028 $localdir     = cwd;
0029 if (  $linkname eq "" ) {
0030     $proddir      = "${localdir}/LaserMonitoring";
0031 }else{
0032     $proddir      = "${localdir}/${linkname}";
0033 }
0034 
0035 $sortdir      = "$proddir/sorting";
0036 $scriptdir    = "$proddir/scripts";
0037 $shapedir     = "$proddir/shapes";
0038 $alphabetadir = "$proddir/alphabeta";
0039 $templatesdir = "$proddir/templates";
0040 $lmfdir       = "$proddir/sorting/${ecalmod}";
0041 $runsdir      = "$proddir/${ecalmod}/Runs";
0042 $laserdir     = "$proddir/${ecalmod}/Laser";
0043 $testpulsedir = "$proddir/${ecalmod}/TestPulse";
0044 
0045 do "${scriptdir}/monitoringFunctions.pl";
0046 
0047 print color("red"), "\n\n***** Saclay Ecal Laser Monitoring & Analysis *****\n\n", color("reset");
0048 
0049 while( 1 ) 
0050 {
0051     system date;
0052     doDetect();
0053     sleep 1;
0054 }
0055 
0056 exit;
0057 
0058 
0059 sub doDetect
0060 
0061 { 
0062     print color("green"), "\n *** Restarting File Detection for FED: ${fed} ***\n\n", color("reset");
0063     
0064     opendir(POOLDIR, $lmfdir) || die "cannot open sorting directory  $lmfdir \n";
0065     
0066     $dirname = "";
0067     $lbname = "";
0068     $run     = 0;
0069     
0070     my @poolfiles = sort  readdir( POOLDIR );
0071 
0072     # Detect news sequences
0073     #=======================
0074 
0075     foreach my $file (@poolfiles)
0076     {
0077     if( $debug == 1 ) {
0078         print "LMF File: ${file} detected.\n";
0079     }
0080     next unless( $file =~ /(.*)_(.*).lmf/ );
0081 
0082     $dirname = $1; 
0083     $lbname = $2;
0084 
0085     # check file is fully copied (no .part)
0086     next unless (  $file eq "${dirname}_${lbname}.lmf" );
0087 
0088     if( $debug == 1 ) {
0089         print "  dirname = $dirname \n";
0090         print "  lbname = $lbname \n";
0091     }
0092 
0093     next unless( $dirname =~ /Run(\d*)/ );
0094     $run    = $1;
0095 
0096     next unless( $lbname =~ /LB(\d*)/ );
0097     $lb    = $1;
0098 
0099     $dirname=$dirname."_LB".$lb;
0100     
0101     if( $debug == 1 ) {
0102         print "DirName: $dirname \n ";
0103         print " $run $firstRun $lastRun $fed \n";
0104     }
0105     
0106     next unless( $run >= $firstRun && $run <= $lastRun );
0107 
0108     my $ecalmod = "";
0109     $ecalmod = getSMname($fed);
0110 
0111     my $mydate = `date +%s`;
0112     print " Run ${run} and LB ${lb} found for fed ${fed} (${ecalmod}) at: ${mydate} \n";    
0113     system date;    
0114 
0115 
0116     my $smdir      = "${proddir}/${ecalmod}";
0117     my $runsdir    = "${smdir}/Runs";
0118     my $jobdir     = "${runsdir}/${dirname}";
0119     my $command;
0120 
0121     # skip if already analyzed
0122     
0123     my $doesHeaderExist=checkHeader(${jobdir});
0124     if( $debug == 1 ){
0125         print "doesHeaderExist: $doesHeaderExist\n";
0126     }   
0127     next if( $doesHeaderExist == 1 );
0128     next if(-e "${laserdir}/Detected/${dirname}");
0129     
0130 
0131     if( $debug == 1 ) {
0132         print "\nRun number = $run  ";
0133         print "\n";
0134     }
0135     
0136     if( -e ${smdir} ) 
0137     {
0138         if( $debug == 1 ) {
0139         print "${smdir} exists.\n";
0140         }
0141     }else{
0142         print "creating directory ${smdir}\n";
0143         $command =  "${scriptdir}/createDirs.csh ${proddir} ${ecalmod}" ;
0144         system ${command};
0145     }
0146     
0147     if(-e ${jobdir} ){
0148         if( $debug == 1 ) {
0149         print "${jobdir} exists.\n";
0150         }
0151     }else{
0152         
0153         print "creating directory ${jobdir}\n";
0154         $command =  "mkdir ${jobdir}" ;
0155         system ${command};
0156 
0157         # link to the pool file
0158         $command = "ln -sf ${lmfdir}/${file} ${jobdir}/input.lmf";
0159         system  ${command};
0160         
0161         # link to the shapes (elec shapes)
0162         if( $useShape == 1 ) {
0163         $command = "ln -sf ${shapedir}/ElecMeanShape.root  ${jobdir}/ElecShape.root ";
0164         system  ${command};
0165         }elsif( $fitAB ){
0166         # link to the alpha beta init file
0167         $command = "ln -sf ${alphabetadir}/AB${fed}.root  ${jobdir}/ABInit.root ";
0168         if(-e "${alphabetadir}/AB${fed}.root"){
0169             system  ${command};
0170         }
0171         }else{
0172         # link to the alpha beta file
0173         $command = "ln -sf ${alphabetadir}/AB${fed}.root  ${jobdir}/ABInit.root ";
0174         if(-e "${alphabetadir}/AB${fed}.root"){
0175             system  ${command};
0176         }
0177         $command = "ln -sf ${alphabetadir}/AB${fed}.root  ${jobdir}/AB.root ";
0178         if(-e "${alphabetadir}/AB${fed}.root"){
0179             system  ${command};
0180         }
0181         }
0182 
0183     }
0184 
0185     my $mydate = `date +%s`;
0186     print " ... done at: ${mydate} \n";
0187     system date;    
0188 
0189     
0190     # Analyze
0191     #=========
0192     
0193     my $type = getType( $file );
0194     }
0195     
0196     close(POOLDIR);
0197 }
0198 
0199 sub getType
0200 {
0201 
0202     my $type = "UNKNOWN";
0203     my $runfromname  = 0;
0204     my $lb  = 0;
0205     my $dirname = "";
0206     my $lbname = "";
0207 
0208     my $file = shift;
0209     
0210     if($debug==1){
0211     print "In getType, file: $file \n";
0212     }
0213     
0214     if( $file  =~ /(.*)_(.*).lmf/ ){
0215     $dirname = $1;
0216     $lbname = $2;       
0217     }
0218 
0219     if( $dirname =~ /Run(\d*)/ ){
0220     $runfromname   = $1;
0221     }
0222     if( $lbname =~ /LB(\d*)/ ){
0223     $lbfromname   = $1;
0224     }
0225     
0226     $dirname=$dirname."_".$lbname;
0227     
0228     my $ecalmodfromname = getSMname($fed);
0229     
0230     my $smdir   = "${proddir}/${ecalmodfromname}";
0231     my $runsdir = "${smdir}/Runs";
0232     my $jobdir  = "${runsdir}/${dirname}";
0233 
0234     # Run StatusMon to get the sequence header
0235     #==========================================
0236 
0237     print "Get the sequence header\n";
0238    
0239    
0240    # my $command = "cp ${templatesdir}/header.py ${jobdir}/header.py";
0241    # system $command;
0242  
0243     open( TEMPLATE, "${templatesdir}/header.py" );
0244     open( CFGFILE, ">${jobdir}/header.py" );
0245     while( <TEMPLATE> )
0246     {  
0247     $_ =~ s/FFFF/$fed/g;
0248     $_ =~ s/DDDD/$debug/g;
0249     print CFGFILE $_;
0250     }
0251     close CFGFILE;
0252     close TEMPLATE;
0253     
0254     my $key="header";
0255     my $isItSent = sendCMSJob(${key}, ${key}, ${nmaxjobshead}, ${user}, ${jobdir}, ${scriptdir});        
0256     if( $isItSent == 1 ){ print "....... done\n"; }
0257     
0258 
0259     # Read the sequence header to run other jobs for each SM
0260     #=======================================================
0261 
0262     my $headerfile = "${jobdir}/header.txt";
0263 
0264     if (open( HEADERFILE, "${headerfile}")){
0265     
0266     while (<HEADERFILE>)
0267     {
0268         chomp($_);
0269         my $theLine = $_;
0270         if( $theLine =~ /RUNTYPE = (\d*)/ )
0271         {
0272         if ( $1 eq 0 ) 
0273         { $type = "COSMIC"; } 
0274         elsif ( $1 eq 4 )
0275         { $type = "LASER_STD"; } 
0276         elsif ( $1 eq 5 )
0277         { $type = "LASER_POWER_SCAN"; } 
0278         elsif ( $1 eq 6 )
0279         { $type = "LASER_DELAY_SCAN"; } 
0280         elsif ( $1 eq 7 ) 
0281         { $type = "TESTPULSE_SCAN_MEM"; }
0282         elsif ( $1 eq 8 ) 
0283         { $type = "TESTPULSE_MGPA"; }
0284         elsif ( $1 eq 9 ) 
0285         { $type = "PEDESTAL_STD"; }
0286         elsif ( $1 eq 10 ) 
0287         { $type = "PEDESTAL_OFFSET_SCAN"; }
0288         elsif ( $1 eq 11 ) 
0289         { $type = "PEDESTAL_25NS_SCAN"; }
0290         elsif ( $1 eq 12 ) 
0291         { $type = "LED_STD"; }
0292         elsif ( $1 eq 16 ) 
0293         { $type = "LASER_GAP"; }
0294         elsif ( $1 eq 17 ) 
0295         { $type = "TESTPULSE_GAP"; }
0296         elsif ( $1 eq 18 ) 
0297         { $type = "PEDESTAL_GAP"; }
0298         elsif ( $1 eq 19 ) 
0299         { $type = "LED_GAP"; }
0300         next;
0301         }
0302         if( $theLine =~ /FEDID = (\d*)/ )
0303         {
0304         $fed = $1;
0305         $ecalmod = getSMname($fed);
0306         
0307         if( ( $type =~ /LASER/ ) ||  ( $type =~ /TESTPULSE/ ) ){
0308             
0309             print "Type is $type\n";
0310             print "ECAL module is ${ecalmod} \n";
0311             
0312             $smdir   = "${proddir}/${ecalmod}";
0313             $runsdir = "${smdir}/Runs";
0314             $jobdir  = "${runsdir}/${dirname}";
0315             
0316             
0317             if  ( $ecalmodfromname eq $ecalmod ){
0318             
0319             # Set Pointers 
0320             #==============
0321             
0322             my $analyzedlaserdir     = "${laserdir}/Analyzed";
0323             my $analyzedtestpulsedir = "${testpulsedir}/Analyzed";
0324             
0325             if( $type =~ /LASER/ )
0326             {
0327                 # skip if already analyzed
0328                 
0329                 if(-e "${analyzedlaserdir}/${dirname}") {
0330                 print " Laser sequence already analyzed: ${laserdir}, ${dirname} \n"; 
0331                 }else{
0332                 
0333                 # otherwise, set pointer to the last detected run
0334                 print " Setting Laser Pointers  ${laserdir}, ${dirname} \n"; 
0335                 if( -e "${laserdir}/lastDetected" ) { system "rm -f ${laserdir}/lastDetected"; }
0336                 system "ln -sf    ../Runs/${dirname} ${laserdir}/lastDetected";
0337                 system "ln -sf ../../Runs/${dirname} ${laserdir}/Detected/${dirname}";
0338                 }
0339             }
0340             elsif( $type =~ /TESTPULSE/ )
0341             {
0342                 
0343                 # skip if already analyzed
0344                 
0345                 if(-e "${analyzedtestpulsedir}/${dirname}") {
0346                 print " TP sequence already analyzed: ${laserdir}, ${dirname} \n"; 
0347                 }else{
0348                 
0349                 # otherwise, set pointer to the last detected run
0350                 print " Setting TP Pointers ${testpulsedir}, ${dirname} \n"; 
0351                 # set pointer to the last detected run
0352                 if( -e "${testpulsedir}/lastDetected" ) { system "rm -f ${testpulsedir}/lastDetected"; }
0353                 system "ln -sf    ../Runs/${dirname} ${testpulsedir}/lastDetected";
0354                 system "ln -sf ../../Runs/${dirname} ${testpulsedir}/Detected/${dirname}";
0355                 }
0356             }
0357             }  
0358             else
0359             { 
0360             print " FED ID $ecalmod does not match fed id in file name $ecalmodfromname: problem must have occured while sorting ";
0361             
0362             }
0363         }  
0364         next;
0365         }
0366     }
0367     
0368     close HEADERFILE;
0369     
0370     }else{
0371     print "cannot open header file in getType \n";
0372     }
0373     
0374     return $type;
0375 }
0376 
0377 
0378 sub checkHeader
0379 {
0380 
0381     my $dir    = shift;
0382     my $headerDone = 0;
0383 
0384     if( -e "${dir}/header.txt" ){
0385     my $nlines=`grep done ${dir}/header.txt | wc -l`;  
0386     if ( $nlines == 0 ){
0387         $headerDone=0;
0388     }else{ 
0389         $headerDone=1;
0390     }
0391     }
0392     return $headerDone;
0393 }