ATLAS Offline Software
DumpGeo.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Initial version:
6 // - 2017, Sep -- Riccardo Maria BIANCHI
7 // <riccardo.maria.bianchi@cern.ch>
8 // Main updates:
9 // - 2024, Feb -- Riccardo Maria BIANCHI
10 // <riccardo.maria.bianchi@cern.ch>
11 // Moved configuration to ComponentAccumulator (CA)
12 // - 2024, Mar -- Riccardo Maria BIANCHI
13 // <riccardo.maria.bianchi@cern.ch>
14 // Removed GeoExporter, moved all to DumpGeo
15 // - 2024, Apr -- Added missing sub-systems: Cavern, HGTD, FWD detectors, ...
16 
17 #include "DumpGeo/DumpGeo.h"
18 
19 // Athena includes
23 
24 // From GeoModel in Externals
25 #include "GeoModelDBManager/GMDBManager.h"
26 #include "GeoModelKernel/GeoBox.h"
27 #include "GeoModelKernel/GeoVDetectorManager.h"
28 #include "GeoModelKernel/GeoVolumeCursor.h"
29 #include "GeoModelWrite/WriteGeoModel.h"
30 // #include "GeoModelHelpers/defineWorld.h" // not available in 24.0...
31 
32 // C++ includes
33 #include <cstdlib> //For setenv
34 #include <iomanip>
35 #include <iostream>
36 #include <stdexcept>
37 #include <vector>
38 
39 // Units
40 #include "GeoModelKernel/Units.h"
41 #define UNITS GeoModelKernelUnits // so we can use, e.g., 'UNITS::cm'
42 
43 // TODO: replace this with GeoModelHelpers/defineWorld.h
44 //_____________________________________________________________________________________
45 GeoPhysVol* createTheWorld() {
46 // Define the units
47 #define gr UNITS::gram
48 #define mole UNITS::mole
49 #define cm3 UNITS::cm3
50 
51  // Define the chemical elements
52  GeoElement* Nitrogen =
53  new GeoElement("Nitrogen", "N", 7.0, 14.0067 * gr / mole);
54  GeoElement* Oxygen = new GeoElement("Oxygen", "O", 8.0, 15.9995 * gr / mole);
55  GeoElement* Argon = new GeoElement("Argon", "Ar", 18.0, 39.948 * gr / mole);
56  GeoElement* Hydrogen =
57  new GeoElement("Hydrogen", "H", 1.0, 1.00797 * gr / mole);
58 
59  // Define the materials
60  double densityOfAir = 0.001214 * gr / cm3;
61  GeoMaterial* air = new GeoMaterial("Air", densityOfAir);
62  air->add(Nitrogen, 0.7494);
63  air->add(Oxygen, 0.2369);
64  air->add(Argon, 0.0129);
65  air->add(Hydrogen, 0.0008);
66  air->lock();
67 
68  //-----------------------------------------------------------------------------------//
69  // create the world volume container and
70  // get the 'world' volume, i.e. the root volume of the GeoModel tree
71  const GeoBox* worldBox =
72  new GeoBox(1000 * UNITS::cm, 1000 * UNITS::cm, 1000 * UNITS::cm);
73  const GeoLogVol* worldLog = new GeoLogVol("WorldLog", worldBox, air);
74  GeoPhysVol* world = new GeoPhysVol(worldLog);
75  return world;
76 }
77 
78 //____________________________________________________________________
79 DumpGeo::DumpGeo(const std::string& name, ISvcLocator* svcLocator)
80  : AthAlgorithm(name, svcLocator) {}
81 
82 //____________________________________________________________________
84  ATH_MSG_DEBUG("in initialize()");
85 
86  ATH_MSG_INFO("===================================================");
87  ATH_MSG_INFO("\t\tLaunching DumpGeo");
88  ATH_MSG_INFO("===================================================");
89 
90  ATH_MSG_INFO("Accessing the ATLAS geometry...");
91  const GeoModelExperiment* theExpt = nullptr;
92  ATH_CHECK(detStore()->retrieve(theExpt, "ATLAS"));
93 
94  // Get the ATLAS GEOMETRY 'World' volume
95  PVConstLink world(theExpt->getPhysVol());
96 
98  "User filter DetectorManagers: " << m_user_filterDetManagersList);
99 
100  ServiceHandle<IGeoDbTagSvc> geoDbTag("GeoDbTagSvc", name());
101  ATH_CHECK(geoDbTag.retrieve());
102  ATH_MSG_INFO(
103  "This is the Geometry TAG we are dumping: " << geoDbTag->atlasVersion());
104 
105  GeoPhysVol* volTop = createTheWorld();
106 
107  // Get list of managers
108  // We fill a set from the output vector,
109  // so we can use its built-in 'count' method later,
110  // to search for DetManagers
111  ATH_MSG_INFO("Dumping the GeoModel tree...");
112  std::set<std::string> managersList{};
113  {
114  std::vector<std::string> blub = theExpt->getListOfManagers();
115  managersList.insert(blub.begin(), blub.end());
116  }
117  if (msgLvl(MSG::INFO)) {
118  ATH_MSG_INFO("List of the GeoModel Detector Managers that are being dumped: ");
119  for (auto const& man : managersList) {
120  // get the DetectorManager
121  const GeoVDetectorManager* manager = theExpt->getManager(man);
122  unsigned int nTreetops = manager->getNumTreeTops();
123  std::cout << "\t" << man << " [contains " << nTreetops << " treetops]"
124  << std::endl;
125  for (unsigned int i = 0; i < nTreetops; ++i) {
126  PVConstLink treetop(manager->getTreeTop(i));
127  // get treetop's volume
128  const GeoVPhysVol* vol = treetop;
129  // get treetop's logvol's name
130  std::string volName = vol->getLogVol()->getName();
131  std::cout << "\t\t treetop: " << volName << std::endl;
132  }
133  }
134  }
135 
136  if (!(m_user_filterDetManagersList.empty())) {
137  // Convert the list of det managers passed by the user into a set
138  std::set<std::string> user_managersList{};
139  {
140  user_managersList.insert(m_user_filterDetManagersList.begin(),
142  }
143  // safety check:
144  // check that all DetManagers requested by the user are in the list
145  // If not, print an error message to warn the user and return
146  for (auto& userDet : user_managersList) {
147  if (!managersList.count(userDet)) {
149  "This Detector Manager you requested to dump is not in the list of "
150  "DetectorManagers for the geometry tag you are using: "
151  << userDet);
152  throw GaudiException(
153  "The Detector Manager you requested to dump is not in the list of "
154  "DetectorManagers.",
155  "DumpGeo", StatusCode::FAILURE);
156  }
157  }
158 
159  if (!(managersList.empty())) {
160  for (auto const& mm : managersList) {
161  // get the DetectorManager
162  const GeoVDetectorManager* manager = theExpt->getManager(mm);
163 
164  // get the name of the DetectorManager
165  std::string detManName = manager->getName();
166  ATH_MSG_INFO("\tDetectorManager: " << detManName);
167 
168  // get the DetManager's TreeTops
169  unsigned int nTreetops = manager->getNumTreeTops();
170  ATH_MSG_INFO("\t" << mm << " - # TreeTops: " << nTreetops);
171 
172  // if ( nTreetops > 0) && isStringInVector(m_user_filterDetManagersList,
173  // detManName) ) {
174  if ((nTreetops > 0) && user_managersList.count(detManName)) {
175 
176  for (unsigned int i = 0; i < nTreetops; ++i) {
177 
178  PVConstLink treetop(manager->getTreeTop(i));
179 
180  // get treetop's volume
181  const GeoVPhysVol* vol = treetop;
182 
183  // get volume's transform
184  // NOTE: we use getDefX() to get the transform without any alignment
185  GeoTransform* volXf = new GeoTransform(vol->getDefX());
186 
187  // get volume's logvol's name
188  std::string volName = vol->getLogVol()->getName();
189  ATH_MSG_DEBUG("\t\t treetop: " << volName);
190 
191  // Add to the main volume a GeoNameTag with the name of the
192  // DetectorManager
193  volTop->add(new GeoNameTag(detManName));
194  // add Transform and Volume to the main PhysVol
195  volTop->add(volXf);
196  volTop->add(const_cast<GeoVPhysVol*>(vol));
197 
198  if (msgLvl(MSG::DEBUG)) {
199  // DEBUG: dive into the Treetop
200  if ("BeamPipe" == detManName) {
201  GeoVolumeCursor av(treetop);
202  while (!av.atEnd()) {
203  ATH_MSG_DEBUG("\t\ttreetop n."
204  << i << " - child name: " << av.getName());
205  av.next(); // increment volume cursor.
206  } // end while
207  }
208  }
209  }
210  }
211  }
212  }
213  }
214 
215  // DEBUG inspection
216  if (msgLvl(MSG::DEBUG)) {
218  "Looping over top volumes in the GeoModel tree (children of the "
219  "'World' volume)...");
220  GeoVolumeCursor av(world);
221  while (!av.atEnd()) {
222  std::string volname = av.getName();
223  ATH_MSG_DEBUG("\t* relevant NameTag:" << volname);
224  av.next(); // increment volume cursor.
225  }
226  }
227 
228  ATH_MSG_INFO("Creating the SQLite DB file...");
229  if (m_outFileName.empty()) {
230  ATH_MSG_FATAL("The name of the output SQLite file is not set!");
231  throw GaudiException("The name of the output SQLite file is not set!",
232  "DumpGeo", StatusCode::FAILURE);
233  }
234  ATH_MSG_INFO("Output file name: " << m_outFileName);
235 
236  // open the DB connection
237  GMDBManager db(m_outFileName);
238 
239  // check the DB connection
240  if (db.checkIsDBOpen())
241  ATH_MSG_INFO("OK! Database is open!");
242  else {
243  ATH_MSG_ERROR(" ***** Database ERROR!! Exiting...");
244  throw GaudiException(
245  "The GeoModel SQLite .db file could not be opened successfully.",
246  "DumpGeo", StatusCode::FAILURE);
247  }
248 
249  ATH_MSG_INFO("Traversing the GeoModel tree...");
250  // Dump the tree volumes into a DB
251  GeoModelIO::WriteGeoModel dumpGeoModelGraph(
252  db); // init the GeoModel node action
253  // visit all GeoModel nodes
254  if (!(m_user_filterDetManagersList.empty())) {
255  volTop->exec(&dumpGeoModelGraph);
256  } else {
257  world->exec(&dumpGeoModelGraph);
258  }
259  ATH_MSG_INFO("Saving the GeoModel tree to the DB...");
260  dumpGeoModelGraph.saveToDB(); // save to the SQlite DB file
261  ATH_MSG_ALWAYS("DONE. Geometry saved to " << m_outFileName);
262 
263  // Quick test if DEBUG
264  if (msgLvl(MSG::DEBUG)) {
266  "Test - list of all the GeoMaterial nodes in the persistified "
267  "geometry:");
268  db.printAllMaterials();
270  "Test - list of all the GeoElement nodes in the persistified "
271  "geometry:");
272  db.printAllElements();
273  }
274 
275  ATH_MSG_DEBUG("End of DumpGeo::init().");
276  return StatusCode::SUCCESS;
277 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
GeoModelExperiment::getPhysVol
GeoPhysVol * getPhysVol()
Destructor.
Definition: GeoModelExperiment.cxx:21
DumpGeo::m_outFileName
Gaudi::Property< std::string > m_outFileName
Definition: DumpGeo.h:48
DumpGeo::initialize
StatusCode initialize()
Definition: DumpGeo.cxx:83
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
createTheWorld
GeoPhysVol * createTheWorld()
Definition: DumpGeo.cxx:45
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthMsgStreamMacros.h
CaloCondBlobAlgs_fillNoiseFromASCII.db
db
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:43
DumpGeo::DumpGeo
DumpGeo(const std::string &name, ISvcLocator *pSvcLocator) ATLAS_CTORDTOR_NOT_THREAD_SAFE
Definition: DumpGeo.cxx:79
cm3
#define cm3
GeoModelExperiment
Definition: GeoModelExperiment.h:32
AthCommonMsg< Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
gr
#define gr
DumpGeo::m_user_filterDetManagersList
Gaudi::Property< std::vector< std::string > > m_user_filterDetManagersList
Definition: DumpGeo.h:50
GeoModelExperiment::getListOfManagers
std::vector< std::string > getListOfManagers() const
Definition: GeoModelExperiment.cxx:64
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
GeoModelExperiment.h
cm
const double cm
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloSimParametrization/tools/FCAL_ChannelMap.cxx:25
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
GeoModelExperiment::getManager
const GeoVDetectorManager * getManager(const std::string &name) const
Definition: GeoModelExperiment.cxx:52
ATH_MSG_ALWAYS
#define ATH_MSG_ALWAYS(x)
Definition: AthMsgStreamMacros.h:35
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthAlgorithm
Definition: AthAlgorithm.h:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
DumpGeo.h
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
DEBUG
#define DEBUG
Definition: page_access.h:11
mole
#define mole
python.Logging.manager
manager
Definition: PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/Logging.py:92
IGeoDbTagSvc.h
ServiceHandle< IGeoDbTagSvc >