def legend( posLeg ='CR' ): """.. function:: legend( pos='CR' ) -> leg Return a TLegend object positioned at 'pos'. The values admitted for 'pos' are:: UL: Up-Left corner UR: Up-Right corner DL: Down-Left corner DR: Down-Right corner CR: Center-Right :param pos: where positioning the legend (default: ``CR``) :type pos: string :return: the object legend :rtype: ROOT.TLegend """ import ROOT if posLeg == 'UL': leg = ROOT.TLegend(0.2,0.69,0.4,0.79) elif posLeg == 'UR': leg = ROOT.TLegend(0.5,0.69,0.7,0.79) elif posLeg == 'DL': leg = ROOT.TLegend(0.2,0.41,0.4,0.51) elif posLeg == 'DR': leg = ROOT.TLegend(0.5,0.41,0.7,0.51) elif posLeg == 'CR': leg = ROOT.TLegend(0.5,0.29,0.70,0.39) else: mess = "Position '%s' not defined" % posLeg printError( legend.__module__+'.'+legeng.__name__, message, AttributeError ) return leg
def paveText(title, posText='CR'): """.. function:: paveText( pos='CR' ) -> pave Return a TLegend object positioned at 'pos'. The values admitted for 'pos' are:: UL: Up-Left corner UR: Up-Right corner DL: Down-Left corner DR: Down-Right corner CL: Center-Left CR: Center-Right OG: Over the graphic :param pos: where positioning the legend (default: ``CR``) :type pos: string :return: the object pave text :rtype: ROOT.TPaveText """ import ROOT if posText == 'UL': text = ROOT.TPaveText(0.25, 0.8, 0.45, 0.9, "NDC") elif posText == 'UR': text = ROOT.TPaveText(0.55, 0.8, 0.75, 0.9, "NDC") elif posText == 'DL': text = ROOT.TPaveText(0.25, 0.1, 0.45, 0.3, "NDC") elif posText == 'DR': text = ROOT.TPaveText(0.55, 0.1, 0.75, 0.3, "NDC") elif posText == 'CL': text = ROOT.TPaveText(0.25, 0.4, 0.45, 0.6, "NDC") elif posText == 'CR': text = ROOT.TPaveText(0.55, 0.4, 0.75, 0.6, "NDC") elif posText == 'OG': text = ROOT.TPaveText(0.45, 0.93, 0.65, 0.98, "NDC") else: mess = "Position '%s' not defined" % posLeg printError(legend.__module__ + '.' + legeng.__name__, message, AttributeError) text.AddText(title) text.SetBorderSize(0) text.SetFillColor(0) text.SetTextSize(0.04) text.SetFillStyle(4000) return text
def paveText( title, posText='CR' ): """.. function:: paveText( pos='CR' ) -> pave Return a TLegend object positioned at 'pos'. The values admitted for 'pos' are:: UL: Up-Left corner UR: Up-Right corner DL: Down-Left corner DR: Down-Right corner CL: Center-Left CR: Center-Right OG: Over the graphic :param pos: where positioning the legend (default: ``CR``) :type pos: string :return: the object pave text :rtype: ROOT.TPaveText """ import ROOT if posText == 'UL': text = ROOT.TPaveText(0.25,0.8,0.45,0.9,"NDC") elif posText == 'UR': text = ROOT.TPaveText(0.55,0.8,0.75,0.9,"NDC") elif posText == 'DL': text = ROOT.TPaveText(0.25,0.1,0.45,0.3,"NDC") elif posText == 'DR': text = ROOT.TPaveText(0.55,0.1,0.75,0.3,"NDC") elif posText == 'CL': text = ROOT.TPaveText(0.25,0.4,0.45,0.6,"NDC") elif posText == 'CR': text = ROOT.TPaveText(0.55,0.4,0.75,0.6,"NDC") elif posText == 'OG': text = ROOT.TPaveText(0.45,0.93,0.65,0.98,"NDC") else: mess = "Position '%s' not defined" % posLeg printError( legend.__module__+'.'+legeng.__name__, message, AttributeError ) text.AddText( title ) text.SetBorderSize(0) text.SetFillColor(0) text.SetTextSize(0.04) text.SetFillStyle(4000) return text
def setkeywords( keywords, keyw ): """.. function setkeywords( keyword, keyw ) -> keyw :param keywords: dictionary containing the attributes and its values :type keywords: dict :param keyw: instance of auxK class :type keyw: auxK :return: an instance of auxK with its attributes modified with the ``keywords`` values :rtype: auxK :raise KeyError: If ``keywords`` contains a key which is not defined as attribute of ``auxK`` """ #-- Sanity check VALID_KEYWORDS = [ _name for _name in dir( keyw ) if _name.find('__') == -1 ] for name, value in keywords.iteritems(): if not name in VALID_KEYWORDS: message = "Invalid keyword argument '%s'\nValid keywords are '%s'" % (name, str(VALID_KEYWORDS)) printError( setkeywords.__module__+'.'+setkeywords.__name__, message, KeyError ) setattr( keyw, name, value ) return keyw
def setkeywords(keywords, keyw): """.. function setkeywords( keyword, keyw ) -> keyw :param keywords: dictionary containing the attributes and its values :type keywords: dict :param keyw: instance of auxK class :type keyw: auxK :return: an instance of auxK with its attributes modified with the ``keywords`` values :rtype: auxK :raise KeyError: If ``keywords`` contains a key which is not defined as attribute of ``auxK`` """ #-- Sanity check VALID_KEYWORDS = [_name for _name in dir(keyw) if _name.find('__') == -1] for name, value in keywords.iteritems(): if not name in VALID_KEYWORDS: message = "Invalid keyword argument '%s'\nValid keywords are '%s'" % ( name, str(VALID_KEYWORDS)) printError(setkeywords.__module__ + '.' + setkeywords.__name__, message, KeyError) setattr(keyw, name, value) return keyw
def legend(posLeg='CR'): """.. function:: legend( pos='CR' ) -> leg Return a TLegend object positioned at 'pos'. The values admitted for 'pos' are:: UL: Up-Left corner UR: Up-Right corner DL: Down-Left corner DR: Down-Right corner CR: Center-Right :param pos: where positioning the legend (default: ``CR``) :type pos: string :return: the object legend :rtype: ROOT.TLegend """ import ROOT if posLeg == 'UL': leg = ROOT.TLegend(0.2, 0.69, 0.4, 0.79) elif posLeg == 'UR': leg = ROOT.TLegend(0.5, 0.69, 0.7, 0.79) elif posLeg == 'DL': leg = ROOT.TLegend(0.2, 0.41, 0.4, 0.51) elif posLeg == 'DR': leg = ROOT.TLegend(0.5, 0.41, 0.7, 0.51) elif posLeg == 'CR': leg = ROOT.TLegend(0.5, 0.29, 0.70, 0.39) else: mess = "Position '%s' not defined" % posLeg printError(legend.__module__ + '.' + legeng.__name__, message, AttributeError) return leg
def diff2DMaps(refT, otherT, varX, varY, Lumi, **keywords): """.. function:: diff2DMaps( tnpRef, tnpOther, varX, varY, Lumi\[,title=thetitle, outputformat=format] ) Differences maps from 2 datasets:: eff_ref-eff_other Comparing |eff_ref-eff_other|/eff_ref ComparingRelative --> DEPRECATED The two files must be have the same binning. :param RefT: tuple of pytnp instance for the efficiency of reference and the data name :type RefT: (pytnp,string) :param otherT: tuple pytnp instance for the second efficiency to compare and the data name :type otherT: (pytnp,string) :param varX: name of the binned variable for the x-axis :type varX: string :param varY: name of the binned variable for the y-axis :type varY: string :param Lumi: luminosity :param Lumi: string :keyword thetitle: title to appear in the canvas :type thetitle: string :keyword format: ouput format (eps, root, png, ...) :type format: string :raise RuntimeError: the first two arguments introduced are not tuples (pytnp object, dataname string) :raise UserWarning: some of the variables introduced are not binned variables :raise AttributeError: dataname not found in the root file :raise NameError: the object introduced as pyntp is not a pytnp instance """ import ROOT from math import sqrt from plotfunctions import plotMapTH2F import rootlogon #---- Initialiting ------------------------------- # Input keywords and defaults KEYWORDS = {'title': None, 'outputformat': 'eps'} for key, value in keywords.iteritems(): try: KEYWORDS[key] = value except KeyError: pass if not isinstance(refT, tuple) or not isinstance(otherT, tuple): Message = "The two firts arguments must be a tuples '(pytnp object, 'dataname')'" printError(diff2DMaps.__module__ + '.' + diff2DMaps.__name__, Message, RuntimeError) tnpRef = refT[0] tnp2 = otherT[0] datanameRef = refT[1] dataname2 = otherT[1] datasets = {} _index = 0 for tnp, name in [(tnpRef, datanameRef), (tnp2, dataname2)]: try: datasets[_index] = tnp.RooDataSet[name] for i in [varX, varY]: if not isbinnedVar(datasets[_index], i): message = "Variable '%s' is not a binned variable in the '%s' RooDataSet" % ( i, dataname) printError( diff2DMaps.__module__ + '.' + diff2DMaps.__name__, message, UserWarning) except KeyError: message = """Invalid dataname '%s', is not in the pytnp instance '%s'""" % ( name, tnp) printError(diff2DMaps.__module__ + '.' + diff2DMaps.__name__, message, AttributeError) except AttributeError: message = """The object '%s' is not a pytnp instance""" % str(tnp) printError(diff2DMaps.__module__ + '.' + diff2DMaps.__name__, message, NameError) _index += 1 #--------------------------------------------------- #--- Name for the histo and for the plot file to be saved plotname = tnpRef.resonance+'_'+tnp2.resonance+'_'+tnpRef[datanameRef]['effType']+'_'+tnpRef[datanameRef]['objectType']+'_'+\ tnpRef[datanameRef]['methodUsed'] plotName = 'Comparing_' + plotname #plotName2 = 'ComparingRelative_'+plotname #--- Title for the plot file #title2 = '|#varepsilon_{'+tnpRef_resLatex+'}'+'-#varepsilon_{'+tnp2_resLatex+'}|/#varepsilon_{'+tnpRef_resLatex+'}'+\ # ' '+tnpRef[datanameRef]['objectType']+' '+dataSet.GetTitle() # Dictionary of objects #histoList = [ { 'histo': None, 'plotName': plotName, 'effList': [] }, #'title': title}, # { 'histo': None, 'plotName': plotName2, 'effList': [] },# 'title': title2 } # ] #--- Checking binned variables ##################################a datasetVarList, effName = getVarNames(datasets[0]) listTableEff1 = listTableEff(datasets[0]) xList = [] yList = [] effList = [] for teff1 in listTableEff1: e1, e1lo, e1hi = teff1[effName] x, xlo, xhi = teff1[varX] y, ylo, yhi = teff1[varY] try: e2, e2lo, e2hi = eval('getEff( datasets[1],' + varX + '=' + str(x) + ',' + varY + '=' + str(y) + ')') except TypeError: #Not found efficiency in ptName-etaName bin. Ignore that bin continue finalEffError = sqrt(((e1hi - e1lo) / 2.0)**2.0 + ((e2hi - e2lo) / 2.0)**2.0) #Simetrizing errors #finalEff = abs(e1-e2) finalEff = e1 - e2 effList.append((finalEff, finalEffError)) xList.append(x) yList.append(y) titles = {} for i in [varX, varY]: titles[i] = tnpRef[datanameRef]['binnedVar'][i]['latexName'] unit = tnpRef[datanameRef]['binnedVar'][i]['unit'] if unit != '': titles[i] += ' (' + unit + ') ' #k = 0 XbinsN, arrayX = getBinning(datasets[0].get()[varX]) YbinsN, arrayY = getBinning(datasets[0].get()[varY]) #histoList[0]['effList'] = effList #histoList[|]['effList'] = [ #for hist in histoList: ztitle = 'eff' title = ' CMS Preliminary,' + Lumi + ' #sqrt{s}=7 TeV ' histoname = plotName #hist['plotName'] hist = plotMapTH2F( xList, yList, effList, titles[varX], titles[varY], ztitle, XbinsN, arrayX, YbinsN, arrayY, \ title=title, graphname=histoname, rangeFrame = (0.0, 1.0) ) # hist['histo'] = plotMapTH2F( xList, yList, effList, titles[varX], titles[varY], ztitle, XbinsN, arrayX, YbinsN, arrayY, \ # title=title, graphname=histoname, rangeFrame = (0.0, 1.0) ) #-- TH2F ( and TODO: RooDataSet creation) mapname = tnpRef.resonance+'_'+tnp2.resonance+'_'+tnpRef[datanameRef]['effType']+'_'+tnpRef[datanameRef]['objectType']+'_'+\ tnpRef[datanameRef]['methodUsed'] f = ROOT.TFile('effMaps_' + mapname + '_SYS.root', 'RECREATE') if f.IsZombie(): message = 'Cannot open \'%s\' file. Check your permissions' % fileOut printError(diff2DMaps.__module__ + '.' + diff2DMaps.__name__, message, IOError) #for hist in histoList: # hist['histo'].Write('TH2F_'+hist['plotName']+'_SYS' ) hist.Write('TH2F_' + histoname + '_SYS') f.Close()
def diff2DMaps( refT, otherT, varX, varY, Lumi, **keywords ): """.. function:: diff2DMaps( tnpRef, tnpOther, varX, varY, Lumi\[,title=thetitle, outputformat=format] ) Differences maps from 2 datasets:: eff_ref-eff_other Comparing |eff_ref-eff_other|/eff_ref ComparingRelative --> DEPRECATED The two files must be have the same binning. :param RefT: tuple of pytnp instance for the efficiency of reference and the data name :type RefT: (pytnp,string) :param otherT: tuple pytnp instance for the second efficiency to compare and the data name :type otherT: (pytnp,string) :param varX: name of the binned variable for the x-axis :type varX: string :param varY: name of the binned variable for the y-axis :type varY: string :param Lumi: luminosity :param Lumi: string :keyword thetitle: title to appear in the canvas :type thetitle: string :keyword format: ouput format (eps, root, png, ...) :type format: string :raise RuntimeError: the first two arguments introduced are not tuples (pytnp object, dataname string) :raise UserWarning: some of the variables introduced are not binned variables :raise AttributeError: dataname not found in the root file :raise NameError: the object introduced as pyntp is not a pytnp instance """ import ROOT from math import sqrt from plotfunctions import plotMapTH2F import rootlogon #---- Initialiting ------------------------------- # Input keywords and defaults KEYWORDS = { 'title': None, 'outputformat': 'eps' } for key,value in keywords.iteritems(): try: KEYWORDS[key] = value except KeyError: pass if not isinstance(refT,tuple) or not isinstance(otherT,tuple): Message = "The two firts arguments must be a tuples '(pytnp object, 'dataname')'" printError( diff2DMaps.__module__+'.'+diff2DMaps.__name__, Message, RuntimeError ) tnpRef = refT[0] tnp2 = otherT[0] datanameRef = refT[1] dataname2 = otherT[1] datasets = {} _index = 0 for tnp, name in [(tnpRef,datanameRef), (tnp2,dataname2)]: try: datasets[_index] = tnp.RooDataSet[name] for i in [varX, varY]: if not isbinnedVar( datasets[_index], i ): message = "Variable '%s' is not a binned variable in the '%s' RooDataSet" % (i, dataname) printError( diff2DMaps.__module__+'.'+diff2DMaps.__name__, message, UserWarning ) except KeyError: message = """Invalid dataname '%s', is not in the pytnp instance '%s'""" % (name, tnp) printError( diff2DMaps.__module__+'.'+diff2DMaps.__name__, message, AttributeError ) except AttributeError: message = """The object '%s' is not a pytnp instance""" % str(tnp) printError( diff2DMaps.__module__+'.'+diff2DMaps.__name__, message, NameError ) _index += 1 #--------------------------------------------------- #--- Name for the histo and for the plot file to be saved plotname = tnpRef.resonance+'_'+tnp2.resonance+'_'+tnpRef[datanameRef]['effType']+'_'+tnpRef[datanameRef]['objectType']+'_'+\ tnpRef[datanameRef]['methodUsed'] plotName = 'Comparing_'+plotname #plotName2 = 'ComparingRelative_'+plotname #--- Title for the plot file #title2 = '|#varepsilon_{'+tnpRef_resLatex+'}'+'-#varepsilon_{'+tnp2_resLatex+'}|/#varepsilon_{'+tnpRef_resLatex+'}'+\ # ' '+tnpRef[datanameRef]['objectType']+' '+dataSet.GetTitle() # Dictionary of objects #histoList = [ { 'histo': None, 'plotName': plotName, 'effList': [] }, #'title': title}, # { 'histo': None, 'plotName': plotName2, 'effList': [] },# 'title': title2 } # ] #--- Checking binned variables ##################################a datasetVarList, effName = getVarNames( datasets[0] ) listTableEff1 = listTableEff( datasets[0] ) xList = [] yList = [] effList = [] for teff1 in listTableEff1: e1,e1lo,e1hi = teff1[effName] x,xlo,xhi = teff1[varX] y,ylo,yhi = teff1[varY] try: e2,e2lo,e2hi = eval('getEff( datasets[1],'+varX+'='+str(x)+','+varY+'='+str(y)+')') except TypeError: #Not found efficiency in ptName-etaName bin. Ignore that bin continue finalEffError = sqrt( ((e1hi-e1lo)/2.0)**2.0 + ((e2hi-e2lo)/2.0)**2.0 ) #Simetrizing errors #finalEff = abs(e1-e2) finalEff = e1-e2 effList.append( (finalEff, finalEffError) ) xList.append( x ) yList.append( y ) titles = {} for i in [varX,varY]: titles[i] = tnpRef[datanameRef]['binnedVar'][i]['latexName'] unit = tnpRef[datanameRef]['binnedVar'][i]['unit'] if unit != '': titles[i] += ' ('+unit+') ' #k = 0 XbinsN, arrayX = getBinning( datasets[0].get()[varX] ) YbinsN, arrayY = getBinning( datasets[0].get()[varY] ) #histoList[0]['effList'] = effList #histoList[|]['effList'] = [ #for hist in histoList: ztitle = 'eff' title =' CMS Preliminary,'+Lumi+' #sqrt{s}=7 TeV ' histoname = plotName#hist['plotName'] hist = plotMapTH2F( xList, yList, effList, titles[varX], titles[varY], ztitle, XbinsN, arrayX, YbinsN, arrayY, \ title=title, graphname=histoname, rangeFrame = (0.0, 1.0) ) # hist['histo'] = plotMapTH2F( xList, yList, effList, titles[varX], titles[varY], ztitle, XbinsN, arrayX, YbinsN, arrayY, \ # title=title, graphname=histoname, rangeFrame = (0.0, 1.0) ) #-- TH2F ( and TODO: RooDataSet creation) mapname = tnpRef.resonance+'_'+tnp2.resonance+'_'+tnpRef[datanameRef]['effType']+'_'+tnpRef[datanameRef]['objectType']+'_'+\ tnpRef[datanameRef]['methodUsed'] f = ROOT.TFile( 'effMaps_'+mapname+'_SYS.root' ,'RECREATE') if f.IsZombie(): message = 'Cannot open \'%s\' file. Check your permissions' % fileOut printError( diff2DMaps.__module__+'.'+diff2DMaps.__name__, message, IOError ) #for hist in histoList: # hist['histo'].Write('TH2F_'+hist['plotName']+'_SYS' ) hist.Write('TH2F_'+histoname+'_SYS' ) f.Close()