File indexing completed on 2024-04-06 12:24:37
0001 #ifndef ALGORITHM_CALIBRATION_H
0002 #define ALGORITHM_CALIBRATION_H
0003
0004 #include <map>
0005 #include <string>
0006 #include <vector>
0007 #include <xercesc/dom/DOM.hpp>
0008 #include <xercesc/dom/DOMElement.hpp>
0009 #include <xercesc/util/XMLString.hpp>
0010 #include <string>
0011 #include <list>
0012 #include <iostream>
0013
0014 #include "CalibrationXML.h"
0015
0016
0017 #include "RecoBTag/XMLCalibration/interface/CalibrationInterface.h"
0018
0019 namespace XERCES_CPP_NAMESPACE {
0020 class DOMNode;
0021 }
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 template <class T, class CO>
0038 class AlgorithmCalibration : public CalibrationInterface<T, CO> {
0039
0040 public:
0041 typedef XERCES_CPP_NAMESPACE::DOMElement DOMElement;
0042 typedef XERCES_CPP_NAMESPACE::DOMNode DOMNode;
0043
0044
0045
0046
0047
0048 AlgorithmCalibration(const std::string &fileName);
0049
0050 ~AlgorithmCalibration();
0051
0052
0053
0054
0055 void startCalibration();
0056
0057
0058
0059
0060 void updateCalibration(const typename T::Input &calibrationInput);
0061 template <class CI>
0062 void updateCalibration(const typename T::Input &calibrationInputForCategory, const CI &inputForCalibration);
0063
0064
0065
0066
0067 void saveCalibration(const std::string &fileName);
0068
0069 protected:
0070 CO *readObject(DOMNode *);
0071 bool readCategories();
0072
0073 protected:
0074 DOMElement *dom() {
0075 if (m_xml == nullptr) {
0076 m_xml = new CalibrationXML();
0077 m_xml->openFile(m_filename);
0078 }
0079 return m_xml->calibrationDOM();
0080 }
0081
0082 private:
0083 std::string m_filename;
0084 CalibrationXML *m_xml;
0085 };
0086
0087 template <class T, class CO>
0088 AlgorithmCalibration<T, CO>::AlgorithmCalibration(const std::string &filename) : m_filename(filename), m_xml(nullptr) {
0089 readCategories();
0090 if (m_xml) {
0091 m_xml->closeFile();
0092 }
0093 }
0094
0095 template <class T, class CO>
0096 AlgorithmCalibration<T, CO>::~AlgorithmCalibration() {
0097 if (m_xml)
0098 delete m_xml;
0099 }
0100
0101 template <class T, class CO>
0102 bool AlgorithmCalibration<T, CO>::readCategories() {
0103 if (dom() == nullptr)
0104 return false;
0105
0106 DOMNode *n1 = dom()->getFirstChild();
0107 while (n1) {
0108 if (n1->getNodeType() == DOMNode::ELEMENT_NODE) {
0109 T *cat = new T();
0110 cat->readFromDOM((DOMElement *)n1);
0111 CO *obj = readObject(n1->getFirstChild());
0112 if (obj) {
0113 this->addEntry(*cat, *obj);
0114 delete obj;
0115 }
0116 delete cat;
0117 }
0118 n1 = n1->getNextSibling();
0119 }
0120
0121 return true;
0122 }
0123 template <class T, class CO>
0124 CO *AlgorithmCalibration<T, CO>::readObject(DOMNode *dom) {
0125 DOMNode *n1 = dom;
0126 while (n1) {
0127 if (n1->getNodeType() == DOMNode::ELEMENT_NODE)
0128 break;
0129 n1 = n1->getNextSibling();
0130 }
0131
0132 if (n1 == nullptr)
0133 return nullptr;
0134
0135 CO *co = new CO();
0136 co->read((DOMElement *)n1);
0137 return co;
0138 }
0139
0140 #endif