EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EicMediaHub.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EicMediaHub.cxx
1 //
2 // AYK (ayk@bnl.gov), 2014/03/11
3 //
4 // EicRoot media interface to TGeo, related FairRoot stuff & Co;
5 //
6 
7 #include <assert.h>
8 #include <stdlib.h>
9 
10 #include <TGeoManager.h>
11 
12 #include <FairLogger.h>
13 
14 #include <EicLibrary.h>
15 #include <EicMediaHub.h>
16 
17 // =======================================================================================
18 
19 EicMediaHub::EicMediaHub(char *mediaName): geoLoad(0), geoFace(0), Media(0), geobuild(0),
20  fSingleMedium(0)
21 #if _LATER_
22  , fMediaMap(0)
23 #endif
24 {
25  // Figure out later was it a single medium or media file name;
26  fMediaName = TString(mediaName);
27 } // EicMediaHub::EicMediaHub()
28 
29 // ---------------------------------------------------------------------------------------
30 
31 int EicMediaHub::importMediaMapFile(TString &mediaMapFileName)
32 {
33 #if _LATER_
34  TString buffer;
35  std::ifstream is(mediaMapFileName);
36 
37  // File does not exist; Ok;
38  if (!is.is_open()) return 0;
39 
40  fMediaMap = new mEntry(EicStlKeyCompare);
41 
42  while (!is.eof())
43  {
44  buffer.ReadLine(is, kTRUE);
45  //std::cout << buffer << std::endl;
46 
47  // Allow for some readability in this file; skip comments and empty lines;
48  if (buffer.BeginsWith("#") || buffer.IsNull())
49  continue;
50  else
51  if (buffer.BeginsWith("color"))
52  {
53  float rgb[3];
54  char color[128], medium[128];
55  // Is it really RGB encoded here?; does not matter;
56  sscanf(buffer.Data(),"%s %f %f %f %s", color, rgb + 0, rgb + 1, rgb + 2, medium);
57  const EicStlKey *key = new EicStlKey(3, rgb);
58 
59  // Well, duplicate entries should not happen;
60  if (fMediaMap->find(key) != fMediaMap->end()) return -1;
61 
62  // Get medium pointer and add one more mapping entry;
63  //printf("%s\n", medium);
64  TGeoMedium *mptr = GetMedium(medium);
65  assert(mptr);
66  (*fMediaMap)[key] = mptr;
67  }
68  else
69  return -1;
70  } // for inf
71 
72  is.close();
73 #endif
74 
75  return 0;
76 } // EicMediaHub::importMediaMapFile()
77 
78 // ---------------------------------------------------------------------------------------
79 
81 {
82  // Initialize these pointers once;
83  //if (!geoLoad)
84  //{
85  // Need it only once, here;
86  FairLogger *fLogger = FairLogger::GetLogger();
87 
90 
91  Media = geoFace->getMedia();
93 
94  // Also try to resolve single medium pointer once; right here;
95  {
96 #if _OLD_
97  TString mediaMapFileName(fMediaName);
98 
99  // Consider absolute path case;
100  if (!fMediaName.BeginsWith("/") && !fMediaName.BeginsWith("./"))
101  mediaMapFileName = TString(getenv("VMCWORKDIR")) + fMediaName;
102 #else
103  TString mediaMapFileName(ExpandedFileName(fMediaName));
104 #endif
105 
106  // Well, first try to see whether a medium with such a name exists; ambiguity whether
107  // it's a medium map file name in STL/SLP case or a single medium name is rather
108  // unlikely, yet check that (if single medium exists), there is no readable file
109  // with this name;
110  if (!fMediaName.IsNull()) {
112 
113  // Is there a better check on read access?;
114  std::ifstream is(mediaMapFileName);
115 
116  if (fSingleMedium) {
117  if (is.is_open())
118  fLogger->Fatal(MESSAGE_ORIGIN,
119  "\033[5m\033[31m Ambiguity: both single medium (%s) "
120  "and media file with this name (%s) exist! \033[0m",
121  fMediaName.Data(), mediaMapFileName.Data());
122  }
123  else {
124  // This is in fact needed for STL/SLP file import; do not mind to try anyway;
125  if (importMediaMapFile(mediaMapFileName))
126  fLogger->Fatal(MESSAGE_ORIGIN,
127  "\033[5m\033[31m Failed to import media map file (%s)! \033[0m",
128  mediaMapFileName.Data());
129  } //if
130  } //if
131  }
132  //} //if
133 
134  //return 0;
135 } // EicMediaHub::Init()
136 
137 // ---------------------------------------------------------------------------------------
138 
139 TGeoMedium *EicMediaHub::GetMedium(const char *medium)
140 {
141  //printf(" -> getting %s\n", medium);
142  // If media is known to the current gGeoManager, just return respective pointer;
143  TGeoMedium *gmedium = gGeoManager->GetMedium(medium);
144  if (gmedium) return gmedium;
145  //printf(" ... does not exist\n");
146 
147  // Make gGeoManager about this medium;
148  {
149  FairGeoMedium *fmedium = Media->getMedium(medium);
150 
151  // Basically means no such medium in media.geo file;
152  if (!fmedium) return 0;
153 
154  geobuild->createMedium(fmedium);
155 
156  // Should not happen;
157  assert(gGeoManager->GetMedium(medium));
158  } //if
159 
160  // NB: this can still return 0 pointer if no such media existed in media.geo file;
161  return gGeoManager->GetMedium(medium);
162 } // EicMediaHub::GetMedium()
163 
164 // =======================================================================================
165