Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-06 06:06:24

0001 import FWCore.ParameterSet.Config as cms
0002 import FWCore.ParameterSet.VarParsing as opts
0003 import csv
0004 from io import open
0005 
0006 options = opts.VarParsing ('standard')
0007 
0008 options.register('MagField',
0009                  None,
0010                  opts.VarParsing.multiplicity.singleton,
0011                  opts.VarParsing.varType.float,
0012                  'Magnetic field value in Tesla')
0013 options.register('Year',
0014                  None,
0015                  opts.VarParsing.multiplicity.singleton,
0016                  opts.VarParsing.varType.string,
0017                  'Current year for versioning')
0018 options.register('Version',
0019                  None,
0020                  opts.VarParsing.multiplicity.singleton,
0021                  opts.VarParsing.varType.string,
0022                  'Template DB object version')
0023 options.register('Append',
0024                  None,
0025                  opts.VarParsing.multiplicity.singleton,
0026                  opts.VarParsing.varType.string,
0027                  'Any additional string to add to the filename, i.e. "bugfix", etc.')
0028 options.register('Map',
0029                  '../data/template1D_IOV0_preMC/IOV0_phase1_preMC_map.csv',
0030                  opts.VarParsing.multiplicity.singleton,
0031                  opts.VarParsing.varType.string,
0032                  'Path to map file')
0033 options.register('Delimiter',
0034                  ',',
0035                  opts.VarParsing.multiplicity.singleton,
0036                  opts.VarParsing.varType.string,
0037                  'Delimiter in csv file')
0038 options.register('Quotechar',
0039                  '"',
0040                  opts.VarParsing.multiplicity.singleton,
0041                  opts.VarParsing.varType.string,
0042                  'Quotechar in csv file')
0043 options.register('TemplateFilePath',
0044                  'CondTools/SiPixel/data/template1D_IOV0_preMC',
0045                  opts.VarParsing.multiplicity.singleton,
0046                  opts.VarParsing.varType.string,
0047                  'Location of template files')
0048 options.register('GlobalTag',
0049                          'auto:phase2_realistic',
0050                  opts.VarParsing.multiplicity.singleton,
0051                  opts.VarParsing.varType.string,
0052                  'Global tag for this run')
0053 options.register('useVectorIndices',
0054                  False,
0055                  opts.VarParsing.multiplicity.singleton,
0056                  opts.VarParsing.varType.bool,
0057                  'Switch on in case Morris uses vector indices in csv file, eg. [0,(N-1)] instead of [1,N]')
0058 options.register('geometry',
0059                  'T5',
0060                  opts.VarParsing.multiplicity.singleton,
0061                  opts.VarParsing.varType.string,
0062                  'Tracker Geometry Default = T5')
0063 options.parseArguments()
0064 
0065 MagFieldValue = 10.*options.MagField #code needs it in deciTesla
0066 print('\nMagField = %f deciTesla \n'%(MagFieldValue))
0067 version = options.Version
0068 print('\nVersion = %s \n'%(version))
0069 magfieldstrsplit = str(options.MagField).split('.')
0070 MagFieldString = magfieldstrsplit[0]
0071 if len(magfieldstrsplit)>1 :
0072     MagFieldString+=magfieldstrsplit[1]
0073 
0074 #open the map file
0075 mapfile = open(options.Map,'r', newline='')
0076 #read the csv file into a reader
0077 mapfilereader = csv.reader(mapfile,delimiter=options.Delimiter,quotechar=options.Quotechar)
0078 #separate into the different sections
0079 barrel_rule_lines = []; endcap_rule_lines = []
0080 barrel_exception_lines = []; endcap_exception_lines = []
0081 sections = [barrel_rule_lines, endcap_rule_lines, barrel_exception_lines, endcap_exception_lines]
0082 i=0; line = next(mapfilereader)
0083 for i in range(len(sections)) :
0084     while line[0].find('TEMPLATE ID')==-1 : #skip to just before the section of info
0085         line=next(mapfilereader)
0086     try :
0087         line=next(mapfilereader)
0088     except StopIteration :
0089         print('Done reading input file')
0090         break
0091     while line[1]!='' : #add the lines that are the barrel rules
0092         sections[i].append(line) 
0093         try :
0094             line=next(mapfilereader)
0095         except StopIteration :
0096             print('Done reading input file')
0097             break
0098 #print 'barrel rules = %s\nendcap rules = %s\nbarrel exceptions = %s\nendcap exceptions = %s'%(barrel_rule_lines,endcap_rule_lines,barrel_exception_lines,endcap_exception_lines) #DEBUG
0099 #Make the lists of location strings and template IDs
0100 barrel_locations = []
0101 barrel_template_IDs = []
0102 endcap_locations = []
0103 endcap_template_IDs = []
0104 template_filenames = []
0105 prefix = options.TemplateFilePath+'/template_summary_zp'
0106 suffix = '.out'
0107 for s in range(len(sections)) :
0108     for line in sections[s] :
0109     #   print 'reading line: %s'%(line) #DEBUG
0110         template_ID_s = line[0]
0111         while len(template_ID_s)<4 :
0112             template_ID_s='0'+template_ID_s
0113         newtemplatefilename = prefix+template_ID_s+suffix
0114         template_ID = int(template_ID_s)
0115         if not newtemplatefilename in template_filenames :
0116             template_filenames.append(newtemplatefilename)
0117         if s%2==0 :
0118             lay, lad, mod = line[1], line[2], line[3]
0119     #       print ' lay = %s, lad = %s, mod = %s'%(lay, lad, mod) #DEBUG
0120             #barrel ID strings are "layer_ladder_module"
0121             laysplit = lay.split('-'); firstlay=int(laysplit[0]); lastlay= int(laysplit[1])+1 if len(laysplit)>1 else firstlay+1
0122             for i in range(firstlay,lastlay) :
0123                 lay_string = str(i)+'_'
0124                 ladsplit = lad.split('-'); firstlad=int(ladsplit[0]); lastlad= int(ladsplit[1])+1 if len(ladsplit)>1 else firstlad+1
0125                 for j in range(firstlad,lastlad) :
0126                     lad_string = lay_string+str(j)+'_'
0127                     modsplit = mod.split('-'); firstmod=int(modsplit[0]); lastmod= int(modsplit[1])+1 if len(modsplit)>1 else firstmod+1
0128                     for k in range(firstmod,lastmod) :
0129                         location_string = lad_string+str(k)
0130                         if s==0 :
0131     #                       print ' Adding with location string "%s" and template ID %d'%(location_string,template_ID) #DEBUG
0132                             barrel_locations.append(location_string)
0133                             barrel_template_IDs.append(template_ID)
0134                         else :
0135                             location_index = barrel_locations.index(location_string)
0136                             barrel_template_IDs[location_index]=template_ID
0137         else : 
0138             disk, blade, side, panel = line[1], line[2], line[3], line[4]
0139             #endcap ID strings are "disk_blade_side_panel"
0140             disksplit = disk.split('-'); firstdisk=int(disksplit[0]); lastdisk = int(disksplit[1])+1 if len(disksplit)>1 else firstdisk+1
0141             for i in range(firstdisk,lastdisk) :
0142                 disk_string = str(i)+'_'
0143                 bladesplit = blade.split('-'); firstblade=int(bladesplit[0]); lastblade = int(bladesplit[1])+1 if len(bladesplit)>1 else firstblade+1
0144                 for j in range(firstblade,lastblade) :
0145                     blade_string = disk_string+str(j)+'_'
0146                     sidesplit = side.split('-'); firstside=int(sidesplit[0]); lastside = int(sidesplit[1])+1 if len(sidesplit)>1 else firstside+1
0147                     for k in range(firstside,lastside) :
0148                         side_string = blade_string+str(k)+'_'
0149                         panelsplit = panel.split('-'); firstpanel=int(panelsplit[0]); lastpanel = int(panelsplit[1])+1 if len(panelsplit)>1 else firstpanel+1
0150                         for m in range(firstpanel,lastpanel) :
0151                             location_string = side_string+str(m)
0152                             if s==1 :
0153                                 endcap_locations.append(location_string)
0154                                 endcap_template_IDs.append(template_ID)
0155                             else :
0156                                 location_index = endcap_locations.index(location_string)
0157                                 endcap_template_IDs[location_index]=template_ID
0158 #Debug print out assignments
0159 #print 'BARREL ASSIGNMENTS:' #DEBUG
0160 #for i in range(len(barrel_locations)) : #DEBUG
0161 #   tempid = barrel_template_IDs[i] #DEBUG
0162 #   lay, lad, mod = barrel_locations[i].split('_')[0], barrel_locations[i].split('_')[1], barrel_locations[i].split('_')[2] #DEBUG
0163 #   print ' layer %s, ladder %s, module %s will have template ID %d assigned to it'%(lay,lad,mod,tempid) #DEBUG
0164 #print 'ENDCAP ASSIGNMENTS:' #DEBUG
0165 #for i in range(len(endcap_locations)) : #DEBUG
0166 #   tempid = endcap_template_IDs[i] #DEBUG
0167 #   disk, blade, side, panel = endcap_locations[i].split('_')[0], endcap_locations[i].split('_')[1], endcap_locations[i].split('_')[2], endcap_locations[i].split('_')[3] #DEBUG
0168 #   print ' disk %s, blade %s, side %s, panel %s will have template ID %d assigned to it'%(disk,blade,side,panel,tempid) #DEBUG
0169 
0170 from Configuration.StandardSequences.Eras import eras
0171 
0172 process = cms.Process("SiPixelTemplateDBUpload",eras.Phase2)#C2)
0173 process.load("CondCore.CondDB.CondDB_cfi")
0174 process.load("FWCore.MessageService.MessageLogger_cfi")
0175 process.MessageLogger = cms.Service("MessageLogger",
0176                                     destinations = cms.untracked.vstring('SiPixelTemplateDBObjectUploader_Phase2.log'))
0177 
0178 geometry_cff = ''
0179 recoGeometry_cff = ''
0180 tGeometry = options.geometry
0181 if tGeometry == 'T5':
0182     geometry_cff = 'GeometryExtended2023D17_cff'
0183     recoGeometry_cff = 'GeometryExtended2023D17Reco_cff'
0184     LA_value = 0.106
0185     tag = 'SiPixelLorentzAngle_Phase2_T5'
0186 elif tGeometry == 'T6':
0187     geometry_cff = 'GeometryExtendedRun4D35_cff'
0188     recoGeometry_cff = 'GeometryExtendedRun4D35Reco_cff'
0189 elif tGeometry == 'T14':
0190     geometry_cff = 'GeometryExtendedRun4D43_cff'
0191     recoGeometry_cff = 'GeometryExtendedRun4D43Reco_cff'
0192 elif tGeometry == 'T15':
0193     geometry_cff = 'GeometryExtendedRun4D49_cff'
0194     recoGeometry_cff = 'GeometryExtendedRun4D49Reco_cff'
0195 elif tGeometry == 'T21':
0196     geometry_cff = 'GeometryExtendedRun4D92_cff'
0197     recoGeometry_cff = 'GeometryExtendedRun4D92Reco_cff'
0198 elif tGeometry == 'T33':
0199     geometry_cff = 'GeometryExtendedRun4D110_cff'
0200     recoGeometry_cff = 'GeometryExtendedRun4D110Reco_cff'
0201 else:
0202     print("Unknown tracker geometry")
0203     print("What are you doing ?!?!?!?!")
0204     exit(1)
0205 geometry_cff = 'Configuration.Geometry.' + geometry_cff
0206 recoGeometry_cff = 'Configuration.Geometry.' + recoGeometry_cff
0207 process.load(geometry_cff)
0208 process.load(recoGeometry_cff)
0209 
0210 global_tag_name = options.GlobalTag+'_'+tGeometry
0211 
0212 process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")
0213 from Configuration.AlCa.GlobalTag import GlobalTag
0214 process.GlobalTag = GlobalTag(process.GlobalTag, global_tag_name, '')
0215 
0216 template_base = 'SiPixelTemplateDBObject_phase2_'+tGeometry+'_v'+version
0217 if options.Append!=None :
0218     template_base+='_'+options.Append
0219 #output SQLite filename
0220 sqlitefilename = 'sqlite_file:'+template_base+'.db'
0221 
0222 print('\nUploading %s with record SiPixelTemplateDBObjectRcd in file %s\n' % (template_base,sqlitefilename))
0223 
0224 process.source = cms.Source("EmptyIOVSource",
0225                             timetype = cms.string('runnumber'),
0226                             firstValue = cms.uint64(1),
0227                             lastValue = cms.uint64(1),
0228                             interval = cms.uint64(1)
0229                             )
0230 process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))
0231 process.PoolDBOutputService = cms.Service("PoolDBOutputService",
0232                                           DBParameters = cms.PSet(messageLevel = cms.untracked.int32(0),
0233                                                                   authenticationPath = cms.untracked.string('.')
0234                                                                   ),
0235                                           timetype = cms.untracked.string('runnumber'),
0236                                           connect = cms.string(sqlitefilename),
0237                                           toPut = cms.VPSet(cms.PSet(record = cms.string('SiPixelTemplateDBObjectRcd'),
0238                                                                      tag = cms.string(template_base)
0239                                                                      )
0240                                                             )
0241                                           )
0242 process.uploader = cms.EDAnalyzer("SiPixelTemplateDBObjectUploader",
0243                                   siPixelTemplateCalibrations = cms.vstring(template_filenames),
0244                                   theTemplateBaseString = cms.string(template_base),
0245                                   Version = cms.double(3.0),
0246                                   MagField = cms.double(MagFieldValue),
0247                                   detIds = cms.vuint32(1,2), #0 is for all, 1 is Barrel, 2 is EndCap
0248                                   barrelLocations = cms.vstring(barrel_locations),
0249                                   endcapLocations = cms.vstring(endcap_locations),
0250                                   barrelTemplateIds = cms.vuint32(barrel_template_IDs),
0251                                   endcapTemplateIds = cms.vuint32(endcap_template_IDs),
0252                                   useVectorIndices  = cms.untracked.bool(options.useVectorIndices),
0253                                  )
0254 process.myprint = cms.OutputModule("AsciiOutputModule")
0255 process.p = cms.Path(process.uploader)
0256 process.CondDB.connect = sqlitefilename
0257 process.CondDB.DBParameters.messageLevel = 0
0258 process.CondDB.DBParameters.authenticationPath = './'