Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:38

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