CMS 3D CMS Logo

getBeamSpotDB.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #____________________________________________________________
3 #
4 #
5 #
6 # A very simple script to read beam spot DB payloads
7 #
8 # Francisco Yumiceva
9 # yumiceva@fnal.gov
10 #
11 # Fermilab, 2009
12 #
13 #____________________________________________________________
14 
15 """
16  getBeamSpotDB.py
17 
18  A very simple script to retrieve from DB a beam spot payload for a given IOV.
19  usage: %prog -t <tag name> -r <run number = 1>
20  -a, --auth = AUTH: Authorization path: \"/afs/cern.ch/cms/DB/conddb\"(default), \"/nfshome0/popcondev/conddb\"
21  -d, --destDB = DESTDB: Destination string for DB connection: \"frontier://PromptProd/CMS_COND_31X_BEAMSPOT\"(default), \"oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT\", \"sqlite_file:mysqlitefile.db\"
22  -g, --globaltag= GLOBALTAG: Name of Global tag. If this is provided, no need to provide beam spot tags.
23  -l, --lumi = LUMI: Lumi section.
24  -r, --run = RUN: Run number.
25  -t, --tag = TAG: Name of Beam Spot DB tag.
26 
27  Francisco Yumiceva (yumiceva@fnal.gov)
28  Fermilab 2010
29 
30 """
31 from __future__ import print_function
32 
33 
34 import sys,os, re
35 import subprocess
36 
37 #_______________OPTIONS________________
38 import optparse
39 
40 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
41 
42 def nonzero(self): # will become the nonzero method of optparse.Values
43  "True if options were given"
44  for v in self.__dict__.values():
45  if v is not None: return True
46  return False
47 
48 optparse.Values.__nonzero__ = nonzero # dynamically fix optparse.Values
49 
50 class ParsingError(Exception): pass
51 
52 optionstring=""
53 
54 def exit(msg=""):
55  raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
56 
57 def parse(docstring, arglist=None):
58  global optionstring
59  optionstring = docstring
60  match = USAGE.search(optionstring)
61  if not match: raise ParsingError("Cannot find the option string")
62  optlines = match.group(1).splitlines()
63  try:
64  p = optparse.OptionParser(optlines[0])
65  for line in optlines[1:]:
66  opt, help=line.split(':')[:2]
67  short,long=opt.split(',')[:2]
68  if '=' in opt:
69  action='store'
70  long=long.split('=')[0]
71  else:
72  action='store_true'
73  p.add_option(short.strip(),long.strip(),
74  action = action, help = help.strip())
75  except (IndexError,ValueError):
76  raise ParsingError("Cannot parse the option string correctly")
77  return p.parse_args(arglist)
78 
79 
80 
81 # lumi tools CondCore/Utilities/python/timeUnitHelper.py
82 def pack(high,low):
83  """pack high,low 32bit unsigned int to one unsigned 64bit long long
84  Note:the print value of result number may appear signed, if the sign bit is used.
85  """
86  h=high<<32
87  return (h|low)
88 
89 def unpack(i):
90  """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
91  """
92  high=i>>32
93  low=i&0xFFFFFFFF
94  return(high,low)
95 
96 def unpackLumiid(i):
97  """unpack 64bit lumiid to dictionary {'run','lumisection'}
98  """
99  j=unpack(i)
100  return {'run':j[0],'lumisection':j[1]}
101 
102 
103 if __name__ == '__main__':
104 
105 
106  # COMMAND LINE OPTIONS
107 
108  option,args = parse(__doc__)
109  if not args and not option: exit()
110 
111  tagname = ''
112  globaltag = ''
113 
114  if ((option.tag and option.globaltag)) == False:
115  print(" NEED to provide beam spot DB tag name, or global tag")
116  exit()
117  elif option.tag:
118  tagname = option.tag
119  elif option.globaltag:
120  globaltag = option.globaltag
121  cmd = 'cmscond_tagtree_list -c frontier://cmsfrontier.cern.ch:8000/Frontier/CMS_COND_31X_GLOBALTAG -P /afs/cern.ch/cms/DB/conddb -T '+globaltag+' | grep BeamSpot'
122  outcmd = subprocess.getstatusoutput( cmd )
123  atag = outcmd[1].split()
124  atag = atag[2]
125  tagname = atag.replace("tag:","")
126  print(" Global tag: "+globaltag+" includes the beam spot tag: "+tagname)
127 
128  iov_since = ''
129  iov_till = ''
130  destDB = 'frontier://PromptProd/CMS_COND_31X_BEAMSPOT'
131  if option.destDB:
132  destDB = option.destDB
133 
134  auth = '/afs/cern.ch/cms/DB/conddb'
135  if option.auth:
136  auth = option.auth
137 
138  run = '1'
139  if option.run:
140  run = option.run
141  lumi = '1'
142  if option.lumi:
143  lumi = option.lumi
144 
145  #sqlite_file = "sqlite_file:"+ tagname +".db"
146 
147 
148 
149 
150  #print "read back sqlite file to check content ..."
151 
152  readdb_out = "readDB_"+tagname+".py"
153 
154  rnewfile = open(readdb_out,'w')
155 
156  rnewfile.write('''
157 import FWCore.ParameterSet.Config as cms
158 
159 process = cms.Process("readDB")
160 process.load("FWCore.MessageLogger.MessageLogger_cfi")
161 
162 process.load("CondCore.DBCommon.CondDBSetup_cfi")
163 
164 process.BeamSpotDBSource = cms.ESSource("PoolDBESSource",
165  process.CondDBSetup,
166  toGet = cms.VPSet(cms.PSet(
167  record = cms.string('BeamSpotObjectsRcd'),
168 ''')
169  rnewfile.write('tag = cms.string(\''+tagname+'\')\n')
170  rnewfile.write(')),\n')
171  rnewfile.write('connect = cms.string(\''+destDB+'\')\n')
172 
173  #connect = cms.string('sqlite_file:Early900GeVCollision_7p4cm_STARTUP_mc.db')
174  #connect = cms.string('oracle://cms_orcoff_prod/CMS_COND_31X_BEAMSPOT')
175  #connect = cms.string('frontier://PromptProd/CMS_COND_31X_BEAMSPOT')
176  rnewfile.write('''
177  )
178 
179 ''')
180  rnewfile.write('process.BeamSpotDBSource.DBParameters.authenticationPath = cms.untracked.string(\"'+auth + '\")')
181 
182  rnewfile.write('''
183 
184 process.source = cms.Source("EmptySource",
185  numberEventsInRun = cms.untracked.uint32(1),
186 ''')
187  rnewfile.write(' firstRun = cms.untracked.uint32('+ run + '),\n')
188  rnewfile.write(' firstLuminosityBlock = cms.untracked.uint32('+ lumi + ')\n')
189  rnewfile.write('''
190 )
191 
192 process.maxEvents = cms.untracked.PSet(
193  input = cms.untracked.int32(1)
194 )
195 process.beamspot = cms.EDAnalyzer("BeamSpotFromDB")
196 
197 
198 process.p = cms.Path(process.beamspot)
199 
200 ''')
201 
202  rnewfile.close()
203  status_rDB = subprocess.getstatusoutput('cmsRun '+ readdb_out)
204 
205  outtext = status_rDB[1]
206  print(outtext)
207 
208 
210 
211  print("DONE.\n")
212 
213 #_________________________________
214 
def nonzero(self)
def exit(msg="")
def unpackLumiid(i)
return((rh ^ lh) &mask)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def pack(high, low)
def parse(docstring, arglist=None)