Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python3
0002 
0003 from builtins import range
0004 import copy
0005 import math
0006 import array
0007 import collections
0008 
0009 import ROOT
0010 ROOT.gROOT.SetBatch(True)
0011 ROOT.PyConfig.IgnoreCommandLineOptions = True
0012 
0013 import Validation.RecoTrack.plotting.plotting as plotting
0014 import Validation.RecoTrack.plotting.trackingPlots as trackingPlots
0015 
0016 _colorindex = 2000
0017 _colortmp = {}
0018 def makeColor(hexstr):
0019     r = int("0x"+hexstr[0:2], 16)
0020     g = int("0x"+hexstr[2:4], 16)
0021     b = int("0x"+hexstr[4:6], 16)
0022     
0023     global _colorindex
0024     global _colortmp
0025 
0026     _colorindex += 1
0027     _colortmp[_colorindex] = ROOT.TColor(_colorindex, r/255., g/255., b/255.)
0028     return _colorindex
0029 
0030 iter_colors = dict(
0031     InitialStep         = makeColor("cde7ff"),
0032     HighPtTripletStep   = makeColor("b2d3f3"),
0033     LowPtQuadStep       = makeColor("97c0e8"),
0034     LowPtTripletStep    = makeColor("7cacdc"),
0035     PixelPairStep       = makeColor("6199d1"),
0036     DetachedQuadStep    = makeColor("fdddc2"),
0037     DetachedTripletStep = makeColor("f8cba6"),
0038     MixedTripletStep    = makeColor("f4ba8a"),
0039     PixelLessStep       = makeColor("dee8c9"),
0040     TobTecStep          = makeColor("cddcab"),
0041     JetCoreRegionalStep = makeColor("c1b3d1"),
0042     MuonSeededStepInOut = makeColor("e2a8a5"),
0043     MuonSeededStepOutIn = makeColor("a74a44"),
0044 )
0045 
0046 class Files:
0047     def __init__(self, filesLegends):
0048         self._files = []
0049         self._legends = []
0050         self._styles = []
0051         for fl in filesLegends:
0052             self._legends.append(fl[1])
0053             self._styles.append(fl[2])
0054             if isinstance(fl[0], tuple):
0055                 self._files.append([ROOT.TFile.Open(f) for f in fl[0]])
0056             else:
0057                 self._files.append(ROOT.TFile.Open(fl[0]))
0058 
0059     def getFiles(self):
0060         return self._files
0061 
0062     def getHistos(self, name):
0063         ret = []
0064         for f in self._files:
0065             path = "DQMData/Run 1/"+name
0066             if isinstance(f, list):
0067                 fil = f[0]
0068                 #if "cutsRecoPt" in name:
0069                 #    path = "DQMData/Run 1/"+name.replace("cutsRecoPt", "cutsReco")
0070                 #    fil = f[1]
0071                 obj = fil.Get(path)
0072             else:
0073                 #if "cutsRecoPt" in name:
0074                 #    path = "DQMData/Run 1/"+name.replace("cutsRecoPt", "cutsReco")
0075                 obj = f.Get(path)
0076             if not obj:
0077                 raise Exception("Object %s not found from %s" % (path, f.GetPath()))
0078             ret.append(obj)
0079         return ret
0080 
0081     def getLegends(self):
0082         return self._legends
0083 
0084     def getStyles(self):
0085         return self._styles
0086 
0087 def applyStyle(h, color, markerStyle):
0088     h.SetMarkerStyle(markerStyle)
0089     h.SetMarkerColor(color)
0090     h.SetMarkerSize(1.2)
0091     h.SetLineColor(color)
0092     h.SetLineWidth(2)
0093 
0094 #styles = [
0095 ##    lambda h: applyStyle(h, ROOT.kBlack, 34),
0096 ##    lambda h: applyStyle(h, ROOT.kBlue, 21),
0097 ##    lambda h: applyStyle(h, ROOT.kRed, 22),
0098 ##    lambda h: applyStyle(h, ROOT.kGreen, 20),
0099 #    lambda h: applyStyle(h, ROOT.kBlack, 21),
0100 #    lambda h: applyStyle(h, ROOT.kBlue, 20),
0101 #    lambda h: applyStyle(h, ROOT.kRed, 22),
0102 #    lambda h: applyStyle(h, ROOT.kGreen, 34),
0103 #]
0104 
0105 class Plot:
0106     def __init__(self, histos, legends, styles):
0107         self._histos = histos[:]
0108         self._legends = legends[:]
0109         self._styles = styles[:]
0110 
0111     def remove(self, i):
0112         del self._histos[i]
0113         del self._legends[i]
0114         del self._styles[i]
0115 
0116     def histos(self):
0117         return self._histos
0118 
0119     def setHistos(self, histos):
0120         if len(self._histos) != len(histos):
0121             raise Exception("Had %d histograms, was trying to set %d" % (len(self._histos), len(histos)))
0122         self._histos = histos
0123 
0124     def scale(self, scale):
0125         for h in self._histos:
0126             h.Scale(scale)
0127 
0128     def rebin(self, rebin):
0129         for h in self._histos:
0130             h.Rebin(rebin)
0131 
0132     def normalizeToUnitArea(self):
0133         for h in self._histos:
0134             if h is None:
0135                 continue
0136             i = h.Integral()
0137             if i == 0:
0138                 continue
0139             if h.GetSumw2().fN <= 0: # to suppress warning
0140                 h.Sumw2()
0141             h.Scale(1.0/i)
0142 
0143     def draw(self, opt=""):
0144         self._isHist = ("hist" in opt.lower())
0145 
0146         for h, style in zip(self._histos, self._styles):
0147             style(h)
0148             if self._isHist:
0149                 h.SetLineWidth(3)
0150 
0151             h.Draw("same"+opt)
0152 
0153     def addToLegend(self, legend, legendColumns):
0154         st = "LP"
0155         if self._isHist:
0156             st = "L"
0157 
0158         for h, label in zip(self._histos, self._legends):
0159             legend.AddEntry(h, label, st)
0160 
0161     def getXmin(self):
0162         if len(self._histos) == 0:
0163             return 0
0164         return min([h.GetXaxis().GetBinLowEdge(h.GetXaxis().GetFirst()) for h in self._histos])
0165 
0166     def getXmax(self):
0167         if len(self._histos) == 0:
0168             return 1
0169         return max([h.GetXaxis().GetBinUpEdge(h.GetXaxis().GetLast()) for h in self._histos])
0170 
0171     def getYmax(self):
0172         if len(self._histos) == 0:
0173             return 1
0174         return max([h.GetMaximum() for h in self._histos])
0175 
0176 class PlotStack:
0177     def __init__(self):
0178         self._histos = []
0179         self._legends = []
0180 
0181     def add(self, histo, legend):
0182         histo.ResetBit(ROOT.TH1.kIsAverage)
0183         self._histos.append(histo)
0184         self._legends.append(legend)
0185 
0186     def draw(self, opt=""):
0187         self._stack = ROOT.THStack()
0188         for h in self._histos:
0189             self._stack.Add(h)
0190         self._stack.Draw("same"+opt)
0191 
0192     def addToLegend(self, legend, legendColumns):
0193         st = "f"
0194         for h, label in zip(self._histos, self._legends):
0195             legend.AddEntry(h, label, st)
0196 
0197 
0198 
0199 class PlotText:
0200     ## Constructor
0201     #
0202     # \param x       X coordinate of the text (in NDC)
0203     # \param y       Y coordinate of the text (in NDC)
0204     # \param text    String to draw
0205     # \param size    Size of text (None for the default value, taken from gStyle)
0206     # \param bold    Should the text be bold?
0207     # \param align   Alignment of text (left, center, right)
0208     # \param color   Color of the text
0209     # \param font    Specify font explicitly
0210     def __init__(self, x, y, text, size=None, bold=True, align="left", color=ROOT.kBlack, font=None):
0211         self.x = x
0212         self.y = y
0213         self.text = text
0214 
0215         self.l = ROOT.TLatex()
0216         self.l.SetNDC()
0217         if not bold:
0218             self.l.SetTextFont(self.l.GetTextFont()-20) # bold -> normal
0219         if font is not None:
0220             self.l.SetTextFont(font)
0221         if size is not None:
0222             self.l.SetTextSize(size)
0223         if isinstance(align, basestring):
0224             if align.lower() == "left":
0225                 self.l.SetTextAlign(11)
0226             elif align.lower() == "center":
0227                 self.l.SetTextAlign(21)
0228             elif align.lower() == "right":
0229                 self.l.SetTextAlign(31)
0230             else:
0231                 raise Exception("Error: Invalid option '%s' for text alignment! Options are: 'left', 'center', 'right'."%align)
0232         else:
0233             self.l.SetTextAlign(align)
0234         self.l.SetTextColor(color)
0235 
0236     ## Draw the text to the current TPad
0237     #
0238     # \param options   For interface compatibility, ignored
0239     #
0240     # Provides interface compatible with ROOT's drawable objects.
0241     def Draw(self, options=None):
0242         self.l.DrawLatex(self.x, self.y, self.text)        
0243 
0244 ## Class for drawing text and a background box
0245 class PlotTextBox:
0246     ## Constructor
0247     #
0248     # \param xmin       X min coordinate of the box (NDC)
0249     # \param ymin       Y min coordinate of the box (NDC) (if None, deduced automatically)
0250     # \param xmax       X max coordinate of the box (NDC)
0251     # \param ymax       Y max coordinate of the box (NDC)
0252     # \param lineheight Line height
0253     # \param fillColor  Fill color of the box
0254     # \param transparent  Should the box be transparent? (in practive the TPave is not created)
0255     # \param kwargs       Forwarded to histograms.PlotText.__init__()
0256     def __init__(self, xmin, ymin, xmax, ymax, lineheight=0.05, fillColor=ROOT.kWhite, transparent=True, **kwargs):
0257         # ROOT.TPave Set/GetX1NDC() etc don't seem to work as expected.
0258         self.xmin = xmin
0259         self.xmax = xmax
0260         self.ymin = ymin
0261         self.ymax = ymax
0262         self.lineheight = lineheight
0263         self.fillColor = fillColor
0264         self.transparent = transparent
0265         self.texts = []
0266         self.textArgs = {}
0267         self.textArgs.update(kwargs)
0268 
0269         self.currenty = ymax
0270 
0271     def clone(self):
0272         return copy.deepcopy(self)
0273 
0274     ## Add text to current position
0275     def addText(self, text, yspace=0):
0276         self.currenty -= self.lineheight
0277         self.currenty -= yspace
0278         self.addPlotObject(PlotText(self.xmin+0.01, self.currenty, text, **self.textArgs))
0279 
0280     def replaceText(self, index, text):
0281         y = self.currenty + (len(self.texts)-1-index)*self.lineheight
0282         self.replacePlotObject(index, PlotText(self.xmin+0.01, y, text, **self.textArgs))
0283 
0284     def removeText(self, index):
0285         self.currenty += self.lineheight
0286         self.removePlotObject(index)
0287 
0288     ## Add PlotText object
0289     def addPlotObject(self, obj):
0290         self.texts.append(obj)
0291 
0292     def replacePlotObject(self, index, obj):
0293         self.texts[index] = obj
0294 
0295     def removePlotObject(self, index):
0296         del self.texts[index]
0297 
0298     ## Move the box and the contained text objects
0299     #
0300     # \param dx  Movement in x (positive is to right)
0301     # \param dy  Movement in y (positive is to up)
0302     # \param dw  Increment of width (negative to decrease width)
0303     # \param dh  Increment of height (negative to decrease height)
0304     #
0305     # \a dx and \a dy affect to both box and text objects, \a dw and
0306     # \dh affect the box only.
0307     def move(self, dx=0, dy=0, dw=0, dh=0):
0308         self.xmin += dx
0309         self.xmax += dx
0310         if self.ymin is not None:
0311             self.ymin += dy
0312         self.ymax += dy
0313         self.currenty += dy
0314 
0315         self.xmax += dw
0316         if self.ymin is not None:
0317             self.ymin -= dh
0318 
0319         for t in self.texts:
0320             t.x += dx
0321             t.y += dy
0322 
0323     ## Draw the box and the text to the current TPad
0324     #
0325     # \param options  Forwarded to ROOT.TPave.Draw(), and the Draw() of the contained objects
0326     def Draw(self, options=""):
0327         if not self.transparent:
0328             ymin = self.ymin
0329             if ymin is None:
0330                 ymin = self.currenty - 0.02
0331             self.pave = ROOT.TPave(self.xmin, ymin, self.xmax, self.ymax, 0, "NDC")
0332             self.pave.SetFillColor(self.fillColor)
0333             self.pave.Draw(options)
0334         for t in self.texts:
0335             t.Draw(options)
0336 
0337 def calculateEfficiency(tfile, num_name, denom_name, rebin=None):
0338     num = tfile.Get(num_name)
0339     denom = tfile.Get(denom_name)
0340     if not num:
0341         raise Exception("Did not find %s from %s" % (num_name, tfile.GetName()))
0342     if not denom:
0343         raise Exception("Did not find %s from %s" % (denom_name, tfile.GetName()))
0344 
0345     eff = num.Clone()
0346     den = denom.Clone()
0347     if rebin is not None:
0348         eff.Rebin(rebin)
0349         den.Rebin(rebin)
0350     eff.Divide(den)
0351     return eff
0352                                
0353 def main():
0354     ROOT.gROOT.LoadMacro("tdrstyle.C")
0355     ROOT.setTDRStyle()
0356     ROOT.gROOT.LoadMacro("CMS_lumi_v2.C")
0357 
0358     ROOT.gStyle.SetTitleYOffset(1.0)
0359     ROOT.gStyle.SetPadGridX(True)
0360     ROOT.gStyle.SetPadGridY(True)
0361 
0362     styleRun2 = lambda h: applyStyle(h, ROOT.kBlue, 21)
0363     styleRun2Realistic = lambda h: applyStyle(h, ROOT.kMagenta, 34)
0364     stylePhase1 = lambda h: applyStyle(h, ROOT.kRed, 20)
0365     stylePhase1PU70 = lambda h: applyStyle(h, ROOT.kBlack, 22)
0366     stylePhase1CA = lambda h: applyStyle(h, ROOT.kBlack, 22)
0367 
0368     stylePU0 = lambda h: applyStyle(h, ROOT.kBlue, 21)
0369     stylePU35 = lambda h: applyStyle(h, ROOT.kRed, 20)
0370     stylePU50 = lambda h: applyStyle(h, ROOT.kBlack, 22)
0371     stylePU70 = lambda h: applyStyle(h, ROOT.kOrange+7, 34)
0372 
0373     phase0 = (
0374         #"../DQM_Phase0_ttbar_pu35_v1.root",
0375         "DQM_Phase0_pu35_v4.root",
0376         #"Current detector",
0377         "2016",
0378         styleRun2
0379     )
0380     phase1 = (
0381         #"../DQM_Phase1_ttbar_pu35_v5.root",
0382         "DQM_Phase1_pu35_v7.root",
0383         #"Upgrade detector",
0384         "2017",
0385         stylePhase1
0386     )
0387     phase1ca = (
0388         #"../DQM_Phase1CA_ttbar_pu35_v5.root",
0389         "DQM_Phase1CA_pu35_v4.root",
0390         #"Upgrade detector",
0391         "2017 (CA)",
0392         stylePhase1CA
0393     )
0394 
0395 
0396     files1 = Files([phase0, phase1])
0397     files1ca = Files([phase0, phase1ca])
0398     files2 = Files([phase1, phase1ca])
0399     files2_pu50 = Files([("DQM_Phase1_pu50_v7.root", "2017", stylePhase1), ("DQM_Phase1CA_pu50_v4.root", "2017 (CA)", stylePhase1CA)])
0400     files2_pu70 = Files([("DQM_Phase1_pu70_v7.root", "2017", stylePhase1), ("DQM_Phase1CA_pu70_v4.root", "2017 (CA)", stylePhase1CA)])
0401 
0402     filespu = Files([
0403         #("../DQM_Phase1_ttbar_pu0_v5.root", "2017 no PU", stylePU0),
0404         #("DQM_Phase1_pu0_v1.root", "2017 no PU", stylePU0),
0405         #("DQM_Phase1_pu0_v4.root", "2017 no PU", stylePU0),
0406         ("DQM_Phase1_pu0_v7.root", "2017 no PU", stylePU0),
0407         ("DQM_Phase1_pu35_v7.root", "2017 #LTPU#GT=35", stylePU35),
0408         ("DQM_Phase1_pu50_v7.root", "2017 #LTPU#GT=50", stylePU50),
0409         ("DQM_Phase1_pu70_v7.root", "2017 #LTPU#GT=70", stylePU70),
0410     ])
0411     filescapu = Files([
0412         ("DQM_Phase1CA_pu0_v5.root", "2017 (CA) no PU", stylePU0),
0413         ("DQM_Phase1CA_pu35_v4.root", "2017 (CA) #LTPU#GT=35", stylePU35),
0414         ("DQM_Phase1CA_pu50_v4.root", "2017 (CA) #LTPU#GT=50", stylePU50),
0415         ("DQM_Phase1CA_pu70_v4.root", "2017 (CA) #LTPU#GT=70", stylePU70),
0416     ])
0417 
0418     files0pu_time = Files([
0419         ("../DQM_Phase0_ttbar_pu0_v1.root", "2016 no PU", stylePU0),
0420         ("../DQM_Phase0_ttbar_pu35_v1.root", "2016 #LTPU#GT=35", stylePU35),
0421         ("../DQM_Phase0_ttbar_pu50_v1.root", "2016 #LTPU#GT=50", stylePU50),
0422         ("../DQM_Phase0_ttbar_pu70_v1.root", "2016 #LTPU#GT=70", stylePU70),
0423     ])
0424     filespu_time = Files([
0425         ("../DQM_Phase1_ttbar_pu0_v5.root", "2017 no PU", stylePU0),
0426         ("../DQM_Phase1_ttbar_pu35_v5.root", "2017 #LTPU#GT=35", stylePU35),
0427         ("../DQM_Phase1_ttbar_pu50_v5.root", "2017 #LTPU#GT=50", stylePU50),
0428         ("../DQM_Phase1_ttbar_pu70_v5.root", "2017 #LTPU#GT=70", stylePU70),
0429     ])
0430     filescapu_time = Files([
0431         ("../DQM_Phase1CA_ttbar_pu0_v5.root", "2017 (CA) no PU", stylePU0),
0432         ("../DQM_Phase1CA_ttbar_pu35_v5.root", "2017 (CA) #LTPU#GT=35", stylePU35),
0433         ("../DQM_Phase1CA_ttbar_pu50_v5.root", "2017 (CA) #LTPU#GT=50", stylePU50),
0434         ("../DQM_Phase1CA_ttbar_pu70_v5.root", "2017 (CA) #LTPU#GT=70", stylePU70),
0435     ])
0436 
0437 
0438     pileup = "#LTPU#GT=35"
0439 
0440     plotEffAndFake(files1, "ttbar_pu35_phase0_phase1", pileup)
0441     plotEffAndFake(files1ca, "ttbar_pu35_phase0_phase1ca", pileup, hasCA=True)
0442     plotEffAndFake(files2, "ttbar_pu35_phase1_ca", pileup, hasCA=True)
0443     plotEffAndFake(files2_pu50, "ttbar_pu50_phase1_ca", pileup.replace("35", "50"), hasCA=True)
0444     plotEffAndFake(files2_pu70, "ttbar_pu70_phase1_ca", pileup.replace("35", "70"), hasPU70=True, hasCA=True)
0445 
0446     plotResol(files1, "ttbar_pu35_phase0_phase1", pileup)
0447     #plotResol(files1ca, "ttbar_pu35_phase0_phase1ca", pileup)
0448     plotResol(files2, "ttbar_pu35_phase1_ca", pileup)
0449 
0450     plotVertex(files1, "ttbar_pu35_phase0_phase1", pileup)
0451     #plotVertex(files1ca, "ttbar_pu35_phase0_phase1ca", pileup)
0452     plotVertex(files2, "ttbar_pu35_phase1_ca", pileup)
0453 
0454     #plotFake(files2, "ttbar_pu35_phase1_tdr", pileup)
0455     plotColoredEff(files1.getFiles()[1], "ttbar_pu35_phase1", pileup)
0456     plotColoredEff(files1ca.getFiles()[1], "ttbar_pu35_phase1ca", pileup)
0457 
0458 #    plotDebug(files1_3, "ttbar_pu35_run2debug")
0459 
0460     plotEffAndFake(filespu, "ttbar_phase1_vs_pu", None, hasPU70=True)
0461     plotResol(filespu, "ttbar_phase1_vs_pu", None, hasPU70=True)
0462     plotVertex(filespu, "ttbar_phase1_vs_pu", None, hasPU70=True)
0463 
0464     plotEffAndFake(filescapu, "ttbar_phase1_ca_vs_pu", None, hasPU70=True, hasCA=True)
0465     plotResol(filescapu, "ttbar_phase1_ca_vs_pu", None, hasPU70=True)
0466     plotVertex(filescapu, "ttbar_phase1_ca_vs_pu", None, hasPU70=True)
0467 
0468     plotTime([files0pu_time, filespu_time], "ttbar_phase0_phase1_vs_pu", [0, 35, 50, 70], ["2016", "2017"], [styleRun2, stylePhase1])
0469     plotTime([files0pu_time, filespu_time, filescapu_time], "ttbar_phase0_phase1ca_vs_pu", [0, 35, 50, 70], ["2016", "2017", "2017 (CA)"], [styleRun2, stylePhase1, stylePhase1CA])
0470 
0471     # trackingOnly with DQM+validation enabled, 1 thread
0472     if False:
0473         peakrss = {
0474             "2016": {0: 1515.45,
0475                      35: 1820.23,
0476                      50: 2021.14,
0477                      70: 2375.56},
0478             "2017": {0: 1696.24,
0479                      35: 2110.91,
0480                      50: 2190.12,
0481                      70: 2674.59},
0482             "2017 (CA)": {0: 1697.5,
0483                           35: 2213.77,
0484                           50: 2413.3,
0485                           70: 2795.5},
0486         }
0487         
0488         # trackingOnly without DQM+validation, no output modules, 4 threads
0489         peakrss = {
0490             "2016": {0: 1278.68,
0491                      35: 1664.89,
0492                      50: 1888.64,
0493                      70: 2314.17},
0494             "2017": {0: 1435.69,
0495                      35: 2143.47,
0496                      50: 2525.84,
0497                      70: 2915.48},
0498             "2017 (CA)": {0: 1452.07,
0499                           35: 2266.03,
0500                           50: 2502.52,
0501                           70: 3051.34},
0502         }
0503         
0504         peakrss_v2 = {
0505             "2016": {0: 1267.67,
0506                      35: 1674.13,
0507                      50: 1853.39,
0508                      70: 2192.17},
0509             "2017": {0: 1434.75,
0510                      35: 2123.64,
0511                      50: 2335.03,
0512                      70: 2667.48},
0513             "2017 (CA)": {0: 1441.02,
0514                           35: 2191.23,
0515                           50: 2445.68,
0516                           70: 2729.58},
0517         }
0518         
0519         peakrss_trajectory = {
0520             "2017 90X IB": {0: 1445.08,
0521                          35: 2112.86,
0522                          50: 2320.14,
0523                          60: 2578.23},
0524             "2017 90X IB+#17098": {0: 1422.88,
0525                                    35: 1974.98,
0526                                    50: 2128.61,
0527                                    60: 2300.59},
0528         }
0529         
0530         plotMemory("ttbar_phase0_phase1_vs_pu", peakrss, ["2016", "2017"], [styleRun2, stylePhase1])
0531         plotMemory("ttbar_phase0_phase1ca_vs_pu", peakrss, ["2016", "2017", "2017 (CA)"], [styleRun2, stylePhase1, stylePhase1CA])
0532         
0533         plotMemory("90x_ttbar_phase1_vs_pu", peakrss_trajectory, ["2017 90X IB", "2017 90X IB+#17098"], [stylePhase1, stylePhase1CA])
0534 
0535     printEffFake(files1, pileup)
0536     print("With CA")
0537     printEffFake(files1ca, pileup)
0538 
0539    
0540 
0541 ################################################################################
0542 ################################################################################
0543 ################################################################################
0544 def plotDebug(files, prefix):
0545     folder_dqm = "Tracking/Run summary/TrackParameters/"
0546 
0547     _common = {
0548 #        "xmin": -3, "xmax": 3, "ymin": 0, "ymax": 1,
0549         "ymax": 5e-1,
0550         "legendDx": -0.27, "legendDy": -0.0, #"legendDw": legendDw,
0551         "ylog": True
0552     }
0553 
0554     plot = Plot(files.getHistos(folder_dqm+"generalTracks/GeneralProperties/TrackPErrOverP_ImpactPoint_GenTk"), files.getLegends(), files.getStyles())
0555     for h in plot._histos:
0556         h.Scale(1.0/h.Integral())
0557     drawPlot(prefix+"_dqm_perroverp", plot, ymin=1e-4, **_common)
0558 
0559     plot = Plot(files.getHistos(folder_dqm+"generalTracks/GeneralProperties/TrackPtErrOverPt_ImpactPoint_GenTk"), files.getLegends(), files.getStyles())
0560     for h in plot._histos:
0561         h.Scale(1.0/h.Integral())
0562     drawPlot(prefix+"_dqm_pterroverpt", plot, ymin=1e-5, **_common)
0563 
0564     plot = Plot(files.getHistos(folder_dqm+"generalTracks/GeneralProperties/TrackEtaErr_ImpactPoint_GenTk"), files.getLegends(), files.getStyles())
0565     for h in plot._histos:
0566         h.Scale(1.0/h.Integral())
0567     drawPlot(prefix+"_dqm_etaerr", plot, ymin=1e-8, **_common)
0568 
0569 ################################################################################
0570 ################################################################################
0571 ################################################################################
0572 def plotEffAndFake(files, prefix, pileup, hasPU70=False, hasCA=False):
0573     folder_eff = "Tracking/Run summary/Track/cutsRecoHp_trackingParticleRecoAsssociation/"
0574     folder_fake = "Tracking/Run summary/Track/cutsRecoPt09Hp_trackingParticleRecoAsssociation/"
0575 
0576     isrun2 = ("run2" in prefix)
0577     isrun2real = isrun2 and (len(files.getFiles()) == 3)
0578 
0579     xmin = 0.33
0580     xmax = 0.87
0581     legendDw = -0.1
0582     if isrun2:
0583         legendDw = 0
0584 #        legendDw = -0.1
0585     if isrun2real:
0586         legendDw = 0.25
0587 
0588     putext = ""
0589     if pileup is not None:
0590         putext = " (%s)" % pileup
0591 
0592     effbox = PlotTextBox(xmin, None, xmax, 0.31, transparent=False)
0593     effbox.addText("t#bar{t} event tracks%s"%putext)
0594     effbox.addText("p_{T} > 0.9 GeV, |#eta| < 2.5, ^{}d_{0} < 3.5 cm")
0595 
0596     fakebox = PlotTextBox(xmin, None, xmax-0.04, 0.85, transparent=False)
0597     fakebox.addText("t#bar{t} event tracks%s"%putext)
0598     fakebox.addText("p_{T} > 0.9 GeV")
0599 
0600 
0601     # eta
0602     effbox_eta = effbox.clone()
0603     effbox_eta.replaceText(1, "p_{T} > 0.9 GeV, ^{}d_{0} < 3.5 cm")
0604     plot = Plot(files.getHistos(folder_eff+"effic"), files.getLegends(), files.getStyles())
0605     _common = {
0606         "xtitle": "Simulated track #eta",
0607         "xmin": -3, "xmax": 3, "ymin": 0, "ymax": 1,
0608         "legendDx": -0.27, "legendDy": -0.4, "legendDw": legendDw,
0609         "customise": lambda: effbox_eta.Draw()
0610     }
0611     if isrun2real:
0612         _common["legendTextSize"] = 0.035
0613         _common["legendDx"] -= 0.05
0614 
0615     drawPlot(prefix+"_efficiency_eta", plot, ytitle="Tracking efficiency", **_common)
0616                
0617     _common["xtitle"] = "Track #eta"
0618     _common["ymin"] = 0
0619     _common["ymax"] = 0.2
0620     _common["legendDy"] = -0.2
0621     _common["customise"] = lambda: fakebox.Draw()
0622 
0623     plot = Plot(files.getHistos(folder_fake+"fakerate"),
0624                 files.getLegends(), files.getStyles())
0625     drawPlot(prefix+"_fakerate_eta", plot, ytitle="Tracking fake rate", **_common)
0626 
0627     _common["ymax"] = 0.1
0628     plot = Plot(files.getHistos(folder_fake+"duplicatesRate"),
0629                 files.getLegends(), files.getStyles())
0630     drawPlot(prefix+"_duprate_eta", plot, ytitle="Tracking duplicate rate", **_common)
0631 
0632     # pT
0633     effbox_pt = effbox.clone()
0634     effbox_pt.replaceText(1, "|#eta| < 2.5, ^{}d_{0} < 3.5 cm")
0635     effbox_pt.move(dx=0.05)
0636     fakebox_pt = fakebox.clone()
0637     fakebox_pt.removeText(1)
0638     plot = Plot(files.getHistos(folder_eff+"efficPt"), files.getLegends(), files.getStyles())
0639     _common = {
0640         "xtitle": "Simulated track p_{T} (GeV)",
0641         "xmin": 1e-1, "xmax": 2e2, "ymin": 0, "ymax": 1,
0642         "xlog": True,
0643         "legendDx": -0.22, "legendDy": -0.4, "legendDw": legendDw,
0644         "customise": lambda: effbox_pt.Draw()
0645     }
0646     if isrun2real:
0647         _common["legendTextSize"] = 0.035
0648         _common["legendDx"] -= 0.05
0649     drawPlot(prefix+"_efficiency_pt", plot, ytitle="Tracking efficiency", **_common)
0650                
0651     _common["xtitle"] = "Track p_{T} (GeV)"
0652     _common["ymin"] = 0
0653     _common["ymax"] = 0.6
0654     _common["legendDy"] = -0.2
0655     _common["customise"] = lambda: fakebox_pt.Draw()
0656     if isrun2real:
0657         _common["legendDx"] -= 0.05
0658         _common["legendDy"] += 0.06
0659 
0660     folder_fake_pt = folder_fake.replace("Pt09", "")
0661     plot = Plot(files.getHistos(folder_fake_pt+"fakeratePt"),
0662                 files.getLegends(), files.getStyles())
0663     drawPlot(prefix+"_fakerate_pt", plot, ytitle="Tracking fake rate", **_common)
0664 
0665     _common["ymax"] = 0.1
0666     plot = Plot(files.getHistos(folder_fake_pt+"duplicatesRate_Pt"),
0667                 files.getLegends(), files.getStyles())
0668     #drawPlot(prefix+"_duprate_pt", plot, ytitle="Tracking duplicate rate", **_common)
0669 
0670     # r
0671     effbox_r = effbox.clone()
0672     effbox_r.move(dy=0.6)
0673     effbox_r.replaceText(1, "p_{T} > 0.9 GeV, |#eta| < 2.5")
0674     fakebox_r = fakebox.clone()
0675     fakebox_r.move(dy=-0.55, dx=0.1)
0676     plot = Plot(files.getHistos(folder_eff+"effic_vs_vertpos"), files.getLegends(), files.getStyles())
0677     _common = {
0678         "xtitle": "Sim. track prod. vertex radius (cm)",
0679         "xmin": 0, "xmax": 60, "ymin": 0, "ymax": 1,
0680         "legendDx": 0.1, "legendDy": -0.12, "legendDw": legendDw,
0681         "customise": lambda: effbox_r.Draw()
0682     }
0683     if isrun2:
0684         _common["legendDx"] = -0.35
0685         _common["legendDy"] = -0.55
0686     if isrun2real:
0687         _common["legendTextSize"] = 0.035
0688         _common["legendDx"] -= 0.05
0689         _common["legendDy"] -= 0.02
0690     if hasPU70 and hasCA:
0691         _common["legendDx"] -= 0.1
0692 
0693     drawPlot(prefix+"_efficiency_r", plot, ytitle="Tracking efficiency", **_common)
0694                
0695     _common["xtitle"] = "Track PCA radius (cm)"
0696     _common["ymin"] = 0
0697     #_common["ymax"] = 0.6
0698     _common["ymax"] = 1
0699     _common["legendDy"] = -0.35
0700     _common["customise"] = lambda: fakebox_r.Draw()
0701     if isrun2:
0702         _common["legendDx"] = 0
0703     if isrun2real:
0704         _common["legendDx"] -= 0.25
0705         _common["legendDy"] -= 0.2
0706         fakebox_r.move(dy=0.2, dx=0.02)
0707     if hasPU70:
0708         _common["ymax"] = 1
0709         _common["legendDx"] -= 0.05
0710         fakebox_r.move(dy=0.0, dx=0.2, dw=-0.2)
0711         if hasCA:
0712             _common["legendDx"] += 0.07
0713             _common["legendDw"] += 0.1
0714             _common["legendDy"] -= 0.2
0715             fakebox_r.move(dy=0.2)
0716             
0717 
0718     plot = Plot(files.getHistos(folder_fake+"fakerate_vs_vertpos"),
0719                 files.getLegends(), files.getStyles())
0720     drawPlot(prefix+"_fakerate_r", plot, ytitle="Tracking fake rate", **_common)
0721 
0722     _common["ymax"] = 0.1
0723     fakebox_r.move(dx=-0.2, dy=0.4)
0724     if hasPU70:
0725         fakebox_r.move(dx=0.1)
0726     plot = Plot(files.getHistos(folder_fake+"duplicatesRate_vertpos"),
0727                 files.getLegends(), files.getStyles())
0728     #drawPlot(prefix+"_duprate_r", plot, ytitle="Tracking duplicate rate", **_common)
0729 
0730 
0731 ################################################################################
0732 ################################################################################
0733 ################################################################################
0734 def plotColoredEff(phase1file, prefix, pileup):
0735     #folder_track = "DQMData/Run 1/Tracking/Run summary/Track/cutsReco%s_trackingParticleRecoAsssociation/"
0736     #folder_track = "DQMData/Run 1/Tracking/Run summary/Track/cutsReco%sHp_trackingParticleRecoAsssociation/"
0737     folder_track = "DQMData/Run 1/Tracking/Run summary/Track/cutsReco%sByOriginalAlgoHp_trackingParticleRecoAsssociation/"
0738     iterations = [
0739         "InitialStep",
0740         "HighPtTripletStep",
0741         "LowPtQuadStep",
0742         "LowPtTripletStep",
0743         "DetachedQuadStep",
0744         "DetachedTripletStep",
0745         "MixedTripletStep",
0746         "PixelPairStep",
0747         "PixelLessStep",
0748         "TobTecStep",
0749         "JetCoreRegionalStep",
0750         "MuonSeededStepInOut",
0751         "MuonSeededStepOutIn",
0752     ]
0753     legendLabels = [x.replace("Step", "").replace("Regional", "").replace("SeededInOut", " inside-out").replace("SeededOutIn", " outside-in") for x in iterations]
0754     legendLabels[1:] = ["+"+x for x in legendLabels[1:]]
0755 
0756     putext = ""
0757     if pileup is not None:
0758         putext = " (%s)" % pileup
0759 
0760     xmin = 0.33
0761     xmax = 0.87
0762     legendDw = 0
0763     effbox = PlotTextBox(xmin, None, xmax, 0.31, transparent=True)
0764     effbox.addText("t#bar{t} event tracks%s"%putext)
0765     effbox.addText("p_{T} > 0.9 GeV, |#eta| < 2.5, ^{}d_{0} < 3.5 cm")
0766 
0767     # pt
0768     effbox_pt = effbox.clone()
0769     effbox_pt.replaceText(1, "|#eta| < 2.5, ^{}d_{0} < 3.5 cm")
0770     effbox_pt.move(dx=0.06, dy=-0.02)
0771 #    effbox_pt.move(dx=-0.13, dy=0.6)
0772     plot = PlotStack()
0773     for it, leg in zip(iterations, legendLabels):
0774         hname = folder_track%it + "efficPt"
0775         h = phase1file.Get(hname)
0776         if not h:
0777             raise Exception("Did not find %s from %s" % (hname, phase1file.GetName()))
0778         h.SetFillColor(iter_colors[it])
0779         h.SetLineColor(ROOT.kBlack)
0780         plot.add(h, leg)
0781     _common = {
0782         "xtitle": "Simulated track p_{T} (GeV)",
0783         "xmin": 1e-1, "xmax": 2e2, "ymin": 0, "ymax": 1,
0784         "xlog": True,
0785         "legendDx": -0.13, "legendDy": -0.24, "legendDw": legendDw, "legendDh": 0.23,
0786         "legendTransparent": True,
0787         "customise": lambda: effbox_pt.Draw(),
0788         "drawOpt": "HIST",
0789     }
0790     drawPlot(prefix+"_efficiency_pt_cum", plot, ytitle="Tracking efficiency", **_common)
0791     
0792 
0793     # eta
0794     effbox_eta = effbox.clone()
0795     effbox_eta.replaceText(1, "p_{T} > 0.9 GeV, ^{}d_{0} < 3.5 cm")
0796     effbox_eta.move(dx=0.06, dy=-0.02)
0797 #    effbox_pt.move(dx=-0.13, dy=0.6)
0798     plot = PlotStack()
0799     for it, leg in zip(iterations, legendLabels):
0800         hname = folder_track%it + "effic"
0801         h = phase1file.Get(hname)
0802         if not h:
0803             raise Exception("Did not find %s from %s" % (hname, phase1file.GetName()))
0804         h.SetFillColor(iter_colors[it])
0805         h.SetLineColor(ROOT.kBlack)
0806         plot.add(h, leg)
0807     _common = {
0808         "xtitle": "Simulated track #eta",
0809         "xmin": -3, "xmax": 3, "ymin": 0, "ymax": 1,
0810         "legendDx": -0.15, "legendDy": -0.24, "legendDw": legendDw, "legendDh": 0.23,
0811         "legendTransparent": True,
0812         "customise": lambda: effbox_eta.Draw(),
0813         "drawOpt": "HIST",
0814     }
0815     drawPlot(prefix+"_efficiency_eta_cum", plot, ytitle="Tracking efficiency", **_common)
0816     
0817     # r
0818     effbox_r = effbox.clone()
0819     #effbox_r.replaceText(1, "p_{T} > 0.9 GeV,")
0820     effbox_r.removeText(1)
0821     effbox_r.addText("p_{T} > 0.9 GeV,", yspace=0.01)
0822     effbox_r.addText("|#eta| < 2.5", yspace=0.01)
0823     effbox_r.transparent = False
0824     effbox_r.move(dx=-0.1, dy=0.6)
0825     _common = {
0826         "xtitle": "Sim. track prod. vertex radius (cm)",
0827         "xmin": 0, "xmax": 60, "ymin": 0, "ymax": 1.2,
0828         "legendDx": 0.02, "legendDy": -0.07, "legendDw": legendDw, "legendDh": 0.23,
0829 #        "legendDx": -0.3, "legendDy": -0.12, "legendDw": legendDw+0.33, "legendDh": 0.05,
0830 #        "legendColumns": 2,
0831 #        "legendTransparent": True,
0832         "customiseBeforeLegend": lambda: effbox_r.Draw(),
0833         "drawOpt": "HIST",
0834     }
0835     plot = PlotStack()
0836     for it, leg in zip(iterations, legendLabels):
0837         #hname = folder_track%it + "effic_vs_vertpos"
0838         #h = phase1file.Get(hname)
0839         #if not h:
0840         #    raise Exception("Did not find %s from %s" % (hname, phase1file.GetName()))
0841         num_name = folder_track%it + "num_assoc(simToReco)_vertpos"
0842         denom_name = folder_track%it + "num_simul_vertpos"
0843         eff = calculateEfficiency(phase1file, num_name, denom_name, rebin=2)
0844         eff.SetFillColor(iter_colors[it])
0845         eff.SetLineColor(ROOT.kBlack)
0846         plot.add(eff, leg)
0847 
0848     drawPlot(prefix+"_efficiency_r_cum", plot, ytitle="Tracking efficiency", **_common)
0849 
0850 ################################################################################
0851 ################################################################################
0852 ################################################################################
0853 def plotFake(files, prefix, pileup):
0854     folder_track = "Tracking/Run summary/Track/"
0855     folder_vertex = "Vertexing/Run summary/PrimaryVertexV/"
0856 
0857     iterations = [
0858         "InitialStep",
0859         "LowPtQuadStep",
0860         "HighPtTripletStep",
0861         "LowPtTripletStep",
0862         "DetachedQuadStep",
0863         "DetachedTripletStep",
0864         "MixedTripletStep",
0865         "PixelLessStep",
0866         "TobTecStep",
0867         "JetCoreRegionalStep",
0868         "MuonSeededStepInOut",
0869         "MuonSeededStepOutIn",
0870     ]
0871     binLabels = ["All tracks (p_{T} > 0.9 GeV)"] + [x.replace("Step", "").replace("Regional", "").replace("SeededInOut", " inside-out").replace("SeededOutIn", " outside-in") for x in iterations]
0872     colls = ["cutsRecoTracksPt09Hp"] + ["cutsRecoTracks%sPt09Hp" % x for x in iterations]
0873     
0874     plot = Plot(files.getHistos(folder_track+"num_reco_coll"), files.getLegends(), files.getStyles())
0875     plot_assoc = Plot(files.getHistos(folder_track+"num_assoc(recoToSim)_coll"), files.getLegends(), files.getStyles())
0876     plot_norm = Plot(files.getHistos(folder_vertex+"offlinePrimaryVertices/TruePVLocationIndexCumulative"), files.getLegends(), files.getStyles())
0877     newhistos = []
0878     for (h, hassoc, hnorm) in zip(plot._histos, plot_assoc._histos, plot_norm._histos):
0879         d = plotting._th1ToOrderedDict(h)
0880         dassoc = plotting._th1ToOrderedDict(hassoc)
0881         norm = hnorm.GetEntries()
0882         hnew = ROOT.TH1F(h.GetName()+"_new", "", len(colls), 0, len(colls))
0883         for i, coll in enumerate(colls):
0884             if coll in d:
0885                 vreco = d[coll]
0886                 vassoc = dassoc[coll]
0887                 vfake = vreco[0]-vassoc[0]
0888                 print(vfake, norm)
0889                 hnew.SetBinContent(i+1, vfake)
0890                 hnew.SetBinError(i+1, math.sqrt(vfake))
0891         newhistos.append(hnew)
0892     plot._histos = newhistos
0893 
0894     _common = {
0895         "xtitle": "",
0896         #"xmin": 0, "xmax": 60, "ymin": 0, "ymax": 1.2,
0897         "legendDx": 0.02, "legendDy": -0.07,
0898 #        "legendDx": -0.3, "legendDy": -0.12, "legendDw": legendDw+0.33, "legendDh": 0.05,
0899 #        "legendColumns": 2,
0900 #        "legendTransparent": True,
0901 #        "customiseBeforeLegend": lambda: effbox_r.Draw(),
0902 #        "drawOpt": "HIST",
0903         "xbinlabels": binLabels,
0904         "xbinlabelsize": 0.03,
0905         "xbinlabeloption": "d",
0906     }
0907     drawPlot(prefix+"_fakes_vs_coll", plot, ytitle="Number of fake tracks / event", **_common)
0908 
0909 
0910 ################################################################################
0911 ################################################################################
0912 ################################################################################
0913 def plotResol(files, prefix, pileup, hasPU70=False):
0914     folder_track = "Tracking/Run summary/Track/cutsRecoHp_trackingParticleRecoAsssociation/"
0915     #folder_vertex = "Vertexing/Run summary/PrimaryVertexV/offlinePrimaryVertices/"
0916     folder_vertex = "Vertexing/Run summary/PrimaryVertexV/selectedOfflinePrimaryVertices/"
0917 
0918     isrun2 = ("run2" in prefix)
0919     isrun2real = isrun2 and (len(files.getFiles()) == 3)
0920 
0921     xmin = 0.33
0922     xmax = 0.87
0923     legendDw = -0.1
0924     if isrun2:
0925         legendDw = 0
0926     if isrun2real:
0927         legendDw = 0.25
0928     hasNoPU = ("no PU" in files.getLegends()[0])
0929 
0930     putext = ""
0931     if pileup is not None:
0932         putext = " (%s)" % pileup
0933 
0934     effbox = PlotTextBox(xmin, None, xmax, 0.85, transparent=False)
0935     effbox.addText("t#bar{t} event tracks%s"%putext)
0936 #    effbox.addText("p_{T} > 0.9 GeV, ^{}d_{0} < 3.5 cm")
0937 
0938     vertbox = PlotTextBox(xmin, None, xmax-0.15, 0.85, transparent=False)
0939     vertbox.addText("t#bar{t} events%s"%putext)
0940 
0941     # dxy
0942     plot = Plot(files.getHistos(folder_track+"dxyres_vs_pt_Sigma"), files.getLegends(), files.getStyles())
0943     _common = {
0944         "xtitle": "Simulated track p_{T} (GeV)",
0945         "xmin": 1e-1,
0946         #"xmax": 2e2,
0947         "xmax": 1e2,
0948         "ymin": 0, "ymax": 0.05,
0949         "xlog": True,
0950         "legendDx": -0.1, "legendDy": -0.15, "legendDw": legendDw,
0951         "customise": lambda: effbox.Draw(),
0952         #"ratio": True
0953     }
0954     if isrun2real:
0955         _common["legendTextSize"] = 0.035
0956         _common["legendDx"] -= 0.17
0957         _common["legendDy"] += 0.15
0958         effbox.move(dy=-0.15, dx=0.05)
0959 
0960     _common_ip = copy.copy(_common)
0961     _common_ip["ymax"] = _common_ip["ymax"]*10000
0962 
0963     plot.scale(10000)
0964     drawPlot(prefix+"_resolution_dxy_pt", plot, ytitle="d_{0} resolution (#mum)", **_common_ip)
0965 
0966     # dz
0967     plot = Plot(files.getHistos(folder_track+"dzres_vs_pt_Sigma"), files.getLegends(), files.getStyles())
0968     plot.scale(10000)
0969     drawPlot(prefix+"_resolution_dz_pt", plot, ytitle="d_{z} resolution (#mum)", **_common_ip)
0970 
0971     # pt
0972     plot = Plot(files.getHistos(folder_track+"ptres_vs_pt_Sigma"), files.getLegends(), files.getStyles())
0973     drawPlot(prefix+"_resolution_pt_pt", plot, ytitle="p_{T} resolution / p_{T}", **_common)
0974 
0975     # pt^2 for Marco
0976     plot = Plot(files.getHistos(folder_track+"ptres_vs_pt_Sigma"), files.getLegends(), files.getStyles())
0977     new = []
0978     for h in plot.histos():
0979         x = []
0980         y = []
0981         axis = h.GetXaxis()
0982         for i in range(1, h.GetNbinsX()+1):
0983             x.append(axis.GetBinCenter(i)**2)
0984             y.append(h.GetBinContent(i))
0985 
0986         gr = ROOT.TGraph(len(x), array.array("d", x), array.array("d", y))
0987         new.append(gr)
0988     plot.setHistos(new)
0989     _common_pt2 = copy.copy(_common)
0990     _common_pt2["xtitle"] = "Simulated track p_{T}^{2} (GeV^{2})"
0991     _common_pt2["xmin"] = 1e-2
0992     _common_pt2["xmax"] = 1e4
0993     #drawPlot(prefix+"_resolution_pt_pt2", plot, ytitle="p_{T} resolution / p_{T}", **_common_pt2)
0994 
0995     # vs. eta
0996     _common["xtitle"] = "Simulated track #eta"
0997     _common["xmin"] = -3
0998     _common["xmax"] = 3
0999     _common["xlog"] = False
1000     _common_ip = copy.copy(_common)
1001     _common_ip["ymax"] = 0.1*10000
1002     plot = Plot(files.getHistos(folder_track+"dxyres_vs_eta_Sigma"), files.getLegends(), files.getStyles())
1003     plot.scale(10000)
1004     drawPlot(prefix+"_resolution_dxy_eta", plot, ytitle="d_{0} resolution (#mum)", **_common_ip)
1005 
1006     plot = Plot(files.getHistos(folder_track+"dzres_vs_eta_Sigma"), files.getLegends(), files.getStyles())
1007     plot.scale(10000)
1008     drawPlot(prefix+"_resolution_dz_eta", plot, ytitle="d_{z} resolution (#mum)", **_common_ip)
1009 
1010     plot = Plot(files.getHistos(folder_track+"ptres_vs_eta_Sigma"), files.getLegends(), files.getStyles())
1011     drawPlot(prefix+"_resolution_pt_eta", plot, ytitle="p_{T} resolution / p_{T}", **_common)
1012 
1013     # vs. phi
1014     _common["xtitle"] = "Simulated track #phi"
1015     _common["xmin"] = -3.15
1016     _common["xmax"] =  3.15
1017     _common["xlog"] = False
1018     _common_ip = copy.copy(_common)
1019     _common_ip["ymax"] = 0.1*10000
1020     plot = Plot(files.getHistos(folder_track+"dxyres_vs_phi_Sigma"), files.getLegends(), files.getStyles())
1021     plot.scale(10000)
1022     drawPlot(prefix+"_resolution_dxy_phi", plot, ytitle="d_{0} resolution (#mum)", **_common_ip)
1023 
1024     plot = Plot(files.getHistos(folder_track+"dzres_vs_phi_Sigma"), files.getLegends(), files.getStyles())
1025     plot.scale(10000)
1026     drawPlot(prefix+"_resolution_dz_phi", plot, ytitle="d_{z} resolution (#mum)", **_common_ip)
1027 
1028     plot = Plot(files.getHistos(folder_track+"ptres_vs_phi_Sigma"), files.getLegends(), files.getStyles())
1029     drawPlot(prefix+"_resolution_pt_phi", plot, ytitle="p_{T} resolution / p_{T}", **_common)
1030 
1031     # vertex x
1032     vertbox.move(dx=0.15,dy=0.05)
1033     plot = Plot(files.getHistos(folder_vertex+"RecoAllAssoc2GenMatched_ResolX_vs_NumTracks_Sigma"), files.getLegends(), files.getStyles())
1034     if hasNoPU:
1035         plot.remove(0)
1036     plot.scale(1e4)
1037     _common = {
1038         "xtitle": "Number of tracks",
1039         "xmin": 0, "xmax": 100, "ymin": 0, "ymax": 100,
1040         "legendDx": 0.03, "legendDy": -0.12, "legendDw": legendDw,
1041         "customise": lambda: vertbox.Draw(),
1042         #"ratio": True
1043     }
1044     if isrun2real:
1045         _common["legendTextSize"] = 0.035
1046         _common["legendDx"] -= 0.3
1047         _common["legendDy"] += 0.13
1048         vertbox.move(dy=-0.15, dx=0.05)
1049     drawPlot(prefix+"_vertex_resolution_x_ntrk", plot, ytitle="Vertex x resolution (#mum)", **_common)
1050 
1051     plot = Plot(files.getHistos(folder_vertex+"RecoAllAssoc2GenMatched_ResolX_vs_NumTracks_Sigma"), files.getLegends(), files.getStyles())
1052     if hasNoPU:
1053         plot.remove(0)
1054     plot.scale(1e4)
1055     drawPlot(prefix+"_vertex_resolution_xy_ntrk", plot, ytitle="Vertex x/y resolution (#mum)", **_common)
1056 
1057     # vertex y
1058     plot = Plot(files.getHistos(folder_vertex+"RecoAllAssoc2GenMatched_ResolY_vs_NumTracks_Sigma"), files.getLegends(), files.getStyles())
1059     if hasNoPU:
1060         plot.remove(0)
1061     plot.scale(1e4)
1062     drawPlot(prefix+"_vertex_resolution_y_ntrk", plot, ytitle="Vertex y resolution (#mum)", **_common)
1063 
1064     # vertex z
1065     plot = Plot(files.getHistos(folder_vertex+"RecoAllAssoc2GenMatched_ResolZ_vs_NumTracks_Sigma"), files.getLegends(), files.getStyles())
1066     if hasNoPU:
1067         plot.remove(0)
1068     plot.scale(1e4)
1069     drawPlot(prefix+"_vertex_resolution_z_ntrk", plot, ytitle="Vertex z resolution (#mum)", **_common)
1070 
1071 
1072 ################################################################################
1073 ################################################################################
1074 ################################################################################
1075 def plotVertex(files, prefix, pileup, hasPU70=False):
1076     #folder_vertex = "Vertexing/Run summary/PrimaryVertexV/offlinePrimaryVertices/"
1077     folder_vertex = "Vertexing/Run summary/PrimaryVertexV/selectedOfflinePrimaryVertices/"
1078 
1079     xmin = 0.33
1080     xmax = 0.87
1081     legendDw = -0.1
1082 
1083     hasNoPU = ("no PU" in files.getLegends()[0])
1084 
1085     putext = ""
1086     if pileup is not None:
1087         putext = "(%s)" % pileup
1088 
1089     vertbox = PlotTextBox(xmin, None, xmax-0.15, 0.35, transparent=False)
1090     vertbox.addText("t#bar{t} events%s"%putext)
1091 
1092     # efficiency
1093     plot = Plot(files.getHistos(folder_vertex+"effic_vs_NumVertices"), files.getLegends(), files.getStyles())
1094     _common = dict(
1095         xtitle="Simulated interactions",
1096         xmin=10, xmax=70, ymin=0, ymax=1,
1097         legendDx=-0.2, legendDy=-0.3, legendDw=legendDw,
1098         customise=lambda: vertbox.Draw()
1099     )
1100     if hasPU70:
1101         _common["xmax"] = 100
1102     if hasNoPU:
1103         plot.remove(0)
1104     drawPlot(prefix+"_vertex_efficiency_pu", plot, ytitle="Vertex efficiency", **_common)
1105     
1106     # fake rate
1107     _common["ymax"] = 0.2
1108     _common["legendDy"] += 0.15
1109     vertbox.move(dy=0.55)
1110     plot = Plot(files.getHistos(folder_vertex+"fakerate_vs_PU"), files.getLegends(), files.getStyles())
1111     if hasNoPU:
1112         plot.remove(0)
1113     drawPlot(prefix+"_vertex_fakerate_pu", plot, ytitle="Vertex fake rate", **_common)
1114 
1115     # nvtx vs. PU
1116     _common_nvtx = copy.copy(_common)
1117     _common_nvtx["ymin"] = _common_nvtx["ymax"]
1118     _common_nvtx["ymax"] = _common_nvtx["xmax"]
1119     _common_nvtx["legendDy"] += 0.05
1120     plot = Plot(files.getHistos(folder_vertex+"RecoVtx_vs_GenVtx"), files.getLegends(), files.getStyles())
1121     if hasNoPU:
1122         plot.remove(0)
1123     drawPlot(prefix+"_vertex_nvtx_vs_pu", plot, ytitle="Vertices", **_common_nvtx)
1124     
1125     # merge rate
1126     plot = Plot(files.getHistos(folder_vertex+"merged_vs_PU"), files.getLegends(), files.getStyles())
1127     if hasNoPU:
1128         plot.remove(0)
1129     drawPlot(prefix+"_vertex_mergerate_pu", plot, ytitle="Vertex merge rate", **_common)
1130 
1131     vertbox.move(dx=0.1)
1132     del _common["xtitle"]
1133     del _common["xmin"]
1134     del _common["xmax"]
1135     _common["ymax"] = 1
1136     _common["legendDx"] += 0.3
1137     plot = Plot(files.getHistos(folder_vertex+"merged_vs_ClosestVertexInZ"), files.getLegends(), files.getStyles())
1138     drawPlot(prefix+"_vertex_mergerate_closestz", plot, xtitle="Distance to closest vertex in z (cm)", ytitle="Vertex merge rate", xlog=True, **_common)
1139 
1140     # purity
1141     vertbox.move(dx=-0.2)
1142     _common["ymin"] = 5e-4
1143     _common["ymax"] = 1
1144     _common["legendDx"] -= 0.3
1145     plot = Plot(files.getHistos(folder_vertex+"RecoPVAssoc2GenPVMatched_Purity"), files.getLegends(), files.getStyles())
1146     if hasNoPU:
1147         plot.remove(0)
1148     plot.normalizeToUnitArea()
1149     #drawPlot(prefix+"_vertex_purity", plot, xtitle="Primary vertex purity", ytitle="Fraction of events", ylog=True, **_common)
1150 
1151 ################################################################################
1152 ################################################################################
1153 ################################################################################
1154 def plotTime(fileCollections, prefix, pileups, legends, styles):
1155     folderTime = "DQMData/Run 1/DQM/Run summary/TimerService/process RECO/Paths"
1156 
1157     xmin = 0.33-0.07
1158     xmax = 0.87
1159     legendDw = -0.1
1160     vertbox = PlotTextBox(xmin, None, xmax-0.3, 0.7, transparent=False)
1161     vertbox.addText("t#bar{t} events")
1162 
1163     relbox = vertbox.clone()
1164     #relbox.addText("2016 no PU tracking time = 1")
1165     relbox.addText("tracking time of", yspace=0.01)
1166     relbox.addText("2016 no PU = 1")
1167 
1168     creator = plotting.AggregateBins("iteration", "reconstruction_step_module_average", trackingPlots._iterModuleMap(includeConvStep=False), ignoreMissingBins=True)
1169 
1170     normfactor = None
1171 
1172     maxAbs = 20
1173     maxRel = 80
1174 
1175     graphs = []
1176     graphs_abs = []
1177     for files in fileCollections:
1178         times = []
1179         for f in files.getFiles():
1180             tdir = f.Get(folderTime)
1181             h = creator.create(tdir)
1182             if not h:
1183                 raise Exception("Did not find time from file %s" % f.GetPath())
1184             times.append(h.Integral()/1000.)
1185 
1186         # rely 2016 being the first being passed, and [0] being noPU
1187         if normfactor is None:
1188             normfactor = 1/times[0]
1189 
1190         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", times))
1191         graphs_abs.append(gr)
1192         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", [t*normfactor for t in times]))
1193         graphs.append(gr)
1194 
1195     print("Time normalization factor", normfactor)
1196 
1197 
1198     plot = Plot(graphs, legends, styles)
1199     drawPlot(prefix+"_time_vs_pu", plot, xtitle="Average pileup", ytitle="Tracking time (a.u.)",
1200              drawOpt="PL", ymax=maxRel,
1201              legendDx=-0.2, legendDw=legendDw,
1202              customise=lambda: relbox.Draw()
1203     )
1204     plot = Plot(graphs_abs, legends, styles)
1205     drawPlot(prefix+"_time_vs_pu_abs", plot, xtitle="Average pileup", ytitle="Tracking time (s)",
1206              drawOpt="PL", ymax=maxAbs,
1207              legendDx=-0.2, legendDw=legendDw,
1208              customise=lambda: vertbox.Draw()
1209     )
1210 
1211     ##########
1212 
1213     creator = plotting.AggregateBins("iteration", "reconstruction_step_module_average", trackingPlots._stepModuleMap(), ignoreMissingBins=True)
1214 
1215     graphs_seed = []
1216     graphs_seed_abs = []
1217     graphs_build = []
1218     graphs_build_abs = []
1219     for files in fileCollections:
1220         times_seed = []
1221         times_build = []
1222         for f in files.getFiles():
1223             tdir = f.Get(folderTime)
1224             h = creator.create(tdir)
1225             if not h:
1226                 raise Exception("Did not find time from file %s" % f.GetPath())
1227 
1228             d = plotting._th1ToOrderedDict(h)
1229 
1230             times_seed.append(d["Seeding"][0]/1000.)
1231             times_build.append(d["Building"][0]/1000.)
1232             
1233         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", times_seed))
1234         graphs_seed_abs.append(gr)
1235         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", [t*normfactor for t in times_seed]))
1236         graphs_seed.append(gr)
1237 
1238         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", times_build))
1239         graphs_build_abs.append(gr)
1240         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", [t*normfactor for t in times_build]))
1241         graphs_build.append(gr)
1242 
1243     plot = Plot(graphs_seed, legends, styles)
1244     drawPlot(prefix+"_seedtime_vs_pu", plot, xtitle="Average pileup", ytitle="Seeding time (a.u.)",
1245              drawOpt="PL", ymax=maxRel,
1246              legendDx=-0.2, legendDw=legendDw,
1247              customise=lambda: relbox.Draw()
1248     )
1249     plot = Plot(graphs_seed_abs, legends, styles)
1250     drawPlot(prefix+"_seedtime_vs_pu_abs", plot, xtitle="Average pileup", ytitle="Seeding time (s)",
1251              drawOpt="PL", ymax=maxAbs,
1252              legendDx=-0.2, legendDw=legendDw,
1253              customise=lambda: vertbox.Draw()
1254     )
1255 
1256     plot = Plot(graphs_build, legends, styles)
1257     drawPlot(prefix+"_buildtime_vs_pu", plot, xtitle="Average pileup", ytitle="Pattern recognition time (a.u.)",
1258              drawOpt="PL", ymax=maxRel,
1259              legendDx=-0.2, legendDw=legendDw,
1260              customise=lambda: relbox.Draw()
1261     )
1262     plot = Plot(graphs_build_abs, legends, styles)
1263     drawPlot(prefix+"_buildtime_vs_pu_abs", plot, xtitle="Average pileup", ytitle="Pattern recognition time (s)",
1264              drawOpt="PL", ymax=maxAbs,
1265              legendDx=-0.2, legendDw=legendDw,
1266              customise=lambda: vertbox.Draw()
1267     )
1268 
1269     #######
1270 
1271     creator = plotting.AggregateBins("initialStepPreSplitting", "reconstruction_step_module_average", collections.OrderedDict(trackingPlots._iterations[0].modules()), ignoreMissingBins=True)
1272 
1273     graphs = []
1274     graphs_abs = []
1275     for files in fileCollections:
1276         times = []
1277         for f in files.getFiles():
1278             tdir = f.Get(folderTime)
1279             h = creator.create(tdir)
1280             if not h:
1281                 raise Exception("Did not find time from file %s" % f.GetPath())
1282 
1283             d = plotting._th1ToOrderedDict(h)
1284 
1285             times.append(d["Seeding"][0]/1000.)
1286 
1287         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", times))
1288         graphs_abs.append(gr)
1289         gr = ROOT.TGraph(len(pileups), array.array("d", pileups), array.array("d", [t*normfactor for t in times]))
1290         graphs.append(gr)
1291 
1292     ymaxfac = 0.025
1293 
1294     plot = Plot(graphs, legends, styles)
1295     drawPlot(prefix+"_pixeltracks_vs_pu", plot, xtitle="Average pileup", ytitle="Pixel tracking time (a.u.)",
1296              drawOpt="PL", ymax=maxRel*ymaxfac,
1297              legendDx=-0.2, legendDw=legendDw,
1298              customise=lambda: relbox.Draw()
1299     )
1300     plot = Plot(graphs_abs, legends, styles)
1301     drawPlot(prefix+"_pixeltracks_vs_pu_abs", plot, xtitle="Average pileup", ytitle="Pixel tracking time (s)",
1302              drawOpt="PL", ymax=maxAbs*ymaxfac,
1303              legendDx=-0.2, legendDw=legendDw,
1304              customise=lambda: vertbox.Draw()
1305     )
1306 
1307 ################################################################################
1308 ################################################################################
1309 ################################################################################
1310 def plotMemory(prefix, data, legends, styles):
1311     xmin = 0.33
1312     xmax = 0.87
1313     legendDw = -0.1
1314     vertbox = PlotTextBox(xmin, None, xmax-0.3, 0.73, transparent=False)
1315     vertbox.addText("t#bar{t} events")
1316 
1317     graphs = []
1318     for l in legends:
1319         d = data[l]
1320         x = d.keys()
1321         x.sort()
1322         y = [d[i]/1000. for i in x]
1323         gr = ROOT.TGraph(len(x), array.array("d", x), array.array("d", y))
1324         graphs.append(gr)
1325 
1326     plot = Plot(graphs, legends, styles)
1327     drawPlot(prefix+"_trkonly_peakrss_vs_pu", plot, xtitle="Average pileup", ytitle="Peak memory (GB)",
1328              drawOpt="PL",
1329              ymin=1, ymax=3.2,
1330              legendDx=-0.2, legendDw=legendDw,
1331              customise=lambda: vertbox.Draw()
1332     )
1333 
1334 
1335 ################################################################################
1336 ################################################################################
1337 ################################################################################
1338 def printEffFake(files, pileup):
1339     print("For pileup", pileup)
1340     for f, l in zip(files.getFiles(), files.getLegends()):
1341         eff_h = f.Get("DQMData/Run 1/Tracking/Run summary/Track/effic_vs_coll")
1342         fake_h = f.Get("DQMData/Run 1/Tracking/Run summary/Track/fakerate_vs_coll")
1343 
1344         eff_d = plotting._th1ToOrderedDict(eff_h)
1345         fake_d = plotting._th1ToOrderedDict(fake_h)
1346 
1347         print(l)
1348 
1349         #coll = "generalTracks"
1350         #collPt = coll
1351         coll = "cutsRecoTracksHp"
1352         collPt = "cutsRecoTracksPt09Hp"
1353         print("Efficiency", eff_d[coll])
1354         print("Fake rate ", fake_d[coll])
1355         print("Efficiency (track pt>0.9)", eff_d[collPt])
1356         print("Fake rate  (track pt>0.9)", fake_d[collPt])
1357 
1358 
1359 
1360 
1361 def drawPlot(name, plot, xmin=None, ymin=0, xmax=None, ymax=None, xlog=False, ylog=False,
1362              xtitle=None, ytitle=None,
1363              drawOpt="PE",
1364              addLegend=True, legendDx=0, legendDy=0, legendDw=0, legendDh=0, legendTransparent=False, legendColumns=1, legendTextSize=0.04,
1365              rebin=None,
1366              ratio=False, ratioYmin=0.5, ratioYmax=1,
1367              customiseBeforeLegend=None, customise=None,
1368              period=4,
1369              #pos=11):
1370              pos=0,
1371              xbinlabels=None, xbinlabelsize=None, xbinlabeloption=None):
1372 #    W = 800
1373 #    H = 600
1374 #    H_ref = 600
1375 #    W_ref = 800
1376 
1377     ratioFactor = 1.25
1378 
1379     W = 600
1380     H = 600
1381     H_ref = 600
1382     W_ref = 600
1383 
1384     T = 0.08*H_ref
1385     B = 0.16*H_ref 
1386     L = 0.18*W_ref
1387     R = 0.04*W_ref
1388 
1389     H_canv = H
1390     if ratio:
1391         H_canv = int(H*ratioFactor)
1392 
1393     canv = ROOT.TCanvas(name,name,10,10,W,H_canv);
1394     canv.SetFillColor(0);
1395     canv.SetBorderMode(0);
1396     canv.SetFrameFillStyle(0);
1397     canv.SetFrameBorderMode(0);
1398     canv.SetLeftMargin( L/W );
1399     canv.SetRightMargin( R/W );
1400     canv.SetTopMargin( T/H );
1401     canv.SetBottomMargin( B/H );
1402     canv.SetTickx(0);
1403     canv.SetTicky(0);
1404 
1405     if ratio:
1406         plotting._modifyPadForRatio(canv, ratioFactor)
1407 
1408     if xmin is None:
1409         xmin = plot.getXmin()
1410     if xmax is None:
1411         xmax = plot.getXmax()
1412     if ymax is None:
1413         ymax = 1.1*plot.getYmax()
1414 
1415     bounds = (xmin, ymin, xmax, ymax)
1416     args = {"nrows": 1,
1417             "zmax": None}
1418     if xbinlabels is not None:
1419         args["xbinlabels"] = xbinlabels
1420         args["xbinlabelsize"] = xbinlabelsize
1421         args["xbinlabeloption"] = xbinlabeloption
1422     if ratio:
1423         ratioBounds = (xmin, ratioYmin, xmax, ratioYmax)
1424         frame = plotting.FrameRatio(canv, bounds, ratioBounds, ratioFactor, **args)
1425         frame._frameRatio.GetYaxis().SetLabelSize(0.12)
1426     else:
1427         frame = plotting.Frame(canv, bounds, **args)
1428 
1429     if xtitle is not None:
1430         frame.setXTitle(xtitle)
1431     if ytitle is not None:
1432         frame.setYTitle(ytitle)
1433 
1434     frame.setXTitleOffset(1.15)
1435     frame.setYTitleOffset(1.55)
1436 
1437     frame.setLogx(xlog)
1438     frame.setLogy(ylog)
1439 
1440     if rebin is not None:
1441         plot.rebin(rebin)
1442 
1443     if ratio:
1444         frame._pad.cd()
1445 
1446     plot.draw(drawOpt)
1447 
1448     ratios = None
1449     if ratio:
1450         frame._padRatio.cd()
1451         ratios = plotting._calculateRatios(plot._histos)
1452         for r in ratios[1:]:
1453             r.draw()
1454         frame._pad.cd()
1455 
1456     if customiseBeforeLegend is not None:
1457         customiseBeforeLegend()
1458 
1459     if addLegend:
1460         lx1 = 0.6
1461         lx2 = 0.95
1462 #        ly1 = 0.8
1463         ly1 = 0.75
1464         ly2 = 0.9
1465         #ly1 = 0.73
1466         #ly2 = 0.83
1467         
1468         #legendDx -= 0.21
1469         
1470         lx1 += legendDx
1471         lx2 += legendDx
1472         ly1 += legendDy
1473         ly2 += legendDy
1474         
1475         lx2 += legendDw
1476         ly1 -= legendDh
1477             
1478         legend = ROOT.TLegend(lx1, ly1, lx2, ly2)
1479         if legendColumns != 1:
1480             legend.SetNColumns(legendColumns)
1481         legend.SetTextSize(legendTextSize)
1482         legend.SetLineColor(1)
1483         legend.SetLineWidth(1)
1484         legend.SetLineStyle(1)
1485         legend.SetFillColor(0)
1486         legend.SetMargin(0.07)
1487         legend.SetBorderSize(0)
1488         if legendTransparent:
1489             legend.SetFillStyle(0)
1490         plot.addToLegend(legend, legendColumns)
1491         legend.Draw()
1492 
1493     frame._pad.cd()
1494     ROOT.CMS_lumi_v2(frame._pad, period, pos)
1495 
1496     canv.Update()
1497     canv.RedrawAxis();
1498     canv.GetFrame().Draw()
1499 
1500     if customise is not None:
1501         customise()
1502 
1503     canv.SaveAs(name+".png")
1504     canv.SaveAs(name+".pdf")
1505 
1506 if __name__ == "__main__":
1507     main()