/** Execute the algorithm. * * @throw runtime_error Thrown if algorithm cannot execute */ void SaveToSNSHistogramNexus::exec() { // NXMSetError(NULL, nexus_print_error); NXMEnableErrorReporting(); // Retrieve the filename from the properties m_inputFilename = getPropertyValue("InputFileName"); m_outputFilename = getPropertyValue("OutputFileName"); m_compress = getProperty("Compress"); inputWorkspace = getProperty("InputWorkspace"); // We'll need to get workspace indices map = inputWorkspace->getDetectorIDToWorkspaceIndexMap(); // Start the progress bar. 3 reports per histogram. prog = new Progress(this, 0, 1.0, inputWorkspace->getNumberHistograms() * 3); EventWorkspace_const_sptr eventWorkspace = boost::dynamic_pointer_cast<const EventWorkspace>(inputWorkspace); if (eventWorkspace) { eventWorkspace->sortAll(TOF_SORT, prog); } int ret; ret = this->copy_file(m_inputFilename.c_str(), NXACC_READ, m_outputFilename.c_str(), NXACC_CREATE5); if (ret == NX_ERROR) throw std::runtime_error("Nexus error while copying the file."); }
/** Executes the algorithm *@param localworkspace :: the input workspace *@param indices :: set of indices to sum up */ void SumSpectra::execEvent(EventWorkspace_const_sptr localworkspace, std::set<int> &indices) { auto outputWorkspace = create<EventWorkspace>(*localworkspace, 1); Progress progress(this, 0, 1, indices.size()); // Get the pointer to the output event list EventList &outEL = outputWorkspace->getSpectrum(0); outEL.setSpectrumNo(m_outSpecNum); outEL.clearDetectorIDs(); const auto &spectrumInfo = localworkspace->spectrumInfo(); // Loop over spectra size_t numSpectra(0); size_t numMasked(0); size_t numZeros(0); for (const auto i : indices) { // Don't go outside the range. if ((i >= m_numberOfSpectra) || (i < 0)) { g_log.error() << "Invalid index " << i << " was specified. Sum was aborted.\n"; break; } if (spectrumInfo.hasDetectors(i)) { // Skip monitors, if the property is set to do so if (!m_keepMonitors && spectrumInfo.isMonitor(i)) continue; // Skip masked detectors if (spectrumInfo.isMasked(i)) { numMasked++; continue; } } numSpectra++; // Add the event lists with the operator const EventList &tOutEL = localworkspace->getSpectrum(i); if (tOutEL.empty()) { ++numZeros; } outEL += tOutEL; progress.report(); } outputWorkspace->mutableRun().addProperty("NumAllSpectra", int(numSpectra), "", true); outputWorkspace->mutableRun().addProperty("NumMaskSpectra", int(numMasked), "", true); outputWorkspace->mutableRun().addProperty("NumZeroSpectra", int(numZeros), "", true); // Assign it to the output workspace property setProperty("OutputWorkspace", std::move(outputWorkspace)); }
void FindCenterOfMassPosition2::exec() { MatrixWorkspace_sptr inputWSWvl = getProperty("InputWorkspace"); MatrixWorkspace_sptr inputWS; // Option to exclude beam area bool direct_beam = getProperty("DirectBeam"); //TODO: Need an input for the X bin to use, assume 0 for now int specID = 0; // Initial center location double center_x = getProperty("CenterX"); double center_y = getProperty("CenterY"); const double tolerance = getProperty("Tolerance"); // Iteration cutoff int max_iteration = 200; // Radius of the beam area, in pixels double beam_radius = getProperty("BeamRadius"); // Get the number of monitors. We assume that all monitors are stored in the first spectra const int numSpec = static_cast<int>(inputWSWvl->getNumberHistograms()); // Set up the progress reporting object Progress progress(this,0.0,1.0,max_iteration); EventWorkspace_const_sptr inputEventWS = boost::dynamic_pointer_cast<const EventWorkspace>(inputWSWvl); if(inputEventWS) { std::vector<double> y_values(numSpec); std::vector<double> e_values(numSpec); PARALLEL_FOR_NO_WSP_CHECK() for (int i = 0; i < numSpec; i++) { double sum_i(0), err_i(0); progress.report("Integrating events"); const EventList& el = inputEventWS->getEventList(i); el.integrate(0,0,true,sum_i,err_i); y_values[i] = sum_i; e_values[i] = err_i; } IAlgorithm_sptr algo = createChildAlgorithm("CreateWorkspace", 0.7, 1.0); algo->setProperty< std::vector<double> >("DataX", std::vector<double>(2,0.0) ); algo->setProperty< std::vector<double> >("DataY", y_values ); algo->setProperty< std::vector<double> >("DataE", e_values ); algo->setProperty<int>("NSpec", numSpec ); algo->execute(); inputWS = algo->getProperty("OutputWorkspace"); WorkspaceFactory::Instance().initializeFromParent(inputWSWvl, inputWS, false); } else {
void ScaleX::execEvent() { g_log.information("Processing event workspace"); const MatrixWorkspace_const_sptr matrixInputWS = this->getProperty("InputWorkspace"); EventWorkspace_const_sptr inputWS = boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS); // generate the output workspace pointer API::MatrixWorkspace_sptr matrixOutputWS = this->getProperty("OutputWorkspace"); EventWorkspace_sptr outputWS; if (matrixOutputWS == matrixInputWS) outputWS = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS); else { //Make a brand new EventWorkspace outputWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create("EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); //Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, false); //You need to copy over the data as well. outputWS->copyDataFrom( (*inputWS) ); //Cast to the matrixOutputWS and save it matrixOutputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outputWS); this->setProperty("OutputWorkspace", matrixOutputWS); } int numHistograms = static_cast<int>(inputWS->getNumberHistograms()); PARALLEL_FOR1(outputWS) for (int i=0; i < numHistograms; ++i) { PARALLEL_START_INTERUPT_REGION //Do the offsetting if ((i >= wi_min) && (i <= wi_max)) { outputWS->getEventList(i).scaleTof(factor); if( factor < 0 ) { outputWS->getEventList(i).reverse(); } } m_progress->report("Scaling X"); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION outputWS->clearMRU(); }
/** Executes the algorithm *@param localworkspace :: the input workspace *@param indices :: set of indices to sum up */ void SumSpectra::execEvent(EventWorkspace_const_sptr localworkspace, std::set<int> &indices) { // Make a brand new EventWorkspace EventWorkspace_sptr outputWorkspace = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create("EventWorkspace", 1, 2, 1)); // Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent(localworkspace, outputWorkspace, true); Progress progress(this, 0, 1, indices.size()); // Get the pointer to the output event list EventList &outEL = outputWorkspace->getEventList(0); outEL.setSpectrumNo(m_outSpecId); outEL.clearDetectorIDs(); // Loop over spectra std::set<int>::iterator it; size_t numSpectra(0); size_t numMasked(0); size_t numZeros(0); // for (int i = m_minSpec; i <= m_maxSpec; ++i) for (it = indices.begin(); it != indices.end(); ++it) { int i = *it; // Don't go outside the range. if ((i >= m_numberOfSpectra) || (i < 0)) { g_log.error() << "Invalid index " << i << " was specified. Sum was aborted.\n"; break; } try { // Get the detector object for this spectrum Geometry::IDetector_const_sptr det = localworkspace->getDetector(i); // Skip monitors, if the property is set to do so if (!m_keepMonitors && det->isMonitor()) continue; // Skip masked detectors if (det->isMasked()) { numMasked++; continue; } } catch (...) { // if the detector not found just carry on } numSpectra++; // Add the event lists with the operator const EventList &tOutEL = localworkspace->getEventList(i); if (tOutEL.empty()) { ++numZeros; } outEL += tOutEL; progress.report(); } // Set all X bins on the output cow_ptr<MantidVec> XValues; XValues.access() = localworkspace->readX(0); outputWorkspace->setAllX(XValues); outputWorkspace->mutableRun().addProperty("NumAllSpectra", int(numSpectra), "", true); outputWorkspace->mutableRun().addProperty("NumMaskSpectra", int(numMasked), "", true); outputWorkspace->mutableRun().addProperty("NumZeroSpectra", int(numZeros), "", true); // Assign it to the output workspace property setProperty("OutputWorkspace", boost::dynamic_pointer_cast<MatrixWorkspace>(outputWorkspace)); }
double DiffractionEventCalibrateDetectors::intensity( double x, double y, double z, double rotx, double roty, double rotz, std::string detname, std::string inname, std::string outname, std::string peakOpt, std::string rb_param, std::string groupWSName) { EventWorkspace_sptr inputW = boost::dynamic_pointer_cast<EventWorkspace>( AnalysisDataService::Instance().retrieve(inname)); bool debug = true; CPUTimer tim; movedetector(x, y, z, rotx, roty, rotz, detname, inputW); if (debug) std::cout << tim << " to movedetector()\n"; IAlgorithm_sptr alg3 = createChildAlgorithm("ConvertUnits"); alg3->setProperty<EventWorkspace_sptr>("InputWorkspace", inputW); alg3->setPropertyValue("OutputWorkspace", outname); alg3->setPropertyValue("Target", "dSpacing"); alg3->executeAsChildAlg(); MatrixWorkspace_sptr outputW = alg3->getProperty("OutputWorkspace"); if (debug) std::cout << tim << " to ConvertUnits\n"; IAlgorithm_sptr alg4 = createChildAlgorithm("DiffractionFocussing"); alg4->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputW); alg4->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputW); alg4->setPropertyValue("GroupingFileName", ""); alg4->setPropertyValue("GroupingWorkspace", groupWSName); alg4->executeAsChildAlg(); outputW = alg4->getProperty("OutputWorkspace"); // Remove file if (debug) std::cout << tim << " to DiffractionFocussing\n"; IAlgorithm_sptr alg5 = createChildAlgorithm("Rebin"); alg5->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputW); alg5->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputW); alg5->setPropertyValue("Params", rb_param); alg5->executeAsChildAlg(); outputW = alg5->getProperty("OutputWorkspace"); if (debug) std::cout << tim << " to Rebin\n"; // Find point of peak centre const MantidVec &yValues = outputW->readY(0); auto it = std::max_element(yValues.begin(), yValues.end()); double peakHeight = *it; if (peakHeight == 0) return -0.000; double peakLoc = outputW->readX(0)[it - yValues.begin()]; IAlgorithm_sptr fit_alg; try { // set the ChildAlgorithm no to log as this will be run once per spectra fit_alg = createChildAlgorithm("Fit", -1, -1, false); } catch (Exception::NotFoundError &) { g_log.error("Can't locate Fit algorithm"); throw; } std::ostringstream fun_str; fun_str << "name=Gaussian,Height=" << peakHeight << ",Sigma=0.01,PeakCentre=" << peakLoc; fit_alg->setProperty("Function", fun_str.str()); fit_alg->setProperty("InputWorkspace", outputW); fit_alg->setProperty("WorkspaceIndex", 0); fit_alg->setProperty("StartX", outputW->readX(0)[0]); fit_alg->setProperty("EndX", outputW->readX(0)[outputW->blocksize()]); fit_alg->setProperty("MaxIterations", 200); fit_alg->setProperty("Output", "fit"); fit_alg->executeAsChildAlg(); if (debug) std::cout << tim << " to Fit\n"; std::vector<double> params; // = fit_alg->getProperty("Parameters"); Mantid::API::IFunction_sptr fun_res = fit_alg->getProperty("Function"); for (size_t i = 0; i < fun_res->nParams(); ++i) { params.push_back(fun_res->getParameter(i)); } peakHeight = params[0]; peakLoc = params[1]; movedetector(-x, -y, -z, -rotx, -roty, -rotz, detname, inputW); if (debug) std::cout << tim << " to movedetector()\n"; // Optimize C/peakheight + |peakLoc-peakOpt| where C is scaled by number of // events EventWorkspace_const_sptr inputE = boost::dynamic_pointer_cast<const EventWorkspace>(inputW); return (static_cast<int>(inputE->getNumberEvents()) / 1.e6) / peakHeight + std::fabs(peakLoc - boost::lexical_cast<double>(peakOpt)); }
/** * Execute the align detectors algorithm for an event workspace. */ void AlignDetectors::execEvent() { // g_log.information("Processing event workspace"); // the calibration information is already read in at this point // convert the input workspace into the event workspace we already know it is const MatrixWorkspace_const_sptr matrixInputWS = this->getProperty("InputWorkspace"); EventWorkspace_const_sptr inputWS = boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS); // generate the output workspace pointer API::MatrixWorkspace_sptr matrixOutputWS = this->getProperty("OutputWorkspace"); EventWorkspace_sptr outputWS; if (matrixOutputWS == matrixInputWS) outputWS = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS); else { // Make a brand new EventWorkspace outputWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create( "EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); // Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, false); // You need to copy over the data as well. outputWS->copyDataFrom((*inputWS)); // Cast to the matrixOutputWS and save it matrixOutputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outputWS); this->setProperty("OutputWorkspace", matrixOutputWS); } // Set the final unit that our output workspace will have setXAxisUnits(outputWS); ConversionFactors converter = ConversionFactors(m_calibrationWS); Progress progress(this, 0.0, 1.0, m_numberOfSpectra); PARALLEL_FOR_NO_WSP_CHECK() for (int64_t i = 0; i < m_numberOfSpectra; ++i) { PARALLEL_START_INTERUPT_REGION auto toDspacing = converter.getConversionFunc( inputWS->getSpectrum(size_t(i))->getDetectorIDs()); outputWS->getEventList(i).convertTof(toDspacing); progress.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION if (outputWS->getTofMin() < 0.) { std::stringstream msg; msg << "Something wrong with the calibration. Negative minimum d-spacing " "created. d_min = " << outputWS->getTofMin() << " d_max " << outputWS->getTofMax(); g_log.warning(msg.str()); } outputWS->clearMRU(); }
/** Executes the rebin algorithm * * @throw runtime_error Thrown if the bin range does not intersect the range of *the input workspace */ void Rebin::exec() { // Get the input workspace MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace"); MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace"); // Are we preserving event workspace-iness? bool PreserveEvents = getProperty("PreserveEvents"); // Rebinning in-place bool inPlace = (inputWS == outputWS); std::vector<double> rbParams = rebinParamsFromInput(getProperty("Params"), *inputWS, g_log); const bool dist = inputWS->isDistribution(); const bool isHist = inputWS->isHistogramData(); // workspace independent determination of length const int histnumber = static_cast<int>(inputWS->getNumberHistograms()); //------------------------------------------------------- bool fullBinsOnly = getProperty("FullBinsOnly"); MantidVecPtr XValues_new; // create new output X axis const int ntcnew = VectorHelper::createAxisFromRebinParams( rbParams, XValues_new.access(), true, fullBinsOnly); //--------------------------------------------------------------------------------- // Now, determine if the input workspace is actually an EventWorkspace EventWorkspace_const_sptr eventInputWS = boost::dynamic_pointer_cast<const EventWorkspace>(inputWS); if (eventInputWS != NULL) { //------- EventWorkspace as input ------------------------------------- EventWorkspace_sptr eventOutputWS = boost::dynamic_pointer_cast<EventWorkspace>(outputWS); if (inPlace && PreserveEvents) { // -------------Rebin in-place, preserving events // ---------------------------------------------- // This only sets the X axis. Actual rebinning will be done upon data // access. eventOutputWS->setAllX(XValues_new); this->setProperty( "OutputWorkspace", boost::dynamic_pointer_cast<MatrixWorkspace>(eventOutputWS)); } else if (!inPlace && PreserveEvents) { // -------- NOT in-place, but you want to keep events for some reason. // ---------------------- // Must copy the event workspace to a new EventWorkspace (and bin that). // Make a brand new EventWorkspace eventOutputWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create( "EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); // Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent( inputWS, eventOutputWS, false); // You need to copy over the data as well. eventOutputWS->copyDataFrom((*eventInputWS)); // This only sets the X axis. Actual rebinning will be done upon data // access. eventOutputWS->setAllX(XValues_new); // Cast to the matrixOutputWS and save it this->setProperty( "OutputWorkspace", boost::dynamic_pointer_cast<MatrixWorkspace>(eventOutputWS)); } else { //--------- Different output, OR you're inplace but not preserving Events //--- create a Workspace2D ------- g_log.information() << "Creating a Workspace2D from the EventWorkspace " << eventInputWS->getName() << ".\n"; // Create a Workspace2D // This creates a new Workspace2D through a torturous route using the // WorkspaceFactory. // The Workspace2D is created with an EMPTY CONSTRUCTOR outputWS = WorkspaceFactory::Instance().create("Workspace2D", histnumber, ntcnew, ntcnew - 1); WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, true); // Initialize progress reporting. Progress prog(this, 0.0, 1.0, histnumber); // Go through all the histograms and set the data PARALLEL_FOR3(inputWS, eventInputWS, outputWS) for (int i = 0; i < histnumber; ++i) { PARALLEL_START_INTERUPT_REGION // Set the X axis for each output histogram outputWS->setX(i, XValues_new); // Get a const event list reference. eventInputWS->dataY() doesn't work. const EventList &el = eventInputWS->getEventList(i); MantidVec y_data, e_data; // The EventList takes care of histogramming. el.generateHistogram(*XValues_new, y_data, e_data); // Copy the data over. outputWS->dataY(i).assign(y_data.begin(), y_data.end()); outputWS->dataE(i).assign(e_data.begin(), e_data.end()); // Report progress prog.report(name()); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION // Copy all the axes for (int i = 1; i < inputWS->axes(); i++) { outputWS->replaceAxis(i, inputWS->getAxis(i)->clone(outputWS.get())); outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit(); } // Copy the units over too. for (int i = 0; i < outputWS->axes(); ++i) outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit(); outputWS->setYUnit(eventInputWS->YUnit()); outputWS->setYUnitLabel(eventInputWS->YUnitLabel()); // Assign it to the output workspace property setProperty("OutputWorkspace", outputWS); } } // END ---- EventWorkspace
/** * Execute the align detectors algorithm for an event workspace. */ void AlignDetectors::execEvent() { //g_log.information("Processing event workspace"); // the calibration information is already read in at this point // convert the input workspace into the event workspace we already know it is const MatrixWorkspace_const_sptr matrixInputWS = this->getProperty("InputWorkspace"); EventWorkspace_const_sptr inputWS = boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS); // generate the output workspace pointer API::MatrixWorkspace_sptr matrixOutputWS = this->getProperty("OutputWorkspace"); EventWorkspace_sptr outputWS; if (matrixOutputWS == matrixInputWS) outputWS = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS); else { //Make a brand new EventWorkspace outputWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create("EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); //Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, false); //outputWS->mutableSpectraMap().clear(); //You need to copy over the data as well. outputWS->copyDataFrom( (*inputWS) ); //Cast to the matrixOutputWS and save it matrixOutputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outputWS); this->setProperty("OutputWorkspace", matrixOutputWS); } // Set the final unit that our output workspace will have outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("dSpacing"); const int64_t numberOfSpectra = static_cast<int64_t>(inputWS->getNumberHistograms()); // Initialise the progress reporting object Progress progress(this,0.0,1.0,numberOfSpectra); PARALLEL_FOR_NO_WSP_CHECK() for (int64_t i = 0; i < int64_t(numberOfSpectra); ++i) { PARALLEL_START_INTERUPT_REGION // Compute the conversion factor double factor = calcConversionFromMap(this->tofToDmap, inputWS->getSpectrum(size_t(i))->getDetectorIDs()); //Perform the multiplication on all events outputWS->getEventList(i).convertTof(factor); progress.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION if (outputWS->getTofMin() < 0.) { std::stringstream msg; msg << "Something wrong with the calibration. Negative minimum d-spacing created. d_min = " << outputWS->getTofMin() << " d_max " << outputWS->getTofMax(); throw std::runtime_error(msg.str()); } outputWS->clearMRU(); }
double EQSANSTofStructure::getTofOffset(EventWorkspace_const_sptr inputWS, bool frame_skipping) { //# Storage for chopper information read from the logs double chopper_set_phase[4] = {0, 0, 0, 0}; double chopper_speed[4] = {0, 0, 0, 0}; double chopper_actual_phase[4] = {0, 0, 0, 0}; double chopper_wl_1[4] = {0, 0, 0, 0}; double chopper_wl_2[4] = {0, 0, 0, 0}; double frame_wl_1 = 0; double frame_srcpulse_wl_1 = 0; double frame_wl_2 = 0; double chopper_srcpulse_wl_1[4] = {0, 0, 0, 0}; double chopper_frameskip_wl_1[4] = {0, 0, 0, 0}; double chopper_frameskip_wl_2[4] = {0, 0, 0, 0}; double chopper_frameskip_srcpulse_wl_1[4] = {0, 0, 0, 0}; // Calculate the frame width auto frequencyLog = dynamic_cast<TimeSeriesProperty<double> *>( inputWS->run().getLogData("frequency")); if (!frequencyLog) { throw std::runtime_error("Frequency log not found."); } double frequency = frequencyLog->getStatistics().mean; double tof_frame_width = 1.0e6 / frequency; double tmp_frame_width = tof_frame_width; if (frame_skipping) tmp_frame_width *= 2.0; // Choice of parameter set int m_set = 0; if (frame_skipping) m_set = 1; bool first = true; bool first_skip = true; double frameskip_wl_1 = 0; double frameskip_srcpulse_wl_1 = 0; double frameskip_wl_2 = 0; for (int i = 0; i < 4; i++) { // Read chopper information std::ostringstream phase_str; phase_str << "Phase" << i + 1; auto log = dynamic_cast<TimeSeriesProperty<double> *>( inputWS->run().getLogData(phase_str.str())); if (!log) { throw std::runtime_error("Phase log not found."); } chopper_set_phase[i] = log->getStatistics().mean; std::ostringstream speed_str; speed_str << "Speed" << i + 1; log = dynamic_cast<TimeSeriesProperty<double> *>( inputWS->run().getLogData(speed_str.str())); if (!log) { throw std::runtime_error("Speed log not found."); } chopper_speed[i] = log->getStatistics().mean; // Only process choppers with non-zero speed if (chopper_speed[i] <= 0) continue; chopper_actual_phase[i] = chopper_set_phase[i] - CHOPPER_PHASE_OFFSET[m_set][i]; while (chopper_actual_phase[i] < 0) chopper_actual_phase[i] += tmp_frame_width; double x1 = (chopper_actual_phase[i] - (tmp_frame_width * 0.5 * CHOPPER_ANGLE[i] / 360.)); // opening edge double x2 = (chopper_actual_phase[i] + (tmp_frame_width * 0.5 * CHOPPER_ANGLE[i] / 360.)); // closing edge if (!frame_skipping) // not skipping { while (x1 < 0) { x1 += tmp_frame_width; x2 += tmp_frame_width; } } if (x1 > 0) { chopper_wl_1[i] = 3.9560346 * x1 / CHOPPER_LOCATION[i]; chopper_srcpulse_wl_1[i] = 3.9560346 * (x1 - chopper_wl_1[i] * PULSEWIDTH) / CHOPPER_LOCATION[i]; } else chopper_wl_1[i] = chopper_srcpulse_wl_1[i] = 0.; if (x2 > 0) chopper_wl_2[i] = 3.9560346 * x2 / CHOPPER_LOCATION[i]; else chopper_wl_2[i] = 0.; if (first) { frame_wl_1 = chopper_wl_1[i]; frame_srcpulse_wl_1 = chopper_srcpulse_wl_1[i]; frame_wl_2 = chopper_wl_2[i]; first = false; } else { if (frame_skipping && i == 2) // ignore chopper 1 and 2 forthe shortest wl. { frame_wl_1 = chopper_wl_1[i]; frame_srcpulse_wl_1 = chopper_srcpulse_wl_1[i]; } if (frame_wl_1 < chopper_wl_1[i]) frame_wl_1 = chopper_wl_1[i]; if (frame_wl_2 > chopper_wl_2[i]) frame_wl_2 = chopper_wl_2[i]; if (frame_srcpulse_wl_1 < chopper_srcpulse_wl_1[i]) frame_srcpulse_wl_1 = chopper_srcpulse_wl_1[i]; } if (frame_skipping) { if (x1 > 0) { x1 += tof_frame_width; // skipped pulse chopper_frameskip_wl_1[i] = 3.9560346 * x1 / CHOPPER_LOCATION[i]; chopper_frameskip_srcpulse_wl_1[i] = 3.9560346 * (x1 - chopper_wl_1[i] * PULSEWIDTH) / CHOPPER_LOCATION[i]; } else chopper_wl_1[i] = chopper_srcpulse_wl_1[i] = 0.; if (x2 > 0) { x2 += tof_frame_width; chopper_frameskip_wl_2[i] = 3.9560346 * x2 / CHOPPER_LOCATION[i]; } else chopper_wl_2[i] = 0.; if (i < 2 && chopper_frameskip_wl_1[i] > chopper_frameskip_wl_2[i]) continue; if (first_skip) { frameskip_wl_1 = chopper_frameskip_wl_1[i]; frameskip_srcpulse_wl_1 = chopper_frameskip_srcpulse_wl_1[i]; frameskip_wl_2 = chopper_frameskip_wl_2[i]; first_skip = false; } else { if (i == 2) // ignore chopper 1 and 2 forthe longest wl. frameskip_wl_2 = chopper_frameskip_wl_2[i]; if (chopper_frameskip_wl_1[i] < chopper_frameskip_wl_2[i] && frameskip_wl_1 < chopper_frameskip_wl_1[i]) frameskip_wl_1 = chopper_frameskip_wl_1[i]; if (chopper_frameskip_wl_1[i] < chopper_frameskip_wl_2[i] && frameskip_srcpulse_wl_1 < chopper_frameskip_srcpulse_wl_1[i]) frameskip_srcpulse_wl_1 = chopper_frameskip_srcpulse_wl_1[i]; if (frameskip_wl_2 > chopper_frameskip_wl_2[i]) frameskip_wl_2 = chopper_frameskip_wl_2[i]; } } } if (frame_wl_1 >= frame_wl_2) // too many frames later. So figure it out { double n_frame[4] = {0, 0, 0, 0}; double c_wl_1[4] = {0, 0, 0, 0}; double c_wl_2[4] = {0, 0, 0, 0}; bool passed = false; do { frame_wl_1 = c_wl_1[0] = chopper_wl_1[0] + 3.9560346 * n_frame[0] * tof_frame_width / CHOPPER_LOCATION[0]; frame_wl_2 = c_wl_2[0] = chopper_wl_2[0] + 3.9560346 * n_frame[0] * tof_frame_width / CHOPPER_LOCATION[0]; for (int i = 1; i < 4; i++) { n_frame[i] = n_frame[i - 1] - 1; passed = false; do { n_frame[i] += 1; c_wl_1[i] = chopper_wl_1[i] + 3.9560346 * n_frame[i] * tof_frame_width / CHOPPER_LOCATION[i]; c_wl_2[i] = chopper_wl_2[i] + 3.9560346 * n_frame[i] * tof_frame_width / CHOPPER_LOCATION[i]; if (frame_wl_1 < c_wl_2[i] && frame_wl_2 > c_wl_1[i]) { passed = true; break; } if (frame_wl_2 < c_wl_1[i]) break; // over shot } while (n_frame[i] - n_frame[i - 1] < 10); if (!passed) { n_frame[0] += 1; break; } else { if (frame_wl_1 < c_wl_1[i]) frame_wl_1 = c_wl_1[i]; if (frame_wl_2 > c_wl_2[i]) frame_wl_2 = c_wl_2[i]; } } } while (!passed && n_frame[0] < 99); if (frame_wl_2 > frame_wl_1) { int n = 3; if (c_wl_1[2] > c_wl_1[3]) n = 2; frame_srcpulse_wl_1 = c_wl_1[n] - 3.9560346 * c_wl_1[n] * PULSEWIDTH / CHOPPER_LOCATION[n]; for (int i = 0; i < 4; i++) { chopper_wl_1[i] = c_wl_1[i]; chopper_wl_2[i] = c_wl_2[i]; if (frame_skipping) { chopper_frameskip_wl_1[i] = c_wl_1[i] + 3.9560346 * 2. * tof_frame_width / CHOPPER_LOCATION[i]; chopper_frameskip_wl_2[i] = c_wl_2[i] + 3.9560346 * 2. * tof_frame_width / CHOPPER_LOCATION[i]; if (i == 0) { frameskip_wl_1 = chopper_frameskip_wl_1[i]; frameskip_wl_2 = chopper_frameskip_wl_2[i]; } else { if (frameskip_wl_1 < chopper_frameskip_wl_1[i]) frameskip_wl_1 = chopper_frameskip_wl_1[i]; if (frameskip_wl_2 > chopper_frameskip_wl_2[i]) frameskip_wl_2 = chopper_frameskip_wl_2[i]; } } } } else frame_srcpulse_wl_1 = 0.0; } // Get source and detector locations // get the name of the mapping file as set in the parameter files std::vector<std::string> temp = inputWS->getInstrument()->getStringParameter("detector-name"); std::string det_name = "detector1"; if (temp.empty()) g_log.information() << "The instrument parameter file does not contain the " "'detector-name' parameter: trying 'detector1'"; else det_name = temp[0]; double source_z = inputWS->getInstrument()->getSource()->getPos().Z(); double detector_z = inputWS->getInstrument()->getComponentByName(det_name)->getPos().Z(); double source_to_detector = (detector_z - source_z) * 1000.0; frame_tof0 = frame_srcpulse_wl_1 / 3.9560346 * source_to_detector; g_log.information() << "Frame width " << tmp_frame_width << '\n'; g_log.information() << "TOF offset = " << frame_tof0 << " microseconds\n"; g_log.information() << "Band defined by T1-T4 " << frame_wl_1 << " " << frame_wl_2; if (frame_skipping) g_log.information() << " + " << frameskip_wl_1 << " " << frameskip_wl_2 << '\n'; else g_log.information() << '\n'; g_log.information() << "Chopper Actual Phase Lambda1 Lambda2\n"; for (int i = 0; i < 4; i++) g_log.information() << i << " " << chopper_actual_phase[i] << " " << chopper_wl_1[i] << " " << chopper_wl_2[i] << '\n'; double low_wl_discard = 3.9560346 * low_tof_cut / source_to_detector; double high_wl_discard = 3.9560346 * high_tof_cut / source_to_detector; setProperty("FrameSkipping", frame_skipping); setProperty("TofOffset", frame_tof0); setProperty("WavelengthMin", frame_wl_1 + low_wl_discard); setProperty("WavelengthMax", frame_wl_2 - high_wl_discard); if (frame_skipping) { setProperty("WavelengthMinFrame2", frameskip_wl_1 + low_wl_discard); setProperty("WavelengthMaxFrame2", frameskip_wl_2 - high_wl_discard); } return frame_tof0; }
/** Execute the algorithm. */ void ResampleX::exec() { // generically having access to the input workspace is a good idea MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace"); MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace"); bool inPlace = (inputWS == outputWS); // Rebinning in-place m_isDistribution = inputWS->isDistribution(); m_isHistogram = inputWS->isHistogramData(); int numSpectra = static_cast<int>(inputWS->getNumberHistograms()); // the easy parameters m_useLogBinning = getProperty("LogBinning"); m_numBins = getProperty("NumberBins"); m_preserveEvents = getProperty("PreserveEvents"); // determine the xmin/xmax for the workspace vector<double> xmins = getProperty("XMin"); vector<double> xmaxs = getProperty("XMax"); string error = determineXMinMax(inputWS, xmins, xmaxs); if (!error.empty()) throw std::runtime_error(error); bool common_limits = true; { double xmin_common = xmins[0]; double xmax_common = xmaxs[0]; for (size_t i = 1; i < xmins.size(); ++i) { if (xmins[i] != xmin_common) { common_limits = false; break; } if (xmaxs[i] != xmax_common) { common_limits = false; break; } } } if (common_limits) { g_log.debug() << "Common limits between all spectra\n"; } else { g_log.debug() << "Does not have common limits between all spectra\n"; } // start doing actual work EventWorkspace_const_sptr inputEventWS = boost::dynamic_pointer_cast<const EventWorkspace>(inputWS); if (inputEventWS != NULL) { if (m_preserveEvents) { EventWorkspace_sptr outputEventWS = boost::dynamic_pointer_cast<EventWorkspace>(outputWS); if (inPlace) { g_log.debug() << "Rebinning event workspace in place\n"; } else { g_log.debug() << "Rebinning event workspace out of place\n"; // copy the event workspace to a new EventWorkspace outputEventWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create( "EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); // copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent( inputEventWS, outputEventWS, false); // copy over the data as well. outputEventWS->copyDataFrom((*inputEventWS)); } if (common_limits) { // get the delta from the first since they are all the same MantidVecPtr xValues; double delta = this->determineBinning(xValues.access(), xmins[0], xmaxs[0]); g_log.debug() << "delta = " << delta << "\n"; outputEventWS->setAllX(xValues); } else { // initialize progress reporting. Progress prog(this, 0.0, 1.0, numSpectra); // do the rebinning PARALLEL_FOR2(inputEventWS, outputWS) for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) { PARALLEL_START_INTERUPT_REGION MantidVec xValues; double delta = this->determineBinning(xValues, xmins[wkspIndex], xmaxs[wkspIndex]); g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << " xmin=" << xmins[wkspIndex] << " xmax=" << xmaxs[wkspIndex] << "\n"; outputEventWS->getSpectrum(wkspIndex)->setX(xValues); prog.report(name()); // Report progress PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION } this->setProperty( "OutputWorkspace", boost::dynamic_pointer_cast<MatrixWorkspace>(outputEventWS)); } // end if (m_preserveEvents) else // event workspace -> matrix workspace { //--------- Different output, OR you're inplace but not preserving Events //--- create a Workspace2D ------- g_log.information() << "Creating a Workspace2D from the EventWorkspace " << inputEventWS->getName() << ".\n"; // Create a Workspace2D // This creates a new Workspace2D through a torturous route using the // WorkspaceFactory. // The Workspace2D is created with an EMPTY CONSTRUCTOR outputWS = WorkspaceFactory::Instance().create("Workspace2D", numSpectra, m_numBins, m_numBins - 1); WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, true); // Initialize progress reporting. Progress prog(this, 0.0, 1.0, numSpectra); // Go through all the histograms and set the data PARALLEL_FOR2(inputEventWS, outputWS) for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) { PARALLEL_START_INTERUPT_REGION // Set the X axis for each output histogram MantidVec xValues; double delta = this->determineBinning(xValues, xmins[wkspIndex], xmaxs[wkspIndex]); g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << "\n"; outputWS->setX(wkspIndex, xValues); // Get a const event list reference. inputEventWS->dataY() doesn't work. const EventList &el = inputEventWS->getEventList(wkspIndex); MantidVec y_data, e_data; // The EventList takes care of histogramming. el.generateHistogram(xValues, y_data, e_data); // Copy the data over. outputWS->dataY(wkspIndex).assign(y_data.begin(), y_data.end()); outputWS->dataE(wkspIndex).assign(e_data.begin(), e_data.end()); // Report progress prog.report(name()); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION // Copy all the axes for (int i = 1; i < inputWS->axes(); i++) { outputWS->replaceAxis(i, inputWS->getAxis(i)->clone(outputWS.get())); outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit(); } // Copy the units over too. for (int i = 0; i < outputWS->axes(); ++i) outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit(); outputWS->setYUnit(inputEventWS->YUnit()); outputWS->setYUnitLabel(inputEventWS->YUnitLabel()); // Assign it to the output workspace property setProperty("OutputWorkspace", outputWS); } return; } else // (inputeventWS != NULL)
/** Execute the algorithm. */ void ResampleX::exec() { // generically having access to the input workspace is a good idea MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace"); MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace"); bool inPlace = (inputWS == outputWS); // Rebinning in-place m_isDistribution = inputWS->isDistribution(); m_isHistogram = inputWS->isHistogramData(); const int numSpectra = static_cast<int>(inputWS->getNumberHistograms()); // the easy parameters m_useLogBinning = getProperty("LogBinning"); m_numBins = getProperty("NumberBins"); m_preserveEvents = getProperty("PreserveEvents"); // determine the xmin/xmax for the workspace vector<double> xmins = getProperty("XMin"); vector<double> xmaxs = getProperty("XMax"); string error = determineXMinMax(inputWS, xmins, xmaxs); if (!error.empty()) throw std::runtime_error(error); bool common_limits = true; { double xmin_common = xmins[0]; double xmax_common = xmaxs[0]; for (size_t i = 1; i < xmins.size(); ++i) { if (xmins[i] != xmin_common) { common_limits = false; break; } if (xmaxs[i] != xmax_common) { common_limits = false; break; } } } if (common_limits) { g_log.debug() << "Common limits between all spectra\n"; } else { g_log.debug() << "Does not have common limits between all spectra\n"; } // start doing actual work EventWorkspace_const_sptr inputEventWS = boost::dynamic_pointer_cast<const EventWorkspace>(inputWS); if (inputEventWS != nullptr) { if (m_preserveEvents) { if (inPlace) { g_log.debug() << "Rebinning event workspace in place\n"; } else { g_log.debug() << "Rebinning event workspace out of place\n"; outputWS = inputWS->clone(); } auto outputEventWS = boost::dynamic_pointer_cast<EventWorkspace>(outputWS); if (common_limits) { // get the delta from the first since they are all the same BinEdges xValues(0); const double delta = this->determineBinning(xValues.mutableRawData(), xmins[0], xmaxs[0]); g_log.debug() << "delta = " << delta << "\n"; outputEventWS->setAllX(xValues); } else { // initialize progress reporting. Progress prog(this, 0.0, 1.0, numSpectra); // do the rebinning PARALLEL_FOR_IF(Kernel::threadSafe(*inputEventWS, *outputWS)) for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) { PARALLEL_START_INTERUPT_REGION BinEdges xValues(0); const double delta = this->determineBinning( xValues.mutableRawData(), xmins[wkspIndex], xmaxs[wkspIndex]); g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << " xmin=" << xmins[wkspIndex] << " xmax=" << xmaxs[wkspIndex] << "\n"; outputEventWS->setHistogram(wkspIndex, xValues); prog.report(name()); // Report progress PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION } } // end if (m_preserveEvents) else // event workspace -> matrix workspace { //--------- Different output, OR you're inplace but not preserving Events g_log.information() << "Creating a Workspace2D from the EventWorkspace " << inputEventWS->getName() << ".\n"; outputWS = create<DataObjects::Workspace2D>( *inputWS, numSpectra, HistogramData::BinEdges(m_numBins + 1)); // Initialize progress reporting. Progress prog(this, 0.0, 1.0, numSpectra); // Go through all the histograms and set the data PARALLEL_FOR_IF(Kernel::threadSafe(*inputEventWS, *outputWS)) for (int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) { PARALLEL_START_INTERUPT_REGION // Set the X axis for each output histogram MantidVec xValues; const double delta = this->determineBinning(xValues, xmins[wkspIndex], xmaxs[wkspIndex]); g_log.debug() << "delta[wkspindex=" << wkspIndex << "] = " << delta << "\n"; outputWS->setBinEdges(wkspIndex, xValues); // Get a const event list reference. inputEventWS->dataY() doesn't work. const EventList &el = inputEventWS->getSpectrum(wkspIndex); MantidVec y_data, e_data; // The EventList takes care of histogramming. el.generateHistogram(xValues, y_data, e_data); // Copy the data over. outputWS->mutableY(wkspIndex) = std::move(y_data); outputWS->mutableE(wkspIndex) = std::move(e_data); // Report progress prog.report(name()); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION // Copy all the axes for (int i = 1; i < inputWS->axes(); i++) { outputWS->replaceAxis(i, inputWS->getAxis(i)->clone(outputWS.get())); outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit(); } // Copy the units over too. for (int i = 0; i < outputWS->axes(); ++i) { outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit(); } outputWS->setYUnit(inputEventWS->YUnit()); outputWS->setYUnitLabel(inputEventWS->YUnitLabel()); } // Assign it to the output workspace property setProperty("OutputWorkspace", outputWS); return; } else // (inputeventWS != NULL)
/** Executes the algorithm * * @throw Exception::FileError If the grouping file cannot be opened or read successfully */ void GetDetOffsetsMultiPeaks::exec() { const double BAD_OFFSET(1000.); // mark things that didn't work with this MatrixWorkspace_sptr inputW=getProperty("InputWorkspace"); double maxOffset=getProperty("MaxOffset"); int nspec=static_cast<int>(inputW->getNumberHistograms()); // Create the output OffsetsWorkspace OffsetsWorkspace_sptr outputW(new OffsetsWorkspace(inputW->getInstrument())); // determine min/max d-spacing of the workspace double wkspDmin, wkspDmax; inputW->getXMinMax(wkspDmin, wkspDmax); // Create the output MaskWorkspace MatrixWorkspace_sptr maskWS(new MaskWorkspace(inputW->getInstrument())); //To get the workspace index from the detector ID detid2index_map * pixel_to_wi = maskWS->getDetectorIDToWorkspaceIndexMap(true); // the peak positions and where to fit std::vector<double> peakPositions = getProperty("DReference"); std::sort(peakPositions.begin(), peakPositions.end()); std::vector<double> fitWindows = generateWindows(wkspDmin, wkspDmax, peakPositions, this->getProperty("FitWindowMaxWidth")); g_log.information() << "windows : "; if (fitWindows.empty()) { g_log.information() << "empty\n"; } else { for (std::vector<double>::const_iterator it = fitWindows.begin(); it != fitWindows.end(); ++it) g_log.information() << *it << " "; g_log.information() << "\n"; } // some shortcuts for event workspaces EventWorkspace_const_sptr eventW = boost::dynamic_pointer_cast<const EventWorkspace>( inputW ); bool isEvent = false; if (eventW) isEvent = true; // cache the peak and background function names m_peakType = this->getPropertyValue("PeakFunction"); m_backType = this->getPropertyValue("BackgroundType"); // the maximum allowable chisq value for an individual peak fit m_maxChiSq = this->getProperty("MaxChiSq"); // Fit all the spectra with a gaussian Progress prog(this, 0, 1.0, nspec); // cppcheck-suppress syntaxError PRAGMA_OMP(parallel for schedule(dynamic, 1) ) for (int wi=0;wi<nspec;++wi) { PARALLEL_START_INTERUPT_REGION double offset = 0.0; double fitSum = 0.0; // checks for dead detectors if ((isEvent) && (eventW->getEventList(wi).empty())) { // dead detector will be masked offset = BAD_OFFSET; } else { const MantidVec& Y = inputW->readY(wi); const int YLength = static_cast<int>(Y.size()); double sumY = 0.0; for (int i = 0; i < YLength; i++) sumY += Y[i]; if (sumY < 1.e-30) { // Dead detector will be masked offset=BAD_OFFSET; } } if (offset < 10.) { // Fit the peak std::vector<double> peakPosToFit, peakPosFitted, chisq; size_t nparams; double minD, maxD; fitSpectra(wi, inputW, peakPositions, fitWindows, nparams, minD, maxD, peakPosToFit, peakPosFitted, chisq); if (nparams > 0) { //double * params = new double[2*nparams+1]; double params[153]; if(nparams > 50) nparams = 50; params[0] = static_cast<double>(nparams); params[1] = minD; params[2] = maxD; for (size_t i = 0; i < nparams; i++) { params[i+3] = peakPosToFit[i]; } for (size_t i = 0; i < nparams; i++) { params[i+3+nparams] = peakPosFitted[i]; } for (size_t i = 0; i < nparams; i++) { params[i+3+2*nparams] = chisq[i]; } const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex; gsl_multimin_fminimizer *s = NULL; gsl_vector *ss, *x; gsl_multimin_function minex_func; // finally do the fitting size_t nopt = 1; size_t iter = 0; int status = 0; double size; /* Starting point */ x = gsl_vector_alloc (nopt); gsl_vector_set_all (x, 0.0); /* Set initial step sizes to 0.001 */ ss = gsl_vector_alloc (nopt); gsl_vector_set_all (ss, 0.001); /* Initialize method and iterate */ minex_func.n = nopt; minex_func.f = &gsl_costFunction; minex_func.params = ¶ms; s = gsl_multimin_fminimizer_alloc (T, nopt); gsl_multimin_fminimizer_set (s, &minex_func, x, ss); do { iter++; status = gsl_multimin_fminimizer_iterate(s); if (status) break; size = gsl_multimin_fminimizer_size (s); status = gsl_multimin_test_size (size, 1e-4); } while (status == GSL_CONTINUE && iter < 50); // Output summary to log file std::string reportOfDiffractionEventCalibrateDetectors = gsl_strerror(status); g_log.debug() << " Workspace Index = " << wi << " Method used = " << " Simplex" << " Iteration = " << iter << " Status = " << reportOfDiffractionEventCalibrateDetectors << " Minimize Sum = " << s->fval << " Offset = " << gsl_vector_get (s->x, 0) << " \n"; offset = gsl_vector_get (s->x, 0); fitSum = s->fval; gsl_vector_free(x); gsl_vector_free(ss); gsl_multimin_fminimizer_free (s); //delete [] params; } else { offset = BAD_OFFSET; } } double mask=0.0; if (std::abs(offset) > maxOffset) { offset = 0.0; mask = 1.0; } // Get the list of detectors in this pixel const std::set<detid_t> & dets = inputW->getSpectrum(wi)->getDetectorIDs(); // Most of the exec time is in FitSpectra, so this critical block should not be a problem. PARALLEL_CRITICAL(GetDetOffsetsMultiPeaks_setValue) { // Use the same offset for all detectors from this pixel std::set<detid_t>::iterator it; for (it = dets.begin(); it != dets.end(); ++it) { outputW->setValue(*it, offset, fitSum); if (mask == 1.) { // Being masked maskWS->maskWorkspaceIndex((*pixel_to_wi)[*it]); maskWS->dataY((*pixel_to_wi)[*it])[0] = mask; } else { // Using the detector maskWS->dataY((*pixel_to_wi)[*it])[0] = mask; } } } prog.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION // Return the output setProperty("OutputWorkspace",outputW); setProperty("MaskWorkspace",maskWS); // Also save to .cal file, if requested std::string filename=getProperty("GroupingFileName"); if (!filename.empty()) { progress(0.9, "Saving .cal file"); IAlgorithm_sptr childAlg = createChildAlgorithm("SaveCalFile"); childAlg->setProperty("OffsetsWorkspace", outputW); childAlg->setProperty("MaskWorkspace", maskWS); childAlg->setPropertyValue("Filename", filename); childAlg->executeAsChildAlg(); } }
/** Executes the algorithm */ void FilterByTime::exec() { EventWorkspace_const_sptr inputWS = this->getProperty("InputWorkspace"); // ---- Find the start/end times ---- DateAndTime start, stop; double start_dbl, stop_dbl; start_dbl = getProperty("StartTime"); stop_dbl = getProperty("StopTime"); std::string start_str, stop_str; start_str = getPropertyValue("AbsoluteStartTime"); stop_str = getPropertyValue("AbsoluteStopTime"); if ( (start_str != "") && (stop_str != "") && (start_dbl <= 0.0) && (stop_dbl <= 0.0) ) { // Use the absolute string start = DateAndTime( start_str ); stop = DateAndTime( stop_str ); } else if ( (start_str == "") && (stop_str == "") && ((start_dbl > 0.0) || (stop_dbl > 0.0)) ) { // Use the relative times in seconds. DateAndTime first = inputWS->getFirstPulseTime(); DateAndTime last = inputWS->getLastPulseTime(); start = first + start_dbl; if (stop_dbl > 0.0) { stop = first + stop_dbl; } else { this->getLogger().debug() << "No end filter time specified - assuming last pulse" << std::endl; stop = last + 10000.0; // so we get all events - needs to be past last pulse } } else { //Either both or none were specified throw std::invalid_argument("You need to specify either the StartTime or StopTime parameters; or both the AbsoluteStartTime and AbsoluteStopTime parameters; but not other combinations."); } if (stop <= start) throw std::invalid_argument("The stop time should be larger than the start time."); // Make a brand new EventWorkspace EventWorkspace_sptr outputWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create("EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); // Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, false); // But we don't copy the data. setProperty("OutputWorkspace", outputWS); size_t numberOfSpectra = inputWS->getNumberHistograms(); // Initialise the progress reporting object Progress prog(this,0.0,1.0,numberOfSpectra); // Loop over the histograms (detector spectra) PARALLEL_FOR_NO_WSP_CHECK() for (int64_t i = 0; i < int64_t(numberOfSpectra); ++i) { PARALLEL_START_INTERUPT_REGION //Get the output event list (should be empty) EventList& output_el = outputWS->getEventList(i); //and this is the input event list const EventList& input_el = inputWS->getEventList(i); //Perform the filtering input_el.filterByPulseTime(start, stop, output_el); prog.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION //Now filter out the run, using the DateAndTime type. outputWS->mutableRun().filterByTime(start, stop); }
void ModeratorTzero::execEvent(const std::string &emode) { g_log.information("Processing event workspace"); const MatrixWorkspace_const_sptr matrixInputWS = getProperty("InputWorkspace"); EventWorkspace_const_sptr inputWS = boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS); // generate the output workspace pointer const size_t numHists = static_cast<size_t>(inputWS->getNumberHistograms()); Mantid::API::MatrixWorkspace_sptr matrixOutputWS = getProperty("OutputWorkspace"); EventWorkspace_sptr outputWS; if (matrixOutputWS == matrixInputWS) { outputWS = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS); } else { // Make a brand new EventWorkspace outputWS = boost::dynamic_pointer_cast<EventWorkspace>( WorkspaceFactory::Instance().create("EventWorkspace", numHists, 2, 1)); // Copy geometry over. WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, false); // You need to copy over the data as well. outputWS->copyDataFrom((*inputWS)); // Cast to the matrixOutputWS and save it matrixOutputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outputWS); setProperty("OutputWorkspace", matrixOutputWS); } // Get pointers to sample and source IComponent_const_sptr source = m_instrument->getSource(); IComponent_const_sptr sample = m_instrument->getSample(); double Lss = source->getDistance(*sample); // distance from source to sample // calculate tof shift once for all neutrons if emode==Direct double t0_direct(-1); if (emode == "Direct") { Kernel::Property *eiprop = inputWS->run().getProperty("Ei"); double Ei = boost::lexical_cast<double>(eiprop->value()); mu::Parser parser; parser.DefineVar("incidentEnergy", &Ei); // associate E1 to this parser parser.SetExpr(m_formula); t0_direct = parser.Eval(); } // Loop over the spectra Progress prog(this, 0.0, 1.0, numHists); // report progress of algorithm PARALLEL_FOR1(outputWS) for (int i = 0; i < static_cast<int>(numHists); ++i) { PARALLEL_START_INTERUPT_REGION size_t wsIndex = static_cast<size_t>(i); EventList &evlist = outputWS->getEventList(wsIndex); if (evlist.getNumberEvents() > 0) // don't bother with empty lists { IDetector_const_sptr det; double L1(Lss); // distance from source to sample double L2(-1); // distance from sample to detector try { det = inputWS->getDetector(i); if (det->isMonitor()) { // redefine the sample as the monitor L1 = source->getDistance(*det); L2 = 0; } else { L2 = sample->getDistance(*det); } } catch (Exception::NotFoundError &) { g_log.error() << "Unable to calculate distances to/from detector" << i << std::endl; } if (L2 >= 0) { // One parser for each parallel processor needed (except Edirect mode) double E1; mu::Parser parser; parser.DefineVar("incidentEnergy", &E1); // associate E1 to this parser parser.SetExpr(m_formula); // fast neutrons are shifted by min_t0_next, irrespective of tof double v1_max = L1 / m_t1min; E1 = m_convfactor * v1_max * v1_max; double min_t0_next = parser.Eval(); if (emode == "Indirect") { double t2(-1.0); // time from sample to detector. (-1) signals error if (det->isMonitor()) { t2 = 0.0; } else { static const double convFact = 1.0e-6 * sqrt(2 * PhysicalConstants::meV / PhysicalConstants::NeutronMass); std::vector<double> wsProp = det->getNumberParameter("Efixed"); if (!wsProp.empty()) { double E2 = wsProp.at(0); //[E2]=meV double v2 = convFact * sqrt(E2); //[v2]=meter/microsec t2 = L2 / v2; } else { // t2 is kept to -1 if no Efixed is found g_log.debug() << "Efixed not found for detector " << i << std::endl; } } if (t2 >= 0) // t2 < 0 when no detector info is available { // fix the histogram bins MantidVec &x = evlist.dataX(); for (double &tof : x) { if (tof < m_t1min + t2) tof -= min_t0_next; else tof -= CalculateT0indirect(tof, L1, t2, E1, parser); } MantidVec tofs = evlist.getTofs(); for (double &tof : tofs) { if (tof < m_t1min + t2) tof -= min_t0_next; else tof -= CalculateT0indirect(tof, L1, t2, E1, parser); } evlist.setTofs(tofs); evlist.setSortOrder(Mantid::DataObjects::EventSortType::UNSORTED); } // end of if( t2>= 0) } // end of if(emode=="Indirect") else if (emode == "Elastic") { // Apply t0 correction to histogram bins MantidVec &x = evlist.dataX(); for (double &tof : x) { if (tof < m_t1min * (L1 + L2) / L1) tof -= min_t0_next; else tof -= CalculateT0elastic(tof, L1 + L2, E1, parser); } MantidVec tofs = evlist.getTofs(); for (double &tof : tofs) { // add a [-0.1,0.1] microsecond noise to avoid artifacts // resulting from original tof data if (tof < m_t1min * (L1 + L2) / L1) tof -= min_t0_next; else tof -= CalculateT0elastic(tof, L1 + L2, E1, parser); } evlist.setTofs(tofs); evlist.setSortOrder(Mantid::DataObjects::EventSortType::UNSORTED); MantidVec tofs_b = evlist.getTofs(); MantidVec xarray = evlist.readX(); } // end of else if(emode=="Elastic") else if (emode == "Direct") { // fix the histogram bins MantidVec &x = evlist.dataX(); for (double &tof : x) { tof -= t0_direct; } MantidVec tofs = evlist.getTofs(); for (double &tof : tofs) { tof -= t0_direct; } evlist.setTofs(tofs); evlist.setSortOrder(Mantid::DataObjects::EventSortType::UNSORTED); } // end of else if(emode=="Direct") } // end of if(L2 >= 0) } // end of if (evlist.getNumberEvents() > 0) prog.report(); PARALLEL_END_INTERUPT_REGION } // end of for (int i = 0; i < static_cast<int>(numHists); ++i) PARALLEL_CHECK_INTERUPT_REGION outputWS->clearMRU(); // Clears the Most Recent Used lists */ } // end of void ModeratorTzero::execEvent()
/// Executes the algorithm for events void UnaryOperation::execEvent() { g_log.information("Processing event workspace"); const MatrixWorkspace_const_sptr matrixInputWS = this->getProperty(inputPropName()); EventWorkspace_const_sptr inputWS = boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS); // generate the output workspace pointer API::MatrixWorkspace_sptr matrixOutputWS = this->getProperty(outputPropName()); EventWorkspace_sptr outputWS; if (matrixOutputWS == matrixInputWS) { outputWS = boost::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS); } else { // Make a brand new EventWorkspace outputWS = boost::dynamic_pointer_cast<EventWorkspace>( API::WorkspaceFactory::Instance().create( "EventWorkspace", inputWS->getNumberHistograms(), 2, 1)); // Copy geometry over. API::WorkspaceFactory::Instance().initializeFromParent(inputWS, outputWS, false); // You need to copy over the data as well. outputWS->copyDataFrom((*inputWS)); // Cast to the matrixOutputWS and save it matrixOutputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outputWS); this->setProperty("OutputWorkspace", matrixOutputWS); } // Now fetch any properties defined by concrete algorithm retrieveProperties(); int64_t numHistograms = static_cast<int64_t>(inputWS->getNumberHistograms()); API::Progress prog = API::Progress(this, 0.0, 1.0, numHistograms); PARALLEL_FOR1(outputWS) for (int64_t i = 0; i < numHistograms; ++i) { PARALLEL_START_INTERUPT_REGION // switch to weighted events if needed, and use the appropriate helper // function EventList *evlist = outputWS->getEventListPtr(i); switch (evlist->getEventType()) { case TOF: // Switch to weights if needed. evlist->switchTo(WEIGHTED); /* no break */ // Fall through case WEIGHTED: unaryOperationEventHelper(evlist->getWeightedEvents()); break; case WEIGHTED_NOTIME: unaryOperationEventHelper(evlist->getWeightedEventsNoTime()); break; } prog.report(); PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION outputWS->clearMRU(); if (inputWS->getNumberEvents() != outputWS->getNumberEvents()) { g_log.information() << "Number of events has changed!!!" << std::endl; } }
/** Executes the algorithm * * @throw runtime_error Thrown if algorithm cannot execute */ void DiffractionEventCalibrateDetectors::exec() { // Try to retrieve optional properties const int maxIterations = getProperty("MaxIterations"); const double peakOpt = getProperty("LocationOfPeakToOptimize"); // Get the input workspace EventWorkspace_const_sptr inputW = getProperty("InputWorkspace"); // retrieve the properties const std::string rb_params=getProperty("Params"); //Get some stuff from the input workspace Instrument_const_sptr inst = inputW->getInstrument(); //Build a list of Rectangular Detectors std::vector<boost::shared_ptr<RectangularDetector> > detList; // --------- Loading only one bank ---------------------------------- std::string onebank = getProperty("BankName"); bool doOneBank = (onebank != ""); for (int i=0; i < inst->nelements(); i++) { boost::shared_ptr<RectangularDetector> det; boost::shared_ptr<ICompAssembly> assem; boost::shared_ptr<ICompAssembly> assem2; det = boost::dynamic_pointer_cast<RectangularDetector>( (*inst)[i] ); if (det) { if (det->getName().compare(onebank) == 0) detList.push_back(det); if (!doOneBank) detList.push_back(det); } else { //Also, look in the first sub-level for RectangularDetectors (e.g. PG3). // We are not doing a full recursive search since that will be very long for lots of pixels. assem = boost::dynamic_pointer_cast<ICompAssembly>( (*inst)[i] ); if (assem) { for (int j=0; j < assem->nelements(); j++) { det = boost::dynamic_pointer_cast<RectangularDetector>( (*assem)[j] ); if (det) { if (det->getName().compare(onebank) == 0) detList.push_back(det); if (!doOneBank) detList.push_back(det); } else { //Also, look in the second sub-level for RectangularDetectors (e.g. PG3). // We are not doing a full recursive search since that will be very long for lots of pixels. assem2 = boost::dynamic_pointer_cast<ICompAssembly>( (*assem)[j] ); if (assem2) { for (int k=0; k < assem2->nelements(); k++) { det = boost::dynamic_pointer_cast<RectangularDetector>( (*assem2)[k] ); if (det) { if (det->getName().compare(onebank) == 0) detList.push_back(det); if (!doOneBank) detList.push_back(det); } } } } } } } } // set-up minimizer std::string inname = getProperty("InputWorkspace"); std::string outname = inname+"2"; //getProperty("OutputWorkspace"); IAlgorithm_sptr algS = createSubAlgorithm("SortEvents"); algS->setPropertyValue("InputWorkspace",inname); algS->setPropertyValue("SortBy", "X Value"); algS->executeAsSubAlg(); inputW=algS->getProperty("InputWorkspace"); //Write DetCal File double baseX,baseY,baseZ,upX,upY,upZ; std::string filename=getProperty("DetCalFilename"); std::fstream outfile; outfile.open(filename.c_str(), std::ios::out); if(detList.size() > 1) { outfile << "#\n"; outfile << "# Mantid Optimized .DetCal file for SNAP with TWO detector panels\n"; outfile << "# Old Panel, nominal size and distance at -90 degrees.\n"; outfile << "# New Panel, nominal size and distance at +90 degrees.\n"; outfile << "#\n"; outfile << "# Lengths are in centimeters.\n"; outfile << "# Base and up give directions of unit vectors for a local\n"; outfile << "# x,y coordinate system on the face of the detector.\n"; outfile << "#\n"; std::time_t current_t = DateAndTime::get_current_time().to_time_t() ; std::tm * current = gmtime( ¤t_t ); outfile << "# "<<asctime (current) <<"\n"; outfile << "#\n"; outfile << "6 L1 T0_SHIFT\n"; IObjComponent_const_sptr source = inst->getSource(); IObjComponent_const_sptr sample = inst->getSample(); outfile << "7 "<<source->getDistance(*sample)*100<<" 0\n"; outfile << "4 DETNUM NROWS NCOLS WIDTH HEIGHT DEPTH DETD CenterX CenterY CenterZ BaseX BaseY BaseZ UpX UpY UpZ\n"; } Progress prog(this,0.0,1.0,detList.size()); for (int det=0; det < static_cast<int>(detList.size()); det++) { std::string par[6]; par[0]=detList[det]->getName(); par[1]=inname; par[2]=outname; std::ostringstream strpeakOpt; strpeakOpt<<peakOpt; par[3]=strpeakOpt.str(); par[4]=rb_params; // --- Create a GroupingWorkspace for this detector name ------ CPUTimer tim; IAlgorithm_sptr alg2 = AlgorithmFactory::Instance().create("CreateGroupingWorkspace", 1); alg2->initialize(); alg2->setPropertyValue("InputWorkspace", getPropertyValue("InputWorkspace")); alg2->setPropertyValue("GroupNames", detList[det]->getName()); std::string groupWSName = "group_" + detList[det]->getName(); alg2->setPropertyValue("OutputWorkspace", groupWSName); alg2->executeAsSubAlg(); par[5] = groupWSName; std::cout << tim << " to CreateGroupingWorkspace" << std::endl; const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex; gsl_multimin_fminimizer *s = NULL; gsl_vector *ss, *x; gsl_multimin_function minex_func; // finally do the fitting int nopt = 6; int iter = 0; int status = 0; double size; /* Starting point */ x = gsl_vector_alloc (nopt); gsl_vector_set (x, 0, 0.0); gsl_vector_set (x, 1, 0.0); gsl_vector_set (x, 2, 0.0); gsl_vector_set (x, 3, 0.0); gsl_vector_set (x, 4, 0.0); gsl_vector_set (x, 5, 0.0); /* Set initial step sizes to 0.1 */ ss = gsl_vector_alloc (nopt); gsl_vector_set_all (ss, 0.1); /* Initialize method and iterate */ minex_func.n = nopt; minex_func.f = &Mantid::Algorithms::gsl_costFunction; minex_func.params = ∥ s = gsl_multimin_fminimizer_alloc (T, nopt); gsl_multimin_fminimizer_set (s, &minex_func, x, ss); do { iter++; status = gsl_multimin_fminimizer_iterate(s); if (status) break; size = gsl_multimin_fminimizer_size (s); status = gsl_multimin_test_size (size, 1e-2); } while (status == GSL_CONTINUE && iter < maxIterations && s->fval != -0.000 ); // Output summary to log file if (s->fval != -0.000) movedetector(gsl_vector_get (s->x, 0), gsl_vector_get (s->x, 1), gsl_vector_get (s->x, 2), gsl_vector_get (s->x, 3), gsl_vector_get (s->x, 4), gsl_vector_get (s->x, 5), par[0], getProperty("InputWorkspace")); else { gsl_vector_set (s->x, 0, 0.0); gsl_vector_set (s->x, 1, 0.0); gsl_vector_set (s->x, 2, 0.0); gsl_vector_set (s->x, 3, 0.0); gsl_vector_set (s->x, 4, 0.0); gsl_vector_set (s->x, 5, 0.0); } std::string reportOfDiffractionEventCalibrateDetectors = gsl_strerror(status); if (s->fval == -0.000) reportOfDiffractionEventCalibrateDetectors = "No events"; g_log.information() << "Detector = " << det << "\n" << "Method used = " << "Simplex" << "\n" << "Iteration = " << iter << "\n" << "Status = " << reportOfDiffractionEventCalibrateDetectors << "\n" << "Minimize PeakLoc-" << peakOpt << " = " << s->fval << "\n"; //Move in cm for small shifts g_log.information() << "Move (X) = " << gsl_vector_get (s->x, 0)*0.01 << " \n"; g_log.information() << "Move (Y) = " << gsl_vector_get (s->x, 1)*0.01 << " \n"; g_log.information() << "Move (Z) = " << gsl_vector_get (s->x, 2)*0.01 << " \n"; g_log.information() << "Rotate (X) = " << gsl_vector_get (s->x, 3) << " \n"; g_log.information() << "Rotate (Y) = " << gsl_vector_get (s->x, 4) << " \n"; g_log.information() << "Rotate (Z) = " << gsl_vector_get (s->x, 5) << " \n"; Kernel::V3D CalCenter=V3D(gsl_vector_get (s->x, 0)*0.01, gsl_vector_get (s->x, 1)*0.01, gsl_vector_get (s->x, 2)*0.01); Kernel::V3D Center=detList[det]->getPos()+CalCenter; int pixmax = detList[det]->xpixels()-1; int pixmid = (detList[det]->ypixels()-1)/2; BoundingBox box; detList[det]->getAtXY(pixmax, pixmid)->getBoundingBox(box); baseX = box.xMax(); baseY = box.yMax(); baseZ = box.zMax(); Kernel::V3D Base=V3D(baseX,baseY,baseZ)+CalCenter; pixmid = (detList[det]->xpixels()-1)/2; pixmax = detList[det]->ypixels()-1; detList[det]->getAtXY(pixmid, pixmax)->getBoundingBox(box); upX = box.xMax(); upY = box.yMax(); upZ = box.zMax(); Kernel::V3D Up=V3D(upX,upY,upZ)+CalCenter; Base-=Center; Up-=Center; //Rotate around x baseX = Base[0]; baseY = Base[1]; baseZ = Base[2]; double deg2rad=M_PI/180.0; double angle = gsl_vector_get (s->x, 3)*deg2rad; Base=V3D(baseX,baseY*cos(angle)-baseZ*sin(angle), baseY*sin(angle)+baseZ*cos(angle)); upX = Up[0]; upY = Up[1]; upZ = Up[2]; Up=V3D(upX,upY*cos(angle)-upZ*sin(angle), upY*sin(angle)+upZ*cos(angle)); //Rotate around y baseX = Base[0]; baseY = Base[1]; baseZ = Base[2]; angle = gsl_vector_get (s->x, 4)*deg2rad; Base=V3D(baseZ*sin(angle)+baseX*cos(angle), baseY,baseZ*cos(angle)-baseX*sin(angle)); upX = Up[0]; upY = Up[1]; upZ = Up[2]; Up=V3D(upZ*cos(angle)-upX*sin(angle),upY, upZ*sin(angle)+upX*cos(angle)); //Rotate around z baseX = Base[0]; baseY = Base[1]; baseZ = Base[2]; angle = gsl_vector_get (s->x, 5)*deg2rad; Base=V3D(baseX*cos(angle)-baseY*sin(angle), baseX*sin(angle)+baseY*cos(angle),baseZ); upX = Up[0]; upY = Up[1]; upZ = Up[2]; Up=V3D(upX*cos(angle)-upY*sin(angle), upX*sin(angle)+upY*cos(angle),upZ); Base.normalize(); Up.normalize(); Center*=100.0; // << det+1 << " " outfile << "5 " << detList[det]->getName().substr(4) << " " << detList[det]->xpixels() << " " << detList[det]->ypixels() << " " << 100.0*detList[det]->xsize() << " " << 100.0*detList[det]->ysize() << " " << "0.2000" << " " << Center.norm() << " " ; Center.write(outfile); outfile << " "; Base.write(outfile); outfile << " "; Up.write(outfile); outfile << "\n"; // clean up dynamically allocated gsl stuff gsl_vector_free(x); gsl_vector_free(ss); gsl_multimin_fminimizer_free (s); // Remove the now-unneeded grouping workspace AnalysisDataService::Instance().remove(groupWSName); prog.report(detList[det]->getName()); } // Closing outfile.close(); return; }