3 #include <FairGeoBuilder.h>
4 #include <FairGeoInterface.h>
5 #include <FairGeoLoader.h>
6 #include <FairGeoMedia.h>
7 #include <FairLogger.h>
8 #include <FairLogger.h>
9 #include <FairModule.h>
12 #include <RtypesCore.h>
13 #include <TCollection.h>
14 #include <TDirectory.h>
15 #include <TGeoManager.h>
16 #include <TGeoMaterial.h>
17 #include <TGeoMatrix.h>
18 #include <TGeoMedium.h>
20 #include <TGeoVolume.h>
23 #include <TObjArray.h>
32 namespace GeometryUtils {
34 TList* media = gGeoManager->GetListOfMedia();
37 while ((med =
static_cast<TGeoMedium*
>(next1()))) {
38 LOG(info) <<
"Medium " << med->GetName() <<
" with ID " << med->GetId();
44 TList* material = gGeoManager->GetListOfMaterials();
45 TIter next1(material);
47 while ((mat =
static_cast<TGeoMaterial*
>(next1()))) {
48 LOG(info) <<
"Material " << mat->GetName() <<
" with ID "
55 TList* media = gGeoManager->GetListOfMedia();
59 while ((med =
static_cast<TGeoMedium*
>(next()))) {
66 TList* materials = gGeoManager->GetListOfMaterials();
67 TIter next(materials);
69 std::map<TString, Bool_t> mapMatName;
71 while ((mat =
static_cast<TGeoMaterial*
>(next()))) {
73 if (mapMatName[mat->GetName()]) {
74 LOG(debug) <<
"Removing duplicate material " << mat->GetName();
75 materials->Remove(mat);
77 mapMatName[mat->GetName()] = kTRUE;
84 TList* media = gGeoManager->GetListOfMedia();
87 std::map<TString, Bool_t> mapMedName;
89 while ((med =
static_cast<TGeoMedium*
>(next()))) {
91 if (mapMedName[med->GetName()]) {
92 LOG(debug) <<
"Removing duplicate medium " << med->GetName();
95 mapMedName[med->GetName()] = kTRUE;
102 FairGeoBuilder* geoBuilder = FairGeoLoader::Instance()->getGeoBuilder();
104 TList* media = gGeoManager->GetListOfMedia();
106 gGeoManager->GetListOfMedia()->Print();
110 for (Int_t
i = geoBuilder->GetNMedia(); i < media->GetEntries();
i++) {
111 med =
static_cast<TGeoMedium*
>(media->At(
i));
115 geoBuilder->SetNMedia(media->GetEntries());
120 media = gGeoManager->GetListOfMedia();
122 while ((med =
static_cast<TGeoMedium*
>(next3()))) {
123 TGeoMaterial* mat = med->GetMaterial();
127 LOG(info) <<
"No Material found for medium " << med->GetName();
130 gGeoManager->SetAllIndex();
134 TString tempString {
""};
135 TGeoMatrix* tempMatrix {
nullptr};
142 TString fVolumeName {
""};
143 TGeoMatrix* tempMatrix {
nullptr};
147 TGeoVolume* module1 = TGeoVolume::Import(filename, fVolumeName.Data());
149 if (gLogger->IsLogNeeded(fair::Severity::debug)) {
150 LOG(debug) <<
"Information about imported volume:";
153 LOG(debug) <<
"Information about imported transformation matrix:";
156 LOG(debug) <<
"There is a transformation matrix passed "
157 <<
"from the module class which overwrites "
158 <<
"the imported matrix.";
160 LOG(debug) <<
"Information about passed transformation matrix:";
170 gGeoManager->GetTopVolume()->AddNode(module1, 0, mat);
172 gGeoManager->GetTopVolume()->AddNode(module1, 0, tempMatrix);
176 gGeoManager->SetAllIndex();
181 TGeoMatrix** matrix) {
183 TFile* oldFile = gFile;
184 TDirectory* oldDirectory = gDirectory;
186 TFile*
f =
new TFile(filename);
187 TList* l =
f->GetListOfKeys();
188 Int_t numKeys = l->GetSize();
192 <<
"Not exactly two keys in the file. File is not of new type.";
199 Bool_t foundGeoVolume = kFALSE;
200 Bool_t foundGeoMatrix = kFALSE;
202 while ((key = (TKey*) next())) {
203 if (key->ReadObj()->InheritsFrom(
"TGeoVolume")) {
204 volumeName = key->GetName();
205 foundGeoVolume = kTRUE;
206 LOG(debug) <<
"Found TGeoVolume with name" << volumeName;
209 if (key->ReadObj()->InheritsFrom(
"TGeoMatrix")) {
210 *matrix =
dynamic_cast<TGeoMatrix*
>(key->ReadObj());
211 foundGeoMatrix = kTRUE;
212 LOG(debug) <<
"Found TGeoMatrix derrived object.";
221 gDirectory = oldDirectory;
223 if (foundGeoVolume && foundGeoMatrix) {
224 LOG(debug) <<
"Geometry file is of new type.";
227 if (!foundGeoVolume) {
229 <<
"No TGeoVolume found in geometry file. File is of unknown type.";
231 if (!foundGeoMatrix) {
232 LOG(fatal) <<
"No TGeoMatrix derived object found in geometry file. "
233 "File is of unknown type.";
247 FairGeoMedia* Media =
248 FairGeoLoader::Instance()->getGeoInterface()->getMedia();
249 FairGeoBuilder* geobuild = FairGeoLoader::Instance()->getGeoBuilder();
251 TGeoMedium* med1 =
v->GetMedium();
262 TString medName =
static_cast<TString
>(med1->GetName());
263 if ((medName.EqualTo(
"dummy"))
264 && (
nullptr == gGeoManager->GetMedium(medName))) {
265 TGeoMaterial* dummyMaterial =
new TGeoMaterial();
266 dummyMaterial->SetName(
"dummy");
268 TGeoMedium* dummyMedium =
new TGeoMedium();
269 dummyMedium->SetName(
"dummy");
270 dummyMedium->SetMaterial(dummyMaterial);
272 gGeoManager->GetListOfMedia()->Add(dummyMedium);
273 gGeoManager->AddMaterial(dummyMaterial);
276 TGeoMaterial* mat1 =
v->GetMaterial();
277 TGeoMaterial* newMat = gGeoManager->GetMaterial(mat1->GetName());
278 if (
nullptr == newMat) {
282 LOG(info) <<
"Create new material " << mat1->GetName();
283 FairGeoMedium* FairMedium = Media->getMedium(mat1->GetName());
285 LOG(fatal) <<
"Material " << mat1->GetName()
286 <<
"is neither defined in ASCII file nor in Root file.";
288 Int_t nmed = geobuild->createMedium(FairMedium);
289 v->SetMedium(gGeoManager->GetMedium(nmed));
290 gGeoManager->SetAllIndex();
294 TGeoMedium* med2 = gGeoManager->GetMedium(mat1->GetName());
298 if (strcmp(
v->ClassName(),
"TGeoVolumeAssembly") != 0) {
299 LOG(fatal) <<
"The volume " <<
v->GetName()
300 <<
"has no medium information and is not an Assembly so "
309 TObjArray* NodeList = vol->GetNodes();
310 for (Int_t Nod = 0; Nod < NodeList->GetEntriesFast(); Nod++) {
311 TGeoNode* fNode = (TGeoNode*) NodeList->At(Nod);
313 TGeoVolume*
v = fNode->GetVolume();
314 if (fNode->GetNdaughters() > 0) {
319 if ((mod->InheritsFrom(
"FairDetector"))
320 && mod->CheckIfSensitive(
v->GetName())) {
321 LOG(debug) <<
"Module " <<
v->GetName() <<
" of detector "
322 << mod->GetName() <<
" is sensitive";
323 mod->AddSensitiveVolume(
v);