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