示例#1
0
/** 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++;
    }

}
示例#2
0
/** 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 &) {
        // Ignore the error if the IS is actually for a monitor
        if (!idIsMonitor(offsetsWS->getInstrument(), udet))
          numErrors++;
      }
    }

    if (doGroup) {
      try {
        groupWS->setValue(udet, double(group));
        if ((!hasGrouped) && (group > 0))
          hasGrouped = true;
      } catch (std::invalid_argument &) {
        // Ignore the error if the IS is actually for a monitor
        if (!idIsMonitor(groupWS->getInstrument(), udet))
          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 {
        // Ignore the error if the IS is actually for a monitor
        if (!idIsMonitor(maskWS->getInstrument(), 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";
}