File indexing completed on 2026-05-29 07:35:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <DD4hep/DD4hepUnits.h>
0024 #include <DD4hep/Factories.h>
0025 #include <DD4hep/Printout.h>
0026 #include <DD4hep/Detector.h>
0027 #include <XML/Conversions.h>
0028 #include <XML/XML.h>
0029 #include <iostream>
0030 #include <fstream>
0031 #include <cerrno>
0032
0033 using namespace dd4hep;
0034
0035 namespace {
0036 class Point {
0037 public:
0038 Position position;
0039 };
0040 }
0041 template <> void Converter<Point>::operator()(xml_h e) const {
0042 xml_dim_t p = e;
0043 Point pt = { Position(p.x(0e0), p.y(0e0), p.z(0e0)) };
0044 auto* points = (std::vector<Point>*)this->param;
0045 points->emplace_back( pt );
0046 }
0047
0048 static int print_field(Detector& detector, int argc, char** argv) {
0049 std::string fname;
0050 for( int i = 0; i < argc && argv[i]; ++i ) {
0051 if ( 0 == ::strncmp("-points",argv[i],4) )
0052 fname = argv[++i];
0053 else {
0054 break;
0055 }
0056 }
0057 if( fname.empty() ) {
0058 std::cout <<
0059 "Usage: -plugin DD4hep_PrintField -arg [-arg] \n\n"
0060 " Print electro magnetic field at various points supplied by xml input \n\n"
0061 " -point <string> Input file with global point coordinates \n"
0062 " Arguments given: " << arguments(argc,argv) << std::endl << std::flush;
0063 ::exit(EINVAL);
0064 }
0065 xml::DocumentHolder doc(xml::DocumentHandler().load(fname));
0066 xml_h root = doc.root();
0067 if( !root.ptr() ) {
0068 printout(ALWAYS, "PrintField","+++ XML input %s does not exist. Exiting.", fname.c_str());
0069 ::exit(EINVAL);
0070 }
0071 printout(ALWAYS, "PrintField","+++ =========================================================");
0072 printout(ALWAYS, "PrintField","+++ Electro-magnetic field strength at selected points from %s", fname.c_str());
0073 printout(ALWAYS, "PrintField","+++ =========================================================");
0074 std::vector<Point> points;
0075 double vm = dd4hep::volt/dd4hep::meter;
0076 OverlayedField field = detector.field();
0077 xml_coll_t(root, _U(point)).for_each(Converter<Point>(detector, &points));
0078 for( std::size_t i=0; i< points.size(); ++i ) {
0079 const auto& pos = points[i].position;
0080 double value[6] = { 0e0, 0e0, 0e0, 0e0, 0e0, 0e0 };
0081 field.electromagneticField(pos, value);
0082 printout(ALWAYS, "PrintField","+++ "
0083 "Position: %7.2f %7.2f %7.2f [cm] "
0084 "electric field: %7.2e %7.2e %7.2e [V/m] "
0085 "magnetic field: %7.2e %7.2e %7.2e [tesla]",
0086 pos.x()/dd4hep::cm, pos.y()/dd4hep::cm, pos.z()/dd4hep::cm,
0087 value[0]/vm, value[1]/vm, value[2]/vm,
0088 value[3]/dd4hep::tesla,
0089 value[4]/dd4hep::tesla,
0090 value[5]/dd4hep::tesla );
0091 }
0092
0093 return 1;
0094 }
0095 DECLARE_APPLY(DD4hep_PrintField,print_field)