5 import argparse, sys, os, array, logging
7 logger = logging.getLogger(
'PURW actual mu profile tool')
10 parser = argparse.ArgumentParser(description=
'Make a BCID-aware pileup reweighting file')
11 parser.add_argument(
'--grl', type=str,
12 help=
'GRL to use (mandatory)')
13 parser.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')
16 parser.add_argument(
'--outfile', type=str,
17 help=
'Output file for PURW tool (default: purw.root)',
20 args = parser.parse_args()
23 logger.error(
'BCID File must be specified\n')
27 bcidfile = 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')
39 if not os.path.isfile(args.grl):
40 logger.error(
'Cannot find GRL file %s', args.grl)
41 logger.error(
'Exiting')
44 outfile = ROOT.TFile.Open(args.outfile,
'RECREATE')
45 metadata = ROOT.TTree(
'DataCustomReweighting',
'DataCustomReweighting')
46 cname_arr = bytearray(
'pileup\0',encoding=
'utf-8')
47 runno_arr = array.array(
'I', [0])
48 histname_arr = bytearray(
'pileup_data_run_XXXXXX\0',encoding=
'utf-8')
50 metadata.Branch(
'CustomName', cname_arr,
'CustomName/C')
51 metadata.Branch(
'RunNumber', runno_arr,
'RunNumber/i')
52 metadata.Branch(
'HistName', histname_arr,
'HistName/C')
54 lgp = ROOT.Root.TGoodRunsListReader(args.grl)
56 grl = lgp.GetMergedGoodRunsList()
57 for 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)
82 outfile.WriteTObject(metadata)
85 lumitag = 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)
92 grlo = ROOT.TNamed(
"InputGRL", args.grl)
93 outfile.WriteTObject(grlo)