ATLAS Offline Software
PURW_create_actual_mu_profile.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3 
4 import ROOT
5 import argparse, sys, os, array, logging
6 
7 logger = logging.getLogger('PURW actual mu profile tool')
8 logging.basicConfig()
9 
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')
15 
16 parser.add_argument('--outfile', type=str,
17  help='Output file for PURW tool (default: purw.root)',
18  default='purw.root')
19 
20 args = parser.parse_args()
21 
22 if not args.bcidfile:
23  logger.error('BCID File must be specified\n')
24  parser.print_help()
25  sys.exit(2)
26 
27 bcidfile = ROOT.TFile.Open(args.bcidfile)
28 if not bcidfile:
29  logger.error('Cannot open the BCID lumi file %s', args.bcidfile)
30  logger.error('Exiting')
31  sys.exit(1)
32 
33 grl = None
34 if not args.grl:
35  logger.error('Must specify GRL file\n')
36  parser.print_help()
37  sys.exit(2)
38 
39 if not os.path.isfile(args.grl):
40  logger.error('Cannot find GRL file %s', args.grl)
41  logger.error('Exiting')
42  sys.exit(1)
43 
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')
49 
50 metadata.Branch('CustomName', cname_arr, 'CustomName/C')
51 metadata.Branch('RunNumber', runno_arr, 'RunNumber/i')
52 metadata.Branch('HistName', histname_arr, 'HistName/C')
53 
54 lgp = ROOT.Root.TGoodRunsListReader(args.grl)
55 lgp.Interpret()
56 grl = lgp.GetMergedGoodRunsList()
57 for run in grl.GetGoodRuns():
58  outhist=None
59  runno = run.GetRunNumber()
60  runno_arr[0] = runno
61  histname_arr[-7:-1] = bytearray(repr(runno),encoding='utf-8')
62  rdir = bcidfile.Get(repr(runno))
63  if not rdir:
64  logger.warning('Unable to retrieve mu distributions for run %s, skipping ...', runno)
65  continue
66  #print rdir
67  runhist = None
68  for lbrange in run:
69  #print runno, lbrange.Begin(), lbrange.End()
70  for lb in range(lbrange.Begin(), lbrange.End()+1):
71  inhist = rdir.Get('%srec' % lb)
72  if not inhist:
73  logger.warning('Unable to retrieve mu distribution for run %s LB %s', (runno, lb))
74  else:
75  if not runhist:
76  runhist = inhist.Clone('pileup_data_run_%s' % runno)
77  else:
78  runhist.Add(inhist)
79  outfile.WriteTObject(runhist)
80  metadata.Fill()
81 
82 outfile.WriteTObject(metadata)
83 
84 #make some other metadata
85 lumitag = bcidfile.Get('LuminosityTag')
86 if not lumitag:
87  logger.warning("Unfortunately, LuminosityTag object not found in BCID info file")
88  logger.warning("This is not fatal, but reduces trackability of information.")
89 else:
90  outfile.WriteTObject(lumitag)
91 
92 grlo = ROOT.TNamed("InputGRL", args.grl)
93 outfile.WriteTObject(grlo)
94 
95 outfile.Close()
96 
97 
98 
99 
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
PyAthena::repr
std::string repr(PyObject *o)
returns the string representation of a python object equivalent of calling repr(o) in python
Definition: PyAthenaUtils.cxx:106