Esempio n. 1
0
def convert_to_calorimeterevent(inputFileName, outputFileName, Row2X_lut_left,
                                Row2X_lut_right, Column2Y_lut_left,
                                Column2Y_lut_right, Z_lut, lane_lut, IsLeft):
    #check flag setting, as position won't be written if LCIO::CHBIT_LONG not set...
    flag = IMPL.LCFlagImpl()
    flag.setBit(EVENT.LCIO.CHBIT_LONG)

    #Create a reader to parse the input file
    reader = LcioReader(inputFileName)

    #create a writer to write to the output file
    writer = IOIMPL.LCFactory.getInstance().createLCWriter()
    writer.open(outputFileName, EVENT.LCIO.WRITE_NEW)

    #create indexes for progress tracking...
    index = 0.
    q = int(0)

    #get the root tree...
    file = TFile(inputFileName, "read")
    tree = file.Get("Frames")

    #loop over all entries in the tree
    for i in range(0, tree.GetEntries()):
        #if index>=10:
        #    break

        index += 1.

        tree.GetEntry(i)
        Lane = tree.lane  #vector
        Row = tree.row  #vector
        Column = tree.column  #vector
        Event = tree.eventNumber
        File = tree.fileNumber
        Run = tree.runNumber

        # ++ Lane, Row and Column should be the same length. Let's just check that they are:
        if (len(Lane) != len(Row)) or (len(Lane) != len(Column)):
            print "ERROR +++++++++++++++++++++++++++++++++++++          Row vector length", len(
                Row), ", Column vector length", len(
                    Column), "and Lane vector length", len(
                        Lane), "aren't equal!"

        #create a new event
        newEvent = IMPL.LCEventImpl()
        newEvent.setEventNumber(Event)
        newEvent.setRunNumber(Run)

        #Create a new collection to add to the new events, now made from CalorimeterHits.
        CaloHits = IMPL.LCCollectionVec(EVENT.LCIO.CALORIMETERHIT)

        #add flag to CaloHits so we can store positions...
        CaloHits.setFlag(flag.getFlag())

        #Create an encoder for the new events...
        encodingString = str("lane:7,row:11,column:11")
        idEncoder = UTIL.CellIDEncoder(IMPL.CalorimeterHitImpl)(encodingString,
                                                                CaloHits)

        #Loop over all entries in the tree...
        for j in range(0, len(Lane)):
            #get the chipID from the lane...
            chipID = inverse_lane_lut[Lane[j]]

            #get x, y and z.
            if (IsLeft[chipID]):
                x = Row2X_lut_left[Row[j]]
                y = Column2Y_lut_left[Column[j]]

            else:
                x = Row2X_lut_right[Row[j]]
                y = Column2Y_lut_right[Column[j]]

            z = Z_lut[chipID]

            #Make new calorimeter hit:
            newHit = IMPL.CalorimeterHitImpl()

            #add the lane, column, row to the new collection...
            idEncoder.reset()
            idEncoder['lane'] = Lane[j]
            idEncoder['row'] = Row[j]
            idEncoder['column'] = Column[j]
            idEncoder.setCellID(newHit)

            #add x, y, z to the new collection...
            Position = TVector3(x, y, z)
            newHit.setPositionVec(Position)

            #add hits to collection
            CaloHits.addElement(newHit)

            #end of hit loop

        #add collection to event
        newEvent.addCollection(CaloHits, 'ECalBarrelCollection')

        #write the event to the output file
        writer.writeEvent(newEvent)

        #print percentage of progress (but not too often!)
        p = int((float(index) / float(tree.GetEntries() * 100)))
        progress = str(p) + '%'
        if (p != q):
            print "Progress:", progress
            q = int(p)

        #end of event loop

    #close the writer
    writer.flush()
    writer.close()
Esempio n. 2
0
def convert_to_calorimeterevent(inputFileName, outputFileName, Row2X_lut_left,
                                Row2X_lut_right, Column2Y_lut_left,
                                Column2Y_lut_right, Z_lut, lane_lut, IsLeft):
    #check flag setting, as position won't be written if LCIO::CHBIT_LONG not set...
    flag = IMPL.LCFlagImpl()
    flag.setBit(EVENT.LCIO.CHBIT_LONG)

    #Create a reader to parse the input file
    reader = LcioReader(inputFileName)

    #create a writer to write to the output file
    writer = IOIMPL.LCFactory.getInstance().createLCWriter()
    writer.open(outputFileName, EVENT.LCIO.WRITE_NEW)

    #create indexes for progress tracking...
    index = 0.
    q = int(0)

    for oldEvent in reader:
        #if index>=10:
        #    break

        index += 1.

        #create a new event and copy its parameters
        newEvent = IMPL.LCEventImpl()
        newEvent.setEventNumber(oldEvent.getEventNumber())
        newEvent.setRunNumber(oldEvent.getRunNumber())

        #Create a new collection to add to the new events, now made from CalorimeterHits.
        CaloHits = IMPL.LCCollectionVec(EVENT.LCIO.CALORIMETERHIT)

        #add flag to CaloHits so we can store positions...
        CaloHits.setFlag(flag.getFlag())

        #Create both an encoder for the new events, and a decoder for the old ones...
        encodingString = str("lane:7,row:11,column:11")
        idEncoder = UTIL.CellIDEncoder(IMPL.CalorimeterHitImpl)(encodingString,
                                                                CaloHits)
        idDecoder = UTIL.CellIDDecoder(
            IMPL.RawCalorimeterHitImpl)(encodingString)

        #get the raw calorimeter hits...
        RawCaloHits = oldEvent.getCollection('RawCalorimeterHits')

        #Loop over the RawCalorimeterHits
        for oldHit in RawCaloHits:
            #get the row, column and lane from the raw calorimter hits
            lane = idDecoder(oldHit)["lane"].value()
            row = idDecoder(oldHit)["row"].value()
            column = idDecoder(oldHit)["column"].value()

            #get the chipID from the lane...
            chipID = inverse_lane_lut[lane]

            #get x, y and z.
            if (IsLeft[chipID]):
                x = Row2X_lut_left[row]
                y = Column2Y_lut_left[column]

            else:
                x = Row2X_lut_right[row]
                y = Column2Y_lut_right[column]

            z = Z_lut[chipID]

            #Make new calorimeter hit:
            newHit = IMPL.CalorimeterHitImpl()

            #add the lane, column, row to the new collection...
            idEncoder.reset()
            idEncoder['lane'] = lane
            idEncoder['row'] = row
            idEncoder['column'] = column
            idEncoder.setCellID(newHit)

            #add x, y, z to the new collection...
            Position = TVector3(x, y, z)
            newHit.setPositionVec(Position)

            #add hits to collection
            CaloHits.addElement(newHit)

            #end of hit loop

        #add collection to event
        newEvent.addCollection(CaloHits, 'ECalBarrelCollection')

        #write the event to the output file
        writer.writeEvent(newEvent)

        #print percentage of progress (but not too often!)
        p = int((float(index) / float(reader.getNumberOfEvents())) * 100)
        progress = str(p) + '%'
        if (p != q):
            print "Progress:", progress
            q = int(p)

        #end of event loop

    #close the writer
    writer.flush()
    writer.close()