Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sim): Allow branch registration in FairGenerator #1575

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simulation/rutherford/macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(maxTestTime 60)

foreach(mcEngine IN LISTS mcEngine_list)
add_test(NAME ex_sim_rutherford_${mcEngine}
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/run_rutherford.sh 10 \"${mcEngine}\")
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/run_rutherford.sh 10 \"${mcEngine}\" false)
set_tests_properties(ex_sim_rutherford_${mcEngine} PROPERTIES
TIMEOUT ${maxTestTime}
PASS_REGULAR_EXPRESSION "Macro finished successfully"
Expand Down
55 changes: 34 additions & 21 deletions fairroot/base/sim/FairMCApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ FairMCApplication::FairMCApplication(const char* name, const char* title, TObjAr
, fMC(nullptr)
, fRun(FairRunSim::Instance())
, fSaveCurrentEvent(kTRUE)
, fState(FairMCApplicationState::kUnknownState)
, fRunInfo()
, fGeometryIsInitialized(kFALSE)
, fOwnedModules()
Expand Down Expand Up @@ -153,7 +152,6 @@ FairMCApplication::FairMCApplication(const FairMCApplication& rhs, std::unique_p
, listDetectors()
, fMC(nullptr)
, fSaveCurrentEvent(kTRUE)
, fState(FairMCApplicationState::kUnknownState)
, fRunInfo()
, fGeometryIsInitialized(kFALSE)
, fOwnedModules()
Expand Down Expand Up @@ -227,7 +225,6 @@ FairMCApplication::FairMCApplication()
, listDetectors()
, fMC(nullptr)
, fSaveCurrentEvent(kTRUE)
, fState(FairMCApplicationState::kUnknownState)
, fRunInfo()
, fGeometryIsInitialized(kFALSE)
, fOwnedModules()
Expand Down Expand Up @@ -283,12 +280,17 @@ void FairMCApplication::InitMC(const char*, const char*)
fTrajFilter = std::make_unique<FairTrajFilter>();
}

InitFinalizer();
fState = FairMCApplicationState::kPostInit;

LOG(info) << "Monte Carlo Engine Initialisation with: " << MCName.Data();
}

//_____________________________________________________________________________
void FairMCApplication::RunMC(Int_t nofEvents)
{
fState = FairMCApplicationState::kRun;

// Reset the time for FairRunInfo. Otherwise the time of the
// first event will include the time needed for initilization.
fRunInfo.Reset();
Expand Down Expand Up @@ -459,7 +461,6 @@ void FairMCApplication::InitOnWorker()
std::lock_guard guard(mtx);
fRootManager->InitSink();
RegisterOutput();
fRootManager->WriteFolder();
}

// Cache thread-local gMC
Expand All @@ -469,6 +470,8 @@ void FairMCApplication::InitOnWorker()
fMC->SetStack(fStack.get());
fMC->SetMagField(fxField);

InitFinalizer();

LOG(info) << "Monte Carlo Engine Worker Initialisation with: " << fMC->GetName();
}

Expand Down Expand Up @@ -634,8 +637,8 @@ void FairMCApplication::FinishEvent()
LOG(debug) << "[" << fRootManager->GetInstanceId()
<< " FairMCMCApplication::FinishEvent: " << fMCEventHeader->GetEventID() << " (MC "
<< gMC->CurrentEvent() << ")";
if (gMC->IsMT()
&& fRun->GetSink()->GetSinkType() == kONLINESINK) { // fix the rare case when running G4 multithreaded on MQ
if (gMC->IsMT() && fRun->GetSink()->GetSinkType() == kONLINESINK)
{ // fix the rare case when running G4 multithreaded on MQ
fMCEventHeader->SetEventID(gMC->CurrentEvent() + 1);
}

Expand Down Expand Up @@ -817,7 +820,7 @@ void FairMCApplication::ConstructGeometry()

gGeoManager->RefreshPhysicalNodes(kFALSE);

fState = FairMCApplicationState::kUnknownState;
fState = FairMCApplicationState::kInit;
}

// ____________________________________________________________________________
Expand All @@ -831,6 +834,8 @@ Bool_t FairMCApplication::MisalignGeometry()
//_____________________________________________________________________________
void FairMCApplication::InitGeometry()
{
if (FairMCApplicationState::kInit != fState)
LOG(fatal) << "InitGeometry possible in kInit state only";
fState = FairMCApplicationState::kInitGeometry;

LOG(info) << "FairMCApplication::InitGeometry: " << fRootManager->GetInstanceId();
Expand Down Expand Up @@ -868,16 +873,9 @@ void FairMCApplication::InitGeometry()
/// save Geo Params in Output file
if (fRootManager && !fParent) {
RegisterOutput();

/**Tasks has to be initialized here, they have access to the detector branches and still can create objects in
* the tree*/
/// There is always a Main Task !
/// so .. always a InitTasks() is called <D.B>
if (fFairTaskList) {
InitTasks();
if (GetIsMT()) {
fRootManager->RemoveOutputFolderForMtMode();
}

fRootManager->WriteFolder();
}
fMCEventHeader->SetRunID(runId);

Expand Down Expand Up @@ -921,7 +919,26 @@ void FairMCApplication::InitGeometry()

fGeometryIsInitialized = kTRUE;

fState = FairMCApplicationState::kUnknownState;
fState = FairMCApplicationState::kInit;
}

//_____________________________________________________________________________
void FairMCApplication::InitFinalizer()
{
/** Initialize the event generator */
if (fEvGen) {
fEvGen->Init();
}
/**Tasks has to be initialized here, they have access to the detector branches and still can create objects in
* the tree*/
if (fFairTaskList) {
InitTasks();
}
{
std::lock_guard guard(mtx);
fRootManager->WriteFolder();
fRootManager->RemoveOutputFolderForMtMode();
}
}

//_____________________________________________________________________________
Expand Down Expand Up @@ -1009,10 +1026,6 @@ void FairMCApplication::AddIons()
<< pdgDatabase->GetParticle(ion->GetName())->PdgCode();
}
}
/** Initialize the event generator */
if (fEvGen) {
fEvGen->Init();
}
}
//_____________________________________________________________________________
void FairMCApplication::AddParticles()
Expand Down
22 changes: 14 additions & 8 deletions fairroot/base/sim/FairMCApplication.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
Expand Down Expand Up @@ -46,9 +46,12 @@ class TTask;

enum class FairMCApplicationState
{
kUnknownState,
kPreInit,
kConstructGeometry,
kInitGeometry
kInit,
kInitGeometry,
kPostInit,
kRun
};

/**
Expand Down Expand Up @@ -88,8 +91,7 @@ class FairMCApplication : public TVirtualMCApplication
/** Add user defined ions (optional)
Called by TVirtualMC.
TGeant3 calls AddIons() first, then InitGeometry().
TGeant4 calls InitGeometry() first, then AddIons().
This function also initializes event generators.*/
TGeant4 calls InitGeometry() first, then AddIons().*/
void AddIons() override; // MC Application
/**
*Add user defined Tasks to be executed after each event (optional)
Expand Down Expand Up @@ -127,8 +129,7 @@ class FairMCApplication : public TVirtualMCApplication
/** Initialize geometry
Called by TVirtualMC.
TGeant3 calls AddIons() first, then InitGeometry().
TGeant4 calls InitGeometry() first, then AddIons().
This function also registers detectors.*/
TGeant4 calls InitGeometry() first, then AddIons().*/
void InitGeometry() override; // MC Application
/** Initialize MC engine */
void InitMC(const char* setup, const char* cuts);
Expand Down Expand Up @@ -241,6 +242,11 @@ class FairMCApplication : public TVirtualMCApplication

/** Register output */
void RegisterOutput();
/** Finalize the init stage:
initialize event generator,
initialize tasks,
write output folder.*/
void InitFinalizer();

void UndoGeometryModifications();

Expand Down Expand Up @@ -320,7 +326,7 @@ class FairMCApplication : public TVirtualMCApplication
Bool_t fSaveCurrentEvent;

/** Current state */
FairMCApplicationState fState; //!
FairMCApplicationState fState{FairMCApplicationState::kPreInit}; //!

ClassDefOverride(FairMCApplication, 5);

Expand Down
3 changes: 0 additions & 3 deletions fairroot/base/sink/FairRootFileSink.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,6 @@ void FairRootFileSink::WriteFolder()
fOutTree = new TTree(FairRootManager::GetTreeName(), Form("/%s", FairRootManager::GetFolderName()), 99);
TruncateBranchNames();
CreatePersistentBranchesAny();

// Delete the folder to make place in the gROOT for fOutFolder created by the Geant4MT threads
gROOT->GetRootFolder()->Remove(fOutFolder);
}
}

Expand Down
11 changes: 9 additions & 2 deletions fairroot/base/steer/FairRootManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <TNamed.h>
#include <TObjArray.h> // for TObjArray
#include <TObjString.h> // for TObjString
#include <TROOT.h> // for gROOT
#include <TTree.h> // for TTree
#include <algorithm> // for find
#include <atomic>
Expand Down Expand Up @@ -221,8 +222,8 @@ TClonesArray* FairRootManager::Register(TString branchName, TString className, T

TClonesArray* FairRootManager::GetEmptyTClonesArray(TString branchName)
{
if (fActiveContainer.find(branchName)
!= fActiveContainer.end()) { // if a TClonesArray is registered in the active container
if (fActiveContainer.find(branchName) != fActiveContainer.end())
{ // if a TClonesArray is registered in the active container
if (fActiveContainer[branchName] == 0) { // the address of the TClonesArray is still valid
std::cout << "-E- FairRootManager::GetEmptyTClonesArray: Container deleted outside FairRootManager!"
<< std::endl;
Expand Down Expand Up @@ -383,6 +384,12 @@ void FairRootManager::WriteFolder()
}
}

void FairRootManager::RemoveOutputFolderForMtMode()
{
auto rootFolder = static_cast<TFolder*>(gROOT->GetRootFolder());
rootFolder->Remove(rootFolder->FindObject(GetFolderName()));
}

Bool_t FairRootManager::SpecifyRunId()
{
if (!fSource) {
Expand Down
7 changes: 6 additions & 1 deletion fairroot/base/steer/FairRootManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
#include <TRefArray.h> // for TRefArray
#include <TString.h> // for TString, operator<
#include <fairlogger/Logger.h>
#include <list>
#include <map> // for map, multimap, etc
#include <memory>
#include <string>
#include <type_traits> // is_pointer, remove_pointer, is_const, remove...
#include <typeinfo>
#include <list>

class BinaryFunctor;
class FairEventHeader;
Expand Down Expand Up @@ -218,6 +218,11 @@ class FairRootManager : public TObject
void WriteFileHeader(FairFileHeader* f);
/**Write the folder structure used to create the tree to the output file */
void WriteFolder();
/**
* \brief Internal: Remove the folder describing the output tree structure from gROOT, called in TGeant4 MT
* simulation mode
*/
void RemoveOutputFolderForMtMode();

/**Check the maximum event number we can run to*/
Int_t CheckMaxEventNo(Int_t EvtEnd = 0);
Expand Down
6 changes: 6 additions & 0 deletions fairroot/base/steer/FairRunSim.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ class FairRunSim : public FairRun
private:
FairRunSim(const FairRunSim& M);
FairRunSim& operator=(const FairRunSim&) { return *this; }
/**
* Performs simulation initialization:
* - runs simulation config (construct TGeant3/TGeant4, configure TGeant3/TGeant4, create stack),
* - runs FairMCApplication->InitMC() (set stack, run fMC->Init(), run fMC->BuildPhysics()),
* - runs postinit config (sets some VMC properties, which are only allowed after init).
*/
void SetMCConfig();
void CheckFlukaExec();

Expand Down
Loading