CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FrontierCondition_GT_autoExpress_cfi.py
Go to the documentation of this file.
1 import FWCore.ParameterSet.Config as cms
3 GlobalTag.connect = "frontier://(proxyurl=http://localhost:3128)(serverurl=http://localhost:8000/FrontierOnProd)(serverurl=http://localhost:8000/FrontierOnProd)(retrieve-ziplevel=0)(failovertoserver=no)/CMS_CONDITIONS"
4 GlobalTag.pfnPrefix = cms.untracked.string("frontier://(proxyurl=http://localhost:3128)(serverurl=http://localhost:8000/FrontierOnProd)(serverurl=http://localhost:8000/FrontierOnProd)(retrieve-ziplevel=0)(failovertoserver=no)/")
5 GlobalTag.globaltag = "GR_E_V42"
6 es_prefer_GlobalTag = cms.ESPrefer('PoolDBESSource','GlobalTag')
7 
8 # ===== auto -> Automatically get the GT string from current Tier0 configuration via a Tier0Das call.
9 # This needs a valid proxy to access the cern.ch network from the .cms one.
10 #
11 auto=True
12 tier0DasUrl = 'https://cmsweb.cern.ch/t0wmadatasvc/prod/'
13 
14 import os
15 import json
16 import sys, urllib2
17 import time
18 import ast
19 
21  """
22  Class handling common Tier0-DAS queries and connected utilities
23  """
24  def __init__(self, url = 'https://cmsweb.cern.ch/t0wmadatasvc/prod/', proxy = None ):
25  """
26  Need base url for Tier0-DAS as input
27  """
28  self._t0DasBaseUrl = url
29  self._debug = False
30  self._retry = 0
31  self._maxretry = 5
32  self._proxy = proxy
33 
34 
35  def getData(self, src, tout=5):
36  """
37  Get the JSON file for a give query specified via the Tier0-DAS url.
38  Timeout can be set via paramter.
39  """
40  # actually get the json file from the given url of the T0-Das service
41  # and returns the data
42 
43  try:
44  if self._proxy:
45  print "setting proxy"
46  opener = urllib2.build_opener(urllib2.HTTPHandler(),
47  urllib2.HTTPSHandler(),
48  urllib2.ProxyHandler({'http':self._proxy, 'https':self._proxy}))
49  urllib2.install_opener(opener)
50  req = urllib2.Request(src)
51  req.add_header("User-Agent",
52  "DQMIntegration/1.0 python/%d.%d.%d" % sys.version_info[:3])
53  req.add_header("Accept","application/json")
54  jsonCall = urllib2.urlopen(req, timeout = tout)
55  url = jsonCall.geturl()
56  except urllib2.HTTPError, error:
57  #print error.url
58  errStr = "Cannot retrieve Tier-0 DAS data from URL \"" + error.url + "\""
59  if self._proxy:
60  errStr += " using proxy \"" + self._proxy + "\""
61  print errStr
62  print error
63  raise urllib2.HTTPError("FIXME: handle exceptions")
64  except urllib2.URLError, error:
65  if self._retry < self._maxretry:
66  print 'Try # ' + str(self._retry) + " connection to Tier-0 DAS timed-out"
67  self._retry += 1
68  newtout = tout*self._retry
69  time.sleep(3*self._retry)
70  return self.getData(src,newtout)
71  else:
72  errStr = "Cannot retrieve Tier-0 DAS data from URL \"" + src + "\""
73  if self._proxy:
74  errStr += " using proxy \"" + self._proxy + "\""
75  self._retry = 0
76  print errStr
77  print error
78  raise urllib2.URLError('TimeOut reading ' + src)
79 
80  except:
81  raise
82  else:
83  if self._debug:
84  print url
85  jsonInfo = jsonCall.info()
86  if self._debug:
87  print jsonInfo
88  jsonText = jsonCall.read()
89  data = json.loads(jsonText)
90  if self._debug:
91  print "data:", data
92  return data
93 
94  def getResultList(self, json):
95  """
96  Extractt the result list out of the JSON file
97  """
98  resultList = []
99  #FIXME try
100  resultList = json['result']
101 
102  #print self.getValues(json, 'result')
103  return resultList
104 
105  def getValues(self, json, key, selection=''):
106  """
107  Extract the value corrisponding to a given key from a JSON file. It is also possible to apply further selections.
108  """
109  # lookup for a key in a json file applying possible selections
110  data = []
111  check = 0
112  if selection != '':
113  check = 1
114  (k, v) = selection
115 
116  for o in json:
117  #print o
118  try:
119  if check == 1:
120  if (o[k] == v):
121  data.append(o[key])
122  else:
123  data.append(o[key])
124  except KeyError as error:
125  print "[Tier0DasInterface::getValues] key: " + key + " not found in json file"
126  print error
127  raise
128  except:
129  print "[Tier0DasInterface::getValues] unknown error"
130  raise
131  #pass
132  #print data
133  return data
134 
135  def lastPromptRun(self):
136  """
137  Query to get the last run released for prompt
138  """
139  url = self._t0DasBaseUrl + "reco_config"
140  try:
141  json = self.getData(url)
142  results = self.getResultList(json)
143  workflowlist = ast.literal_eval(results[0])
144  maxRun = -1
145  for workflow in workflowlist:
146  run = workflow['run']
147  if int(run) > maxRun:
148  maxRun = run
149  return maxRun
150  except:
151  print "[Tier0DasInterface::lastPromptRun] error"
152  raise
153  return 0
154 
156  """
157  Query to ge the run for which the Tier0 system considers safe the update to the conditions
158  """
159  url = self._t0DasBaseUrl + "firstconditionsaferun"
160  try:
161  json = self.getData(url)
162  results = self.getResultList(json)
163  return results[0]
164  except Exception as details:
165  print "[Tier0DasInterface::firstConditionSafeRun] error", details
166  raise
167  return 0
168 
169  def promptGlobalTag(self, dataset):
170  """
171  Query the GT currently used by prompt = GT used by the last run released for prompt.
172  """
173  url = self._t0DasBaseUrl + "reco_config"
174  #print "url =", url
175  try:
176  json = self.getData(url)
177  results = self.getResultList(json)
178  workflowlist = ast.literal_eval(results[0])
179  gt = "UNKNOWN"
180  for workflow in workflowlist:
181  if workflow['primary_dataset'] == dataset:
182  gt = workflow['global_tag']
183  # FIXME: do we realluy need to raise?
184  if gt == "UNKNOWN":
185  raise KeyError
186  return gt
187  except:
188  print "[Tier0DasInterface::promptGlobalTag] error"
189  raise
190  return None
191 
192  def expressGlobalTag(self):
193  """
194  Query the GT currently used by express = GT used by the last run released for express.
195  """
196  url = self._t0DasBaseUrl + "express_config"
197  #print "url =", url
198  try:
199  gt = "UNKNOWN"
200  json = self.getData(url)
201  results = self.getResultList(json)
202  config = ast.literal_eval(results[0])[0]
203  gt = config['global_tag']
204  # FIXME: do we realluy need to raise?
205  if gt == "UNKNOWN":
206  raise KeyError
207  return gt
208  except:
209  print "[Tier0DasInterface::expressGlobalTag] error"
210  raise
211  return None
212 
213 
214 if auto:
215  expressGT = "UNKNOWN"
216 
217  proxyurl = None
218  if 'http_proxy' in os.environ:
219  proxyurl = os.environ['http_proxy']
220  test = Tier0DasInterface(url=tier0DasUrl,proxy = proxyurl)
221 
222 
223  try:
224  expressGT = test.expressGlobalTag()
225  print "Tier0 DAS express GT: ", expressGT
226  except Exception as error:
227  print 'Error'
228  print error
229 
230  GlobalTag.globaltag = expressGT
231