vpy = 0. vpz = 0. vertex = array('d', [vpx, vpy, vpz]) time = 0. # --- particle charge if j % 2 == 1: pdg = -pdg charge = -charge #--------------- create MCParticle ------------------- mcp = IMPL.MCParticleImpl() mcp.setGeneratorStatus(genstat) mcp.setMass(mass) mcp.setPDG(pdg) mcp.setMomentum(momentum) mcp.setCharge(charge) mcp.setVertex(vertex) mcp.setTime(time) if (decayLen < 1.e9): # arbitrary ... mcp.setEndpoint(endpoint) print(" ", ipart, pdg, charge, pt, phi, theta) #-------------------------------------------------------
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()
# Correcting the weight w *= w_corr # Converting vertex coordinates: cm -> mm x *= 10 y *= 10 z *= 10 # Calculating how many copies of the particle to create according to the weight nW = math.floor(w) if random.random() < math.modf(w)[0]: nW += 1 # Creating the particle with original parameters particle = IMPL.MCParticleImpl() particle.setPDG(pdg) particle.setGeneratorStatus(1) particle.setTime(toff) particle.setMass(mass) particle.setCharge(charge) pos = array('d', [x, y, z]) mom = array('f', [px, py, pz]) # Creating the particle copies with random Phi rotation for iP in range(nW): p = IMPL.MCParticleImpl(particle) # Rotating position and momentum by random angle in Phi dPhi = random.random() * math.pi * 2 co = math.cos(dPhi) si = math.sin(dPhi)
momentum1 = array('f', [px1, py1, pz1]) momentum2 = array('f', [px2, py2, pz2]) # start from HCAL endcap vtx1 = array('d', [50., 1400., 2650]) vtx2 = array('d', [50., 1400 - shifty, 2650]) #epx = decayLen * math.cos( phi ) * math.sin( theta ) #epy = decayLen * math.sin( phi ) * math.sin( theta ) #epz = decayLen * math.cos( theta ) #endpoint = array('d',[ epx, epy, epz ] ) #--------------- create MCParticle ------------------- mcp1 = IMPL.MCParticleImpl() mcp1.setGeneratorStatus(genstat) mcp1.setMass(mass1) mcp1.setPDG(pdg1) mcp1.setMomentum(momentum1) mcp1.setCharge(charge1) mcp1.setVertex(vtx1) #if( decayLen < 1.e9 ) : # arbitrary ... # mcp.setEndpoint( endpoint ) mcp2 = IMPL.MCParticleImpl() mcp2.setGeneratorStatus(genstat) mcp2.setMass(mass2) mcp2.setPDG(pdg2) mcp2.setMomentum(momentum2)
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()