5import argparse, sys, os, array, logging
7logger = logging.getLogger(
'PURW actual mu profile tool')
10parser = argparse.ArgumentParser(description=
'Make a BCID-aware pileup reweighting file')
11parser.add_argument(
'--grl', type=str,
12 help=
'GRL to use (mandatory)')
13parser.add_argument(
'--bcidfile', type=str,
14 help=
'Input BCID lumi ROOT file (not from iLumiCalc!) (mandatory) For example, /afs/cern.ch/atlas/www/GROUPS/DATAPREPARATION/InteractionsperCrossing/data17_13TeV-OflLumi-13TeV-010.root')
16parser.add_argument(
'--outfile', type=str,
17 help=
'Output file for PURW tool (default: purw.root)',
20args = parser.parse_args()
23 logger.error(
'BCID File must be specified\n')
27bcidfile = ROOT.TFile.Open(args.bcidfile)
29 logger.error(
'Cannot open the BCID lumi file %s', args.bcidfile)
30 logger.error(
'Exiting')
35 logger.error(
'Must specify GRL file\n')
39if not os.path.isfile(args.grl):
40 logger.error(
'Cannot find GRL file %s', args.grl)
41 logger.error(
'Exiting')
44outfile = ROOT.TFile.Open(args.outfile,
'RECREATE')
45metadata = ROOT.TTree(
'DataCustomReweighting',
'DataCustomReweighting')
46cname_arr = bytearray(
'pileup\0',encoding=
'utf-8')
47runno_arr = array.array(
'I', [0])
48histname_arr = bytearray(
'pileup_data_run_XXXXXX\0',encoding=
'utf-8')
50metadata.Branch(
'CustomName', cname_arr,
'CustomName/C')
51metadata.Branch(
'RunNumber', runno_arr,
'RunNumber/i')
52metadata.Branch(
'HistName', histname_arr,
'HistName/C')
54lgp = ROOT.Root.TGoodRunsListReader(args.grl)
56grl = lgp.GetMergedGoodRunsList()
57for run
in grl.GetGoodRuns():
59 runno = run.GetRunNumber()
61 histname_arr[-7:-1] = bytearray(repr(runno),encoding=
'utf-8')
62 rdir = bcidfile.Get(repr(runno))
64 logger.warning(
'Unable to retrieve mu distributions for run %s, skipping ...', runno)
70 for lb
in range(lbrange.Begin(), lbrange.End()+1):
71 inhist = rdir.Get(
'%srec' % lb)
73 logger.warning(
'Unable to retrieve mu distribution for run %s LB %s', (runno, lb))
76 runhist = inhist.Clone(
'pileup_data_run_%s' % runno)
79 outfile.WriteTObject(runhist)
82outfile.WriteTObject(metadata)
85lumitag = bcidfile.Get(
'LuminosityTag')
87 logger.warning(
"Unfortunately, LuminosityTag object not found in BCID info file")
88 logger.warning(
"This is not fatal, but reduces trackability of information.")
90 outfile.WriteTObject(lumitag)
92grlo = ROOT.TNamed(
"InputGRL", args.grl)
93outfile.WriteTObject(grlo)