BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
GeoSetup.h
Go to the documentation of this file.
1#ifndef _GEO_SETUP_INCLUDED
2#define _GEO_SETUP_INCLUDED
3
4#include "BmnFieldMap.h"
5#include "FairModule.h"
6#include "FairRunSim.h"
7#include "TFile.h"
8#include "TGeoMatrix.h"
9#include "TGeoVolume.h"
10#include "TSQLRow.h"
11#include "TSQLServer.h"
12#include "TString.h"
13#include "TXMLEngine.h"
14#include "geo_db_settings.h"
15
16#include <map>
17#include <string>
18#include <utility>
19
20using namespace std;
21
29
30typedef struct MODULEREVISION
31{
33 int idmr{-1}, idm{-1}, rev{-1};
34 bool active{true};
35 // name of the module object passed to the constructor of the class ('object_name' column of the module_class table)
38 // shift (cm) of the module relative to its position in the geometry file, set for the modules added by the local
39 // settings of the geometry ('addSetupModule' element)
40 double xMove{0}, yMove{0}, zMove{0};
42
43/**
44 * GeoSetup class. It is used for loading necessary setup geometries from the Geometry Database.
45 */
47{
48 public:
49 // default constructor
51 : m_db(nullptr)
52 , m_field(nullptr)
53 , m_fid(-1)
54 , m_sid(-1)
55 , m_tmpFolder("")
56 {}
57
58 /**
59 * Destructor method
60 */
61 ~GeoSetup();
62
63 /**
64 * Load selected geometry setup from the Local Geometry Database and create gGeoManager instance.
65 * Use for Reconstruction and Analysis
66 * @param setup_tag the name of the setup tag.
67 * @return 0, if the setup is loaded. Error code, if the setup is not loaded.
68 */
69 int LoadSetupToGeoManager(string_view setup_tag);
70
71 /**
72 * Load selected geometry setup from the Local or Central (defined by url) Geometry Database and add it to the
73 * FairRunSim instance. Use for Simulation
74 * @param setupTag the name of the setup tag.
75 * @param localSettings path to local setting file that can disable or move some detector in selected geometry
76 * (see examples/README.md). The setup is not loaded, if the local settings are wrong.
77 * @return 0, if the setup is loaded. Error code, if the setup is not loaded: -2 - the setup is not found, -3 - the
78 * setup is damaged (no modules, no geometry file or a module cannot be created), -4 - the local settings cannot be
79 * applied.
80 */
81 int LoadSetupToFairRunSim(string_view setupTag, const char* localSettings = nullptr);
82
83 /**
84 * Parse the geometry tag, which can contain the path to an XML file with a local customization of the setup (see
85 * examples/README.md) after the ';' separator, e.g. "run9_26.09.0;$VMCWORKDIR/my_setup.xml". If no XML file is
86 * given after the separator, the whole value is considered as the setup tag.
87 * @param geometry_tag the geometry tag to parse, the parsed setup tag is returned via the same link.
88 * @param local_settings returns the path to the XML file as given (not expanded, e.g. with $VMCWORKDIR) to be
89 * passed to LoadSetupToFairRunSim, "" if no file is given.
90 * @return 0 - success, < 0 - error codes: -1 - unexpected part in the tag, -2 - the XML file is not found.
91 */
92 static int ParseGeometryTag(TString& geometry_tag, TString& local_settings);
93
94 /**
95 * Get magnetic field of the current setup geometry. The field map is returned as it was measured, i.e. with the
96 * unit scale corresponding to the reference voltage (see GetRefVoltage). The scale of the run ('field voltage of
97 * the run / reference voltage') is set by the caller before the field is initialized (in BmnRoot by
98 * BmnFunctionSet::SetFieldScale for any type of the field), the zero scale gives the zero field (the magnet is off,
99 * e.g. for the calibration runs).
100 * @return Instance of the magnetic field class inherited from FairField. nullptr if the setup has no field.
101 */
103
104 /**
105 * Get reference voltage of the magnetic field of the current setup geometry, i.e. the voltage, at which the field
106 * map was measured. It is read from the Geometry Database together with the field map, so GetMagneticField has to
107 * be called before.
108 * @return Reference voltage (mV). -1 if the field is not loaded, 0 if the Geometry Database does not provide the
109 * value (old versions of the database).
110 */
111 double GetRefVoltage() { return m_refVoltage; }
112
113 /**
114 * Get path to parameter file for the given detector name of the current setup.
115 * @param detector_name the name of the detector (the module name in the Geometry Database, e.g. GEM), for which the
116 * parameter file is needed.
117 * @return Path to the parameter file in the storage of the Local Geometry Database. "" if file did not set.
118 */
119 string GetParameterFile(string_view detector_name);
120
121 /**
122 * Get the material file of the current setup, which defines the media of the setup geometry.
123 * @return Name of the material file in the Geometry Database. "" if the setup is not loaded or has no materials.
124 */
125 string GetMaterialFile();
126
127 /**
128 * Get tag for the last approved setup corresponding to the pattern.
129 * @param tag_prefix The prefix string to match against approved setup tags.
130 * @return Tag of last approved setup corresponding to the prefix string. "" if no approved setup is found.
131 */
132 string GetLastApprovedSetupTag(string_view tag_prefix);
133 /**
134 * Get tag for the loaded setup.
135 * @return The Tag of loaded setup.
136 */
137 string_view GetSetupTag() { return m_setupTag; }
138
139 private:
140 map<int, TString> m_modules_table;
141 // whether the module is a detector ('active' column of the module table) by the module ID, empty if the Geometry
142 // Database does not provide the column
143 map<int, bool> m_modules_active;
144 map<int, module_r_ptr> m_modules_revision_table;
145 map<int, TString> m_loadedModule;
146 // local settings of the setup modules by the tag of the setup module ('tag' attribute)
147 map<TString, setup_module_ptr> m_moduleEdit;
148 // local settings of the setup modules by the name of the module ('module' attribute)
149 map<TString, setup_module_ptr> m_moduleEditByName;
150 // local settings applied to all the detector modules of the setup ('module="*"' attribute), nullptr if not set
152 vector<module_r_ptr> m_moduleAdd;
153
154 // path to the field map from the local settings ('editFieldMap' element), which replaces the map of the setup
156 // connection to the Geometry Database
158 // magnetic field of the geometry setup, its scale is set by the caller
160 // reference voltage (mV) of the field map: -1 - the field is not loaded, 0 - not provided by the Geometry Database
161 double m_refVoltage = -1;
162 // id of the current magnetic field
163 int m_fid;
164 // id of the current geometry setup
165 int m_sid;
166 // current tmp directory (/tmp/$USER/)
168 // tag name of setup
169 string m_setupTag = "";
170 // name of the material file of the setup in the Geometry Database
171 string m_materialFile = "";
172
173 unsigned long int GetServerRevision(const char* url = GEO_SERVER_HOST);
174 std::pair<int, string> GetTagName4PatternServer(string_view pattern_tag, const char* url = GEO_SERVER_HOST);
175 std::pair<int, uint64_t> CheckTagOnServer(string_view setup_tag, const char* url = GEO_SERVER_HOST);
176 unsigned long int GetLocalRevision(TString& localDbPath);
177 std::pair<int, string> GetTagName4PatternLocal(string_view pattern_tag, TString& localDbPath);
178 int installLocalDB(string_view setup_tag, const char* url = GEO_SERVER_HOST);
179 void CloseDB();
181
182 // create connection to the Local Geometry Database and fill the tables with modules and their revisions
184 bool IsEqlMatrix(const char* fileName, TSQLRow* row);
185 TGeoNode* ConstructRootGeometry(const char* fileName);
186 static bool exists_file(const char* name);
187 // check that the directory can be modified, e.g. it is not a part of a read-only file system
188 static bool IsWritableDir(const char* path);
189 // get a writable directory (a unique folder in $DBL_CACHE_PATH, $TMPDIR, /tmp/$USER) to install
190 // the Local Geometry Database to, when the directory of the database is read-only
192 // get path to the storage of the Local Geometry Database (GEOMPATH) and to the database file (local.db in the
193 // parent directory of the storage); "" if GEOMPATH is not set
194 string GetRootPath();
195 string GetLocalDbPath();
196 // get expanded value of the GEOMPATH environment variable (the storage of the Local Geometry Database) without the
197 // trailing slashes; "" if not set or cannot be expanded
198 string ExpandGeomPath();
199 // get path to a temporary directory (/tmp/$USER/), and create if absent
201 // get unique name of a temporary file in the temporary directory
202 TString GetTmpFileName(const char* baseName);
203 // get root volume in the TFile object
205
206 // get module name by index; return "Unknown" if not found
207 TString GetModuleName(int dbModuleUndex);
208 // get module index by name; return -1 if not found
209 int GetModuleIndex(TString& name);
210 // get module ID by revision number; return -1 if not found
211 int GetModuleId(int dbModuleRev);
212 // get module type by revision number
213 TString GetModuleType(int dbModuleRev);
214 // get module_r_ptr (pointer to the module revision) by revision number
215 module_r_ptr GetModuleRevision(int dbModuleRev);
216
217 // load local settings from the file for all setup modules
218 // return value: false, if the file cannot be parsed or contains wrong settings (all errors are reported)
219 bool LoadLocalSettings(const char* fileName);
220 // parse the elements of the local settings: change or disable a module of the setup ('editSetupModule'), add a new
221 // module ('addSetupModule'), replace the field map ('editFieldMap'); return value: false for wrong settings
225 // delete the loaded local settings of the geometry
226 void ClearLocalSettings();
227 // get local settings for the setup module: by the tag of the setup module, then by the name of its module, then by
228 // the settings for all the detector modules ('module="*"'); return nullptr if no local settings for the module
229 setup_module_ptr FindModuleEdit(TString setup_module_tag, TString module_name, int dbModuleRev);
230 // find the geometry file of a module set by the local settings and check that it contains a geometry. A relative
231 // path is searched for in the same directories as FairRoot does: $GEOMPATH (the storage of the Local Geometry
232 // Database) and $VMCWORKDIR/geometry. The full path of the found file is returned via the link
233 // return value: true, if the file can be used as the geometry of a module
234 bool FindModuleGeometryFile(TString& file_path);
235 // write the geometry of the module from the ROOT file to a temporary ROOT file with a new position of the module
236 // file_path: geometry file of the module; module_name: name to create a unique name of the temporary file;
237 // placement: new matrix of the module, nullptr - the matrix from the file; xMove, yMove, zMove: additional shift
238 // return value: path to the temporary file (it is removed at the process exit), "" in case of errors
239 TString ExportModuleGeometry(const TString& file_path,
240 const TString& module_name,
241 const TGeoMatrix* placement,
242 double xMove,
243 double yMove,
244 double zMove);
245 // create the module object of the given class with the given module name. The active flag is passed to the
246 // constructor if the class accepts it, as the detector classes do, but the passive FairRoot modules (the cave, the
247 // magnet, the target and the beam pipe) have no such parameter
248 // return value: the created module, nullptr in case of errors
249 FairModule* CreateModule(const TString& class_name, const TString& module_name, bool isActive);
250
251 // load module from the Geometry Database and add to the FairRunSim instance
252 // return value: 0 - success, -1 - no geometry of the setup module, -2 - the module cannot be created, -3 - the
253 // local settings cannot be applied to the module
254 int LoadModuleToFairRunSim(int idsm, FairRunSim* fRun);
255 // add FairModule defined by revision number and geometry file path to the FairRunSim instance
256 bool AddModuleToFairRunSim(int dbModuleRev, const char* path, FairRunSim* fRun);
257
258 // fill map with modules from the Geometry Database
259 int PutModules();
260 // fill map with module revisions from the Geometry Database
261 int PutModuleRevisions();
262};
263
264#endif
struct SETUPMODULE setup_module
struct SETUPMODULE * setup_module_ptr
struct MODULEREVISION module_r
struct MODULEREVISION * module_r_ptr
void * XMLNodePointer_t
map< TString, setup_module_ptr > m_moduleEditByName
Definition GeoSetup.h:149
map< TString, setup_module_ptr > m_moduleEdit
Definition GeoSetup.h:147
map< int, module_r_ptr > m_modules_revision_table
Definition GeoSetup.h:144
FairField * GetMagneticField()
Get magnetic field of the current setup geometry.
TString & GetTmpPath()
double GetRefVoltage()
Get reference voltage of the magnetic field of the current setup geometry, i.e.
Definition GeoSetup.h:111
int GetModuleId(int dbModuleRev)
TGeoNode * ConstructRootGeometry(const char *fileName)
string GetParameterFile(string_view detector_name)
Get path to parameter file for the given detector name of the current setup.
void ClearLocalSettings()
string m_setupTag
Definition GeoSetup.h:169
bool OpenLocalGeoDatabase()
Definition GeoSetup.cxx:538
double m_refVoltage
Definition GeoSetup.h:161
int m_sid
Definition GeoSetup.h:165
TString GetModuleName(int dbModuleUndex)
FairModule * CreateModule(const TString &class_name, const TString &module_name, bool isActive)
TString m_ExternalField
Definition GeoSetup.h:155
int LoadModuleToFairRunSim(int idsm, FairRunSim *fRun)
TSQLServer * m_db
Definition GeoSetup.h:157
int PutModules()
TString GetTmpFileName(const char *baseName)
Definition GeoSetup.cxx:171
int LoadSetupToGeoManager(string_view setup_tag)
Load selected geometry setup from the Local Geometry Database and create gGeoManager instance.
Definition GeoSetup.cxx:592
string GetLocalDbPath()
bool FindModuleGeometryFile(TString &file_path)
bool ParseEditFieldMap(TXMLEngine &xml, XMLNodePointer_t node)
bool LoadLocalSettings(const char *fileName)
string GetMaterialFile()
Get the material file of the current setup, which defines the media of the setup geometry.
map< int, TString > m_loadedModule
Definition GeoSetup.h:145
map< int, TString > m_modules_table
Definition GeoSetup.h:140
TString ExportModuleGeometry(const TString &file_path, const TString &module_name, const TGeoMatrix *placement, double xMove, double yMove, double zMove)
string GetRootPath()
bool ParseEditSetupModule(TXMLEngine &xml, XMLNodePointer_t node)
std::pair< int, uint64_t > CheckTagOnServer(string_view setup_tag, const char *url=GEO_SERVER_HOST)
Definition GeoSetup.cxx:251
TGeoVolume * GetRootVolume(TFile *f)
int installLocalDB(string_view setup_tag, const char *url=GEO_SERVER_HOST)
Definition GeoSetup.cxx:407
unsigned long int GetServerRevision(const char *url=GEO_SERVER_HOST)
Definition GeoSetup.cxx:176
string_view GetSetupTag()
Get tag for the loaded setup.
Definition GeoSetup.h:137
TString GetModuleType(int dbModuleRev)
GeoSetup()
Definition GeoSetup.h:50
void CloseDB()
Definition GeoSetup.cxx:516
int PutModuleRevisions()
TString PrepareWritableDbDir()
Definition GeoSetup.cxx:376
setup_module_ptr FindModuleEdit(TString setup_module_tag, TString module_name, int dbModuleRev)
int m_fid
Definition GeoSetup.h:163
module_r_ptr GetModuleRevision(int dbModuleRev)
static bool exists_file(const char *name)
Definition GeoSetup.cxx:34
static int ParseGeometryTag(TString &geometry_tag, TString &local_settings)
Parse the geometry tag, which can contain the path to an XML file with a local customization of the s...
Definition GeoSetup.cxx:742
static bool IsWritableDir(const char *path)
Definition GeoSetup.cxx:352
int GetModuleIndex(TString &name)
TString m_tmpFolder
Definition GeoSetup.h:167
bool ParseAddSetupModule(TXMLEngine &xml, XMLNodePointer_t node)
bool IsEqlMatrix(const char *fileName, TSQLRow *row)
~GeoSetup()
Destructor method.
Definition GeoSetup.cxx:531
unsigned long int GetLocalRevision(TString &localDbPath)
Definition GeoSetup.cxx:324
FairField * m_field
Definition GeoSetup.h:159
std::pair< int, string > GetTagName4PatternLocal(string_view pattern_tag, TString &localDbPath)
Definition GeoSetup.cxx:298
bool AddModuleToFairRunSim(module_r_ptr module, FairRunSim *fRun)
std::pair< int, string > GetTagName4PatternServer(string_view pattern_tag, const char *url=GEO_SERVER_HOST)
Definition GeoSetup.cxx:210
string ExpandGeomPath()
string GetLastApprovedSetupTag(string_view tag_prefix)
Get tag for the last approved setup corresponding to the pattern.
Definition GeoSetup.cxx:196
setup_module_ptr m_moduleEditAll
Definition GeoSetup.h:151
vector< module_r_ptr > m_moduleAdd
Definition GeoSetup.h:152
string m_materialFile
Definition GeoSetup.h:171
map< int, bool > m_modules_active
Definition GeoSetup.h:143
int LoadSetupToFairRunSim(string_view setupTag, const char *localSettings=nullptr)
Load selected geometry setup from the Local or Central (defined by url) Geometry Database and add it ...
Definition GeoSetup.cxx:793
const char *const GEO_SERVER_HOST
TString class_name
Definition GeoSetup.h:32
double yMove
Definition GeoSetup.h:40
double zMove
Definition GeoSetup.h:40
double xMove
Definition GeoSetup.h:40
TString object_name
Definition GeoSetup.h:36
TString file_path
Definition GeoSetup.h:37
TString type
Definition GeoSetup.h:24
double zMove
Definition GeoSetup.h:25
TString file_path
Definition GeoSetup.h:27
double xMove
Definition GeoSetup.h:25
double yMove
Definition GeoSetup.h:25
bool enable
Definition GeoSetup.h:26