ATLAS Offline Software
Loading...
Searching...
No Matches
DeMoAtlasDataLoss.py
Go to the documentation of this file.
1#! /usr/bin/env python
2# Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3# Author : Benjamin Trocme (CNRS/IN2P3 - LPSC Grenoble) - 2023
4#
5# Documentation: https://twiki.cern.ch/twiki/bin/viewauth/Atlas/DataQualityDemo
6
7
8import os,sys
9from math import fabs
10from re import match
11from time import strftime,localtime
12
13from ROOT import TFile
14from ROOT import TH1F,TProfile
15from ROOT import TCanvas
16from ROOT import kTeal
17from ROOT import gStyle,gROOT,gPad
18from ROOT import kYellow,kOrange,kRed,kBlue,kPink,kMagenta,kGreen,kSpring,kViolet,kAzure,kCyan,kTeal,kBlack,kWhite,kGray
19gROOT.SetBatch(False)
20
21sys.path.append("/afs/cern.ch/user/l/larmon/public/prod/Misc")
22from LArMonCoolLib import GetLBTimeStamps,GetOfflineLumiFromCOOL,GetReadyFlag
23from DeMoLib import retrieveYearTagProperties,returnPeriod,plotStack,MakeTH1,MakeLegend,strLumi
24
25from DQUtils import fetch_iovs
26
27from argparse import RawTextHelpFormatter,ArgumentParser
28
29global debug
30debug = False
31#debug = True
32
33
36from DQDefects import DefectsDB
37
38parser = ArgumentParser(description='',formatter_class=RawTextHelpFormatter)
39parser.add_argument('-y','--year',dest='parser_year',default = "2022",help='Year [Default: 2022]',action='store')
40parser.add_argument('-t','--tag',dest='parser_tag',default = "AtlasReady",help='Defect tag [Default: "AtlasReady"]',action='store')
41parser.add_argument('-b','--batch',dest='parser_batchMode',help='Batch mode',action='store_true')
42
43args = parser.parse_args()
44year = args.parser_year
45tag = args.parser_tag
46
47parser.print_help()
48
49if args.parser_batchMode:
50 gROOT.SetBatch(True)
51
52affectedLBs = {}
53
54systemList = ["LAr","Pixel","SCT","TRT","Tile","MDT","TGC","RPC","Trig_L1","Trig_HLT","Lumi","Global","ALFA","LUCID","ZDC","IDGlobal","BTag","CaloCP","MuonCP"]
55systemList2 = systemList + ["All"]
56
57for iSyst in systemList2:
58 affectedLBs[iSyst] = {}
59
60DeMoConfig = {}
61for iSyst in systemList:
62 recapDefectsFile = open("YearStats-%s/%s/%s/recapDefects.txt"%(iSyst,year,tag))
63
64 runNumber = ""
65 for iline in recapDefectsFile:
66 ilineSplit = iline.split("|")
67 lbRange = ""
68 if (len(ilineSplit) == 8) and ("Run" not in ilineSplit[0]): # Found a new run
69 runNumber = ilineSplit[0]
70 lbRange = ilineSplit[5]
71 if (len(ilineSplit) == 4): # Found a new run
72 lbRange = ilineSplit[1]
73
74 if (lbRange != ""):
75
76 if (runNumber not in affectedLBs[iSyst].keys()):
77 affectedLBs[iSyst][runNumber] = []
78
79 if (runNumber not in affectedLBs["All"].keys()):
80 affectedLBs["All"][runNumber] = []
81
82 lbRangeSplit = lbRange.split("->")
83 if len(lbRangeSplit) == 1: # Single LB
84 lbAffInt = int(lbRange)
85 if (lbAffInt not in affectedLBs[iSyst][runNumber]):
86 affectedLBs[iSyst][runNumber].append(lbAffInt)
87 if (lbAffInt not in affectedLBs["All"][runNumber]):
88 affectedLBs["All"][runNumber].append(lbAffInt)
89
90 if len(lbRangeSplit) == 2: # Single LB
91 for iLB in range(int(lbRangeSplit[0]),int(lbRangeSplit[1])+1):
92 if (iLB not in affectedLBs[iSyst][runNumber]):
93 affectedLBs[iSyst][runNumber].append(iLB)
94 if (iLB not in affectedLBs["All"][runNumber]):
95 affectedLBs["All"][runNumber].append(iLB)
96
97 if ("Tolerable defect" in iline): # Ignore the tolerable defects
98 break
99
100DeMoConfig = retrieveYearTagProperties(year,tag)
101
102# Retrieve the luminosity for each affected LB from the COOL dbs
103lbLumi = {}
104for iRun in affectedLBs["All"].keys():
105 instOfflLumi = GetOfflineLumiFromCOOL(int(iRun),0,DeMoConfig["OflLumi tag"])
106 atlasready=GetReadyFlag(int(iRun))
107
108 lbLumi[iRun] = {}
109 v_lbTimeSt = GetLBTimeStamps(int(iRun))
110
111 lumiacct=fetch_iovs('COOLOFL_TRIGGER::/TRIGGER/OFLLUMI/LumiAccounting', tag=DeMoConfig["OflLumiAcct tag"], since=v_lbTimeSt[1][0]*1000000000, until=v_lbTimeSt[len(v_lbTimeSt)][1]*1000000000)
112
113 for iLumiAcct in range(len(lumiacct)):
114 if (lumiacct[iLumiAcct].LumiBlock in affectedLBs["All"][iRun]):
115 if (atlasready[lumiacct[iLumiAcct].LumiBlock]):
116 lbLumi[iRun][lumiacct[iLumiAcct].LumiBlock] = lumiacct[iLumiAcct].LBTime*lumiacct[iLumiAcct].LiveFraction*lumiacct[iLumiAcct].InstLumiAll
117 lbLumi[iRun][lumiacct[iLumiAcct].LumiBlock] = lumiacct[iLumiAcct].LBTime*lumiacct[iLumiAcct].LiveFraction*instOfflLumi[lumiacct[iLumiAcct].LumiBlock]
118 else: # Special treatment needed when there are several IOVs with ATLAS ready
119 affectedLBs["All"][iRun].remove(lumiacct[iLumiAcct].LumiBlock)
120 for iSyst in systemList:
121 if (iRun in affectedLBs[iSyst].keys() and lumiacct[iLumiAcct].LumiBlock in affectedLBs[iSyst][iRun]):
122 affectedLBs[iSyst][iRun].remove(lumiacct[iLumiAcct].LumiBlock)
123
124# Retrieve the list of periods and the total luminosity from the TProfile.root file (Global system but should be the same for all)
125periodFile = TFile("YearStats-Global/%s/%s/TProfiles.root"%(year,tag))
126
127h1_lumi = periodFile.Get("h1Period_IntLuminosity_archive")
128periodListHash = h1_lumi.GetXaxis().GetLabels()
129periodName = []
130periodLumi = {}
131
132dataLossPerPeriod = {}
133for iSyst in systemList2:
134 dataLossPerPeriod[iSyst]={}
135
136for i in range(len(periodListHash)):
137 periodName.append(str(periodListHash[i]))
138 periodLumi[periodName[i]] = h1_lumi.GetBinContent(i+1)
139 for iSyst in systemList2:
140 dataLossPerPeriod[iSyst][periodName[i]] = 0.
141
142hprof_dataLossPerSystem = {}
143for iSyst in systemList2:
144 hprof_dataLossPerSystem[iSyst] = MakeTH1("dataLoss_%s"%iSyst,"Period","dataLoss_%s"%iSyst,-0.5,-0.5+len(periodName),len(periodName),kBlack)
145 for i in range(len(periodName)):
146 hprof_dataLossPerSystem[iSyst].GetXaxis().SetBinLabel(i+1,periodName[i])
147
148# Extract data loss per period for each system
149colorPalette = {"Pixel":kViolet-4,
150 "SCT":kSpring-9,
151 "TRT":kPink,
152 "LAr":kRed+1,
153 "Tile":kYellow+1,
154 "MDT":kBlue+3,
155 "TGC":kBlue-4,
156 "RPC":kGreen-2,
157 "Trig_L1":kOrange,
158 "Trig_HLT":kAzure+6,
159 "Lumi":kBlue-9,
160 "Global":kCyan,
161 "IDGlobal":kMagenta+2,
162 "BTag":kPink+9,
163 "CaloCP":kGreen+3,
164 "MuonCP":kGray,
165 "All":kBlack}
166
167for iSyst in systemList2:
168 for iAffRun in affectedLBs[iSyst].keys():
169 period = returnPeriod(int(iAffRun),"Global",year,tag)
170 for iAffLB in affectedLBs[iSyst][iAffRun]:
171 dataLossPerPeriod[iSyst][period] = dataLossPerPeriod[iSyst][period] + lbLumi[iAffRun][iAffLB]
172 dataLossPerPeriod[iSyst]["All"] = dataLossPerPeriod[iSyst]["All"] + lbLumi[iAffRun][iAffLB]
173
174 print("==== %s"%iSyst)
175 if (len(affectedLBs[iSyst])):
176 for i in range(len(periodName)):
177 if (periodLumi[periodName[i]] != 0.):
178 percentLoss = dataLossPerPeriod[iSyst][periodName[i]]/periodLumi[periodName[i]]/10000
179 else:
180 percentLoss = 0.
181 if percentLoss != 0.:
182 print("Period %s -> %.4f percent"%(periodName[i],percentLoss))
183 hprof_dataLossPerSystem[iSyst].Fill(i,percentLoss)
184 hprof_dataLossPerSystem[iSyst].SetBinError(i+1,0.)
185 hprof_dataLossPerSystem[iSyst].SetFillColor(colorPalette[iSyst])
186
187canvas = {}
188stacks = {}
189legends = {}
190systemDict = {}
191for iSyst in systemList:
192 systemDict[iSyst] = iSyst
193
194plotStack("Defects--Periods--%s"%DeMoConfig["Description"],hprof_dataLossPerSystem,systemList,systemDict,h1_lumi,False,stacks,canvas,legends,False)
195hprof_dataLossPerSystem["All"].SetMarkerColor(kOrange+7)
196hprof_dataLossPerSystem["All"].SetMarkerStyle(20)
197hprof_dataLossPerSystem["All"].Draw("PSAME")
198
199ATLASLegend = MakeLegend(0.35,0.79,0.65,0.89)
200ATLASLegend.AddEntry(hprof_dataLossPerSystem["All"], "#splitline{ATLAS inefficiency}{All periods: %.2f%% / %s (%s OK)}"%(dataLossPerPeriod["All"]["All"]/periodLumi["All"]/10000,strLumi(dataLossPerPeriod["All"]["All"]),strLumi(periodLumi["All"]-dataLossPerPeriod["All"]["All"]/1000000,"pb",True,True)), "P")
201ATLASLegend.SetFillColor(kWhite)
202ATLASLegend.Draw()
203
204canvas["Defects--Periods--%s"%DeMoConfig["Description"]].SaveAs("YearStats-Global/%s/%s/DataLoss.png"%(year,tag))
void print(char *figname, TCanvas *c1)