00001
00002
00003 #include "Riostream.h"
00004
00005 #include "GaudiKernel/AlgFactory.h"
00006 #include "GaudiKernel/IMagneticFieldSvc.h"
00007 #include "GaudiKernel/IChronoStatSvc.h"
00008 #include "GaudiKernel/SystemOfUnits.h"
00009 #include "GaudiKernel/RndmGenerators.h"
00010
00011
00012 #include "Kernel/IBIntegrator.h"
00013
00014
00015 #include "GaudiKernel/Vector3DTypes.h"
00016 #include "GaudiKernel/Point3DTypes.h"
00017
00018
00019 #include "MagFieldReader.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 DECLARE_ALGORITHM_FACTORY( MagFieldReader );
00032
00033
00034
00035
00036 MagFieldReader::MagFieldReader( const std::string& name,
00037 ISvcLocator* pSvcLocator)
00038 : GaudiTupleAlg ( name , pSvcLocator )
00039 , m_pIMF(0)
00040 {
00041 declareProperty("Zmin", m_zMin = -500.0*Gaudi::Units::mm);
00042 declareProperty("Zmax", m_zMax = 14000.0*Gaudi::Units::mm);
00043 declareProperty("Step", m_step = 100.0*Gaudi::Units::mm);
00044 declareProperty("Xmin", m_xMin = 0.0*Gaudi::Units::mm);
00045 declareProperty("Xmax", m_xMax = 4000.0*Gaudi::Units::mm);
00046 declareProperty("Ymin", m_yMin = 0.0*Gaudi::Units::mm);
00047 declareProperty("Ymax", m_yMax = 4000.0*Gaudi::Units::mm);
00048 declareProperty("FieldSvcName",m_FieldServiceName="MagneticFieldSvc");
00049 declareProperty("TestFieldInt",m_testbdl=false);
00050 declareProperty("NInt",m_nInt=1000);
00051
00052 }
00053
00054
00055
00056
00057 StatusCode MagFieldReader::initialize() {
00058
00059 StatusCode sc = GaudiTupleAlg::initialize();
00060 if ( sc.isFailure() ) return sc;
00061
00062 debug() << "FieldReader intialize() has been called" << endmsg;
00063
00064 m_pIMF = svc<IMagneticFieldSvc>( m_FieldServiceName, true );
00065
00066
00067 info() << "MagFieldReader initialized with service ==> " << m_FieldServiceName << endmsg;
00068 return StatusCode::SUCCESS;
00069 };
00070
00071
00072
00073
00074 StatusCode MagFieldReader::execute() {
00075
00076 if (m_testbdl) TestBdl();
00077
00078
00079
00080 debug() << "m_pIMF = " << m_pIMF << endreq;
00081
00082 Tuple nt1 = nTuple( 10, "Field", CLID_ColumnWiseTuple );
00083
00084 Gaudi::XYZVector B(0.0,0.0,0.0);
00085
00086
00087
00088 for ( double z = m_zMin; z <= m_zMax; z += m_step ) {
00089 for( double y = m_yMin; y <= m_yMax; y += m_step ) {
00090 for( double x = m_xMin; x <= m_xMax; x += m_step ) {
00091 Gaudi::XYZPoint P( x, y, z );
00092
00093
00094 m_pIMF->fieldVector( P, B );
00095
00096
00097
00098
00099
00100 nt1->column( "x", P.x()/Gaudi::Units::cm );
00101 nt1->column( "y", P.y()/Gaudi::Units::cm );
00102 nt1->column( "z", P.z()/Gaudi::Units::cm );
00103 nt1->column( "Bx", B.x()/Gaudi::Units::tesla );
00104 nt1->column( "By", B.y()/Gaudi::Units::tesla );
00105 nt1->column( "Bz", B.z()/Gaudi::Units::tesla );
00106
00107 nt1->write();
00108 }
00109 }
00110
00111 Gaudi::XYZPoint P0( 0.0, 0.0, z);
00112 Gaudi::XYZPoint P02( 0.0, 0.0, z);
00113 m_pIMF->fieldVector( P0, B );
00114
00115
00116 debug() << "Magnetic Field at ("
00117 << P0.x() << ", " << P0.y() << ", " << P0.z() << " ) = "
00118 << (B.x())/Gaudi::Units::tesla << ", "
00119 << (B.y())/Gaudi::Units::tesla << ", "
00120 << (B.z())/Gaudi::Units::tesla << " Tesla "
00121 << endmsg;
00122
00123
00124 }
00125
00126
00127
00128
00129 return StatusCode::SUCCESS;
00130 }
00131
00132
00133 StatusCode MagFieldReader::finalize() {
00134
00135 StatusCode sc = GaudiTupleAlg::finalize();
00136 if ( sc.isFailure() ) return sc;
00137 if ( sc.isSuccess() )
00138 info() << "Service finalized successfully" << endmsg;
00139 return StatusCode::SUCCESS;
00140 };
00141
00142
00143 void MagFieldReader::TestBdl()
00144 {
00145
00146 Tuple nt2 = nTuple( 20, "Field Integral", CLID_ColumnWiseTuple );
00147
00148 IBIntegrator* bIntegrator = tool<IBIntegrator>("BIntegrator");
00149
00150 Gaudi::XYZPoint start(0,0,0);
00151 Gaudi::XYZPoint stop(0,0,9000/Gaudi::Units::mm);
00152 double sigtx(0.3);
00153 double sigty(0.25);
00154 double zC;
00155 Gaudi::XYZVector bdl;
00156
00157
00158 Rndm::Numbers gausstx(randSvc(),Rndm::Gauss(0.,sigtx/2));
00159 Rndm::Numbers gaussty(randSvc(),Rndm::Gauss(0.,sigty/2));
00160
00161 for (int i=0;i<m_nInt;i++) {
00162 double tx = gausstx();
00163 double ty = gaussty();
00164 if (fabs(tx) < sigtx && fabs(ty) < sigty) {
00165
00166 bIntegrator->calculateBdlAndCenter(start, stop, tx, ty, zC, bdl);
00167
00168 nt2->column( "tx", tx);
00169 nt2->column( "ty", ty);
00170 nt2->column( "Bdlx", bdl.x());
00171 nt2->column( "Bdly", bdl.y());
00172 nt2->column( "Bdlz", bdl.z());
00173 nt2->column( "zCenter", zC);
00174 nt2->write();
00175 }
00176 }
00177
00178 releaseTool(bIntegrator).ignore();
00179
00180 }