/** Initialize a workspace from its parent * This sets values such as title, instrument, units, sample, spectramap. * This does NOT copy any data. * * @deprecated Replaced by functions in MantidDataObjects/WorkspaceCreation.h * @param parent :: the parent workspace * @param child :: the child workspace * @param differentSize :: A flag to indicate if the two workspace will be *different sizes */ void WorkspaceFactoryImpl::initializeFromParent( const MatrixWorkspace &parent, MatrixWorkspace &child, const bool differentSize) const { child.setTitle(parent.getTitle()); child.setComment(parent.getComment()); child.copyExperimentInfoFrom(&parent); child.setYUnit(parent.m_YUnit); child.setYUnitLabel(parent.m_YUnitLabel); child.setDistribution(parent.isDistribution()); // Only copy the axes over if new sizes are not given if (!differentSize) { // Only copy mask map if same size for now. Later will need to check // continued validity. child.m_masks = parent.m_masks; } // Same number of histograms = copy over the spectra data if (parent.getNumberHistograms() == child.getNumberHistograms()) { child.m_isInitialized = false; for (size_t i = 0; i < parent.getNumberHistograms(); ++i) child.getSpectrum(i).copyInfoFrom(parent.getSpectrum(i)); child.m_isInitialized = true; // We use this variant without ISpectrum update to avoid costly rebuilds // triggered by setIndexInfo(). ISpectrum::copyInfoFrom sets invalid flags // for spectrum definitions, so it is important to call this *afterwards*, // since it clears the flags: child.setIndexInfoWithoutISpectrumUpdate(parent.indexInfo()); } // deal with axis for (size_t i = 0; i < parent.m_axes.size(); ++i) { if (parent.m_axes[i]->isSpectra()) { // By default the child already has a spectra axis which // does not need to get cloned from the parent. continue; } const bool isBinEdge = dynamic_cast<const BinEdgeAxis *const>(parent.m_axes[i]) != nullptr; const size_t newAxisLength = child.m_axes[i]->length() + (isBinEdge ? 1 : 0); const size_t oldAxisLength = parent.m_axes[i]->length(); // Need to delete the existing axis created in init above delete child.m_axes[i]; child.m_axes[i] = nullptr; if (newAxisLength == oldAxisLength) { // Now set to a copy of the parent workspace's axis child.m_axes[i] = parent.m_axes[i]->clone(&child); } else { // Call the 'different length' clone variant child.m_axes[i] = parent.m_axes[i]->clone(newAxisLength, &child); } } }
void addFullInstrumentToWorkspace(MatrixWorkspace &workspace, bool includeMonitors, bool startYNegative, const std::string &instrumentName) { auto instrument = boost::make_shared<Instrument>(instrumentName); instrument->setReferenceFrame( boost::make_shared<ReferenceFrame>(Y, Z, Right, "")); workspace.setInstrument(instrument); const double pixelRadius(0.05); Object_sptr pixelShape = ComponentCreationHelper::createCappedCylinder( pixelRadius, 0.02, V3D(0.0, 0.0, 0.0), V3D(0., 1.0, 0.), "tube"); const double detZPos(5.0); // Careful! Do not use size_t or auto, the unisgned will break the -=2 below. int ndets = static_cast<int>(workspace.getNumberHistograms()); if (includeMonitors) ndets -= 2; for (int i = 0; i < ndets; ++i) { std::ostringstream lexer; lexer << "pixel-" << i << ")"; Detector *physicalPixel = new Detector(lexer.str(), workspace.getAxis(1)->spectraNo(i), pixelShape, instrument.get()); int ycount(i); if (startYNegative) ycount -= 1; const double ypos = ycount * 2.0 * pixelRadius; physicalPixel->setPos(0.0, ypos, detZPos); instrument->add(physicalPixel); instrument->markAsDetector(physicalPixel); workspace.getSpectrum(i).setDetectorID(physicalPixel->getID()); } // Monitors last if (includeMonitors) // These occupy the last 2 spectra { Detector *monitor1 = new Detector("mon1", workspace.getAxis(1)->spectraNo(ndets), Object_sptr(), instrument.get()); monitor1->setPos(0.0, 0.0, -9.0); instrument->add(monitor1); instrument->markAsMonitor(monitor1); workspace.getSpectrum(ndets).setDetectorID(ndets + 1); Detector *monitor2 = new Detector("mon2", workspace.getAxis(1)->spectraNo(ndets) + 1, Object_sptr(), instrument.get()); monitor2->setPos(0.0, 0.0, -2.0); instrument->add(monitor2); instrument->markAsMonitor(monitor2); workspace.getSpectrum(ndets + 1).setDetectorID(ndets + 2); } // Define a source and sample position // Define a source component ObjComponent *source = new ObjComponent( "moderator", ComponentCreationHelper::createSphere(0.1, V3D(0, 0, 0), "1"), instrument.get()); source->setPos(V3D(0.0, 0.0, -20.0)); instrument->add(source); instrument->markAsSource(source); // Define a sample as a simple sphere ObjComponent *sample = new ObjComponent( "samplePos", ComponentCreationHelper::createSphere(0.1, V3D(0, 0, 0), "1"), instrument.get()); instrument->setPos(0.0, 0.0, 0.0); instrument->add(sample); instrument->markAsSamplePos(sample); // chopper position Component *chop_pos = new Component("chopper-position", Kernel::V3D(0, 0, -10), instrument.get()); instrument->add(chop_pos); }