Back to home page

Project CMSSW displayed by LXR

 
 

    


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 //#include "CondFormats/BTagObjects/interface/CalibrationInterface.h"
0016 //#include "RecoBTag/TrackProbability/interface/CalibrationInterface.h"
0017 #include "RecoBTag/XMLCalibration/interface/CalibrationInterface.h"
0018 
0019 namespace XERCES_CPP_NAMESPACE {
0020   class DOMNode;
0021 }
0022 
0023 //template <class T,class CO> class   CalibrationInterface;
0024 /**
0025 * The AlgorithmCalibration class is the interface between user code and calibration framework 
0026 * developed for BReco subsytem.
0027 * Both the analysis programs and the calibration programs should only access calibration information
0028 * via that class.
0029 *
0030 * An algorithm is supposed to keep a single instance of that class to avoid the reload of
0031 * XML for every  event (during analysis) and because you want to accumulate over all events the data 
0032 * to perform a new calibration in the calibration program.
0033 * 
0034 * The class is templated on the Category T and on the CalibratedObject CO
0035 */
0036 
0037 template <class T, class CO>
0038 class AlgorithmCalibration : public CalibrationInterface<T, CO> {
0039   // friend class CalibrationInterface<T,CO>;
0040 public:
0041   typedef XERCES_CPP_NAMESPACE::DOMElement DOMElement;
0042   typedef XERCES_CPP_NAMESPACE::DOMNode DOMNode;
0043 
0044   /**
0045    * Create an AlgorithmCalibration class and load from fileName the
0046    * cateogries and their objects.
0047    */
0048   AlgorithmCalibration(const std::string &fileName);
0049 
0050   ~AlgorithmCalibration();
0051 
0052   /**
0053      * Prepare for a new calibration run
0054      */
0055   void startCalibration();
0056 
0057   /**
0058      * Accumulate information to calibrate.
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      * Finalize and save the calibration run to fileName
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;  //Cannot find any calibrated objects
0134 
0135   CO *co = new CO();
0136   co->read((DOMElement *)n1);
0137   return co;
0138 }
0139 
0140 #endif