void MantidMatrix::setup(Mantid::API::MatrixWorkspace_const_sptr ws, int start, int end) { if (!ws) { QMessageBox::critical(0, "WorkspaceMatrixModel error", "2D workspace expected."); m_rows = 0; m_cols = 0; m_startRow = 0; m_endRow = 0; return; } m_workspace = ws; m_workspaceTotalHist = static_cast<int>(ws->getNumberHistograms()); m_startRow = (start < 0 || start >= m_workspaceTotalHist) ? 0 : start; m_endRow = (end < 0 || end >= m_workspaceTotalHist || end < start) ? m_workspaceTotalHist - 1 : end; m_rows = m_endRow - m_startRow + 1; m_cols = static_cast<int>(ws->blocksize()); if (ws->isHistogramData()) m_histogram = true; connect(this, SIGNAL(needsUpdating()), this, SLOT(repaintAll())); m_bk_color = QColor(128, 255, 255); m_matrix_icon = getQPixmap("mantid_matrix_xpm"); m_column_width = 100; }
void MantidWSIndexDialog::generateWsIndexIntervals() { // Get the available interval for each of the workspaces, and then // present the user with interval which is the INTERSECTION of each of // those intervals. QList<QString>::const_iterator it = m_wsNames.constBegin(); // Cycle through the workspaces ... for ( ; it != m_wsNames.constEnd(); ++it ) { Mantid::API::MatrixWorkspace_const_sptr ws = boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve((*it).toStdString())); if ( NULL == ws ) continue; const int endWs = static_cast<int>(ws->getNumberHistograms() - 1);//= static_cast<int> (end->first); Interval interval(0,endWs); // If no interval has been added yet, just add it ... if(it == m_wsNames.constBegin()) m_wsIndexIntervals.addInterval(interval); // ... else set the list as the intersection of what's already there // and what has just been added. else m_wsIndexIntervals.setIntervalList(IntervalList::intersect(m_wsIndexIntervals,interval)); } }
/** * Creates and returns a "mini plot", from the given QwtPlot and QwtPlotCurve objects, as well as the given workspace * and workspace index. * * @param plot :: the QwtPlot object * @param curve :: the QwtPlotCurve object * @param workspace :: the workspace to use * @param wsIndex :: the workspace index * * @returns the resulting QwtPlotCurve object */ QwtPlotCurve* IDATab::plotMiniplot(QwtPlot* plot, QwtPlotCurve* curve, const std::string & workspace, size_t wsIndex) { if ( curve != NULL ) { curve->attach(0); delete curve; curve = 0; } Mantid::API::MatrixWorkspace_const_sptr ws = boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve(workspace)); size_t nhist = ws->getNumberHistograms(); if ( wsIndex >= nhist ) { showInformationBox("Error: Workspace index out of range."); return NULL; } using Mantid::MantidVec; const MantidVec & dataX = ws->readX(wsIndex); const MantidVec & dataY = ws->readY(wsIndex); curve = new QwtPlotCurve(); curve->setData(&dataX[0], &dataY[0], static_cast<int>(ws->blocksize())); curve->attach(plot); plot->replot(); return curve; }
/** * Convert to the output dimensions * @param inputWs : Input Matrix workspace * @return workspace group containing output matrix workspaces of ki and kf */ Mantid::API::MatrixWorkspace_sptr ReflectometryTransform::execute( Mantid::API::MatrixWorkspace_const_sptr inputWs) const { auto ws = boost::make_shared<Mantid::DataObjects::Workspace2D>(); ws->initialize(m_d1NumBins, m_d0NumBins, m_d0NumBins); // Create the output workspace as a distribution // Mapping so that d0 and d1 values calculated can be added to the matrix // workspace at the correct index. const double gradD0 = double(m_d0NumBins) / (m_d0Max - m_d0Min); // The x - axis const double gradD1 = double(m_d1NumBins) / (m_d1Max - m_d1Min); // Actually the y-axis const double cxToIndex = -gradD0 * m_d0Min; const double cyToIndex = -gradD1 * m_d1Min; const double cxToD0 = m_d0Min - (1 / gradD0); const double cyToD1 = m_d1Min - (1 / gradD1); // Create an X - Axis. MantidVec xAxisVec = createXAxis(ws.get(), gradD0, cxToD0, m_d0NumBins, m_d0Label, "1/Angstroms"); // Create a Y (vertical) Axis createVerticalAxis(ws.get(), xAxisVec, gradD1, cyToD1, m_d1NumBins, m_d1Label, "1/Angstroms"); // Loop over all entries in the input workspace and calculate d0 and d1 // for each. auto spectraAxis = inputWs->getAxis(1); for (size_t index = 0; index < inputWs->getNumberHistograms(); ++index) { auto counts = inputWs->readY(index); auto wavelengths = inputWs->readX(index); auto errors = inputWs->readE(index); const size_t nInputBins = wavelengths.size() - 1; const double theta_final = spectraAxis->getValue(index); m_calculator->setThetaFinal(theta_final); // Loop over all bins in spectra for (size_t binIndex = 0; binIndex < nInputBins; ++binIndex) { const double wavelength = 0.5 * (wavelengths[binIndex] + wavelengths[binIndex + 1]); double _d0 = m_calculator->calculateDim0(wavelength); double _d1 = m_calculator->calculateDim1(wavelength); if (_d0 >= m_d0Min && _d0 <= m_d0Max && _d1 >= m_d1Min && _d1 <= m_d1Max) // Check that the calculated ki and kf are in range { const int outIndexX = static_cast<int>((gradD0 * _d0) + cxToIndex); const int outIndexZ = static_cast<int>((gradD1 * _d1) + cyToIndex); ws->dataY(outIndexZ)[outIndexX] += counts[binIndex]; ws->dataE(outIndexZ)[outIndexX] += errors[binIndex]; } } } return ws; }
void StepScan::fillNormalizationCombobox( const Mantid::API::MatrixWorkspace_const_sptr & ws ) { // If there are more than 3 entries in the combobox (nothing, time, proton_charge) then // remove any stale ones while ( m_uiForm.normalization->count() > 3 ) { m_uiForm.normalization->removeItem(m_uiForm.normalization->count()-1); } for ( std::size_t i = 0; i < ws->getNumberHistograms(); ++i ) { const std::string monitorName = ws->getDetector(i)->getName(); m_uiForm.normalization->addItem( QString::fromStdString( monitorName ) ); } }
/** * Find all of the spectra in the workspace that have width data * * @param ws :: The workspace to search */ void JumpFit::findAllWidths(Mantid::API::MatrixWorkspace_const_sptr ws) { m_uiForm.cbWidth->clear(); m_spectraList.clear(); for (size_t i = 0; i < ws->getNumberHistograms(); ++i) { auto axis = dynamic_cast<Mantid::API::TextAxis*>(ws->getAxis(1)); if(!axis) return; std::string title = axis->label(i); //check if the axis labels indicate this spectrum is width data size_t qLinesWidthIndex = title.find(".Width"); size_t convFitWidthIndex = title.find(".FWHM"); bool qLinesWidth = qLinesWidthIndex != std::string::npos; bool convFitWidth = convFitWidthIndex != std::string::npos; //if we get a match, add this spectrum to the combobox if(convFitWidth || qLinesWidth) { std::string cbItemName = ""; size_t substrIndex = 0; if (qLinesWidth) { substrIndex = qLinesWidthIndex; } else if (convFitWidth) { substrIndex = convFitWidthIndex; } cbItemName = title.substr(0, substrIndex); m_spectraList[cbItemName] = static_cast<int>(i); m_uiForm.cbWidth->addItem(QString(cbItemName.c_str())); //display widths f1.f1, f2.f1 and f2.f2 if (m_uiForm.cbWidth->count() == 3) { return; } } } }
/** * Performs centre-point rebinning and produces an MDWorkspace * @param inputWs : The workspace you wish to perform centre-point rebinning on. * @param boxController : controls how the MDWorkspace will be split * @param frame: the md frame for the two MDHistoDimensions * @returns An MDWorkspace based on centre-point rebinning of the inputWS */ Mantid::API::IMDEventWorkspace_sptr ReflectometryTransform::executeMD( Mantid::API::MatrixWorkspace_const_sptr inputWs, BoxController_sptr boxController, Mantid::Geometry::MDFrame_uptr frame) const { auto dim0 = boost::make_shared<MDHistoDimension>( m_d0Label, m_d0ID, *frame, static_cast<Mantid::coord_t>(m_d0Min), static_cast<Mantid::coord_t>(m_d0Max), m_d0NumBins); auto dim1 = boost::make_shared<MDHistoDimension>( m_d1Label, m_d1ID, *frame, static_cast<Mantid::coord_t>(m_d1Min), static_cast<Mantid::coord_t>(m_d1Max), m_d1NumBins); auto ws = createMDWorkspace(dim0, dim1, boxController); auto spectraAxis = inputWs->getAxis(1); for (size_t index = 0; index < inputWs->getNumberHistograms(); ++index) { auto counts = inputWs->readY(index); auto wavelengths = inputWs->readX(index); auto errors = inputWs->readE(index); const size_t nInputBins = wavelengths.size() - 1; const double theta_final = spectraAxis->getValue(index); m_calculator->setThetaFinal(theta_final); // Loop over all bins in spectra for (size_t binIndex = 0; binIndex < nInputBins; ++binIndex) { const double &wavelength = 0.5 * (wavelengths[binIndex] + wavelengths[binIndex + 1]); double _d0 = m_calculator->calculateDim0(wavelength); double _d1 = m_calculator->calculateDim1(wavelength); double centers[2] = {_d0, _d1}; ws->addEvent(MDLeanEvent<2>(float(counts[binIndex]), float(errors[binIndex] * errors[binIndex]), centers)); } } ws->splitAllIfNeeded(nullptr); ws->refreshCache(); return ws; }
/**The method responsible for analyzing input workspace parameters and preprocessing detectors positions into reciprocal space * * @param InWS2D -- input Matrix workspace with defined instrument * @param dEModeRequested -- energy conversion mode (direct/indirect/elastic) * @param updateMasks -- if full detector positions calculations or just update masking requested * @param OutWSName -- the name for the preprocessed detectors workspace to have in the analysis data service * * @return shared pointer to the workspace with preprocessed detectors information. */ DataObjects::TableWorkspace_const_sptr ConvertToMDParent::preprocessDetectorsPositions( Mantid::API::MatrixWorkspace_const_sptr InWS2D,const std::string &dEModeRequested, bool updateMasks, const std::string & OutWSName) { DataObjects::TableWorkspace_sptr TargTableWS; Kernel::DeltaEMode::Type Emode; // Do we need to reuse output workspace bool storeInDataService(true); std::string tOutWSName(OutWSName); if(tOutWSName=="-"||tOutWSName.empty()) // TargTableWS is recalculated each time; { storeInDataService = false; tOutWSName = "ServiceTableWS"; // TODO: should be hidden? } else { storeInDataService = true; } // if output workspace exists in dataservice, we may try to use it if(storeInDataService && API::AnalysisDataService::Instance().doesExist(tOutWSName) ) { TargTableWS = API::AnalysisDataService::Instance().retrieveWS<DataObjects::TableWorkspace>(tOutWSName); // get number of all histograms (may be masked or invalid) size_t nHist = InWS2D->getNumberHistograms(); size_t nDetMap=TargTableWS->rowCount(); if(nHist==nDetMap) { // let's take at least some precaution to ensure that instrument have not changed std::string currentWSInstrumentName = InWS2D->getInstrument()->getName(); std::string oldInstrName = TargTableWS->getLogs()->getPropertyValueAsType<std::string>("InstrumentName"); if(oldInstrName==currentWSInstrumentName) { if(!updateMasks) return TargTableWS; //Target workspace with preprocessed detectors exists and seems is correct one. // We still need to update masked detectors information TargTableWS = this->runPreprocessDetectorsToMDChildUpdatingMasks(InWS2D,tOutWSName,dEModeRequested,Emode); return TargTableWS; } } else // there is a workspace in the data service with the same name but this ws is not suitable as target for this algorithm. { // Should delete this WS from the dataservice API::AnalysisDataService::Instance().remove(tOutWSName); } } // No result found in analysis data service or the result is unsatisfactory. Try to calculate target workspace. TargTableWS =this->runPreprocessDetectorsToMDChildUpdatingMasks(InWS2D,tOutWSName,dEModeRequested,Emode); if(storeInDataService) API::AnalysisDataService::Instance().addOrReplace(tOutWSName,TargTableWS); // else // TargTableWS->setName(OutWSName); // check if we got what we wanted: // in direct or indirect mode input ws has to have input energy if(Emode==Kernel::DeltaEMode::Direct||Emode==Kernel::DeltaEMode::Indirect) { double m_Ei = TargTableWS->getLogs()->getPropertyValueAsType<double>("Ei"); if(isNaN(m_Ei)) { // Direct mode needs Ei if(Emode==Kernel::DeltaEMode::Direct)throw(std::invalid_argument("Input neutron's energy has to be defined in inelastic mode ")); // Do we have at least something for Indirect? float *eFixed = TargTableWS->getColDataArray<float>("eFixed"); if(!eFixed) throw(std::invalid_argument("Input neutron's energy has to be defined in inelastic mode ")); uint32_t NDetectors = TargTableWS->getLogs()->getPropertyValueAsType<uint32_t>("ActualDetectorsNum"); for(uint32_t i=0;i<NDetectors;i++) if(isNaN(*(eFixed+i)))throw(std::invalid_argument("Undefined eFixed energy for detector N: "+boost::lexical_cast<std::string>(i))); } } return TargTableWS; }