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