def generateEvents( outputFileName, nEvents ): random = TRandom3( 12345 ) # define a particle source sourcePosition = TVector3( 0., 0., 0. ) sourceSpreadXY = 10. pdgid = 13 charge = -1. mass = 0.105658 momentum = TVector3( 0.3, 0.1, 10. ) runNumber = 321 # define a detector with positions for the tracker planes detectorName = 'ToyTracker' trackerPlanePositions = [] hitResolution = 0.01 planeNormal = TVector3( 0., 0., 1. ) for planeZ in [ 100., 250., 480., 510., 640. ]: trackerPlanePositions.append( TVector3( 0., 0., planeZ ) ) # create a writer and open the output file writer = IOIMPL.LCFactory.getInstance().createLCWriter() writer.open( outputFileName, EVENT.LCIO.WRITE_NEW ) # create a run header and add it to the file (optional) run = IMPL.LCRunHeaderImpl() run.setRunNumber( runNumber ) run.setDetectorName( detectorName ) run.setDescription( 'This is a test run' ) writer.writeRunHeader( run ) for iEvent in xrange( nEvents ): # create an event and set its parameters event = IMPL.LCEventImpl() event.setEventNumber( iEvent ) event.setDetectorName( detectorName ) event.setRunNumber( runNumber ) event.setTimeStamp( int( time() * 1000000000. ) ) # create the mc particle collection mcParticles = IMPL.LCCollectionVec( EVENT.LCIO.MCPARTICLE ) # calculate the origin of the particle x = random.Gaus( sourcePosition.x(), sourceSpreadXY ) y = random.Gaus( sourcePosition.y(), sourceSpreadXY ) z = sourcePosition.z() origin = TVector3( x, y, z ) # create a particle mcParticle = IMPL.MCParticleImpl() mcParticle.setPDG( pdgid ) mcParticle.setMass( mass ) mcParticle.setMomentumVec( momentum ) mcParticle.setGeneratorStatus( 1 ) mcParticle.setVertexVec( origin ) mcParticle.setTime( 0. ) mcParticles.addElement( mcParticle ) # create a tracker hit collection trackerHits = IMPL.LCCollectionVec( EVENT.LCIO.SIMTRACKERHIT ) trackerHits.setFlag( UTIL.set_bit( trackerHits.getFlag(), EVENT.LCIO.THBIT_MOMENTUM ) ) # create an IDEncoder to store hit IDs # defines the tags and the number of bits for the different bit fields encodingString = 'system:3,layer:6' idEncoder = UTIL.CellIDEncoder( IMPL.SimTrackerHitImpl )( encodingString, trackerHits ) # add a hit for each layer for planePosition in trackerPlanePositions: # calculate the intersection with the plane distance = ( planePosition - origin ).Dot( planeNormal ) / momentum.Dot( planeNormal ) intersect = TVector3( momentum ) intersect.SetMag( distance ) # smear the hit position with the resolution hitX = random.Gaus( intersect.x(), hitResolution ) hitY = random.Gaus( intersect.x(), hitResolution ) hitPosition = TVector3( hitX, hitY, intersect.z() ) # build the tracker hit trackerHit = IMPL.SimTrackerHitImpl() trackerHit.setPositionVec( hitPosition ) trackerHit.setMomentumVec( momentum ) trackerHit.setMCParticle( mcParticle ) trackerHit.setTime( distance / TMath.C() ) trackerHit.setEDep( 0.1 ) # set the cell ID idEncoder.reset() idEncoder['layer'] = trackerPlanePositions.index( planePosition ) idEncoder['system'] = 1 idEncoder.setCellID( trackerHit ) trackerHits.addElement( trackerHit ) event.addCollection( mcParticles, EVENT.LCIO.MCPARTICLE ) event.addCollection( trackerHits, 'SimTrackerHits' ) writer.writeEvent( event ) 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 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 getinfo(inputfilename, outputfilename): #Set up the LCIO output file writer = IOIMPL.LCFactory.getInstance().createLCWriter() writer.open(outputfilename, EVENT.LCIO.WRITE_NEW) #Get the file and the tree in the file: file = TFile(inputfilename, "read") tree = file.Get("Frames") #iterator for progress bar... q = int(0) #loop over all entries in the tree... for i in range(0, tree.GetEntries()): 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 an event LCEvent = IMPL.LCEventImpl() LCEvent.setEventNumber(Event) LCEvent.setRunNumber(Run) # ++ create a raw calorimeter hit collection RawCaloHits = IMPL.LCCollectionVec(EVENT.LCIO.RAWCALORIMETERHIT) # ++ create an IDEncoder to store hit IDs # ++ defines the tags and the number of bits for the different bit fields encodingString = str("lane:7,row:11,column:11") idEncoder = UTIL.CellIDEncoder(IMPL.RawCalorimeterHitImpl)( encodingString, RawCaloHits) for j in range(0, len(Lane)): # ++ build the raw calorimeter hit Hit = IMPL.RawCalorimeterHitImpl() # ++ set the cell ID idEncoder.reset() idEncoder['lane'] = Lane[j] idEncoder['row'] = Row[j] idEncoder['column'] = Column[j] idEncoder.setCellID(Hit) # ++ add the hit to the collection RawCaloHits.addElement(Hit) # ++ end of hit loop # ++ add the collection to the event LCEvent.addCollection(RawCaloHits, 'RawCalorimeterHits') # ++ write the event to the output file writer.writeEvent(LCEvent) # ++ print percentage of progress (but not too often!) p = int((float(i) / 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 convertRun( inputTarFile, outputFileName ): # read file names from given path #fileNames = sorted( glob.glob( inputPath+'/mpx*.txt' ) ) #nEvents = len( fileNames ) inputFiles = [] tar = tarfile.open( inputTarFile, 'r:gz' ) for member in tar.getmembers(): if not member.isfile(): continue inputFiles.append( tar.extractfile(member) ) # get run number from first file (same for the rest) assuming filename is this form: mpx-YYMMDD-HHmmSS-RUN_FRAME.txt runNumber = int( inputFiles[0].name.split('-')[-1].split('_')[0] ) runNumber = int( inputTarFile.split('n')[-1].split('.')[0] ) # define detector name detectorName = 'EUTelescope' # create a writer and open the output file writer = IOIMPL.LCFactory.getInstance().createLCWriter() writer.open( outputFileName, EVENT.LCIO.WRITE_NEW ) print "Runnumber: " + str(runNumber) # create a run header and add it to the file run = IMPL.LCRunHeaderImpl() run.setRunNumber( runNumber ) run.setDetectorName( detectorName ) run.parameters().setValue ( 'GeoID' , 0 ) run.parameters().setValues ( 'MaxX' , std.vector(int)(6,1151) ) run.parameters().setValues ( 'MaxY' , std.vector(int)(6,575) ) run.parameters().setValues ( 'MinX' , std.vector(int)(6,0) ) run.parameters().setValues ( 'MinY' , std.vector(int)(6,0) ) run.parameters().setValue ( 'NoOfDetector' , 6 ) run.parameters().setValues ( 'AppliedProcessor' , std.vector('string')(1,'') ) run.parameters().setValue ( 'DAQHWName' , 'EUDRB' ) run.parameters().setValue ( 'DAQSWName' , 'EUDAQ' ) run.parameters().setValue ( 'DataType' , 'SimData' ) run.parameters().setValue ( 'DateTime' , '24.12.2000 23:59:59.000000000' ) run.parameters().setValue ( 'EUDRBDet' , 'MIMOSA26' ) run.parameters().setValue ( 'EUDRBMode' , 'ZS2' ) writer.writeRunHeader( run ) MAXEVENTS = 1000000 NEVENTS = 0 # event loop for eventFile in inputFiles: if NEVENTS == MAXEVENTS: break NEVENTS += 1 # get event number from file name ( i.e. frame ID ) assuming file name format above iEvent = int( eventFile.name.split('_')[-1].split('.')[0] ) if ( NEVENTS%1000 == 0 ): print 'Events processed: %i ...' % NEVENTS # create an event and set its parameters event = IMPL.LCEventImpl() event.setEventNumber( iEvent ) event.setDetectorName( detectorName ) event.setRunNumber( runNumber ) event.setTimeStamp( int( time() * 1000000000. ) ) event.parameters().setValue( 'EventType', 2 ) # parse input file telescopeData, DUTData = parseFrameFile( eventFile ) # is there DUT data? containsDUT = False if len( DUTData ) > 0: containsDUT = True # if first event, create additional setup collection(s) if iEvent == 0: eudrbSetup = IMPL.LCCollectionVec( EVENT.LCIO.LCGENERICOBJECT ) # collection parameters eudrbSetup.parameters().setValue( 'DataDescription', 'type:i,mode:i,spare1:i,spare2:i,spare3:i' ) eudrbSetup.parameters().setValue( 'TypeName', 'Setup Description' ) # create on setup object per Telescope plane for sensorID in sorted( telescopeData.iterkeys() ): setupObj = IMPL.LCGenericObjectImpl(5,0,0) setupObj.setIntVal( 0, 102 ) setupObj.setIntVal( 1, 101 ) eudrbSetup.addElement( setupObj ) event.addCollection ( eudrbSetup, 'eudrbSetup' ) # check if there is a DUT if containsDUT: DUTSetup = IMPL.LCCollectionVec( EVENT.LCIO.LCGENERICOBJECT ) event.addCollection ( DUTSetup, 'DUTSetup' ) # ID encoder info encodingString = 'sensorID:7,sparsePixelType:5' # Telescope data collection trackerDataColl = IMPL.LCCollectionVec( EVENT.LCIO.TRACKERDATA ) idEncoder_Telescope = UTIL.CellIDEncoder( IMPL.TrackerDataImpl )( encodingString, trackerDataColl ) # check if there is a DUT if containsDUT: # DUT data collection DUTDataColl = IMPL.LCCollectionVec( EVENT.LCIO.TRACKERDATA ) idEncoder_DUT = UTIL.CellIDEncoder( IMPL.TrackerDataImpl )( encodingString, DUTDataColl ) REFDataColl = IMPL.LCCollectionVec( EVENT.LCIO.TRACKERDATA ) idEncoder_REF = UTIL.CellIDEncoder( IMPL.TrackerDataImpl )( encodingString, REFDataColl ) for i,sensorID in enumerate( sorted( DUTData.iterkeys() ) ): planeData = IMPL.TrackerDataImpl() idEncoder_DUT.reset() #idEncoder_DUT['sensorID'] = int( sensorID ) - 500 + 6 # cannot fit 500 in 5 bits!! FIXME idEncoder_DUT['sensorID'] = i+6 # cannot fit 500 in 5 bits!! FIXME idEncoder_DUT['sparsePixelType'] = 2 idEncoder_DUT.setCellID( planeData ) chargeVec = std.vector(float)() for val in DUTData[sensorID]: chargeVec.push_back( val ) if val < 0: print 'Negative number in Event %i' % iEvent planeData.setChargeValues( chargeVec ) if int(sensorID) == 900 : DUTDataColl.addElement( planeData ) event.addCollection( DUTDataColl, 'CMSPixelDUT' ) elif int(sensorID) == 901 : REFDataColl.addElement( planeData ) event.addCollection( REFDataColl, 'CMSPixelREF' ) else: print "Shit. Who am I? sensorID: " + str(int(sensorID)) # fill telescope collection for sensorID in sorted( telescopeData.iterkeys() ): planeData = IMPL.TrackerDataImpl() idEncoder_Telescope.reset() idEncoder_Telescope['sensorID'] = int( sensorID ) - 300 # cannot fit 300 in 5 bits!! FIXME idEncoder_Telescope['sparsePixelType'] = 2 idEncoder_Telescope.setCellID( planeData ) # loop over hits chargeVec = std.vector(float)() for val in telescopeData[sensorID]: chargeVec.push_back( val ) planeData.setChargeValues( chargeVec ) trackerDataColl.addElement( planeData ) event.addCollection( trackerDataColl, 'zsdata_m26' ) writer.writeEvent( event ) writer.flush() writer.close()
def convertRun(inputFileName, outputFileName, runNumber, nplanes=7): # define detector name detectorName = 'FEI4Tel' # create a writer and open the output file writer = IOIMPL.LCFactory.getInstance().createLCWriter() writer.open(outputFileName, EVENT.LCIO.WRITE_NEW) # create a run header and add it to the file run = IMPL.LCRunHeaderImpl() run.setRunNumber(runNumber) run.setDetectorName(detectorName) run.parameters().setValue('GeoID', 0) run.parameters().setValues('MaxX', std.vector(int)(nplanes, 335)) run.parameters().setValues('MaxY', std.vector(int)(nplanes, 79)) run.parameters().setValues('MinX', std.vector(int)(nplanes, 0)) run.parameters().setValues('MinY', std.vector(int)(nplanes, 0)) run.parameters().setValue('NoOfDetector', nplanes) run.parameters().setValues('AppliedProcessor', std.vector('string')(1, '')) run.parameters().setValue('DAQHWName', 'EUDRB') run.parameters().setValue('DAQSWName', 'EUDAQ') run.parameters().setValue('DataType', 'Data') run.parameters().setValue('DateTime', '24.12.2000 23:59:59.000000000') run.parameters().setValue('EUDRBDet', 'MIMOSA26') run.parameters().setValue('EUDRBMode', 'ZS2') writer.writeRunHeader(run) aDataSet = JudithData(inputFileName, nplanes) MAXEVENTS = aDataSet.GetNEvents() #MAXEVENTS = 500 NEVENTS = 0 # event loop for eventnr in range(MAXEVENTS): if (eventnr % 1000 == 0): print 'Events processed: %i ...' % eventnr # create an event and set its parameters event = IMPL.LCEventImpl() event.setEventNumber(eventnr) event.setDetectorName(detectorName) event.setRunNumber(runNumber) event.setTimeStamp(int(time() * 1000000000.)) event.parameters().setValue('EventType', 2) # parse input file telescopeData = aDataSet.GetEvent(eventnr) #aDataSet.PrintEvent(eventnr) # if first event, create additional setup collection(s) if eventnr == 0: eudrbSetup = IMPL.LCCollectionVec(EVENT.LCIO.LCGENERICOBJECT) # collection parameters eudrbSetup.parameters().setValue( 'DataDescription', 'type:i,mode:i,spare1:i,spare2:i,spare3:i') eudrbSetup.parameters().setValue('TypeName', 'Setup Description') # create on setup object per Telescope plane for sensorID in range(nplanes): setupObj = IMPL.LCGenericObjectImpl(5, 0, 0) setupObj.setIntVal(0, 102) setupObj.setIntVal(1, 101) eudrbSetup.addElement(setupObj) event.addCollection(eudrbSetup, 'eudrbSetup') # ID encoder info encodingString = 'sensorID:5,sparsePixelType:5' # Telescope data collection trackerDataColl = IMPL.LCCollectionVec(EVENT.LCIO.TRACKERDATA) idEncoder_Telescope = UTIL.CellIDEncoder(IMPL.TrackerDataImpl)( encodingString, trackerDataColl) # fill telescope collection for sensorID in range(nplanes): planeData = IMPL.TrackerDataImpl() idEncoder_Telescope.reset() idEncoder_Telescope['sensorID'] = int( sensorID) # cannot fit 300 in 5 bits!! FIXME plane_tmp = int(sensorID) idEncoder_Telescope['sparsePixelType'] = 2 idEncoder_Telescope.setCellID(planeData) # loop over hits chargeVec = std.vector(float)() for hit in telescopeData[sensorID]: for j, val in enumerate(hit): chargeVec.push_back(val) planeData.setChargeValues(chargeVec) trackerDataColl.addElement(planeData) event.addCollection(trackerDataColl, 'zsdata_FEI4') writer.writeEvent(event) writer.flush() writer.close()