Skip to content

Commit

Permalink
Add G4SD "simple calorimeter" that outputs to JSON (celeritas-project…
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Nov 22, 2023
1 parent 008c9d4 commit d4daf26
Show file tree
Hide file tree
Showing 13 changed files with 562 additions and 41 deletions.
94 changes: 69 additions & 25 deletions app/celer-g4/DetectorConstruction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

#include "corecel/cont/ArrayIO.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/OutputRegistry.hh"
#include "celeritas/Quantities.hh"
#include "celeritas/field/RZMapFieldInput.hh"
#include "celeritas/field/RZMapFieldParams.hh"
#include "celeritas/field/UniformFieldData.hh"
#include "accel/AlongStepFactory.hh"
#include "accel/GeantSimpleCalo.hh"
#include "accel/RZMapMagneticField.hh"
#include "accel/SetupOptions.hh"
#include "accel/SharedParams.hh"
Expand Down Expand Up @@ -68,10 +70,37 @@ DetectorConstruction::DetectorConstruction(SPParams params)
*/
G4VPhysicalVolume* DetectorConstruction::Construct()
{
CELER_EXPECT(GlobalSetup::Instance()->GetSDSetupOptions().enabled
== (GlobalSetup::Instance()->input().sd_type
!= SensitiveDetectorType::none));

auto geo = this->construct_geo();
CELER_ASSERT(geo.world);
detectors_ = std::move(geo.detectors);

if (!detectors_.empty()
&& GlobalSetup::Instance()->input().sd_type
== SensitiveDetectorType::simple_calo)
{
this->foreach_detector([this](auto start, auto stop) {
std::string name = start->first;
std::vector<G4LogicalVolume*> volumes;
for (auto iter = start; iter != stop; ++iter)
{
volumes.push_back(iter->second);
}
CELER_LOG(debug) << "Creating GeantSimpleCalo '" << name
<< "' with " << volumes.size() << " volumes";

// Create calo manager
simple_calos_.push_back(std::make_shared<GeantSimpleCalo>(
std::move(name), params_, std::move(volumes)));

// Add to output
params_->output_reg()->insert(simple_calos_.back());
});
}

auto field = this->construct_field();
CELER_ASSERT(field.along_step);
GlobalSetup::Instance()->SetAlongStepFactory(std::move(field.along_step));
Expand Down Expand Up @@ -224,38 +253,53 @@ void DetectorConstruction::ConstructSDandField()
convert_to_geant(field_options.delta_intersection, CLHEP::cm));
}

if (detectors_.empty())
auto sd_type = GlobalSetup::Instance()->input().sd_type;
auto* sd_manager = G4SDManager::GetSDMpointer();

if (sd_type == SensitiveDetectorType::none)
{
return;
CELER_LOG_LOCAL(debug) << "No sensitive detectors requested";
}

CELER_LOG_LOCAL(status) << "Loading sensitive detectors";

G4SDManager* sd_manager = G4SDManager::GetSDMpointer();
this->foreach_detector([sd_manager](auto start, auto stop) {
// Create one detector for all the volumes
auto detector = std::make_unique<SensitiveDetector>(start->first);

// Attach sensitive detectors
for (auto iter = start; iter != stop; ++iter)
else if (sd_type == SensitiveDetectorType::simple_calo)
{
CELER_LOG_LOCAL(status) << "Attaching simple calorimeters";
for (auto& calo : simple_calos_)
{
CELER_LOG_LOCAL(debug)
<< "Attaching '" << iter->first << "'@" << detector.get()
<< " to '" << iter->second->GetName() << "'@"
<< static_cast<void const*>(iter->second);
iter->second->SetSensitiveDetector(detector.get());
// Create and attach SD
auto detector = calo->MakeSensitiveDetector();
CELER_ASSERT(detector);
sd_manager->AddNewDetector(detector.release());
}
}
else if (sd_type == SensitiveDetectorType::event_hit)
{
CELER_LOG_LOCAL(status) << "Creating SDs";
this->foreach_detector([sd_manager](auto start, auto stop) {
// Create one detector for all the volumes
auto detector = std::make_unique<SensitiveDetector>(start->first);

// Hand SD to the manager
sd_manager->AddNewDetector(detector.release());
// Attach sensitive detectors
for (auto iter = start; iter != stop; ++iter)
{
CELER_LOG_LOCAL(debug)
<< "Attaching '" << iter->first << "'@" << detector.get()
<< " to '" << iter->second->GetName() << "'@"
<< static_cast<void const*>(iter->second);
iter->second->SetSensitiveDetector(detector.get());
}

// Add to ROOT output
if (RootIO::use_root())
{
RootIO::Instance()->AddSensitiveDetector(start->first);
}
});
// Hand SD to the manager
sd_manager->AddNewDetector(detector.release());

// Add to ROOT output
if (RootIO::use_root())
{
RootIO::Instance()->AddSensitiveDetector(start->first);
}
});
}
}

//---------------------------------------------------------------------------//
/*!
* Apply a function to the range of volumes for each detector.
Expand Down
3 changes: 3 additions & 0 deletions app/celer-g4/DetectorConstruction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class G4MagneticField;
namespace celeritas
{
class SharedParams;
class GeantSimpleCalo;

namespace app
{
Expand Down Expand Up @@ -49,6 +50,7 @@ class DetectorConstruction final : public G4VUserDetectorConstruction
using MapDetectors = std::multimap<std::string, G4LogicalVolume*>;
using AlongStepFactory = SetupOptions::AlongStepFactory;
using SPMagneticField = std::shared_ptr<G4MagneticField>;
using SPSimpleCalo = std::shared_ptr<GeantSimpleCalo>;

struct GeoData
{
Expand All @@ -67,6 +69,7 @@ class DetectorConstruction final : public G4VUserDetectorConstruction

MapDetectors detectors_;
SPMagneticField mag_field_;
std::vector<SPSimpleCalo> simple_calos_;

//// METHODS ////

Expand Down
1 change: 1 addition & 0 deletions app/celer-g4/RunInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ char const* to_cstring(SensitiveDetectorType value)
{
static EnumStringMapper<SensitiveDetectorType> const to_cstring_impl{
"none",
"simple_calo",
"event_hit",
};
return to_cstring_impl(value);
Expand Down
1 change: 1 addition & 0 deletions app/celer-g4/RunInput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum class PhysicsListSelection
enum class SensitiveDetectorType
{
none, //!< No SDs
simple_calo, //!< Integrated energy deposition over all events
event_hit, //!< Record basic hit data
size_,
};
Expand Down
11 changes: 4 additions & 7 deletions app/celer-g4/test-harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,22 @@ def strtobool(text):
"secondary_stack_factor": 2,
"physics_list": "ftfp_bert",
"field_type": "uniform",
"field": [
0.0,
0.0,
1.0
],
"field": [ 0.0, 0.0, 1.0 ],
"field_options": {
"minimum_step": 0.000001,
"delta_chord": 0.025,
"delta_intersection": 0.00001,
"epsilon_step": 0.00001
},
"sd_type": "event_hit" if use_root else "simple_calo",
"step_diagnostic": ext == "none",
"step_diagnostic_bins": 10
"step_diagnostic_bins": 8,
}

with open(inp_file, "w") as f:
json.dump(inp, f, indent=1)

print("Running", exe, file=stderr)
print("Running", exe, inp_file, file=stderr)
result = subprocess.run([exe, inp_file])

if result.returncode:
Expand Down
2 changes: 2 additions & 0 deletions src/accel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ set(PUBLIC_DEPS Celeritas::celeritas Celeritas::corecel ${Geant4_LIBRARIES})
list(APPEND SOURCES
AlongStepFactory.cc
FastSimulationOffload.cc
GeantSimpleCalo.cc
GeantStepDiagnostic.cc
Logger.cc
LocalTransporter.cc
SetupOptions.cc
SetupOptionsMessenger.cc
SharedParams.cc
SimpleOffload.cc
detail/GeantSimpleCaloSD.cc
detail/HitManager.cc
detail/HitProcessor.cc
detail/SensDetInserter.cc
Expand Down

0 comments on commit d4daf26

Please sign in to comment.