// MAUS WARNING: THIS IS LEGACY CODE. #include "Interface/MagFieldMap.hh" #include "Interface/SplineInterpolator.hh" #include "Utils/Exception.hh" #include "Interface/Squeak.hh" #include "Interface/STLUtils.hh" #include #include #include //constructor MagFieldMap::MagFieldMap(const std::string& mapFile, const std::string& fileType, std::string interpolation) : myFieldCalculation("interpolation"), myFileName(mapFile), zOffset(0), myInterpolator(NULL) { ReadMap(mapFile, fileType, interpolation); } MagFieldMap::MagFieldMap(Interpolator3dSolenoidalTo3d* Interpolator) : myFieldCalculation("interpolation"), myFileName(""), zOffset(0), myInterpolator(Interpolator), numberOfRCoords(Interpolator->GetGrid()->xSize()), numberOfZCoords(Interpolator->GetGrid()->ySize()), myZMin( Interpolator->GetGrid()->y(1) ), myZMax( Interpolator->GetGrid()->y(numberOfZCoords) ), myRMin( Interpolator->GetGrid()->x(1) ), myRMax( Interpolator->GetGrid()->x(numberOfRCoords) ) { } MagFieldMap::MagFieldMap(const MagFieldMap & rhs) : myFieldCalculation(rhs.myFieldCalculation), mygridtype(rhs.mygridtype), myFileName(rhs.myFileName), zOffset(rhs.zOffset), myInterpolator(rhs.myInterpolator),myZMin(rhs.myZMin), myZMax(rhs.myZMax), myRMin(rhs.myRMin), myRMax(rhs.myRMax) { SetSheetInformation(rhs.sheetInformation); } MagFieldMap::MagFieldMap() : myFieldCalculation("interpolation"), zOffset(0), myInterpolator(NULL) { } // Destructor MagFieldMap::~MagFieldMap() { for(unsigned int i=0; i> dummy >> dummy >> dummy >> mygridtype >> dummy >> dummy >> dummy; fMap >> dummy >> dummy >> dummy >> minZ >> dummy >> dummy >> maxZ >> dummy >> dummy >> dZ; fMap >> dummy >> dummy >> dummy >> minR >> dummy >> dummy >> maxR >> dummy >> dummy >> dR; numberOfRCoords=(int)((maxR-minR)/dR)+1; numberOfZCoords=(int)((maxZ-minZ)/dZ)+1; //awkward way of setting these pointers to 2D arrays //To avoid loss of information at the Grid fringe, our spline is always a little bit bigger than the grid. inputBr = new double*[numberOfRCoords]; inputBz = new double*[numberOfRCoords]; for(int i=0; i> inputBz[j][i] >> inputBr[j][i]; //Set the z length parameters myZMin = minZ; myZMax = maxZ; //Set the r width parameters myRMin = minR; myRMax = maxR; return SplineInterpolator(dR, dZ, minZ, numberOfRCoords, numberOfZCoords, inputBz, inputBr); } SplineInterpolator MagFieldMap::ReadG4BeamlineMap(std::ifstream &fMap) { double **inputBr=0, **inputBz=0; double minZ, minR, dZ, dR; std::string dummy; char cha; getline(fMap, dummy); getline(fMap, dummy); //ME set value of minR minR = 0; //read in header info fMap >> dummy >> cha >> cha >> cha >> minZ >> cha >> cha >> cha >> dZ >> cha >> cha >> cha >> numberOfZCoords >> cha >> cha >> cha >> dR >> cha >> cha >> cha >> numberOfRCoords; getline(fMap, dummy); getline(fMap, dummy); //awkward way of setting these pointers to 2D arrays //To avoid loss of information at the Grid fringe, our spline is always a little bit bigger than the grid. inputBr = new double*[numberOfRCoords]; inputBz = new double*[numberOfRCoords]; for(int i=0; i> dummy >> dummy >> inputBr[j][i] >> inputBz[j][i]; inputBr[j][i]*=1e-3; //convert T to kT inputBz[j][i]*=1e-3; //convert T to kT } //Set the z length parameters myZMin = minZ; myZMax = minZ + dZ * (double)(numberOfZCoords-1); //Set the r width parameters myRMin = minR; myRMax = minR + dR * (double)(numberOfRCoords-1); if(!fMap) throw(MAUS::Exception(MAUS::Exception::recoverable, "There was a problem while reading a fieldmap.", "MagFieldMap::ReadG4BeamlineMap" )); return SplineInterpolator(dR, dZ, minZ, numberOfRCoords, numberOfZCoords, inputBz, inputBr); } SplineInterpolator MagFieldMap::ReadIcoolMap(std::ifstream &fMap) { double **inputBr=0, **inputBz=0; numberOfRCoords=0, numberOfZCoords=0; std::string dummy; //read in header info getline(fMap, dummy); fMap >> numberOfZCoords >> numberOfRCoords; //work out the dR and dZ //awkward way of setting these pointers to 2D arrays inputBr = new double*[numberOfRCoords]; inputBz = new double*[numberOfRCoords]; for(int i=0; i> dummy >> dummy >> inputZ[i] >> inputR[j] >> inputBz[j][i] >> inputBr[j][i]; inputBz[j][i] *= 1e-3; inputBr[j][i] *= 1e-3; } double minZ = GetZOffset(); double dZ = (inputZ[1] - inputZ[0]) * 1.e3; double dR = (inputR[1] - inputR[0]) * 1.e3; //Set the z length parameters myZMin = inputZ[0]*1.e3; myZMax = inputZ[numberOfZCoords-1]*1.e3; //Set the r width parameters myRMin = inputR[0]*1.e3; myRMax = inputR[numberOfRCoords-1]*1.e3; // Squeak::mout(Squeak::debug) << "icool field parameters nZ:" << numberOfZCoords << " dZ: " << dZ << " zMin: " << myZMin << " zMax: " << myZMax << " nR: " // << numberOfRCoords << " dR: " << dR << " rMin: " << myRMin << " rMax: " << myRMax << std::endl; delete [] inputZ; delete [] inputR; return SplineInterpolator(dR, dZ, minZ, numberOfRCoords, numberOfZCoords, inputBz, inputBr); } SplineInterpolator MagFieldMap::ReadGregoireMap(std::ifstream &fMap) { std::string dummy=""; numberOfRCoords=0, numberOfZCoords=0; double dR, r1, r2, dZ, z1, z2, minZ; double ** inputBr, ** inputBz; //skip header getline(fMap, dummy); getline(fMap, dummy); //work out the dR and dZ - not easy! fMap >> dummy >> dummy >> dummy >> z1 >> r1 >> dummy >> dummy; getline(fMap, dummy); z2 = z1; //scan to the point on the grid one dr and one dz away while(z2==z1) { fMap >> dummy >> dummy >> dummy >> z2 >> r2 >> dummy >> dummy; getline(fMap, dummy); } fMap >> dummy >> dummy >> dummy >> z2 >> r2 >> dummy >> dummy; getline(fMap, dummy); //Now extract dr, dz dR = (r2-r1)*10.; // remembering to convert from cm to mm dZ = (z2-z1)*10.; //Work out the extent of the map while(fMap) fMap >> dummy >> dummy >> dummy >> z2 >> r2 >> dummy >> dummy; numberOfRCoords = (int)((r2 - r1 / dR) + 1); numberOfZCoords = (int)((z2 - z1 / dZ) + 1); minZ = GetZOffset(); //awkward way of setting these pointers to 2D arrays inputBr = new double*[numberOfRCoords]; inputBz = new double*[numberOfRCoords]; for(int i=0; i> dummy >> dummy >> dummy >> dummy >> dummy >> inputBr[j][i] >> inputBz[j][i]; getline(fMap, dummy); inputBz[j][i] *= 1e-7; //convert from Gauss to kT inputBr[j][i] *= 1e-7; //convert from Gauss to kT } //Set the z length parameters myZMin = minZ; myZMax = minZ + dZ*(numberOfZCoords-1); //Set the r width parameters myRMin = 0; myRMax = (numberOfRCoords - 1) * dR; if(!fMap) throw(MAUS::Exception(MAUS::Exception::recoverable, "There was a problem while loading a field map", "MagFieldMap::LoadGregoireMap")); return SplineInterpolator(dR, dZ, minZ, numberOfRCoords, numberOfZCoords, inputBz, inputBr); } SplineInterpolator MagFieldMap::ReadPandiraMap(std::ifstream &fMap) { std::string dummy=""; numberOfRCoords=0, numberOfZCoords=0; double rD=0, rOld=1, zD=0, zOld=1, brD=0, bzD=0; std::vector< std::vector > brV, bzV, rV, zV; //skip header while(fMap && dummy!="Index") fMap >> dummy; //work out the dR and dZ - not easy! while(fMap) { fMap >> rD >> zD >> brD >> bzD; if(rOld > rD || zOld > zD) { brV.push_back(std::vector()); bzV.push_back(std::vector()); rV .push_back(std::vector()); zV .push_back(std::vector()); } brV.back().push_back(brD*1e-7); bzV.back().push_back(bzD*1e-7); rV .back().push_back(rD*10.); zV .back().push_back(zD*10.); rOld = rD; zOld = zD; getline(fMap, dummy); } double ** bzA = new double*[bzV[0].size()]; double ** brA = new double*[brV[0].size()]; for(unsigned int i=0; i(&nSheet), sizeof(nSheet)); //floats for backward compatibility float aSheet[9]; // array to store all sheet related data for (int i=0; i(&aSheet), sizeof(aSheet)); sheetInformation.push_back(new double[9]); for(int j=0; j<9; j++) sheetInformation[i][j] = (double)aSheet[j]; } fMap.read(reinterpret_cast(&numberOfRCoords), sizeof(numberOfRCoords)); fMap.read(reinterpret_cast(&numberOfZCoords), sizeof(numberOfZCoords)); numberOfRCoords++; numberOfZCoords++; rCoordinates = new double[numberOfRCoords]; zCoordinates = new double[numberOfZCoords]; inputBr = new double*[numberOfRCoords]; inputBz = new double*[numberOfRCoords]; for(int i=0; i(outArray), sizeof(outArray)); zCoordinates[j] = outArray[1]; inputBr[i][j] = outArray[2]; inputBz[i][j] = outArray[3]; } rCoordinates[i] = outArray[0]; } myZMin = zCoordinates[0]; myRMin = rCoordinates[0]; myZMax = zCoordinates[numberOfZCoords-1]; myRMax = rCoordinates[numberOfRCoords-1]; TwoDGrid* grid = new TwoDGrid(numberOfRCoords, rCoordinates, numberOfZCoords, zCoordinates); myInterpolator = new Interpolator3dSolenoidalTo3d(grid, inputBr, inputBz, algorithm); // myInterpolator does not take ownership of memory so we now need to clean up for(int i=0; i(&numberOfSheets),sizeof(numberOfSheets)); for(int i=0; i(aSheetInformation),sizeof(aSheetInformation)); } numberOfRCoords--; //horrible! Backward compatibility numberOfZCoords--; fout.write(reinterpret_cast(&numberOfRCoords),sizeof(numberOfRCoords)); fout.write(reinterpret_cast(&numberOfZCoords),sizeof(numberOfZCoords)); Mesh* myMesh = myInterpolator->GetMesh(); double position[2] = {0., 0.}; double value[3] = {0., 0., 0.}; Mesh::Iterator itEnd = myMesh->End(); for (Mesh::Iterator it = myMesh->Begin(); it < itEnd; ++it) { it.Position(position); myInterpolator->F(it, value); outArray[0] = position[0]; outArray[1] = position[1]; outArray[2] = value[0]; outArray[3] = value[2]; fout.write(reinterpret_cast(outArray), sizeof(outArray)); } if(!fout) std::cerr << "There was a problem writing the field map " << mapFile << std::endl; fout.close(); } void MagFieldMap::SetSheetInformation(vector someSheetInformation) { for(unsigned int i=0; i inpSheets, std::string mapFile) { std::string dummy; mapFile = STLUtils::ReplaceVariables(std::string(mapFile)).c_str(); std::ifstream fMap; fMap.open(mapFile.c_str(), std::fstream::in | std::fstream::binary); if(!fMap) return false; int nSheet; // number of sheets fMap.read(reinterpret_cast(&nSheet), sizeof(nSheet)); //floats for backward compatibility vector sheetInformation; float aSheet[9]; // array to store all sheet related data for (int i=0; i(&aSheet), sizeof(aSheet)); sheetInformation.push_back(new double[9]); for(int j=0; j<9; j++) sheetInformation[i][j] = (double)aSheet[j]; } if(inpSheets.size()!=sheetInformation.size()) return false; for(unsigned int i=0; i