CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Alignment/MuonAlignmentAlgorithms/scripts/findQualityFiles.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 ######################################################
00004 ### See documentation at
00005 ### https://twiki.cern.ch/twiki/bin/view/CMS/FindQualityFilesPy
00006 ### also run it with -h option
00007 ######################################################
00008 
00009 import os,sys, DLFCN
00010 import optparse
00011 
00012 # for RunInfo API
00013 from pluginCondDBPyInterface import *
00014 from CondCore.Utilities import iovInspector as inspect
00015 from CondCore.Utilities.timeUnitHelper import *
00016 
00017 ######################################################
00018 # functions definitions
00019 
00020 
00021 #########################
00022 # get good B field runs from RunInfo DB
00023 def getGoodBRuns(options):
00024 
00025     runs_b_on = []
00026 
00027     sys.setdlopenflags(DLFCN.RTLD_GLOBAL+DLFCN.RTLD_LAZY)
00028 
00029     a = FWIncantation()
00030     #os.putenv("CORAL_AUTH_PATH","/afs/cern.ch/cms/DB/conddb")
00031     rdbms = RDBMS("/afs/cern.ch/cms/DB/conddb")
00032 
00033     db = rdbms.getDB(options.dbName)
00034     tags = db.allTags()
00035 
00036     if options.printTags:
00037         print  "\nOverview of all tags in "+options.dbName+" :\n"
00038         print tags
00039         print "\n"
00040         sys.exit()
00041 
00042     # for inspecting last run after run has started  
00043     #tag = 'runinfo_31X_hlt'
00044     tag = options.dbTag
00045 
00046     # for inspecting last run after run has stopped  
00047     #tag = 'runinfo_test'
00048 
00049     try :
00050         #log = db.lastLogEntry(tag)
00051 
00052         #for printing all log info present into log db 
00053         #print log.getState()
00054 
00055         iov = inspect.Iov(db,tag)
00056         #print "########overview of tag "+tag+"########"
00057         #print iov.list()
00058     
00059         if v>1 :
00060             print "######## summries ########"
00061             for x in  iov.summaries():
00062                 print x[0], x[1], x[2] ,x[3]
00063     
00064         what={}
00065     
00066         if v>1 :
00067             print "###(start_current,stop_current,avg_current,max_current,min_current,run_interval_micros) vs runnumber###"
00068             print iov.trend(what)
00069     
00070         if v>0:
00071             print "######## trends ########"
00072         for x in iov.trendinrange(what,options.startRun-1,options.endRun+1):
00073             if v>0 or x[0]==67647L or x[0]==66893L or x[0]==67264L:
00074                 print x[0],x[1] ,x[2], x[2][4], x[2][3]
00075                 #print x[0],x[1] ,x[2], x[2][4], timeStamptoUTC(x[2][6]), timeStamptoUTC(x[2][7])
00076             if x[2][4] >= minI and x[2][3] <= maxI:
00077                 runs_b_on.append(int(x[0]))
00078 
00079     except Exception, er :
00080         print er
00081 
00082     print "### runs with good B field ###"
00083     print runs_b_on
00084 
00085     return runs_b_on
00086 
00087 
00088 #########################
00089 # obtaining list of good quality runs
00090 
00091 def getGoodQRuns(options):
00092 
00093     runs_good_dq = []
00094 
00095     dbs_quiery = "find run where dataset="+options.dqDataset+" and dq="+options.dqCriteria
00096 
00097     os.system('python $DBSCMD_HOME/dbsCommandLine.py -c  search --noheader --query="'+dbs_quiery+'" | sort > /tmp/runs_full_of_pink_bunnies')
00098 
00099     #print 'python $DBSCMD_HOME/dbsCommandLine.py -c  search --noheader --query="'+dbs_quiery+'" | sort > /tmp/runs_full_of_pink_bunnies'
00100 
00101     ff = open('/tmp/runs_full_of_pink_bunnies', "r")
00102     line = ff.readline()
00103     while line and line!='':
00104         runs_good_dq.append(int(line))
00105         line = ff.readline()
00106     ff.close()
00107 
00108     os.system('rm /tmp/runs_full_of_pink_bunnies')
00109 
00110     print "### runs with good quality ###"
00111     print runs_good_dq
00112 
00113     return runs_good_dq
00114 
00115 
00116 
00117 ######################################################
00118 # To parse commandline args
00119 
00120 usage='%prog [options]\n\n'+\
00121     'Creates a Python configuration file with filenames for runs in specified run range, with certain min B field and data quality requirements.'
00122 
00123 parser=optparse.OptionParser(usage)
00124 
00125 parser.add_option("-d", "--alcaDataset",
00126                    help="[REQUIRED] Name of the input AlCa dataset to get filenames from.",
00127                    type="string",
00128                    #default="/Cosmics/Commissioning08-2213_Tosca090322_2pi_scaled_ReReco_FromTrackerPointing-v1/RAW-RECO",
00129                    #default="/Cosmics/Commissioning08_CRAFT_ALL_V11_StreamALCARECOMuAlGlobalCosmics_227_Tosca090216_ReReco_FromTrackerPointing_v5/ALCARECO",
00130                    default='',
00131                    dest="alcaDataset")
00132 
00133 parser.add_option("-m", "--isMC",
00134                    help="Whether sample is MC (true) or real data (false).",
00135                    type="string",
00136                    default="false",
00137                    dest="isMC")
00138 
00139 parser.add_option("-s", "--startRun",
00140                    help="First run number in range.",
00141                    type="int",
00142                    default=0L,
00143                    dest="startRun")
00144 
00145 parser.add_option("-e", "--endRun",
00146                    help="Last run number in range.",
00147                    type="int",
00148                    default=999999999L,
00149                    dest="endRun")
00150 
00151 parser.add_option("-b", "--minB",
00152                    help="Lower limit on minimal B field for a run.",
00153                    type="float",
00154                    #default=3.77,
00155                    default=0.,
00156                    dest="minB")
00157 
00158 parser.add_option("--maxB",
00159                    help="Upper limit on B field for a run.",
00160                    type="float",
00161                    default=999.,
00162                    dest="maxB")
00163 
00164 parser.add_option("-t", "--dbTag",
00165                    help="Runinfo DB tag to use.",
00166                    type="string",
00167                    default="runinfo_31X_hlt",
00168                    dest="dbTag")
00169 
00170 parser.add_option("--printTags",
00171                    help="If present, the only thing script will do is printing list of tags in the DB",
00172                    action="store_true",
00173                    default=False,
00174                    dest="printTags")
00175 
00176 parser.add_option("--dbName",
00177                    help="RunInfo DB name to use. The default one is "+\
00178                    "'oracle://cms_orcoff_prod/CMS_COND_31X_RUN_INFO'",
00179                    type="string",
00180                    default="oracle://cms_orcoff_prod/CMS_COND_31X_RUN_INFO",
00181                    dest="dbName")
00182 
00183 parser.add_option("--dqDataset",
00184                    help="Dataset name to query for good data quality runs. "+\
00185                    "If this option is not used, dqDataset=alcaDataset is automatically set. "+\
00186                    "If alcaDataset does not have DQ information use /Cosmics/Commissioning08-v1/RAW for CRAFT08 "+\
00187                    "and use /Cosmics/CRAFT09-v1/RAW for CRAFT08",
00188                    type="string",
00189                    #default="/Cosmics/Commissioning08-v1/RAW",
00190                    #default="/Cosmics/CRAFT09-v1/RAW",
00191                    default="",
00192                    dest="dqDataset")
00193 
00194 parser.add_option("-c", "--dqCriteria",
00195                    help="Set of DQ criteria to use with -dq flag of dbs.\n"+\
00196                    "An example of a really strict condition:\n"
00197                    "'DT_Shift_Offline=GOOD&CSC_Shift_Offline=GOOD&SiStrip_Shift_Offline=GOOD&Pixel_Shift_Offline=GOOD'",
00198                    type="string",
00199                    #default="DT_Shift_Offline=GOOD&SiStrip_Shift_Offline=GOOD&Pixel_Shift_Offline=GOOD",
00200                    #default="DT_Shift_Offline=GOOD&Pixel_Shift_Offline=GOOD",
00201                    #default="DT_Shift_Offline=GOOD",
00202                    default="",
00203                    dest="dqCriteria")
00204 
00205 parser.add_option("-o", "--outputFile",
00206                    help="Name for output file (please include the .py suffix)",
00207                    type="string",
00208                    default="filelist.py",
00209                    dest="outputFile")
00210 
00211 parser.add_option("-v", "--verbose",
00212                    help="Degree of debug info verbosity",
00213                    type="int",
00214                    default=0,
00215                    dest="verbose")
00216 
00217 options,args=parser.parse_args() 
00218 
00219 #if '' in (options.infilename,
00220 #          options.outfilename,
00221 #          options.outputCommands):
00222 #    raise ('Incomplete list of arguments!')
00223 
00224 
00225 if options.alcaDataset=='' and not options.printTags:
00226     print "--alcaDataset /your/dataset/name is required!"
00227     sys.exit()
00228     
00229 if options.dqDataset=='':
00230     options.dqDataset = options.alcaDataset
00231 
00232 if not (options.isMC=='true' or options.isMC=='false'):
00233     print "--isMC option can have only 'true' or 'false' arguments"
00234     sys.exit()
00235 
00236 v = options.verbose
00237 
00238 minI = options.minB*18160/3.8
00239 maxI = options.maxB*18160/3.8
00240 
00241 
00242 copyargs = sys.argv[:]
00243 for i in range(len(copyargs)):
00244     if copyargs[i] == "":
00245         copyargs[i] = "\"\""
00246 
00247 infotofile = ["### %s\n" % " ".join(copyargs)]
00248 
00249 allOptions = '### ' + copyargs[0] + ' --alcaDataset ' + options.alcaDataset + ' --isMC ' + options.isMC + \
00250              ' --startRun ' + str(options.startRun) + ' --endRun '+ str(options.endRun) + \
00251              ' --minB ' + str(options.minB) + ' --maxB ' + str(options.maxB) + \
00252              ' --dbTag ' + options.dbTag + ' --dqDataset ' + options.dqDataset + ' --dqCriteria "' + options.dqCriteria + '"'\
00253              ' --outputFile ' + options.outputFile
00254 
00255 print "### all options, including default:"
00256 print allOptions
00257 
00258 
00259 
00260 ######################################################
00261 # get good B field runs from RunInfo DB
00262 
00263 runs_b_on = []
00264 
00265 if options.isMC=='false':
00266     runs_b_on = getGoodBRuns(options)
00267 
00268     infotofile.append("### runs with good B field ###\n")
00269     infotofile.append("### %s\n" % str(runs_b_on))
00270 
00271 ######################################################
00272 # Add requiremment of good quality runs
00273 
00274 runs_good_dq = []
00275 runs_good = []
00276 
00277 if options.isMC=='false':
00278     runs_good_dq = getGoodQRuns(options)
00279         
00280     infotofile.append("### runs with good quality ###\n")
00281     infotofile.append("### %s\n" % str(runs_good_dq))
00282 
00283     # find intersection of runs_b_on and runs_good_dq
00284     runs_good = [val for val in runs_b_on if val in runs_good_dq]
00285 
00286     print "### runs with good B field and quality ###"
00287     print runs_good
00288 
00289     infotofile.append("### runs with good B field and quality ###\n")
00290     infotofile.append("### %s\n" % str(runs_good))
00291 
00292 ######################################################
00293 # Find files for good runs
00294 
00295 dbs_quiery = "find run, file.numevents, file where dataset="+options.alcaDataset+" and run>="+str(options.startRun)+" and run <="+str(options.endRun)
00296 
00297 os.system('python $DBSCMD_HOME/dbsCommandLine.py -c  search --noheader --query="'+dbs_quiery+'" | sort > /tmp/runs_and_files_full_of_pink_bunnies')
00298 
00299 list_of_files = []
00300 list_of_runs = []
00301 total_numevents = 0
00302 
00303 ff = open('/tmp/runs_and_files_full_of_pink_bunnies','r')
00304 for line in ff:
00305     (run, numevents, fname) = line.split('   ')
00306     if options.isMC=='false' and (int(run) not in runs_good):
00307         continue
00308     fname = fname.rstrip('\n')
00309     list_of_files.append(fname)
00310     list_of_runs.append(run)
00311     total_numevents += int(numevents)
00312 ff.close()
00313 os.system('rm /tmp/runs_and_files_full_of_pink_bunnies')
00314 print "### total number of events in those runs = "+str(total_numevents)
00315 
00316 infotofile.append("### total number of events in those runs = "+str(total_numevents))
00317 
00318 ######################################################
00319 # Write out results
00320 
00321 # ff = open(options.outputFile+'.txt','w')
00322 size = len(list_of_files)
00323 # for i in range(0,size):
00324 #     ff.write(list_of_runs[i] + ", " + list_of_files[i]+"\n")
00325 # ff.close()
00326 
00327 ff = open(options.outputFile,'w')
00328 ff.write("".join(infotofile))
00329 ff.write("\nfileNames = [\n")
00330 comma = ","
00331 for i in range(0,size):
00332     if i==size-1:
00333         comma=""
00334     ff.write("    '"+ list_of_files[i] +"'"+comma+" # "+ list_of_runs[i] +"\n")
00335 ff.write(']\n')
00336 ff.close()
00337 
00338 
00339 
00340