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()
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()
def write_to_lcio(showers, energy, model_name, outfile, N): status_text = st.empty() status_text.text("Writing to LCIO files...") progress_bar = st.progress(0) ## get the dictionary f = open('cell_maps/cell-map_HCAL.pickle', 'rb') cmap = pickle.load(f) #pbar_cache = pkbar.Pbar(name='Writing to lcio files', target=N) wrt = IOIMPL.LCFactory.getInstance().createLCWriter() wrt.open(outfile, EVENT.LCIO.WRITE_NEW) random.seed() #========== MC particle properties =================== genstat = 1 charge = 1 mass = 1.40e-01 #decayLen = 1.e32 pdg = 211 # write a RunHeader run = IMPL.LCRunHeaderImpl() run.setRunNumber(0) run.parameters().setValue("Generator", model_name) run.parameters().setValue("PDG", pdg) wrt.writeRunHeader(run) for j in range(0, N): ### MC particle Collections colmc = IMPL.LCCollectionVec(EVENT.LCIO.MCPARTICLE) ## we are shooting 90 deg. HCAL px = 0.00 py = energy[j] pz = 0.00 vx = 30.00 vy = 1000.00 vz = 1000.00 epx = 50.00 epy = 3000.00 epz = 1000.00 momentum = arr.array('f', [px, py, pz]) vertex = arr.array('d', [vx, vy, vz]) endpoint = arr.array('d', [epx, epy, epz]) mcp = IMPL.MCParticleImpl() mcp.setGeneratorStatus(genstat) mcp.setMass(mass) mcp.setPDG(pdg) mcp.setMomentum(momentum) mcp.setCharge(charge) mcp.setVertex(vertex) mcp.setEndpoint(endpoint) colmc.addElement(mcp) evt = IMPL.LCEventImpl() evt.setEventNumber(j) evt.addCollection(colmc, "MCParticle") ### Calorimeter Collections col = IMPL.LCCollectionVec(EVENT.LCIO.SIMCALORIMETERHIT) flag = IMPL.LCFlagImpl(0) flag.setBit(EVENT.LCIO.CHBIT_LONG) flag.setBit(EVENT.LCIO.CHBIT_ID1) col.setFlag(flag.getFlag()) col.parameters().setValue( EVENT.LCIO.CellIDEncoding, 'system:0:5,module:5:3,stave:8:4,tower:12:5,layer:17:6,slice:23:4,x:32:-16,y:48:-16' ) evt.addCollection(col, "HcalBarrelRegCollection") for layer in range(48): ## loop over layers nx, nz = np.nonzero( showers[j][layer]) ## get non-zero energy cells for k in range(0, len(nx)): try: cell_energy = showers[j][layer][nx[k]][nz[k]] / 1000.0 tmp = cmap[(layer, nx[k], nz[k])] sch = IMPL.SimCalorimeterHitImpl() position = arr.array('f', [tmp[0], tmp[1], tmp[2]]) sch.setPosition(position) sch.setEnergy(cell_energy) sch.setCellID0(int(tmp[3])) sch.setCellID1(int(tmp[4])) col.addElement(sch) except KeyError: # Key is not present pass progress_bar.progress(int(j * 100 / N)) wrt.writeEvent(evt) progress_bar.empty() st.info('LCIO file was created: {}'.format(os.getcwd() + '/' + outfile)) status_text_rec = st.empty() status_text_rec.text("We are ready to run reconstruction via iLCsoft") wrt.close()