Example #1
0
  /** 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);
  }
Example #2
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++;
    }

}
/**
 * Apply the detector test criterion
 * @param counts1 :: A workspace containing the integrated counts of the first
 * white beam run
 * @param counts2 :: A workspace containing the integrated counts of the first
 * white beam run
 * @param average :: The computed median
 * @param variation :: The allowed variation in terms of number of medians, i.e
 * those spectra where
 * the ratio of the counts outside this range will fail the tests and will be
 * masked on counts1
 * @return number of detectors for which tests failed
 */
int DetectorEfficiencyVariation::doDetectorTests(
    API::MatrixWorkspace_const_sptr counts1,
    API::MatrixWorkspace_const_sptr counts2, const double average,
    double variation) {
  // DIAG in libISIS did this.  A variation of less than 1 doesn't make sense in
  // this algorithm
  if (variation < 1) {
    variation = 1.0 / variation;
  }
  // criterion for if the the first spectrum is larger than expected
  double largest = average * variation;
  // criterion for if the the first spectrum is lower than expected
  double lowest = average / variation;

  const int numSpec = static_cast<int>(counts1->getNumberHistograms());
  const int progStep = static_cast<int>(std::ceil(numSpec / 30.0));

  // Create a workspace for the output
  MaskWorkspace_sptr maskWS = this->generateEmptyMask(counts1);

  bool checkForMask = false;
  Geometry::Instrument_const_sptr instrument = counts1->getInstrument();
  if (instrument != nullptr) {
    checkForMask = ((instrument->getSource() != nullptr) &&
                    (instrument->getSample() != nullptr));
  }

  const double deadValue(1.0);
  int numFailed(0);
  PARALLEL_FOR3(counts1, counts2, maskWS)
  for (int i = 0; i < numSpec; ++i) {
    PARALLEL_START_INTERUPT_REGION
    // move progress bar
    if (i % progStep == 0) {
      advanceProgress(progStep * static_cast<double>(RTMarkDetects) / numSpec);
      progress(m_fracDone);
      interruption_point();
    }

    if (checkForMask) {
      const std::set<detid_t> &detids =
          counts1->getSpectrum(i)->getDetectorIDs();
      if (instrument->isMonitor(detids))
        continue;
      if (instrument->isDetectorMasked(detids)) {
        // Ensure it is masked on the output
        maskWS->dataY(i)[0] = deadValue;
        continue;
      }
    }

    const double signal1 = counts1->readY(i)[0];
    const double signal2 = counts2->readY(i)[0];

    // Mask out NaN and infinite
    if (boost::math::isinf(signal1) || boost::math::isnan(signal1) ||
        boost::math::isinf(signal2) || boost::math::isnan(signal2)) {
      maskWS->dataY(i)[0] = deadValue;
      PARALLEL_ATOMIC
      ++numFailed;
      continue;
    }

    // Check the ratio is within the given range
    const double ratio = signal1 / signal2;
    if (ratio < lowest || ratio > largest) {
      maskWS->dataY(i)[0] = deadValue;
      PARALLEL_ATOMIC
      ++numFailed;
    }

    PARALLEL_END_INTERUPT_REGION
  }
  PARALLEL_CHECK_INTERUPT_REGION

  // Register the results with the ADS
  setProperty("OutputWorkspace", maskWS);

  return numFailed;
}
Example #4
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 &)
        {
          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";
  }
Example #5
0
/** 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);
  }
}