/***********************************************************************//** * @brief Write CTA instrument background model into XML element * * @param[in] xml XML element. * * Write CTA instrument background model information into an XML element. * The XML element will have the following structure * * <source name="..." type="..." instrument="..."> * <spectrum type="..."> * ... * </spectrum> * </source> * * If the model contains a non-constant temporal model, the temporal * component will also be written following the syntax * * <source name="..." type="..." instrument="..."> * <spectrum type="..."> * ... * </spectrum> * <temporalModel type="..."> * ... * </temporalModel> * </source> * * If no temporal component is found a constant model is assumed. ***************************************************************************/ void GCTAModelIrfBackground::write(GXmlElement& xml) const { // Initialise pointer on source GXmlElement* src = NULL; // Search corresponding source int n = xml.elements("source"); for (int k = 0; k < n; ++k) { GXmlElement* element = xml.element("source", k); if (element->attribute("name") == name()) { src = element; break; } } // If we have a temporal model that is either not a constant, or a // constant with a normalization value that differs from 1.0 then // write the temporal component into the XML element. This logic // assures compatibility with the Fermi/LAT format as this format // does not handle temporal components. bool write_temporal = ((m_temporal != NULL) && (m_temporal->type() != "Constant" || (*m_temporal)[0].value() != 1.0)); // If no source with corresponding name was found then append one if (src == NULL) { src = xml.append("source"); if (spectral() != NULL) src->append(GXmlElement("spectrum")); if (write_temporal) src->append(GXmlElement("temporalModel")); } // Set model type, name and optionally instruments src->attribute("name", name()); src->attribute("type", type()); if (instruments().length() > 0) { src->attribute("instrument", instruments()); } std::string identifiers = ids(); if (identifiers.length() > 0) { src->attribute("id", identifiers); } // Write spectral model if (spectral() != NULL) { GXmlElement* spec = src->element("spectrum", 0); spectral()->write(*spec); } // Optionally write temporal model if (write_temporal) { if (dynamic_cast<GModelTemporalConst*>(temporal()) == NULL) { GXmlElement* temp = src->element("temporalModel", 0); temporal()->write(*temp); } } // Return return; }
/***********************************************************************//** * @brief Write model into XML element * * @param[in] xml XML element. * * @todo Document method. ***************************************************************************/ void GCTAModelBackground::write(GXmlElement& xml) const { // Initialise pointer on source GXmlElement* src = NULL; // Search corresponding source int n = xml.elements("source"); for (int k = 0; k < n; ++k) { GXmlElement* element = xml.element("source", k); if (element->attribute("name") == name()) { src = element; break; } } // If no source with corresponding name was found then append one if (src == NULL) { src = xml.append("source"); if (spectral() != NULL) src->append(GXmlElement("spectrum")); if (spatial() != NULL) src->append(GXmlElement("spatialModel")); //if (temporal() != NULL) src->append(GXmlElement("temporalModel")); } // Set model type, name and optionally instruments src->attribute("name", name()); src->attribute("type", type()); if (instruments().length() > 0) { src->attribute("instrument", instruments()); } std::string identifiers = ids(); if (identifiers.length() > 0) { src->attribute("id", identifiers); } // Write spectral model if (spectral() != NULL) { GXmlElement* spec = src->element("spectrum", 0); spectral()->write(*spec); } // Write spatial model if (spatial() != NULL) { GXmlElement* spat = src->element("spatialModel", 0); spatial()->write(*spat); } // Write temporal model /* if (temporal() != NULL) { if (dynamic_cast<GModelTemporalConst*>(temporal()) == NULL) { GXmlElement* temp = src->element("temporalModel", 0); temporal()->write(*temp); } } */ // Return return; }
/***********************************************************************//** * @brief Read sky model from XML element * * @param[in] xml XML element. ***************************************************************************/ void GModelSky::read(const GXmlElement& xml) { // Clear sky model clear(); // Get pointers on spectrum and spatial model GXmlElement* spec = static_cast<GXmlElement*>(xml.element("spectrum", 0)); GXmlElement* spat = static_cast<GXmlElement*>(xml.element("spatialModel", 0)); // Allocate constant GModelTemporalConst temporal; // Clone spatial and spectral models m_spatial = xml_spatial(*spat); m_spectral = xml_spectral(*spec); m_temporal = temporal.clone(); // Set model name name(xml.attribute("name")); // Set instruments instruments(xml.attribute("instrument")); // Set observation identifiers ids(xml.attribute("id")); // Set parameter pointers set_pointers(); // Return return; }
/***********************************************************************//** * @brief Read CTA instrument background model from XML element * * @param[in] xml XML element. * * Set up CTA instrument background model from the information provided by * an XML element. The XML element is expected to have the following * structure * * <source name="..." type="..." instrument="..."> * <spectrum type="..."> * ... * </spectrum> * </source> * * Optionally, a temporal model may be provided using the following * syntax * * <source name="..." type="..." instrument="..."> * <spectrum type="..."> * ... * </spectrum> * <temporalModel type="..."> * ... * </temporalModel> * </source> * * If no temporal component is found a constant model is assumed. ***************************************************************************/ void GCTAModelIrfBackground::read(const GXmlElement& xml) { // Clear model clear(); // Initialise XML elements const GXmlElement* spectral = NULL; const GXmlElement* temporal = NULL; // Get pointer on spectrum spectral = xml.element("spectrum", 0); // Extract spectral model m_spectral = xml_spectral(*spectral); // Optionally get temporal model if (xml.elements("temporalModel")) { temporal = xml.element("temporalModel", 0); m_temporal = xml_temporal(*temporal); } else { GModelTemporalConst constant; m_temporal = constant.clone(); } // Set model name name(xml.attribute("name")); // Set instruments instruments(xml.attribute("instrument")); // Set observation identifiers ids(xml.attribute("id")); // Check flag if TS value should be computed bool tscalc = (xml.attribute("tscalc") == "1") ? true : false; // Set flag if TS value should be computed this->tscalc(tscalc); // Set parameter pointers set_pointers(); // Return return; }
/***********************************************************************//** * @brief XML constructor * * @param[in] xml XML element. * * Construct model from XML element. The method extracts the "name", * "instrument" and "id" attributes from the model node. ***************************************************************************/ GModel::GModel(const GXmlElement& xml) { // Initialise members init_members(); // Set model name name(xml.attribute("name")); // Set instruments instruments(xml.attribute("instrument")); // Set observation identifiers ids(xml.attribute("id")); // Return return; }
/***********************************************************************//** * @brief Read model from XML element * * @param[in] xml XML element. * * The model is composed of a spectrum component ('spectral'), a spatial * component ('spatialModel'), and, optionally, of a temporal component * ('lightcurve'). If no temporal component is found a constant model is * assumed. ***************************************************************************/ void GCTAModelBackground::read(const GXmlElement& xml) { // Clear model clear(); // Initialise XML elements const GXmlElement* spat = NULL; const GXmlElement* spec = NULL; const GXmlElement* temp = NULL; // Get pointers on spectrum and radial model spat = xml.element("spatialModel", 0); spec = xml.element("spectrum", 0); // Clone radial and spectral models m_spatial = xml_spatial(*spat); m_spectral = xml_spectral(*spec); // Optionally get temporal model if (xml.elements("temporalModel")) { temp = xml.element("temporalModel", 0); m_temporal = xml_temporal(*temp); } else { GModelTemporalConst temporal; m_temporal = temporal.clone(); } // Set model name name(xml.attribute("name")); // Set instruments instruments(xml.attribute("instrument")); // Set observation identifiers ids(xml.attribute("id")); // Set parameter pointers set_pointers(); // Return return; }
/** * \brief Create a new Instrument */ Instrument::Instrument(Database db, const std::string& name) : _database(db), _name(name) { debug(LOG_DEBUG, DEBUG_LOG, 0, "instrument '%s' constructed", name.c_str()); // get the information from the instruments table InstrumentTable instruments(_database); // find out whether there already is an instrument in the table int instrumentid = -1; try { instrumentid = instruments.id(name); debug(LOG_DEBUG, DEBUG_LOG, 0, "instrument already exists"); } catch (const std::runtime_error& x) { debug(LOG_DEBUG, DEBUG_LOG, 0, "instrument does not exist, creating one"); InstrumentRecord instrumentrecord; instrumentrecord.name = name; instrumentid = instruments.add(instrumentrecord); debug(LOG_DEBUG, DEBUG_LOG, 0, "id of new instrument: %d", instrumentid); } // retrieve all the matching metadata InstrumentComponentTable components(_database); std::string condition = stringprintf("instrument = %d", instrumentid); auto l = components.select(condition); for (auto ptr = l.begin(); ptr != l.end(); ptr++) { // find the type from the string version of the type DeviceName::device_type type = InstrumentComponentTableAdapter::type(ptr->type); InstrumentComponent::component_t ctype = InstrumentComponentTableAdapter::component_type( ptr->componenttype); // construct suitable InstrumentComponent objects depending // on the mapped field of the component record InstrumentComponentPtr iptr; switch (ctype) { case InstrumentComponent::mapped: // in the case of mapped devices, teh device name is // not an actuald evie name, but rather the name of // the map entry iptr = InstrumentComponentPtr( new InstrumentComponentMapped(type, _database, ptr->devicename)); break; case InstrumentComponent::direct: // for direct components, matters are simplest, so // all fields have the meaning the name suggest iptr = InstrumentComponentPtr( new InstrumentComponentDirect(type, DeviceName(ptr->devicename), ptr->unit, ptr->servername)); break; case InstrumentComponent::derived: // in this case, the devicename is really the component // type from which the component should be derived iptr = InstrumentComponentPtr( new InstrumentComponentDerived(type, *this, InstrumentComponentTableAdapter::type( ptr->devicename), ptr->unit)); break; } // add the new component add(iptr); } debug(LOG_DEBUG, DEBUG_LOG, 0, "instrument constructed"); }
/***********************************************************************//** * @brief Write model into XML element * * @param[in] xml XML element. * * @exception GException::model_invalid_spectral * Existing XML element is not of required type * @exception GException::model_invalid_parnum * Invalid number of model parameters or nodes found in XML element. * @exception GException::model_invalid_parnames * Invalid model parameter names found in XML element. * * Write the COMPTEL DRB fitting model into an XML element. * * The model is composed of nodes that define the normalization value as * function of Phibar value. The following XML file syntax is expected: * * <source name="Background" type="DRBFitting" instrument="COM"> * <node> * <parameter name="Phibar" .../> * <parameter name="Normalization" .../> * </node> * ... * <node> * <parameter name="Phibar" .../> * <parameter name="Normalization" .../> * </node> * </source> ***************************************************************************/ void GCOMModelDRBFitting::write(GXmlElement& xml) const { // Initialise pointer on source GXmlElement* src = NULL; // Search corresponding source int n = xml.elements("source"); for (int k = 0; k < n; ++k) { GXmlElement* element = xml.element("source", k); if (element->attribute("name") == name()) { src = element; break; } } // If no source with corresponding name was found then append one. // Set also the type and the instrument. if (src == NULL) { src = xml.append("source"); src->attribute("name", name()); src->attribute("type", type()); if (instruments().length() > 0) { src->attribute("instrument", instruments()); } } // Verify model type if (src->attribute("type") != type()) { std::string msg = "Invalid model type \""+src->attribute("type")+"\" " "found in XML file. Model type \""+type()+"\" " "expected."; throw GException::invalid_value(G_WRITE, msg); } // Determine number of nodes int nodes = m_phibars.size(); // If XML element has 0 nodes then append nodes if (src->elements() == 0) { for (int i = 0; i < nodes; ++i) { src->append(GXmlElement("node")); } } // Verify that XML element has the required number of nodes if (src->elements() != nodes || src->elements("node") != nodes) { std::string msg = "Invalid number of nodes "+ gammalib::str(src->elements("node"))+ " found in XML file, but model requires exactly "+ gammalib::str(nodes)+" nodes."; throw GException::invalid_value(G_WRITE, msg); } // Loop over all nodes for (int i = 0; i < nodes; ++i) { // Get node GXmlElement* node = src->element("node", i); // Write Phibar parameter GXmlElement* par = gammalib::xml_need_par(G_WRITE, *node, "Phibar"); m_phibars[i].write(*par); // Write Normalization parameter par = gammalib::xml_need_par(G_WRITE, *node, "Normalization"); m_values[i].write(*par); } // endfor: looped over nodes // Return return; }
/***********************************************************************//** * @brief Read model from XML element * * @param[in] xml XML element. * * @exception GException::invalid_value * Model definition requires at least one node. * * Read the COMPTEL DRB fitting model from an XML element. * * The model is composed of nodes that define the normalization value as * function of Phibar value. The following XML file syntax is expected: * * <source name="Background" type="DRBFitting" instrument="COM"> * <node> * <parameter name="Phibar" .../> * <parameter name="Normalization" .../> * </node> * ... * <node> * <parameter name="Phibar" .../> * <parameter name="Normalization" .../> * </node> * </source> ***************************************************************************/ void GCOMModelDRBFitting::read(const GXmlElement& xml) { // Free space for nodes m_phibars.clear(); m_values.clear(); // Get number of nodes from XML file int nodes = xml.elements("node"); // Throw an error if there are no nodes if (nodes < 1) { std::string msg = "DRB fitting model requires at least one Phibar " "node. Please correct XML format."; throw GException::invalid_value(G_READ, msg); } // Loop over all nodes for (int i = 0; i < nodes; ++i) { // Allocate node parameters GModelPar phibar; GModelPar normalization; // Get node const GXmlElement* node = xml.element("node", i); // Read Phibar parameter const GXmlElement* par = gammalib::xml_get_par(G_READ, *node, "Phibar"); phibar.read(*par); // Read Normalization parameter par = gammalib::xml_get_par(G_READ, *node, "Normalization"); normalization.read(*par); // Set parameter names std::string phibar_name = "Phibar layer "+gammalib::str(i); std::string normalization_name = "Scale factor "+gammalib::str(i); // Set Phibar attributes phibar.name(phibar_name); phibar.unit("deg"); phibar.has_grad(false); // Set normalization attributes normalization.name(normalization_name); normalization.unit(""); normalization.has_grad(true); // Push node parameters on list m_phibars.push_back(phibar); m_values.push_back(normalization); } // endfor: looped over nodes // Set model name name(xml.attribute("name")); // Set instruments instruments(xml.attribute("instrument")); // Set observation identifiers ids(xml.attribute("id")); // Check flag if TS value should be computed bool tscalc = (xml.attribute("tscalc") == "1") ? true : false; // Set flag if TS value should be computed this->tscalc(tscalc); // Set pointers set_pointers(); // Set evluation cache set_cache(); // Return return; }