From d91778675e50056e59e8d719ef839a7d4ba65328 Mon Sep 17 00:00:00 2001 From: Dmitri Smirnov Date: Wed, 2 Oct 2024 17:03:58 -0400 Subject: [PATCH] style: format code according to microsoft style --- src/consgeo.cpp | 49 ++--- src/csg_tree.cpp | 27 +-- src/simg4ox.cpp | 489 ++++++++++++++++++++++++----------------------- src/simtox.cpp | 73 ++++--- 4 files changed, 326 insertions(+), 312 deletions(-) diff --git a/src/consgeo.cpp b/src/consgeo.cpp index aa77f0d..ea98686 100644 --- a/src/consgeo.cpp +++ b/src/consgeo.cpp @@ -2,9 +2,9 @@ #include #include -#include -#include #include +#include +#include #include @@ -12,35 +12,36 @@ using namespace std; - int main(int argc, char **argv) { - using PLogFormat = plog::TxtFormatter; - static plog::ColorConsoleAppender consoleAppender; - plog::init(plog::debug, &consoleAppender); + using PLogFormat = plog::TxtFormatter; + static plog::ColorConsoleAppender consoleAppender; + plog::init(plog::debug, &consoleAppender); - argparse::ArgumentParser program("consgeo", "0.0.0"); + argparse::ArgumentParser program("consgeo", "0.0.0"); - string gdml_file; + string gdml_file; - program.add_argument("-g", "--gdml") - .help("path to GDML file") - .default_value(string("geom.gdml")) - .nargs(1) - .store_into(gdml_file); + program.add_argument("-g", "--gdml") + .help("path to GDML file") + .default_value(string("geom.gdml")) + .nargs(1) + .store_into(gdml_file); - try { - program.parse_args(argc, argv); - } - catch (const exception& err) { - cerr << err.what() << endl; - cerr << program; - exit(EXIT_FAILURE); - } + try + { + program.parse_args(argc, argv); + } + catch (const exception &err) + { + cerr << err.what() << endl; + cerr << program; + exit(EXIT_FAILURE); + } - cout << "gdml_file: " << gdml_file << endl; + cout << "gdml_file: " << gdml_file << endl; - from_gdml(gdml_file); + from_gdml(gdml_file); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/src/csg_tree.cpp b/src/csg_tree.cpp index 2cefdb8..21771b4 100644 --- a/src/csg_tree.cpp +++ b/src/csg_tree.cpp @@ -9,23 +9,24 @@ using namespace std; - void from_gdml(filesystem::path gdmlpath) { - G4GDMLParser parser; - parser.Read(gdmlpath.string(), false); + G4GDMLParser parser; + parser.Read(gdmlpath.string(), false); - const G4VPhysicalVolume* world = parser.GetWorldVolume(); + const G4VPhysicalVolume *world = parser.GetWorldVolume(); - if (!world) { - LOG_ERROR << "Failed creatng G4 volume from GDML " << gdmlpath << endl; - } - else { - LOG_INFO << "Created G4 volume " << world->GetName() << " from " << gdmlpath << endl; + if (!world) + { + LOG_ERROR << "Failed creatng G4 volume from GDML " << gdmlpath << endl; + } + else + { + LOG_INFO << "Created G4 volume " << world->GetName() << " from " << gdmlpath << endl; - G4CXOpticks* g4cx = G4CXOpticks::SetGeometry(world); - g4cx->saveGeometry("./out/csg"); + G4CXOpticks *g4cx = G4CXOpticks::SetGeometry(world); + g4cx->saveGeometry("./out/csg"); - delete g4cx; - } + delete g4cx; + } } diff --git a/src/simg4ox.cpp b/src/simg4ox.cpp index 8926eb8..fccf402 100644 --- a/src/simg4ox.cpp +++ b/src/simg4ox.cpp @@ -10,9 +10,9 @@ #include "G4OpBoundaryProcess.hh" #include "G4OpticalPhoton.hh" #include "G4OpticalPhysics.hh" -#include "G4PrimaryVertex.hh" -#include "G4PrimaryParticle.hh" #include "G4PhysicalConstants.hh" +#include "G4PrimaryParticle.hh" +#include "G4PrimaryVertex.hh" #include "G4RunManager.hh" #include "G4SystemOfUnits.hh" #include "G4ThreeVector.hh" @@ -27,9 +27,9 @@ #include "G4VUserDetectorConstruction.hh" #include "G4VUserPrimaryGeneratorAction.hh" -#include "G4VisExecutive.hh" #include "G4UIExecutive.hh" #include "G4UImanager.hh" +#include "G4VisExecutive.hh" #include "G4CX/G4CXOpticks.hh" #include "SysRap/NP.hh" @@ -44,304 +44,317 @@ using namespace std; +SEvt *sev = nullptr; -SEvt* sev = nullptr; - -struct DetectorConstruction : G4VUserDetectorConstruction { - - DetectorConstruction(filesystem::path gdml_file) : gdml_file_(gdml_file) {} +struct DetectorConstruction : G4VUserDetectorConstruction +{ - G4VPhysicalVolume* Construct() override { - G4GDMLParser parser; - parser.Read(gdml_file_.string(), false); + DetectorConstruction(filesystem::path gdml_file) : gdml_file_(gdml_file) + { + } - G4VPhysicalVolume* world = parser.GetWorldVolume(); + G4VPhysicalVolume *Construct() override + { + G4GDMLParser parser; + parser.Read(gdml_file_.string(), false); - G4CXOpticks::SetGeometry(world); + G4VPhysicalVolume *world = parser.GetWorldVolume(); - return world; - } + G4CXOpticks::SetGeometry(world); - private: + return world; + } - filesystem::path gdml_file_; + private: + filesystem::path gdml_file_; }; +struct PrimaryGenerator : G4VUserPrimaryGeneratorAction +{ -struct PrimaryGenerator : G4VUserPrimaryGeneratorAction { + void GeneratePrimaries(G4Event *event) override + { + NP *photons = NP::Make(0, 4, 4); - void GeneratePrimaries(G4Event* event) override { - NP* photons = NP::Make(0, 4, 4); + photons->load("out/photons.npy"); - photons->load("out/photons.npy"); + size_t n_photons = photons->num_items(); + sphoton *sphotons = reinterpret_cast(photons->bytes()); - size_t n_photons = photons->num_items(); - sphoton* sphotons = reinterpret_cast(photons->bytes()); + for (int i = 0; i < n_photons; i++) + { + sphoton &p = sphotons[i]; - for (int i=0; i < n_photons; i++) { - sphoton &p = sphotons[i]; + G4ThreeVector position_mm(p.pos.x, p.pos.y, p.pos.z); + G4double time_ns = p.time; + G4ThreeVector direction(p.mom.x, p.mom.y, p.mom.z); + // direction = direction.unit(); + G4double wavelength_nm = p.wavelength; + G4ThreeVector polarization(p.pol.x, p.pol.y, p.pol.z); - G4ThreeVector position_mm(p.pos.x, p.pos.y, p.pos.z); - G4double time_ns = p.time; - G4ThreeVector direction(p.mom.x, p.mom.y, p.mom.z); - //direction = direction.unit(); - G4double wavelength_nm = p.wavelength ; - G4ThreeVector polarization(p.pol.x, p.pol.y, p.pol.z); + G4PrimaryVertex *vertex = new G4PrimaryVertex(position_mm, time_ns); + G4double kineticEnergy = h_Planck * c_light / (wavelength_nm * nm); - G4PrimaryVertex* vertex = new G4PrimaryVertex(position_mm, time_ns); - G4double kineticEnergy = h_Planck*c_light/(wavelength_nm*nm) ; + G4PrimaryParticle *particle = new G4PrimaryParticle(G4OpticalPhoton::Definition()); + particle->SetKineticEnergy(kineticEnergy); + particle->SetMomentumDirection(direction); + particle->SetPolarization(polarization); - G4PrimaryParticle* particle = new G4PrimaryParticle(G4OpticalPhoton::Definition()); - particle->SetKineticEnergy(kineticEnergy); - particle->SetMomentumDirection(direction); - particle->SetPolarization(polarization); + vertex->SetPrimary(particle); + event->AddPrimaryVertex(vertex); + } - vertex->SetPrimary(particle); - event->AddPrimaryVertex(vertex); + sev->SetInputPhoton(photons); } - - sev->SetInputPhoton(photons); - } }; +struct EventAction : G4UserEventAction +{ -struct EventAction : G4UserEventAction { - - void BeginOfEventAction(const G4Event *event) override { - sev->beginOfEvent(event->GetEventID()); - } + void BeginOfEventAction(const G4Event *event) override + { + sev->beginOfEvent(event->GetEventID()); + } - void EndOfEventAction(const G4Event *event) override { - int eventID = event->GetEventID(); - sev->addEventConfigArray(); - sev->gather(); - sev->endOfEvent(eventID); + void EndOfEventAction(const G4Event *event) override + { + int eventID = event->GetEventID(); + sev->addEventConfigArray(); + sev->gather(); + sev->endOfEvent(eventID); - // GPU-based simulation - G4CXOpticks* gx = G4CXOpticks::Get() ; - gx->simulate(eventID, true ) ; - } + // GPU-based simulation + G4CXOpticks *gx = G4CXOpticks::Get(); + gx->simulate(eventID, true); + } }; - -void get_label(spho& ulabel, const G4Track* track) +void get_label(spho &ulabel, const G4Track *track) { - spho* label = STrackInfo::GetRef(track); - assert(label && label->isDefined() && "all photons are expected to be labelled"); + spho *label = STrackInfo::GetRef(track); + assert(label && label->isDefined() && "all photons are expected to be labelled"); - std::array a_label; - label->serialize(a_label); + std::array a_label; + label->serialize(a_label); - ulabel.load(a_label); + ulabel.load(a_label); } +struct SteppingAction : G4UserSteppingAction +{ -struct SteppingAction : G4UserSteppingAction { - - void UserSteppingAction(const G4Step* step) { + void UserSteppingAction(const G4Step *step) + { - if (step->GetTrack()->GetDefinition() != G4OpticalPhoton::OpticalPhotonDefinition()) - return; + if (step->GetTrack()->GetDefinition() != G4OpticalPhoton::OpticalPhotonDefinition()) + return; - const G4VProcess* process = step->GetPreStepPoint()->GetProcessDefinedStep(); + const G4VProcess *process = step->GetPreStepPoint()->GetProcessDefinedStep(); - if (process == nullptr) - return; + if (process == nullptr) + return; - const G4Track* track = step->GetTrack(); - G4VPhysicalVolume* pv = track->GetVolume(); - const G4VTouchable* touch = track->GetTouchable(); + const G4Track *track = step->GetTrack(); + G4VPhysicalVolume *pv = track->GetVolume(); + const G4VTouchable *touch = track->GetTouchable(); - spho ulabel = {}; - get_label(ulabel, track); + spho ulabel = {}; + get_label(ulabel, track); - const G4StepPoint* pre = step->GetPreStepPoint(); - const G4StepPoint* post = step->GetPostStepPoint(); + const G4StepPoint *pre = step->GetPreStepPoint(); + const G4StepPoint *post = step->GetPostStepPoint(); - G4ThreeVector delta = step->GetDeltaPosition(); - double step_mm = delta.mag()/mm ; + G4ThreeVector delta = step->GetDeltaPosition(); + double step_mm = delta.mag() / mm; - sev->checkPhotonLineage(ulabel); + sev->checkPhotonLineage(ulabel); - sphoton& current_photon = sev->current_ctx.p ; + sphoton ¤t_photon = sev->current_ctx.p; - if (current_photon.flagmask_count() == 1) { - U4StepPoint::Update(current_photon, pre); // populate current_photon with pos, mom, pol, time, wavelength - sev->pointPhoton(ulabel); // copying current into buffers - } + if (current_photon.flagmask_count() == 1) + { + U4StepPoint::Update(current_photon, pre); // populate current_photon with pos, mom, pol, time, wavelength + sev->pointPhoton(ulabel); // copying current into buffers + } - bool tir; - unsigned flag = U4StepPoint::Flag(post, true, tir); - bool is_detect_flag = OpticksPhoton::IsSurfaceDetectFlag(flag); + bool tir; + unsigned flag = U4StepPoint::Flag(post, true, tir); + bool is_detect_flag = OpticksPhoton::IsSurfaceDetectFlag(flag); - current_photon.iindex = is_detect_flag ? U4Touchable::ImmediateReplicaNumber(touch) : U4Touchable::AncestorReplicaNumber(touch); + current_photon.iindex = + is_detect_flag ? U4Touchable::ImmediateReplicaNumber(touch) : U4Touchable::AncestorReplicaNumber(touch); - U4StepPoint::Update(current_photon, post); + U4StepPoint::Update(current_photon, post); - current_photon.set_flag(flag); + current_photon.set_flag(flag); - sev->pointPhoton(ulabel); - } + sev->pointPhoton(ulabel); + } }; - struct TrackingAction : G4UserTrackingAction { - const G4Track* transient_fSuspend_track = nullptr; - - void PreUserTrackingAction_Optical_FabricateLabel(const G4Track* track) - { - U4Track::SetFabricatedLabel(track); - spho* label = STrackInfo::GetRef(track); - assert(label); - } - + const G4Track *transient_fSuspend_track = nullptr; - void PreUserTrackingAction(const G4Track* track) override { - spho* label = STrackInfo::GetRef(track); - - if (label == nullptr) { - PreUserTrackingAction_Optical_FabricateLabel(track); - label = STrackInfo::GetRef(track); + void PreUserTrackingAction_Optical_FabricateLabel(const G4Track *track) + { + U4Track::SetFabricatedLabel(track); + spho *label = STrackInfo::GetRef(track); + assert(label); } - assert(label && label->isDefined()); - - std::array a_label; - label->serialize(a_label); - - spho ulabel = {}; - ulabel.load(a_label); - - U4Random::SetSequenceIndex(ulabel.id); - - bool resume_fSuspend = track == transient_fSuspend_track ; - - if (ulabel.gen() == 0) { - if (resume_fSuspend == false) - sev->beginPhoton(ulabel); - else - sev->resumePhoton(ulabel); - } else if ( ulabel.gen() > 0 ) { - if (resume_fSuspend == false) - sev->rjoinPhoton(ulabel); - else - sev->rjoin_resumePhoton(ulabel); + void PreUserTrackingAction(const G4Track *track) override + { + spho *label = STrackInfo::GetRef(track); + + if (label == nullptr) + { + PreUserTrackingAction_Optical_FabricateLabel(track); + label = STrackInfo::GetRef(track); + } + + assert(label && label->isDefined()); + + std::array a_label; + label->serialize(a_label); + + spho ulabel = {}; + ulabel.load(a_label); + + U4Random::SetSequenceIndex(ulabel.id); + + bool resume_fSuspend = track == transient_fSuspend_track; + + if (ulabel.gen() == 0) + { + if (resume_fSuspend == false) + sev->beginPhoton(ulabel); + else + sev->resumePhoton(ulabel); + } + else if (ulabel.gen() > 0) + { + if (resume_fSuspend == false) + sev->rjoinPhoton(ulabel); + else + sev->rjoin_resumePhoton(ulabel); + } } - } - - - void PostUserTrackingAction(const G4Track* track) override { - G4TrackStatus tstat = track->GetTrackStatus(); - - bool is_fStopAndKill = tstat == fStopAndKill; - bool is_fSuspend = tstat == fSuspend; - bool is_fStopAndKill_or_fSuspend = is_fStopAndKill || is_fSuspend; - - assert(is_fStopAndKill_or_fSuspend); - - spho ulabel = {}; - get_label(ulabel, track); - if (is_fStopAndKill) { - U4Random::SetSequenceIndex(-1); - sev->finalPhoton(ulabel); - transient_fSuspend_track = nullptr; - } else if (is_fSuspend) { - transient_fSuspend_track = track; + void PostUserTrackingAction(const G4Track *track) override + { + G4TrackStatus tstat = track->GetTrackStatus(); + + bool is_fStopAndKill = tstat == fStopAndKill; + bool is_fSuspend = tstat == fSuspend; + bool is_fStopAndKill_or_fSuspend = is_fStopAndKill || is_fSuspend; + + assert(is_fStopAndKill_or_fSuspend); + + spho ulabel = {}; + get_label(ulabel, track); + + if (is_fStopAndKill) + { + U4Random::SetSequenceIndex(-1); + sev->finalPhoton(ulabel); + transient_fSuspend_track = nullptr; + } + else if (is_fSuspend) + { + transient_fSuspend_track = track; + } } - } }; - struct G4App { - G4App(filesystem::path gdml_file) : - det_cons_(new DetectorConstruction(gdml_file)), - prim_gen_(new PrimaryGenerator), - stepping_(new SteppingAction), - tracking_(new TrackingAction), - event_act_(new EventAction) - { - } - - G4VUserDetectorConstruction* det_cons_; - G4VUserPrimaryGeneratorAction* prim_gen_; - SteppingAction* stepping_; - TrackingAction* tracking_; - EventAction* event_act_; -}; + G4App(filesystem::path gdml_file) + : det_cons_(new DetectorConstruction(gdml_file)), prim_gen_(new PrimaryGenerator), + stepping_(new SteppingAction), tracking_(new TrackingAction), event_act_(new EventAction) + { + } + G4VUserDetectorConstruction *det_cons_; + G4VUserPrimaryGeneratorAction *prim_gen_; + SteppingAction *stepping_; + TrackingAction *tracking_; + EventAction *event_act_; +}; int main(int argc, char **argv) { - argparse::ArgumentParser program("simg4ox", "0.0.0"); - - string gdml_file, macro_name; - bool interactive; - - program.add_argument("-g", "--gdml") - .help("path to GDML file") - .default_value(string("geom.gdml")) - .nargs(1) - .store_into(gdml_file); - - program.add_argument("-m", "--macro") - .help("path to G4 macro") - .default_value(string("run.mac")) - .nargs(1) - .store_into(macro_name); - - program.add_argument("-i", "--interactive") - .help("whether to open an interactive window with a viewer") - .flag() - .store_into(interactive); - - try { - program.parse_args(argc, argv); - } - catch (const exception& err) { - cerr << err.what() << endl; - cerr << program; - exit(EXIT_FAILURE); - } - - // Create global event - sev = SEvt::HighLevelCreate(SEvt::ECPU); - - // Configure Geant4 - // The physics list must be instantiated before other user actions - G4VModularPhysicsList *physics = new FTFP_BERT; - physics->RegisterPhysics(new G4OpticalPhysics); - - G4RunManager run_mgr; - run_mgr.SetUserInitialization(physics); - - G4App* g4app = new G4App(gdml_file); - run_mgr.SetUserInitialization(g4app->det_cons_); - run_mgr.SetUserAction(g4app->prim_gen_); - run_mgr.SetUserAction(g4app->event_act_); - run_mgr.SetUserAction(g4app->tracking_); - run_mgr.SetUserAction(g4app->stepping_); - run_mgr.Initialize(); - - G4UIExecutive *uix = nullptr; - G4VisManager *vis = nullptr; - - if (interactive) { - uix = new G4UIExecutive(argc, argv); - vis = new G4VisExecutive; - vis->Initialize(); - } - - G4UImanager *ui = G4UImanager::GetUIpointer(); - ui->ApplyCommand("/control/execute " + macro_name); - - if (interactive) { - uix->SessionStart(); - } - - delete uix; - - return EXIT_SUCCESS; + argparse::ArgumentParser program("simg4ox", "0.0.0"); + + string gdml_file, macro_name; + bool interactive; + + program.add_argument("-g", "--gdml") + .help("path to GDML file") + .default_value(string("geom.gdml")) + .nargs(1) + .store_into(gdml_file); + + program.add_argument("-m", "--macro") + .help("path to G4 macro") + .default_value(string("run.mac")) + .nargs(1) + .store_into(macro_name); + + program.add_argument("-i", "--interactive") + .help("whether to open an interactive window with a viewer") + .flag() + .store_into(interactive); + + try + { + program.parse_args(argc, argv); + } + catch (const exception &err) + { + cerr << err.what() << endl; + cerr << program; + exit(EXIT_FAILURE); + } + + // Create global event + sev = SEvt::HighLevelCreate(SEvt::ECPU); + + // Configure Geant4 + // The physics list must be instantiated before other user actions + G4VModularPhysicsList *physics = new FTFP_BERT; + physics->RegisterPhysics(new G4OpticalPhysics); + + G4RunManager run_mgr; + run_mgr.SetUserInitialization(physics); + + G4App *g4app = new G4App(gdml_file); + run_mgr.SetUserInitialization(g4app->det_cons_); + run_mgr.SetUserAction(g4app->prim_gen_); + run_mgr.SetUserAction(g4app->event_act_); + run_mgr.SetUserAction(g4app->tracking_); + run_mgr.SetUserAction(g4app->stepping_); + run_mgr.Initialize(); + + G4UIExecutive *uix = nullptr; + G4VisManager *vis = nullptr; + + if (interactive) + { + uix = new G4UIExecutive(argc, argv); + vis = new G4VisExecutive; + vis->Initialize(); + } + + G4UImanager *ui = G4UImanager::GetUIpointer(); + ui->ApplyCommand("/control/execute " + macro_name); + + if (interactive) + { + uix->SessionStart(); + } + + delete uix; + + return EXIT_SUCCESS; } diff --git a/src/simtox.cpp b/src/simtox.cpp index e3890a0..e2d6d25 100644 --- a/src/simtox.cpp +++ b/src/simtox.cpp @@ -10,57 +10,56 @@ using namespace std; - int main(int argc, char **argv) { - unsigned n_photons = 100; + unsigned n_photons = 100; - // Initialize one torch object - storch torch; + // Initialize one torch object + storch torch; - // Assign values to all data members based on the FillGenstep function - torch.gentype = OpticksGenstep_TORCH; - torch.trackid = 0; - torch.matline = 0; - torch.numphoton = n_photons; + // Assign values to all data members based on the FillGenstep function + torch.gentype = OpticksGenstep_TORCH; + torch.trackid = 0; + torch.matline = 0; + torch.numphoton = n_photons; - // Assign default values for position, time, momentum, and other attributes - torch.pos = {-10.0f, -30.0f, -90.0f}; - torch.time = 0.0f; + // Assign default values for position, time, momentum, and other attributes + torch.pos = {-10.0f, -30.0f, -90.0f}; + torch.time = 0.0f; - torch.mom = {0.0f, 0.3f, 1.0f}; - torch.mom = normalize(torch.mom); - torch.weight = 0.0f; + torch.mom = {0.0f, 0.3f, 1.0f}; + torch.mom = normalize(torch.mom); + torch.weight = 0.0f; - torch.pol = {1.0f, 0.0f, 0.0f}; - torch.wavelength = 420.0f; + torch.pol = {1.0f, 0.0f, 0.0f}; + torch.wavelength = 420.0f; - torch.zenith = {0.0f, 1.0f}; - torch.azimuth = {0.0f, 1.0f}; + torch.zenith = {0.0f, 1.0f}; + torch.azimuth = {0.0f, 1.0f}; - torch.radius = 15.0f; - torch.distance = 0.0f; - torch.mode = 255; - torch.type = T_DISC; + torch.radius = 15.0f; + torch.distance = 0.0f; + torch.mode = 255; + torch.type = T_DISC; - cout << torch.desc() << endl; + cout << torch.desc() << endl; - NP* photons = NP::Make(n_photons, 4, 4); + NP *photons = NP::Make(n_photons, 4, 4); - const quad6& qtorch = *reinterpret_cast(&torch); - sphoton* qphotons = reinterpret_cast(photons->bytes()); - int unused = -1; + const quad6 &qtorch = *reinterpret_cast(&torch); + sphoton *qphotons = reinterpret_cast(photons->bytes()); + int unused = -1; - srng rng(12345); + srng rng(12345); - for(unsigned photon_id = 0; photon_id < n_photons; photon_id++) - { - storch::generate(qphotons[photon_id], rng, qtorch, unused, unused); - } + for (unsigned photon_id = 0; photon_id < n_photons; photon_id++) + { + storch::generate(qphotons[photon_id], rng, qtorch, unused, unused); + } - photons->set_meta({"my photon meta info 1", "additional meta info"}); - photons->dump(); - photons->save("out/photons.npy"); + photons->set_meta({"my photon meta info 1", "additional meta info"}); + photons->dump(); + photons->save("out/photons.npy"); - return EXIT_SUCCESS; + return EXIT_SUCCESS; }