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