Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:07

0001 from fnmatch import fnmatch
0002 import FWCore.ParameterSet.Config as cms
0003 import FWCore.Utilities.FileUtils as FileUtils
0004 import FWCore.ParameterSet.VarParsing as VarParsing
0005 import sys
0006 
0007 from Configuration.StandardSequences.Eras import eras
0008 
0009 ###################################################################
0010 def best_match(rcd):
0011 ###################################################################
0012     '''
0013     find out where to best match the input conditions
0014     '''
0015     print(rcd)
0016     for pattern, string in connection_map:
0017         print(pattern, fnmatch(rcd, pattern))
0018         if fnmatch(rcd, pattern):
0019             return string
0020 
0021 options = VarParsing.VarParsing ()
0022 options.register('maxEvents',
0023                  -1,
0024                  VarParsing.VarParsing.multiplicity.singleton,
0025                  VarParsing.VarParsing.varType.int,
0026                  "number of events to process (\"-1\" for all)")
0027 options.register ('era',
0028                   '2022', # default value
0029                   VarParsing.VarParsing.multiplicity.singleton, # singleton or list
0030                   VarParsing.VarParsing.varType.string,         # string, int, or float
0031                   "CMS running era")
0032 
0033 options.register ('GlobalTag',
0034                   'auto:phase1_2022_realistic', # default value
0035                   VarParsing.VarParsing.multiplicity.singleton,
0036                   VarParsing.VarParsing.varType.string,
0037                   "seed number")
0038 
0039 options.register ('records',
0040                   [],
0041                   VarParsing.VarParsing.multiplicity.list, # singleton or list
0042                   VarParsing.VarParsing.varType.string,          # string, int, or float
0043                   "record:tag names to be used/changed from GT")
0044 
0045 options.register ('external',
0046                   [],
0047                   VarParsing.VarParsing.multiplicity.list, # singleton or list
0048                   VarParsing.VarParsing.varType.string,          # string, int, or float
0049                   "record:fle.db picks the following record from this external file")
0050 
0051 options.register ('myseed',
0052                   '1', # default value
0053                   VarParsing.VarParsing.multiplicity.singleton,
0054                   VarParsing.VarParsing.varType.string,
0055                   "seed number")
0056 
0057 options.register ('myfile',
0058                   '/store/relval/CMSSW_12_4_0_pre4/RelValZEE_14/GEN-SIM-RECO/PU_124X_mcRun3_2021_realistic_v1-v1/2580000/4a1ae43b-f4b3-4ad9-b86e-a7d9f6fc5c40.root',
0059                   VarParsing.VarParsing.multiplicity.singleton,
0060                   VarParsing.VarParsing.varType.string,
0061                   "file name")
0062 
0063 options.register ('FileList',
0064                   '', # default value
0065                   VarParsing.VarParsing.multiplicity.singleton, 
0066                   VarParsing.VarParsing.varType.string,
0067                   "FileList in DAS format")
0068 
0069 options.register ('outputName',
0070                   'default', # default value
0071                   VarParsing.VarParsing.multiplicity.singleton, # singleton or list
0072                   VarParsing.VarParsing.varType.string,         # string, int, or float
0073                   "output file")
0074 
0075 options.parseArguments()
0076 
0077 if(options.FileList):
0078     print("FileList:           ", options.FileList)
0079 else:
0080     print("inputFile:          ", options.myfile)
0081 print("outputFile:         ", "DiElectronVertexValidation_{fname}_{fseed}.root".format(fname = options.outputName,fseed=options.myseed))
0082 print("era:                ", options.era)
0083 print("conditionGT:        ", options.GlobalTag)
0084 print("conditionOverwrite: ", options.records)
0085 print("external conditions:", options.external)
0086 print("max events:         ", options.maxEvents)
0087 
0088 if options.era=='2016':
0089     print("===> running era 2016")
0090     process = cms.Process('Analysis',eras.Run2_2016)
0091 elif options.era=='2017':
0092     print("===> running era 2017")
0093     process = cms.Process('Analysis',eras.Run2_2017)
0094 elif options.era=='2018':
0095     print("===> running era 2018")
0096     process = cms.Process('Analysis',eras.Run2_2018)
0097 elif options.era=='2022':
0098     print("===> running era 2022")
0099     process = cms.Process('Analysis',eras.Run3)
0100 elif options.era=='2023':
0101     print("===> running era 2023")
0102     process = cms.Process('Analysis',eras.Run3_2023)
0103 else:
0104     print("unrecognized era %s" % options.era)
0105     sys.exit(1)
0106 
0107 # import of standard configurations
0108 process.load('Configuration.StandardSequences.Services_cff')
0109 process.load('Configuration.StandardSequences.GeometryRecoDB_cff')
0110 process.load('Configuration.StandardSequences.MagneticField_AutoFromDBCurrent_cff')
0111 process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')
0112 
0113 ###################################################################
0114 # Tell the program where to find the conditons
0115 connection_map = [
0116     ('Tracker*', 'frontier://PromptProd/CMS_CONDITIONS'),
0117     ('SiPixel*', 'frontier://PromptProd/CMS_CONDITIONS'),
0118     ('SiStrip*', 'frontier://PromptProd/CMS_CONDITIONS'),
0119     ('Beam*', 'frontier://PromptProd/CMS_CONDITIONS'),
0120     ]
0121 
0122 if options.external:
0123     connection_map.extend(
0124         (i.split(':')[0], 'sqlite_file:%s' % i.split(':')[1]) for i in options.external
0125         )
0126 
0127 connection_map.sort(key=lambda x: -1*len(x[0]))
0128 
0129 ###################################################################
0130 # creat the map for the GT toGet
0131 records = []
0132 if options.records:
0133     for record in options.records:
0134         rcd, tag = tuple(record.split(':'))
0135         print("control point:",rcd,tag)
0136         if len(rcd)==0:
0137             print("no overriding will occur")
0138             continue
0139         records.append(
0140             cms.PSet(
0141                 record = cms.string(rcd),
0142                 tag    = cms.string(tag),
0143                 connect = cms.string(best_match(rcd))
0144                 )
0145             )
0146 
0147 ###################################################################
0148 # configure the Global Tag
0149 from Configuration.AlCa.GlobalTag import GlobalTag
0150 process.GlobalTag = GlobalTag(process.GlobalTag, options.GlobalTag, '')
0151 process.GlobalTag.toGet = cms.VPSet(*records)
0152 
0153 process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(options.maxEvents) )
0154 
0155 #process.load('FWCore.MessageService.MessageLogger_cfi')
0156 #process.MessageLogger.cerr.FwkReport.reportEvery = 1000
0157 
0158 ###################################################################
0159 # Messages
0160 ###################################################################
0161 process.load('FWCore.MessageService.MessageLogger_cfi')   
0162 process.MessageLogger.cerr.enable = False
0163 process.MessageLogger.TrackRefitter=dict()
0164 process.MessageLogger.GsfTrackRefitter=dict()
0165 process.MessageLogger.PrimaryVertexProducer=dict()
0166 process.MessageLogger.DiElectronVertexValidation=dict()
0167 process.MessageLogger.DiLeptonHelpCounts=dict()
0168 process.MessageLogger.PlotsVsKinematics=dict()
0169 process.MessageLogger.cout = cms.untracked.PSet(
0170     enable = cms.untracked.bool(True),
0171     threshold = cms.untracked.string("INFO"),
0172     default   = cms.untracked.PSet(limit = cms.untracked.int32(0)),                       
0173     FwkReport = cms.untracked.PSet(limit = cms.untracked.int32(-1),
0174                                    reportEvery = cms.untracked.int32(1000)
0175                                    ),                                                      
0176     DiElectronVertexValidation = cms.untracked.PSet( limit = cms.untracked.int32(-1)),
0177     DiLeptonHelpCounts = cms.untracked.PSet( limit = cms.untracked.int32(-1)),
0178     enableStatistics = cms.untracked.bool(True)
0179     )
0180 
0181 ###################################################################
0182 # Source
0183 ###################################################################
0184 if(options.FileList):
0185     print('Loading file list from ASCII file')
0186     filelist = FileUtils.loadListFromFile (options.FileList)
0187     readFiles = cms.untracked.vstring( *filelist)
0188 else:
0189     readFiles = cms.untracked.vstring([options.myfile])
0190 
0191 process.source = cms.Source("PoolSource",
0192                             fileNames = readFiles)
0193 
0194 ###################################################################
0195 # TransientTrack from https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideTransientTracks
0196 ###################################################################
0197 process.load("TrackingTools.TransientTrack.TransientTrackBuilder_cfi")
0198 process.load('TrackPropagation.SteppingHelixPropagator.SteppingHelixPropagatorOpposite_cfi')
0199 process.load('TrackPropagation.SteppingHelixPropagator.SteppingHelixPropagatorAlong_cfi')
0200 process.load('TrackingTools.TrackAssociator.DetIdAssociatorESProducer_cff')
0201 
0202 ####################################################################
0203 # Get the BeamSpot
0204 ####################################################################
0205 process.load("RecoVertex.BeamSpotProducer.BeamSpot_cff")
0206 
0207 ####################################################################
0208 # Track Refitter
0209 ####################################################################
0210 process.load("RecoTracker.TrackProducer.TrackRefitters_cff")
0211 import RecoTracker.TrackProducer.TrackRefitters_cff
0212 process.TrackRefitter = RecoTracker.TrackProducer.TrackRefitter_cfi.TrackRefitter.clone()
0213 process.TrackRefitter.src = "generalTracks"
0214 process.TrackRefitter.TrajectoryInEvent = True
0215 process.TrackRefitter.NavigationSchool = ''
0216 process.TrackRefitter.TTRHBuilder = "WithAngleAndTemplate"
0217 
0218 ####################################################################
0219 # Load  electron configuration files
0220 ####################################################################
0221 process.load("TrackingTools.GsfTracking.GsfElectronFit_cff")
0222 
0223 ####################################################################
0224 # GSF Track Refitter
0225 ####################################################################
0226 process.load("TrackingTools.GsfTracking.fwdGsfElectronPropagator_cff")
0227 process.load("RecoTracker.TrackProducer.GsfTrackRefitter_cff")
0228 process.GsfTrackRefitter = RecoTracker.TrackProducer.GsfTrackRefitter_cff.GsfTrackRefitter.clone()
0229 process.GsfTrackRefitter.src = cms.InputTag('electronGsfTracks')  
0230 process.GsfTrackRefitter.TrajectoryInEvent = True
0231 process.GsfTrackRefitter.AlgorithmName = cms.string('gsf')
0232 #process.GsfTrackRefitter.NavigationSchool = ''
0233 #process.GsfTrackRefitter.TTRHBuilder = "WithAngleAndTemplate"
0234 
0235 ####################################################################
0236 # Sequence
0237 ####################################################################
0238 process.load("RecoLocalTracker.SiPixelRecHits.SiPixelTemplateStoreESProducer_cfi")
0239 process.seqTrackselRefit = cms.Sequence(process.offlineBeamSpot*
0240                                         # in case NavigatioSchool is set !=''
0241                                         #process.MeasurementTrackerEvent*
0242                                         process.TrackRefitter*
0243                                         process.GsfTrackRefitter,
0244                                         cms.Task(process.SiPixelTemplateStoreESProducer))
0245 
0246 ####################################################################
0247 # Re-do vertices
0248 ####################################################################
0249 from RecoVertex.PrimaryVertexProducer.OfflinePrimaryVertices_cfi import offlinePrimaryVertices
0250 process.offlinePrimaryVerticesFromRefittedTrks = offlinePrimaryVertices.clone()
0251 process.offlinePrimaryVerticesFromRefittedTrks.TrackLabel = cms.InputTag("TrackRefitter")
0252 
0253 ####################################################################
0254 # Output file
0255 ####################################################################
0256 process.TFileService = cms.Service("TFileService",fileName=cms.string("DiElectronVertexValidation_"+options.outputName+"_"+options.myseed+".root"))
0257 
0258 # Additional output definition
0259 process.analysis = cms.EDAnalyzer("DiElectronVertexValidation",
0260                                   gsfTracks = cms.InputTag('GsfTrackRefitter'),
0261                                   vertices = cms.InputTag('offlinePrimaryVerticesFromRefittedTrks'))
0262 
0263 ####################################################################
0264 # Path
0265 ####################################################################
0266 process.p = cms.Path(process.seqTrackselRefit                        +
0267                      process.offlinePrimaryVerticesFromRefittedTrks  +
0268                      process.analysis)