Skip to content

Commit 0a4e061

Browse files
ChristianTackeGSIdennisklein
authored andcommitted
refactor(Base): Introduce FairGeoLoader::LoadAndCreate
... and use it around
1 parent 14eb6f9 commit 0a4e061

5 files changed

Lines changed: 38 additions & 45 deletions

File tree

examples/common/passive/FairCave.cxx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "FairCave.h"
1010

1111
#include "FairGeoCave.h" // for FairGeoCave
12-
#include "FairGeoInterface.h" // for FairGeoInterface
1312
#include "FairGeoLoader.h" // for FairGeoLoader
1413
#include "FairGeoNode.h" // for FairGeoNode
1514
#include "FairGeoPassivePar.h" // for FairGeoPassivePar
@@ -24,17 +23,9 @@
2423

2524
void FairCave::ConstructGeometry()
2625
{
27-
FairGeoLoader& loader = GetGeometryLoader();
28-
FairGeoInterface* GeoInterface = loader.getGeoInterface();
29-
FairGeoCave* MGeo = new FairGeoCave();
30-
MGeo->setGeomFile(GetGeometryFileName());
31-
GeoInterface->addGeoModule(MGeo);
32-
Bool_t rc = GeoInterface->readSet(MGeo);
33-
if (rc) {
34-
MGeo->create(loader.getGeoBuilder());
35-
}
26+
FairGeoSet& MGeo = GetGeometryLoader().LoadAndCreate<FairGeoCave>(GetGeometryFileName());
3627

37-
TList* volList = MGeo->getListOfVolumes();
28+
TList* volList = MGeo.getListOfVolumes();
3829
// store geo parameter
3930
FairRun* fRun = FairRun::Instance();
4031
FairRuntimeDb* rtdb = FairRun::Instance()->GetRuntimeDb();

fairroot/base/sim/FairModule.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "FairGeoInterface.h" // for FairGeoInterface
1212
#include "FairGeoLoader.h" // for FairGeoLoader
1313
#include "FairGeoNode.h" // for FairGeoNode
14-
#include "FairGeoVolume.h" // for FairGeoVolume
15-
#include "FairLogger.h"
14+
#include "FairGeoSet.h"
15+
#include "FairGeoVolume.h"
1616
#include "FairRun.h" // for FairRun
1717
#include "FairRuntimeDb.h" // for FairRuntimeDb
1818

@@ -26,7 +26,10 @@
2626
#include <TRefArray.h> // for TRefArray
2727
#include <TString.h> // for TString, operator!=
2828
#include <TVirtualMC.h>
29+
#include <fairlogger/Logger.h>
2930
#include <string>
31+
#include <type_traits>
32+
#include <utility>
3033
#include <vector>
3134

3235
class FairRunSim;
@@ -197,24 +200,17 @@ class FairModule : public TNamed
197200
template<class T, class U>
198201
void FairModule::ConstructASCIIGeometry(TString containerName)
199202
{
200-
FairGeoLoader& loader = GetGeometryLoader();
201-
FairGeoInterface* GeoInterface = loader.getGeoInterface();
202-
T* MGeo = new T();
203-
MGeo->print();
204-
MGeo->setGeomFile(GetGeometryFileName());
205-
GeoInterface->addGeoModule(MGeo); // takes ownership!
206-
Bool_t rc = GeoInterface->readSet(MGeo);
207-
if (rc) {
208-
MGeo->create(loader.getGeoBuilder());
209-
}
203+
static_assert(std::is_base_of_v<FairGeoSet, T>);
204+
static_assert(std::is_base_of_v<FairParSet, U>);
205+
FairGeoSet& MGeo = GetGeometryLoader().LoadAndCreate<T>(GetGeometryFileName());
210206

211-
TList* volList = MGeo->getListOfVolumes();
207+
TList* volList = MGeo.getListOfVolumes();
212208
// store geo parameter
213209
FairRun* fRun = FairRun::Instance();
214210
FairRuntimeDb* rtdb = FairRun::Instance()->GetRuntimeDb();
215211

216212
if ("" != containerName) {
217-
LOG(info) << "Add GeoNodes for " << MGeo->getDescription() << " to container " << containerName;
213+
LOG(info) << "Add GeoNodes for " << MGeo.getDescription() << " to container " << containerName;
218214

219215
// U par=(U)(rtdb->getContainer(containerName));
220216
U* par = static_cast<U*>(rtdb->getContainer(containerName));

fairroot/geobase/FairGeoLoader.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define FairGeoLOADER_H
1818

1919
#include "FairGeoInterface.h"
20+
#include "FairGeoSet.h"
2021

2122
#include <Rtypes.h> // for FairGeoLoader::Class, etc
2223
#include <TNamed.h> // for TNamed
@@ -41,6 +42,12 @@ class FairGeoLoader : public TNamed
4142
/** static access method*/
4243
static FairGeoLoader* Instance();
4344

45+
/**
46+
* \brief Even more low level helper function for creating goemetry
47+
*/
48+
template<class T, class... Args>
49+
FairGeoSet& LoadAndCreate(const char* geomfile, Args&&... args);
50+
4451
private:
4552
FairGeoLoader(const FairGeoLoader&);
4653
FairGeoLoader& operator=(const FairGeoLoader&);
@@ -57,4 +64,19 @@ class FairGeoLoader : public TNamed
5764
ClassDefOverride(FairGeoLoader, 0);
5865
};
5966

67+
template<class T, class... Args>
68+
FairGeoSet& FairGeoLoader::LoadAndCreate(const char* geomfile, Args&&... args)
69+
{
70+
static_assert(std::is_base_of_v<FairGeoSet, T>);
71+
FairGeoSet* MGeo = new T(std::forward<Args>(args)...);
72+
MGeo->print();
73+
MGeo->setGeomFile(geomfile);
74+
fInterface.addGeoModule(MGeo); // takes ownership!
75+
Bool_t rc = fInterface.readSet(MGeo);
76+
if (rc) {
77+
MGeo->create(fGeoBuilder.get());
78+
}
79+
return *MGeo;
80+
}
81+
6082
#endif

templates/project_root_containers/passive/MyCave.cxx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// -------------------------------------------------------------------------
1313
#include "MyCave.h"
1414

15-
#include "FairGeoInterface.h" // for FairGeoInterface
1615
#include "FairGeoLoader.h" // for FairGeoLoader
1716
#include "FairGeoNode.h" // for FairGeoNode
1817
#include "FairGeoVolume.h" // for FairGeoVolume
@@ -27,16 +26,9 @@
2726

2827
void MyCave::ConstructGeometry()
2928
{
30-
FairGeoLoader& loader = GetGeometryLoader();
31-
FairGeoInterface* GeoInterface = loader.getGeoInterface();
32-
MyGeoCave* MGeo = new MyGeoCave();
33-
MGeo->setGeomFile(GetGeometryFileName());
34-
GeoInterface->addGeoModule(MGeo);
35-
Bool_t rc = GeoInterface->readSet(MGeo);
36-
if (rc) {
37-
MGeo->create(loader.getGeoBuilder());
38-
}
29+
GetGeometryLoader().LoadAndCreate<MyGeoCave>(GetGeometryFileName());
3930
}
31+
4032
MyCave::MyCave()
4133
: FairModule()
4234
{}

templates/project_stl_containers/passive/MyCave.cxx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// -------------------------------------------------------------------------
1313
#include "MyCave.h"
1414

15-
#include "FairGeoInterface.h" // for FairGeoInterface
1615
#include "FairGeoLoader.h" // for FairGeoLoader
1716
#include "FairGeoNode.h" // for FairGeoNode
1817
#include "FairGeoVolume.h" // for FairGeoVolume
@@ -27,16 +26,9 @@
2726

2827
void MyCave::ConstructGeometry()
2928
{
30-
FairGeoLoader& loader = GetGeometryLoader();
31-
FairGeoInterface* GeoInterface = loader.getGeoInterface();
32-
MyGeoCave* MGeo = new MyGeoCave();
33-
MGeo->setGeomFile(GetGeometryFileName());
34-
GeoInterface->addGeoModule(MGeo);
35-
Bool_t rc = GeoInterface->readSet(MGeo);
36-
if (rc) {
37-
MGeo->create(loader.getGeoBuilder());
38-
}
29+
GetGeometryLoader().LoadAndCreate<MyGeoCave>(GetGeometryFileName());
3930
}
31+
4032
MyCave::MyCave()
4133
: FairModule()
4234
{}

0 commit comments

Comments
 (0)