3 __author__ =
"Marcin Nowak"
5 Create a sorted collection of Event references from a set of Athnea files containing EventInfoTags
6 (or from other compatible APR Event collections)
9 from CollectionUtilities.GetCollectionType
import getCollectionType
13 """Creates a sorted (APR) collection of event references from input files"""
16 from AthenaCommon
import Logging
17 Logging.log.name = name
19 self.
info = Logging.log.info
26 """import ROOT, create CollectionSvc"""
32 """read Collection Description and remember it"""
33 desc = collection.description()
34 self.
debug(
"Reading Collection Description from {}".
format(desc.name()))
36 for an
in range(0, desc.numberOfAttributeColumns()):
37 attr = desc.attributeColumn(an)
49 """read all input collections into memore"""
55 for inFileName
in inputCollections:
61 self.
info(
"Reading Events from {}".
format(inFileName))
63 query = iColl.newQuery()
65 cursor = query.execute()
68 row = cursor.currentRow()
72 t.append( row.attributeList()[nam].data[ self.
attrTypes[nam] ]() )
80 self.
info(
"Finished reading input collections, total events read: {}".
format(len(self.
allRows)) )
83 """sort the events based on an attribute name"""
84 self.
info(
"Sorting on attribute {}, sort order {}".
format(sortAttrName, (
"Descending" if sortReverse
else "Ascending") ))
87 self.
allRows.sort( key=
lambda t: t[attrPos], reverse=sortReverse )
94 """write sorted collection into a Collection file"""
95 self.
info(
"Writing Event collection {}".
format(outputCollection))
100 row = self.
pool.CollectionRowBuffer()
101 dstColl.initNewRow( row )
104 for idx,nam
in enumerate(self.
attrNames):
106 row.attributeList()[nam].setValue[type]( t[idx+1] )
107 dstColl.insertRow( row )
112 def execute(self, inputCollections, outputCollection="PFN:collection.root
", sortAttribute="LumiBlockN
",
113 sortOrder="Ascending", outputCollectionType=
"RootCollection"):
114 sort_opts = (
"Ascending",
"Descending")
115 self.
info(
"Executing SortedCollectionCreator, inputs={}, output='{}' ({}), sort by: {}, order: {}"
116 .
format(inputCollections, outputCollection, outputCollectionType, sortAttribute, sortOrder))
117 if isinstance(inputCollections, str):
118 inputs = [inputCollections]
120 inputs = inputCollections
121 if sortOrder.lower()
not in [opt.lower()
for opt
in sort_opts]:
122 raise Exception(self.
name +
": Accepted sortOrder values are: " +
str(sort_opts))
123 sortReverse = ( sortOrder.lower()[0] ==
"d" )