CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/CaloOnlineTools/HcalOnlineDb/python/o2o.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #########################################################################
00004 #
00005 # HCAL o2o script
00006 #
00007 # by Gena Kukartsev, December 10, 2009
00008 #
00009 # Usage: ./o2o.py
00010 # base_dir must contain: o2o.py, query .sql files, dbwrite_o2o_template.py
00011 #
00012 # Pararmeters: see section below marked "parameters" for all changeable parameters
00013 #
00014 #########################################################################
00015 
00016 import os
00017 import sys
00018 import subprocess
00019 
00020 #
00021 #_____ parameters _______________________________________________________
00022 #
00023 #
00024 mode = 'online'
00025 #input_pool_connect_string = "oracle://CMSDEVR_LB/CMS_COND_HCAL"    # read list of tags/iovs from
00026 #output_pool_connect_string = "oracle://CMSDEVR_LB/CMS_COND_HCAL"   # where to write changes
00027 input_pool_connect_string = "sqlite_file:/nfshome0/kukartse/hcal/dcs/HcalDcsValues_v1.00_offline.db"
00028 output_pool_connect_string = "sqlite_file:/nfshome0/kukartse/hcal/dcs/HcalDcsValues_v1.00_offline.db"
00029 pool_auth_path = "/nfshome0/popcondev/conddb"
00030 pool_logconnect = "sqlite_file:/nfshome0/kukartse/hcal/dcs/log.db"                   # pool log DB file
00031 omds_accessor_string = "occi://CMS_HCL_APPUSER_R@anyhost/cms_omds_lb?PASSWORD=HCAL_Reader_44"
00032 base_dir = "."
00033 output_dir = "."
00034 o2o_tag_list_file = "o2o_tag_list.txt"
00035 python_popcon_template_file = "dbwrite_o2o_template.py"
00036 python_popcon_file = "dbwrite_o2o.py"
00037 #
00038 # dropbox config (optional)
00039 #use_dropbox = 'true'
00040 use_dropbox = 'false'
00041 dropbox_dir = "./dropbox"
00042 dropbox_destination_db = 'oracle://cms_orcon_prod/CMS_COND_31X_HCAL'
00043 dropbox_comment = 'generated by CaloOnlineTools/HcalOnlineDb/test/o2o.py script'
00044 #mode = 'online_dropbox'
00045 #mode = 'offline_development'
00046 #
00047 #_____ end of parameters ______________________________________________
00048 
00049 #
00050 #_____ define helper functions __________________________________________
00051 #
00052 def make_popcon_config_file(filename):
00053     py_templ_file = open(base_dir+"/"+python_popcon_template_file)
00054     py_file = open(str(filename), "w")
00055     for line in py_templ_file:
00056         line=line.replace('CONNECT_STRING', output_pool_connect_string)
00057         line=line.replace('POOL_AUTH_PATH', pool_auth_path)
00058         line=line.replace('CONDITION_TYPE', condition_type)
00059         line=line.replace('OMDS_CONDITION_TAG', tag)
00060         line=line.replace('OMDS_IOV', str(iov))
00061         line=line.replace('OMDS_ACCESSOR_STRING', omds_accessor_string)
00062         line=line.replace('OMDS_QUERY', query)
00063         line=line.replace('POOL_LOGCONNECT', pool_logconnect)
00064         line=line.replace('POOL_RECORD', pool_record)
00065         line=line.replace('POOL_OUTPUT_TAG', tag)
00066         line=line.replace('POOL_IOV', str(pool_iov))
00067         py_file.write(line)
00068     py_file.close()
00069 
00070 #
00071 # HCAL conditions have a rigid naming scheme:
00072 # RECORD_VERSION_USE, e.g. HcalChannelQuality_v1.00_mc
00073 # or
00074 # RECORD_UNIT_VERSION_USE, e.g. HcalPedestals_ADC_v2.31_offline
00075 #
00076 def guess_condition_from_tag(tagname):
00077     guessed_type = tagname[0:tagname.find("_")]
00078     # FIXME: gak is a temporary debugging case
00079     if guessed_type == "gak":
00080         guessed_type = "HcalChannelQuality"
00081         guessed_query_file_name = base_dir+"/HcalChannelQuality.sql"
00082         guessed_pool_record = "HcalChannelQualityRcd"
00083 
00084     elif guessed_type == "HcalDcsValues":
00085         guessed_type = "DcsValues"
00086         guessed_query_file_name = base_dir+"/HcalDcsValues.sql"
00087         guessed_pool_record = "HcalDcsRcd"
00088 
00089     elif guessed_type == "HcalChannelQuality":
00090         guessed_type = "ChannelQuality"
00091         guessed_query_file_name = base_dir+"/HcalChannelQuality.sql"
00092         guessed_pool_record = "HcalChannelQualityRcd"
00093 
00094     elif guessed_type == "HcalLutMetadata":
00095         guessed_type = "LutMetadata"
00096         guessed_query_file_name = base_dir+"/HcalLutMetadata.sql"
00097         guessed_pool_record = "HcalLutMetadataRcd"
00098 
00099     elif guessed_type == "HcalRespCorrs":
00100         guessed_type = "RespCorrs"
00101         guessed_query_file_name = base_dir+"/HcalRespCorrs.sql"
00102         guessed_pool_record = "HcalRespCorrsRcd"
00103 
00104     elif guessed_type == "HcalValidationCorrs":
00105         guessed_type = "ValidationCorrs"
00106         guessed_query_file_name = base_dir+"/HcalValidationCorrs.sql"
00107         guessed_pool_record = "HcalValidationCorrsRcd"
00108 
00109     elif guessed_type == "HcalPedestals":
00110         guessed_type = "Pedestals"
00111         guessed_query_file_name = base_dir+"/HcalPedestals.sql"
00112         guessed_pool_record = "HcalPedestalsRcd"
00113 
00114     elif guessed_type == "HcalPedestalWidths":
00115         guessed_type = "PedestalWidths"
00116         guessed_query_file_name = base_dir+"/HcalPedestalWidths.sql"
00117         guessed_pool_record = "HcalPedestalWidthsRcd"
00118 
00119     elif guessed_type == "HcalGains":
00120         guessed_type = "Gains"
00121         guessed_query_file_name = base_dir+"/HcalGains.sql"
00122         guessed_pool_record = "HcalGainsRcd"
00123 
00124     elif guessed_type == "HcalGainWidths":
00125         guessed_type = "GainWidths"
00126         guessed_query_file_name = base_dir+"/HcalGainWidths.sql"
00127         guessed_pool_record = "HcalGainWidthsRcd"
00128 
00129     elif guessed_type == "HcalQIEData":
00130         guessed_type = "QIEData"
00131         guessed_query_file_name = base_dir+"/HcalQieData.sql"
00132         guessed_pool_record = "HcalQIEDataRcd"
00133 
00134     elif guessed_type == "HcalElectronicsMap":
00135         guessed_type = "ElectronicsMap"
00136         guessed_query_file_name = base_dir+"/HcalEmap.sql"
00137         guessed_pool_record = "HcalElectronicsMapRcd"
00138 
00139     elif guessed_type == "HcalZSThresholds":
00140         guessed_type = "ZSThresholds"
00141         guessed_query_file_name = base_dir+"/HcalZSThresholds.sql"
00142         guessed_pool_record = "HcalZSThresholdsRcd"
00143 
00144     elif guessed_type == "HcalL1TriggerObjects":
00145         guessed_type = "L1TriggerObjects"
00146         guessed_query_file_name = base_dir+"/HcalL1TriggerObjects.sql"
00147         guessed_pool_record = "HcalL1TriggerObjectsRcd"
00148 
00149     else:
00150         print "Cannot guess condition type"
00151 
00152     return {'condition_type':guessed_type,
00153             'query_file_name':guessed_query_file_name,
00154             'pool_record':guessed_pool_record}
00155 
00156 #_____ get list of IOVs that need updating, from comparison of OMDS and ORCON
00157 def get_tags(tag_list_file_name):
00158     try:
00159         tag_list_file = open(tag_list_file_name)
00160     except:
00161         print "Cannot open the file with the list of tags, exiting..."
00162         sys.exit()
00163     tag_list = []
00164     for line in tag_list_file:
00165         tag_list . append(line)
00166     tag_list_file.close()
00167     #tags = subprocess.Popen(["cat", tag_list], stdout=subprocess.PIPE).communicate()[0]
00168     return tag_list
00169 
00170 #_____ get list of IOVs that need updating, from comparison of OMDS and ORCON
00171 def get_iovs(tag, input_pool_connect_string, mode):
00172     result = "fail" # default
00173     newtag = False  # default
00174     # nominal mode of operations
00175     if mode == "online" or mode == "online_dropbox":
00176         try:
00177             iovs = subprocess.Popen(["./xmlToolsRun", "--list-iovs-for-o2o", "--tag-name", tag, "--pool-connect-string", input_pool_connect_string, "--pool-auth-path", pool_auth_path], stdout=subprocess.PIPE).communicate()[0]
00178             iov_list = iovs.splitlines()
00179             result = "success"
00180         except:
00181             print "ERROR: Cannot get the IOV update list for tag", tag+". This tag will not be updated. Now continue to the next tag..."
00182             iov_list=[]
00183             result = "fail"
00184 
00185     # script development mode, DB interfaces substituted with dummies
00186     if mode == "offline_development":
00187         #iovs = subprocess.Popen(["cat", tag+"_iov_to_update.devel"], stdout=subprocess.PIPE).communicate()[0]
00188         try:
00189             iov_list_file = open(base_dir+"/o2o/"+tag+"_iov_to_update.devel")
00190         except:
00191             print "ERROR: Cannot open file with the IOV list for tag", tag+". This tag will not be updated. Now continue to the next tag..."
00192             result = "fail"
00193         result = "success"
00194         iov_list = []
00195         for line in iov_list_file:
00196             iov_list . append(line.strip())
00197         iov_list_file.close()
00198     # now parse iov_list.
00199     # May contain various important output. Prefix defines the type of instruction.
00200     # IOVs start with O2O_IOV_LIST:
00201     # CONNECTION ERROR: indicates problems with connection to Pool and invalidates
00202     #                   the IOV list
00203     iovs=[]
00204     for line in iov_list:
00205         line_stripped = line.strip()
00206         if line.strip()[0:13] == "O2O_IOV_LIST:":
00207            iovs.append(line.lstrip("O2O_IOV_LIST:"))
00208         if line_stripped[0:line_stripped.find(":")] == "CONNECTION ERROR":
00209             result = "fail_connect"
00210         if line_stripped[0:line_stripped.find(":")] == "NEW_POOL_TAG_TRUE":
00211             newtag = True
00212     return {'iovs':iovs,
00213             'result':result,
00214             'newtag':newtag}
00215 
00216 #_____ run PopCon
00217 def run_popcon():
00218     status = "fail"
00219     try:
00220         print "iov=", iov
00221         if mode == "online" or mode == "online_dropbox":
00222             subprocess.call(["cmsRun", str(python_popcon_file)])
00223         else:
00224             print "In online mode would run Popcon now:", "cmsRun", str(python_popcon_file)
00225         status = "success"
00226     except OSError:
00227         print "Cannot execute cmsRun. Further processing of this tag is stopped, going to the next tag..."
00228         #break
00229     except:
00230         print "Cannot execute cmsRun. Further processing of this tag is stopped, going to the next tag..."
00231         #break
00232     
00233     #
00234     #_____ copy to the Dropbox area (optional)
00235     #
00236     if use_dropbox == "true" or use_dropbox == "True" or use_dropbox == "TRUE":
00237         try:
00238             retcall_meta = subprocess.call(['mv', str(output_dir)+'/'+str(dropbox_file_name_prefix)+'.txt', str(dropbox_dir)+'/'])
00239             retcall_sql    = subprocess.call(['mv', str(output_dir)+'/'+str(dropbox_file_name_prefix)+'.sql', str(dropbox_dir)+'/'])
00240         except:
00241             print "ERROR: Cannot copy files to the dropbox area"
00242 
00243     return {'status':status}
00244 #
00245 #_____ end of helper functions __________________________________________
00246 
00247 
00248 tags = get_tags(base_dir+"/"+o2o_tag_list_file)
00249 for tag_name in tags:
00250     # FIXME: do we need this?
00251     tag = tag_name.strip()
00252 
00253     #
00254     #_____ guess condition from tag name
00255     #
00256     guessed_condition = guess_condition_from_tag(tag)
00257     condition_type    = guessed_condition['condition_type']
00258     query_file_name   = guessed_condition['query_file_name']
00259     pool_record       = guessed_condition['pool_record']
00260 
00261     print ""
00262     print "Processing tag:", tag
00263     print "Condition type:",condition_type 
00264 
00265     #
00266     #_____ read SQL query from a file
00267     #
00268     query_file = open(query_file_name, "r")
00269     query = query_file.read()
00270     query_file.close()
00271     
00272     #
00273     #_____ Popcon Dropbox requirements (optional)
00274     #
00275     if use_dropbox == "true" or use_dropbox == "True" or use_dropbox == "TRUE":
00276         #output_pool_connect_string = "sqlite_file:testDropbox.db"
00277         dropbox_file_name_prefix = str(output_dir)+"/"+str(tag)
00278         output_pool_connect_string = 'sqlite_file:'+str(dropbox_file_name_prefix)+'.db'
00279         dropbox_txt_file_name = str(dropbox_file_name_prefix)+".txt"
00280         dropbox_txt_file = open(dropbox_txt_file_name, "w")
00281         dropbox_txt_file.write('destDB '+dropbox_destination_db+'\n')
00282         dropbox_txt_file.write('inputtag\n')
00283         dropbox_txt_file.write('tag '+str(tag)+'\n')
00284         dropbox_txt_file.write('since\n')
00285         dropbox_txt_file.write('till\n')
00286         dropbox_txt_file.write('usertext '+str(dropbox_comment)+'\n')
00287         dropbox_txt_file.close()
00288     
00289     #
00290     #_____ loop over IOV to copy
00291     #
00292     #FIXME: move the binary inside the popcon job, and try multiple IOV
00293     gotten_iovs = get_iovs(tag, input_pool_connect_string, mode)
00294 
00295     # stop processing current tag if IOVs were not obtained
00296     if (gotten_iovs['result'] != "success"):
00297         if (gotten_iovs['result'] == "fail_connect"):
00298             print "Problems connecting to", input_pool_connect_string
00299             print "Unable to process tag", tag_name, "continuing to the next tag..."
00300         continue
00301     
00302     o2o_iovs = gotten_iovs['iovs']
00303     isnewtag = gotten_iovs['newtag'] # is this a new POOL tag?
00304     i = 0
00305     for line in o2o_iovs:
00306         i = i+1
00307         iov = int(line)
00308         pool_iov = iov
00309         
00310         if i==1: # first IOV to update may be a special case
00311             if isnewtag:
00312                 print "Creating a new offline tag: ", tag_name
00313                 if condition_type == "DcsValues" and iov != 1:
00314                     print "DCS values: The first IOV in the online tag is ", iov
00315                     print "DCS values: Adding default DCS set values to the offline"
00316                     print "DCS values: tag for IOVs 1 - ", iov-1
00317                     print "DCS values: because first IOV in the offline tag must always be 1."
00318                     query_save = query
00319                     query_file_name = query_file_name+".default"
00320                     query_file = open(query_file_name, "r")
00321                     query = query_file.read()
00322                     query_file.close()
00323                     pool_iov = 1
00324                     make_popcon_config_file(python_popcon_file)
00325                     run_result = run_popcon()
00326                     query = query_save
00327                     pool_iov = iov
00328                     if run_result['status'] != "success":
00329                         break
00330                 else:
00331                     pool_iov = 1 # force the first IOV in offline tag to be 1 by default
00332                 
00333         make_popcon_config_file(python_popcon_file)
00334         run_result = run_popcon()
00335         if run_result['status'] != "success":
00336             break
00337