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"""
30 self.
collSvc.setMessageSvcQuiet()
33 """read Collection Description and remember it"""
34 desc = collection.description()
35 self.
debug(
"Reading Collection Description from {}".
format(desc.name()))
37 for an
in range(0, desc.numberOfAttributeColumns()):
38 attr = desc.attributeColumn(an)
50 """read all input collections into memore"""
56 for inFileName
in inputCollections:
62 self.
info(
"Reading Events from {}".
format(inFileName))
64 query = iColl.newQuery()
66 cursor = query.execute()
69 row = cursor.currentRow()
73 t.append( row.attributeList()[nam].data[ self.
attrTypes[nam] ]() )
81 self.
info(
"Finished reading input collections, total events read: {}".
format(len(self.
allRows)) )
84 """sort the events based on an attribute name"""
85 self.
info(
"Sorting on attribute {}, sort order {}".
format(sortAttrName, (
"Descending" if sortReverse
else "Ascending") ))
88 self.
allRows.sort( key=
lambda t: t[attrPos], reverse=sortReverse )
95 """write sorted collection into a Collection file"""
96 self.
info(
"Writing Event collection {}".
format(outputCollection))
101 row = self.
pool.CollectionRowBuffer()
102 dstColl.initNewRow( row )
105 for idx,nam
in enumerate(self.
attrNames):
107 row.attributeList()[nam].setValue[type]( t[idx+1] )
108 dstColl.insertRow( row )
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]
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" )
131 import multiprocessing
132 process = multiprocessing.Process( target=self.
execute, args=args, kwargs=kwargs)
133 self.
debug(
"Sorting Events in a subprocess")
136 return process.exitcode