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