7#include <FairGeoBuilder.h>
8#include <FairGeoInterface.h>
9#include <FairGeoLoader.h>
10#include <FairGeoMedia.h>
11#include <FairModule.h>
14#include <RtypesCore.h>
15#include <TCollection.h>
16#include <TDirectory.h>
18#include <TGeoManager.h>
19#include <TGeoMaterial.h>
20#include <TGeoMatrix.h>
21#include <TGeoMedium.h>
23#include <TGeoVolume.h>
38 namespace GeometryUtils
42 TList* media = gGeoManager->GetListOfMedia();
45 while ((med =
static_cast<TGeoMedium*
>(next1()))) {
46 LOG(info) <<
"Medium " << med->GetName() <<
" with ID " << med->GetId();
53 TList* material = gGeoManager->GetListOfMaterials();
54 TIter next1(material);
56 while ((mat =
static_cast<TGeoMaterial*
>(next1()))) {
57 LOG(info) <<
"Material " << mat->GetName() <<
" with ID " << mat->GetIndex();
75 TList* media = gGeoManager->GetListOfMedia();
82 while (j < media->GetSize()) {
84 med = (TGeoMedium*) media->At(j);
85 mat = med->GetMaterial();
89 for (
size_t i = 0; i < strlen(mat->GetName()); ++i) {
90 if (mat->GetName()[i] >=
'A' && mat->GetName()[i] <=
'Z') {
92 ((Int_t) pow((
double) base, (
double) i) % INT_MAX) * ((Int_t) mat->GetName()[i] -
'B');
94 else if (med->GetMaterial()->GetName()[i] >=
'a' && mat->GetName()[i] <=
'z') {
96 ((Int_t) pow((
double) base, (
double) i) % INT_MAX) * ((Int_t) mat->GetName()[i] -
'b');
100 ((Int_t) pow((
double) base, (
double) i + 1) % INT_MAX) * ((Int_t) mat->GetName()[i]);
104 med->SetId((UniqueID > 0) ? UniqueID : -1 * UniqueID);
105 mat->SetIndex((UniqueID > 0) ? UniqueID : -1 * UniqueID);
107 LOG(debug) <<
"Reset ID number of Medium " << med->GetName() <<
" to " << med->GetId();
108 LOG(debug) <<
"Reset ID number of Material " << mat->GetName() <<
" to " << mat->GetIndex();
118 TList* materials = gGeoManager->GetListOfMaterials();
119 TIter next(materials);
121 std::map<TString, Bool_t> mapMatName;
123 while ((mat =
static_cast<TGeoMaterial*
>(next()))) {
125 if (mapMatName[mat->GetName()]) {
126 LOG(debug) <<
"Removing duplicate material " << mat->GetName();
127 materials->Remove(mat);
130 mapMatName[mat->GetName()] = kTRUE;
138 TList* media = gGeoManager->GetListOfMedia();
141 std::map<TString, Bool_t> mapMedName;
143 while ((med =
static_cast<TGeoMedium*
>(next()))) {
145 if (mapMedName[med->GetName()]) {
146 LOG(debug) <<
"Removing duplicate medium " << med->GetName();
150 mapMedName[med->GetName()] = kTRUE;
158 FairGeoBuilder* geoBuilder = FairGeoLoader::Instance()->getGeoBuilder();
160 TList* media = gGeoManager->GetListOfMedia();
162 gGeoManager->GetListOfMedia()->Print();
166 for (Int_t i = geoBuilder->GetNMedia(); i < media->GetEntries(); i++) {
167 med =
static_cast<TGeoMedium*
>(media->At(i));
171 geoBuilder->SetNMedia(media->GetEntries());
176 media = gGeoManager->GetListOfMedia();
178 while ((med =
static_cast<TGeoMedium*
>(next3()))) {
179 TGeoMaterial* mat = med->GetMaterial();
184 LOG(info) <<
"No Material found for medium " << med->GetName();
187 gGeoManager->SetAllIndex();
192 TString tempString {
""};
193 TGeoMatrix* tempMatrix {
nullptr};
200 TString fVolumeName {
""};
201 TGeoMatrix* tempMatrix {
nullptr};
205 TGeoVolume* module1 = TGeoVolume::Import(filename, fVolumeName.Data());
207 if (fair::Logger::Logging(fair::Severity::debug)) {
208 LOG(debug) <<
"Information about imported volume:";
211 LOG(debug) <<
"Information about imported transformation matrix:";
214 LOG(debug) <<
"There is a transformation matrix passed "
215 <<
"from the module class which overwrites "
216 <<
"the imported matrix.";
218 LOG(debug) <<
"Information about passed transformation matrix:";
229 if (mat) { gGeoManager->GetTopVolume()->AddNode(module1, 0, mat); }
231 gGeoManager->GetTopVolume()->AddNode(module1, 0, tempMatrix);
235 gGeoManager->SetAllIndex();
241 TFile* oldFile = gFile;
242 TDirectory* oldDirectory = gDirectory;
244 TFile* f =
new TFile(filename);
245 TList* l = f->GetListOfKeys();
246 Int_t numKeys = l->GetSize();
249 LOG(debug) <<
"Not exactly two keys in the file. File is not of new type.";
256 Bool_t foundGeoVolume = kFALSE;
257 Bool_t foundGeoMatrix = kFALSE;
259 while ((key = (TKey*) next())) {
260 if (key->ReadObj()->InheritsFrom(
"TGeoVolume")) {
261 volumeName = key->GetName();
262 foundGeoVolume = kTRUE;
263 LOG(debug) <<
"Found TGeoVolume with name" << volumeName;
266 if (key->ReadObj()->InheritsFrom(
"TGeoMatrix")) {
267 *matrix =
dynamic_cast<TGeoMatrix*
>(key->ReadObj());
268 foundGeoMatrix = kTRUE;
269 LOG(debug) <<
"Found TGeoMatrix derrived object.";
278 gDirectory = oldDirectory;
280 if (foundGeoVolume && foundGeoMatrix) {
281 LOG(debug) <<
"Geometry file is of new type.";
285 if (!foundGeoVolume) { LOG(fatal) <<
"No TGeoVolume found in geometry file. File is of unknown type."; }
286 if (!foundGeoMatrix) {
287 LOG(fatal) <<
"No TGeoMatrix derived object found in geometry file. "
288 "File is of unknown type.";
303 FairGeoMedia* Media = FairGeoLoader::Instance()->getGeoInterface()->getMedia();
304 FairGeoBuilder* geobuild = FairGeoLoader::Instance()->getGeoBuilder();
306 TGeoMedium* med1 =
v->GetMedium();
317 TString medName =
static_cast<TString
>(med1->GetName());
318 if ((medName.EqualTo(
"dummy")) && (
nullptr == gGeoManager->GetMedium(medName))) {
319 TGeoMaterial* dummyMaterial =
new TGeoMaterial();
320 dummyMaterial->SetName(
"dummy");
322 TGeoMedium* dummyMedium =
new TGeoMedium();
323 dummyMedium->SetName(
"dummy");
324 dummyMedium->SetMaterial(dummyMaterial);
326 gGeoManager->GetListOfMedia()->Add(dummyMedium);
327 gGeoManager->AddMaterial(dummyMaterial);
330 TGeoMaterial* mat1 =
v->GetMaterial();
331 TGeoMaterial* newMat = gGeoManager->GetMaterial(mat1->GetName());
332 if (
nullptr == newMat) {
336 LOG(info) <<
"Create new material " << mat1->GetName();
337 FairGeoMedium* FairMedium = Media->getMedium(mat1->GetName());
339 LOG(fatal) <<
"Material " << mat1->GetName() <<
"is neither defined in ASCII file nor in Root file.";
342 Int_t nmed = geobuild->createMedium(FairMedium);
343 v->SetMedium(gGeoManager->GetMedium(nmed));
344 gGeoManager->SetAllIndex();
349 TGeoMedium* med2 = gGeoManager->GetMedium(mat1->GetName());
354 if (strcmp(
v->ClassName(),
"TGeoVolumeAssembly") != 0) {
355 LOG(fatal) <<
"The volume " <<
v->GetName()
356 <<
"has no medium information and is not an Assembly so "
366 TObjArray* NodeList = vol->GetNodes();
367 for (Int_t Nod = 0; Nod < NodeList->GetEntriesFast(); Nod++) {
368 TGeoNode* fNode = (TGeoNode*) NodeList->At(Nod);
370 TGeoVolume*
v = fNode->GetVolume();
374 if ((mod->InheritsFrom(
"FairDetector")) && mod->IsSensitive(
v->GetName())) {
375 LOG(debug) <<
"Module " <<
v->GetName() <<
" of detector " << mod->GetName() <<
" is sensitive";
376 mod->AddSensitiveVolume(
v);
392 double r00 = m.GetRotationMatrix()[0];
393 double r01 = m.GetRotationMatrix()[1];
394 double r10 = m.GetRotationMatrix()[3];
395 double r11 = m.GetRotationMatrix()[4];
398 double rc00 = r00 * c00 + r01 * c10;
399 double rc01 = r00 * c10 + r01 * c11;
400 double rc10 = r10 * c00 + r11 * c10;
401 double rc11 = r10 * c10 + r11 * c11;
404 covXX = rc00 * r00 + rc01 * r01;
405 covXY = rc10 * r00 + rc11 * r01;
406 covYY = rc10 * r10 + rc11 * r11;
415 FairGeoLoader* geoLoad =
new FairGeoLoader(
"TGeo",
"FairGeoLoader");
416 FairGeoInterface* geoFace = geoLoad->getGeoInterface();
417 TString geoPath = gSystem->Getenv(
"VMCWORKDIR");
418 TString geoFile = geoPath +
"/geometry/media.geo";
419 geoFace->setMediaFile(geoFile);
420 geoFace->readMedia();
423 FairGeoMedia* geoMedia = geoFace->getMedia();
424 FairGeoBuilder* geoBuild = geoLoad->getGeoBuilder();
426 int num = geoMedia->getListOfMedia()->GetSize();
427 FairGeoMedium* med =
new FairGeoMedium();
429 for (
int i = 0; i < num; i++) {
430 med = geoMedia->getMedium(geoMedia->getListOfMedia()->At(i)->GetName());
431 geoBuild->createMedium(med);
434 gGeoManager->SetTitle(name);
440 bool add_binary(
const char rootFile[], TGeoVolume* top, TGeoMedium* med, Int_t inum, TGeoMatrix* mat)
443 TFile* file = TFile::Open(rootFile,
"OPEN");
445 LOG(info) <<
"Error file " << rootFile <<
" not opened";
449 ((TGeoVolume*) file->Get(file->GetListOfKeys()->First()->GetName()))->SetMedium(med);
450 top->AddNode((TGeoVolume*) (file->Get(file->GetListOfKeys()->First()->GetName()))->Clone(), inum, mat);
453 (top->GetNode(top->GetNodes()->Last()->GetName()))->GetVolume()->SetMedium(med);
459 TGeoMatrix*
cad_matrix(
double XX,
double XY,
double XZ,
double YX,
double YY,
double YZ,
double ZX,
double ZY,
460 double ZZ,
double TX,
double TY,
double TZ)
463 TGeoHMatrix* hmat =
new TGeoHMatrix();
464 (hmat->GetRotationMatrix())[0] = XX;
465 (hmat->GetRotationMatrix())[3] = XY;
466 (hmat->GetRotationMatrix())[6] = XZ;
468 (hmat->GetRotationMatrix())[1] = YX;
469 (hmat->GetRotationMatrix())[4] = YY;
470 (hmat->GetRotationMatrix())[7] = YZ;
472 (hmat->GetRotationMatrix())[2] = ZX;
473 (hmat->GetRotationMatrix())[5] = ZY;
474 (hmat->GetRotationMatrix())[8] = ZZ;
476 TGeoRotation* rot =
new TGeoRotation();
477 rot->SetRotation(*hmat);
479 TGeoCombiTrans* mat =
new TGeoCombiTrans(TX / 1, TY / 1, TZ / 1, rot);
void RemoveDuplicateMedia()
TGeoMatrix * cad_matrix(double XX, double XY, double XZ, double YX, double YY, double YZ, double ZX, double ZY, double ZZ, double TX, double TY, double TZ)
void AssignMediumAtImport(TGeoVolume *v)
Bool_t IsNewGeometryFile(TString &filename)
void ExpandNodes(TGeoVolume *vol, FairModule *mod)
void RemoveDuplicateMaterials()
void ImportRootGeometry(TString &filename, FairModule *mod, TGeoMatrix *mat)
void LocalToMasterCovarianceMatrix(const TGeoMatrix &m, Double_t &covXX, Double_t &covXY, Double_t &covYY)
Convert the local X/Y covariance matrix to global coordinates.
TGeoManager * pop_TGeoManager(const char *name)
bool add_binary(const char rootFile[], TGeoVolume *top, TGeoMedium *med, Int_t inum, TGeoMatrix *mat)