CMS 3D CMS Logo

o2o.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #########################################################################
4 #
5 # HCAL o2o script
6 #
7 # by Gena Kukartsev, December 10, 2009
8 #
9 # Usage: ./o2o.py
10 # base_dir must contain: o2o.py, query .sql files, dbwrite_o2o_template.py
11 #
12 # Pararmeters: see section below marked "parameters" for all changeable parameters
13 #
14 #########################################################################
15 
16 import os
17 import sys
18 import subprocess
19 
20 #
21 #_____ parameters _______________________________________________________
22 #
23 #
24 mode = 'online'
25 #input_pool_connect_string = "oracle://CMSDEVR_LB/CMS_COND_HCAL" # read list of tags/iovs from
26 #output_pool_connect_string = "oracle://CMSDEVR_LB/CMS_COND_HCAL" # where to write changes
27 input_pool_connect_string = "sqlite_file:/nfshome0/kukartse/hcal/dcs/HcalDcsValues_v1.00_offline.db"
28 output_pool_connect_string = "sqlite_file:/nfshome0/kukartse/hcal/dcs/HcalDcsValues_v1.00_offline.db"
29 pool_auth_path = "/nfshome0/popcondev/conddb"
30 pool_logconnect = "sqlite_file:/nfshome0/kukartse/hcal/dcs/log.db" # pool log DB file
31 omds_accessor_string = "occi://CMS_HCL_APPUSER_R@anyhost/cms_omds_lb?PASSWORD=HCAL_Reader_44"
32 base_dir = "."
33 output_dir = "."
34 o2o_tag_list_file = "o2o_tag_list.txt"
35 python_popcon_template_file = "dbwrite_o2o_template.py"
36 python_popcon_file = "dbwrite_o2o.py"
37 #
38 # dropbox config (optional)
39 #use_dropbox = 'true'
40 use_dropbox = 'false'
41 dropbox_dir = "./dropbox"
42 dropbox_destination_db = 'oracle://cms_orcon_prod/CMS_COND_31X_HCAL'
43 dropbox_comment = 'generated by CaloOnlineTools/HcalOnlineDb/test/o2o.py script'
44 #mode = 'online_dropbox'
45 #mode = 'offline_development'
46 #
47 #_____ end of parameters ______________________________________________
48 
49 #
50 #_____ define helper functions __________________________________________
51 #
53  py_templ_file = open(base_dir+"/"+python_popcon_template_file)
54  py_file = open(str(filename), "w")
55  for line in py_templ_file:
56  line=line.replace('CONNECT_STRING', output_pool_connect_string)
57  line=line.replace('POOL_AUTH_PATH', pool_auth_path)
58  line=line.replace('CONDITION_TYPE', condition_type)
59  line=line.replace('OMDS_CONDITION_TAG', tag)
60  line=line.replace('OMDS_IOV', str(iov))
61  line=line.replace('OMDS_ACCESSOR_STRING', omds_accessor_string)
62  line=line.replace('OMDS_QUERY', query)
63  line=line.replace('POOL_LOGCONNECT', pool_logconnect)
64  line=line.replace('POOL_RECORD', pool_record)
65  line=line.replace('POOL_OUTPUT_TAG', tag)
66  line=line.replace('POOL_IOV', str(pool_iov))
67  py_file.write(line)
68  py_file.close()
69 
70 #
71 # HCAL conditions have a rigid naming scheme:
72 # RECORD_VERSION_USE, e.g. HcalChannelQuality_v1.00_mc
73 # or
74 # RECORD_UNIT_VERSION_USE, e.g. HcalPedestals_ADC_v2.31_offline
75 #
77  guessed_type = tagname[0:tagname.find("_")]
78  # FIXME: gak is a temporary debugging case
79  if guessed_type == "gak":
80  guessed_type = "HcalChannelQuality"
81  guessed_query_file_name = base_dir+"/HcalChannelQuality.sql"
82  guessed_pool_record = "HcalChannelQualityRcd"
83 
84  elif guessed_type == "HcalDcsValues":
85  guessed_type = "DcsValues"
86  guessed_query_file_name = base_dir+"/HcalDcsValues.sql"
87  guessed_pool_record = "HcalDcsRcd"
88 
89  elif guessed_type == "HcalChannelQuality":
90  guessed_type = "ChannelQuality"
91  guessed_query_file_name = base_dir+"/HcalChannelQuality.sql"
92  guessed_pool_record = "HcalChannelQualityRcd"
93 
94  elif guessed_type == "HcalLutMetadata":
95  guessed_type = "LutMetadata"
96  guessed_query_file_name = base_dir+"/HcalLutMetadata.sql"
97  guessed_pool_record = "HcalLutMetadataRcd"
98 
99  elif guessed_type == "HcalRespCorrs":
100  guessed_type = "RespCorrs"
101  guessed_query_file_name = base_dir+"/HcalRespCorrs.sql"
102  guessed_pool_record = "HcalRespCorrsRcd"
103 
104  elif guessed_type == "HcalValidationCorrs":
105  guessed_type = "ValidationCorrs"
106  guessed_query_file_name = base_dir+"/HcalValidationCorrs.sql"
107  guessed_pool_record = "HcalValidationCorrsRcd"
108 
109  elif guessed_type == "HcalPedestals":
110  guessed_type = "Pedestals"
111  guessed_query_file_name = base_dir+"/HcalPedestals.sql"
112  guessed_pool_record = "HcalPedestalsRcd"
113 
114  elif guessed_type == "HcalPedestalWidths":
115  guessed_type = "PedestalWidths"
116  guessed_query_file_name = base_dir+"/HcalPedestalWidths.sql"
117  guessed_pool_record = "HcalPedestalWidthsRcd"
118 
119  elif guessed_type == "HcalGains":
120  guessed_type = "Gains"
121  guessed_query_file_name = base_dir+"/HcalGains.sql"
122  guessed_pool_record = "HcalGainsRcd"
123 
124  elif guessed_type == "HcalGainWidths":
125  guessed_type = "GainWidths"
126  guessed_query_file_name = base_dir+"/HcalGainWidths.sql"
127  guessed_pool_record = "HcalGainWidthsRcd"
128 
129  elif guessed_type == "HcalQIEData":
130  guessed_type = "QIEData"
131  guessed_query_file_name = base_dir+"/HcalQieData.sql"
132  guessed_pool_record = "HcalQIEDataRcd"
133 
134  elif guessed_type == "HcalElectronicsMap":
135  guessed_type = "ElectronicsMap"
136  guessed_query_file_name = base_dir+"/HcalEmap.sql"
137  guessed_pool_record = "HcalElectronicsMapRcd"
138 
139  elif guessed_type == "HcalZSThresholds":
140  guessed_type = "ZSThresholds"
141  guessed_query_file_name = base_dir+"/HcalZSThresholds.sql"
142  guessed_pool_record = "HcalZSThresholdsRcd"
143 
144  elif guessed_type == "HcalL1TriggerObjects":
145  guessed_type = "L1TriggerObjects"
146  guessed_query_file_name = base_dir+"/HcalL1TriggerObjects.sql"
147  guessed_pool_record = "HcalL1TriggerObjectsRcd"
148 
149  else:
150  print "Cannot guess condition type"
151 
152  return {'condition_type':guessed_type,
153  'query_file_name':guessed_query_file_name,
154  'pool_record':guessed_pool_record}
155 
156 #_____ get list of IOVs that need updating, from comparison of OMDS and ORCON
157 def get_tags(tag_list_file_name):
158  try:
159  tag_list_file = open(tag_list_file_name)
160  except:
161  print "Cannot open the file with the list of tags, exiting..."
162  sys.exit()
163  tag_list = []
164  for line in tag_list_file:
165  tag_list . append(line)
166  tag_list_file.close()
167  #tags = subprocess.Popen(["cat", tag_list], stdout=subprocess.PIPE).communicate()[0]
168  return tag_list
169 
170 #_____ get list of IOVs that need updating, from comparison of OMDS and ORCON
171 def get_iovs(tag, input_pool_connect_string, mode):
172  result = "fail" # default
173  newtag = False # default
174  # nominal mode of operations
175  if mode == "online" or mode == "online_dropbox":
176  try:
177  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]
178  iov_list = iovs.splitlines()
179  result = "success"
180  except:
181  print "ERROR: Cannot get the IOV update list for tag", tag+". This tag will not be updated. Now continue to the next tag..."
182  iov_list=[]
183  result = "fail"
184 
185  # script development mode, DB interfaces substituted with dummies
186  if mode == "offline_development":
187  #iovs = subprocess.Popen(["cat", tag+"_iov_to_update.devel"], stdout=subprocess.PIPE).communicate()[0]
188  try:
189  iov_list_file = open(base_dir+"/o2o/"+tag+"_iov_to_update.devel")
190  except:
191  print "ERROR: Cannot open file with the IOV list for tag", tag+". This tag will not be updated. Now continue to the next tag..."
192  result = "fail"
193  result = "success"
194  iov_list = []
195  for line in iov_list_file:
196  iov_list . append(line.strip())
197  iov_list_file.close()
198  # now parse iov_list.
199  # May contain various important output. Prefix defines the type of instruction.
200  # IOVs start with O2O_IOV_LIST:
201  # CONNECTION ERROR: indicates problems with connection to Pool and invalidates
202  # the IOV list
203  iovs=[]
204  for line in iov_list:
205  line_stripped = line.strip()
206  if line.strip()[0:13] == "O2O_IOV_LIST:":
207  iovs.append(line.lstrip("O2O_IOV_LIST:"))
208  if line_stripped[0:line_stripped.find(":")] == "CONNECTION ERROR":
209  result = "fail_connect"
210  if line_stripped[0:line_stripped.find(":")] == "NEW_POOL_TAG_TRUE":
211  newtag = True
212  return {'iovs':iovs,
213  'result':result,
214  'newtag':newtag}
215 
216 #_____ run PopCon
218  status = "fail"
219  try:
220  print "iov=", iov
221  if mode == "online" or mode == "online_dropbox":
222  subprocess.call(["cmsRun", str(python_popcon_file)])
223  else:
224  print "In online mode would run Popcon now:", "cmsRun", str(python_popcon_file)
225  status = "success"
226  except OSError:
227  print "Cannot execute cmsRun. Further processing of this tag is stopped, going to the next tag..."
228  #break
229  except:
230  print "Cannot execute cmsRun. Further processing of this tag is stopped, going to the next tag..."
231  #break
232 
233  #
234  #_____ copy to the Dropbox area (optional)
235  #
236  if use_dropbox == "true" or use_dropbox == "True" or use_dropbox == "TRUE":
237  try:
238  retcall_meta = subprocess.call(['mv', str(output_dir)+'/'+str(dropbox_file_name_prefix)+'.txt', str(dropbox_dir)+'/'])
239  retcall_sql = subprocess.call(['mv', str(output_dir)+'/'+str(dropbox_file_name_prefix)+'.sql', str(dropbox_dir)+'/'])
240  except:
241  print "ERROR: Cannot copy files to the dropbox area"
242 
243  return {'status':status}
244 #
245 #_____ end of helper functions __________________________________________
246 
247 
248 tags = get_tags(base_dir+"/"+o2o_tag_list_file)
249 for tag_name in tags:
250  # FIXME: do we need this?
251  tag = tag_name.strip()
252 
253  #
254  #_____ guess condition from tag name
255  #
256  guessed_condition = guess_condition_from_tag(tag)
257  condition_type = guessed_condition['condition_type']
258  query_file_name = guessed_condition['query_file_name']
259  pool_record = guessed_condition['pool_record']
260 
261  print ""
262  print "Processing tag:", tag
263  print "Condition type:",condition_type
264 
265  #
266  #_____ read SQL query from a file
267  #
268  query_file = open(query_file_name, "r")
269  query = query_file.read()
270  query_file.close()
271 
272  #
273  #_____ Popcon Dropbox requirements (optional)
274  #
275  if use_dropbox == "true" or use_dropbox == "True" or use_dropbox == "TRUE":
276  #output_pool_connect_string = "sqlite_file:testDropbox.db"
277  dropbox_file_name_prefix = str(output_dir)+"/"+str(tag)
278  output_pool_connect_string = 'sqlite_file:'+str(dropbox_file_name_prefix)+'.db'
279  dropbox_txt_file_name = str(dropbox_file_name_prefix)+".txt"
280  dropbox_txt_file = open(dropbox_txt_file_name, "w")
281  dropbox_txt_file.write('destDB '+dropbox_destination_db+'\n')
282  dropbox_txt_file.write('inputtag\n')
283  dropbox_txt_file.write('tag '+str(tag)+'\n')
284  dropbox_txt_file.write('since\n')
285  dropbox_txt_file.write('till\n')
286  dropbox_txt_file.write('usertext '+str(dropbox_comment)+'\n')
287  dropbox_txt_file.close()
288 
289  #
290  #_____ loop over IOV to copy
291  #
292  #FIXME: move the binary inside the popcon job, and try multiple IOV
293  gotten_iovs = get_iovs(tag, input_pool_connect_string, mode)
294 
295  # stop processing current tag if IOVs were not obtained
296  if (gotten_iovs['result'] != "success"):
297  if (gotten_iovs['result'] == "fail_connect"):
298  print "Problems connecting to", input_pool_connect_string
299  print "Unable to process tag", tag_name, "continuing to the next tag..."
300  continue
301 
302  o2o_iovs = gotten_iovs['iovs']
303  isnewtag = gotten_iovs['newtag'] # is this a new POOL tag?
304  i = 0
305  for line in o2o_iovs:
306  i = i+1
307  iov = int(line)
308  pool_iov = iov
309 
310  if i==1: # first IOV to update may be a special case
311  if isnewtag:
312  print "Creating a new offline tag: ", tag_name
313  if condition_type == "DcsValues" and iov != 1:
314  print "DCS values: The first IOV in the online tag is ", iov
315  print "DCS values: Adding default DCS set values to the offline"
316  print "DCS values: tag for IOVs 1 - ", iov-1
317  print "DCS values: because first IOV in the offline tag must always be 1."
318  query_save = query
319  query_file_name = query_file_name+".default"
320  query_file = open(query_file_name, "r")
321  query = query_file.read()
322  query_file.close()
323  pool_iov = 1
324  make_popcon_config_file(python_popcon_file)
325  run_result = run_popcon()
326  query = query_save
327  pool_iov = iov
328  if run_result['status'] != "success":
329  break
330  else:
331  pool_iov = 1 # force the first IOV in offline tag to be 1 by default
332 
333  make_popcon_config_file(python_popcon_file)
334  run_result = run_popcon()
335  if run_result['status'] != "success":
336  break
337 
def get_tags(tag_list_file_name)
Definition: o2o.py:157
static void * communicate(void *obj)
Definition: DQMNet.cc:1251
def get_iovs(tag, input_pool_connect_string, mode)
Definition: o2o.py:171
def run_popcon()
Definition: o2o.py:217
def guess_condition_from_tag(tagname)
Definition: o2o.py:76
def make_popcon_config_file(filename)
Definition: o2o.py:52