/** * Set the absolute detector position of a detector * @param instrument :: The instrument that contains the defined detector * @param detID :: Detector ID * @param pos :: new position of Dectector * @param sameParent :: true if detector has same parent as previous detector set here. */ void ApplyCalibration::setDetectorPosition(const Geometry::Instrument_const_sptr & instrument, int detID, V3D pos, bool /*sameParent*/ ) { Geometry::IDetector_const_sptr det = instrument->getDetector(detID); // Then find the corresponding relative position boost::shared_ptr<const Geometry::IComponent> parent = det->getParent(); if (parent) { pos -= parent->getPos(); Quat rot = parent->getRelativeRot(); rot.inverse(); rot.rotate(pos); } boost::shared_ptr<const Geometry::IComponent>grandparent = parent->getParent(); if (grandparent) { Quat rot = grandparent->getRelativeRot(); rot.inverse(); rot.rotate(pos); boost::shared_ptr<const Geometry::IComponent>greatgrandparent = grandparent->getParent(); if (greatgrandparent) { Quat rot2 = greatgrandparent->getRelativeRot(); rot2.inverse(); rot2.rotate(pos); } } // Add a parameter for the new position m_pmap->addV3D(det.get(), "pos", pos); }
/** This function will check how to group spectra when calculating median * * */ std::vector<std::vector<size_t> > DetectorDiagnostic::makeMap(API::MatrixWorkspace_sptr countsWS) { std::multimap<Mantid::Geometry::ComponentID,size_t> mymap; Geometry::Instrument_const_sptr instrument = countsWS->getInstrument(); if (m_parents==0) { return makeInstrumentMap(countsWS); } if (!instrument) { g_log.warning("Workspace has no instrument. LevelsUP is ignored"); return makeInstrumentMap(countsWS); } //check if not grouped. If grouped, it will throw if ( countsWS->hasGroupedDetectors() ) { throw std::runtime_error("Median detector test: not able to create detector to spectra map. Try with LevelUp=0."); } for(size_t i=0;i < countsWS->getNumberHistograms();i++) { detid_t d=(*((countsWS->getSpectrum(i))->getDetectorIDs().begin())); std::vector<boost::shared_ptr<const Mantid::Geometry::IComponent> > anc=instrument->getDetector(d)->getAncestors(); //std::vector<boost::shared_ptr<const IComponent> > anc=(*(countsWS->getSpectrum(i)->getDetectorIDs().begin()))->getAncestors(); if (anc.size()<static_cast<size_t>(m_parents)) { g_log.warning("Too many levels up. Will ignore LevelsUp"); m_parents=0; return makeInstrumentMap(countsWS); } mymap.insert(std::pair<Mantid::Geometry::ComponentID,size_t>(anc[m_parents-1]->getComponentID(),i)); } std::vector<std::vector<size_t> > speclist; std::vector<size_t> speclistsingle; std::multimap<Mantid::Geometry::ComponentID,size_t>::iterator m_it, s_it; for (m_it = mymap.begin(); m_it != mymap.end(); m_it = s_it) { Mantid::Geometry::ComponentID theKey = (*m_it).first; std::pair<std::multimap<Mantid::Geometry::ComponentID,size_t>::iterator,std::multimap<Mantid::Geometry::ComponentID,size_t>::iterator> keyRange = mymap.equal_range(theKey); // Iterate over all map elements with key == theKey speclistsingle.clear(); for (s_it = keyRange.first; s_it != keyRange.second; ++s_it) { speclistsingle.push_back( (*s_it).second ); } speclist.push_back(speclistsingle); } return speclist; }
/** * Set the absolute detector position of a detector * @param instrument :: The instrument that contains the defined detector * @param detID :: Detector ID * @param pos :: new position of Dectector * @param sameParent :: true if detector has same parent as previous detector set here. */ void ApplyCalibration::setDetectorPosition(const Geometry::Instrument_const_sptr & instrument, int detID, V3D pos, bool /*sameParent*/ ) { IComponent_const_sptr det =instrument->getDetector(detID); ; // Do the move using namespace Geometry::ComponentHelper; TransformType positionType = Absolute; // TransformType positionType = Relative; Geometry::ComponentHelper::moveComponent(*det, *m_pmap, pos, positionType); }
/* * Define edges for each instrument by masking. For CORELLI, tubes 1 and 16, and *pixels 0 and 255. * Get Q in the lab frame for every peak, call it C * For every point on the edge, the trajectory in reciprocal space is a straight *line, going through O=V3D(0,0,0). * Calculate a point at a fixed momentum, say k=1. Q in the lab frame *E=V3D(-k*sin(tt)*cos(ph),-k*sin(tt)*sin(ph),k-k*cos(ph)). * Normalize E to 1: E=E*(1./E.norm()) * * @param inst: instrument */ void IntegrateEllipsoids::calculateE1(Geometry::Instrument_const_sptr inst) { std::vector<detid_t> detectorIDs = inst->getDetectorIDs(); for (auto &detectorID : detectorIDs) { Mantid::Geometry::IDetector_const_sptr det = inst->getDetector(detectorID); if (det->isMonitor()) continue; // skip monitor if (!det->isMasked()) continue; // edge is masked so don't check if not masked double tt1 = det->getTwoTheta(V3D(0, 0, 0), V3D(0, 0, 1)); // two theta double ph1 = det->getPhi(); // phi V3D E1 = V3D(-std::sin(tt1) * std::cos(ph1), -std::sin(tt1) * std::sin(ph1), 1. - std::cos(tt1)); // end of trajectory E1 = E1 * (1. / E1.norm()); // normalize E1Vec.push_back(E1); } }
/** * Set the detector positions given the r,theta and phi. * @param detID :: A vector of detector IDs * @param l2 :: A vector of l2 distances * @param theta :: A vector of theta distances * @param phi :: A vector of phi values */ void UpdateInstrumentFromFile::setDetectorPositions(const std::vector<int32_t> & detID, const std::vector<float> & l2, const std::vector<float> & theta, const std::vector<float> & phi) { Geometry::Instrument_const_sptr inst = m_workspace->getInstrument(); const int numDetector = static_cast<int>(detID.size()); g_log.information() << "Setting new positions for " << numDetector << " detectors\n"; for (int i = 0; i < numDetector; ++i) { try { Geometry::IDetector_const_sptr det = inst->getDetector(detID[i]); setDetectorPosition(det, l2[i], theta[i], phi[i]); } catch (Kernel::Exception::NotFoundError&) { continue; } progress(static_cast<double>(i)/numDetector,"Updating Detector Positions from File"); } }
/** * Updates from a more generic ascii file * @param filename :: The input filename */ void UpdateInstrumentFromFile::updateFromAscii(const std::string & filename) { AsciiFileHeader header; const bool isSpectrum = parseAsciiHeader(header); Geometry::Instrument_const_sptr inst = m_workspace->getInstrument(); // Throws for multiple detectors const spec2index_map specToIndex(m_workspace->getSpectrumToWorkspaceIndexMap()); std::ifstream datfile(filename.c_str(), std::ios_base::in); const int skipNLines = getProperty("SkipFirstNLines"); std::string line; int lineCount(0); while(lineCount < skipNLines) { std::getline(datfile,line); ++lineCount; } std::vector<double> colValues(header.colCount - 1, 0.0); while(std::getline(datfile,line)) { boost::trim(line); std::istringstream is(line); // Column 0 should be ID/spectrum number int32_t detOrSpec(-1000); is >> detOrSpec; // If first thing read is not a number then skip the line if(is.fail()) { g_log.debug() << "Skipping \"" << line << "\". Cannot interpret as list of numbers.\n"; continue; } Geometry::IDetector_const_sptr det; try { if(isSpectrum) { auto it = specToIndex.find(detOrSpec); if(it != specToIndex.end()) { const size_t wsIndex = it->second; det = m_workspace->getDetector(wsIndex); } else { g_log.debug() << "Skipping \"" << line << "\". Spectrum is not in workspace.\n"; continue; } } else { det = inst->getDetector(detOrSpec); } } catch(Kernel::Exception::NotFoundError&) { g_log.debug() << "Skipping \"" << line << "\". Spectrum in workspace but cannot find associated detector.\n"; continue; } // Special cases for detector r,t,p. Everything else is // attached as an detector parameter double R(0.0),theta(0.0), phi(0.0); for(size_t i = 1; i < header.colCount; ++i) { double value(0.0); is >> value; if(i < header.colCount - 1 && is.eof()) { //If stringstream is at EOF & we are not at the last column then // there aren't enought columns in the file throw std::runtime_error("UpdateInstrumentFromFile::updateFromAscii - " "File contains fewer than expected number of columns, check AsciiHeader property."); } if(i == header.rColIdx) R = value; else if(i == header.thetaColIdx) theta = value; else if(i == header.phiColIdx) phi = value; else if(header.detParCols.count(i) == 1) { Geometry::ParameterMap & pmap = m_workspace->instrumentParameters(); pmap.addDouble(det->getComponentID(), header.colToName[i],value); } } // Check stream state. stringstream::EOF should have been reached, if not then there is still more to // read and the file has more columns than the header indicated if(!is.eof()) { throw std::runtime_error("UpdateInstrumentFromFile::updateFromAscii - " "File contains more than expected number of columns, check AsciiHeader property."); } // If not supplied use current values double r,t,p; det->getPos().getSpherical(r,t,p); if(header.rColIdx == 0) R = r; if(header.thetaColIdx == 0) theta = t; if(header.phiColIdx == 0) phi = p; setDetectorPosition(det, static_cast<float>(R), static_cast<float>(theta), static_cast<float>(phi)); } }