Skip to content

Commit

Permalink
o2-sim: Apply dynamic detector lib loading to all detectors
Browse files Browse the repository at this point in the history
* code reduction:
- automatic factory method via DetImpl base class
- helper macro to implement extern "C" hooks

* application of dynamic loading during geometry construction
  and in the HitMerger

* removal of detector linking to o2-sim in CMakeFile

The benefit is ~60MB of memory reduction when only simulating
single detectors `o2-sim-serial -m TPC` and a faster load time in general.
  • Loading branch information
sawenzel committed May 29, 2024
1 parent 5b5abc2 commit 9ddc990
Show file tree
Hide file tree
Showing 28 changed files with 134 additions and 150 deletions.
23 changes: 23 additions & 0 deletions Detectors/Base/include/DetectorsBase/Detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@

#include <fairmq/FwdDecls.h>

// macro implementation helper to code unmanged "C" function
// constructing a detector
#define O2DetectorCreatorImpl(creatorfuncname, detname) \
extern "C" { \
o2::base::Detector* create_detector_##detname(bool active) \
{ \
return creatorfuncname(active); \
} \
}

namespace o2::base
{

Expand Down Expand Up @@ -302,6 +312,19 @@ class DetImpl : public o2::base::Detector
// offer same constructors as base
using Detector::Detector;

/// automatic implementation of static Detector factory functions
template <typename... Args>
static o2::base::Detector* create(Args&&... args)
{
if constexpr (std::is_constructible_v<Det, Args...>) {
return new Det(std::forward<Args>(args)...);
} else {
static_assert(std::is_constructible_v<Det, Args...>,
"No matching constructor found in Derived class.");
return nullptr;
}
}

// default implementation for getHitBranchNames
std::string getHitBranchNames(int probe) const override
{
Expand Down
1 change: 1 addition & 0 deletions Detectors/CPV/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using namespace o2::cpv;

ClassImp(Detector);
O2DetectorCreatorImpl(o2::cpv::Detector::create, cpv);

Detector::Detector(Bool_t active)
: o2::base::DetImpl<Detector>("CPV", active),
Expand Down
1 change: 1 addition & 0 deletions Detectors/EMCAL/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using namespace o2::emcal;

ClassImp(Detector);
O2DetectorCreatorImpl(o2::emcal::Detector::create, emc);

Detector::Detector(Bool_t active)
: o2::base::DetImpl<Detector>("EMC", active),
Expand Down
1 change: 1 addition & 0 deletions Detectors/FIT/FDD/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using o2::fdd::Geometry;
using o2::fdd::Hit;

ClassImp(o2::fdd::Detector);
O2DetectorCreatorImpl(o2::fdd::Detector::create, fdd);

//_____________________________________________________________________________
Detector::Detector(Bool_t Active)
Expand Down
1 change: 1 addition & 0 deletions Detectors/FIT/FT0/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using namespace o2::ft0;
using o2::ft0::Geometry;

ClassImp(Detector);
O2DetectorCreatorImpl(o2::ft0::Detector::create, ft0);

Detector::Detector(Bool_t Active)
: o2::base::DetImpl<Detector>("FT0", Active), mIdSens1(0), mPMTeff(nullptr), mHits(o2::utils::createSimVector<o2::ft0::HitType>()), mTrackIdTop(-1), mTrackIdMCPtop(-1)
Expand Down
1 change: 1 addition & 0 deletions Detectors/FIT/FV0/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ using namespace o2::fv0;
using o2::fv0::Geometry;

ClassImp(Detector);
O2DetectorCreatorImpl(o2::fv0::Detector::create, fv0);

Detector::Detector()
: o2::base::DetImpl<Detector>("FV0", kTRUE),
Expand Down
1 change: 1 addition & 0 deletions Detectors/HMPID/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1379,3 +1379,4 @@ void Detector::defineOpticalProperties()
} // end namespace o2

ClassImp(o2::hmpid::Detector);
O2DetectorCreatorImpl(o2::hmpid::Detector::create, hmp);
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ class Detector : public o2::base::DetImpl<Detector>
/// kFALSE for inactive detectors
Detector(Bool_t active, TString name = "ITS");

// Factory method
static o2::base::Detector* create(const char* name, bool active)
{
return new Detector(active, name);
}

/// Default constructor
Detector();

Expand Down
5 changes: 2 additions & 3 deletions Detectors/ITSMFT/ITS/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1318,10 +1318,9 @@ Hit* Detector::addHit(int trackID, int detID, const TVector3& startPos, const TV

ClassImp(o2::its::Detector);

// Define Factory method for calling from the outside
extern "C" {
o2::base::Detector* create_detector_its(const char* name, bool active)
{
return o2::its::Detector::create(name, active);
}
return o2::its::Detector::create(active, name);
}
}
1 change: 1 addition & 0 deletions Detectors/ITSMFT/MFT/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using o2::itsmft::Hit;
using namespace o2::mft;

ClassImp(o2::mft::Detector);
O2DetectorCreatorImpl(o2::mft::Detector::create, mft);

//_____________________________________________________________________________
Detector::Detector()
Expand Down
1 change: 1 addition & 0 deletions Detectors/MUON/MCH/Simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "TGeoManager.h"

ClassImp(o2::mch::Detector);
O2DetectorCreatorImpl(o2::mch::Detector::create, mch);

namespace o2
{
Expand Down
1 change: 1 addition & 0 deletions Detectors/MUON/MID/Simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "FairVolume.h"

ClassImp(o2::mid::Detector);
O2DetectorCreatorImpl(o2::mid::Detector::create, mid);

namespace o2
{
Expand Down
1 change: 1 addition & 0 deletions Detectors/PHOS/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using namespace o2::phos;

ClassImp(Detector);
O2DetectorCreatorImpl(o2::phos::Detector::create, phs);

Detector::Detector(Bool_t active)
: o2::base::DetImpl<Detector>("PHS", active),
Expand Down
1 change: 1 addition & 0 deletions Detectors/TOF/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using namespace o2::tof;

ClassImp(Detector);
O2DetectorCreatorImpl(o2::tof::Detector::create, tof);

Detector::Detector(Bool_t active)
: o2::base::DetImpl<Detector>("TOF", active), mEventNr(0), mTOFHoles(kTRUE), mHits(o2::utils::createSimVector<HitType>())
Expand Down
6 changes: 0 additions & 6 deletions Detectors/TPC/simulation/include/TPCSimulation/Detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ class Detector : public o2::base::DetImpl<Detector>
*/
Detector(Bool_t Active);

// Factory method
static o2::base::Detector* create(bool active)
{
return new Detector(active);
}

/** default constructor */
Detector();

Expand Down
8 changes: 1 addition & 7 deletions Detectors/TPC/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3217,10 +3217,4 @@ std::string Detector::getHitBranchNames(int probe) const

ClassImp(o2::tpc::Detector);

// Define Factory method for calling from the outside
extern "C" {
o2::base::Detector* create_detector_tpc(bool active)
{
return o2::tpc::Detector::create(active);
}
}
O2DetectorCreatorImpl(o2::tpc::Detector::create, tpc)
1 change: 1 addition & 0 deletions Detectors/TRD/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,4 @@ void Detector::addAlignableVolumes() const
}

ClassImp(Detector);
O2DetectorCreatorImpl(o2::trd::Detector::create, trd)
3 changes: 2 additions & 1 deletion Detectors/Upgrades/ALICE3/ECal/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,5 @@ o2::itsmft::Hit* Detector::addHit(int trackID, int detID, const TVector3& startP
} // namespace ecal
} // namespace o2

ClassImp(o2::ecal::Detector);
ClassImp(o2::ecal::Detector);
O2DetectorCreatorImpl(o2::ecal::Detector::create, ecl);
1 change: 1 addition & 0 deletions Detectors/Upgrades/ALICE3/FCT/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,4 @@ Hit* Detector::addHit(int trackID, int detID, const TVector3& startPos, const TV
}

ClassImp(o2::fct::Detector);
O2DetectorCreatorImpl(o2::fct::Detector::create, fct);
1 change: 1 addition & 0 deletions Detectors/Upgrades/ALICE3/FT3/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,4 @@ Hit* Detector::addHit(int trackID, int detID, const TVector3& startPos, const TV
}

ClassImp(o2::ft3::Detector);
O2DetectorCreatorImpl(o2::ft3::Detector::create, ft3);
3 changes: 2 additions & 1 deletion Detectors/Upgrades/ALICE3/MID/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,5 @@ o2::itsmft::Hit* Detector::addHit(int trackID, int detID, const TVector3& startP
return &(mHits->back());
}
} // namespace o2::mi3
ClassImp(o2::mi3::Detector);
ClassImp(o2::mi3::Detector);
O2DetectorCreatorImpl(o2::mi3::Detector::create, mi3);
3 changes: 2 additions & 1 deletion Detectors/Upgrades/ALICE3/RICH/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,5 @@ void Detector::prepareLayout()
} // namespace rich
} // namespace o2

ClassImp(o2::rich::Detector);
ClassImp(o2::rich::Detector);
O2DetectorCreatorImpl(o2::rich::Detector::create, rch);
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ class Detector : public o2::base::DetImpl<Detector>
Detector();
~Detector();

// Factory method
static o2::base::Detector* create(bool active)
{
return new Detector(active);
}

void ConstructGeometry() override;

o2::itsmft::Hit* addHit(int trackID, int detID, const TVector3& startPos, const TVector3& endPos,
Expand Down
9 changes: 1 addition & 8 deletions Detectors/Upgrades/ALICE3/TRK/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,4 @@ o2::itsmft::Hit* Detector::addHit(int trackID, int detID, const TVector3& startP
} // namespace o2

ClassImp(o2::trk::Detector);

// Define Factory method for calling from the outside
extern "C" {
o2::base::Detector* create_detector_trk(bool active)
{
return o2::trk::Detector::create(active);
}
}
O2DetectorCreatorImpl(o2::trk::Detector::create, trk);
1 change: 1 addition & 0 deletions Detectors/ZDC/simulation/src/Detector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using namespace o2::zdc;

ClassImp(o2::zdc::Detector);
O2DetectorCreatorImpl(o2::zdc::Detector::create, zdc);
#define kRaddeg TMath::RadToDeg()

//_____________________________________________________________________________
Expand Down
Loading

0 comments on commit 9ddc990

Please sign in to comment.