CMS 3D CMS Logo

getBeamSpotDB.py
Go to the documentation of this file.
1 #!/usr/bin/env python
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 commands
36 import six
37 
38 #_______________OPTIONS________________
39 import optparse
40 
41 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
42 
43 def nonzero(self): # will become the nonzero method of optparse.Values
44  "True if options were given"
45  for v in six.itervalues(self.__dict__):
46  if v is not None: return True
47  return False
48 
49 optparse.Values.__nonzero__ = nonzero # dynamically fix optparse.Values
50 
51 class ParsingError(Exception): pass
52 
53 optionstring=""
54 
55 def exit(msg=""):
56  raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
57 
58 def parse(docstring, arglist=None):
59  global optionstring
60  optionstring = docstring
61  match = USAGE.search(optionstring)
62  if not match: raise ParsingError("Cannot find the option string")
63  optlines = match.group(1).splitlines()
64  try:
65  p = optparse.OptionParser(optlines[0])
66  for line in optlines[1:]:
67  opt, help=line.split(':')[:2]
68  short,long=opt.split(',')[:2]
69  if '=' in opt:
70  action='store'
71  long=long.split('=')[0]
72  else:
73  action='store_true'
74  p.add_option(short.strip(),long.strip(),
75  action = action, help = help.strip())
76  except (IndexError,ValueError):
77  raise ParsingError("Cannot parse the option string correctly")
78  return p.parse_args(arglist)
79 
80 
81 
82 # lumi tools CondCore/Utilities/python/timeUnitHelper.py
83 def pack(high,low):
84  """pack high,low 32bit unsigned int to one unsigned 64bit long long
85  Note:the print value of result number may appear signed, if the sign bit is used.
86  """
87  h=high<<32
88  return (h|low)
89 
90 def unpack(i):
91  """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
92  """
93  high=i>>32
94  low=i&0xFFFFFFFF
95  return(high,low)
96 
97 def unpackLumiid(i):
98  """unpack 64bit lumiid to dictionary {'run','lumisection'}
99  """
100  j=unpack(i)
101  return {'run':j[0],'lumisection':j[1]}
102 
103 
104 if __name__ == '__main__':
105 
106 
107  # COMMAND LINE OPTIONS
108  #################################
109  option,args = parse(__doc__)
110  if not args and not option: exit()
111 
112  tagname = ''
113  globaltag = ''
114 
115  if ((option.tag and option.globaltag)) == False:
116  print(" NEED to provide beam spot DB tag name, or global tag")
117  exit()
118  elif option.tag:
119  tagname = option.tag
120  elif option.globaltag:
121  globaltag = option.globaltag
122  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'
123  outcmd = commands.getstatusoutput( cmd )
124  atag = outcmd[1].split()
125  atag = atag[2]
126  tagname = atag.replace("tag:","")
127  print(" Global tag: "+globaltag+" includes the beam spot tag: "+tagname)
128 
129  iov_since = ''
130  iov_till = ''
131  destDB = 'frontier://PromptProd/CMS_COND_31X_BEAMSPOT'
132  if option.destDB:
133  destDB = option.destDB
134 
135  auth = '/afs/cern.ch/cms/DB/conddb'
136  if option.auth:
137  auth = option.auth
138 
139  run = '1'
140  if option.run:
141  run = option.run
142  lumi = '1'
143  if option.lumi:
144  lumi = option.lumi
145 
146  #sqlite_file = "sqlite_file:"+ tagname +".db"
147 
148 
149  ##### READ
150 
151  #print "read back sqlite file to check content ..."
152 
153  readdb_out = "readDB_"+tagname+".py"
154 
155  rnewfile = open(readdb_out,'w')
156 
157  rnewfile.write('''
158 import FWCore.ParameterSet.Config as cms
159 
160 process = cms.Process("readDB")
161 process.load("FWCore.MessageLogger.MessageLogger_cfi")
162 
163 process.load("CondCore.DBCommon.CondDBSetup_cfi")
164 
165 process.BeamSpotDBSource = cms.ESSource("PoolDBESSource",
166  process.CondDBSetup,
167  toGet = cms.VPSet(cms.PSet(
168  record = cms.string('BeamSpotObjectsRcd'),
169 ''')
170  rnewfile.write('tag = cms.string(\''+tagname+'\')\n')
171  rnewfile.write(')),\n')
172  rnewfile.write('connect = cms.string(\''+destDB+'\')\n')
173 
174  #connect = cms.string('sqlite_file:Early900GeVCollision_7p4cm_STARTUP_mc.db')
175  #connect = cms.string('oracle://cms_orcoff_prod/CMS_COND_31X_BEAMSPOT')
176  #connect = cms.string('frontier://PromptProd/CMS_COND_31X_BEAMSPOT')
177  rnewfile.write('''
178  )
179 
180 ''')
181  rnewfile.write('process.BeamSpotDBSource.DBParameters.authenticationPath = cms.untracked.string(\"'+auth + '\")')
182 
183  rnewfile.write('''
184 
185 process.source = cms.Source("EmptySource",
186  numberEventsInRun = cms.untracked.uint32(1),
187 ''')
188  rnewfile.write(' firstRun = cms.untracked.uint32('+ run + '),\n')
189  rnewfile.write(' firstLuminosityBlock = cms.untracked.uint32('+ lumi + ')\n')
190  rnewfile.write('''
191 )
192 
193 process.maxEvents = cms.untracked.PSet(
194  input = cms.untracked.int32(1)
195 )
196 process.beamspot = cms.EDAnalyzer("BeamSpotFromDB")
197 
198 
199 process.p = cms.Path(process.beamspot)
200 
201 ''')
202 
203  rnewfile.close()
204  status_rDB = commands.getstatusoutput('cmsRun '+ readdb_out)
205 
206  outtext = status_rDB[1]
207  print(outtext)
208 
209  #### CLEAN up
210  #os.system("rm "+ readdb_out)
211 
212  print("DONE.\n")
213 
214 #_________________________________
215 
def nonzero(self)
def exit(msg="")
def unpackLumiid(i)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
return((rh^lh)&mask)
def pack(high, low)
def parse(docstring, arglist=None)
double split
Definition: MVATrainer.cc:139