CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Validation/RecoTau/python/ValidationUtils.py

Go to the documentation of this file.
00001 import FWCore.ParameterSet.Config as cms
00002 import copy
00003 
00004 def CreatePlotEntry(analyzer, discriminatorLabel=None, step=True):
00005     """CreatePlotEntry(analyzer, discriminatorLabel)\n
00006     Creates a PSet with the informations used by TauDQMHistEffProducer\n
00007     where to find the numerator and denominator\n
00008     where to put the new plot and how to name it\n
00009     which variables control"""
00010 
00011     producer = analyzer.TauProducer.pythonValue()[1:-1]
00012     ext = analyzer.ExtensionName.pythonValue()[1:-1]
00013     if discriminatorLabel == None:
00014         num = 'RecoTauV/%s%s_Matched/%sMatched_vs_#PAR#TauVisible'%(producer,ext,producer)
00015         #out = 'RecoTauV/%s%s_Matched/PFJetMatchingEff#PAR#'%(producer,ext)
00016         if producer.find('caloReco') != -1:
00017             out = 'RecoTauV/%s%s_Matched/CaloJetMatchingEff#PAR#'%(producer,ext)
00018         else:
00019             out = 'RecoTauV/%s%s_Matched/PFJetMatchingEff#PAR#'%(producer,ext)
00020     else:
00021         num = 'RecoTauV/%s%s_%s/%s_vs_#PAR#TauVisible'%(producer,ext,discriminatorLabel,discriminatorLabel)
00022         if discriminatorLabel.find('DiscriminationBy') != -1:
00023             hname = discriminatorLabel[(discriminatorLabel.find('DiscriminationBy')+len('DiscriminationBy')):]
00024         else:
00025             hname = discriminatorLabel[(discriminatorLabel.find('Discrimination')+len('Discrimination')):]
00026         out = 'RecoTauV/%s%s_%s/%sEff#PAR#'%(producer,ext,discriminatorLabel,hname)
00027 
00028     den = 'RecoTauV/%s%s_ReferenceCollection/nRef_Taus_vs_#PAR#TauVisible'%(producer,ext)
00029     ret = cms.PSet(
00030         numerator = cms.string(num),
00031         denominator = cms.string(den),
00032         efficiency = cms.string(out),
00033         parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
00034         stepByStep = cms.bool(step)
00035         )
00036     return ret
00037 
00038 def NameVariable(analyzer, discriminatorLabel=None):
00039     """NameVariable(analyzer, discriminatorLabel)\n
00040     returns a string with the name of the pset created by CreatePlotEntry"""
00041     #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!
00042     if analyzer.TauProducer.pythonValue()[1:-1] == 'shrinkingConePFTauProducer':
00043         if analyzer.ExtensionName.pythonValue()[1:-1] == 'Tanc':
00044             first='ShrinkingConeTanc'
00045         elif analyzer.ExtensionName.pythonValue()[1:-1] == 'LeadingPion':
00046             first='PFTauHighEfficiencyLeadingPion'
00047         elif analyzer.ExtensionName.pythonValue()[1:-1] == "":
00048             first='PFTauHighEfficiency'
00049         else:
00050             #print 'Case not found check the available cases in Validation/RecoTau/python/ValidationUtils.py -- NameVariable'
00051             first=analyzer.TauProducer.pythonValue()[1:-1]+analyzer.ExtensionName.pythonValue()[1:-1]
00052     elif analyzer.TauProducer.pythonValue()[1:-1] == 'hpsPFTauProducer':
00053         first='HPS'
00054     elif analyzer.TauProducer.pythonValue()[1:-1] == 'hpsTancTaus':
00055         first='HPSTanc'+analyzer.ExtensionName.value()
00056     elif analyzer.TauProducer.pythonValue()[1:-1] == 'caloRecoTauProducer':
00057         first='CaloTau'
00058     else:
00059         #print 'Case not found check the available cases in Validation/RecoTau/python/ValidationUtils.py -- NameVariable'
00060         first=analyzer.TauProducer.pythonValue()[1:-1]+analyzer.ExtensionName.pythonValue()[1:-1]
00061     
00062     if discriminatorLabel == None:
00063         last = 'Matching'
00064     else:
00065         if discriminatorLabel.find('DiscriminationBy') != -1:
00066             last = discriminatorLabel[(discriminatorLabel.find('DiscriminationBy')+len('DiscriminationBy')):]
00067             if last.find('TaNCfr') != -1:
00068                 last = last[len('TaNCfr'):]
00069         else:
00070             last = discriminatorLabel[(discriminatorLabel.find('DiscriminationAgainst')+len('DiscriminationAgainst')):]+"Rejection"
00071 
00072     return first+"ID"+last+"Efficiencies"
00073 
00074 def PlotAnalyzer(pset, analyzer):
00075     """PlotAnalyzer(pset, analyzer)\n
00076     fills a PSet that contains all the performance plots for a anlyzer\n
00077     pset is the PSet to fill/add"""
00078 
00079     setattr(pset,NameVariable(analyzer),CreatePlotEntry(analyzer))
00080 
00081     for currentDiscriminator in analyzer.discriminators:
00082         label = currentDiscriminator.discriminator.pythonValue()[1:-1]
00083         step = currentDiscriminator.plotStep.value()
00084         setattr(pset,NameVariable(analyzer,label),CreatePlotEntry(analyzer,label,step))
00085 
00086 class Scanner(object):
00087     """Class to scan a sequence and give a list of analyzer used and a list of their names"""
00088     def __init__(self):
00089         self._analyzerRef = []
00090     def enter(self,visitee):
00091         self._analyzerRef.append(visitee)
00092     def modules(self):
00093         return self._analyzerRef
00094     def leave(self, visitee):
00095         pass
00096 
00097 def DisableQCuts(sequence):
00098    scanner = Scanner()
00099    sequence.visit(scanner)
00100    disabled = cms.PSet(
00101     isolationQualityCuts = cms.PSet(
00102         minTrackHits = cms.uint32(0),
00103         minTrackVertexWeight = cms.double(-1),
00104         minTrackPt = cms.double(0),
00105         maxTrackChi2 = cms.double(9999),
00106         minTrackPixelHits = cms.uint32(0),
00107         minGammaEt = cms.double(0),
00108         maxDeltaZ = cms.double(0.2),
00109         maxTransverseImpactParameter = cms.double(9999)
00110         ),
00111     pvFindingAlgo = cms.string('highestWeightForLeadTrack'),
00112     primaryVertexSrc = cms.InputTag("offlinePrimaryVertices"),
00113     signalQualityCuts = cms.PSet(
00114         minTrackHits = cms.uint32(0),
00115         minTrackVertexWeight = cms.double(-1),
00116         minTrackPt = cms.double(0),
00117         maxTrackChi2 = cms.double(9999),
00118         minTrackPixelHits = cms.uint32(0),
00119         minGammaEt = cms.double(0),
00120         maxDeltaZ = cms.double(0.2),
00121         maxTransverseImpactParameter = cms.double(9999)
00122         )
00123     )
00124    for module in scanner.modules():
00125       if hasattr(module,'qualityCuts'):
00126          setattr(module,'qualityCuts',disabled)
00127 
00128 
00129 def SetPlotSequence(sequence):
00130     """SetSequence(seqence)\n
00131     This Function return a PSet of the sequence given to be used by TauDQMHistEffProducer"""
00132     pset = cms.PSet()
00133     scanner = Scanner()
00134     sequence.visit(scanner)
00135     for analyzer in scanner.modules():#The first one is the sequence itself
00136         if type(analyzer) is cms.EDAnalyzer:
00137             PlotAnalyzer(pset, analyzer)
00138     return pset
00139 
00140 def SpawnPSet(lArgument, subPset):
00141     """SpawnPSet(lArgument, subPset) --> cms.PSet\n
00142     lArgument is a list containing a list of three strings/values:\n
00143            1-name to give to the spawned pset\n
00144            2-variable(s) to be changed\n
00145            3-value(s) of the variable(s): SAME LENGTH OF 2-!\n
00146            Supported types: int string float(converted to double)"""
00147     ret = cms.PSet()
00148     for spawn in lArgument:
00149         if len(spawn) != 3:
00150             print "ERROR! SpawnPSet uses argument of three data\n"
00151             print self.__doc__
00152             return None
00153         if len(spawn[1]) != len(spawn[2]):
00154             print "ERROR! Lists of arguments to replace must have the same length"
00155             print self.__doc__
00156             return None
00157         spawnArg = copy.deepcopy(subPset)
00158         for par, val in zip(spawn[1],spawn[2]):
00159             if type(val) is str :
00160                 setattr(spawnArg,par,cms.string(val))
00161             elif type(val) is int :
00162                 setattr(spawnArg,par,cms.int32(val))
00163             elif type(val) is float :
00164                 setattr(spawnArg,par,cms.double(val))
00165         setattr(ret,spawn[0],spawnArg)
00166     return ret
00167 
00168 def SetpByStep(analyzer, plotPset, useOnly):
00169     """SetpByStep(analyzer, plotPset) --> PSet\n
00170      This function produces the parameter set stepBystep for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
00171     standardEfficiencyOverlay = cms.PSet(
00172         parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
00173         title = cms.string('TauId step by step efficiencies'),
00174         xAxis = cms.string('#PAR#'),
00175         yAxis = cms.string('efficiency'),
00176         legend = cms.string('efficiency_overlay'),
00177         labels = cms.vstring('pt', 'eta')
00178         )
00179     ret = cms.PSet(
00180         standardEfficiencyOverlay,
00181         plots = cms.VPSet()
00182         )
00183     producer = analyzer.TauProducer.pythonValue()[1:-1]
00184     ext = analyzer.ExtensionName.pythonValue()[1:-1]
00185     keyword = producer + ext + "_"
00186     counter = 0
00187     tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
00188     hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
00189     for parName in plotPset.parameterNames_():
00190         isToBePlotted = getattr(plotPset,parName).stepByStep.value()
00191         if isToBePlotted:
00192             effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
00193             discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
00194             if useOnly == 'tanc':
00195                 useThis = discriminator in tancDisc
00196             elif useOnly == 'hps':
00197                 useThis = discriminator in hpsDisc
00198             else :
00199                 useThis = True
00200             if (effplot.find(keyword) != -1) and useThis:
00201                 monEl = '#PROCESSDIR#/'+effplot
00202                 counter = counter + 1
00203                 drawOpt = 'eff_overlay0%s'%(counter)
00204                 psetName = effplot[effplot.rfind('/')+1:-8]
00205                 ret.plots.append(cms.PSet(
00206                     dqmMonitorElements = cms.vstring(monEl),
00207                     process = cms.string('test'),
00208                     drawOptionEntry = cms.string(drawOpt),
00209                     legendEntry = cms.string(psetName)
00210                     ))
00211     return ret
00212 
00213 def SpawnDrawJobs(analyzer, plotPset, useOnly=None):
00214     """SpwnDrawJobs(analyzer, plotPset) --> cms.PSet\n
00215     This function produces the parameter set drawJobs for the EDAnalyzer TauDQMHistPlotter starting from the PSet produced for TauDQMHistEffProducer and the analyzer to plot"""
00216     standardEfficiencyParameters = cms.PSet(
00217         parameter = cms.vstring('pt', 'eta', 'phi', 'pileup'),
00218         xAxis = cms.string('#PAR#'),
00219         yAxis = cms.string('efficiency'),
00220         legend = cms.string('efficiency'),
00221         labels = cms.vstring('pt', 'eta'),
00222         drawOptionSet = cms.string('efficiency')
00223         )
00224     ret = cms.PSet()
00225     tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
00226     hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
00227     producer = analyzer.TauProducer.pythonValue()[1:-1]
00228     ext = analyzer.ExtensionName.pythonValue()[1:-1]
00229     keyword = producer + ext + "_"
00230     for parName in plotPset.parameterNames_():
00231         effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
00232         discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
00233         if useOnly == 'tanc':
00234             useThis = discriminator in tancDisc
00235         elif useOnly == 'hps':
00236             useThis = discriminator in hpsDisc
00237         else :
00238             useThis = True
00239         if (effplot.find(keyword) != -1) and useThis:
00240             monEl = '#PROCESSDIR#/'+effplot
00241             psetName = effplot[effplot.rfind('/')+1:-5]
00242             psetVal = cms.PSet(
00243                 standardEfficiencyParameters,
00244                 plots = cms.PSet(
00245                     dqmMonitorElements = cms.vstring(monEl),
00246                     processes = cms.vstring('test', 'reference')
00247                     )
00248             )
00249             setattr(ret,psetName,psetVal)
00250     setattr(ret,'TauIdEffStepByStep',SetpByStep(analyzer, plotPset,useOnly))
00251     return ret #control if it's ok