ATLAS Offline Software
Loading...
Searching...
No Matches
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
4import ROOT
5import argparse, sys, os, array, logging
6
7logger = logging.getLogger('PURW actual mu profile tool')
8logging.basicConfig()
9
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')
15
16parser.add_argument('--outfile', type=str,
17 help='Output file for PURW tool (default: purw.root)',
18 default='purw.root')
19
20args = parser.parse_args()
21
22if not args.bcidfile:
23 logger.error('BCID File must be specified\n')
24 parser.print_help()
25 sys.exit(2)
26
27bcidfile = ROOT.TFile.Open(args.bcidfile)
28if not bcidfile:
29 logger.error('Cannot open the BCID lumi file %s', args.bcidfile)
30 logger.error('Exiting')
31 sys.exit(1)
32
33grl = None
34if not args.grl:
35 logger.error('Must specify GRL file\n')
36 parser.print_help()
37 sys.exit(2)
38
39if 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
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')
49
50metadata.Branch('CustomName', cname_arr, 'CustomName/C')
51metadata.Branch('RunNumber', runno_arr, 'RunNumber/i')
52metadata.Branch('HistName', histname_arr, 'HistName/C')
53
54lgp = ROOT.Root.TGoodRunsListReader(args.grl)
55lgp.Interpret()
56grl = lgp.GetMergedGoodRunsList()
57for 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
82outfile.WriteTObject(metadata)
83
84#make some other metadata
85lumitag = bcidfile.Get('LuminosityTag')
86if 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.")
89else:
90 outfile.WriteTObject(lumitag)
91
92grlo = ROOT.TNamed("InputGRL", args.grl)
93outfile.WriteTObject(grlo)
94
95outfile.Close()
96
97
98
99