CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoBTag/XMLCalibration/interface/AlgorithmCalibration.h

Go to the documentation of this file.
00001 #ifndef ALGORITHM_CALIBRATION_H
00002 #define ALGORITHM_CALIBRATION_H
00003 
00004 #include <map>
00005 #include <string>
00006 #include <vector>
00007 #include <xercesc/dom/DOM.hpp>
00008 #include <xercesc/dom/DOMElement.hpp>
00009 #include <xercesc/util/XMLString.hpp>
00010 #include <string>
00011 #include <list>
00012 #include <iostream>
00013 
00014 #include "CalibrationXML.h"
00015 //#include "CondFormats/BTagObjects/interface/CalibrationInterface.h"
00016 //#include "RecoBTag/TrackProbability/interface/CalibrationInterface.h"
00017 #include "RecoBTag/XMLCalibration/interface/CalibrationInterface.h"
00018 
00019 namespace XERCES_CPP_NAMESPACE { class DOMNode; }
00020 
00021 
00022 
00023 //template <class T,class CO> class   CalibrationInterface;
00037 template <class T,class CO> class AlgorithmCalibration : public CalibrationInterface<T,CO>
00038 {
00039 // friend class CalibrationInterface<T,CO>; 
00040   public:
00041     typedef XERCES_CPP_NAMESPACE::DOMElement DOMElement;
00042     typedef XERCES_CPP_NAMESPACE::DOMNode DOMNode;
00043 
00048     AlgorithmCalibration(const std::string & fileName);
00049     
00050     ~AlgorithmCalibration();
00051     
00055     void startCalibration();
00056 
00060     void updateCalibration(const typename T::Input & calibrationInput);
00061     template <class CI> void updateCalibration(const typename T::Input & calibrationInputForCategory,const CI & inputForCalibration);
00062 
00066     void saveCalibration(const std::string & fileName);
00067 
00068  protected:
00069 
00070     CO* readObject(DOMNode *);
00071     bool readCategories();
00072     
00073  protected:
00074 
00075     DOMElement * dom() 
00076     {
00077       if(m_xml == 0)
00078        {
00079         m_xml=new CalibrationXML();
00080         m_xml->openFile(m_filename);
00081         
00082        } 
00083     return m_xml->calibrationDOM();
00084     }
00085 
00086   private:
00087     std::string m_filename;
00088     CalibrationXML *m_xml;
00089 };
00090 
00091 
00092 template <class T,class CO> 
00093 AlgorithmCalibration<T,CO>::AlgorithmCalibration(const std::string & filename) : m_filename(filename), m_xml(0)
00094 {
00095  readCategories();
00096  if(m_xml) {
00097   m_xml->closeFile();
00098  }
00099 }
00100 
00101 template <class T,class CO> 
00102 AlgorithmCalibration<T,CO>::~AlgorithmCalibration()
00103 {
00104    if(m_xml) delete m_xml;
00105 }
00106     
00107 template <class T,class CO> 
00108 bool AlgorithmCalibration<T,CO>::readCategories()
00109 {
00110    if(dom()==0) return false;
00111    
00112    DOMNode* n1 = dom()->getFirstChild();
00113    while(n1)
00114     {
00115       if (n1->getNodeType() == DOMNode::ELEMENT_NODE   )
00116       {
00117           T *cat = new T();
00118           cat->readFromDOM((DOMElement *)n1);
00119           CO * obj =readObject(n1->getFirstChild());
00120           if(obj) 
00121            {
00122                 addEntry(*cat,*obj);  
00123                 delete obj;  
00124            }
00125           delete cat;
00126       }
00127       n1 = n1->getNextSibling();
00128     }
00129     
00130  return true;
00131 }
00132 template <class T,class CO>
00133 CO * AlgorithmCalibration<T,CO>::readObject(DOMNode * dom)
00134 {
00135   DOMNode* n1 = dom;
00136   while(n1)
00137     {
00138       if (n1->getNodeType() == DOMNode::ELEMENT_NODE   )
00139            break;
00140       n1 = n1->getNextSibling();
00141     }
00142      
00143   if(n1==0) return 0; //Cannot find any calibrated objects
00144   
00145   CO * co = new CO();
00146   co->read((DOMElement *)n1);
00147   return co;
00148 }
00149 
00150 #endif