File indexing completed on 2023-07-02 22:50:03
0001
0002 #include <map>
0003 #include <string>
0004
0005
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/EventSetup.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0014 #include "FWCore/PluginManager/interface/ModuleDef.h"
0015 #include "DetectorDescription/Core/interface/DDCompactView.h"
0016 #include "DetectorDescription/Core/interface/DDSolid.h"
0017 #include "DetectorDescription/Core/interface/DDSolidShapes.h"
0018 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0019 #include "DetectorDescription/DDCMS/interface/DDDetector.h"
0020 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0021 #include "DD4hep/Detector.h"
0022 #include "DD4hep/DD4hepRootPersistency.h"
0023
0024 #include "TGeoManager.h"
0025 #include "TFile.h"
0026 #include "TSystem.h"
0027
0028 class PrintGeomSolids : public edm::one::EDAnalyzer<> {
0029 public:
0030 explicit PrintGeomSolids(const edm::ParameterSet&);
0031 ~PrintGeomSolids() override {}
0032 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0033
0034 void analyze(edm::Event const& iEvent, edm::EventSetup const&) override;
0035
0036 private:
0037 edm::ESGetToken<DDCompactView, IdealGeometryRecord> cpvTokenDDD_;
0038 edm::ESGetToken<cms::DDCompactView, IdealGeometryRecord> cpvTokenDD4hep_;
0039 bool fromDD4hep_;
0040 };
0041
0042 PrintGeomSolids::PrintGeomSolids(const edm::ParameterSet& ps) {
0043 fromDD4hep_ = ps.getParameter<bool>("fromDD4hep");
0044 if (fromDD4hep_)
0045 cpvTokenDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord>(edm::ESInputTag());
0046 else
0047 cpvTokenDDD_ = esConsumes<DDCompactView, IdealGeometryRecord>(edm::ESInputTag());
0048
0049 edm::LogVerbatim("PrintGeom") << "PrintGeomSolids created with dd4hep: " << fromDD4hep_;
0050 }
0051
0052 void PrintGeomSolids::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0053 edm::ParameterSetDescription desc;
0054 desc.add<bool>("fromDD4hep", false);
0055 descriptions.add("printGeomSolids", desc);
0056 }
0057
0058 void PrintGeomSolids::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0059 int solids(0);
0060 std::map<std::string, int> shapes;
0061 if (fromDD4hep_) {
0062 const cms::DDCompactView* cpv = &iSetup.getData(cpvTokenDD4hep_);
0063 const cms::DDDetector* det = cpv->detector();
0064 TGeoManager const& geom = det->description()->manager();
0065 TGeoIterator next(geom.GetTopVolume());
0066 TGeoNode* node;
0067 TString path;
0068 std::vector<std::string> names;
0069 while ((node = next())) {
0070 next.GetPath(path);
0071 std::string name = static_cast<std::string>(node->GetVolume()->GetName());
0072 if (std::find(names.begin(), names.end(), name) == names.end()) {
0073 edm::LogVerbatim("PrintGeom") << name << " "
0074 << static_cast<std::string>(node->GetVolume()->GetShape()->GetTitle());
0075 names.emplace_back(name);
0076 std::string shape = node->GetVolume()->GetShape()->GetTitle();
0077 if (shapes.find(shape) == shapes.end())
0078 shapes.emplace(std::make_pair(shape, 1));
0079 else
0080 ++shapes[shape];
0081 ++solids;
0082 }
0083 }
0084
0085 } else {
0086 const DDCompactView* cpv = &iSetup.getData(cpvTokenDDD_);
0087 const auto& gra = cpv->graph();
0088 for (DDCompactView::Graph::const_adj_iterator git = gra.begin(); git != gra.end(); ++git) {
0089 const DDLogicalPart& ddLP = gra.nodeData(git);
0090 const DDSolid& solid = ddLP.solid();
0091 edm::LogVerbatim("PrintGeom") << solid.name() << " " << DDSolidShapesName::name(solid.shape());
0092 std::string shape = DDSolidShapesName::name(solid.shape());
0093 if (shapes.find(shape) == shapes.end())
0094 shapes.emplace(std::make_pair(shape, 1));
0095 else
0096 ++shapes[shape];
0097 ++solids;
0098 }
0099 }
0100 edm::LogVerbatim("PrintGeom") << "\n\nPrintGeomSolids finds " << solids << " solids\n\n";
0101 for (std::map<std::string, int>::iterator itr = shapes.begin(); itr != shapes.end(); ++itr) {
0102 edm::LogVerbatim("PrintGeom") << "Shape:" << itr->first << " # " << itr->second;
0103 }
0104 }
0105
0106
0107 DEFINE_FWK_MODULE(PrintGeomSolids);