Skip to content

Commit

Permalink
Refs #8432. Using SaveIsawDetCal from SCDCalibratePanel.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Nov 22, 2013
1 parent 265bdb1 commit d18faaa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ namespace Crystal
boost::shared_ptr<Geometry::ParameterMap> pmap,
boost::shared_ptr<const Geometry::ParameterMap> pmapSv);

void SaveIsawDetCal( boost::shared_ptr<const Geometry::Instrument> &NewInstrument,
void SaveIsawDetCal(boost::shared_ptr<const Geometry::Instrument> &instrument,
std::set<std::string> &AllBankName,
double T0,std::string FileName);
double T0, std::string filename);

void LoadISawDetCal(
boost::shared_ptr<const Geometry::Instrument> &instrument,
Expand Down
134 changes: 30 additions & 104 deletions Code/Mantid/Framework/Crystal/src/SCDCalibratePanels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,20 @@ namespace Mantid

DECLARE_ALGORITHM(SCDCalibratePanels)

namespace{
namespace
{
const double maxDetHWScale =1.15;
const double minDetHWScale = .85;
}

SCDCalibratePanels::SCDCalibratePanels():API::Algorithm()

{

// g_log.setLevel(7);
}


SCDCalibratePanels::~SCDCalibratePanels()
{

}

const std::string SCDCalibratePanels::name() const
Expand Down Expand Up @@ -396,7 +394,8 @@ namespace Mantid
Groups.push_back( Group0 );

}
}else
}
else
{
g_log.error("No mode " + Grouping + " defined yet");
throw invalid_argument("No mode " + Grouping + " defined yet");
Expand Down Expand Up @@ -426,17 +425,13 @@ namespace Mantid
if( preprocessCommand == "A)No PreProcessing")
return instrument;

bool xml = false;
if( preprocessCommand == "C)Apply a LoadParameter.xml type file")
xml = true;

bool xml =
( preprocessCommand == "C)Apply a LoadParameter.xml type file");


boost::shared_ptr<const ParameterMap> pmap0 = instrument->getParameterMap();
boost::shared_ptr<ParameterMap> pmap1( new ParameterMap());



for( vector< string >::iterator vit = AllBankNames.begin();
vit != AllBankNames.end(); ++vit )
{
Expand Down Expand Up @@ -550,10 +545,10 @@ namespace Mantid
Yoffset0 = change.Y();
Zoffset0 = change.Z();

double scalexI = 1;
double scalexPre = 1;
double scaleyI = 1;
double scaleyPre = 1;
double scalexI = 1.;
double scalexPre = 1.;
double scaleyI = 1.;
double scaleyPre = 1.;

vector< double > ScalexI = pmap->getDouble( bankName, "scalex");
vector< double > ScalexPre = pmapPre->getDouble( bankName, "scalex");
Expand Down Expand Up @@ -794,14 +789,10 @@ namespace Mantid
throw invalid_argument("No Rectangular detector bank " + banksVec[ 0 ] + " in instrument");
}




// if( it1 == (*itv).begin())
CalcInitParams( bank_rect, instrument, PreCalibinstrument, detWidthScale0
,detHeightScale0, Xoffset0, Yoffset0, Zoffset0, Xrot0, Yrot0, Zrot0);


// --- set Function property ----------------------

oss << "," << Gprefix<<"detWidthScale=" << fixed << detWidthScale0 << ","
Expand All @@ -827,11 +818,8 @@ namespace Mantid
if( i == 0)
{
first = true;

}



if( !use_PanelWidth)
{
if( !first)
Expand All @@ -842,8 +830,6 @@ namespace Mantid
oss1 <<Gprefix<<"detWidthScale=" << fixed << detWidthScale0;
}



if( !use_PanelHeight)
{
if( !first)
Expand All @@ -868,8 +854,6 @@ namespace Mantid
Gprefix<<"Zoffset=" << Zoffset0;
}



if( ! use_PanelOrientation )
{
if( !first)
Expand All @@ -882,10 +866,6 @@ namespace Mantid
<<Gprefix<<"Zrot=" << Zrot0;
}





//--------------- set Constraints Property -------------------------------

ostringstream oss2 ( ostringstream::out);
Expand All @@ -910,7 +890,7 @@ namespace Mantid
}//for vector< string > in Groups


//Constraints for sample offsets
//Constraints for sample offsets
maxXYOffset = getProperty("MaxSamplePositionChangeMeters");

if( getProperty("AllowSampleShift"))
Expand Down Expand Up @@ -1435,80 +1415,26 @@ namespace Mantid
* @param T0 -The time offset
* @param FileName -The name of the DetCal file to save the results to
*/
void SCDCalibratePanels::SaveIsawDetCal( boost::shared_ptr<const Instrument> &NewInstrument,
set<string> &AllBankName,double T0,string FileName)
void SCDCalibratePanels::SaveIsawDetCal( boost::shared_ptr<const Instrument> &instrument,
set<string> &AllBankName,double T0,string filename)
{
double beamline_norm,L0=0.0;
V3D beamline, samplePos;
filebuf fb;
fb.open(FileName.c_str(), ios::out);
ostream os(&fb);
os << "# NEW CALIBRATION FILE FORMAT (in NeXus/SNS coordinates):" << endl;
os << "# Lengths are in centimeters." << endl;
os << "# Base and up give directions of unit vectors for a local " << endl;
os << "# x,y coordinate system on the face of the detector." << endl;
os << "#" << endl;
os << "#" << endl;
os << "#" << DateAndTime::getCurrentTime().toISO8601String() << endl;
os << "6 L1 T0_SHIFT" << endl;


NewInstrument->getInstrumentParameters(L0, beamline, beamline_norm, samplePos);
os << "7 " << setw(10);
os << setprecision(4) << fixed << (L0 * 100);
os << setw(12) << setprecision(3) << fixed;
// Time offset of 0.00 for now
os << setw(12) << setprecision(4) << T0 << endl;

os
<< "4 DETNUM NROWS NCOLS WIDTH HEIGHT DEPTH DETD CenterX CenterY CenterZ BaseX BaseY BaseZ UpX UpY UpZ"
<< endl;
for (set<string>::iterator it = AllBankName.begin(); it != AllBankName.end(); ++it)
{
string bankName = *it;
string::reverse_iterator rit = bankName.rbegin();
string S;
while (isdigit(*rit))
{
S = *rit +S;
++rit;
}
int bankNum = boost::lexical_cast<int>(S);

boost::shared_ptr<const RectangularDetector> det = boost::dynamic_pointer_cast<
const RectangularDetector>(NewInstrument->getComponentByName(bankName));
if (!det)
continue;
V3D center = det->getPos();
double detd = (center - NewInstrument->getSample()->getPos()).norm();
// Base unit vector (along the horizontal, X axis)
V3D base = det->getAtXY(det->xpixels() - 1, 0)->getPos() - det->getAtXY(0, 0)->getPos();
base.normalize();
// Up unit vector (along the vertical, Y axis)
V3D up = det->getAtXY(0, det->ypixels() - 1)->getPos() - det->getAtXY(0, 0)->getPos();
up.normalize();

// Write the line
os << "5 " << setw(6) << right << bankNum << " " << setw(6) << right
<< det->xpixels() << " " << setw(6) << right << det->ypixels() << " "
<< setw(7) << right << fixed << setprecision(4) << 100.0 * det->xsize()
<< " " << setw(7) << right << fixed << setprecision(4) << 100.0
* det->ysize() << " " << " 0.2000 " << setw(6) << right << fixed
<< setprecision(2) << 100.0 * detd << " " << setw(9) << right << fixed
<< setprecision(4) << 100.0 * center.X() << " " << setw(9) << right
<< fixed << setprecision(4) << 100.0 * center.Y() << " " << setw(9)
<< right << fixed << setprecision(4) << 100.0 * center.Z() << " "
<< setw(8) << right << fixed << setprecision(5) << base.X() << " "
<< setw(8) << right << fixed << setprecision(5) << base.Y() << " "
<< setw(8) << right << fixed << setprecision(5) << base.Z() << " "
<< setw(8) << right << fixed << setprecision(5) << up.X() << " "
<< setw(8) << right << fixed << setprecision(5) << up.Y() << " "
<< setw(8) << right << fixed << setprecision(5) << up.Z() << " "
<< endl;

}

fb.close();
// create a workspace to pass to SaveIsawDetCal
const size_t number_spectra = instrument->getNumberDetectors();
DataObjects::Workspace2D_sptr wksp =
boost::dynamic_pointer_cast<DataObjects::Workspace2D>(WorkspaceFactory::Instance().create("Workspace2D",number_spectra,2,1));
wksp->setInstrument(instrument);
wksp->rebuildSpectraMapping( true /* include monitors */);

// convert the bank names into a vector
std::vector<string> banknames(AllBankName.begin(), AllBankName.end());

// call SaveIsawDetCal
API::IAlgorithm_sptr alg = createChildAlgorithm("SaveIsawDetCal");
alg->setProperty("InputWorkspace", wksp);
alg->setProperty("Filename", filename);
alg->setProperty("TimeOffset", T0);
alg->setProperty("BankNames", banknames);
alg->executeAsChildAlg();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class SCDCalibratePanelsTest : public CxxTest::TestSuite
alg->setProperty("Grouping","26");
alg->setPropertyValue("ResultWorkspace","Result");
alg->setPropertyValue("QErrorWorkspace","QErrorResult");
alg->setPropertyValue("DetCalFilename", "/tmp/topaz.detcal"); // deleteme
TS_ASSERT(alg->execute());

// verify the results
Expand Down
93 changes: 53 additions & 40 deletions Code/Mantid/Framework/DataHandling/src/SaveIsawDetCal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Other names will fail or create an invalid .DetCal file.
#include "MantidDataHandling/SaveIsawDetCal.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/System.h"
#include <fstream>
#include "MantidAPI/Workspace.h"
Expand All @@ -22,6 +23,7 @@ Other names will fail or create an invalid .DetCal file.
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using std::string;

namespace Mantid
{
Expand Down Expand Up @@ -61,7 +63,8 @@ namespace DataHandling
*/
void SaveIsawDetCal::init()
{
declareProperty(new WorkspaceProperty<Workspace>("InputWorkspace","",Direction::Input), "An input workspace.");
declareProperty(new WorkspaceProperty<Workspace>("InputWorkspace","",Direction::Input),
"An input workspace.");

std::vector<std::string> exts;
exts.push_back(".DetCal");
Expand All @@ -70,6 +73,8 @@ namespace DataHandling
"Path to an ISAW-style .detcal file to save.");

declareProperty( "TimeOffset",0.0,"Offsets to be applied to times");
declareProperty(new ArrayProperty<string>("BankNames", Direction::Input),
"Optional: Only select the specified banks");
}

//----------------------------------------------------------------------------------------------
Expand All @@ -87,10 +92,15 @@ namespace DataHandling
std::ofstream out;
out.open( filename.c_str());

std::vector<std::string> bankNames = getProperty("BankNames");

Instrument_const_sptr inst = ws->getInstrument();
if (!inst) throw std::runtime_error("No instrument in the Workspace. Cannot save DetCal file.");

double l1; V3D beamline; double beamline_norm; V3D samplePos;
double l1;
V3D beamline;
double beamline_norm;
V3D samplePos;
inst->getInstrumentParameters(l1, beamline, beamline_norm, samplePos);
out << "# NEW CALIBRATION FILE FORMAT (in NeXus/SNS coordinates):" << std::endl;
out << "# Lengths are in centimeters." << std::endl;
Expand Down Expand Up @@ -118,44 +128,47 @@ namespace DataHandling
{
// Retrieve it
RectangularDetector_const_sptr det = boost::dynamic_pointer_cast<const RectangularDetector>(comps[i]);
if (det)
{
std::string name = det->getName();
if (name.size() < 5) continue;
std::string bank = name.substr(4,name.size()-4);

// Center of the detector
V3D center = det->getPos();
// Distance to center of detector
double detd = (center - inst->getSample()->getPos()).norm();

// Base unit vector (along the horizontal, X axis)
V3D base = det->getAtXY(det->xpixels()-1,0)->getPos() - det->getAtXY(0,0)->getPos();
base.normalize();
// Up unit vector (along the vertical, Y axis)
V3D up = det->getAtXY(0,det->ypixels()-1)->getPos() - det->getAtXY(0,0)->getPos();
up.normalize();

// Write the line
out << "5 "
<< std::setw(6) << std::right << bank << " "
<< std::setw(6) << std::right << det->xpixels() << " "
<< std::setw(6) << std::right << det->ypixels() << " "
<< std::setw(7) << std::right << std::fixed << std::setprecision(4) << 100.0*det->xsize() << " "
<< std::setw(7) << std::right << std::fixed << std::setprecision(4) << 100.0*det->ysize() << " "
<< " 0.2000 "
<< std::setw(6) << std::right << std::fixed << std::setprecision(2) << 100.0*detd << " "
<< std::setw(9) << std::right << std::fixed << std::setprecision(4) << 100.0*center.X() << " "
<< std::setw(9) << std::right << std::fixed << std::setprecision(4) << 100.0*center.Y() << " "
<< std::setw(9) << std::right << std::fixed << std::setprecision(4) << 100.0*center.Z() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << base.X() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << base.Y() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << base.Z() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << up.X() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << up.Y() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << up.Z() << " "
<< std::endl;
}
if (!det) continue;

// determine the name and if it should be written out to the file
std::string name = det->getName();
if (name.size() < 5) continue;
if ((!bankNames.empty())
&& (std::find(bankNames.begin(), bankNames.end(), name) == bankNames.end()))
continue;
std::string bank = name.substr(4,name.size()-4);

// Center of the detector
V3D center = det->getPos();
// Distance to center of detector
double detd = (center - inst->getSample()->getPos()).norm();

// Base unit vector (along the horizontal, X axis)
V3D base = det->getAtXY(det->xpixels()-1,0)->getPos() - det->getAtXY(0,0)->getPos();
base.normalize();
// Up unit vector (along the vertical, Y axis)
V3D up = det->getAtXY(0,det->ypixels()-1)->getPos() - det->getAtXY(0,0)->getPos();
up.normalize();

// Write the line
out << "5 "
<< std::setw(6) << std::right << bank << " "
<< std::setw(6) << std::right << det->xpixels() << " "
<< std::setw(6) << std::right << det->ypixels() << " "
<< std::setw(7) << std::right << std::fixed << std::setprecision(4) << 100.0*det->xsize() << " "
<< std::setw(7) << std::right << std::fixed << std::setprecision(4) << 100.0*det->ysize() << " "
<< " 0.2000 "
<< std::setw(6) << std::right << std::fixed << std::setprecision(2) << 100.0*detd << " "
<< std::setw(9) << std::right << std::fixed << std::setprecision(4) << 100.0*center.X() << " "
<< std::setw(9) << std::right << std::fixed << std::setprecision(4) << 100.0*center.Y() << " "
<< std::setw(9) << std::right << std::fixed << std::setprecision(4) << 100.0*center.Z() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << base.X() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << base.Y() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << base.Z() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << up.X() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << up.Y() << " "
<< std::setw(8) << std::right << std::fixed << std::setprecision(5) << up.Z() << " "
<< std::endl;
}

out.close();
Expand Down

0 comments on commit d18faaa

Please sign in to comment.