CbmRoot
CbmGeometryUtils.cxx
Go to the documentation of this file.
1 #include "CbmGeometryUtils.h"
2 
3 #include <FairGeoBuilder.h> // for FairGeoBuilder
4 #include <FairGeoInterface.h> // for FairGeoInterface
5 #include <FairGeoLoader.h> // for FairGeoLoader
6 #include <FairGeoMedia.h> // for FairGeoMedia
7 #include <FairLogger.h> // for LOG, Logger, Severity, Severity::debug
8 #include <FairLogger.h> // for FairLogger, gLogger
9 #include <FairModule.h> // for FairModule
10 #include <TFile.h> // for TFile, gFile
11 
12 #include <RtypesCore.h> // for Bool_t, Int_t, kTRUE, kFALSE
13 #include <TCollection.h> // for TIter
14 #include <TDirectory.h> // for gDirectory, TDirectory (ptr only)
15 #include <TGeoManager.h> // for TGeoManager, gGeoManager
16 #include <TGeoMaterial.h> // for TGeoMaterial
17 #include <TGeoMatrix.h> // for TGeoMatrix
18 #include <TGeoMedium.h> // for TGeoMedium
19 #include <TGeoNode.h> // for TGeoNode
20 #include <TGeoVolume.h> // for TGeoVolume
21 #include <TKey.h> // for TKey
22 #include <TList.h> // for TList
23 #include <TObjArray.h> // for TObjArray
24 #include <TObject.h> // for TObject
25 #include <TString.h> // for TString, operator<, operator<<
26 
27 #include <map> // for map
28 #include <string.h> // for strcmp
29 
30 
31 namespace Cbm {
32  namespace GeometryUtils {
33  void PrintMedia() {
34  TList* media = gGeoManager->GetListOfMedia();
35  TIter next1(media);
36  TGeoMedium* med;
37  while ((med = static_cast<TGeoMedium*>(next1()))) {
38  LOG(info) << "Medium " << med->GetName() << " with ID " << med->GetId();
39  }
40  LOG(info) << "****";
41  }
42 
43  void PrintMaterials() {
44  TList* material = gGeoManager->GetListOfMaterials();
45  TIter next1(material);
46  TGeoMaterial* mat;
47  while ((mat = static_cast<TGeoMaterial*>(next1()))) {
48  LOG(info) << "Material " << mat->GetName() << " with ID "
49  << mat->GetIndex();
50  }
51  LOG(info) << "****";
52  }
53 
54  void CorrectMediaId() {
55  TList* media = gGeoManager->GetListOfMedia();
56  TIter next(media);
57  TGeoMedium* med;
58  Int_t counter {0};
59  while ((med = static_cast<TGeoMedium*>(next()))) {
60  med->SetId(counter++);
61  }
62  }
63 
65  // Revove duplicate materials
66  TList* materials = gGeoManager->GetListOfMaterials();
67  TIter next(materials);
68  // map for existing materials
69  std::map<TString, Bool_t> mapMatName;
70  TGeoMaterial* mat;
71  while ((mat = static_cast<TGeoMaterial*>(next()))) {
72  // If material exist - delete dublicated. If not - set the flag
73  if (mapMatName[mat->GetName()]) {
74  LOG(debug) << "Removing duplicate material " << mat->GetName();
75  materials->Remove(mat);
76  } else {
77  mapMatName[mat->GetName()] = kTRUE;
78  }
79  }
80  }
81 
83  // Revove duplicate media
84  TList* media = gGeoManager->GetListOfMedia();
85  TIter next(media);
86  // map for existing materials
87  std::map<TString, Bool_t> mapMedName;
88  TGeoMedium* med;
89  while ((med = static_cast<TGeoMedium*>(next()))) {
90  // If medium exist - delete duplicated. If not - set the flag
91  if (mapMedName[med->GetName()]) {
92  LOG(debug) << "Removing duplicate medium " << med->GetName();
93  media->Remove(med);
94  } else {
95  mapMedName[med->GetName()] = kTRUE;
96  }
97  }
98  }
99 
101  // Initialise pointer to GeoBuilder
102  FairGeoBuilder* geoBuilder = FairGeoLoader::Instance()->getGeoBuilder();
103  // Get list of TGeo media
104  TList* media = gGeoManager->GetListOfMedia();
105 
106  gGeoManager->GetListOfMedia()->Print();
107 
108  // Loop over new media which are not in GeoBase and shift the ID
109  TGeoMedium* med;
110  for (Int_t i = geoBuilder->GetNMedia(); i < media->GetEntries(); i++) {
111  med = static_cast<TGeoMedium*>(media->At(i));
112  med->SetId(i + 1);
113  }
114  // Change GeoBase medium index
115  geoBuilder->SetNMedia(media->GetEntries());
116 
119 
120  media = gGeoManager->GetListOfMedia();
121  TIter next3(media);
122  while ((med = static_cast<TGeoMedium*>(next3()))) {
123  TGeoMaterial* mat = med->GetMaterial();
124  if (mat) {
125  // mat->Print();
126  } else {
127  LOG(info) << "No Material found for medium " << med->GetName();
128  }
129  }
130  gGeoManager->SetAllIndex();
131  }
132 
133  Bool_t IsNewGeometryFile(TString& filename) {
134  TString tempString {""};
135  TGeoMatrix* tempMatrix {nullptr};
136  return IsNewGeometryFile(filename, tempString, &tempMatrix);
137  }
138 
139  void
140  ImportRootGeometry(TString& filename, FairModule* mod, TGeoMatrix* mat) {
141 
142  TString fVolumeName {""};
143  TGeoMatrix* tempMatrix {nullptr};
144 
145  IsNewGeometryFile(filename, fVolumeName, &tempMatrix);
146 
147  TGeoVolume* module1 = TGeoVolume::Import(filename, fVolumeName.Data());
148 
149  if (gLogger->IsLogNeeded(fair::Severity::debug)) {
150  LOG(debug) << "Information about imported volume:";
151  module1->Print();
152  LOG(debug);
153  LOG(debug) << "Information about imported transformation matrix:";
154  tempMatrix->Print();
155  if (mat) {
156  LOG(debug) << "There is a transformation matrix passed "
157  << "from the module class which overwrites "
158  << "the imported matrix.";
159  LOG(debug);
160  LOG(debug) << "Information about passed transformation matrix:";
161  mat->Print();
162  }
163  }
164 
168 
169  if (mat) {
170  gGeoManager->GetTopVolume()->AddNode(module1, 0, mat);
171  } else {
172  gGeoManager->GetTopVolume()->AddNode(module1, 0, tempMatrix);
173  }
174 
175  Cbm::GeometryUtils::ExpandNodes(module1, mod);
176  gGeoManager->SetAllIndex();
177  }
178 
179  Bool_t IsNewGeometryFile(TString& filename,
180  TString& volumeName,
181  TGeoMatrix** matrix) {
182  // Save current gFile and gDirectory information
183  TFile* oldFile = gFile;
184  TDirectory* oldDirectory = gDirectory;
185 
186  TFile* f = new TFile(filename);
187  TList* l = f->GetListOfKeys();
188  Int_t numKeys = l->GetSize();
189 
190  if (2 != numKeys) {
191  LOG(debug)
192  << "Not exactly two keys in the file. File is not of new type.";
193  return kFALSE;
194  }
195 
196  TKey* key;
197  TIter next(l);
198 
199  Bool_t foundGeoVolume = kFALSE;
200  Bool_t foundGeoMatrix = kFALSE;
201 
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;
207  continue;
208  }
209  if (key->ReadObj()->InheritsFrom("TGeoMatrix")) {
210  *matrix = dynamic_cast<TGeoMatrix*>(key->ReadObj());
211  foundGeoMatrix = kTRUE;
212  LOG(debug) << "Found TGeoMatrix derrived object.";
213  continue;
214  }
215  }
216 
217  // Restore previous gFile and gDirectory information
218  f->Close();
219  delete f;
220  gFile = oldFile;
221  gDirectory = oldDirectory;
222 
223  if (foundGeoVolume && foundGeoMatrix) {
224  LOG(debug) << "Geometry file is of new type.";
225  return kTRUE;
226  } else {
227  if (!foundGeoVolume) {
228  LOG(fatal)
229  << "No TGeoVolume found in geometry file. File is of unknown type.";
230  }
231  if (!foundGeoMatrix) {
232  LOG(fatal) << "No TGeoMatrix derived object found in geometry file. "
233  "File is of unknown type.";
234  }
235  return kFALSE;
236  }
237  }
238 
239 
240  void AssignMediumAtImport(TGeoVolume* v) {
247  FairGeoMedia* Media =
248  FairGeoLoader::Instance()->getGeoInterface()->getMedia();
249  FairGeoBuilder* geobuild = FairGeoLoader::Instance()->getGeoBuilder();
250 
251  TGeoMedium* med1 = v->GetMedium();
252 
253  if (med1) {
254  // In newer ROOT version also a TGeoVolumeAssembly has a material and medium.
255  // This medium is called dummy and is automatically set when the geometry is constructed.
256  // Since this material and medium is neither in the TGeoManager (at this point) nor in our
257  // ASCII file we have to create it the same way it is done in TGeoVolume::CreateDummyMedium()
258  // In the end the new medium and material has to be added to the TGeomanager, because this is
259  // not done automatically when using the default constructor. For all other constructors the
260  // newly created medium or material is added to the TGeomanger.
261  // Create the medium and material only the first time.
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");
267 
268  TGeoMedium* dummyMedium = new TGeoMedium();
269  dummyMedium->SetName("dummy");
270  dummyMedium->SetMaterial(dummyMaterial);
271 
272  gGeoManager->GetListOfMedia()->Add(dummyMedium);
273  gGeoManager->AddMaterial(dummyMaterial);
274  }
275 
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());
284  if (!FairMedium) {
285  LOG(fatal) << "Material " << mat1->GetName()
286  << "is neither defined in ASCII file nor in Root file.";
287  } else {
288  Int_t nmed = geobuild->createMedium(FairMedium);
289  v->SetMedium(gGeoManager->GetMedium(nmed));
290  gGeoManager->SetAllIndex();
291  }
292  } else {
294  TGeoMedium* med2 = gGeoManager->GetMedium(mat1->GetName());
295  v->SetMedium(med2);
296  }
297  } else {
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 "
301  "we have to quit";
302  }
303  }
304  }
305 
306  void ExpandNodes(TGeoVolume* vol, FairModule* mod) {
307 
309  TObjArray* NodeList = vol->GetNodes();
310  for (Int_t Nod = 0; Nod < NodeList->GetEntriesFast(); Nod++) {
311  TGeoNode* fNode = (TGeoNode*) NodeList->At(Nod);
312 
313  TGeoVolume* v = fNode->GetVolume();
314  if (fNode->GetNdaughters() > 0) {
316  }
318 
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);
324  }
325  }
326  }
327 
328 
329  } // namespace GeometryUtils
330 } // namespace Cbm
Cbm::GeometryUtils::ReAssignMediaId
void ReAssignMediaId()
Definition: CbmGeometryUtils.cxx:100
f
float f
Definition: L1/vectors/P4_F32vec4.h:24
Cbm::GeometryUtils::RemoveDuplicateMaterials
void RemoveDuplicateMaterials()
Definition: CbmGeometryUtils.cxx:64
Cbm::GeometryUtils::PrintMaterials
void PrintMaterials()
Definition: CbmGeometryUtils.cxx:43
Cbm::GeometryUtils::CorrectMediaId
void CorrectMediaId()
Definition: CbmGeometryUtils.cxx:54
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
Cbm::GeometryUtils::AssignMediumAtImport
void AssignMediumAtImport(TGeoVolume *v)
Definition: CbmGeometryUtils.cxx:240
Cbm::GeometryUtils::RemoveDuplicateMedia
void RemoveDuplicateMedia()
Definition: CbmGeometryUtils.cxx:82
Cbm::GeometryUtils::IsNewGeometryFile
Bool_t IsNewGeometryFile(TString &filename)
Definition: CbmGeometryUtils.cxx:133
Cbm::GeometryUtils::ExpandNodes
void ExpandNodes(TGeoVolume *vol, FairModule *mod)
Definition: CbmGeometryUtils.cxx:306
counter
int counter
Definition: CbmMvdSensorDigiToHitTask.cxx:72
v
__m128 v
Definition: L1/vectors/P4_F32vec4.h:1
Cbm::GeometryUtils::ImportRootGeometry
void ImportRootGeometry(TString &filename, FairModule *mod, TGeoMatrix *mat)
Definition: CbmGeometryUtils.cxx:140
CbmGeometryUtils.h
Cbm::GeometryUtils::PrintMedia
void PrintMedia()
Definition: CbmGeometryUtils.cxx:33
Cbm
Definition: CbmGeometryUtils.cxx:31