Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:38

0001 import FWCore.ParameterSet.Config as cms
0002 import copy
0003 
0004 def CreatePlotEntry(analyzer, discriminatorLabel=None, step=True):
0005     """CreatePlotEntry(analyzer, discriminatorLabel)\n
0006     Creates a PSet with the informations used by TauDQMHistEffProducer\n
0007     where to find the numerator and denominator\n
0008     where to put the new plot and how to name it\n
0009     which variables control"""
0010 
0011     producer = analyzer.TauProducer.pythonValue()[1:-1]
0012     ext = analyzer.ExtensionName.pythonValue()[1:-1]
0013     if discriminatorLabel == None:
0014         num = 'RecoTauV/%s%s_Matched/%sMatched_vs_#PAR#TauVisible'%(producer,ext,producer)
0015         out = 'RecoTauV/%s%s_Matched/PFJetMatchingEff#PAR#'%(producer,ext)
0016     else:
0017         num = 'RecoTauV/%s%s_%s/%s_vs_#PAR#TauVisible'%(producer,ext,discriminatorLabel,discriminatorLabel)
0018         if discriminatorLabel.find('DiscriminationBy') != -1:
0019             hname = discriminatorLabel[(discriminatorLabel.find('DiscriminationBy')+len('DiscriminationBy')):]
0020         else:
0021             hname = discriminatorLabel[(discriminatorLabel.find('Discrimination')+len('Discrimination')):]
0022         out = 'RecoTauV/%s%s_%s/%sEff#PAR#'%(producer,ext,discriminatorLabel,hname)
0023 
0024     den = 'RecoTauV/%s%s_ReferenceCollection/nRef_Taus_vs_#PAR#TauVisible'%(producer,ext)
0025     ret = cms.PSet(
0026         numerator = cms.string(num),
0027         denominator = cms.string(den),
0028         efficiency = cms.string(out),
0029         parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
0030         stepByStep = cms.bool(step)
0031         )
0032     return ret
0033 
0034 def NameVariable(analyzer, discriminatorLabel=None):
0035     """NameVariable(analyzer, discriminatorLabel)\n
0036     returns a string with the name of the pset created by CreatePlotEntry"""
0037     #This part is messy, there is no way to directly link the producer and the name of the variable. There are more exception than rules! IF THE DISCRIMINATOR NAME CHANGES YOU HAVE TO CHANHE IT HERE TOO!
0038     if analyzer.TauProducer.pythonValue()[1:-1] == 'shrinkingConePFTauProducer':
0039         if analyzer.ExtensionName.pythonValue()[1:-1] == 'Tanc':
0040             first='ShrinkingConeTanc'
0041         elif analyzer.ExtensionName.pythonValue()[1:-1] == 'LeadingPion':
0042             first='PFTauHighEfficiencyLeadingPion'
0043         elif analyzer.ExtensionName.pythonValue()[1:-1] == "":
0044             first='PFTauHighEfficiency'
0045         else:
0046             #print 'Case not found check the available cases in Validation/RecoTau/python/ValidationUtils.py -- NameVariable'
0047             first=analyzer.TauProducer.pythonValue()[1:-1]+analyzer.ExtensionName.pythonValue()[1:-1]
0048     elif analyzer.TauProducer.pythonValue()[1:-1] == 'hpsPFTauProducer':
0049         first='HPS'
0050     elif analyzer.TauProducer.pythonValue()[1:-1] == 'hpsTancTaus':
0051         first='HPSTanc'+analyzer.ExtensionName.value()
0052     else:
0053         #print 'Case not found check the available cases in Validation/RecoTau/python/ValidationUtils.py -- NameVariable'
0054         first=analyzer.TauProducer.pythonValue()[1:-1]+analyzer.ExtensionName.pythonValue()[1:-1]
0055     
0056     if discriminatorLabel == None:
0057         last = 'Matching'
0058     else:
0059         if discriminatorLabel.find('DiscriminationBy') != -1:
0060             last = discriminatorLabel[(discriminatorLabel.find('DiscriminationBy')+len('DiscriminationBy')):]
0061             if last.find('TaNCfr') != -1:
0062                 last = last[len('TaNCfr'):]
0063         else:
0064             last = discriminatorLabel[(discriminatorLabel.find('DiscriminationAgainst')+len('DiscriminationAgainst')):]+"Rejection"
0065 
0066     return first+"ID"+last+"Efficiencies"
0067 
0068 def PlotAnalyzer(pset, analyzer):
0069     """PlotAnalyzer(pset, analyzer)\n
0070     fills a PSet that contains all the performance plots for a anlyzer\n
0071     pset is the PSet to fill/add"""
0072 
0073     setattr(pset,NameVariable(analyzer),CreatePlotEntry(analyzer))
0074 
0075     for currentDiscriminator in analyzer.discriminators:
0076         label = currentDiscriminator.discriminator.pythonValue()[1:-1]
0077         step = currentDiscriminator.plotStep.value()
0078         setattr(pset,NameVariable(analyzer,label),CreatePlotEntry(analyzer,label,step))
0079 
0080 class Scanner(object):
0081     """Class to scan a sequence and give a list of analyzer used and a list of their names"""
0082     def __init__(self):
0083         self._analyzerRef = []
0084     def enter(self,visitee):
0085         self._analyzerRef.append(visitee)
0086     def modules(self):
0087         return self._analyzerRef
0088     def leave(self, visitee):
0089         pass
0090 
0091 def DisableQCuts(sequence):
0092    scanner = Scanner()
0093    sequence.visit(scanner)
0094    disabled = cms.PSet(
0095     isolationQualityCuts = cms.PSet(
0096         minTrackHits = cms.uint32(0),
0097         minTrackVertexWeight = cms.double(-1),
0098         minTrackPt = cms.double(0),
0099         maxTrackChi2 = cms.double(9999),
0100         minTrackPixelHits = cms.uint32(0),
0101         minGammaEt = cms.double(0),
0102         maxDeltaZ = cms.double(0.2),
0103         maxTransverseImpactParameter = cms.double(9999)
0104         ),
0105     pvFindingAlgo = cms.string('highestWeightForLeadTrack'),
0106     primaryVertexSrc = cms.InputTag("offlinePrimaryVertices"),
0107     signalQualityCuts = cms.PSet(
0108         minTrackHits = cms.uint32(0),
0109         minTrackVertexWeight = cms.double(-1),
0110         minTrackPt = cms.double(0),
0111         maxTrackChi2 = cms.double(9999),
0112         minTrackPixelHits = cms.uint32(0),
0113         minGammaEt = cms.double(0),
0114         maxDeltaZ = cms.double(0.2),
0115         maxTransverseImpactParameter = cms.double(9999)
0116         )
0117     )
0118    for module in scanner.modules():
0119       if hasattr(module,'qualityCuts'):
0120          setattr(module,'qualityCuts',disabled)
0121 
0122 
0123 def SetPlotSequence(sequence):
0124     """SetSequence(seqence)\n
0125     This Function return a PSet of the sequence given to be used by TauDQMHistEffProducer"""
0126     pset = cms.PSet()
0127     scanner = Scanner()
0128     sequence.visit(scanner)
0129     for analyzer in scanner.modules():#The first one is the sequence itself
0130         PlotAnalyzer(pset, analyzer)
0131     return pset
0132 
0133 def SpawnPSet(lArgument, subPset):
0134     """SpawnPSet(lArgument, subPset) --> cms.PSet\n
0135     lArgument is a list containing a list of three strings/values:\n
0136            1-name to give to the spawned pset\n
0137            2-variable(s) to be changed\n
0138            3-value(s) of the variable(s): SAME LENGTH OF 2-!\n
0139            Supported types: int string float(converted to double)"""
0140     ret = cms.PSet()
0141     for spawn in lArgument:
0142         if len(spawn) != 3:
0143             print("ERROR! SpawnPSet uses argument of three data\n")
0144             print(self.__doc__)
0145             return None
0146         if len(spawn[1]) != len(spawn[2]):
0147             print("ERROR! Lists of arguments to replace must have the same length")
0148             print(self.__doc__)
0149             return None
0150         spawnArg = copy.deepcopy(subPset)
0151         for par, val in zip(spawn[1],spawn[2]):
0152             if isinstance(val, str) :
0153                 setattr(spawnArg,par,cms.string(val))
0154             elif isinstance(val, int) :
0155                 setattr(spawnArg,par,cms.int32(val))
0156             elif isinstance(val, float) :
0157                 setattr(spawnArg,par,cms.double(val))
0158         setattr(ret,spawn[0],spawnArg)
0159     return ret
0160 
0161 def SetpByStep(analyzer, plotPset, useOnly):
0162     """SetpByStep(analyzer, plotPset) --> PSet\n
0163      This function produces the parameter set stepBystep for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
0164     standardEfficiencyOverlay = cms.PSet(
0165         parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
0166         title = cms.string('TauId step by step efficiencies'),
0167         xAxis = cms.string('#PAR#'),
0168         yAxis = cms.string('efficiency'),
0169         legend = cms.string('efficiency_overlay'),
0170         labels = cms.vstring('pt', 'eta')
0171         )
0172     ret = cms.PSet(
0173         standardEfficiencyOverlay,
0174         plots = cms.VPSet()
0175         )
0176     producer = analyzer.TauProducer.pythonValue()[1:-1]
0177     ext = analyzer.ExtensionName.pythonValue()[1:-1]
0178     keyword = producer + ext + "_"
0179     counter = 0
0180     tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
0181     hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
0182     for parName in plotPset.parameterNames_():
0183         isToBePlotted = getattr(plotPset,parName).stepByStep.value()
0184         if isToBePlotted:
0185             effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
0186             discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
0187             if useOnly == 'tanc':
0188                 useThis = discriminator in tancDisc
0189             elif useOnly == 'hps':
0190                 useThis = discriminator in hpsDisc
0191             else :
0192                 useThis = True
0193             if (effplot.find(keyword) != -1) and useThis:
0194                 monEl = '#PROCESSDIR#/'+effplot
0195                 counter = counter + 1
0196                 drawOpt = 'eff_overlay0%s'%(counter)
0197                 psetName = effplot[effplot.rfind('/')+1:-8]
0198                 ret.plots.append(cms.PSet(
0199                     dqmMonitorElements = cms.vstring(monEl),
0200                     process = cms.string('test'),
0201                     drawOptionEntry = cms.string(drawOpt),
0202                     legendEntry = cms.string(psetName)
0203                     ))
0204     return ret
0205 
0206 def SpawnDrawJobs(analyzer, plotPset, useOnly=None):
0207     """SpwnDrawJobs(analyzer, plotPset) --> cms.PSet\n
0208     This function produces the parameter set drawJobs for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
0209     standardEfficiencyParameters = cms.PSet(
0210         parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
0211         xAxis = cms.string('#PAR#'),
0212         yAxis = cms.string('efficiency'),
0213         legend = cms.string('efficiency'),
0214         labels = cms.vstring('pt', 'eta'),
0215         drawOptionSet = cms.string('efficiency')
0216         )
0217     ret = cms.PSet()
0218     tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
0219     hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
0220     producer = analyzer.TauProducer.pythonValue()[1:-1]
0221     ext = analyzer.ExtensionName.pythonValue()[1:-1]
0222     keyword = producer + ext + "_"
0223     for parName in plotPset.parameterNames_():
0224         effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
0225         discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
0226         if useOnly == 'tanc':
0227             useThis = discriminator in tancDisc
0228         elif useOnly == 'hps':
0229             useThis = discriminator in hpsDisc
0230         else :
0231             useThis = True
0232         if (effplot.find(keyword) != -1) and useThis:
0233             monEl = '#PROCESSDIR#/'+effplot
0234             psetName = effplot[effplot.rfind('/')+1:-5]
0235             psetVal = cms.PSet(
0236                 standardEfficiencyParameters,
0237                 plots = cms.PSet(
0238                     dqmMonitorElements = cms.vstring(monEl),
0239                     processes = cms.vstring('test', 'reference')
0240                     )
0241             )
0242             setattr(ret,psetName,psetVal)
0243     setattr(ret,'TauIdEffStepByStep',SetpByStep(analyzer, plotPset,useOnly))
0244     return ret #control if it's ok