ATLAS Offline Software
SortedCollectionCreator.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 __author__ = "Marcin Nowak"
4 __doc__ = """
5 Create a sorted collection of Event references from a set of Athnea files containing EventInfoTags
6 (or from other compatible APR Event collections)
7 """
8 
9 
11  """Creates a sorted (APR) collection of event references from input files"""
12 
13  def __init__(self, name= "sortEvents"):
14  from AthenaCommon import Logging
15  Logging.log.name = name
16  self.name = name
17  self.info = Logging.log.info
18  self.debug = Logging.log.debug
19  self.verbose = Logging.log.verbose
20  self.collDescription = None
21  self.allRows = []
22  self.attrNames = []
23  self.attrTypes = {}
24  self.tokenName = None
25  self.collSvc = None
26 
27  def loadRoot(self):
28  """import ROOT, create CollectionSvc"""
29  import ROOT
30  self.collSvc = ROOT.pool.CollectionService()
31 
32  def readCollectionDescription(self, collection):
33  """read Collection Description and remember it"""
34  desc = collection.description()
35  self.debug("Reading Collection Description from {}".format(desc.name()))
36  import ROOT
37  # read attributes' names and types from the description and remember them
38  for an in range(0, desc.numberOfAttributeColumns()):
39  attr = desc.attributeColumn(an)
40  name = attr.name()
41  self.attrNames.append(name)
42  self.attrTypes[name] = attr.type()
43  self.tokenName = desc.eventReferenceColumnName()
44  # make a local copy of the description
45  self.collDescription = ROOT.pool.CollectionDescription( desc )
46  # prevent overwriting input by accident
47  self.collDescription.setName("")
48 
49 
50  def readInputCollections(self, inputCollections):
51  """read all input collections into memore"""
52  self.collDescription = None
53  for inFileName in inputCollections:
54  self.debug("Opening {}".format(inFileName))
55  iColl = self.collSvc.open( "PFN:"+inFileName, "RootCollection")
56  self.debug("{} opened".format(inFileName))
57  if self.collDescription is None:
58  self.readCollectionDescription(iColl)
59  self.info("Reading Events from {}".format(inFileName))
60 
61  query = iColl.newQuery()
62  query.selectAll()
63  cursor = query.execute()
64 
65  while cursor.next():
66  row = cursor.currentRow()
67  # put the token first in the attribute list, for convenience
68  t = [ row.tokenList()[self.tokenName].toString() ]
69  for nam in self.attrNames:
70  t.append( row.attributeList()[nam].data[ self.attrTypes[nam] ]() )
71  self.allRows.append(t)
72 
73  iColl.close()
74 
75  for t in self.allRows:
76  self.verbose( t[1:], t[0])
77  self.verbose('='*80)
78  self.info("Finished reading input collections, total events read: {}".format(len(self.allRows)) )
79 
80  def sortEvents(self, sortAttrName, sortReverse=False):
81  """sort the events based on an attribute name"""
82  self.info("Sorting on attribute {}, sort order {}".format(sortAttrName, ("Descending" if sortReverse else "Ascending") ))
83  # add 1 to offsets because the Ref is first
84  attrPos = self.attrNames.index(sortAttrName)+1
85  self.allRows.sort( key=lambda t: t[attrPos], reverse=sortReverse )
86 
87  for t in self.allRows:
88  self.verbose( t[1:], t[0] )
89  self.verbose('='*80)
90 
91  def writeCollection(self, outputCollection):
92  """write sorted collection into a Collection file"""
93  self.info("Writing Event collection {} ".format(outputCollection+".root"))
94  self.collDescription.setName(outputCollection)
95  self.collDescription.setType("RootCollection")
96  # create the output collection (file)
97  dstColl = self.collSvc.create(self.collDescription)
98 
99  for t in self.allRows:
100  row = dstColl.dataEditor().rowBuffer()
101  row.tokenList()[0].fromString( t[0] )
102  for idx,nam in enumerate(self.attrNames):
103  type = self.attrTypes[nam]
104  row.attributeList()[nam].setValue[type]( t[idx+1] )
105  dstColl.dataEditor().insertRow( row )
106 
107  dstColl.commit()
108  dstColl.close()
109 
110  def execute(self, inputCollections, outputCollection="PFN:collection.root", sortAttribute="LumiBlockN", sortOrder="Ascending"):
111  sort_opts = ("Ascending", "Descending")
112  self.info("Executing SortedCollectionCreator, inputs={}, output='{}', sort by: {}, order: {}"
113  .format(inputCollections, outputCollection, sortAttribute, sortOrder))
114  if isinstance(inputCollections, str):
115  inputs = [inputCollections]
116  else:
117  inputs = inputCollections
118  if sortOrder.lower() not in [opt.lower() for opt in sort_opts]:
119  raise Exception(self.name + ": Accepted sortOrder values are: " + str(sort_opts))
120  sortReverse = ( sortOrder.lower()[0] == "d" )
121  self.loadRoot()
122  self.readInputCollections(inputs)
123  self.sortEvents(sortAttribute, sortReverse)
124  self.writeCollection(outputCollection)
125 
126 
python.SortedCollectionCreator.SortedCollectionCreator.readCollectionDescription
def readCollectionDescription(self, collection)
Definition: SortedCollectionCreator.py:32
vtune_athena.format
format
Definition: vtune_athena.py:14
python.SortedCollectionCreator.SortedCollectionCreator.__init__
def __init__(self, name="sortEvents")
Definition: SortedCollectionCreator.py:13
python.SortedCollectionCreator.SortedCollectionCreator.attrTypes
attrTypes
Definition: SortedCollectionCreator.py:23
index
Definition: index.py:1
python.SortedCollectionCreator.SortedCollectionCreator.tokenName
tokenName
Definition: SortedCollectionCreator.py:24
python.SortedCollectionCreator.SortedCollectionCreator.readInputCollections
def readInputCollections(self, inputCollections)
Definition: SortedCollectionCreator.py:50
python.SortedCollectionCreator.SortedCollectionCreator.verbose
verbose
Definition: SortedCollectionCreator.py:19
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.SortedCollectionCreator.SortedCollectionCreator
Definition: SortedCollectionCreator.py:10
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.SortedCollectionCreator.SortedCollectionCreator.debug
debug
Definition: SortedCollectionCreator.py:18
PixelConditionsData::fromString
T fromString(const std::string &s)
Definition: PixelConditionsDataStringUtils.h:14
python.SortedCollectionCreator.SortedCollectionCreator.info
info
Definition: SortedCollectionCreator.py:17
python.SortedCollectionCreator.SortedCollectionCreator.writeCollection
def writeCollection(self, outputCollection)
Definition: SortedCollectionCreator.py:91
python.SortedCollectionCreator.SortedCollectionCreator.sortEvents
def sortEvents(self, sortAttrName, sortReverse=False)
Definition: SortedCollectionCreator.py:80
python.SortedCollectionCreator.SortedCollectionCreator.loadRoot
def loadRoot(self)
Definition: SortedCollectionCreator.py:27
python.SortedCollectionCreator.SortedCollectionCreator.collDescription
collDescription
Definition: SortedCollectionCreator.py:20
Trk::open
@ open
Definition: BinningType.h:40
python.SortedCollectionCreator.SortedCollectionCreator.execute
def execute(self, inputCollections, outputCollection="PFN:collection.root", sortAttribute="LumiBlockN", sortOrder="Ascending")
Definition: SortedCollectionCreator.py:110
python.SortedCollectionCreator.SortedCollectionCreator.allRows
allRows
Definition: SortedCollectionCreator.py:21
python.SortedCollectionCreator.SortedCollectionCreator.name
name
Definition: SortedCollectionCreator.py:16
str
Definition: BTagTrackIpAccessor.cxx:11
python.SortedCollectionCreator.SortedCollectionCreator.attrNames
attrNames
Definition: SortedCollectionCreator.py:22
python.SortedCollectionCreator.SortedCollectionCreator.collSvc
collSvc
Definition: SortedCollectionCreator.py:25