CMS 3D CMS Logo

run_AlCaRecoTriggerBitsUpdateWorkflow.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 """
4 Example script to test reading from local sqlite db.
5 """
6 from __future__ import print_function
7 import os
8 import sys
9 import ast
10 import optparse
11 import multiprocessing
12 import CondCore.Utilities.conddblib as conddb
13 
14 officialdbs = {
15  # frontier connections
16  'frontier://PromptProd/CMS_CONDITIONS' :'pro',
17  'frontier://FrontierProd/CMS_CONDITIONS' :'pro',
18  'frontier://FrontierArc/CMS_CONDITIONS' :'arc',
19  'frontier://FrontierInt/CMS_CONDITIONS' :'int',
20  'frontier://FrontierPrep/CMS_CONDITIONS' :'dev'
21 }
22 
23 
24 def getCommandOutput(command):
25 
26  """This function executes `command` and returns it output.
27  Arguments:
28  - `command`: Shell command to be invoked by this function.
29  """
30  print(command)
31  child = os.popen(command)
32  data = child.read()
33  err = child.close()
34  if err:
35  raise Exception('%s failed w/ exit code %d' % (command, err))
36  return data
37 
38 
39 def get_iovs(db, tag):
40 
41  """Retrieve the list of IOVs from `db` for `tag`.
42 
43  Arguments:
44  - `db`: database connection string
45  - `tag`: tag of database record
46  """
47 
48 
50  if db in officialdbs.keys():
51  db = officialdbs[db]
52 
53 
54  db = db.replace("sqlite_file:", "").replace("sqlite:", "")
55 
56  con = conddb.connect(url = conddb.make_url(db))
57  session = con.session()
58  IOV = session.get_dbtype(conddb.IOV)
59 
60  iovs = set(session.query(IOV.since).filter(IOV.tag_name == tag).all())
61  if len(iovs) == 0:
62  print("No IOVs found for tag '"+tag+"' in database '"+db+"'.")
63  sys.exit(1)
64 
65  session.close()
66 
67  return sorted([int(item[0]) for item in iovs])
68 
69 
70 def updateBits(blob):
71 
72  if(blob[0]<blob[1]):
73  command = 'conddb --yes --db %s copy %s --destdb %s --from %s --to %s' % (blob[4],
74  blob[5],
75  blob[5]+"_IOV_"+str(blob[2])+".db",
76  str(blob[2]) ,
77  str(blob[3]))
78  getCommandOutput(command)
79  else:
80  # last IOV needs special command
81  command = 'conddb --yes --db %s copy %s --destdb %s --from %s' % (blob[4],
82  blob[5],
83  blob[5]+"_IOV_"+str(blob[2])+".db",
84  str(blob[2]))
85  getCommandOutput(command)
86 
87  # update the trigger bits
88  cmsRunCommand='cmsRun $CMSSW_BASE/src/CondTools/HLT/test/AlCaRecoTriggerBitsRcdUpdate_TEMPL_cfg.py \
89  inputDB=%s inputTag=%s outputDB=%s outputTag=%s firstRun=%s' % ("sqlite_file:"+blob[5]+"_IOV_"+str(blob[2])+".db",
90  blob[5],
91  "sqlite_file:"+blob[5]+"_IOV_"+str(blob[2])+"_updated.db",
92  blob[6],
93  str(blob[2]))
94  getCommandOutput(cmsRunCommand)
95 
96 
97 def main():
98 
99 
100  defaultDB = 'sqlite_file:mySqlite.db'
101  defaultInTag = 'myInTag'
102  defaultOutTag = 'myOutTag'
103  defaultProc = 20
104 
105  parser = optparse.OptionParser(usage = 'Usage: %prog [options] <file> [<file> ...]\n')
106  parser.add_option('-f', '--inputDB',
107  dest = 'inputDB',
108  default = defaultDB,
109  help = 'file to inspect')
110  parser.add_option('-i', '--inputTag',
111  dest = 'InputTag',
112  default = defaultInTag,
113  help = 'tag to be inspected')
114  parser.add_option('-d', '--destTag',
115  dest = 'destTag',
116  default = defaultOutTag,
117  help = 'tag to be written')
118  parser.add_option('-p', '--processes',
119  dest = 'nproc',
120  default = defaultProc,
121  help = 'multiprocesses to run')
122  parser.add_option("-C", '--clean',
123  dest="doTheCleaning",
124  action="store_true",
125  default = True,
126  help = 'if true remove the transient files')
127 
128  (options, arguments) = parser.parse_args()
129 
130  db_url = options.inputDB
131  if db_url in officialdbs.keys():
132  db_url = officialdbs[db_url]
133 
134 
135  db_url = db_url.replace("sqlite_file:", "").replace("sqlite:", "")
136 
137  sinces = get_iovs(options.inputDB,options.InputTag)
138 
139  print("List of sinces: %s" % sinces)
140 
141  myInputTuple=[]
142 
143  for i,since in enumerate(sinces):
144  if(i<len(sinces)-1):
145  # 0 1 2 3 4 5 6
146  myInputTuple.append((i,len(sinces)-1,sinces[i],sinces[i+1]-1,db_url,options.InputTag,options.destTag))
147  else:
148  myInputTuple.append((i,len(sinces)-1,sinces[i],-1,db_url,options.InputTag,options.destTag))
149 
150  pool = multiprocessing.Pool(processes=options.nproc) # start nproc worker processes
151  count = pool.map(updateBits,myInputTuple)
152 
153  # merge the output
154  for i,since in enumerate(sinces):
155  mergeCommand='conddb --yes --db %s copy %s --destdb %s --from %s' % (options.InputTag+"_IOV_"+str(sinces[i])+"_updated.db",
156  options.destTag,
157  options.destTag+".db",
158  str(sinces[i]))
159  getCommandOutput(mergeCommand)
160 
161  # clean the house (after all is done)
162  if(options.doTheCleaning):
163  cleanCommand = 'rm -fr *updated*.db *IOV_*.db'
164  getCommandOutput(cleanCommand)
165  else:
166  print("======> keeping the transient files")
167 
168 if __name__ == "__main__":
169  main()
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
def replace(string, replacements)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Definition: main.py:1
#define str(s)
if(threadIdxLocalY==0 &&threadIdxLocalX==0)