Generate the primary vertices in the Geant4 event.
268 {
269 if (n_events_generated_ == 0) {
270
271
272
273 auto seed = G4Random::getTheEngine()->getSeed();
274 ldmx_log(debug) << "Initializing GENIE with seed " << seed;
275 genie::utils::app_init::RandGen(seed);
276 }
277
278 auto nucl_target_i = 0;
279
280 if (targets_.size() > 0) {
281 double rand_uniform = G4Random::getTheGenerator()->flat();
282
283 nucl_target_i = std::distance(
284 ev_weighting_integral_.begin(),
285 std::lower_bound(ev_weighting_integral_.begin(),
286 ev_weighting_integral_.end(), rand_uniform));
287
288 ldmx_log(debug) << "Random number = " << rand_uniform << ", target picked "
289 << targets_.at(nucl_target_i);
290 }
291
292 auto x_pos = position_[0] +
293 (G4Random::getTheGenerator()->flat() - 0.5) * beam_size_[0];
294 auto y_pos = position_[1] +
295 (G4Random::getTheGenerator()->flat() - 0.5) * beam_size_[1];
296 auto z_pos = position_[2] +
297 (G4Random::getTheGenerator()->flat() - 0.5) * target_thickness_;
298
299 ldmx_log(debug) << "Generating interaction at (x_,y_,z_)=" << "(" << x_pos
300 << "," << y_pos << "," << z_pos << ")";
301
302
303 TParticle initial_e;
304 initial_e.SetPdgCode(11);
305 float elec_i_p =
306 std::sqrt(energy_ * energy_ - initial_e.GetMass() * initial_e.GetMass());
307 initial_e.SetMomentum(elec_i_p * direction_[0], elec_i_p * direction_[1],
308 elec_i_p * direction_[2], energy_);
309 TLorentzVector e_p4;
310 initial_e.Momentum(e_p4);
311
312 ldmx_log(debug) << "Generating interation with (px,py,pz,e)=" << "("
313 << e_p4.Px() << "," << e_p4.Py() << "," << e_p4.Pz() << ","
314 << e_p4.E() << ")";
315
316 n_events_by_target_[nucl_target_i] += 1;
317
318
319 genie::EventRecord* genie_event = NULL;
320 while (!genie_event)
321 genie_event =
evg_drivers_[nucl_target_i].GenerateEvent(e_p4);
322
323 auto ev_info = new UserEventInformation;
324 auto hepmc3_genie = hep_mc3_converter_.ConvertToHepMC3(*genie_event);
326 hepmc3_genie->write_data(hepmc3_ldmx_genie);
327 ev_info->addHepMC3GenEvent(hepmc3_ldmx_genie);
328 event->SetUserInformation(ev_info);
329
330
331
332 G4PrimaryVertex* vertex = new G4PrimaryVertex();
333 vertex->SetPosition(x_pos, y_pos, z_pos);
334 vertex->SetWeight(genie_event->Weight());
335
336
337 int n_entries = genie_event->GetEntries();
338
339 ldmx_log(debug) << "---------- " << "Generated Event "
340 << n_events_generated_ + 1 << " ----------";
341
342 for (int i_p = 0; i_p < n_entries; ++i_p) {
343 genie::GHepParticle* p = (genie::GHepParticle*)(*genie_event)[i_p];
344
345
346 if (p->Status() != 1) continue;
347
348 ldmx_log(debug) << "\tAdding particle " << p->Pdg() << " with status "
349 << p->Status() << " energy " << p->E() << " ...";
350
351 G4PrimaryParticle* primary = new G4PrimaryParticle();
352 primary->SetPDGcode(p->Pdg());
353
354
355
356
357
358
359
360 primary->SetMomentum(p->Px() * CLHEP::GeV, p->Py() * CLHEP::GeV,
361 p->Pz() * CLHEP::GeV);
362
363 primary->SetProperTime(time_ * CLHEP::ns);
364
365 UserPrimaryParticleInformation* primary_info =
366 new UserPrimaryParticleInformation();
367 primary_info->setHepEvtStatus(1);
368 primary->SetUserInformation(primary_info);
369
370 vertex->SetPrimary(primary);
371 }
372
373
374 event->AddPrimaryVertex(vertex);
375
376
379 }
380
381 ++n_events_generated_;
382 delete genie_event;
383}
bool useBeamspot() const
Check if beam spot smearing is enabled for this generator.
void smearBeamspot(G4PrimaryVertex *primary_vertex)
Apply beam spot smearing to a primary vertex.