/** * This function gets the input workspace. In the case for a RebinnedOutput * workspace, it must be cleaned before proceeding. Other workspaces are * untouched. * @return the input workspace, cleaned if necessary */ MatrixWorkspace_sptr Integration::getInputWorkspace() { MatrixWorkspace_sptr temp = getProperty("InputWorkspace"); if (temp->id() == "RebinnedOutput") { // Clean the input workspace in the RebinnedOutput case for nan's and // inf's in order to treat the data correctly later. IAlgorithm_sptr alg = this->createChildAlgorithm("ReplaceSpecialValues"); alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", temp); std::string outName = "_" + temp->getName() + "_clean"; alg->setProperty("OutputWorkspace", outName); alg->setProperty("NaNValue", 0.0); alg->setProperty("NaNError", 0.0); alg->setProperty("InfinityValue", 0.0); alg->setProperty("InfinityError", 0.0); alg->executeAsChildAlg(); temp = alg->getProperty("OutputWorkspace"); } // To integrate point data it will be converted to histograms if (!temp->isHistogramData()) { auto alg = this->createChildAlgorithm("ConvertToHistogram"); alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", temp); std::string outName = "_" + temp->getName() + "_histogram"; alg->setProperty("OutputWorkspace", outName); alg->executeAsChildAlg(); temp = alg->getProperty("OutputWorkspace"); temp->setDistribution(true); } return temp; }
/** * This function creates the output workspace. In the case of a RebinnedOutput * workspace, the resulting workspace only needs to be a Workspace2D to handle * the integration. Other workspaces are handled normally. * * @param inWS input workspace to integrate * @param minSpec minimum spectrum to integrate * @param maxSpec maximum spectrum to integrate * * @return the output workspace */ MatrixWorkspace_sptr Integration::getOutputWorkspace(MatrixWorkspace_sptr inWS, const int minSpec, const int maxSpec) { if (inWS->id() == "RebinnedOutput") { MatrixWorkspace_sptr outWS = API::WorkspaceFactory::Instance().create( "Workspace2D", maxSpec - minSpec + 1, 2, 1); API::WorkspaceFactory::Instance().initializeFromParent(inWS, outWS, true); return outWS; } else { return API::WorkspaceFactory::Instance().create(inWS, maxSpec - minSpec + 1, 2, 1); } }
/** * This function gets the input workspace. In the case for a RebinnedOutput * workspace, it must be cleaned before proceeding. Other workspaces are * untouched. * @return the input workspace, cleaned if necessary */ MatrixWorkspace_const_sptr Integration::getInputWorkspace() { MatrixWorkspace_sptr temp = getProperty("InputWorkspace"); if (temp->id() == "RebinnedOutput") { // Clean the input workspace in the RebinnedOutput case for nan's and // inf's in order to treat the data correctly later. IAlgorithm_sptr alg = this->createChildAlgorithm("ReplaceSpecialValues"); alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", temp); std::string outName = "_"+temp->getName()+"_clean"; alg->setProperty("OutputWorkspace", outName); alg->setProperty("NaNValue", 0.0); alg->setProperty("NaNError", 0.0); alg->setProperty("InfinityValue", 0.0); alg->setProperty("InfinityError", 0.0); alg->executeAsChildAlg(); temp = alg->getProperty("OutputWorkspace"); } return temp; }
/** * Executes the algorithm. * Saves the workspace specified by the user to the VTK XML format */ void SaveVTK::exec() { std::string filename = getProperty("Filename"); g_log.debug() << "Parameters: Filename='" << filename << "'" << std::endl; //add extension filename += ".vtu"; MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace"); if( !inputWorkspace ) { g_log.error("Failed to retrieve inputWorkspace."); throw Exception::NullPointerException("SaveVTK::exec()", "inputWorkspace"); } checkOptionalProperties(); //Open file for writing std::ofstream outVTP(filename.c_str()); if( !outVTP ) { g_log.error("Failed to open file: " + filename); throw Exception::FileError("Failed to open file ", filename); } // First write document level XML header outVTP << "<?xml version=\"1.0\"?>\n" "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n" "<UnstructuredGrid>\n"; const std::string workspaceID = inputWorkspace->id(); if( workspaceID.find("Workspace2D") != std::string::npos ) { const Workspace2D_sptr localWorkspace = boost::dynamic_pointer_cast<Workspace2D>(inputWorkspace); // const size_t numberOfHist = localWorkspace->getNumberHistograms(); //Write out whole range bool xMin(m_Xmin > 0.0), xMax(m_Xmax > 0.0); Progress prog(this,0.0,1.0,97); if( !xMin && !xMax ) { for( int hNum = 2; hNum < 100; ++hNum ) { writeVTKPiece(outVTP, localWorkspace->dataX(hNum), localWorkspace->dataY(hNum), localWorkspace->dataE(hNum), hNum); prog.report(); } } else { for( int hNum = 2; hNum < 100; ++hNum ) { std::vector<double> xValue, yValue, errors; std::vector<double>::size_type nVals(localWorkspace->dataY(hNum).size()); for( int i = 0; i < (int)nVals; ++i ) { if( xMin && localWorkspace->dataX(hNum)[i] < m_Xmin ) continue; if( xMax && localWorkspace->dataX(hNum)[i+1] > m_Xmax) { xValue.push_back(localWorkspace->dataX(hNum)[i]); break; } xValue.push_back(localWorkspace->dataX(hNum)[i]); if( i == (int)nVals - 1 ) { xValue.push_back(localWorkspace->dataX(hNum)[i+1]); } yValue.push_back(localWorkspace->dataY(hNum)[i]); errors.push_back(localWorkspace->dataE(hNum)[i]); } //sanity check assert( (int)xValue.size() == (int)yValue.size() + 1 ); writeVTKPiece(outVTP, xValue, yValue, errors, hNum); prog.report(); } } } else { outVTP.close(); Poco::File(filename).remove(); throw Exception::NotImplementedError("SaveVTK only implemented for Workspace2D\n"); } // Final XML end block tags outVTP << "</UnstructuredGrid>\n</VTKFile>\n"; outVTP.close(); }