7 #include "nusimdata/SimulationBase/MCParticle.h"
8 #include "nusimdata/SimulationBase/MCTruth.h"
11 #include "art/Framework/Core/EDProducer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "art/Framework/Principal/Event.h"
14 #include "art/Framework/Principal/Run.h"
17 #include "artg4tk/geantInit/ArtG4DetectorConstruction.hh"
18 #include "artg4tk/geantInit/ArtG4RunManager.hh"
21 #include "artg4tk/geantInit/ArtG4EventAction.hh"
22 #include "artg4tk/geantInit/ArtG4PrimaryGeneratorAction.hh"
23 #include "artg4tk/geantInit/ArtG4StackingAction.hh"
24 #include "artg4tk/geantInit/ArtG4SteppingAction.hh"
25 #include "artg4tk/geantInit/ArtG4TrackingAction.hh"
30 #include "art/Framework/Services/Optional/RandomNumberGenerator.h"
31 #include "art/Framework/Services/Registry/ServiceHandle.h"
32 #include "artg4tk/services/ActionHolder_service.hh"
33 #include "artg4tk/services/DetectorHolder_service.hh"
34 #include "artg4tk/services/PhysicsListHolder_service.hh"
38 #include "nug4/ParticleNavigation/ParticleList.h"
39 #include "nurandom/RandomUtils/NuRandomService.h"
41 #include "Geant4/G4UImanager.hh"
42 #include "Geant4/G4UIterminal.hh"
58 explicit larg4Main(fhicl::ParameterSet
const&
p);
62 void produce(art::Event&
e)
override;
65 void endRun(art::Run&)
override;
67 std::vector<art::Handle<MCTruthCollection>>
inputCollections(art::Event
const& e)
const;
74 G4UImanager*
UI_{
nullptr};
130 , seed_(
p.get<
long>(
"seed", -1))
131 , macroPath_(
p.get<std::string>(
"macroPath",
"FW_SEARCH_PATH"))
132 , pathFinder_(macroPath_)
133 , g4MacroFile_(
p.get<std::string>(
"visMacro",
"larg4.mac"))
134 , inputCollectionTags_{
p.get<std::vector<art::InputTag>>(
"inputCollections", {})}
139 produces<std::map<int,std::set<int>>>();
140 produces<std::vector<simb::MCParticle>>();
141 produces<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>();
145 art::ServiceHandle<artg4tk::ActionHolderService> actionHolder;
146 art::ServiceHandle<artg4tk::DetectorHolderService> detectorHolder;
148 if (initializeDetectors_) {
149 detectorHolder->initialize();
151 detectorHolder->constructAllLVs();
152 initializeDetectors_ =
false;
156 actionHolder->callArtProduces(producesCollector());
157 detectorHolder->callArtProduces(producesCollector());
160 if (seed_ > 900000000) {
161 throw cet::exception(
"largeant:BadSeedValue")
162 <<
"The provided largeant seed value: " << seed_ <<
" is invalid! Maximum seed value is 9E8.";
166 (
void)art::ServiceHandle<rndm::NuRandomService>()->createEngine(*
this,
"G4Engine",
p,
"seed");
171 pauseAfterEvent_ =
true;
185 mf::LogDebug(
"Main_Run_Manager") <<
"In begin job";
189 runManager_.reset(
new artg4tk::ArtG4RunManager);
195 if (processingRun_++ != 0) {
200 art::ServiceHandle<artg4tk::PhysicsListHolderService const> physicsListHolder;
201 runManager_->SetUserInitialization(physicsListHolder->makePhysicsList());
205 auto detectorHolder = art::ServiceHandle<artg4tk::DetectorHolderService>().
get();
206 runManager_->SetUserInitialization(
207 new artg4tk::ArtG4DetectorConstruction{detectorHolder->worldPhysicalVolume()});
210 auto actionHolder = art::ServiceHandle<artg4tk::ActionHolderService>().
get();
211 actionHolder->initialize();
214 actionHolder->setCurrArtRun(r);
217 runManager_->SetUserAction(
new artg4tk::ArtG4PrimaryGeneratorAction{actionHolder});
224 runManager_->SetUserAction(
new artg4tk::ArtG4SteppingAction{actionHolder});
225 runManager_->SetUserAction(
new artg4tk::ArtG4StackingAction{actionHolder});
226 runManager_->SetUserAction(
new artg4tk::ArtG4EventAction{actionHolder, detectorHolder});
227 runManager_->SetUserAction(
new artg4tk::ArtG4TrackingAction{actionHolder});
229 runManager_->Initialize();
230 physicsListHolder->initializePhysicsList();
233 UI_ = G4UImanager::GetUIpointer();
236 std::string macroLocation =
"";
237 bool macroWasFound = pathFinder_.find_file(g4MacroFile_, macroLocation);
238 mf::LogInfo(
"larg4Main") <<
"Finding path for " << g4MacroFile_ <<
"...\nSearch "
239 << (macroWasFound ?
"successful " :
"unsuccessful ") <<
"and path is: \n"
245 mf::LogInfo(
"larg4Main") <<
"Executing macro: " << g4MacroFile_;
246 std::string commandToExecute =
"/control/execute ";
247 commandToExecute.append(macroLocation);
248 UI_->ApplyCommand(commandToExecute);
251 mf::LogInfo(
"larg4Main") <<
"Unable to find " << g4MacroFile_ <<
" in the path(s) "
257 session_ =
new G4UIterminal;
258 session_->SessionStart();
263 runManager_->BeamOnBeginRun(r.id().run());
271 art::ServiceHandle<artg4tk::ActionHolderService>()->setCurrArtEvent(e);
272 art::ServiceHandle<artg4tk::DetectorHolderService>()->setCurrArtEvent(e);
274 auto const mclists = inputCollections(e);
275 art::ServiceHandle<larg4::MCTruthEventActionService>()->setInputCollections(mclists);
277 art::ServiceHandle<larg4::ParticleListActionService> pla;
278 pla->setInputCollections(mclists);
279 auto const pid = e.getProductID<std::vector<simb::MCParticle>>();
280 pla->setPtrInfo(pid, e.productGetter(pid));
282 runManager_->BeamOnDoOneEvent(e.id().event());
283 runManager_->BeamOnEndEvent();
285 e.put(pla->ParticleCollection());
286 e.put(pla->AssnsMCTruthToMCParticle());
287 e.put(pla->DroppedTracksCollection());
293 if (--processingRun_ != 0) {
297 runManager_->BeamOnEndRun();
300 std::vector<art::Handle<MCTruthCollection>>
303 if (
empty(inputCollectionTags_)) {
304 return e.getMany<std::vector<simb::MCTruth>>();
307 std::vector<art::Handle<MCTruthCollection>> result;
308 for (
auto const& tag : inputCollectionTags_) {
static bool initializeDetectors_
static std::atomic< int > processingRun_
void endRun(art::Run &) override
rmvlevel_(p.get< int >("rmvlevel", 0))
static std::unique_ptr< artg4tk::ArtG4RunManager > runManager_
void produce(art::Event &e) override
std::vector< art::Handle< MCTruthCollection > > inputCollections(art::Event const &e) const
cet::search_path pathFinder_
Contains data associated to particles from detector simulation.
Utility functions to print MC truth information.
Use Geant4's user "hooks" to maintain a list of particles generated by Geant4.
std::vector< simb::MCTruth > MCTruthCollection
std::vector< art::InputTag > inputCollectionTags_
afterEvent_(p.get< std::string >("afterEvent","pass"))
void beginRun(art::Run &r) override
larg4Main(fhicl::ParameterSet const &p)
uiAtBeginRun_(p.get< bool >("uiAtBeginRun", false))
bool empty(FixedBins< T, C > const &) noexcept