Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:11

0001 '''
0002 cfg to produce alignment validation plots from JetHT sample 
0003 here doing refit of tracks and vertices using latest alignment 
0004 '''
0005 
0006 # Define the process
0007 import FWCore.ParameterSet.Config as cms
0008 from Alignment.OfflineValidation.TkAlAllInOneTool.defaultInputFiles_cff import filesDefaultData_JetHTRun2018D
0009 process = cms.Process("JetHTAnalyzer")
0010 
0011 # Choose whether to run with Monte Carlo or data settings based on command line argument
0012 import FWCore.ParameterSet.VarParsing as VarParsing
0013 
0014 options = VarParsing.VarParsing()
0015 options.register ('config',
0016                   "", # Default file
0017                   VarParsing.VarParsing.multiplicity.singleton, # singleton or list
0018                   VarParsing.VarParsing.varType.string,         # string, int, or float
0019                   "AllInOne config.")
0020 options.register ('runType',
0021                   "condor", # Default type
0022                   VarParsing.VarParsing.multiplicity.singleton, # singleton or list
0023                   VarParsing.VarParsing.varType.string,         # string, int, or float
0024                   "AllInOne config.")
0025 options.register ('jobNumber',
0026                   -1, # Default value
0027                   VarParsing.VarParsing.multiplicity.singleton, # singleton or list
0028                   VarParsing.VarParsing.varType.int,         # string, int, or float
0029                   "AllInOne config.")
0030 options.parseArguments()
0031 
0032 #Read in AllInOne config in JSON format
0033 import json
0034 import os
0035 import re
0036 
0037 if options.config == "":
0038     configuration = {"validation": {},
0039                      "alignment": {}}
0040 else:
0041     with open(options.config, "r") as configFile:
0042         configuration = json.load(configFile)
0043 
0044 # Read parameters from the configuration file
0045 useMC = configuration["validation"].get("mc", False)
0046 printTriggers = configuration["validation"].get("printTriggers", False)
0047 triggerFilter = str(configuration["validation"].get("triggerFilter", "nothing"))
0048 iovListFile = str(configuration["validation"].get("iovListFile", "nothing"))
0049 iovListList = configuration["validation"].get("iovList", [0,500000])
0050 ptBorders = configuration["validation"].get("profilePtBorders", [3,5,10,20,50,100])
0051 trackCollection = str(configuration["validation"].get("trackCollection", "ALCARECOTkAlMinBias"))
0052 maxEventsToRun = configuration["validation"].get("maxevents", 1)
0053 filesPerJob = configuration["validation"].get("filesPerJob", 5)
0054 runsInFiles = configuration.get("runsInFiles",[])
0055 
0056 # The default global tag is suiteble for the unit test file for data
0057 globalTag = str(configuration["alignment"].get("globaltag", "auto:run2_data"))
0058 
0059 # Alignment conditions can be also loaded from a configuration file instead of database
0060 alignmentFile = str(configuration["validation"].get("TrackerAlignmentRcdFile", "nothing"))
0061 alignmentErrorFile = str(configuration["validation"].get("TrackerAlignmentErrorFile", "nothing"))
0062 
0063 
0064 # If IOV list file is given, read the IOV:s from the file
0065 if not (iovListFile == "nothing" or iovListFile == ""):
0066 
0067     # Expand CMSSW_BASE
0068     iovListFile = iovListFile.replace("CMSSW_BASE", os.environ["CMSSW_BASE"])
0069 
0070     # Read the file
0071     iovListList = []
0072     iovReader = open(iovListFile,"r")
0073     iovContents = iovReader.readlines()
0074 
0075     for line in iovContents:
0076         lineContents = line.split()
0077         iovListList.append(int(lineContents[0]))
0078 
0079     # Add a dummy run to the end of the list to make the last number its own IOV
0080     iovListList.append(iovListList[-1]+100)
0081 
0082 
0083 ###################################################################
0084 # Messages
0085 ###################################################################
0086 process.load("FWCore.MessageService.MessageLogger_cfi")
0087 process.MessageLogger.cerr.FwkReport.reportEvery = 1000
0088 process.MessageLogger.cout.enableStatistics = cms.untracked.bool(True)
0089 
0090 # If maxEvents not specified, only do one event
0091 process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(maxEventsToRun) )
0092 
0093 ###################################################################
0094 # Basic modules
0095 ###################################################################
0096 process.load("RecoVertex.BeamSpotProducer.BeamSpot_cff")
0097 process.load("Configuration.Geometry.GeometryDB_cff")
0098 process.load('Configuration.StandardSequences.Services_cff')
0099 process.load("Configuration.StandardSequences.MagneticField_cff")
0100 
0101 ##Read filenames from given .txt file and define input source
0102 readFiles = []
0103 
0104 if "dataset" in configuration["validation"]:
0105 
0106     # We have defined a CMS dataset
0107     if re.match( r'^/[^/.]+/[^/.]+/[^/.]+$', configuration["validation"]["dataset"] ):
0108 
0109         ##Define a dummy source. This will be overwritten by CRAB
0110         process.source = cms.Source("PoolSource",
0111                                 fileNames = cms.untracked.vstring("dummy.dat"),
0112                                 skipEvents = cms.untracked.uint32(0)
0113                                )
0114       
0115     # We are dealing with a filelist
0116     else:
0117         with open(configuration["validation"]["dataset"], "r") as datafiles:
0118             for fileName in datafiles.readlines():
0119                 readFiles.append(fileName.replace("\n", ""))
0120 
0121         # If we do run number based splitting, only read the files that correspond to the current run number
0122         if len(runsInFiles) > 0:
0123             newFiles = []
0124             for line in readFiles:
0125                 runAndFile = line.split()
0126                 if runsInFiles[options.jobNumber] == runAndFile[0]:
0127                     newFiles.append(runAndFile[1])
0128             readFiles = newFiles
0129 
0130             ##Define input source
0131             process.source = cms.Source("PoolSource",
0132                                 fileNames = cms.untracked.vstring(readFiles),
0133                                 eventsToProcess = cms.untracked.VEventRange("{}:1-{}:max".format(runsInFiles[options.jobNumber], runsInFiles[options.jobNumber]))
0134                                )
0135 
0136         ## If we are not doing run number based splitting but have defined a job number, we have file based splitting. Only analyze the files corresponding to this job number
0137         elif options.jobNumber >= 0:
0138             newFiles = []
0139             numberOfFiles = len(readFiles)
0140             firstIndex = filesPerJob * options.jobNumber
0141             for fileIndex in range(firstIndex, firstIndex+filesPerJob):
0142                 if fileIndex >= numberOfFiles:
0143                     break
0144                 newFiles.append(readFiles[fileIndex])
0145             readFiles = newFiles
0146 
0147             ##Define input source
0148             process.source = cms.Source("PoolSource",
0149                                 fileNames = cms.untracked.vstring(readFiles),
0150                                 skipEvents = cms.untracked.uint32(0)
0151                                )
0152 
0153         ## In the default case we are most likely doing CRAB running. Just use the whole file list. CRAB will handle splitting
0154         else:
0155             ##Define input source
0156             process.source = cms.Source("PoolSource",
0157                                 fileNames = cms.untracked.vstring(readFiles),
0158                                 skipEvents = cms.untracked.uint32(0)
0159                                )
0160 
0161 # If no text file with dataset given, read default file
0162 else:
0163     print(">>>>>>>>>> JetHT_cfg.py: msg%-i: dataset not specified in configuration! Loading default file!")
0164 
0165     if useMC:
0166         print(">>>>>>>>>> JetHT_cfg.py: msg%-i: Default file for 2018 MC from 170-300 pT hat bin.")
0167         process.source = cms.Source("PoolSource",
0168                               fileNames = cms.untracked.vstring('root://xrootd-cms.infn.it//store/mc/RunIIWinter19PFCalibDRPremix/QCD_Pt_170to300_TuneCP5_13TeV_pythia8/ALCARECO/TkAlMinBias-2018Conditions_105X_upgrade2018_realistic_v4-v1/270000/C42688BC-7401-3A41-9008-7CD1CA4B09E1.root')
0169                               )
0170     else:
0171         print(">>>>>>>>>> JetHT_cfg.py: msg%-i: Default file read from 2018D JetHT dataset.")
0172         process.source = cms.Source("PoolSource",
0173                                     fileNames = filesDefaultData_JetHTRun2018D)
0174 
0175 ####################################################################
0176 # Global tag
0177 ####################################################################
0178 process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")
0179 from Configuration.AlCa.GlobalTag import GlobalTag
0180 process.GlobalTag = GlobalTag(process.GlobalTag, globalTag)
0181 
0182 ####################################################################
0183 # Alignment conditions
0184 ####################################################################
0185 
0186 import CalibTracker.Configuration.Common.PoolDBESSource_cfi
0187 
0188 # Determine tracker conditions if defined in the configuration
0189 if "conditions" in configuration["alignment"]:
0190     from CalibTracker.Configuration.Common.PoolDBESSource_cfi import poolDBESSource
0191 
0192     for condition in configuration["alignment"]["conditions"]:
0193         setattr(process, "conditionsIn{}".format(condition), poolDBESSource.clone(
0194              connect = cms.string(str(configuration["alignment"]["conditions"][condition]["connect"])),
0195              toGet = cms.VPSet(
0196                         cms.PSet(
0197                                  record = cms.string(str(condition)),
0198                                  tag = cms.string(str(configuration["alignment"]["conditions"][condition]["tag"]))
0199                         )
0200                      )
0201             )
0202         )
0203 
0204         setattr(process, "prefer_conditionsIn{}".format(condition), cms.ESPrefer("PoolDBESSource", "conditionsIn{}".format(condition)))
0205 
0206 # If the reference to the database is not provided, check if a database file is provided
0207 if not (alignmentFile == "nothing" or alignmentFile == ""):
0208   process.conditionsInTrackerAlignmentRcd = CalibTracker.Configuration.Common.PoolDBESSource_cfi.poolDBESSource.clone(
0209        connect = cms.string("sqlite_file:" + alignmentFile),
0210        toGet = cms.VPSet(cms.PSet(record = cms.string('TrackerAlignmentRcd'),
0211                                  tag = cms.string('Alignments')
0212                                  )
0213                         )
0214       )
0215   process.prefer_conditionsInTrackerAlignmentRcd = cms.ESPrefer("PoolDBESSource", "conditionsInTrackerAlignmentRcd")
0216 
0217 # If the reference to the database is not provided, check if a database file is provided
0218 if not (alignmentErrorFile == "nothing" or alignmentErrorFile == ""):
0219   process.conditionsInTrackerAlignmentErrorExtendedRcd = CalibTracker.Configuration.Common.PoolDBESSource_cfi.poolDBESSource.clone(
0220        connect = cms.string("sqlite_file:" + alignmentErrorFile),
0221        toGet = cms.VPSet(cms.PSet(record = cms.string('TrackerAlignmentErrorExtendedRcd'),
0222                                  tag = cms.string('APEs')
0223                                  )
0224                         )
0225       )
0226   process.prefer_conditionsInTrackerAlignmentErrorExtendedRcd = cms.ESPrefer("PoolDBESSource", "conditionsInTrackerAlignmentErrorExtendedRcd")
0227 
0228 ####################################################################
0229 # Track and primary vertex refit
0230 ####################################################################
0231 
0232 
0233 # Setup track refitter
0234 process.load("RecoTracker.TrackProducer.TrackRefitters_cff")
0235 # remove the following lines if you run on RECO files
0236 process.TrackRefitter.src = trackCollection
0237 process.TrackRefitter.NavigationSchool = ""
0238 
0239 ## PV refit
0240 process.load("TrackingTools.TransientTrack.TransientTrackBuilder_cfi")
0241 
0242 from RecoVertex.PrimaryVertexProducer.OfflinePrimaryVertices_cfi import offlinePrimaryVertices 
0243 process.offlinePrimaryVerticesFromRefittedTrks  = offlinePrimaryVertices.clone()
0244 process.offlinePrimaryVerticesFromRefittedTrks.TrackLabel                                       = cms.InputTag("TrackRefitter") 
0245 process.offlinePrimaryVerticesFromRefittedTrks.vertexCollections.maxDistanceToBeam              = 1
0246 process.offlinePrimaryVerticesFromRefittedTrks.TkFilterParameters.maxNormalizedChi2             = 20
0247 process.offlinePrimaryVerticesFromRefittedTrks.TkFilterParameters.minSiliconLayersWithHits      = 5
0248 process.offlinePrimaryVerticesFromRefittedTrks.TkFilterParameters.maxD0Significance             = 5.0 
0249 process.offlinePrimaryVerticesFromRefittedTrks.TkFilterParameters.minPixelLayersWithHits        = 2   
0250 
0251 ###################################################################
0252 # The trigger filter module
0253 ###################################################################
0254 from HLTrigger.HLTfilters.triggerResultsFilter_cfi import *
0255 if not (triggerFilter == "nothing" or triggerFilter == ""):
0256     triggerPath = triggerFilter
0257     process.HLTFilter = triggerResultsFilter.clone(
0258         triggerConditions = cms.vstring(triggerPath),
0259         hltResults = cms.InputTag( "TriggerResults", "", "HLT" ),
0260         l1tResults = cms.InputTag( "" ),
0261         throw = cms.bool(False)
0262     )
0263 
0264 ###################################################################
0265 # The analysis module
0266 ###################################################################
0267 from Alignment.OfflineValidation.jetHTAnalyzer_cfi import jetHTAnalyzer as _jetHTAnalyzer
0268 process.jetHTAnalyzer = _jetHTAnalyzer.clone(
0269                                        vtxCollection       = "offlinePrimaryVerticesFromRefittedTrks",
0270                                        trackCollection     = "TrackRefitter",
0271                                        triggerResults      = ("TriggerResults","","HLT"),
0272                                        printTriggerTable   = printTriggers,
0273                                        minVertexNdf        = 10.,
0274                                        minVertexMeanWeight = 0.5,
0275                                        profilePtBorders    = ptBorders,
0276                                        iovList             = iovListList)
0277 
0278 jobNumberString = ""
0279 if options.jobNumber >= 0:
0280     jobNumberString = "_{}".format(options.jobNumber)
0281 
0282 outputName = "{}/JetHTAnalysis{}.root".format(configuration.get("output", os.getcwd()), jobNumberString)
0283 if options.runType == "crab":
0284     outputName = "JetHTAnalysis.root"
0285 
0286 process.TFileService = cms.Service("TFileService",
0287                                    fileName = cms.string(outputName),   
0288                                    closeFileFast = cms.untracked.bool(False)
0289                                    )
0290 
0291 
0292 if (triggerFilter == "nothing" or triggerFilter == ""):
0293     process.p = cms.Path(process.offlineBeamSpot                        + 
0294                          process.TrackRefitter                          + 
0295                          process.offlinePrimaryVerticesFromRefittedTrks +
0296                          process.jetHTAnalyzer)
0297 else:
0298     process.p = cms.Path(process.HLTFilter                              +
0299                          process.offlineBeamSpot                        + 
0300                          process.TrackRefitter                          + 
0301                          process.offlinePrimaryVerticesFromRefittedTrks +
0302                          process.jetHTAnalyzer)