LDMX Software
FromScoringPlane.cxx
1#include "SimCore/Generators/FromScoringPlane.h"
2
3#include <unordered_map>
4
5#include "DetDescr/SimSpecialID.h"
6#include "G4Event.hh"
7#include "G4PrimaryParticle.hh"
10
11namespace simcore {
12namespace generators {
13
15 const std::string& name, const framework::config::Parameters& parameters)
16 : PrimaryGenerator(name, parameters),
17 coll_name_{parameters.get<std::string>("coll_name")},
18 pass_name_{parameters.get<std::string>("pass_name")},
19 select_planes_{parameters.get<std::vector<int>>("select_planes")} {}
20
22 ldmx_log(debug) << "preparing event";
23 const auto& scoring_plane_hits{
24 event.getCollection<ldmx::SimTrackerHit>(coll_name_, pass_name_)};
25 std::unordered_map<int, std::vector<const ldmx::SimTrackerHit*>>
26 hits_by_track_id;
27 for (const auto& hit : scoring_plane_hits) {
28 ldmx::SimSpecialID id(hit.getID());
29 bool keep{select_planes_.empty() or
30 std::find(select_planes_.begin(), select_planes_.end(),
31 id.plane()) != select_planes_.end()};
32 // do filtering by layer here
33 ldmx_log(trace) << "hit with plane = " << id.plane() << " is "
34 << (keep ? "used" : "ignored");
35 if (keep) {
36 // sort hits that we are keeping by track ID
37 hits_by_track_id[hit.getTrackID()].push_back(&hit);
38 }
39 }
40
41 // copy earliest hit by track Id in as primary
42 primary_vertices_.clear();
43 for (auto& [track_id, hits] : hits_by_track_id) {
44 auto earliest_hit_it = std::min_element(
45 hits.begin(), hits.end(), [](const auto& hit_lhs, const auto& hit_rhs) {
46 return hit_lhs->getTime() < hit_rhs->getTime();
47 });
48 const auto* earliest_hit = (*earliest_hit_it);
49
50 G4PrimaryParticle* particle = new G4PrimaryParticle;
51 // proper PDG also copies in mass and charge
52 particle->SetPDGcode(earliest_hit->getPdgID());
53
54 auto momentum{earliest_hit->getMomentum()};
55 auto energy{earliest_hit->getEnergy()};
56 particle->Set4Momentum(momentum[0], momentum[1], momentum[2], energy);
57 // probably not correct from a relativistic perspective
58 particle->SetProperTime(earliest_hit->getTime());
59 // label this particle as a primary for purposes of serialization
61 uppi->setHepEvtStatus(1);
62 particle->SetUserInformation(uppi);
63 // NOT copying over the TrackID into the primary because
64 // (as of Geant4.10.2.3) the track IDs of particles created
65 // in this simulation do not start at the primary's number and
66 // count up which is what I would want to make this understandable
67 // particle->SetTrackID(track_id);
68
69 auto pos{earliest_hit->getPosition()};
70 G4PrimaryVertex* vertex = new G4PrimaryVertex;
71 vertex->SetPosition(pos[0], pos[1], pos[2]);
72 // not setting a weight
73 vertex->SetPrimary(particle);
74
75 primary_vertices_.push_back(vertex);
76 }
77}
78
80 ldmx_log(debug) << "generating primary vertex";
81 for (auto* primary_vertex : primary_vertices_) {
82 anEvent->AddPrimaryVertex(primary_vertex);
83 }
84}
85
86void FromScoringPlane::RecordConfig(const std::string& id,
87 ldmx::RunHeader& rh) {
88 rh.setStringParameter(id + " Class", "simcore::generators::FromScoringPlane");
89 rh.setStringParameter(id + " Coll Name", coll_name_);
90 rh.setStringParameter(id + " Pass Name", pass_name_);
91}
92
93} // namespace generators
94} // namespace simcore
95
#define DECLARE_GENERATOR(CLASS)
@macro DECLARE_GENERATOR
Class which encapsulates information from a hit in a simulated tracking detector.
Class that provides extra information for Geant4 primary particles.
Implements an event buffer system for storing event data.
Definition Event.h:40
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:68
void setStringParameter(const std::string &name, std::string value)
Set a string parameter value.
Definition RunHeader.h:261
Implements detector ids for special simulation-derived hits like scoring planes.
Represents a simulated tracker hit in the simulation.
Interface that defines a simulation primary generator.
Defines extra information attached to a Geant4 primary particle.
void setHepEvtStatus(int hepEvtStatus)
Set the HEP event status (generator status) e.g.
Copy particles from a particular scoring plane in as primaries of a new simulation.
std::string coll_name_
name of scoring plane collection we should use
std::vector< G4PrimaryVertex * > primary_vertices_
list of primary vertices we will give over to the G4Event
std::vector< int > select_planes_
list of plane ID numbers to select for (use all hits if empty)
std::string pass_name_
name of pass for scoring plane collection we should use
void GeneratePrimaryVertex(G4Event *anEvent) override
Generate vertices in the Geant4 event.
FromScoringPlane(const std::string &name, const framework::config::Parameters &parameters)
Class constructor.
void prepEvent(const framework::Event &event) override
Retrieve the collection of scoring plane hits and copy the selected hits into our copies of primary p...
void RecordConfig(const std::string &id, ldmx::RunHeader &rh) override
Record configuration information.
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...