CbmRoot
CbmSetup.cxx
Go to the documentation of this file.
1 #
7 #include "CbmSetup.h"
8 #include "CbmAddress.h"
9 #include "CbmFieldMap.h"
10 #include "CbmFieldMapSym2.h"
11 #include "CbmFieldMapSym3.h"
12 #include "FairLogger.h"
13 #include "FairModule.h"
14 #include "FairRunSim.h"
15 #include "TFile.h"
16 #include "TGeoMatrix.h"
17 #include "TGeoNode.h"
18 #include "TGeoVolume.h"
19 #include "TKey.h"
20 #include "TSystem.h"
21 #include <iomanip>
22 #include <sstream>
23 
24 #include "CbmFieldMapData.h"
25 
26 using std::string;
27 using std::stringstream;
28 
29 // ----- Initialise static instance ------------------------------------
31 // -------------------------------------------------------------------------
32 
33 
34 // ----- Clear the setup -----------------------------------------------
35 void CbmSetup::Clear(Option_t*) { fProvider->Reset(); }
36 // -------------------------------------------------------------------------
37 
38 
39 // ----- Get field map type --------------------------------------------
42 
43  // --- Open the map file
44  TString fileName = field.GetFilePath();
45  fileName = TString(gSystem->Getenv("VMCWORKDIR")) + "/" + fileName;
46 
47  TFile mapFile(fileName);
48  if (!mapFile.IsOpen()) {
49  LOG(error) << "Could not open field map file " << fileName;
50  return NULL;
51  }
52 
53  // --- Get map file type
54  TString mapName = "field_";
55  mapName += field.GetTag().c_str();
56  CbmFieldMapData* data = NULL;
57  mapFile.GetObject(mapName, data);
58  if (!data) {
59  LOG(error) << "Could not find CbmFieldMapData object " << mapName
60  << " in file " << fileName;
61  return NULL;
62  }
63  Int_t fieldType = data->GetType();
64 
65  // --- Instantiate field
66  CbmFieldMap* fieldMap = NULL;
67  switch (fieldType) {
68  case 2: fieldMap = new CbmFieldMapSym2(mapName); break;
69  case 3: fieldMap = new CbmFieldMapSym3(mapName); break;
70  default: LOG(error) << "Unknown field type " << fieldType;
71  }
72 
73  // --- Set scale and position of field map
74  if (fieldMap) {
75  fieldMap->SetScale(field.GetScale());
76  fieldMap->SetPosition(field.GetMatrix().GetTranslation()[0],
77  field.GetMatrix().GetTranslation()[1],
78  field.GetMatrix().GetTranslation()[2]);
79  }
80 
81  return fieldMap;
82 }
83 // -------------------------------------------------------------------------
84 
85 
86 // ----- Get a geometry file name ---------------------------------------
87 Bool_t CbmSetup::GetGeoFileName(ECbmModuleId moduleId, TString& fileName) {
88  auto& moduleMap = fProvider->GetSetup().GetModuleMap();
89 
90  if (moduleMap.find(moduleId) == moduleMap.end()) {
91  fileName = "";
92  return kFALSE;
93  }
94  fileName = moduleMap.at(moduleId).GetFilePath();
95  return kTRUE;
96 }
97 // -------------------------------------------------------------------------
98 
99 
100 // ----- Get a geometry tag ---------------------------------------------
101 Bool_t CbmSetup::GetGeoTag(ECbmModuleId moduleId, TString& tag) {
102  auto& moduleMap = fProvider->GetSetup().GetModuleMap();
103 
104  if (moduleMap.find(moduleId) == moduleMap.end()) {
105  tag = "";
106  return kFALSE;
107  }
108  tag = moduleMap.at(moduleId).GetTag();
109  return kTRUE;
110 }
111 // -------------------------------------------------------------------------
112 
113 
114 // ----- Instance ------------------------------------------------------
116  if (!fgInstance) fgInstance = new CbmSetup();
117  return fgInstance;
118 }
119 // -------------------------------------------------------------------------
120 
121 
122 // ----- Get activity flag ----------------------------------------------
124  auto& moduleMap = fProvider->GetSetup().GetModuleMap();
125 
126  if (moduleMap.find(moduleId) == moduleMap.end()) return kFALSE;
127  return moduleMap.at(moduleId).GetActive();
128 }
129 // -------------------------------------------------------------------------
130 
131 
132 // ----- Remove a module -----------------------------------------------
134  fProvider->RemoveModule(moduleId);
135 }
136 // -------------------------------------------------------------------------
137 
138 
139 // ----- Activate or deactivate a detector -----------------------------
140 void CbmSetup::SetActive(ECbmModuleId moduleId, Bool_t active) {
141 
142  auto& moduleMap = fProvider->GetSetup().GetModuleMap();
143 
144  // Check presence of module in current setup
145  if (moduleMap.find(moduleId) == moduleMap.end()) {
146  LOG(warn) << "Module " << moduleId << " does not exist in setup!";
147  return;
148  }
149 
150  // Set activity flag
151  moduleMap.at(moduleId).SetActive(active);
152 }
153 // -------------------------------------------------------------------------
154 
155 
156 // ----- Set the field map ---------------------------------------------
157 void CbmSetup::SetField(const char* tag,
158  Double_t scale,
159  Double_t xPos,
160  Double_t yPos,
161  Double_t zPos) {
162 
163  LOG(warn) << GetName() << ": Overriding field map "
165  << " (according to magnet geometry) with field map " << tag;
166 
168  field.SetScale(scale);
169  field.GetMatrix().SetTranslation(xPos, yPos, zPos);
170  fProvider->GetSetup().SetField(field);
171 }
172 // -------------------------------------------------------------------------
173 
174 
175 // ----- Add or replace a module in the setup --------------------------
177  const char* geoTag,
178  Bool_t active) {
179 
180  std::map<ECbmModuleId, CbmGeoSetupModule> modmap =
182 
183  if (modmap.find(moduleId) != modmap.end()) {
184 
185  CbmGeoSetupModule& module =
186  fProvider->GetSetup().GetModuleMap().at(moduleId);
187  // Check presence of module in current setup
188  LOG(debug) << GetName() << ": Changing module " << moduleId << ": "
189  << module.GetTag() << " -> " << geoTag;
190  }
191  fProvider->SetModuleTag(moduleId, geoTag, active);
192 }
193 // -------------------------------------------------------------------------
194 
195 
196 // ----- Info to string ------------------------------------------------
197 string CbmSetup::ToString() const {
198 
199  stringstream ss;
200  CbmGeoSetup& setup = fProvider->GetSetup();
201  ss << std::left << "CBM setup: " << setup.GetName() << ", " << GetNofModules()
202  << " modules \n";
203  for (auto& it : setup.GetModuleMap()) {
204  ECbmModuleId moduleId = it.first;
205  CbmGeoSetupModule& module = it.second;
206  ss << " " << std::setw(8)
207  << CbmModuleList::GetModuleNameCaps(moduleId) << ": " << std::setw(8)
208  << module.GetTag();
209  if (module.GetActive())
210  ss << " *ACTIVE* ";
211  else
212  ss << " ";
213  ss << " using " << module.GetFilePath() << "\n";
214  }
215 
216  CbmGeoSetupField& field = setup.GetField();
217  ss << " Field : " << field.GetTag() << ", Position ( "
218  << field.GetMatrix().GetTranslation()[0] << ", "
219  << field.GetMatrix().GetTranslation()[1] << ", "
220  << field.GetMatrix().GetTranslation()[2] << " ) cm, scaling "
221  << field.GetScale() << "\n";
222 
223  return ss.str();
224 }
225 // -------------------------------------------------------------------------
226 
227 
228 // ----- Set the source the setup will be loaded from -------------------
230  switch (setupSource) {
231  case kRepo: SetProvider(new CbmGeoSetupRepoProvider()); break;
232  case kDb: SetProvider(new CbmGeoSetupDbProvider()); break;
233  default:
234  LOG(fatal) << "Invalid value for geo setup provider source "
235  << setupSource;
236  }
237 }
238 // --------------------------------------------------------------------------
239 
240 
CbmSetup::GetGeoFileName
Bool_t GetGeoFileName(ECbmModuleId moduleId, TString &fileName)
Definition: CbmSetup.cxx:87
CbmFieldMapSym3
Definition: CbmFieldMapSym3.h:35
CbmGeoSetupField::GetMatrix
TGeoTranslation & GetMatrix()
Definition: CbmGeoSetupField.h:31
CbmGeoSetupProvider::SetModuleTag
void SetModuleTag(ECbmModuleId moduleId, std::string tag, Bool_t active)
Loads the detector with a tag into setup, will invoke GetModuleByTag.
Definition: CbmGeoSetupProvider.cxx:57
CbmSetup::ToString
std::string ToString() const
Info to string.
Definition: CbmSetup.cxx:197
CbmGeoSetup::SetField
void SetField(CbmGeoSetupField value)
Definition: CbmGeoSetup.h:55
CbmSetup::SetField
void SetField(const char *tag, Double_t scale=1., Double_t xPos=0., Double_t yPos=0., Double_t zPos=0.)
Definition: CbmSetup.cxx:157
CbmSetup::IsActive
Bool_t IsActive(ECbmModuleId moduleId)
Definition: CbmSetup.cxx:123
CbmSetup::SetModule
void SetModule(ECbmModuleId moduleId, const char *geoTag, Bool_t active=kTRUE)
Definition: CbmSetup.cxx:176
CbmSetup::SetProvider
void SetProvider(CbmGeoSetupProvider *value)
Set the geo setup provider.
Definition: CbmSetup.h:192
CbmSetup::SetSetupSource
void SetSetupSource(ECbmSetupSource setupSource)
Set the source the setup will be loaded from.
Definition: CbmSetup.cxx:229
CbmSetup::RemoveModule
void RemoveModule(ECbmModuleId moduleId)
Definition: CbmSetup.cxx:133
CbmFieldMapSym2
Definition: CbmFieldMapSym2.h:35
ECbmModuleId
ECbmModuleId
Definition: CbmDefs.h:33
CbmFieldMapData.h
CbmGeoSetupRepoProvider
Setup provider with local (svn) repository functionality.
Definition: CbmGeoSetupRepoProvider.h:21
CbmSetup::CreateFieldMap
CbmFieldMap * CreateFieldMap()
Definition: CbmSetup.cxx:40
CbmGeoSetupModule::GetActive
Bool_t GetActive()
Definition: CbmGeoSetupModule.h:34
CbmFieldMap::SetPosition
virtual void SetPosition(Double_t x, Double_t y, Double_t z)
Definition: CbmFieldMap.cxx:549
CbmSetup
Definition: CbmSetup.h:31
CbmModuleList::GetModuleNameCaps
static TString GetModuleNameCaps(ECbmModuleId moduleId)
Definition: CbmModuleList.cxx:77
CbmGeoSetupModule::GetFilePath
std::string GetFilePath()
Definition: CbmGeoSetupModule.h:31
CbmSetup::GetNofModules
Int_t GetNofModules() const
Definition: CbmSetup.h:90
CbmFieldMap::SetScale
virtual void SetScale(Double_t factor)
Definition: CbmFieldMap.h:129
kDb
@ kDb
Definition: CbmSetup.h:25
CbmSetup::fgInstance
static CbmSetup * fgInstance
Pointer to static instance.
Definition: CbmSetup.h:195
CbmGeoSetupField
Definition: CbmGeoSetupField.h:21
CbmFieldMap
Definition: CbmFieldMap.h:34
CbmGeoSetup::GetName
std::string GetName()
Definition: CbmGeoSetup.h:33
CbmFieldMapData::GetType
Int_t GetType() const
Definition: CbmFieldMapData.h:49
CbmSetup::Instance
static CbmSetup * Instance()
Definition: CbmSetup.cxx:115
CbmSetup::fProvider
CbmGeoSetupProvider * fProvider
Definition: CbmSetup.h:200
CbmGeoSetupProvider::GetSetup
CbmGeoSetup & GetSetup()
Direct access to underlying geometry setup representation. Allows for fine-tuning of parameters,...
Definition: CbmGeoSetupProvider.h:62
CbmGeoSetupDbProvider
Setup provider with database functionality.
Definition: CbmGeoSetupDbProvider.h:21
CbmGeoSetupField::GetTag
std::string GetTag()
Definition: CbmGeoSetupField.h:24
CbmFieldMapSym3.h
CbmGeoSetupField::GetFilePath
std::string GetFilePath()
Definition: CbmGeoSetupField.h:28
CbmGeoSetupField::SetScale
void SetScale(Double_t value)
Definition: CbmGeoSetupField.h:40
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmSetup::GetGeoTag
Bool_t GetGeoTag(ECbmModuleId moduleId, TString &tag)
Definition: CbmSetup.cxx:101
CbmSetup::Clear
virtual void Clear(Option_t *opt="")
Definition: CbmSetup.cxx:35
CbmGeoSetupProvider::GetFieldByTag
virtual CbmGeoSetupField GetFieldByTag(std::string tag)=0
Abstract method for constructing the field by tag.
CbmGeoSetup
Data transfer object to represent the CBM Detector setup.
Definition: CbmGeoSetup.h:30
ECbmSetupSource
ECbmSetupSource
Definition: CbmSetup.h:25
CbmAddress.h
CbmGeoSetupField::GetScale
Double_t GetScale()
Definition: CbmGeoSetupField.h:30
CbmGeoSetupProvider::RemoveModule
void RemoveModule(ECbmModuleId moduleId)
Removes the module from setup.
Definition: CbmGeoSetupProvider.cxx:65
CbmFieldMapData
Definition: CbmFieldMapData.h:29
kRepo
@ kRepo
Definition: CbmSetup.h:25
CbmGeoSetup::GetField
CbmGeoSetupField & GetField()
Definition: CbmGeoSetup.h:42
CbmFieldMapSym2.h
CbmGeoSetupModule
Definition: CbmGeoSetupModule.h:22
CbmGeoSetupProvider::Reset
void Reset()
Resets the setup to default (empty)
Definition: CbmGeoSetupProvider.cxx:173
CbmFieldMap.h
CbmGeoSetupModule::GetTag
std::string GetTag()
Definition: CbmGeoSetupModule.h:26
CbmGeoSetup::GetModuleMap
std::map< ECbmModuleId, CbmGeoSetupModule > & GetModuleMap()
Definition: CbmGeoSetup.h:39
CbmSetup::SetActive
void SetActive(ECbmModuleId moduleId, Bool_t active=kTRUE)
Definition: CbmSetup.cxx:140
CbmSetup::CbmSetup
CbmSetup()
! Setup provider
Definition: CbmSetup.h:205