DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
dumpBfield.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 //==========================================================================
11 //
12 // Simple program to dump the B-field of the world volume in a cartesian grid
13 //
14 // Author : F.Gaede, DESY
15 // Date : 29 Mar 2017
16 //==========================================================================
17 
18 // Framework include files
19 #include "DD4hep/Detector.h"
20 #include "DD4hep/DD4hepUnits.h"
21 
22 using namespace std ;
23 using namespace dd4hep ;
24 using namespace dd4hep::detail;
25 
26 //=============================================================================
27 
28 
29 static int invoke_dump_B_field(int argc, char** argv ){
30 
31  if( argc != 8 ) {
32  std::cout << " usage: dumpBfield compact.xml xmin[:xmax] ymin[:ymax] zmin[:zmax] dx dy dz [in cm]" << std::endl
33  << " will dump the B-field in volume (xmin:xmax, ymin:ymax, zmin:zmax) with steps (dx,dy,dz). All values are in cm."
34  << " If a single value is given for a range, symmetric boundaries are used"
35  << std::endl ;
36 
37  exit(1) ;
38  }
39 
40  std::string inFile = argv[1] ;
41 
42  std::stringstream sstr ;
43  sstr << argv[2] << " " << argv[3] << " " << argv[4] << " " << argv[5] << " " << argv[6] << " " << argv[7] ;
44 
45  std::string RangeX , RangeY , RangeZ;
46  float dx , dy, dz ;
47 
48  sstr >> RangeX >> RangeY >> RangeZ >> dx >> dy >> dz;
49 
50  size_t colon_posX = RangeX.find(':');
51  size_t colon_posY = RangeY.find(':');
52  size_t colon_posZ = RangeZ.find(':');
53 
54  float minX=0, maxX=0, minY=0, maxY=0, minZ=0, maxZ=0;
55 
56  if( colon_posX == std::string::npos ) {
57  std::cout << "X Interval not specified as xmin:xmax" << std::endl
58  << " setting xmin = -xmax " << std::endl;
59  maxX = std::stof( RangeX );
60  minX = -maxX;
61  }
62  else {
63  minX = std::stof( RangeX.substr(0, colon_posX) );
64  maxX = std::stof( RangeX.substr(colon_posX+1) );
65  }
66 
67  if( colon_posY == std::string::npos ) {
68  std::cout << "Y Interval not specified as ymin:ymax" << std::endl
69  << " setting ymin = -ymax " << std::endl;
70  maxY = std::stof( RangeY );
71  minY = -maxY;
72  }
73  else {
74  minY = std::stof( RangeY.substr(0, colon_posY) );
75  maxY = std::stof( RangeY.substr(colon_posY+1) );
76  }
77 
78 if( colon_posZ == std::string::npos ) {
79  std::cout << "Z Interval not specified as zmin:zmax" << std::endl
80  << " setting zmin = -zmax " << std::endl;
81  maxZ = std::stof( RangeZ );
82  minZ = -maxZ;
83  }
84  else {
85  minZ = std::stof( RangeZ.substr(0, colon_posZ) );
86  maxZ = std::stof( RangeZ.substr(colon_posZ+1) );
87  }
88 
89  minX *= dd4hep::cm;
90  maxX *= dd4hep::cm;
91  minY *= dd4hep::cm;
92  maxY *= dd4hep::cm;
93  minZ *= dd4hep::cm;
94  maxZ *= dd4hep::cm;
95  dx *= dd4hep::cm;
96  dy *= dd4hep::cm;
97  dz *= dd4hep::cm;
98 
99  Detector& description = Detector::getInstance();
100  description.fromCompact( inFile );
101 
102  printf("#######################################################################################################\n");
103  printf(" x[cm] y[cm] z[cm] Bx[Tesla] By[Tesla] Bz[Tesla] \n");
104 
105  for( float x = minX ; x <= maxX ; x += dx ){
106  for( float y = minY ; y <= maxY ; y += dy ){
107  for( float z = minZ ; z <= maxZ ; z += dz ){
108 
109  double posV[3] = { x, y, z } ;
110  double bfieldV[3] ;
111  description.field().magneticField( posV , bfieldV ) ;
112 
113  printf(" %+15.8e %+15.8e %+15.8e %+15.8e %+15.8e %+15.8e \n",
114  posV[0]/dd4hep::cm, posV[1]/dd4hep::cm, posV[2]/dd4hep::cm,
115  bfieldV[0]/dd4hep::tesla , bfieldV[1]/dd4hep::tesla, bfieldV[2]/dd4hep::tesla ) ;
116 
117  }
118  }
119  }
120  printf("#######################################################################################################\n");
121  return 0;
122 }
123 
124 
125 
126 int main(int argc, char** argv ){
127  try {
128  return invoke_dump_B_field(argc,argv);
129  }
130  catch(const std::exception& e) {
131  std::cout << "Got uncaught exception: " << e.what() << std::endl;
132  }
133  catch (...) {
134  std::cout << "Got UNKNOWN uncaught exception." << std::endl;
135  }
136  return EINVAL;
137 }
138 
139 //=============================================================================
Detector.h
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::detail
DD4hep internal namespace.
Definition: Alignments.h:32
dd4hep::Detector::field
virtual OverlayedField field() const =0
Return handle to the combined electromagentic field description.
dd4hep::Detector::fromCompact
virtual void fromCompact(const std::string &fname, DetectorBuildType type=BUILD_DEFAULT)=0
Deprecated call (use fromXML): Read compact geometry description or alignment file.
std
Definition: Plugins.h:30
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::OverlayedField::magneticField
void magneticField(const Position &pos, double *field) const
Returns the 3 magnetic field components (x, y, z).
Definition: Fields.cpp:140
main
int main(int argc, char **argv)
Definition: dumpBfield.cpp:126
DD4hepUnits.h