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