ATLAS Offline Software
FilterUtils.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2 
3 from __future__ import print_function
4 
5 """
6 This library defines different classes for filtering events in athena.
7 Intended for use in JobRunner templates.
8 """
9 __author__ = 'Juerg Beringer'
10 __version__ = '$Id $'
11 
12 
13 # General setup
14 from AthenaCommon.AlgSequence import AthSequencer
15 # Leave it named 'filterSeq' for now to not
16 # break JOs importing this file. Should be changed
17 # to masterSeq to avoid confusion...
18 filterSeq = AthSequencer("AthMasterSeq")
19 from AthenaPython.PyAthena import StatusCode
20 import AthenaPython.PyAthena as PyAthena
21 
22 
23 #
24 # Event filtering based on BCID
25 #
27  def __init__(self,bcidList,name='BCIDFilter'):
28  super(BCIDFilter,self).__init__(name=name)
29  self.bcidList = bcidList
30  return
31 
32  def initialize(self):
33  print ('BCIDFilter: Found %i BCIDs to accept: ' % len(self.bcidList), self.bcidList)
34  if not self.bcidList:
35  print ('BCIDFilter: WARNING: Empty bcidList - will accept all events')
36  self.sg = PyAthena.py_svc('StoreGateSvc')
37  return StatusCode.Success
38 
39  def execute(self):
40  if self.bcidList:
41  eventID = self.sg.retrieve('EventInfo','ByteStreamEventInfo').event_ID()
42  bcid = eventID.bunch_crossing_id()
43  #print (bcid, bcid in self.bcidList)
44  self.setFilterPassed(bcid in self.bcidList)
45  else:
46  self.setFilterPassed(True)
47  return StatusCode.Success
48 
49  def finalize(self):
50  return StatusCode.Success
51 
52 
53 #
54 # Event filtering based on LB number
55 #
57  def __init__(self,lbList,name='LBFilter'):
58  super(LBFilter,self).__init__(name=name)
59  self.lbList = lbList
60  self._eventsSeen = 0
61  self.OutputLevel = 4
62  return
63 
64  def initialize(self):
65  print ('LBFilter: Found %i LBs to accept: ' % len(self.lbList), self.lbList)
66  if not self.lbList:
67  print ('LBFilter: WARNING: Empty lbList - will accept all events')
68  self.sg = PyAthena.py_svc('StoreGateSvc')
69  return StatusCode.Success
70 
71  def execute(self):
72  self._eventsSeen += 1
73  self.msg.debug('LBFilter exe %d', self._eventsSeen)
74  for item in self.sg.keys():
75  if 'EventInfo' in item:
76  self.msg.debug(item)
77  if self.lbList:
78  if 'EventInfo' in self.sg.keys():
79  eventInfo = self.sg.retrieve('EventInfo')
80  lb = eventInfo.event_ID().lumi_block()
81  self.msg.debug( 'LBFilter %d %d', lb, eventInfo.event_ID().event_number() )
82  self.setFilterPassed(lb in self.lbList)
83  else:
84  self.msg.info( 'LBFilter : no LB info' )
85  self.setFilterPassed(True)
86  else:
87  self.setFilterPassed(True)
88  return StatusCode.Success
89 
90  def finalize(self):
91  return StatusCode.Success
92 
93 
94 
95 # Event filtering based on LB number
96 #
98  def __init__(self,name='LBPrinter'):
99  super(LBPrinter,self).__init__(name=name)
100  return
101 
102  def initialize(self):
103  print ('LBPrinter: Init')
104  self.sg = PyAthena.py_svc('StoreGateSvc')
105  self._eventsSeen = 0
106  self.OutputLevel = 1
107  return StatusCode.Success
108 
109  def execute(self):
110  self._eventsSeen += 1
111  self.msg.info( 'LBPrinter exe %d', self._eventsSeen )
112  for item in self.sg.keys():
113  if 'EventInfo' in item:
114  self.msg.info( item )
115  if 'EventInfo' in self.sg.keys():
116  eventInfo = self.sg.retrieve('EventInfo')
117  lb = eventInfo.event_ID().lumi_block()
118  self.msg.info( 'LBPrinter %d %d' , lb, eventInfo.event_ID().event_number() )
119  else:
120  self.msg.info( 'LBPrinter : no LB info' )
121  return StatusCode.Success
122 
123  def finalize(self):
124  return StatusCode.Success
125 
126 
127 #
128 # LumiScan filter for filter and setting pseudo-LB numbers
129 #
130 from time import asctime,gmtime
131 
132 class LbInfo:
133  def __init__(self,line):
134  self.line = line.strip()
135  l = line.split()
136  self.lbNumber = int(l[0])
137  #self.point = int(l[0])
138  self.startTime = int(l[1])
139  self.endTime = int(l[2])
140  self.separation = float(l[3])
141 
142  def __repr__(self):
143  #return self.line
144  s = 'Point %2d: %s - %s UTC (deltaT = %5.2f s), nom.sep. = %f' % (self.lbNumber,asctime(gmtime(self.startTime/1e9)),asctime(gmtime(self.endTime/1e9)),(self.endTime-self.startTime)/1.e9,self.separation)
145  return s
146 
147  def match(self,sec,nsec):
148  # Time now in nsec
149  t = sec*1e9 + nsec
150  #t = sec*1e3 + nsec/1e6
151  return (t>=self.startTime) and (t<self.endTime)
152 
153  def isScanPoint(self):
154  #return (self.endTime-self.startTime > 25e9)
155  return True
156 
158  def __init__(self,lbData,name='LumiBlockFilter'):
159  super(LumiBlockFilter,self).__init__(name=name)
160  self.lbList = []
161 
162  # Treat lbData as a file containing the pLB info if it is a string or a direct list of pLB info if it is a list
163  lbInfo = open(lbData) if isinstance(lbData,str) else lbData
164 
165  for l in lbInfo:
166  if l[0]=='#': continue
167  self.lbList.append(LbInfo(l))
168 
169  if hasattr (lbInfo, 'close'):
170  lbInfo.close()
171 
172  print()
173  print ('Found',len(self.lbList),'(pseudo)LB entries:')
174  for i in range(len(self.lbList)):
175  print ('Data for new LB %3i: ' % self.lbList[i].lbNumber, self.lbList[i])
176  print()
177  return
178 
179  def initialize(self):
180  self.sg = PyAthena.py_svc('StoreGateSvc')
181  return StatusCode.Success
182 
183  def execute(self):
184  eventID = self.sg.retrieve('EventInfo','ByteStreamEventInfo').event_ID()
185  foundEvent = False
186  for lb in self.lbList:
187  if lb.match(eventID.time_stamp(),eventID.time_stamp_ns_offset()) and lb.isScanPoint():
188  #print ('Match found with LB %3i (data: %s)' % (lb.lbNumber,lb))
189  eventID.set_lumi_block(lb.lbNumber)
190  foundEvent = True
191  break
192  self.setFilterPassed(foundEvent)
193  return StatusCode.Success
194 
195  def finalize(self):
196  return StatusCode.Success
197 
198 
199 #
200 # Event filtering based on z of primary vertex position
201 #
203  def __init__(self,zRange,name='ZFilter'):
204  super(ZFilter,self).__init__(name=name)
205  self.zRange = zRange
206  self.zMin = zRange[0]
207  self.zMax = zRange[1]
208  return
209 
210  def initialize(self):
211  print ('ZFilter: accepting only primary vertices from',self.zMin,'to',self.zMax)
212  self.sg = PyAthena.py_svc('StoreGateSvc')
213  return StatusCode.Success
214 
215  def execute(self):
216  vxContainer = self.sg.retrieve("VxContainer","VxPrimaryCandidate")
217  #print ('vxContainer size =',vxContainer.size())
218  accept = False
219  for i in range(vxContainer.size()):
220  if vxContainer[i].vertexType() != 1: continue # Only use primary vertex candidates
221  z = vxContainer[i].recVertex().position().z()
222  accept = (z >= self.zMin) and (z <= self.zMax)
223  #print (z, accept)
224  break
225  self.setFilterPassed(accept)
226  return StatusCode.Success
227 
228  def finalize(self):
229  return StatusCode.Success
grepfile.info
info
Definition: grepfile.py:38
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
python.FilterUtils.LumiBlockFilter.sg
sg
Definition: FilterUtils.py:180
python.FilterUtils.LBPrinter.__init__
def __init__(self, name='LBPrinter')
Definition: FilterUtils.py:98
python.FilterUtils.LbInfo.__repr__
def __repr__(self)
Definition: FilterUtils.py:142
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.FilterUtils.ZFilter.sg
sg
Definition: FilterUtils.py:212
python.FilterUtils.ZFilter.zMin
zMin
Definition: FilterUtils.py:206
python.FilterUtils.LBPrinter
Definition: FilterUtils.py:97
python.FilterUtils.ZFilter.zMax
zMax
Definition: FilterUtils.py:207
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
python.FilterUtils.LbInfo.isScanPoint
def isScanPoint(self)
Definition: FilterUtils.py:153
python.FilterUtils.BCIDFilter.__init__
def __init__(self, bcidList, name='BCIDFilter')
Definition: FilterUtils.py:27
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.FilterUtils.LbInfo.separation
separation
Definition: FilterUtils.py:140
python.FilterUtils.LbInfo.startTime
startTime
Definition: FilterUtils.py:138
python.FilterUtils.LBFilter.OutputLevel
OutputLevel
Definition: FilterUtils.py:61
python.FilterUtils.LBFilter._eventsSeen
_eventsSeen
Definition: FilterUtils.py:60
python.FilterUtils.LbInfo.__init__
def __init__(self, line)
Definition: FilterUtils.py:133
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
python.FilterUtils.LBFilter
Definition: FilterUtils.py:56
python.FilterUtils.LBFilter.__init__
def __init__(self, lbList, name='LBFilter')
Definition: FilterUtils.py:57
python.FilterUtils.LBPrinter._eventsSeen
_eventsSeen
Definition: FilterUtils.py:105
python.FilterUtils.LumiBlockFilter
Definition: FilterUtils.py:157
python.FilterUtils.BCIDFilter
Definition: FilterUtils.py:26
python.FilterUtils.LBPrinter.sg
sg
Definition: FilterUtils.py:104
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.FilterUtils.LbInfo.line
line
Definition: FilterUtils.py:134
python.FilterUtils.LumiBlockFilter.__init__
def __init__(self, lbData, name='LumiBlockFilter')
Definition: FilterUtils.py:158
python.FilterUtils.LbInfo.lbNumber
lbNumber
Definition: FilterUtils.py:136
python.FilterUtils.LbInfo.endTime
endTime
Definition: FilterUtils.py:139
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
python.FilterUtils.BCIDFilter.sg
sg
Definition: FilterUtils.py:36
xAOD::vertexType
vertexType
Definition: Vertex_v1.cxx:166
python.FilterUtils.ZFilter.__init__
def __init__(self, zRange, name='ZFilter')
Definition: FilterUtils.py:203
python.FilterUtils.ZFilter.zRange
zRange
Definition: FilterUtils.py:205
Trk::open
@ open
Definition: BinningType.h:40
python.AlgSequence.AthSequencer
AthSequencer
Definition: Control/AthenaCommon/python/AlgSequence.py:64
python.FilterUtils.LbInfo.match
def match(self, sec, nsec)
Definition: FilterUtils.py:147
python.FilterUtils.LumiBlockFilter.lbList
lbList
Definition: FilterUtils.py:160
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
python.FilterUtils.LbInfo
Definition: FilterUtils.py:132
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
PyAthena::Alg
Definition: PyAthenaAlg.h:33
python.FilterUtils.LBFilter.lbList
lbList
Definition: FilterUtils.py:59
python.FilterUtils.BCIDFilter.bcidList
bcidList
Definition: FilterUtils.py:29
readCCLHist.float
float
Definition: readCCLHist.py:83
python.FilterUtils.ZFilter
Definition: FilterUtils.py:202
python.FilterUtils.LBFilter.sg
sg
Definition: FilterUtils.py:68
python.FilterUtils.LBPrinter.OutputLevel
OutputLevel
Definition: FilterUtils.py:106