Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-27 03:18:06

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