ATLAS Offline Software
Loading...
Searching...
No Matches
ExampleMonitorAlgorithm.py
Go to the documentation of this file.
2# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3#
4
5'''@file ExampleMonitorAlgorithm.py
6@author C. D. Burton
7@author P. Onyisi
8@date 2018-01-11
9@brief Example python configuration for the Run III AthenaMonitoring package
10'''
11
13 '''Function to configures some algorithms in the monitoring system.'''
14
15
21
22 # The following class will make a sequence, configure algorithms, and link
23 # them to GenericMonitoringTools
24 from AthenaMonitoring import AthMonitorCfgHelper
25 helper = AthMonitorCfgHelper(flags, 'ExampleAthMonitorCfg')
26
27
28
35 from AthenaConfiguration.ComponentFactory import CompFactory
36 exampleMonAlg = helper.addAlgorithm(CompFactory.ExampleMonitorAlgorithm,'ExampleMonAlg')
37
38 # You can actually make multiple instances of the same algorithm and give
39 # them different configurations
40 anotherExampleMonAlg = helper.addAlgorithm(CompFactory.ExampleMonitorAlgorithm,'AnotherExampleMonAlg')
41
42 # # If for some really obscure reason you need to instantiate an algorithm
43 # # yourself, the AddAlgorithm method will still configure the base
44 # # properties and add the algorithm to the monitoring sequence.
45 # helper.AddAlgorithm(myExistingAlg)
46
47
48
54
55
58
59 # # Then, add a tool that doesn't have its own configuration function. In
60 # # this example, no accumulator is returned, so no merge is necessary.
61 # exampleMonAlg.MyDomainTool = CompFactory.MyDomainTool()
62
63 # Add a generic monitoring tool (a "group" in old language). The returned
64 # object here is the standard GenericMonitoringTool.
65 myGroup = helper.addGroup(
66 exampleMonAlg,
67 'ExampleMonitor',
68 'OneRing/'
69 )
70
71 # Add a GMT for the other example monitor algorithm
72 anotherGroup = helper.addGroup(anotherExampleMonAlg,'ExampleMonitor')
73
74
75
77 myGroup.defineHistogram('lumiPerBCID',title='Luminosity,WithCommaInTitle;L/BCID;Events',
78 path='ToRuleThemAll',xbins=40,xmin=0.0,xmax=80.0)
79 myGroup.defineHistogram('lb', title='Luminosity Block;lb;Events',
80 path='ToFindThem',xbins=1000,xmin=-0.5,xmax=999.5,weight='testweight')
81 myGroup.defineHistogram('random;random2', title='LB;x;Events',
82 path='ToBringThemAll',xbins=30,xmin=0,xmax=1,opt='kLBNHistoryDepth=10')
83 myGroup.defineHistogram('random', title='title;x;y',path='ToBringThemAll',
84 xbins=[0,.1,.2,.4,.8,1.6])
85 myGroup.defineHistogram('random,pT', type='TH2F', title='title;x;y',path='ToBringThemAll',
86 xbins=[0,.1,.2,.4,.8,1.6],ybins=[0,10,30,40,60,70,90])
87 # specify a merge method
88 myGroup.defineHistogram('lumiPerBCID;lumiPerBCID_merge',title='Luminosity,WithCommaInTitle;L/BCID;Events',
89 path='ToRuleThemAll',xbins=40,xmin=0.0,xmax=80.0, merge='weightedAverage')
90 # use strings as bin labels
91 myGroup.defineHistogram('evtstr', title='Event number as string;Event number;Events',
92 xbins=2500, path='ToRuleThemAll', merge='merge')
93 # TEfficiencies
94 myGroup.defineHistogram('pT_passed,pT', type='TEfficiency', title='Test TEfficiency;x;Eff',
95 path='AndInTheDarkness', xbins=100, xmin=0.0, xmax=50.0)
96 myGroup.defineHistogram('pT_passed,pT,random', type='TEfficiency', title='Test TEfficiency 2D;x;y;Eff',
97 path='AndInTheDarkness', xbins=100, xmin=0.0, xmax=50.0,
98 ybins=10, ymin=0.0, ymax=2.0)
99 # # use a cutmask to only fill certain events
100 myGroup.defineHistogram('pT;pT_with_cut', title='p_{T};p_{T};Events', path='AndInTheDarkness',
101 xbins=50, xmin=0, xmax=50, cutmask='pT_passed')
102 # make a plot which will always exist, even though it is never filled
103 myGroup.defineHistogram('dummy;alwaysempty', title='Plot is always empty', path='AndInTheDarkness',
104 xbins=50, xmin=0, xmax=50, opt='kAlwaysCreate')
105 # make a TTree
106 myGroup.defineTree('pT,lb,pT_vec,strvec,str;testtree', path='BindThem',
107 treedef='pT/F:lb/i:pT_vec/vector<float>:strvec/vector<string>:str/string')
108
109 anotherGroup.defineHistogram('lbWithFilter',title='Lumi;lb;Events',
110 path='top',xbins=1000,xmin=-0.5,xmax=999.5)
111 anotherGroup.defineHistogram('run',title='Run Number;run;Events',
112 path='top',xbins=1000000,xmin=-0.5,xmax=999999.5)
113
114 # Example defining an array of histograms. This is useful if one seeks to create a
115 # number of histograms in an organized manner. (For instance, one plot for each ASIC
116 # in the subdetector, and these components are mapped in eta, phi, and layer.) Thus,
117 # one might have an array of TH1's such as quantity[etaIndex][phiIndex][layerIndex].
118 for alg in [exampleMonAlg, anotherExampleMonAlg]:
119 # Using an array of groups
120 topPath = 'OneRing' if alg == exampleMonAlg else 'top'
121 array = helper.addArray([2],alg,'ExampleMonitor', topPath=topPath)
122 array.defineHistogram('a,b',title='AB',type='TH2F',path='Eta',
123 xbins=10,xmin=0.0,xmax=10.0,
124 ybins=10,ymin=0.0,ymax=10.0)
125 array.defineHistogram('c',title='C',path='Eta',
126 xbins=10,xmin=0.0,xmax=10.0)
127 array = helper.addArray([4,2],alg,'ExampleMonitor', topPath=topPath)
128 array.defineHistogram('a',title='A',path='EtaPhi',
129 xbins=10,xmin=0.0,xmax=10.0)
130
131 # Using a map of groups
132 layerList = ['layer1', 'layer2']
133 clusterList = ['clusterX', 'clusterB']
134 array1D = helper.addArray([layerList], alg, 'ExampleMonitor', topPath=topPath)
135 array1D.defineHistogram('c', title='C', path='Layer',
136 xbins=10, xmin=0, xmax=10.0)
137 array2D = helper.addArray([layerList, clusterList], alg, 'ExampleMonitor', topPath=topPath)
138 array2D.defineHistogram('c', title='C', path='LayerCluster',
139 xbins=10, xmin=0, xmax=10.0)
140
141 # Using templates for histogram titles or paths
142 array1D.defineHistogram('c', title='Layer {0}', path='Keys', xmax=3.)
143 array1D.defineHistogram('c;c_alternate', title='Layer', path='Keys/{0}', xmax=3.)
144 array1D.defineHistogram('c;c_{0}_formatted', path='Keys', xmax=3.)
145 array2D.defineHistogram('c', title='Cluster {1}, Layer {0}', path='Keys/{1}', xmax=3.)
146
147 # Making a histogram only for certain elements of the array
148 array2D.defineHistogram('c;c_restricted', path='Keys', pattern=[('layer1', 'clusterB'), ('layer2', 'clusterX')])
149
150
155 return helper.result()
156
157 # # Otherwise, merge with result object and return
158 # acc = helper.result()
159 # result.merge(acc)
160 # return result
161
162if __name__=='__main__':
163 # Setup logs
164 from AthenaCommon.Logging import log
165 from AthenaCommon.Constants import INFO
166 log.setLevel(INFO)
167
168 # Set the Athena configuration flags
169 from AthenaConfiguration.AllConfigFlags import initConfigFlags
170 flags = initConfigFlags()
171 import sys
172 nightly = '/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art/CommonInputs/'
173 file = 'data16_13TeV.00311321.physics_Main.recon.AOD.r9264/AOD.11038520._000001.pool.root.1'
174 flags.Input.Files = [nightly+file]
175 flags.Input.isMC = False
176 flags.Output.HISTFileName = 'ExampleMonitorOutput.root'
177 flags.fillFromArgs(sys.argv[1:])
178
179 flags.lock()
180
181 # Initialize configuration object, add accumulator, merge, and run.
182 from AthenaConfiguration.MainServicesConfig import MainServicesCfg
183 from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
184 cfg = MainServicesCfg(flags)
185 cfg.merge(PoolReadCfg(flags))
186
187 exampleMonitorAcc = ExampleMonitoringConfig(flags)
188 cfg.merge(exampleMonitorAcc)
189
190 # If you want to turn on more detailed messages ...
191 # exampleMonitorAcc.getEventAlgo('ExampleMonAlg').OutputLevel = 2 # DEBUG
192 cfg.printConfig(withDetails=False) # set True for exhaustive info
193
194 cfg.run() #use cfg.run(20) to only run on first 20 events