/** * Loads the mapping between index -> set of detector IDs * * If "detector_index", "detector_count" and "detector_list" are all present, * use these to get the mapping, otherwise spectrum number = detector ID * (one-to-one) * * The spectrum spectrum_index[i] maps to detector_count[i] detectors, whose * detector IDs are in detector_list starting at the index detector_index[i] * * @returns :: map of index -> detector IDs * @throws std::runtime_error if fails to read data from file */ std::map<int, std::set<int>> LoadMuonNexus2::loadDetectorMapping(const Mantid::NeXus::NXInt &spectrumIndex) { std::map<int, std::set<int>> mapping; const int nSpectra = spectrumIndex.dim0(); // Find and open the data group NXRoot root(getPropertyValue("Filename")); NXEntry entry = root.openEntry(m_entry_name); const std::string detectorName = [&entry]() { // Only the first NXdata found for (auto &group : entry.groups()) { std::string className = group.nxclass; if (className == "NXdata") { return group.nxname; } } throw std::runtime_error("No NXdata found in file"); }(); NXData dataGroup = entry.openNXData(detectorName); // Usually for muon data, detector id = spectrum number // If not, the optional groups "detector_index", "detector_list" and // "detector_count" will be present to map one to the other const bool hasDetectorMapping = dataGroup.containsDataSet("detector_index") && dataGroup.containsDataSet("detector_list") && dataGroup.containsDataSet("detector_count"); if (hasDetectorMapping) { // Read detector IDs try { const auto detIndex = dataGroup.openNXInt("detector_index"); const auto detCount = dataGroup.openNXInt("detector_count"); const auto detList = dataGroup.openNXInt("detector_list"); const int nSpectra = detIndex.dim0(); for (int i = 0; i < nSpectra; ++i) { const int start = detIndex[i]; const int nDetectors = detCount[i]; std::set<int> detIDs; for (int jDet = 0; jDet < nDetectors; ++jDet) { detIDs.insert(detList[start + jDet]); } mapping[i] = detIDs; } } catch (const ::NeXus::Exception &err) { // Throw a more user-friendly message std::ostringstream message; message << "Failed to read detector mapping: " << err.what(); throw std::runtime_error(message.str()); } } else { for (int i = 0; i < nSpectra; ++i) { mapping[i] = std::set<int>{spectrumIndex[i]}; } } return mapping; }