CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoVertex/BeamSpotProducer/scripts/getBeamSpotDB.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #____________________________________________________________
00003 #
00004 #  
00005 #
00006 # A very simple script to read beam spot DB payloads
00007 #
00008 # Francisco Yumiceva
00009 # yumiceva@fnal.gov
00010 #
00011 # Fermilab, 2009
00012 #
00013 #____________________________________________________________
00014 
00015 """
00016    getBeamSpotDB.py
00017 
00018    A very simple script to retrieve from DB a beam spot payload for a given IOV.
00019    usage: %prog -t <tag name> -r <run number = 1>
00020    -a, --auth     = AUTH: Authorization path: \"/afs/cern.ch/cms/DB/conddb\"(default), \"/nfshome0/popcondev/conddb\"
00021    -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\"
00022    -g, --globaltag= GLOBALTAG: Name of Global tag. If this is provided, no need to provide beam spot tags.
00023    -l, --lumi     = LUMI: Lumi section.
00024    -r, --run      = RUN: Run number.
00025    -t, --tag      = TAG: Name of Beam Spot DB tag.
00026    
00027    Francisco Yumiceva (yumiceva@fnal.gov)
00028    Fermilab 2010
00029    
00030 """
00031 
00032 
00033 import sys,os, re
00034 import commands
00035 
00036 #_______________OPTIONS________________
00037 import optparse
00038 
00039 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
00040 
00041 def nonzero(self): # will become the nonzero method of optparse.Values
00042     "True if options were given"
00043     for v in self.__dict__.itervalues():
00044         if v is not None: return True
00045     return False
00046 
00047 optparse.Values.__nonzero__ = nonzero # dynamically fix optparse.Values
00048 
00049 class ParsingError(Exception): pass
00050 
00051 optionstring=""
00052 
00053 def exit(msg=""):
00054     raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
00055 
00056 def parse(docstring, arglist=None):
00057     global optionstring
00058     optionstring = docstring
00059     match = USAGE.search(optionstring)
00060     if not match: raise ParsingError("Cannot find the option string")
00061     optlines = match.group(1).splitlines()
00062     try:
00063         p = optparse.OptionParser(optlines[0])
00064         for line in optlines[1:]:
00065             opt, help=line.split(':')[:2]
00066             short,long=opt.split(',')[:2]
00067             if '=' in opt:
00068                 action='store'
00069                 long=long.split('=')[0]
00070             else:
00071                 action='store_true'
00072             p.add_option(short.strip(),long.strip(),
00073                          action = action, help = help.strip())
00074     except (IndexError,ValueError):
00075         raise ParsingError("Cannot parse the option string correctly")
00076     return p.parse_args(arglist)
00077 
00078 
00079 
00080 # lumi tools CondCore/Utilities/python/timeUnitHelper.py
00081 def pack(high,low):
00082     """pack high,low 32bit unsigned int to one unsigned 64bit long long
00083        Note:the print value of result number may appear signed, if the sign bit is used.
00084     """
00085     h=high<<32
00086     return (h|low)
00087 
00088 def unpack(i):
00089     """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
00090     """
00091     high=i>>32
00092     low=i&0xFFFFFFFF
00093     return(high,low)
00094 
00095 def unpackLumiid(i):
00096     """unpack 64bit lumiid to dictionary {'run','lumisection'}
00097     """
00098     j=unpack(i)
00099     return {'run':j[0],'lumisection':j[1]}
00100 
00101 
00102 if __name__ == '__main__':
00103 
00104 
00105     # COMMAND LINE OPTIONS
00106     #################################
00107     option,args = parse(__doc__)
00108     if not args and not option: exit()
00109     
00110     tagname = ''
00111     globaltag = ''
00112     
00113     if ((option.tag and option.globaltag)) == False: 
00114         print " NEED to provide beam spot DB tag name, or global tag"
00115         exit()
00116     elif option.tag:
00117         tagname = option.tag
00118     elif option.globaltag:
00119         globaltag = option.globaltag
00120         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'
00121         outcmd = commands.getstatusoutput( cmd )
00122         atag = outcmd[1].split()
00123         atag = atag[2]
00124         tagname = atag.replace("tag:","")
00125         print " Global tag: "+globaltag+" includes the beam spot tag: "+tagname
00126     
00127     iov_since = ''
00128     iov_till = ''
00129     destDB = 'frontier://PromptProd/CMS_COND_31X_BEAMSPOT'
00130     if option.destDB:
00131         destDB = option.destDB
00132     
00133     auth = '/afs/cern.ch/cms/DB/conddb'
00134     if option.auth:
00135         auth = option.auth
00136     
00137     run = '1'
00138     if option.run:
00139         run = option.run
00140     lumi = '1'
00141     if option.lumi:
00142         lumi = option.lumi
00143         
00144     #sqlite_file = "sqlite_file:"+ tagname +".db"
00145 
00146     
00147     ##### READ 
00148 
00149     #print "read back sqlite file to check content ..."
00150     
00151     readdb_out = "readDB_"+tagname+".py"
00152     
00153     rnewfile = open(readdb_out,'w')
00154 
00155     rnewfile.write('''
00156 import FWCore.ParameterSet.Config as cms
00157 
00158 process = cms.Process("readDB")
00159 process.load("FWCore.MessageLogger.MessageLogger_cfi")
00160 
00161 process.load("CondCore.DBCommon.CondDBSetup_cfi")
00162 
00163 process.BeamSpotDBSource = cms.ESSource("PoolDBESSource",
00164                                         process.CondDBSetup,
00165                                         toGet = cms.VPSet(cms.PSet(
00166     record = cms.string('BeamSpotObjectsRcd'),
00167 ''')
00168     rnewfile.write('tag = cms.string(\''+tagname+'\')\n')
00169     rnewfile.write(')),\n')
00170     rnewfile.write('connect = cms.string(\''+destDB+'\')\n')
00171     
00172     #connect = cms.string('sqlite_file:Early900GeVCollision_7p4cm_STARTUP_mc.db')
00173     #connect = cms.string('oracle://cms_orcoff_prod/CMS_COND_31X_BEAMSPOT')
00174     #connect = cms.string('frontier://PromptProd/CMS_COND_31X_BEAMSPOT')
00175     rnewfile.write('''
00176                                         )
00177 
00178 ''')
00179     rnewfile.write('process.BeamSpotDBSource.DBParameters.authenticationPath = cms.untracked.string(\"'+auth + '\")')
00180 
00181     rnewfile.write('''
00182 
00183 process.source = cms.Source("EmptySource",
00184         numberEventsInRun = cms.untracked.uint32(1),
00185 ''')
00186     rnewfile.write('  firstRun = cms.untracked.uint32('+ run + '),\n')
00187     rnewfile.write('  firstLuminosityBlock = cms.untracked.uint32('+ lumi + ')\n')
00188     rnewfile.write('''               
00189 )
00190 
00191 process.maxEvents = cms.untracked.PSet(
00192             input = cms.untracked.int32(1)
00193 )
00194 process.beamspot = cms.EDAnalyzer("BeamSpotFromDB")
00195 
00196 
00197 process.p = cms.Path(process.beamspot)
00198 
00199 ''')
00200         
00201     rnewfile.close()
00202     status_rDB = commands.getstatusoutput('cmsRun '+ readdb_out)
00203     
00204     outtext = status_rDB[1]
00205     print outtext
00206 
00207     #### CLEAN up
00208     #os.system("rm "+ readdb_out)
00209 
00210     print "DONE.\n"
00211     
00212 #_________________________________    
00213