Histogram2DNode.hh 3.59 KB
/*
 * Histogram2DNode.hh
 *
 *  Created on: Jan 27, 2023
 *      Author: AKKODIS
 */

#ifndef HISTOGRAM2DNODENODE_HH_
#define HISTOGRAM2DNODENODE_HH_

#include <libxml/globals.h>
#include <libxml/tree.h>
#include <libxml/xmlstring.h>
#include <iosfwd>

#include "FileConfigurator.hh"
#include "DrawingPropertiesNode.hh"
#include "PlotLogger.hh"
#include "NodeCfg.hh"
#include "Histogram2DSeriesProperties.hh"

namespace plot {




/**
 * Class that handle a <histogram2d> xml node.
 * Template class that should be instanciated for each subclass of PanelOutputPanel.
 */


template<class PlotType>
class Histogram2DSeriesNode: public plot::DrawingPropertiesNode<PlotType> {
public:
	Histogram2DSeriesNode() : 
			DrawingPropertiesNode<PlotType>() {
			DrawingPropertiesNode<PlotType>::getChildList()["bins"] =  AMDA::XMLConfigurator::NodeCfgSPtr(new BinsNode());
			DrawingPropertiesNode<PlotType>::getChildList()["histotype"] = AMDA::XMLConfigurator::NodeCfgSPtr(new HistotypeNode());
	}
	virtual ~Histogram2DSeriesNode() {
	}
	void proceed(xmlNodePtr pNode_,
			const AMDA::Parameters::CfgContext& pContext_) {
		LOG4CXX_DEBUG(gLogger, "Histogram2DSeriesNode::proceed");
		PlotType* plotOutput = pContext_.get<PlotType*>();
		xmlChar* name = pContext_.get<xmlChar*>();

		// initialize series with default properties
		DrawingProperties defaultProps = plotOutput->getParameter(
				(const char*) name).getDefaultProperties();

		std::shared_ptr<Histogram2DSeriesProperties> histogramPropsPtr =
				std::shared_ptr<Histogram2DSeriesProperties>(new Histogram2DSeriesProperties(defaultProps));

		// parse attributes...
		parseAttributes(pNode_, *histogramPropsPtr.get());
		// add series definition to parameter
		histogramPropsPtr->setParamId((const char*)name);
		plotOutput->getParameter((const char*) name).addHistogram2DSeriesProperties(histogramPropsPtr);

		//parse children nodes
		AMDA::Parameters::CfgContext context;
		context.push<xmlChar*>(name);
		context.push<Histogram2DSeriesProperties*>(histogramPropsPtr.get());
		context.push<DrawingProperties*>(histogramPropsPtr.get());
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode_, context);

	}
protected:
	void parseAttributes(xmlNodePtr pNode_, DrawingProperties& props_) {
		// read parent attributes
		DrawingPropertiesNode<PlotType>::parseAttributes(pNode_, props_);
		// specific series node attributes :
		xmlChar * value = NULL;

		value = xmlGetProp(pNode_, (const xmlChar*) "index");
		if (value) {
			AMDA::Common::ParameterIndexComponentList indexList;
			if (!AMDA::Common::ParameterIndexesTool::parse((const char*) value, -1, -1, indexList) || indexList.empty())
			{
				LOG4CXX_ERROR(gLogger, "Histogram2DSeriesNode::parseAttributes - Bad index definition " << (const char*) value);
				xmlFree(value);
				BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Bad Histogram2Dserie index definition"));
			}
			((Histogram2DSeriesProperties&) props_).setIndex(indexList.front());
			xmlFree(value);
		} else {
			((Histogram2DSeriesProperties&) props_).setIndex(AMDA::Common::ParameterIndexComponent(-1,-1)); // default case: if no index attribute, we use default index value -1 which means for all series.
		}

		value = xmlGetProp(pNode_, (const xmlChar*) "id");
		if (value) {
			((Histogram2DSeriesProperties&) props_).setId(atoi((const char*) value));
			xmlFree(value);
		}

		// -- associated x serie id
		value = xmlGetProp(pNode_, (const xmlChar*) "xId");
		if (value) {
			((Histogram2DSeriesProperties&) props_).setXId(atoi((const char*) value));
			xmlFree(value);
		}
	}
};


}
/* namespace plot */

#endif /* HISTOGRAM2DNODENODE_HH_ */