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 genie::InitialState initial_state(targets_.at(nucl_target_i), 11);
305
306
307 TParticle initial_e;
308 initial_e.SetPdgCode(11);
309 float elec_i_p =
310 std::sqrt(energy_ * energy_ - initial_e.GetMass() * initial_e.GetMass());
311 initial_e.SetMomentum(elec_i_p * direction_[0], elec_i_p * direction_[1],
312 elec_i_p * direction_[2], energy_);
313 TLorentzVector e_p4;
314 initial_e.Momentum(e_p4);
315
316 ldmx_log(debug) << "Generating interation with (px,py,pz,e)=" << "("
317 << e_p4.Px() << "," << e_p4.Py() << "," << e_p4.Pz() << ","
318 << e_p4.E() << ")";
319
320
321 if (n_events_by_target_[nucl_target_i] == 0)
322 xsec_by_target_[nucl_target_i] =
evg_driver_.XSecSum(e_p4);
323
324 n_events_by_target_[nucl_target_i] += 1;
325
326
327 genie::EventRecord* genie_event = NULL;
328 while (!genie_event) genie_event =
evg_driver_.GenerateEvent(e_p4);
329
330 auto ev_info = new UserEventInformation;
331 auto hepmc3_genie = hep_mc3_converter_.ConvertToHepMC3(*genie_event);
333 hepmc3_genie->write_data(hepmc3_ldmx_genie);
334 ev_info->addHepMC3GenEvent(hepmc3_ldmx_genie);
335 event->SetUserInformation(ev_info);
336
337
338
339 G4PrimaryVertex* vertex = new G4PrimaryVertex();
340 vertex->SetPosition(x_pos, y_pos, z_pos);
341 vertex->SetWeight(genie_event->Weight());
342
343
344 int n_entries = genie_event->GetEntries();
345
346 ldmx_log(debug) << "---------- " << "Generated Event "
347 << n_events_generated_ + 1 << " ----------";
348
349 for (int i_p = 0; i_p < n_entries; ++i_p) {
350 genie::GHepParticle* p = (genie::GHepParticle*)(*genie_event)[i_p];
351
352
353 if (p->Status() != 1) continue;
354
355 ldmx_log(debug) << "\tAdding particle " << p->Pdg() << " with status "
356 << p->Status() << " energy " << p->E() << " ...";
357
358 G4PrimaryParticle* primary = new G4PrimaryParticle();
359 primary->SetPDGcode(p->Pdg());
360
361
362
363
364
365
366
367 primary->SetMomentum(p->Px() * CLHEP::GeV, p->Py() * CLHEP::GeV,
368 p->Pz() * CLHEP::GeV);
369
370 primary->SetProperTime(time_ * CLHEP::ns);
371
372 UserPrimaryParticleInformation* primary_info =
373 new UserPrimaryParticleInformation();
374 primary_info->setHepEvtStatus(1);
375 primary->SetUserInformation(primary_info);
376
377 vertex->SetPrimary(primary);
378 }
379
380
381 event->AddPrimaryVertex(vertex);
382
383 ++n_events_generated_;
384
385}