File indexing completed on 2024-11-25 02:29:01
0001 import ROOT
0002 ROOT.gROOT.SetBatch(True)
0003 from setTDRStyle import setTDRStyle
0004 from granularity import *
0005 import os
0006
0007 try:
0008 base = os.environ['CMSSW_BASE']
0009 except KeyError:
0010 base = "../../../../.."
0011
0012
0013 lumiFiles = {
0014 2016: "{base}/src/Alignment/APEEstimation/data/lumiperrun2016.txt",
0015 2017: "{base}/src/Alignment/APEEstimation/data/lumiperrun2017.txt",
0016 2018: "{base}/src/Alignment/APEEstimation/data/lumiperrun2018.txt",
0017 }
0018 pixelIOVs = {
0019 2016: [271866, 276315, 278271, 280928],
0020 2017: [297281, 298653, 299443, 300389, 301046, 302131, 303790, 303998, 304911],
0021 2018: [316758, 317527, 317661, 317664, 318227, 320377],
0022 }
0023
0024 lumis = {}
0025 intLumis = {}
0026 runs = {}
0027 years = []
0028
0029 for year in lumiFiles.keys():
0030 runs[year] = []
0031 years.append(year)
0032 intLumis[year] = 0
0033 with open(lumiFiles[year].format(base=base), "r") as fi:
0034 for line in fi:
0035 if len(line) == 0:
0036 continue
0037 line = line.strip()
0038 run, lumi = line.split(" ")
0039 run = int(run)
0040 lumi = float(lumi)*0.001
0041 lumis[run] = lumi
0042 runs[year].append(run)
0043 intLumis[year] += lumi
0044 years.sort()
0045
0046 def runToLumi(whichRun, fromYear, inclusive=False):
0047 lumiResult = 0.0
0048 for year in years:
0049 if year < fromYear:
0050 continue
0051 for run in runs[year]:
0052 if run > whichRun:
0053 break
0054 if run < whichRun:
0055 lumiResult += lumis[run]
0056 if run == whichRun and inclusive:
0057 lumiResult += lumis[run]
0058 return lumiResult
0059
0060
0061 def whichYear(run):
0062 thisYear = -1
0063 years = list(runs.keys())
0064 years.sort()
0065 for year in years:
0066 if min(runs[year]) <= run:
0067 thisYear = year
0068 return thisYear
0069 print("Run %d not in range of any year"%(run))
0070 return -1
0071
0072
0073 class TrendPlotter:
0074 def __init__(self):
0075 setTDRStyle()
0076 self.names = {}
0077 self.outPath = None
0078 self.granularity = standardGranularity
0079 self.title = ""
0080 self.points = []
0081 self.years = []
0082 self.doLumi = True
0083 self.colors = []
0084 self.log = False
0085
0086 def addTrend(self, label, points, dashed=False, color=None, marker=None):
0087 self.points.append( (label, points, dashed, color, marker) )
0088 if color:
0089 self.colors.append(color)
0090
0091 def setGranularity(self, granularity):
0092 self.granularity = granularity
0093
0094 def setOutputPath(self, outPath):
0095 self.outPath = outPath
0096
0097 def setTitle(self, title):
0098 self.title = title
0099
0100 def setLog(self, log=True):
0101 self.log = log
0102
0103 def convertName(self, name):
0104 out = name.replace("Bpix", "BPIX")
0105 out = out.replace("Fpix", "FPIX")
0106 out = out.replace("Plus", "+")
0107 out = out.replace("Minus", "-")
0108 out = out.replace("Fpix", "FPIX")
0109 out = out.replace("Tib", "TIB")
0110 out = out.replace("Tob", "TOB")
0111 out = out.replace("Tid", "TID")
0112 out = out.replace("Tec", "TEC")
0113 out = out.replace("Layer", " L")
0114 out = out.replace("Ring", " R")
0115 out = out.replace("Stereo", "S")
0116 out = out.replace("Rphi", "R")
0117 out = out.replace("In", "i")
0118 out = out.replace("Out", "o")
0119 return out
0120
0121 def drawTrendPlot(self, sector, coordinate, number):
0122 self.canvas = ROOT.TCanvas("canvas%s_%s"%(sector, coordinate), "canvas", int(ROOT.gStyle.GetCanvasDefW()*3),ROOT.gStyle.GetCanvasDefH())
0123 ROOT.gPad.SetLeftMargin(0.06)
0124 ROOT.gPad.SetRightMargin(0.04)
0125
0126 iTrend = 0
0127
0128 if self.log:
0129 minApe = 0.9
0130 maxApe = 7000.0
0131 ROOT.gPad.SetLogy()
0132 else:
0133 minApe = 0
0134 maxApe = 100
0135
0136
0137
0138 firstRun = 999999
0139 lastRun = 0
0140 for label, points, dashed, color, marker in self.points:
0141 firstRun = min(min(points, key=lambda x:x[0])[0], firstRun)
0142 lastRun = max(max(points, key=lambda x:x[1])[1], lastRun)
0143 theFirstRun = firstRun
0144 theLastRun = lastRun
0145
0146 firstYear = whichYear(firstRun)
0147 lastYear = whichYear(lastRun)
0148 minLumi = 0
0149
0150 maxLumi = 0
0151 for year in intLumis.keys():
0152 if year >= firstYear and year <= lastYear:
0153 maxLumi += intLumis[year]
0154
0155 verticalLines = []
0156 lineLabels = []
0157 i = 0
0158 for year in range(firstYear, lastYear+1):
0159 for position in pixelIOVs[year]:
0160 if self.doLumi:
0161 posLumi = runToLumi(position, firstYear, False)
0162 else:
0163 posLumi = position
0164 vLine = ROOT.TLine(posLumi,minApe,posLumi,maxApe)
0165 vLine.SetLineStyle(9)
0166 vLine.SetLineColor(ROOT.kRed)
0167 verticalLines.append(vLine)
0168
0169 posApe = 70+3.5*(maxApe-minApe)/100*(i % 5)
0170
0171 text = ROOT.TLatex(posLumi + (maxLumi-minLumi)*0.003 , posApe, str(position))
0172 text.SetTextFont(42)
0173 text.SetTextSize(0.035)
0174 text.SetTextColor(ROOT.kRed+2)
0175 lineLabels.append(text)
0176 i += 1
0177
0178
0179 legend = ROOT.TLegend(0.07, 0.89, 0.935, 0.96)
0180 legend.SetTextFont(42)
0181 legend.SetTextSize(0.045)
0182 legend.SetFillStyle(0)
0183 legend.SetBorderSize(0)
0184 legend.SetNColumns(5)
0185
0186 if self.doLumi:
0187 hAxisLumi = ROOT.TH2F("hAxisRun%s_%s"%(sector, coordinate),"", 10, float(minLumi), float(maxLumi), 10, minApe, maxApe)
0188 hAxisLumi.SetTitle(";integrated luminosity [fb^{-1}];#sigma_{align," + coordinate.lower() + "} [#mum]")
0189 else:
0190 hAxisLumi = ROOT.TH2F("hAxisRun%s_%s"%(sector, coordinate),"", 10, theFirstRun, theLastRun, 10, minApe, maxApe)
0191 hAxisLumi.SetTitle(";Run number;#sigma_{align," + coordinate.lower() + "} [#mum]")
0192 hAxisLumi.GetYaxis().SetTitleOffset(0.4)
0193 hAxisLumi.GetXaxis().SetNdivisions(510)
0194 hAxisLumi.Draw("AXIS")
0195 trends = []
0196 useColor = 1
0197 for label, points, dashed, color, marker in self.points:
0198 iTrend += 1
0199 graphLumi = ROOT.TGraphErrors()
0200 trends.append(graphLumi)
0201
0202 if color:
0203 graphLumi.SetLineColor(color)
0204 graphLumi.SetMarkerColor(color)
0205 else:
0206 while True:
0207 if useColor not in self.colors and useColor not in [0,10]:
0208 self.colors.append(useColor)
0209 graphLumi.SetLineColor(useColor)
0210 graphLumi.SetMarkerColor(useColor)
0211 break
0212 useColor += 1
0213
0214 if marker:
0215 graphLumi.SetLineWidth(0)
0216 graphLumi.SetMarkerSize(1.3)
0217 graphLumi.SetMarkerStyle(marker)
0218 else:
0219 graphLumi.SetLineWidth(2)
0220 graphLumi.SetMarkerSize(0)
0221 graphLumi.SetMarkerStyle(20)
0222
0223
0224 if dashed:
0225 graphLumi.SetLineStyle(2)
0226
0227
0228 iPoint = 0
0229 for firstRun, lastRun, file in points:
0230 fi = ROOT.TFile(file, "READ")
0231 nameTree = fi.Get("nameTree")
0232 apeTree = fi.Get("iterTree{}".format(coordinate))
0233 nameTree.GetEntry(0)
0234 apeTree.GetEntry(apeTree.GetEntries()-1)
0235
0236 sectorApe = 10000. * (float(getattr(apeTree, "Ape_Sector_{}".format(sector))))**0.5
0237 sectorName = str(getattr(nameTree, "Ape_Sector_{}".format(sector)))
0238
0239
0240
0241 if self.doLumi:
0242 lumiStart = runToLumi(firstRun, firstYear, False)
0243 lumiEnd = runToLumi(lastRun, firstYear, True)
0244 else:
0245 lumiStart = firstRun
0246 lumiEnd = lastRun
0247
0248 xPosLumi = (lumiStart+lumiEnd) / 2
0249 xErrLumi = -(lumiStart-lumiEnd) / 2
0250 graphLumi.SetPoint(iPoint, xPosLumi, sectorApe)
0251 graphLumi.SetPointError(iPoint, xErrLumi,0)
0252
0253 iPoint += 1
0254 fi.Close()
0255 graphLumi.Draw("PZ same")
0256 if marker:
0257 legend.AddEntry(graphLumi, label, "pl")
0258 else:
0259 legend.AddEntry(graphLumi, label, "l")
0260 cmsText = ROOT.TLatex(0.16,0.96,self.title)
0261 cmsText.SetTextFont(42)
0262 cmsText.SetNDC()
0263 cmsText.Draw("same")
0264
0265 sectorText = ROOT.TLatex(0.9,0.96,sectorName)
0266 sectorText.SetTextAlign(31)
0267 sectorText.SetTextFont(42)
0268 sectorText.SetNDC()
0269 sectorText.Draw("same")
0270
0271
0272
0273 for vLine in verticalLines:
0274 vLine.Draw("same")
0275 for llabel in lineLabels:
0276 llabel.Draw("same")
0277
0278 legend.Draw("same")
0279
0280 ROOT.gPad.RedrawAxis()
0281
0282 import os
0283 if not os.path.isdir("{}/{}".format(self.outPath, self.granularity.names[coordinate][number])):
0284 os.makedirs("{}/{}".format(self.outPath, self.granularity.names[coordinate][number]))
0285
0286 app = ""
0287 if not self.doLumi:
0288 app = "_byRun"
0289
0290 self.canvas.SaveAs("{}/{}/trend_{}_{}{}.pdf".format(self.outPath, self.granularity.names[coordinate][number], coordinate, sectorName, app))
0291 self.canvas = None
0292
0293
0294
0295 def draw(self):
0296 for coordinate in self.granularity.sectors.keys():
0297 plotNumber = 0
0298 rangeList = self.granularity.sectors[coordinate]
0299 for sectorRange in rangeList:
0300 for sector in range(sectorRange[0], sectorRange[1]+1):
0301 self.drawTrendPlot(sector, coordinate, plotNumber)
0302 plotNumber += 1
0303
0304
0305 def main():
0306 pass
0307
0308
0309 if __name__ == "__main__":
0310 main()