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"
9
10namespace simcore {
11namespace generators {
12
14 const std::string& name, const framework::config::Parameters& parameters)
15 : PrimaryGenerator(name, parameters),
16 coll_name_{parameters.get<std::string>("coll_name")},
17 pass_name_{parameters.get<std::string>("pass_name")},
18 select_planes_{parameters.get<std::vector<int>>("select_planes")} {}
19
21 ldmx_log(debug) << "preparing event";
22 const auto& scoring_plane_hits{
23 event.getCollection<ldmx::SimTrackerHit>(coll_name_, pass_name_)};
24 std::unordered_map<int, std::vector<const ldmx::SimTrackerHit*>>
25 hits_by_track_id;
26 for (const auto& hit : scoring_plane_hits) {
27 ldmx::SimSpecialID id(hit.getID());
28 bool keep{select_planes_.empty() or
29 std::find(select_planes_.begin(), select_planes_.end(),
30 id.plane()) != select_planes_.end()};
31 // do filtering by layer here
32 ldmx_log(trace) << "hit with plane = " << id.plane() << " is "
33 << (keep ? "used" : "ignored");
34 if (keep) {
35 // sort hits that we are keeping by track ID
36 hits_by_track_id[hit.getTrackID()].push_back(&hit);
37 }
38 }
39
40 // copy earliest hit by track Id in as primary
41 primary_vertices_.clear();
42 for (auto& [track_id, hits] : hits_by_track_id) {
43 auto earliest_hit_it = std::min_element(
44 hits.begin(), hits.end(), [](const auto& hit_lhs, const auto& hit_rhs) {
45 return hit_lhs->getTime() < hit_rhs->getTime();
46 });
47 const auto* earliest_hit = (*earliest_hit_it);
48
49 G4PrimaryParticle* particle = new G4PrimaryParticle;
50 // proper PDG also copies in mass and charge
51 particle->SetPDGcode(earliest_hit->getPdgID());
52
53 auto momentum{earliest_hit->getMomentum()};
54 auto energy{earliest_hit->getEnergy()};
55 particle->Set4Momentum(momentum[0], momentum[1], momentum[2], energy);
56 // probably not correct from a relativistic perspective
57 particle->SetProperTime(earliest_hit->getTime());
58 // label this particle as a primary for purposes of serialization
60 uppi->setHepEvtStatus(1);
61 particle->SetUserInformation(uppi);
62 // NOT copying over the TrackID into the primary because
63 // (as of Geant4.10.2.3) the track IDs of particles created
64 // in this simulation do not start at the primary's number and
65 // count up which is what I would want to make this understandable
66 // particle->SetTrackID(track_id);
67
68 auto pos{earliest_hit->getPosition()};
69 G4PrimaryVertex* vertex = new G4PrimaryVertex;
70 vertex->SetPosition(pos[0], pos[1], pos[2]);
71 // not setting a weight
72 vertex->SetPrimary(particle);
73
74 primary_vertices_.push_back(vertex);
75 }
76}
77
79 ldmx_log(debug) << "generating primary vertex";
80 for (auto* primary_vertex : primary_vertices_) {
81 anEvent->AddPrimaryVertex(primary_vertex);
82 }
83}
84
85void FromScoringPlane::RecordConfig(const std::string& id,
86 ldmx::RunHeader& rh) {
87 rh.setStringParameter(id + " Class", "simcore::generators::FromScoringPlane");
88 rh.setStringParameter(id + " Coll Name", coll_name_);
89 rh.setStringParameter(id + " Pass Name", pass_name_);
90}
91
92} // namespace generators
93} // namespace simcore
94
#define DECLARE_GENERATOR(CLASS)
@macro DECLARE_GENERATOR
Class which encapsulates information from a hit in a simulated tracking detector.
Implements an event buffer system for storing event data.
Definition Event.h:42
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:57
void setStringParameter(const std::string &name, std::string value)
Set a string parameter value.
Definition RunHeader.h:222
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 ...