ghit
Loading...
Searching...
No Matches
ghit_example.cc
Go to the documentation of this file.
1
13// gemc
14#include "gtouchable.h"
15#include "gtouchable_options.h"
16#include "glogger.h"
17
18// ghit
19#include "ghit.h"
20
21using std::string;
22using std::vector;
23
24int main(int argc, char* argv[]) {
25 // Build options using the touchable module option definitions.
26 auto gopts = std::make_shared<GOptions>(argc, argv, gtouchable::defineOptions());
27
28 // Local logger for this example program.
29 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, TOUCHABLE_LOGGER); // duplicate logger
30
31 // Select which optional hit information to record.
32 HitBitSet hitBitSet;
33
34 // Create a concrete touchable corresponding to a detector element identity.
35 auto a_ctof_gtouchable = GTouchable(gopts, "readout", "sector: 6, paddle: 10", {10.0, 20.0, 30.0}, 100.0*CLHEP::g);
36
37 // Create a hit tied to that touchable. (No step is provided here, so vectors start empty.)
38 auto a_hit = new GHit(std::make_shared<GTouchable>(a_ctof_gtouchable), hitBitSet);
39
40 // Emulating a hits collection (in Geant4 this is commonly a G4THitsCollection<GHit>).
41 vector<GHit*> hits;
42 hits.emplace_back(a_hit); // pass ownership (caller owns and must delete if this were not a short-lived example)
43
44 for (unsigned i = 1; i < 100; i++) {
45 // Create randomized test hits (for demonstration only).
46 auto hit = GHit::create(gopts);
47
48 log->info(" GHit: ", *hit->getGTouchable(), " with total Edep: ", hit->getTotalEnergyDeposited(), " MeV");
49
50 // Compare without copying; hits stores raw pointers, so iterate by pointer.
51 for (const auto& hit_in_v : hits) {
52 if (hit->is_same_hit(hit_in_v)) {
53 log->info(NORMAL, (*hit->getGTouchable()), " found in hit n. ", i);
54 }
55 }
56
57 // Note: in a real program you would also manage ownership of 'hit' to avoid leaks.
58 }
59
60 return EXIT_SUCCESS;
61}
Stores step-by-step and aggregated information for a detector hit.
Definition ghit.h:43
static GHit * create(const std::shared_ptr< GOptions > &gopts)
Create a fake hit for testing, using the current options.
Definition ghit.h:462
std::bitset< NHITBITS > HitBitSet
Bitset selecting which optional hit information is recorded.
#define SFUNCTION_NAME
NORMAL
constexpr const char * TOUCHABLE_LOGGER
GOptions defineOptions()
int main(int argc, char *argv[])