CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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 DQMHistEffProducer\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', 'energy'),
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 SetPlotSequence(sequence):
00098     """SetSequence(seqence)\n
00099     This Function return a PSet of the sequence given to be used by DQMHistEffProducer"""
00100     pset = cms.PSet()
00101     scanner = Scanner()
00102     sequence.visit(scanner)
00103     for analyzer in scanner.modules():#The first one is the sequence itself
00104         if type(analyzer) is cms.EDAnalyzer:
00105             PlotAnalyzer(pset, analyzer)
00106     return pset
00107 
00108 def SpawnPSet(lArgument, subPset):
00109     """SpawnPSet(lArgument, subPset) --> cms.PSet\n
00110     lArgument is a list containing a list of three strings/values:\n
00111            1-name to give to the spawned pset\n
00112            2-variable(s) to be changed\n
00113            3-value(s) of the variable(s): SAME LENGTH OF 2-!\n
00114            Supported types: int string float(converted to double)"""
00115     ret = cms.PSet()
00116     for spawn in lArgument:
00117         if len(spawn) != 3:
00118             print "ERROR! SpawnPSet uses argument of three data\n"
00119             print self.__doc__
00120             return None
00121         if len(spawn[1]) != len(spawn[2]):
00122             print "ERROR! Lists of arguments to replace must have the same length"
00123             print self.__doc__
00124             return None
00125         spawnArg = copy.deepcopy(subPset)
00126         for par, val in zip(spawn[1],spawn[2]):
00127             if type(val) is str :
00128                 setattr(spawnArg,par,cms.string(val))
00129             elif type(val) is int :
00130                 setattr(spawnArg,par,cms.int32(val))
00131             elif type(val) is float :
00132                 setattr(spawnArg,par,cms.double(val))
00133         setattr(ret,spawn[0],spawnArg)
00134     return ret
00135 
00136 def SetpByStep(analyzer, plotPset, useOnly):
00137     """SetpByStep(analyzer, plotPset) --> PSet\n
00138      This function produces the parameter set stepBystep for the EDAnalyzer DQMHistPlotter starting from the PSet produced for DQMHistEffProducer and the analyzer to plot"""
00139     standardEfficiencyOverlay = cms.PSet(
00140         parameter = cms.vstring('pt', 'eta', 'phi', 'energy'),
00141         title = cms.string('TauId step by step efficiencies'),
00142         xAxis = cms.string('#PAR#'),
00143         yAxis = cms.string('efficiency'),
00144         legend = cms.string('efficiency_overlay'),
00145         labels = cms.vstring('pt', 'eta')
00146         )
00147     ret = cms.PSet(
00148         standardEfficiencyOverlay,
00149         plots = cms.VPSet()
00150         )
00151     producer = analyzer.TauProducer.pythonValue()[1:-1]
00152     ext = analyzer.ExtensionName.pythonValue()[1:-1]
00153     keyword = producer + ext + "_"
00154     counter = 0
00155     tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
00156     hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
00157     for parName in plotPset.parameterNames_():
00158         isToBePlotted = getattr(plotPset,parName).stepByStep.value()
00159         if isToBePlotted:
00160             effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
00161             discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
00162             if useOnly == 'tanc':
00163                 useThis = discriminator in tancDisc
00164             elif useOnly == 'hps':
00165                 useThis = discriminator in hpsDisc
00166             else :
00167                 useThis = True
00168             if (effplot.find(keyword) != -1) and useThis:
00169                 monEl = '#PROCESSDIR#/'+effplot
00170                 counter = counter + 1
00171                 drawOpt = 'eff_overlay0%s'%(counter)
00172                 psetName = effplot[effplot.rfind('/')+1:-8]
00173                 ret.plots.append(cms.PSet(
00174                     dqmMonitorElements = cms.vstring(monEl),
00175                     process = cms.string('test'),
00176                     drawOptionEntry = cms.string(drawOpt),
00177                     legendEntry = cms.string(psetName)
00178                     ))
00179     return ret
00180 
00181 def SpawnDrawJobs(analyzer, plotPset, useOnly=None):
00182     """SpwnDrawJobs(analyzer, plotPset) --> cms.PSet\n
00183     This function produces the parameter set drawJobs for the EDAnalyzer DQMHistPlotter starting from the PSet produced for DQMHistEffProducer and the analyzer to plot"""
00184     standardEfficiencyParameters = cms.PSet(
00185         parameter = cms.vstring('pt', 'eta', 'phi', 'energy'),
00186         xAxis = cms.string('#PAR#'),
00187         yAxis = cms.string('efficiency'),
00188         legend = cms.string('efficiency'),
00189         labels = cms.vstring('pt', 'eta'),
00190         drawOptionSet = cms.string('efficiency')
00191         )
00192     ret = cms.PSet()
00193     tancDisc = ['Matching','DecayModeSelection','LeadingPionPtCut','LeadingTrackFinding','LeadingTrackPtCut','Tanc','TancVLoose','TancLoose','TancMedium','TancRaw','TancTight','AgainstElectron','AgainstMuon']
00194     hpsDisc = ['Matching','DecayModeSelection','LooseIsolation','MediumIsolation','TightIsolation']
00195     producer = analyzer.TauProducer.pythonValue()[1:-1]
00196     ext = analyzer.ExtensionName.pythonValue()[1:-1]
00197     keyword = producer + ext + "_"
00198     for parName in plotPset.parameterNames_():
00199         effplot = getattr(plotPset,parName).efficiency.pythonValue()[1:-1]
00200         discriminator = parName[parName.find('ID')+len('ID'):-len('Efficiencies')]
00201         if useOnly == 'tanc':
00202             useThis = discriminator in tancDisc
00203         elif useOnly == 'hps':
00204             useThis = discriminator in hpsDisc
00205         else :
00206             useThis = True
00207         if (effplot.find(keyword) != -1) and useThis:
00208             monEl = '#PROCESSDIR#/'+effplot
00209             psetName = effplot[effplot.rfind('/')+1:-5]
00210             psetVal = cms.PSet(
00211                 standardEfficiencyParameters,
00212                 plots = cms.PSet(
00213                     dqmMonitorElements = cms.vstring(monEl),
00214                     processes = cms.vstring('test', 'reference')
00215                     )
00216             )
00217             setattr(ret,psetName,psetVal)
00218     setattr(ret,'TauIdEffStepByStep',SetpByStep(analyzer, plotPset,useOnly))
00219     return ret #control if it's ok