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)
11 """Creates a sorted (APR) collection of event references from input files"""
14 from AthenaCommon
import Logging
15 Logging.log.name = name
17 self.
info = Logging.log.info
28 """import ROOT, create CollectionSvc"""
30 self.
collSvc = ROOT.pool.CollectionService()
33 """read Collection Description and remember it"""
34 desc = collection.description()
35 self.
debug(
"Reading Collection Description from {}".
format(desc.name()))
38 for an
in range(0, desc.numberOfAttributeColumns()):
39 attr = desc.attributeColumn(an)
43 self.
tokenName = desc.eventReferenceColumnName()
51 """read all input collections into memore"""
53 for inFileName
in inputCollections:
55 iColl = self.
collSvc.
open(
"PFN:"+inFileName,
"RootCollection")
59 self.
info(
"Reading Events from {}".
format(inFileName))
61 query = iColl.newQuery()
63 cursor = query.execute()
66 row = cursor.currentRow()
70 t.append( row.attributeList()[nam].data[ self.
attrTypes[nam] ]() )
78 self.
info(
"Finished reading input collections, total events read: {}".
format(len(self.
allRows)) )
81 """sort the events based on an attribute name"""
82 self.
info(
"Sorting on attribute {}, sort order {}".
format(sortAttrName, (
"Descending" if sortReverse
else "Ascending") ))
85 self.
allRows.sort( key=
lambda t: t[attrPos], reverse=sortReverse )
92 """write sorted collection into a Collection file"""
93 self.
info(
"Writing Event collection {} ".
format(outputCollection+
".root"))
100 row = dstColl.dataEditor().rowBuffer()
102 for idx,nam
in enumerate(self.
attrNames):
104 row.attributeList()[nam].setValue[type]( t[idx+1] )
105 dstColl.dataEditor().insertRow( row )
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]
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" )