Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:33:12

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import os
0005 import sys
0006 import fileinput
0007 import string
0008 
0009 ##########################################################################
0010 ##########################################################################
0011 ######### User variables
0012 
0013 #Run on FastSim events if true
0014 FastSimUse="False"
0015 
0016 #(post) process only recoMuon related histos
0017 onlyRecoMuons="False"
0018 
0019 #Release to be validated:
0020 NewRelease='CMSSW_3_8_0_pre1'
0021 
0022 # startup and ideal sample list
0023 if (FastSimUse=="True"):
0024     startupsamples= ['RelValTTbar']
0025     idealsamples= ['RelValSingleMuPt10', 'RelValSingleMuPt100', 'RelValTTbar']
0026 else:
0027     startupsamples= ['RelValTTbar','RelValZMM','RelValJpsiMM']
0028     idealsamples= ['RelValSingleMuPt10', 'RelValSingleMuPt100', 'RelValSingleMuPt1000', 'RelValTTbar']
0029 
0030 
0031 # track algorithm name and quality. Can be a list.
0032 Algos= ['']
0033 Qualities=['']
0034 #Qualities=['', 'highPurity']
0035 
0036 #Leave unchanged unless the track collection name changed
0037 Tracksname=''
0038 
0039 # Sequence. Possible values:
0040 #   -only_validation
0041 #   -re_tracking
0042 #   -digi2track
0043 #   -only_validation_and_TP
0044 #   -re_tracking_and_TP
0045 #   -digi2track_and_TP
0046 #   -harvesting
0047 
0048 #Sequence='only_validation_and_TP'
0049 Sequence='harvesting'
0050 
0051 Submit=False
0052 #DBS=False  # Ineffective...
0053 DBS=True
0054 OneAtATime=False
0055 
0056 # Ideal and Statup tags
0057 IdealTag='MC'
0058 StartupTag='STARTUP'
0059 
0060 IdealTagUse='MC_37Y_V5'
0061 StartupTagUse='START37_V5'
0062 VersionTag='v1'
0063 #UnwantedTag=''
0064 
0065 # Reference directory name (the macro will search for ReferenceSelection_Quality_Algo)
0066 ReferenceSelection='IDEAL_31X__noPU'
0067 StartupReferenceSelection='STARTUP_31X_noPU'
0068 
0069 # Default label is GlobalTag_noPU__Quality_Algo. Change this variable if you want to append an additional string.
0070 if (FastSimUse=="True"):
0071     NewSelectionLabel='_FSIM'
0072 else:
0073     NewSelectionLabel=''
0074 
0075 WorkDirBase = '/tmp/aperrott/'+NewRelease+'/src/Validation/RecoMuon/test'
0076 #WorkDirBase = '/afs/cern.ch/user/a/aeverett/scratch0'
0077 #WorkDirBase = '/afs/cern.ch/user/a/aperrott/scratch0/'+NewRelease+'/src/Validation/RecoMuon/test'
0078 
0079 #Reference and new repository
0080 RefRepository = '/afs/cern.ch/cms/Physics/muon/CMSSW/Performance/RecoMuon/Validation/val'
0081 NewRepository = '/afs/cern.ch/cms/Physics/muon/CMSSW/Performance/RecoMuon/Validation/val'
0082 
0083 #Default Nevents
0084 defaultNevents ='-1'
0085 
0086 #Put here the number of event to be processed for specific samples (numbers must be strings) if not specified is -1:
0087 Events={} #{ 'RelValZMM':'5000', 'RelValTTbar':'5000'}
0088 #Events={ 'RelValZMM':'5000', 'RelValTTbar':'5000','RelValSingleMuPt10':'10000',  'RelValSingleMuPt100':'10000'}
0089 
0090 # template file names. Usually should not be changed.
0091 cfg='muonReleaseValidationFastSim_cfg.py'
0092 macro='macro/TrackValHistoPublisher.C'
0093 
0094 # Define a different process name for each sequence:
0095 ProcessName = 'MUVAL'
0096 if (Sequence=='harvesting'):
0097     ProcessName = 'MUVALHV'
0098 elif (Sequence=='only_validation_and_TP'):
0099     ProcessName = 'MUVALTP'
0100 
0101 
0102 #########################################################################
0103 #########################################################################
0104 ############ Functions
0105 
0106 def replace(map, filein, fileout):
0107     replace_items = map.items()
0108     while True:
0109         line = filein.readline()
0110         if not line: break
0111         for old, new in replace_items:
0112             line = string.replace(line, old, new)
0113         fileout.write(line)
0114     fileout.close()
0115     filein.close()
0116     
0117 ############################################
0118 
0119     
0120 def do_validation(samples, GlobalTag, trackquality, trackalgorithm):
0121     global Sequence, RefSelection, RefRepository, NewSelection, NewRepository, defaultNevents, Events, GlobalTagUse
0122     global cfg, macro, Tracksname
0123     print('Search Tag: ' + GlobalTag)
0124     print('Tag to use: ' + GlobalTagUse)
0125 
0126     #build the New Selection name
0127     NewSelection=GlobalTag +'_noPU'
0128     if( trackquality !=''):
0129         NewSelection+='_'+trackquality
0130     if(trackalgorithm!=''):
0131         NewSelection+='_'+trackalgorithm
0132     if(trackquality =='') and (trackalgorithm==''):
0133         if(Tracksname==''):
0134             NewSelection+='_ootb'
0135             Tracks='generalTracks'
0136         else:
0137            NewSelection+= Tracks
0138     elif(Tracksname==''):
0139         Tracks='cutsRecoTracks'
0140     else:
0141         Tracks=Tracksname
0142     NewSelection+=NewSelectionLabel
0143 
0144     #loop on all the requested samples
0145     for sample in samples :
0146         print('Get information from DBS for sample', sample)
0147 
0148         newdir=NewRepository+'/'+NewRelease+'/'+NewSelection+'/'+sample 
0149 
0150         WorkDir = WorkDirBase+'/'+NewRelease+'/'+NewSelection+'/'+sample
0151 
0152         if(os.path.exists(WorkDir)==False):
0153             os.makedirs(WorkDir)
0154 
0155         
0156         #chech if the sample is already done
0157         if(os.path.isfile(newdir+'/val.'+sample+'.rootytootie' )!=True):    
0158             
0159             #search the primary dataset
0160             cmd='python3 $DBSCMD_HOME/dbsCommandLine.py "find dataset where dataset like *'
0161             #search for correct EventContent (and site)
0162             if (FastSimUse=="True"):
0163                 cmd+=sample+'/'+NewRelease+'-'+GlobalTagUse+'*GEN-SIM-DIGI-RECO* AND site like *cern* "'
0164 #                cmd+='|grep '+sample+'|grep '+VersionTag+'|grep -v '+UnwantedTag+'|grep FastSim |sort| tail -1 | cut -d "," -f2 '
0165                 cmd+='|grep '+sample+'|grep '+VersionTag+'|grep FastSim |sort| tail -1 | cut -d "," -f2 '
0166             else:
0167 #                cmd+=sample+'/'+NewRelease+'_'+GlobalTagUse+'*GEN-SIM-DIGI-RAW-HLTDEBUG-RECO* AND site like *cern* "'
0168                 cmd+=sample+'/'+NewRelease+'-'+GlobalTagUse+'*GEN-SIM-RECO* AND site like *cern* "'
0169 #                cmd+='|grep '+sample+'|grep '+VersionTag+'|grep -v '+UnwantedTag+'|grep -v FastSim |sort| tail -1 | cut -d "," -f2 '
0170                 cmd+='|grep '+sample+'|grep '+VersionTag+'|grep -v FastSim |sort| tail -1 | cut -d "," -f2 '
0171             print(cmd)
0172             dataset= os.popen(cmd).readline()
0173             print('DataSet:  ', dataset, '\n')
0174             
0175             #Check if a dataset is found
0176             if(dataset!="" or DBS==False):
0177                 print('dataset found ', dataset[:-1])
0178                 #Find and format the list of files
0179                 cmd2='python3 $DBSCMD_HOME/dbsCommandLine.py "find file where dataset like '+ dataset[:-1] +'"|grep ' + sample 
0180 
0181                 thisFile=0
0182                 for thisFilename in os.popen(cmd2).readlines():
0183                     templatecfgFile = open(cfg, 'r')
0184                     thisFile=thisFile+1
0185                     filenames='import FWCore.ParameterSet.Config as cms\n\n'
0186                     filenames+='readFiles = cms.untracked.vstring()\n'
0187                     filenames+='secFiles = cms.untracked.vstring()\n'
0188                     filenames+='source = cms.Source ("PoolSource",fileNames = readFiles, secondaryFileNames = secFiles)\n'
0189                     filenames+='readFiles.extend( [\n'
0190                     if dataset!="":
0191                         if (OneAtATime==False):
0192                             for filename in os.popen(cmd2).readlines():
0193                                 filenames+='"'+filename[:-1]+'",'
0194                         else:
0195                             filenames+='"'+thisFilename+'",'
0196                     filenames+=']);\n'
0197 
0198                 
0199                 # if not harvesting find secondary file names
0200                     if(dataset!="" and Sequence!="harvesting"):
0201                         print('Getting secondary files')
0202                         if (FastSimUse=="True"):
0203                             cmd3=cmd
0204                         else:
0205                             cmd3='python3 $DBSCMD_HOME/dbsCommandLine.py "find dataset.parent where dataset like '+ dataset[:-1] +'"|grep ' + sample
0206                         parentdataset=os.popen(cmd3).readline()
0207                         print('Parent DataSet:  ', parentdataset, '\n')
0208                     
0209                         #Check if a dataset is found
0210                         if parentdataset!="":
0211                             cmd4='python3 $DBSCMD_HOME/dbsCommandLine.py "find file where dataset like '+ parentdataset[:-1] +'"|grep ' + sample 
0212                             filenames+='secFiles.extend( [\n'
0213                             first=True                        
0214                             for line in os.popen(cmd4).readlines():
0215                                 filenames+='"'+line[:-1]+'",'
0216                             filenames+=']);\n'
0217                         else :
0218                             print("No primary dataset found skipping sample: ", sample)
0219                             continue
0220                     else :
0221                         filenames+='secFiles.extend( (               ) )\n'
0222 
0223                     cfgFileName=('%s_%d') % (sample,thisFile)
0224                     print('cfgFileName ' + cfgFileName)
0225                     cfgFile = open(cfgFileName+'.py' , 'w' )
0226                     cfgFile.write(filenames)
0227 
0228                     if ((sample in Events)!=True):
0229                         Nevents=defaultNevents
0230                     else:
0231                         Nevents=Events[sample]
0232                     print('line 199')
0233                     symbol_map = { 'PROCESSNAME':ProcessName, 'NEVENT':Nevents, 'GLOBALTAG':GlobalTagUse, 'SEQUENCE':Sequence, 'SAMPLE': sample, 'ALGORITHM':trackalgorithm, 'QUALITY':trackquality, 'TRACKS':Tracks, 'FASTSIM':FastSimUse, 'ONLYRECOMUONS':onlyRecoMuons}
0234 
0235 
0236                     cfgFile = open(cfgFileName+'.py' , 'a' )
0237                     replace(symbol_map, templatecfgFile, cfgFile)
0238 
0239                     cmdrun='cmsRun ' + WorkDir +'/'+cfgFileName+ '.py >&  ' + cfgFileName + '.log < /dev/zero '
0240 
0241                     print(cmdrun)
0242 
0243                     lancialines='#!/usr/local/bin/bash \n'
0244                     lancialines+='cd '+ProjectBase+'/src \n'
0245                     lancialines+='eval `scramv1 run -sh` \n\n'
0246                     lancialines+='cd '+WorkDir+'\n'
0247                     lancialines+='cmsRun '+cfgFileName+'.py  >&  ' + cfgFileName + '.log < /dev/zero \n'
0248                     lancialines+='mv  DQM_V0001_R000000001__' + GlobalTagUse+ '__' + sample + '__Validation.root' + ' ' + 'val.' +sample+'.root \n'
0249                     
0250                     lanciaName=('lancia_%s_%s_%d') % (GlobalTag,sample,thisFile)
0251                     lanciaFile = open(lanciaName,'w')
0252                     lanciaFile.write(lancialines)
0253                     lanciaFile.close()
0254                     
0255                     print(("copying py file for sample: %s %s") % (sample,lanciaName))
0256                     iii = os.system('mv '+lanciaName+' '+WorkDir+'/.')
0257                     print(iii)
0258                     iii = os.system('mv '+cfgFileName+'.py '+WorkDir)
0259                     print(iii)
0260                     if(OneAtATime==False):
0261                         break
0262 
0263                 retcode=0
0264 
0265                 if(Submit):
0266                     retcode=os.system(cmdrun)
0267                     if os.path.isfile(sample+'.log'):
0268                         os.system('mv '+sample+'.log ' + WorkDir)
0269                 else:
0270                     continue
0271 
0272                 if (retcode!=0):
0273                     print('Job for sample '+ sample + ' failed. \n')
0274                 else:
0275                     if Sequence=="harvesting":
0276                         os.system('mv  DQM_V0001_R000000001__' + GlobalTag+ '__' + sample + '__Validation.root' + ' ' + WorkDir+'/val.' +sample+'.root')
0277                     if os.path.isfile('fullOutput.'+sample+'.root'):
0278                         os.system('mv fullOutput.'+sample+'.root ' + WorkDir)
0279                     if os.path.isfile('output.'+sample+'.root'):
0280                         os.system('mv output.'+sample+'.root ' + WorkDir)
0281                         
0282             else:
0283                 print('No dataset found skipping sample: '+ sample, '\n')
0284         else:
0285             print('Validation for sample ' + sample + ' already done. Skipping this sample. \n')
0286 
0287 
0288 
0289 
0290 ##########################################################################
0291 #########################################################################
0292 ######## This is the main program
0293             
0294 try:
0295      #Get some environment variables to use
0296      if Submit:
0297          print('Will Submit job')
0298          #NewRelease     = os.environ["CMSSW_VERSION"]
0299      ProjectBase    = os.environ["CMSSW_BASE"]
0300      #else:
0301          #NewRelease='CMSSW_2_2_3'
0302 
0303 
0304       
0305 except KeyError:
0306      print('Error: The environment variable CMSSW_VERSION is not available.', file=sys.stderr)
0307      print('       Please run eval `scramv1 runtime -csh` to set your environment variables', file=sys.stderr)
0308      sys.exit()
0309 
0310 
0311 #print 'Running validation on the following samples: ', samples
0312 
0313 if os.path.isfile( 'dbsCommandLine.py')!=True:
0314     print("Failed to find the file ($DBSCMD_HOME/dbsCommandLine.py)", file=sys.stderr)
0315 #if os.path.isfile( 'DDSearchCLI.py')!=True:
0316 #    e =os.system("wget --no-check-certificate https://cmsweb.cern.ch/dbs_discovery/aSearchCLI -O DDSearchCLI.py")
0317 #    if  e != 0:
0318 #        print >>sys.stderr, "Failed to dowload dbs aSearch file (https://cmsweb.cern.ch/dbs_discovery/aSearchCLI)"
0319 #        print >>sys.stderr, "Child was terminated by signal", e
0320 #        os.remove('DDSearchCLI.py')
0321 #        sys.exit()
0322 #    else:
0323 #        os.system('chmod +x DDSearchCLI.py')
0324 
0325 NewSelection=''
0326 
0327 for algo in Algos:
0328     print('algo ' + algo)
0329     for quality in Qualities:
0330         print('quality ' + quality)
0331         RefSelection=ReferenceSelection
0332         print('Before RefSelection: ' + RefSelection)
0333         if( quality !=''):
0334             RefSelection+='_'+quality
0335         if(algo!=''):
0336             RefSelection+='_'+algo
0337         if(quality =='') and (algo==''):
0338             RefSelection+='_ootb'
0339         print('After RefSelection: ' + RefSelection)
0340         GlobalTagUse=IdealTagUse
0341         do_validation(idealsamples, IdealTag, quality , algo)
0342         RefSelection=StartupReferenceSelection
0343         print('Before StartupRefSelection: ' + RefSelection)
0344         if( quality !=''):
0345             RefSelection+='_'+quality
0346         if(algo!=''):
0347             RefSelection+='_'+algo
0348         if(quality =='') and (algo==''):
0349             RefSelection+='_ootb'
0350         print('After StartupRefSelection: ' + RefSelection)
0351         GlobalTagUse=StartupTagUse
0352         do_validation(startupsamples, StartupTag, quality , algo)
0353