/** Execute the algorithm. */ void LoadCalFile::exec() { std::string CalFilename = getPropertyValue("CalFilename"); std::string WorkspaceName = getPropertyValue("WorkspaceName"); bool MakeGroupingWorkspace = getProperty("MakeGroupingWorkspace"); bool MakeOffsetsWorkspace = getProperty("MakeOffsetsWorkspace"); bool MakeMaskWorkspace = getProperty("MakeMaskWorkspace"); if (WorkspaceName.empty()) throw std::invalid_argument("Must specify WorkspaceName."); Instrument_const_sptr inst = LoadCalFile::getInstrument3Ways(this); GroupingWorkspace_sptr groupWS; OffsetsWorkspace_sptr offsetsWS; MaskWorkspace_sptr maskWS; // Title of all workspaces = the file without path std::string title = Poco::Path(CalFilename).getFileName(); // Initialize all required workspaces. if (MakeGroupingWorkspace) { groupWS = GroupingWorkspace_sptr(new GroupingWorkspace(inst)); groupWS->setTitle(title); declareProperty(new WorkspaceProperty<GroupingWorkspace>("OutputGroupingWorkspace", WorkspaceName + "_group", Direction::Output), "Set the the output GroupingWorkspace, if any."); groupWS->mutableRun().addProperty("Filename",CalFilename); setProperty("OutputGroupingWorkspace", groupWS); } if (MakeOffsetsWorkspace) { offsetsWS = OffsetsWorkspace_sptr(new OffsetsWorkspace(inst)); offsetsWS->setTitle(title); declareProperty(new WorkspaceProperty<OffsetsWorkspace>("OutputOffsetsWorkspace", WorkspaceName + "_offsets", Direction::Output), "Set the the output OffsetsWorkspace, if any."); offsetsWS->mutableRun().addProperty("Filename",CalFilename); setProperty("OutputOffsetsWorkspace", offsetsWS); } if (MakeMaskWorkspace) { maskWS = MaskWorkspace_sptr(new MaskWorkspace(inst)); maskWS->setTitle(title); declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputMaskWorkspace", WorkspaceName + "_mask", Direction::Output), "Set the the output MaskWorkspace, if any."); maskWS->mutableRun().addProperty("Filename",CalFilename); setProperty("OutputMaskWorkspace", maskWS); } LoadCalFile::readCalFile(CalFilename, groupWS, offsetsWS, maskWS); }
void AlignDetectors::getCalibrationWS(MatrixWorkspace_sptr inputWS) { const std::string calFileName = getPropertyValue("CalibrationFile"); if (!calFileName.empty()) { progress(0.0, "Reading calibration file"); loadCalFile(inputWS, calFileName); } else { m_calibrationWS = getProperty("CalibrationWorkspace"); if (!m_calibrationWS) { OffsetsWorkspace_sptr offsetsWS = getProperty("OffsetsWorkspace"); auto alg = createChildAlgorithm("ConvertDiffCal"); alg->setProperty("OffsetsWorkspace", offsetsWS); alg->executeAsChildAlg(); m_calibrationWS = alg->getProperty("OutputWorkspace"); m_calibrationWS->setTitle(offsetsWS->getTitle()); } } }
void AlignDetectors::getCalibrationWS(MatrixWorkspace_sptr inputWS) { m_calibrationWS = getProperty("CalibrationWorkspace"); if (m_calibrationWS) return; // nothing more to do OffsetsWorkspace_sptr offsetsWS = getProperty("OffsetsWorkspace"); if (offsetsWS) { auto alg = createChildAlgorithm("ConvertDiffCal"); alg->setProperty("OffsetsWorkspace", offsetsWS); alg->executeAsChildAlg(); m_calibrationWS = alg->getProperty("OutputWorkspace"); m_calibrationWS->setTitle(offsetsWS->getTitle()); return; } const std::string calFileName = getPropertyValue("CalibrationFile"); if (!calFileName.empty()) { progress(0.0, "Reading calibration file"); loadCalFile(inputWS, calFileName); return; } throw std::runtime_error("Failed to determine calibration information"); }
/** Reads the calibration file. * * @param calFileName :: path to the old .cal file * @param groupWS :: optional, GroupingWorkspace to save. Will be 0 if not specified. * @param offsetsWS :: optional, OffsetsWorkspace to save. Will be 0.0 if not specified. * @param maskWS :: optional, masking-type workspace to save. Will be 1 (selected) if not specified. */ void SaveCalFile::saveCalFile(const std::string& calFileName, GroupingWorkspace_sptr groupWS, OffsetsWorkspace_sptr offsetsWS, MaskWorkspace_sptr maskWS) { Instrument_const_sptr inst; bool doGroup = false; if (groupWS) { doGroup = true; inst = groupWS->getInstrument(); } bool doOffsets = false; if (offsetsWS) { doOffsets = true; inst = offsetsWS->getInstrument(); } bool doMask = false; if (maskWS) { doMask = true; inst = maskWS->getInstrument(); if (!inst) g_log.warning() << "Mask workspace " << maskWS->name() << " has no instrument associated with." << "\n"; } g_log.information() << "Status: doGroup = " << doGroup << " doOffsets = " << doOffsets << " doMask = " << doMask << "\n"; if (!inst) throw std::invalid_argument("You must give at least one of the grouping, offsets or masking workspaces."); // Header of the file std::ofstream fout(calFileName.c_str()); fout <<"# Calibration file for instrument " << inst->getName() << " written on " << DateAndTime::getCurrentTime().toISO8601String() << ".\n"; fout <<"# Format: number UDET offset select group\n"; // Get all the detectors detid2det_map allDetectors; inst->getDetectors(allDetectors); int64_t number=0; detid2det_map::const_iterator it; for (it = allDetectors.begin(); it != allDetectors.end(); ++it) { detid_t detectorID = it->first; // Geometry::IDetector_const_sptr det = it->second; //Find the offset, if any double offset = 0.0; if (doOffsets) offset = offsetsWS->getValue(detectorID, 0.0); //Find the group, if any int64_t group = 1; if (doGroup) group = static_cast<int64_t>(groupWS->getValue(detectorID, 0.0)); // Find the selection, if any int selected = 1; if (doMask && (maskWS->isMasked(detectorID))) selected = 0; //if(group > 0) fout << std::fixed << std::setw(9) << number << std::fixed << std::setw(15) << detectorID << std::fixed << std::setprecision(7) << std::setw(15)<< offset << std::fixed << std::setw(8) << selected << std::fixed << std::setw(8) << group << "\n"; number++; } }
/** Reads the calibration file. * * @param calFileName :: path to the old .cal file * @param groupWS :: optional, GroupingWorkspace to fill. Must be initialized to the right instrument. * @param offsetsWS :: optional, OffsetsWorkspace to fill. Must be initialized to the right instrument. * @param maskWS :: optional, masking-type workspace to fill. Must be initialized to the right instrument. */ void LoadCalFile::readCalFile(const std::string& calFileName, GroupingWorkspace_sptr groupWS, OffsetsWorkspace_sptr offsetsWS, MaskWorkspace_sptr maskWS) { bool doGroup = bool(groupWS); bool doOffsets = bool(offsetsWS); bool doMask = bool(maskWS); bool hasUnmasked(false); bool hasGrouped(false); if (!doOffsets && !doGroup && !doMask) throw std::invalid_argument("You must give at least one of the grouping, offsets or masking workspaces."); std::ifstream grFile(calFileName.c_str()); if (!grFile) { throw std::runtime_error("Unable to open calibration file " + calFileName); } size_t numErrors = 0; detid2index_map detID_to_wi; if (doMask) { detID_to_wi = maskWS->getDetectorIDToWorkspaceIndexMap(); } // not all of these should be doubles, but to make reading work read as double then recast to int int n,udet,select,group; double n_d, udet_d, offset, select_d, group_d; std::string str; while(getline(grFile,str)) { if (str.empty() || str[0] == '#') continue; std::istringstream istr(str); // read in everything as double then cast as appropriate istr >> n_d >> udet_d >> offset >> select_d >> group_d; n = static_cast<int>(n_d); udet = static_cast<int>(udet_d); select = static_cast<int>(select_d); group = static_cast<int>(group_d); if (doOffsets) { if (offset <= -1.) // should never happen { std::stringstream msg; msg << "Encountered offset = " << offset << " at index " << n << " for udet = " << udet << ". Offsets must be greater than -1."; throw std::runtime_error(msg.str()); } try { offsetsWS->setValue(udet, offset); } catch (std::invalid_argument &) { numErrors++; } } if (doGroup) { try { groupWS->setValue(udet, double(group) ); if ((!hasGrouped) && (group > 0)) hasGrouped = true; } catch (std::invalid_argument &) { numErrors++; } } if (doMask) { detid2index_map::const_iterator it = detID_to_wi.find(udet); if (it != detID_to_wi.end()) { size_t wi = it->second; if (select <= 0) { // Not selected, then mask this detector maskWS->maskWorkspaceIndex(wi); maskWS->dataY(wi)[0] = 1.0; } else { // Selected, set the value to be 0 maskWS->dataY(wi)[0] = 0.0; if (!hasUnmasked) hasUnmasked = true; } } else { // Could not find the UDET. numErrors++; } } } // Warn about any errors if (numErrors > 0) Logger("LoadCalFile").warning() << numErrors << " errors (invalid Detector ID's) found when reading .cal file '" << calFileName << "'.\n"; if (doGroup && (!hasGrouped)) Logger("LoadCalFile").warning() << "'" << calFileName << "' has no spectra grouped\n"; if (doMask && (!hasUnmasked)) Logger("LoadCalFile").warning() << "'" << calFileName << "' masks all spectra\n"; }
/** Execute the algorithm. */ void LoadCalFile::exec() { std::string CalFilename = getPropertyValue("CalFilename"); std::string WorkspaceName = getPropertyValue("WorkspaceName"); bool MakeGroupingWorkspace = getProperty("MakeGroupingWorkspace"); bool MakeOffsetsWorkspace = getProperty("MakeOffsetsWorkspace"); bool MakeMaskWorkspace = getProperty("MakeMaskWorkspace"); if (WorkspaceName.empty()) throw std::invalid_argument("Must specify WorkspaceName."); Instrument_const_sptr inst = LoadCalFile::getInstrument3Ways(this); GroupingWorkspace_sptr groupWS; OffsetsWorkspace_sptr offsetsWS; MaskWorkspace_sptr maskWS; // Title of all workspaces = the file without path std::string title = Poco::Path(CalFilename).getFileName(); // Initialize all required workspaces. if (MakeGroupingWorkspace) { groupWS = GroupingWorkspace_sptr(new GroupingWorkspace(inst)); groupWS->setTitle(title); declareProperty(Kernel::make_unique<WorkspaceProperty<GroupingWorkspace>>( "OutputGroupingWorkspace", WorkspaceName + "_group", Direction::Output), "Set the the output GroupingWorkspace, if any."); groupWS->mutableRun().addProperty("Filename", CalFilename); setProperty("OutputGroupingWorkspace", groupWS); } if (MakeOffsetsWorkspace) { offsetsWS = OffsetsWorkspace_sptr(new OffsetsWorkspace(inst)); offsetsWS->setTitle(title); declareProperty(Kernel::make_unique<WorkspaceProperty<OffsetsWorkspace>>( "OutputOffsetsWorkspace", WorkspaceName + "_offsets", Direction::Output), "Set the the output OffsetsWorkspace, if any."); offsetsWS->mutableRun().addProperty("Filename", CalFilename); setProperty("OutputOffsetsWorkspace", offsetsWS); } if (MakeMaskWorkspace) { maskWS = MaskWorkspace_sptr(new MaskWorkspace(inst)); maskWS->setTitle(title); declareProperty( Kernel::make_unique<WorkspaceProperty<MatrixWorkspace>>( "OutputMaskWorkspace", WorkspaceName + "_mask", Direction::Output), "Set the the output MaskWorkspace, if any."); maskWS->mutableRun().addProperty("Filename", CalFilename); setProperty("OutputMaskWorkspace", maskWS); } LoadCalFile::readCalFile(CalFilename, groupWS, offsetsWS, maskWS); if (MakeOffsetsWorkspace) { auto alg = createChildAlgorithm("ConvertDiffCal"); alg->setProperty("OffsetsWorkspace", offsetsWS); alg->executeAsChildAlg(); ITableWorkspace_sptr calWS = alg->getProperty("OutputWorkspace"); calWS->setTitle(title); declareProperty( Kernel::make_unique<WorkspaceProperty<ITableWorkspace>>( "OutputCalWorkspace", WorkspaceName + "_cal", Direction::Output), "Set the output Diffraction Calibration workspace, if any."); setProperty("OutputCalWorkspace", calWS); } }