Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:34:06

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