/** Execute the algorithm. */ void ConvertDiffCal::exec() { OffsetsWorkspace_const_sptr offsetsWS = getProperty("OffsetsWorkspace"); // initial setup of new style config ITableWorkspace_sptr configWksp = boost::make_shared<TableWorkspace>(); configWksp->addColumn("int", "detid"); configWksp->addColumn("double", "difc"); configWksp->addColumn("double", "difa"); configWksp->addColumn("double", "tzero"); // create values in the table const size_t numberOfSpectra = offsetsWS->getNumberHistograms(); Progress progress(this, 0.0, 1.0, numberOfSpectra); for (size_t i = 0; i < numberOfSpectra; ++i) { API::TableRow newrow = configWksp->appendRow(); newrow << static_cast<int>(getDetID(offsetsWS, i)); newrow << calculateDIFC(offsetsWS, i); newrow << 0.; // difa newrow << 0.; // tzero progress.report(); } // sort the results IAlgorithm_sptr sortTable = createChildAlgorithm("SortTableWorkspace"); sortTable->setProperty("InputWorkspace", configWksp); sortTable->setProperty("OutputWorkspace", configWksp); sortTable->setPropertyValue("Columns", "detid"); sortTable->executeAsChildAlg(); // copy over the results configWksp = sortTable->getProperty("OutputWorkspace"); setProperty("OutputWorkspace", configWksp); }
/** * Creates an output workspace from calculated and observed values * * This method creates a table workspace for an ILatticeFunction, containing * observed and calculated d-values for each HKL, as well as the difference * between those two values. * * @param baseName :: Basename for output workspace. * @param function :: An ILatticeFunction * @param domain :: Pointer to LatticeDomain instance. * @param values :: Pointer to FunctionValues instance. * @param outputWorkspacePropertyName :: Name of output workspace property. * @return TableWorkspace with calculated and observed d-values. */ Workspace_sptr LatticeDomainCreator::createOutputWorkspace( const std::string &baseName, IFunction_sptr function, boost::shared_ptr<FunctionDomain> domain, boost::shared_ptr<FunctionValues> values, const std::string &outputWorkspacePropertyName) { boost::shared_ptr<LatticeDomain> latticeDomain = boost::dynamic_pointer_cast<LatticeDomain>(domain); if (!latticeDomain) { throw std::invalid_argument("LatticeDomain is required."); } ILatticeFunction_sptr latticeFunction = boost::dynamic_pointer_cast<ILatticeFunction>(function); if (!latticeFunction) { throw std::invalid_argument( "LatticeDomainCreator can only process ILatticeFunction."); } // Calculate function values again. latticeFunction->functionLattice(*latticeDomain, *values); ITableWorkspace_sptr tableWorkspace = WorkspaceFactory::Instance().createTable(); if (tableWorkspace) { tableWorkspace->addColumn("V3D", "HKL"); tableWorkspace->addColumn("double", "d(obs)"); tableWorkspace->addColumn("double", "d(calc)"); tableWorkspace->addColumn("double", "d(obs) - d(calc)"); for (size_t i = 0; i < values->size(); ++i) { double dObs = values->getFitData(i); double dCalc = values->getCalculated(i); TableRow newRow = tableWorkspace->appendRow(); newRow << (*latticeDomain)[i] << dObs << dCalc << dObs - dCalc; } } if (m_manager && !outputWorkspacePropertyName.empty()) { declareProperty( new WorkspaceProperty<ITableWorkspace>(outputWorkspacePropertyName, "", Kernel::Direction::Output), "Result workspace"); m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspace"); m_manager->setProperty(outputWorkspacePropertyName, tableWorkspace); } return tableWorkspace; }
/// Returns a TableWorkspace with refined cell parameters and error. ITableWorkspace_sptr PoldiFitPeaks2D::getRefinedCellParameters( const IFunction_sptr &fitFunction) const { Poldi2DFunction_sptr poldi2DFunction = boost::dynamic_pointer_cast<Poldi2DFunction>(fitFunction); if (!poldi2DFunction || poldi2DFunction->nFunctions() < 1) { throw std::invalid_argument( "Cannot process function that is not a Poldi2DFunction."); } // Create a new table for lattice parameters ITableWorkspace_sptr latticeParameterTable = WorkspaceFactory::Instance().createTable(); latticeParameterTable->addColumn("str", "Parameter"); latticeParameterTable->addColumn("double", "Value"); latticeParameterTable->addColumn("double", "Error"); // The first function should be PoldiSpectrumPawleyFunction boost::shared_ptr<PoldiSpectrumPawleyFunction> poldiPawleyFunction = boost::dynamic_pointer_cast<PoldiSpectrumPawleyFunction>( poldi2DFunction->getFunction(0)); if (!poldiPawleyFunction) { throw std::invalid_argument("First function in Poldi2DFunction is not " "PoldiSpectrumPawleyFunction."); } // Get the actual PawleyFunction to extract parameters. IPawleyFunction_sptr pawleyFunction = boost::dynamic_pointer_cast<IPawleyFunction>( poldiPawleyFunction->getDecoratedFunction()); if (pawleyFunction) { CompositeFunction_sptr pawleyParts = boost::dynamic_pointer_cast<CompositeFunction>( pawleyFunction->getDecoratedFunction()); // The first function in PawleyFunction contains the parameters IFunction_sptr pawleyParameters = pawleyParts->getFunction(0); for (size_t i = 0; i < pawleyParameters->nParams(); ++i) { TableRow newRow = latticeParameterTable->appendRow(); newRow << pawleyParameters->parameterName(i) << pawleyParameters->getParameter(i) << pawleyParameters->getError(i); } } return latticeParameterTable; }
/// Execute void EstimatePeakErrors::exec() { IFunction_sptr function = getProperty("Function"); ITableWorkspace_sptr results = WorkspaceFactory::Instance().createTable("TableWorkspace"); results->addColumn("str", "Parameter"); results->addColumn("double", "Value"); results->addColumn("double", "Error"); auto matrix = function->getCovarianceMatrix(); if (!matrix) { g_log.warning() << "Function doesn't have covariance matrix.\n"; setProperty("OutputWorkspace", results); return; } IPeakFunction *peak = dynamic_cast<IPeakFunction *>(function.get()); if (peak) { GSLMatrix covariance(*matrix); calculatePeakValues(*peak, *results, covariance, ""); } else { CompositeFunction *cf = dynamic_cast<CompositeFunction *>(function.get()); if (cf) { size_t ip = 0; for (size_t i = 0; i < cf->nFunctions(); ++i) { IFunction *fun = cf->getFunction(i).get(); size_t np = fun->nParams(); peak = dynamic_cast<IPeakFunction *>(fun); if (peak) { std::string prefix = "f" + std::to_string(i) + "."; GSLMatrix covariance(*matrix, ip, ip, np, np); calculatePeakValues(*peak, *results, covariance, prefix); } ip += np; } } else { g_log.warning() << "Function has no peaks.\n"; } } setProperty("OutputWorkspace", results); }
ITableWorkspace_sptr ALCBaselineModellingModel::exportSections() { if (!m_sections.empty()) { ITableWorkspace_sptr table = WorkspaceFactory::Instance().createTable("TableWorkspace"); table->addColumn("double", "Start X"); table->addColumn("double", "End X"); for (auto it = m_sections.begin(); it != m_sections.end(); ++it) { TableRow newRow = table->appendRow(); newRow << it->first << it->second; } return table; } else { return ITableWorkspace_sptr(); } }
/** Creates grouping table from supplied forward and backward spectra * @param fwd :: [Input] Forward spectra * @param bwd :: [Input] Backward spectra * @return :: Workspace containing custom grouping */ Workspace_sptr PlotAsymmetryByLogValue::createCustomGrouping(const std::vector<int> &fwd, const std::vector<int> &bwd) { ITableWorkspace_sptr group = WorkspaceFactory::Instance().createTable("TableWorkspace"); group->addColumn("vector_int", "group"); TableRow row = group->appendRow(); row << fwd; row = group->appendRow(); row << bwd; return boost::dynamic_pointer_cast<Workspace>(group); }
/** Execute the algorithm. */ void CreateChunkingFromInstrument::exec() { // get the instrument Instrument_const_sptr inst = this->getInstrument(); // setup the output workspace ITableWorkspace_sptr strategy = WorkspaceFactory::Instance().createTable("TableWorkspace"); strategy->addColumn("str", "BankName"); this->setProperty("OutputWorkspace", strategy); // get the correct level of grouping string groupLevel = this->getPropertyValue(PARAM_CHUNK_BY); vector<string> groupNames = getGroupNames(this->getPropertyValue(PARAM_CHUNK_NAMES)); if (groupLevel.compare("All") == 0) { return; // nothing to do } else if (inst->getName().compare("SNAP") == 0 && groupLevel.compare("Group") == 0) { groupNames.clear(); groupNames.push_back("East"); groupNames.push_back("West"); } // set up a progress bar with the "correct" number of steps int maxBankNum = this->getProperty(PARAM_MAX_BANK_NUM); Progress progress(this, .2, 1., maxBankNum); // search the instrument for the bank names int maxRecurseDepth = this->getProperty(PARAM_MAX_RECURSE); map<string, vector<string>> grouping; // cppcheck-suppress syntaxError PRAGMA_OMP(parallel for schedule(dynamic, 1) ) for (int num = 0; num < maxBankNum; ++num) { PARALLEL_START_INTERUPT_REGION ostringstream mess; mess << "bank" << num; IComponent_const_sptr comp = inst->getComponentByName(mess.str(), maxRecurseDepth); PARALLEL_CRITICAL(grouping) if (comp) { // get the name of the correct parent string parent; if (groupNames.empty()) { parent = parentName(comp, groupLevel); } else { parent = parentName(comp, groupNames); } // add it to the correct chunk if (!parent.empty()) { if (grouping.count(parent) == 0) grouping[parent] = vector<string>(); grouping[parent].push_back(comp->getName()); } } progress.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION // check to see that something happened if (grouping.empty()) throw std::runtime_error("Failed to find any banks in the instrument"); // fill in the table workspace for (auto group = grouping.begin(); group != grouping.end(); ++group) { stringstream banks; for (auto bank = group->second.begin(); bank != group->second.end(); ++bank) banks << (*bank) << ","; // remove the trailing comma string banksStr = banks.str(); banksStr = banksStr.substr(0, banksStr.size() - 1); // add it to the table TableRow row = strategy->appendRow(); row << banksStr; } }
/** * Executes the algorithm. */ void LoadTBL::exec() { std::string filename = getProperty("Filename"); std::ifstream file(filename.c_str()); if (!file) { throw Exception::FileError("Unable to open file: ", filename); } std::string line; ITableWorkspace_sptr ws = WorkspaceFactory::Instance().createTable(); std::vector<std::string> columnHeadings; Kernel::Strings::extractToEOL(file, line); // We want to check if the first line contains an empty string or series of // ",,,,," // to see if we are loading a TBL file that actually contains data or not. boost::split(columnHeadings, line, boost::is_any_of(","), boost::token_compress_off); for (auto entry = columnHeadings.begin(); entry != columnHeadings.end();) { if (entry->empty()) { // erase the empty values entry = columnHeadings.erase(entry); } else { // keep any non-empty values ++entry; } } if (columnHeadings.empty()) { // we have an empty string or series of ",,,,," throw std::runtime_error("The file you are trying to load is Empty. \n " "Please load a non-empty TBL file"); } else { // set columns back to empty ready to populated with columnHeadings. columnHeadings.clear(); } // this will tell us if we need to just fill in the cell values // or whether we will have to create the column headings as well. bool isOld = getColumnHeadings(line, columnHeadings); std::vector<std::string> rowVec; if (isOld) { /**THIS IS ESSENTIALLY THE OLD LoadReflTBL CODE**/ // create the column headings auto colStitch = ws->addColumn("str", "StitchGroup"); auto colRuns = ws->addColumn("str", "Run(s)"); auto colTheta = ws->addColumn("str", "ThetaIn"); auto colTrans = ws->addColumn("str", "TransRun(s)"); auto colQmin = ws->addColumn("str", "Qmin"); auto colQmax = ws->addColumn("str", "Qmax"); auto colDqq = ws->addColumn("str", "dq/q"); auto colScale = ws->addColumn("str", "Scale"); auto colOptions = ws->addColumn("str", "Options"); auto colHiddenOptions = ws->addColumn("str", "HiddenOptions"); for (size_t i = 0; i < ws->columnCount(); i++) { auto col = ws->getColumn(i); col->setPlotType(0); } // we are using the old ReflTBL format // where all of the entries are on one line // so we must reset the stream to reread the first line. std::ifstream file(filename.c_str()); if (!file) { throw Exception::FileError("Unable to open file: ", filename); } std::string line; int stitchID = 1; while (Kernel::Strings::extractToEOL(file, line)) { if (line.empty() || line == ",,,,,,,,,,,,,,,,") { continue; } getCells(line, rowVec, 16, isOld); const std::string scaleStr = rowVec.at(16); const std::string stitchStr = boost::lexical_cast<std::string>(stitchID); // check if the first run in the row has any data associated with it // 0 = runs, 1 = theta, 2 = trans, 3 = qmin, 4 = qmax if (!rowVec[0].empty() || !rowVec[1].empty() || !rowVec[2].empty() || !rowVec[3].empty() || !rowVec[4].empty()) { TableRow row = ws->appendRow(); row << stitchStr; for (int i = 0; i < 5; ++i) { row << rowVec.at(i); } row << rowVec.at(15); row << scaleStr; } // check if the second run in the row has any data associated with it // 5 = runs, 6 = theta, 7 = trans, 8 = qmin, 9 = qmax if (!rowVec[5].empty() || !rowVec[6].empty() || !rowVec[7].empty() || !rowVec[8].empty() || !rowVec[9].empty()) { TableRow row = ws->appendRow(); row << stitchStr; for (int i = 5; i < 10; ++i) { row << rowVec.at(i); } row << rowVec.at(15); row << scaleStr; } // check if the third run in the row has any data associated with it // 10 = runs, 11 = theta, 12 = trans, 13 = qmin, 14 = qmax if (!rowVec[10].empty() || !rowVec[11].empty() || !rowVec[12].empty() || !rowVec[13].empty() || !rowVec[14].empty()) { TableRow row = ws->appendRow(); row << stitchStr; for (int i = 10; i < 17; ++i) { if (i == 16) row << scaleStr; else row << rowVec.at(i); } } ++stitchID; setProperty("OutputWorkspace", ws); } } else { // we have a TBL format that contains column headings // on the first row. These are now entries in the columns vector if (!columnHeadings.empty()) { // now we need to add the custom column headings from // the columns vector to the TableWorkspace for (auto heading = columnHeadings.begin(); heading != columnHeadings.end();) { if (heading->empty()) { // there is no need to have empty column headings. heading = columnHeadings.erase(heading); } else { Mantid::API::Column_sptr col; col = ws->addColumn("str", *heading); col->setPlotType(0); heading++; } } } size_t expectedCommas = columnHeadings.size() - 1; while (Kernel::Strings::extractToEOL(file, line)) { if (line.empty() || line == ",,,,,,,,,,,,,,,,") { // skip over any empty lines continue; } getCells(line, rowVec, columnHeadings.size() - 1, isOld); // populate the columns with their values for this row. TableRow row = ws->appendRow(); for (size_t i = 0; i < expectedCommas + 1; ++i) { row << rowVec.at(i); } } setProperty("OutputWorkspace", ws); } }
//---------------------------------------------------------------------------------------------- /// Run the algorithm void QueryMDWorkspace::exec() { // Extract the required normalisation. std::string strNormalisation = getPropertyValue("Normalisation"); MDNormalization requestedNormalisation = whichNormalisation(strNormalisation); IMDWorkspace_sptr input = getProperty("InputWorkspace"); const bool transformCoordsToOriginal = getProperty("TransformCoordsToOriginal"); // Define a table workspace with a specific column schema. ITableWorkspace_sptr output = WorkspaceFactory::Instance().createTable(); const std::string signalColumnName = "Signal/" + strNormalisation; const std::string errorColumnName = "Error/" + strNormalisation; output->addColumn("double", signalColumnName); output->addColumn("double", errorColumnName); output->addColumn("int", "Number of Events"); const size_t ndims = input->getNumDims(); for (size_t index = 0; index < ndims; ++index) { Mantid::Geometry::IMDDimension_const_sptr dim = input->getDimension(index); std::string dimInUnit = dim->getName() + "/" + dim->getUnits().ascii(); output->addColumn("double", dimInUnit); // Magic numbers required to configure the X axis. output->getColumn(dimInUnit)->setPlotType(1); } // Magic numbers required to configure the Y axis. output->getColumn(signalColumnName)->setPlotType(2); output->getColumn(errorColumnName)->setPlotType(5); IMDIterator *it = input->createIterator(); it->setNormalization(requestedNormalisation); bool bLimitRows = getProperty("LimitRows"); int maxRows = 0; if (bLimitRows) { maxRows = getProperty("MaximumRows"); } // Use the iterator to loop through each MDBoxBase and create a row for each // entry. int rowCounter = 0; Progress progress(this, 0, 1, int64_t(input->getNPoints())); while (true) { size_t cellIndex = 0; output->appendRow(); output->cell<double>(rowCounter, cellIndex++) = it->getNormalizedSignal(); output->cell<double>(rowCounter, cellIndex++) = it->getNormalizedError(); output->cell<int>(rowCounter, cellIndex++) = int(it->getNumEvents()); VMD center = it->getCenter(); const size_t numberOriginal = input->getNumberTransformsToOriginal(); if (transformCoordsToOriginal && numberOriginal > 0) { const size_t index = numberOriginal - 1; CoordTransform const *transform = input->getTransformToOriginal(index); VMD temp = transform->applyVMD(center); center = temp; } for (size_t index = 0; index < ndims; ++index) { output->cell<double>(rowCounter, cellIndex++) = center[index]; } progress.report(); if (!it->next() || (bLimitRows && ((rowCounter + 1) >= maxRows))) { break; } rowCounter++; } setProperty("OutputWorkspace", output); delete it; // IMDEventWorkspace_sptr mdew; CALL_MDEVENT_FUNCTION(this->getBoxData, input); }
void QueryMDWorkspace::getBoxData( typename Mantid::DataObjects::MDEventWorkspace<MDE, nd>::sptr ws) { if (this->getPropertyValue("BoxDataTable").empty()) return; ITableWorkspace_sptr output = WorkspaceFactory::Instance().createTable(); output->addColumn("int", "RecursionDepth"); output->addColumn("int", "NumBoxes"); output->addColumn("int", "NumWithEvents"); output->addColumn("double", "PctWithEvents"); output->addColumn("int", "TotalEvents"); output->addColumn("double", "AvgEventsPer"); output->addColumn("double", "TotalWeight"); output->addColumn("double", "TotalSignal"); output->addColumn("double", "TotalErrorSquared"); for (size_t d = 0; d < nd; d++) output->addColumn("double", "Dim" + Strings::toString(d)); size_t depth = ws->getBoxController()->getMaxDepth() + 1; std::vector<int> NumBoxes(depth, 0); std::vector<int> NumWithEvents(depth, 0); std::vector<int> TotalEvents(depth, 0); std::vector<double> TotalWeight(depth, 0); std::vector<double> TotalSignal(depth, 0); std::vector<double> TotalErrorSquared(depth, 0); std::vector<std::vector<double>> Dims(depth, std::vector<double>(nd, 0.0)); std::vector<API::IMDNode *> boxes; ws->getBox()->getBoxes(boxes, depth, true); for (size_t i = 0; i < boxes.size(); i++) { MDBoxBase<MDE, nd> *box = dynamic_cast<MDBoxBase<MDE, nd> *>(boxes[i]); if (!box) throw(std::runtime_error("Can not cast IMDNode to any type of boxes")); size_t d = box->getDepth(); NumBoxes[d] += 1; if (box->getNPoints() > 0) NumWithEvents[d] += 1; TotalEvents[d] += static_cast<int>(box->getNPoints()); TotalWeight[d] += box->getTotalWeight(); TotalSignal[d] += box->getSignal(); TotalErrorSquared[d] += box->getErrorSquared(); for (size_t dim = 0; dim < nd; dim++) Dims[d][dim] = double(box->getExtents(dim).getSize()); } int rowCounter = 0; for (size_t d = 0; d < depth; d++) { int col = 0; output->appendRow(); output->cell<int>(rowCounter, col++) = int(d); output->cell<int>(rowCounter, col++) = NumBoxes[d]; output->cell<int>(rowCounter, col++) = NumWithEvents[d]; output->cell<double>(rowCounter, col++) = 100.0 * double(NumWithEvents[d]) / double(NumBoxes[d]); output->cell<int>(rowCounter, col++) = TotalEvents[d]; output->cell<double>(rowCounter, col++) = double(TotalEvents[d]) / double(NumBoxes[d]); output->cell<double>(rowCounter, col++) = TotalWeight[d]; output->cell<double>(rowCounter, col++) = TotalSignal[d]; output->cell<double>(rowCounter, col++) = TotalErrorSquared[d]; for (size_t dim = 0; dim < nd; dim++) output->cell<double>(rowCounter, col++) = Dims[d][dim]; rowCounter++; } setProperty("BoxDataTable", output); }
/** * Loads a tomography parameterization file into a newly created table * workspace. The file must have the following syntax: * * @verbatim <NXentry name="entry1"> <NXprocess name="processing"> <NXnote name="id"> <values id="ID VALUE" params="..." name="..." cite="..."> </values> </NXnote> </NXprocess> </NXentry> @endverbatim * * @param fname name of the parameterization file * @param wsName name of workspace where to load the file data * * @return table workspace with parameters (plugins) found in the * loaded file */ ITableWorkspace_sptr LoadSavuTomoConfig::loadFile(std::string& fname, std::string& wsName) { // Throws an exception if there is a problem with file access //Mantid::NeXus::NXRoot root(fname); boost::shared_ptr<NeXus::File> f; if (!checkOpenFile(fname, f)) { throw std::runtime_error( "Failed to recognize this file as a NeXus file, cannot continue."); } ITableWorkspace_sptr ws = API::WorkspaceFactory::Instance().createTable(); if (!ws) throw std::runtime_error("Could not create TableWorkspace for " "workspace with name '" + wsName + "'"); // init workspace ws->setTitle("Table with tomography parameters from file " + fname); ws->addColumn("str", "ID"); ws->addColumn("str", "Parameters"); ws->addColumn("str", "Name"); ws->addColumn("str", "Cite"); // a bit of file consistency check, check at least there's a // 'entry1' // it could be more strict and demand entries.size()==1 std::map<std::string, std::string> entries = f->getEntries(); std::string mainEntryName = "entry"; auto it = entries.find(mainEntryName); if (entries.end() == it) { throw std::runtime_error("Could not find the '" + mainEntryName + "' " "entry. Even though this file looks like a valid NeXus file, it is " "not in the correct format for tomography reconstruction " "parameterization files."); } // go through the input file plugin entries f->openGroup(mainEntryName, "NXentry"); f->openGroup("process", "NXprocess"); size_t pluginsLen = f->getEntries().size(); for (size_t j=0; j<pluginsLen; j++) { API::TableRow table = ws->appendRow(); std::string entryIdx = boost::lexical_cast<std::string>(j); try { f->openGroup(entryIdx, "NXnote"); } catch(NeXus::Exception &e) { // detailed NeXus error message and throw... g_log.error() << "Failed to load plugin '" << j << "' from" "NeXus file. Error description: " << e.what() << std::endl; throw std::runtime_error("Could not load one or more plugin " "entries from the tomographic reconstruction parameterization " "file. Please check that the file is correct."); } // TODO: check final 'schema', get these 4 fields from the file std::string id = ""; std::string params = ""; std::string name = ""; std::string cite = ""; try { f->readData("data", params); f->readData("id", id); f->readData("name", name); // cite not available for now // f->readData("cite", cite); // This might be extended to an NXcite group that would be included // not here but inside an "intermediate" NXcollection group. That // NXcite would have 4 arrays: description, doi, endnote, bibtex. // But this is what we have so far. cite = "Not available"; } catch(NeXus::Exception &e) { // permissive, just error message but carry on g_log.warning() << "Failed to read some fields in tomographic " "reconstruction plugin line. The file seems to be wrong. Error " "description: " << e.what() << std::endl; } table << id << params << name << cite; f->closeGroup(); progress(static_cast<double>(j)/static_cast<double>(pluginsLen)); } f->close(); return ws; }
/** * Executes the algorithm */ void PlotPeakByLogValue::exec() { // Create a list of the input workspace const std::vector<InputData> wsNames = makeNames(); std::string fun = getPropertyValue("Function"); //int wi = getProperty("WorkspaceIndex"); std::string logName = getProperty("LogValue"); bool sequential = getPropertyValue("FitType") == "Sequential"; bool isDataName = false; // if true first output column is of type string and is the data source name ITableWorkspace_sptr result = WorkspaceFactory::Instance().createTable("TableWorkspace"); if (logName == "SourceName") { result->addColumn("str","Source name"); isDataName = true; } else if (logName.empty()) { result->addColumn("double","axis-1"); } else { result->addColumn("double",logName); } // Create an instance of the fitting function to obtain the names of fitting parameters IFitFunction* ifun = FunctionFactory::Instance().createInitialized(fun); if (!ifun) { throw std::invalid_argument("Fitting function failed to initialize"); } for(size_t iPar=0;iPar<ifun->nParams();++iPar) { result->addColumn("double",ifun->parameterName(iPar)); result->addColumn("double",ifun->parameterName(iPar)+"_Err"); } result->addColumn("double","Chi_squared"); delete ifun; setProperty("OutputWorkspace",result); double dProg = 1./static_cast<double>(wsNames.size()); double Prog = 0.; for(int i=0;i<static_cast<int>(wsNames.size());++i) { InputData data = getWorkspace(wsNames[i]); if (!data.ws) { g_log.warning() << "Cannot access workspace " << wsNames[i].name << '\n'; continue; } if (data.i < 0 && data.indx.empty()) { g_log.warning() << "Zero spectra selected for fitting in workspace " << wsNames[i].name << '\n'; continue; } int j,jend; if (data.i >= 0) { j = data.i; jend = j + 1; } else {// no need to check data.indx.empty() j = data.indx.front(); jend = data.indx.back() + 1; } dProg /= abs(jend - j); for(;j < jend;++j) { // Find the log value: it is either a log-file value or simply the workspace number double logValue; if (logName.empty()) { API::Axis* axis = data.ws->getAxis(1); logValue = (*axis)(j); } else if (logName != "SourceName") { Kernel::Property* prop = data.ws->run().getLogData(logName); if (!prop) { throw std::invalid_argument("Log value "+logName+" does not exist"); } TimeSeriesProperty<double>* logp = dynamic_cast<TimeSeriesProperty<double>*>(prop); logValue = logp->lastValue(); } std::string resFun = fun; std::vector<double> errors; double chi2; try { // Fit the function API::IAlgorithm_sptr fit = createSubAlgorithm("Fit"); fit->initialize(); fit->setProperty("InputWorkspace",data.ws); //fit->setPropertyValue("InputWorkspace",data.ws->getName()); fit->setProperty("WorkspaceIndex",j); fit->setPropertyValue("Function",fun); fit->setPropertyValue("StartX",getPropertyValue("StartX")); fit->setPropertyValue("EndX",getPropertyValue("EndX")); fit->setPropertyValue("Minimizer",getPropertyValue("Minimizer")); fit->setPropertyValue("CostFunction",getPropertyValue("CostFunction")); fit->execute(); resFun = fit->getPropertyValue("Function"); errors = fit->getProperty("Errors"); chi2 = fit->getProperty("OutputChi2overDoF"); } catch(...) { g_log.error("Error in Fit subalgorithm"); throw; } if (sequential) { fun = resFun; } // Extract the fitted parameters and put them into the result table TableRow row = result->appendRow(); if (isDataName) { row << wsNames[i].name; } else { row << logValue; } ifun = FunctionFactory::Instance().createInitialized(resFun); for(size_t iPar=0;iPar<ifun->nParams();++iPar) { row << ifun->getParameter(iPar) << errors[iPar]; } row << chi2; delete ifun; Prog += dProg; progress(Prog); interruption_point(); } // for(;j < jend;++j) } }
Workspace_sptr GeneralDomainCreator::createOutputWorkspace( const std::string &baseName, IFunction_sptr function, boost::shared_ptr<FunctionDomain> domain, boost::shared_ptr<FunctionValues> values, const std::string &outputWorkspacePropertyName) { if (function->getValuesSize(*domain) != values->size()) { throw std::runtime_error("Failed to create output workspace: domain and " "values object don't match."); } size_t rowCount = domain->size(); if (rowCount == 0) { auto &generalFunction = dynamic_cast<IFunctionGeneral &>(*function); rowCount = generalFunction.getDefaultDomainSize(); } ITableWorkspace_sptr outputWorkspace; auto inputWorkspace = getInputWorkspace(); // Clone the data and domain columns from inputWorkspace to outputWorkspace. if (inputWorkspace) { // Collect the names of columns to clone std::vector<std::string> columnsToClone; for (auto &propName : m_domainColumnNames) { auto columnName = m_manager->getPropertyValue(propName); columnsToClone.push_back(columnName); } for (auto &propName : m_dataColumnNames) { auto columnName = m_manager->getPropertyValue(propName); columnsToClone.push_back(columnName); } outputWorkspace = inputWorkspace->cloneColumns(columnsToClone); if (rowCount != outputWorkspace->rowCount()) { throw std::runtime_error("Cloned workspace has wrong number of rows."); } // Add columns with the calculated data size_t i0 = 0; for (auto &propName : m_dataColumnNames) { auto dataColumnName = m_manager->getPropertyValue(propName); auto calcColumnName = dataColumnName + "_calc"; auto column = outputWorkspace->addColumn("double", calcColumnName); for (size_t row = 0; row < rowCount; ++row) { auto value = values->getCalculated(i0 + row); column->fromDouble(row, value); } i0 += rowCount; } } else { outputWorkspace = API::WorkspaceFactory::Instance().createTable(); outputWorkspace->setRowCount(rowCount); size_t i0 = 0; for (auto &propName : m_dataColumnNames) { auto calcColumnName = m_manager->getPropertyValue(propName); if (calcColumnName.empty()) { calcColumnName = propName; } auto column = outputWorkspace->addColumn("double", calcColumnName); for (size_t row = 0; row < rowCount; ++row) { auto value = values->getCalculated(i0 + row); column->fromDouble(row, value); } i0 += rowCount; } } if (!outputWorkspacePropertyName.empty()) { declareProperty( new API::WorkspaceProperty<API::ITableWorkspace>( outputWorkspacePropertyName, "", Kernel::Direction::Output), "Name of the output Workspace holding resulting simulated values"); m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspace"); m_manager->setProperty(outputWorkspacePropertyName, outputWorkspace); } return outputWorkspace; }
/** Executes the algorithm. Reading in the file and creating and populating * the output workspace * * @throw Exception::FileError If the Nexus file cannot be found/opened * @throw std::invalid_argument If the optional properties are set to invalid values */ void PoldiLoadIPP::exec() { //////////////////////////////////////////////////////////////////////// // About the workspace //////////////////////////////////////////////////////////////////////// DataObjects::Workspace2D_sptr localWorkspace = this->getProperty("InputWorkspace"); //////////////////////////////////////////////////////////////////////// // Load the data into the workspace //////////////////////////////////////////////////////////////////////// try { ITableWorkspace_sptr outputws = WorkspaceFactory::Instance().createTable(); outputws->addColumn("str","param"); outputws->addColumn("str","unit"); outputws->addColumn("double","value"); Geometry::Instrument_const_sptr inst = localWorkspace->getInstrument(); double distChopSampl = localWorkspace->getInstrument()->getNumberParameter("dist-chopper-sample")[0]; g_log.debug() << "_poldi : param " << "dist-chopper-sample" << " : " << distChopSampl << std::endl; TableRow t0 = outputws->appendRow(); t0 << "dist-chopper-sample" << "[mm]" << distChopSampl ; double distSamplDet = localWorkspace->getInstrument()->getNumberParameter("dist-sample-detector")[0]; g_log.debug() << "_poldi : param " << "dist-sample-detector" << " : " << distSamplDet << std::endl; TableRow t1 = outputws->appendRow(); t1 << "dist-sample-detector" << "[mm]" << distSamplDet ; double x0det = localWorkspace->getInstrument()->getNumberParameter("x0det")[0]; g_log.debug() << "_poldi : param " << "x0det" << " : " << x0det << std::endl; TableRow t2 = outputws->appendRow(); t2 << "x0det" << "[mm]" << x0det ; double y0det = localWorkspace->getInstrument()->getNumberParameter("y0det")[0]; g_log.debug() << "_poldi : param " << "y0det" << " : " << y0det << std::endl; TableRow t3 = outputws->appendRow(); t3 << "y0det" << "[mm]" << y0det ; double twotheta = localWorkspace->getInstrument()->getNumberParameter("twothet")[0]; g_log.debug() << "_poldi : param " << "twothet" << " : " << twotheta << std::endl; TableRow t4 = outputws->appendRow(); t4 << "twothet" << "[deg]" << twotheta ; double tps0 = localWorkspace->getInstrument()->getNumberParameter("t0")[0]; g_log.debug() << "_poldi : param " << "t0" << " : " << tps0 << std::endl; TableRow t5 = outputws->appendRow(); t5 << "t0" << "[mysec]" << tps0 ; double tcycle = localWorkspace->getInstrument()->getNumberParameter("tconst")[0]; g_log.debug() << "_poldi : param " << "tconst" << " : " << tcycle << std::endl; TableRow t6 = outputws->appendRow(); t6 << "tconst" << "[mysec]" << tcycle ; double det_radius = localWorkspace->getInstrument()->getNumberParameter("det_radius")[0]; g_log.debug() << "_poldi : param " << "det_radius" << " : " << det_radius << std::endl; TableRow t8 = outputws->appendRow(); t8 << "det_radius" << "[mm]" << det_radius ; double det_nb_channel = localWorkspace->getInstrument()->getNumberParameter("det_nb_channel")[0]; g_log.debug() << "_poldi : param " << "det_nb_channel" << " : " << det_nb_channel << std::endl; TableRow t9 = outputws->appendRow(); t9 << "det_nb_channel" << "[]" << det_nb_channel ; double det_channel_resolution = localWorkspace->getInstrument()->getNumberParameter("det_channel_resolution")[0]; g_log.debug() << "_poldi : param " << "det_channel_resolution" << " : " << det_channel_resolution << std::endl; TableRow t10 = outputws->appendRow(); t10 << "det_channel_resolution" << "[mm]" << det_channel_resolution ; setProperty("PoldiIPP",outputws); } catch(Mantid::Kernel::Exception::NotFoundError& ) { throw std::runtime_error("Error when saving the PoldiIPP Results data to Workspace : NotFoundError"); } catch(std::runtime_error &) { throw std::runtime_error("Error when saving the PoldiIPP Results data to Workspace : runtime_error"); } }