CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.cc

Go to the documentation of this file.
00001 #include "RectangularCylindricalMFGrid.h"
00002 #include "binary_ifstream.h"
00003 #include "LinearGridInterpolator3D.h"
00004 #include <iostream>
00005 
00006 using namespace std;
00007 
00008 RectangularCylindricalMFGrid::RectangularCylindricalMFGrid( binary_ifstream& inFile, 
00009                                                             const GloballyPositioned<float>& vol)
00010   : MFGrid3D(vol)
00011 {
00012   // The parameters read from the data files are given in global coordinates.
00013   // In version 85l, local frame has the same orientation of global frame for the reference
00014   // volume, i.e. the r.f. transformation is only a translation.
00015   // There is therefore no need to convert the field values to local coordinates.
00016   // Check this assumption: 
00017   GlobalVector localXDir(frame().toGlobal(LocalVector(1,0,0)));
00018   GlobalVector localYDir(frame().toGlobal(LocalVector(0,1,0)));
00019 
00020   if (localXDir.dot(GlobalVector(1,0,0)) > 0.999999 &&
00021       localYDir.dot(GlobalVector(0,1,0)) > 0.999999) {
00022     // "null" rotation - requires no conversion...
00023   } else {
00024     cout << "ERROR: RectangularCylindricalMFGrid: unexpected orientation: x: " 
00025          << localXDir << " y: " << localYDir << endl;
00026   }
00027 
00028   int n1, n2, n3;
00029   inFile >> n1 >> n2 >> n3;
00030   double xref, yref, zref;
00031   inFile >> xref >> yref >> zref;
00032   double stepx, stepy, stepz;
00033   inFile >> stepx    >> stepy    >> stepz;
00034 
00035   vector<BVector> fieldValues;
00036   float Bx, By, Bz;
00037   int nLines = n1*n2*n3;
00038   fieldValues.reserve(nLines);
00039   for (int iLine=0; iLine<nLines; ++iLine){
00040     inFile >> Bx >> By >> Bz;
00041     fieldValues.push_back(BVector(Bx,By,Bz));
00042   }
00043   // check completeness
00044   string lastEntry;
00045   inFile >> lastEntry;
00046   if (lastEntry != "complete"){
00047     cout << "ERROR during file reading: file is not complete" << endl;
00048   }
00049 
00050   GlobalPoint grefp( GlobalPoint::Cylindrical( xref, yref, zref));
00051   LocalPoint lrefp = frame().toLocal( grefp);
00052 
00053 #ifdef DEBUG_GRID
00054   cout << "Grid reference point in grid system: " << xref << "," << yref << "," << zref << endl;
00055   cout << "Grid reference point in global x,y,z: " << grefp << endl;
00056   cout << "Grid reference point in local x,y,z: " << lrefp << endl;
00057   cout << "steps " << stepx << "," <<  stepy << "," << stepz << endl;
00058 #endif
00059 
00060   Grid1D gridX( lrefp.perp(), lrefp.perp() + stepx*(n1-1), n1);
00061   //Grid1D gridY( lrefp.phi(), lrefp.phi() + stepy*(n2-1), n2); // wrong: gives zero
00062   Grid1D gridY( yref, yref + stepy*(n2-1), n2);
00063   Grid1D gridZ( lrefp.z(), lrefp.z() + stepz*(n3-1), n3);
00064 
00065   grid_ = GridType( gridX, gridY, gridZ, fieldValues);
00066   
00067 }
00068 
00069 void RectangularCylindricalMFGrid::dump() const
00070 {
00071   cout << endl << "Dump of RectangularCylindricalMFGrid" << endl;
00072   cout << "Number of points from Grid1D " 
00073        << grid_.grida().nodes() << " " << grid_.gridb().nodes() << " " << grid_.gridc().nodes() << endl;
00074 
00075   cout << "Reference Point from Grid1D " 
00076        << grid_.grida().lower() << " " << grid_.gridb().lower() << " " << grid_.gridc().lower() << endl;
00077 
00078   cout << "Basic Distance from Grid1D "
00079        << grid_.grida().step() << " " << grid_.gridb().step() << " " << grid_.gridc().step() << endl;
00080 
00081 
00082   cout << "Dumping " << grid_.data().size() << " field values " << endl;
00083   // grid_.dump();
00084 }
00085 
00086 MFGrid::LocalVector RectangularCylindricalMFGrid::uncheckedValueInTesla( const LocalPoint& p) const
00087 {
00088   const float minimalSignificantR = 1e-6; // [cm], points below this radius are treated as zero radius
00089   float R = p.perp();
00090   if (R < minimalSignificantR) {
00091     if (grid_.grida().lower() < minimalSignificantR) {
00092       int k = grid_.gridc().index(p.z());
00093       double u = (p.z() - grid_.gridc().node(k)) / grid_.gridc().step();
00094       LocalVector result((1-u)*grid_(0,  0,  k) + u*grid_(0,  0,  k+1)); 
00095       return result;
00096     }
00097   }
00098   
00099   LinearGridInterpolator3D interpol( grid_);
00100   // FIXME: "OLD" convention of phi.
00101   // GridType::ValueType value = interpol( R, Geom::pi() - p.phi(), p.z());
00102   GridType::ReturnType value = interpol.interpolate( R, p.phi(), p.z());
00103   return LocalVector(value);
00104 }
00105 
00106 void RectangularCylindricalMFGrid::toGridFrame( const LocalPoint& p, 
00107                                               double& a, double& b, double& c) const
00108 {
00109   a = p.perp();
00110   // FIXME: "OLD" convention of phi.
00111   //  b = Geom::pi() - p.phi();
00112   b = p.phi();
00113   c = p.z();
00114 }
00115  
00116 MFGrid::LocalPoint RectangularCylindricalMFGrid::fromGridFrame( double a, double b, double c) const
00117 {
00118 
00119   // FIXME: "OLD" convention of phi.
00120   //  return LocalPoint( LocalPoint::Cylindrical(a, Geom::pi() - b, c));
00121   return LocalPoint( LocalPoint::Cylindrical(a, b, c));
00122 }