ATLAS Offline Software
LumiQuery.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
2 
3 # module LumiQuery
4 from __future__ import print_function
5 from past.builtins import long
6 
7 """ Simple module for interaction with the luminosity metadata in a file. """
8 
9 global LumiRangeList
10 LumiRangeList = []
11 
12 class PyRunLumiP1(long):
13  """ Python (run,block)/(run,event) type object based on the LumiBlockRange_p1 persistency format.
14 
15  LumiBlockRanges are stored as encoded run and (luminosity) block
16  numbers. Since the conventional run-based accessors for IOVTime
17  are run() and event(), event() is provided *as a synonym* for block.
18 
19  This class is initialized with an IOVTime *as stored* in the
20  persistent object LumiBlockRange_p1 (Note, it does not work on the
21  transient object!)
22 
23  Warning: although PyRunLumiP1 inherits from long, addition and
24  subtraction are not well defined and should be treated with care!
25  Nevertheless, *incrementing* a PyRunLumiP1 gives a basically intuitive
26  result.)
27 
28  """
29  def run(self):
30  return int(self >> 32)
31  def block(self):
32  return int(self & 0xFFFFFFFF)
33  def event(self):
34  return self.block()
35  def __str__(self):
36  return 'R='+ str(self.run()) + '; LB=' + str(self.event())
37  def __repr__(self):
38  return self.__hex__().__repr__() + '->(' + repr(self.run()) + ':' + repr(self.event()) + ')'
39 
40 
41 
42 def ListFromFile(filename="FewEventsLumi.pool.root", label = "LumiBlockCollection_p1_LumiBlocks", metadatatree="MetaData"):
43  """ Returns a list of RunLumiBlock ranges (stored as python tuples of type PyRunLumiP1) from the named file.
44 
45  The file should have metadata stored internally as a ROOT tree
46  (named 'Stream' by default). It can be a POOL file or other generic
47  ntuple file.
48 
49  Note: This function does not access the MetaDataStore. Please use
50  a LumiBlock AlgTool for that.
51  """
52 
53  import ROOT
54  a = ROOT.TFile.Open(filename)
55  global LumiRangeList
56  LumiRangeList = []
57  try:
58  T = a.Get(metadatatree)
59  T.SetBranchStatus("*",0)
60  T.SetBranchStatus(label,1)
61  T.SetBranchStatus("vector<LumiBlockRange_p1>",1)
62  T.SetBranchStatus("vector<LumiBlockRange_p1>.m_start",1)
63  T.SetBranchStatus("vector<LumiBlockRange_p1>.m_stop",1)
64  T.GetEntry(0)
65  except AttributeError:
66  print("There is no tree named", metadatatree, "in file" ,filename)
67  if a.__repr__() != 'None':
68  print(8*'--', "Contents:", 8*'--')
69  a.ls()
70  return
71 
72 
76  try:
77  iterRange = getattr( T, label ).size() #T.GetLeaf("vector<LumiBlockRange_p1>.m_start").GetLen()
78  except AttributeError as a:
79  print(a)
80  print("The tree", metadatatree, "in file" ,filename, "has no vector<LumiBlockRange_p1>.m_start branch, so I don't understand its format.")
81  return
82  for i in range(iterRange):
83  LumiRangeList += [ (PyRunLumiP1( getattr( T, label )[i].m_start ),PyRunLumiP1( getattr( T, label )[i].m_stop ))]
84  return [ x for x in LumiRangeList ]
85 
86 
87 def PrintList():
88  """ An example function. It prints the list returned by ListFromFile. """
89  global LumiRangeList
90  for (x,y) in LumiRangeList: print(x, 'to', y)
91 
92 
93 if __name__ == '__main__':
94  import sys
95  ll1 = ListFromFile( sys.argv[1], "LumiBlockCollection_p1_LumiBlocks", "MetaData" )
96  ll2 = ListFromFile( sys.argv[1], "LumiBlockCollection_p1_IncompleteLumiBlocks", "MetaData" )
python.LumiQuery.ListFromFile
def ListFromFile(filename="FewEventsLumi.pool.root", label="LumiBlockCollection_p1_LumiBlocks", metadatatree="MetaData")
Definition: LumiQuery.py:42
python.LumiQuery.PyRunLumiP1.block
def block(self)
Definition: LumiQuery.py:31
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.LumiQuery.PyRunLumiP1
Definition: LumiQuery.py:12
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
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
python.LumiQuery.PyRunLumiP1.__repr__
def __repr__(self)
Definition: LumiQuery.py:37
python.LumiQuery.PyRunLumiP1.event
def event(self)
Definition: LumiQuery.py:33
python.LumiQuery.PyRunLumiP1.__str__
def __str__(self)
Definition: LumiQuery.py:35
python.LumiQuery.PyRunLumiP1.run
def run(self)
Definition: LumiQuery.py:29
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
str
Definition: BTagTrackIpAccessor.cxx:11
python.LumiQuery.PrintList
def PrintList()
Definition: LumiQuery.py:87