Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:29:04

0001 #!/usr/bin/env python3
0002 #____________________________________________________________
0003 #
0004 #  
0005 #
0006 # A very simple script to read beam spot DB payloads
0007 #
0008 # Francisco Yumiceva
0009 # yumiceva@fnal.gov
0010 #
0011 # Fermilab, 2009
0012 #
0013 #____________________________________________________________
0014 
0015 """
0016    getBeamSpotDB.py
0017 
0018    A very simple script to retrieve from DB a beam spot payload for a given IOV.
0019    usage: %prog -t <tag name> -r <run number = 1>
0020    -a, --auth     = AUTH: Authorization path: \"/afs/cern.ch/cms/DB/conddb\"(default), \"/nfshome0/popcondev/conddb\"
0021    -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\"
0022    -g, --globaltag= GLOBALTAG: Name of Global tag. If this is provided, no need to provide beam spot tags.
0023    -l, --lumi     = LUMI: Lumi section.
0024    -r, --run      = RUN: Run number.
0025    -t, --tag      = TAG: Name of Beam Spot DB tag.
0026    
0027    Francisco Yumiceva (yumiceva@fnal.gov)
0028    Fermilab 2010
0029    
0030 """
0031 from __future__ import print_function
0032 
0033 
0034 import sys,os, re
0035 import subprocess
0036 
0037 #_______________OPTIONS________________
0038 import optparse
0039 
0040 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
0041 
0042 def nonzero(self): # will become the nonzero method of optparse.Values
0043     "True if options were given"
0044     for v in self.__dict__.values():
0045         if v is not None: return True
0046     return False
0047 
0048 optparse.Values.__nonzero__ = nonzero # dynamically fix optparse.Values
0049 
0050 class ParsingError(Exception): pass
0051 
0052 optionstring=""
0053 
0054 def exit(msg=""):
0055     raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
0056 
0057 def parse(docstring, arglist=None):
0058     global optionstring
0059     optionstring = docstring
0060     match = USAGE.search(optionstring)
0061     if not match: raise ParsingError("Cannot find the option string")
0062     optlines = match.group(1).splitlines()
0063     try:
0064         p = optparse.OptionParser(optlines[0])
0065         for line in optlines[1:]:
0066             opt, help=line.split(':')[:2]
0067             short,long=opt.split(',')[:2]
0068             if '=' in opt:
0069                 action='store'
0070                 long=long.split('=')[0]
0071             else:
0072                 action='store_true'
0073             p.add_option(short.strip(),long.strip(),
0074                          action = action, help = help.strip())
0075     except (IndexError,ValueError):
0076         raise ParsingError("Cannot parse the option string correctly")
0077     return p.parse_args(arglist)
0078 
0079 
0080 
0081 # lumi tools CondCore/Utilities/python/timeUnitHelper.py
0082 def pack(high,low):
0083     """pack high,low 32bit unsigned int to one unsigned 64bit long long
0084        Note:the print value of result number may appear signed, if the sign bit is used.
0085     """
0086     h=high<<32
0087     return (h|low)
0088 
0089 def unpack(i):
0090     """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
0091     """
0092     high=i>>32
0093     low=i&0xFFFFFFFF
0094     return(high,low)
0095 
0096 def unpackLumiid(i):
0097     """unpack 64bit lumiid to dictionary {'run','lumisection'}
0098     """
0099     j=unpack(i)
0100     return {'run':j[0],'lumisection':j[1]}
0101 
0102 
0103 if __name__ == '__main__':
0104 
0105 
0106     # COMMAND LINE OPTIONS
0107     #################################
0108     option,args = parse(__doc__)
0109     if not args and not option: exit()
0110 
0111     tagname = ''
0112     globaltag = ''
0113 
0114     if ((option.tag and option.globaltag)) == False: 
0115         print(" NEED to provide beam spot DB tag name, or global tag")
0116         exit()
0117     elif option.tag:
0118         tagname = option.tag
0119     elif option.globaltag:
0120         globaltag = option.globaltag
0121         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'
0122         outcmd = subprocess.getstatusoutput( cmd )
0123         atag = outcmd[1].split()
0124         atag = atag[2]
0125         tagname = atag.replace("tag:","")
0126         print(" Global tag: "+globaltag+" includes the beam spot tag: "+tagname)
0127 
0128     iov_since = ''
0129     iov_till = ''
0130     destDB = 'frontier://PromptProd/CMS_COND_31X_BEAMSPOT'
0131     if option.destDB:
0132         destDB = option.destDB
0133 
0134     auth = '/afs/cern.ch/cms/DB/conddb'
0135     if option.auth:
0136         auth = option.auth
0137 
0138     run = '1'
0139     if option.run:
0140         run = option.run
0141     lumi = '1'
0142     if option.lumi:
0143         lumi = option.lumi
0144 
0145     #sqlite_file = "sqlite_file:"+ tagname +".db"
0146 
0147 
0148     ##### READ 
0149 
0150     #print "read back sqlite file to check content ..."
0151 
0152     readdb_out = "readDB_"+tagname+".py"
0153 
0154     rnewfile = open(readdb_out,'w')
0155 
0156     rnewfile.write('''
0157 import FWCore.ParameterSet.Config as cms
0158 
0159 process = cms.Process("readDB")
0160 process.load("FWCore.MessageLogger.MessageLogger_cfi")
0161 
0162 process.load("CondCore.DBCommon.CondDBSetup_cfi")
0163 
0164 process.BeamSpotDBSource = cms.ESSource("PoolDBESSource",
0165                                         process.CondDBSetup,
0166                                         toGet = cms.VPSet(cms.PSet(
0167     record = cms.string('BeamSpotObjectsRcd'),
0168 ''')
0169     rnewfile.write('tag = cms.string(\''+tagname+'\')\n')
0170     rnewfile.write(')),\n')
0171     rnewfile.write('connect = cms.string(\''+destDB+'\')\n')
0172 
0173     #connect = cms.string('sqlite_file:Early900GeVCollision_7p4cm_STARTUP_mc.db')
0174     #connect = cms.string('oracle://cms_orcoff_prod/CMS_COND_31X_BEAMSPOT')
0175     #connect = cms.string('frontier://PromptProd/CMS_COND_31X_BEAMSPOT')
0176     rnewfile.write('''
0177                                         )
0178 
0179 ''')
0180     rnewfile.write('process.BeamSpotDBSource.DBParameters.authenticationPath = cms.untracked.string(\"'+auth + '\")')
0181 
0182     rnewfile.write('''
0183 
0184 process.source = cms.Source("EmptySource",
0185         numberEventsInRun = cms.untracked.uint32(1),
0186 ''')
0187     rnewfile.write('  firstRun = cms.untracked.uint32('+ run + '),\n')
0188     rnewfile.write('  firstLuminosityBlock = cms.untracked.uint32('+ lumi + ')\n')
0189     rnewfile.write('''               
0190 )
0191 
0192 process.maxEvents = cms.untracked.PSet(
0193             input = cms.untracked.int32(1)
0194 )
0195 process.beamspot = cms.EDAnalyzer("BeamSpotFromDB")
0196 
0197 
0198 process.p = cms.Path(process.beamspot)
0199 
0200 ''')
0201 
0202     rnewfile.close()
0203     status_rDB = subprocess.getstatusoutput('cmsRun '+ readdb_out)
0204 
0205     outtext = status_rDB[1]
0206     print(outtext)
0207 
0208     #### CLEAN up
0209     #os.system("rm "+ readdb_out)
0210 
0211     print("DONE.\n")
0212 
0213 #_________________________________    
0214