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