Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:04

0001 #include <string>
0002 #include <vector>
0003 #include <iostream>
0004 #include <algorithm>
0005 
0006 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/EventSetup.h"
0009 #include "FWCore/Framework/interface/ESTransientHandle.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 #include "FWCore/ParameterSet/interface/types.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 
0015 #include "DataFormats/DetId/interface/DetId.h"
0016 #include "DataFormats/Math/interface/Vector3D.h"
0017 #include "DetectorDescription/Core/interface/DDFilteredView.h"
0018 #include "DetectorDescription/Core/interface/DDCompactView.h"
0019 #include "DetectorDescription/Core/interface/DDMaterial.h"
0020 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0021 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0022 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0023 #include "Geometry/TrackerNumberingBuilder/interface/CmsTrackerStringToEnum.h"
0024 #include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h"
0025 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0026 
0027 static bool dddGetStringRaw(const DDFilteredView &view, const std::string &name, std::string &value) {
0028   DDValue parameter(name);
0029   std::vector<const DDsvalues_type *> result;
0030   view.specificsV(result);
0031   for (std::vector<const DDsvalues_type *>::iterator it = result.begin(); it != result.end(); ++it) {
0032     if (DDfetch(*it, parameter)) {
0033       if (parameter.strings().size() == 1) {
0034         value = parameter.strings().front();
0035         return true;
0036       } else {
0037         return false;
0038       }
0039     }
0040   }
0041   return false;
0042 }
0043 
0044 /*
0045 static inline
0046 double dddGetDouble(const std::string & s, const DDFilteredView & view) {
0047   std::string value;
0048   if (dddGetStringRaw(view, s, value))
0049     return double(::atof(value.c_str()));
0050   else
0051     return NAN;
0052 }
0053 */
0054 
0055 static inline std::string dddGetString(const std::string &s, const DDFilteredView &view) {
0056   std::string value;
0057   if (dddGetStringRaw(view, s, value))
0058     return value;
0059   else
0060     return std::string();
0061 }
0062 
0063 static inline std::ostream &operator<<(std::ostream &out, const math::XYZVector &v) {
0064   return out << "(" << v.rho() << ", " << v.z() << ", " << v.phi() << ")";
0065 }
0066 
0067 class ListIds : public edm::one::EDAnalyzer<> {
0068 public:
0069   ListIds(const edm::ParameterSet &);
0070   ~ListIds() override;
0071 
0072 private:
0073   void analyze(const edm::Event &, const edm::EventSetup &) override;
0074   void beginJob() override {}
0075   void endJob() override;
0076   // List of material names used to select specific detectors.
0077   // Names are matched literally, w/o any usage of regexp.
0078   // Names should also be specified with the correct namespace,
0079   // otherwise the matching will fail.
0080   bool printMaterial_;
0081   std::vector<std::string> materials_;
0082   edm::ESGetToken<DDCompactView, IdealGeometryRecord> dddToken_;
0083   edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> geoToken_;
0084 };
0085 
0086 ListIds::ListIds(const edm::ParameterSet &pset)
0087     : printMaterial_(pset.getUntrackedParameter<bool>("printMaterial")),
0088       materials_(pset.getUntrackedParameter<std::vector<std::string> >("materials")),
0089       dddToken_(esConsumes()),
0090       geoToken_(esConsumes()) {}
0091 
0092 ListIds::~ListIds() {}
0093 
0094 void ListIds::analyze(const edm::Event &evt, const edm::EventSetup &setup) {
0095   std::cout << "______________________________ DDD ______________________________" << std::endl;
0096   auto hDdd = setup.getTransientHandle(dddToken_);
0097 
0098   std::string attribute = "TkDDDStructure";
0099   CmsTrackerStringToEnum theCmsTrackerStringToEnum;
0100   DDSpecificsHasNamedValueFilter filter{attribute};
0101   DDFilteredView fv(*hDdd, filter);
0102   if (theCmsTrackerStringToEnum.type(dddGetString(attribute, fv)) != GeometricDet::Tracker) {
0103     fv.firstChild();
0104     if (theCmsTrackerStringToEnum.type(dddGetString(attribute, fv)) != GeometricDet::Tracker)
0105       throw cms::Exception("Configuration") << "The first child of the DDFilteredView is not what is expected \n"
0106                                             << dddGetString(attribute, fv);
0107   }
0108 
0109   std::cout << std::fixed << std::setprecision(3);
0110   bool printAnyMaterial = (std::find(materials_.begin(), materials_.end(), "ANY") != materials_.end());
0111   do {
0112     // print the full hierarchy of all elements whose material
0113     // has been specified by the user. An empty list of
0114     // materials will print no elements. The special
0115     // keyword ANY (in any location of the vector)
0116     // will select all elements.
0117     if (printAnyMaterial ||
0118         (std::find(materials_.begin(), materials_.end(), fv.logicalPart().material().name().fullname()) !=
0119          materials_.end())) {
0120       // start from 2 to skip the leading /OCMS[0]/CMSE[1] part
0121       const DDGeoHistory &history = fv.geoHistory();
0122       std::cout << '/';
0123       for (unsigned int h = 2; h < history.size(); ++h) {
0124         std::cout << '/' << history[h].logicalPart().name().ns() << ":" << history[h].logicalPart().name().name() << '['
0125                   << history[h].copyno() << ']';
0126       }
0127       if (printMaterial_)
0128         std::cout << " Material: |" << fv.logicalPart().material().name() << "|";
0129       // DD3Vector and DDTranslation are the same type as math::XYZVector
0130       math::XYZVector position = fv.translation() / 10.;  // mm -> cm
0131       std::cout << "\t" << position << std::endl;
0132     }
0133   } while (fv.next());
0134   std::cout << std::endl;
0135 
0136   std::cout << "______________________________ std::vector<GeomDet*> from TrackerGeometry::dets() "
0137                "______________________________"
0138             << std::endl;
0139   auto const &geo = setup.getData(geoToken_);
0140 
0141   std::cout << std::fixed << std::setprecision(3);
0142   auto const &dets = geo.dets();
0143   for (unsigned int i = 0; i < dets.size(); ++i) {
0144     const GeomDet &det = *dets[i];
0145 
0146     // Surface::PositionType is a typedef for Point3DBase<float,GlobalTag> a.k.a. GlobalPoint
0147     const Surface::PositionType &p = det.position();
0148     math::XYZVector position(p.x(), p.y(), p.z());
0149 
0150     std::cout << det.subDetector() << '\t' << det.geographicalId().det() << '\t' << det.geographicalId().subdetId()
0151               << '\t' << det.geographicalId().rawId() << "\t" << position;
0152     const std::vector<const GeomDet *> &parts = det.components();
0153     if (!parts.empty()) {
0154       std::cout << "\t[" << parts[0]->geographicalId().rawId();
0155       for (unsigned int j = 1; j < parts.size(); ++j)
0156         std::cout << '\t' << parts[j]->geographicalId().rawId();
0157       std::cout << ']';
0158     }
0159     std::cout << std::endl;
0160   }
0161 }
0162 
0163 void ListIds::endJob() {}
0164 
0165 //-------------------------------------------------------------------------
0166 // define as a plugin
0167 #include "FWCore/PluginManager/interface/ModuleDef.h"
0168 #include "FWCore/Framework/interface/MakerMacros.h"
0169 DEFINE_FWK_MODULE(ListIds);