CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/RecoBTag/ImpactParameterLearning/plugins/SumHistoCalibration.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    ImpactParameterCalibration
00004 // Class:      ImpactParameterCalibration
00005 // 
00013 //
00014 // Original Author:  Jeremy Andrea
00015 //         Created:  Wed Mar  5 19:17:38 CEST 2008
00016 // $Id: SumHistoCalibration.cc,v 1.8 2010/02/11 00:13:31 wmtan Exp $
00017 //
00018 //
00019 // system include files
00020 
00021 #include "FWCore/Framework/interface/Frameworkfwd.h"
00022 #include "FWCore/Framework/interface/EDAnalyzer.h"
00023 #include "FWCore/Framework/interface/Event.h"
00024 #include "FWCore/Framework/interface/MakerMacros.h"
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 #include "FWCore/Utilities/interface/InputTag.h"
00027 #include "FWCore/Framework/interface/ESHandle.h"
00028 #include "FWCore/Framework/interface/EventSetup.h"
00029 #include "Utilities/General/interface/FileInPath.h"
00030 
00031 #include "FWCore/Framework/interface/IOVSyncValue.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h"
00033 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00034 
00035 
00036 #include "DataFormats/BTauReco/interface/TrackIPTagInfo.h"
00037 #include "CondFormats/BTauObjects/interface/TrackProbabilityCalibration.h"
00038 #include "CondFormats/BTauObjects/interface/CalibratedHistogram.h"
00039 
00040 #include "CondFormats/DataRecord/interface/BTagTrackProbability2DRcd.h"
00041 #include "CondFormats/DataRecord/interface/BTagTrackProbability3DRcd.h"
00042 
00043 
00044 #include "RecoBTag/XMLCalibration/interface/AlgorithmCalibration.h"
00045 #include "RecoBTag/XMLCalibration/interface/CalibratedHistogramXML.h"
00046 #include "RecoBTag/TrackProbability/interface/TrackClassFilter.h"
00047 
00048 #include "CondFormats/BTauObjects/interface/TrackProbabilityCalibration.h"
00049 
00050 
00051 
00052 #include "RVersion.h"
00053 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
00054 #include "TBufferFile.h"
00055 typedef TBufferFile MyTBuffer;
00056 #else
00057 #include "TBuffer.h"
00058 typedef TBuffer MyTBuffer;
00059 #endif
00060 
00061 #include <TClass.h>
00062 #include <TBufferXML.h>
00063 #include <iostream>
00064 #include <fstream>
00065 #include <sstream>
00066 #include <string>
00067 #include <vector>
00068 #include <memory>
00069 
00070 #include "TrackClassMatch.h"
00071 
00072 
00073 using namespace reco;
00074 using namespace std;
00075 //
00076 // class decleration
00077 //
00078 
00079 
00080 
00081 class SumHistoCalibration : public edm::EDAnalyzer {
00082    public:
00083       explicit SumHistoCalibration(const edm::ParameterSet&);
00084       ~SumHistoCalibration();
00085 
00086 
00087    private:
00088       virtual void beginJob() ;
00089       virtual void analyze(const edm::Event&, const edm::EventSetup&);
00090       virtual void endJob() ;
00091       edm::ParameterSet config;
00092 
00093    TrackProbabilityCalibration * fromXml(edm::FileInPath xmlCalibration);
00094 
00095   
00096   std::vector<std::string>  m_xmlilelist2d;
00097   std::vector<std::string>  m_xmlilelist3d;
00098   bool m_sum2D;
00099   bool m_sum3D;
00100   unsigned int minLoop, maxLoop;
00101   TrackProbabilityCalibration * m_calibration[2];
00102  
00103 
00104   
00105 };
00106 
00107 SumHistoCalibration::SumHistoCalibration(const edm::ParameterSet& iConfig):config(iConfig)
00108 {
00109   m_xmlilelist2d   = iConfig.getParameter<std::vector<std::string> >("xmlfiles2d");
00110   m_xmlilelist3d   = iConfig.getParameter<std::vector<std::string> >("xmlfiles3d");
00111   m_sum2D          = iConfig.getParameter<bool>("sum2D");   
00112   m_sum3D          = iConfig.getParameter<bool>("sum3D");   
00113 }
00114 
00115 
00116 SumHistoCalibration::~SumHistoCalibration()
00117 {
00118 }
00119 
00120 
00121 void
00122 SumHistoCalibration::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00123 {
00124   using namespace edm;
00125   using namespace reco;
00126   using namespace std;
00127 
00128 }
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 // ------------ method called once each job just before starting event loop  ------------
00140 void 
00141 SumHistoCalibration::beginJob()
00142 {
00143   if(m_sum2D && m_sum3D){minLoop = 0; maxLoop =1;}
00144   if(m_sum2D && !m_sum3D){minLoop = 0; maxLoop =0;}
00145   if(!m_sum2D && m_sum3D){minLoop = 1; maxLoop =1;}
00146   using namespace edm;
00147   m_calibration[0] =   new TrackProbabilityCalibration();
00148   m_calibration[1] =   new TrackProbabilityCalibration();
00149   
00150   const TrackProbabilityCalibration * ca[2];
00151   edm::FileInPath fip(m_xmlilelist3d[0]);
00152   edm::FileInPath fip2(m_xmlilelist2d[0]);
00153   ca[0]  = fromXml(fip);
00154   ca[1]  = fromXml(fip2);
00155   
00156   for(unsigned int i=minLoop;i <=maxLoop ;i++)
00157    for(unsigned int j=0;j<ca[i]->data.size() ; j++)
00158    {
00159     TrackProbabilityCalibration::Entry e;
00160     e.category=ca[i]->data[j].category;
00161     e.histogram=ca[i]->data[j].histogram;
00162     m_calibration[i]->data.push_back(e);
00163    }
00164 
00165    delete ca[0];
00166    delete ca[1];
00167 
00168   
00169   
00170   
00171   
00172   
00173   
00174   
00175   
00176   
00177   
00178   
00179   
00180 }
00181 
00182 TrackProbabilityCalibration * SumHistoCalibration::fromXml(edm::FileInPath xmlCalibration)   
00183 {
00184   std::ifstream xmlFile(xmlCalibration.fullPath().c_str());
00185   if (!xmlFile.good())
00186     throw cms::Exception("BTauFakeMVAJetTagConditions")
00187       << "File \"" << xmlCalibration.fullPath()
00188       << "\" could not be opened for reading."
00189       << std::endl;
00190   std::ostringstream ss;
00191   ss << xmlFile.rdbuf();
00192   xmlFile.close();
00193   TClass *classType = 0;
00194   void *ptr = TBufferXML(TBuffer::kRead).ConvertFromXMLAny(
00195                                                            ss.str().c_str(), &classType, kTRUE, kFALSE);
00196   if (!ptr)
00197     throw cms::Exception("SumHistoCalibration")
00198       << "Unknown error parsing XML serialization"
00199       << std::endl;
00200   
00201   if (std::strcmp(classType->GetName(),
00202                   "TrackProbabilityCalibration")) {
00203     classType->Destructor(ptr);
00204     throw cms::Exception("SumHistoCalibration")
00205       << "Serialized object has wrong C++ type."
00206                         << std::endl;
00207   }
00208   
00209   return static_cast<TrackProbabilityCalibration*>(ptr);
00210 }
00211 
00212 
00213 
00214 
00215 // ------------ method called once each job just after ending the event loop  ------------
00216 void 
00217 SumHistoCalibration::endJob() {
00218   
00219   
00220   using namespace edm;
00221   using namespace reco;
00222   using namespace std;
00223 
00224   
00225   
00226   
00227   if(m_sum3D){
00228     for(unsigned int itFile =1; itFile< m_xmlilelist3d.size(); itFile++){
00229       edm::FileInPath fip(m_xmlilelist3d[itFile]);
00230       const TrackProbabilityCalibration *ca  = fromXml(fip);
00231       for(unsigned int j=0;j<ca->data.size() ; j++)
00232         {
00233           for(int k = 0; k< m_calibration[0]->data[j].histogram.numberOfBins(); k++){
00234             m_calibration[0]->data[j].histogram.setBinContent(k, ca->data[j].histogram.binContent(k)
00235                                                               + m_calibration[0]->data[j].histogram.binContent(k));
00236           }
00237         }
00238       delete ca;
00239     } 
00240   }
00241   if(m_sum2D){
00242     for(unsigned int itFile =1; itFile< m_xmlilelist2d.size(); itFile++){
00243       edm::FileInPath fip(m_xmlilelist2d[itFile]);
00244       TrackProbabilityCalibration * ca  = fromXml(fip);
00245       for(unsigned int j=0;j<ca->data.size() ; j++)
00246         {
00247           for(int k = 0; k< m_calibration[1]->data[j].histogram.numberOfBins(); k++){
00248             m_calibration[1]->data[j].histogram.setBinContent(k,ca->data[j].histogram.binContent(k)
00249                                                               + m_calibration[1]->data[j].histogram.binContent(k)); 
00250           }
00251         }
00252       delete ca; 
00253     }
00254   }
00255   
00256   
00257   
00258   
00259   
00260   
00261   
00262   
00263   
00264   
00265   
00266   
00267   
00268   
00269   if(config.getParameter<bool>("writeToDB"))
00270     {
00271       edm::Service<cond::service::PoolDBOutputService> mydbservice;
00272       if( !mydbservice.isAvailable() ) return;
00273       //mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[0],  mydbservice->endOfTime(),"BTagTrackProbability3DRcd");
00274       //mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[1],  mydbservice->endOfTime(),"BTagTrackProbability2DRcd"); 
00275       mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[0],  mydbservice->beginOfTime(), mydbservice->endOfTime(),"BTagTrackProbability3DRcd");
00276       mydbservice->createNewIOV<TrackProbabilityCalibration>(m_calibration[1],  mydbservice->beginOfTime(), mydbservice->endOfTime(),"BTagTrackProbability2DRcd");
00277       
00278     } 
00279   
00280   
00281   if(config.getParameter<bool>("writeToRootXML"))
00282     {
00283       std::ofstream of2("2d.xml");
00284       TBufferXML b2(TBuffer::kWrite);
00285       of2 << b2.ConvertToXML(const_cast<void*>(static_cast<const void*>(m_calibration[1])),
00286                              TClass::GetClass("TrackProbabilityCalibration"),
00287                              kTRUE, kFALSE);
00288       of2.close();
00289       std::ofstream of3("3d.xml");
00290       TBufferXML b3(TBuffer::kWrite);
00291       of3 << b3.ConvertToXML(const_cast<void*>(static_cast<const void*>(m_calibration[0])),
00292                              TClass::GetClass("TrackProbabilityCalibration"),
00293                              kTRUE, kFALSE);
00294       of3.close();
00295     }
00296   
00297   
00298   if(config.getParameter<bool>("writeToBinary"))
00299     {
00300       std::ofstream ofile("2d.dat");
00301       MyTBuffer buffer(TBuffer::kWrite);
00302       buffer.StreamObject(const_cast<void*>(static_cast<const void*>(m_calibration[1])),
00303                           TClass::GetClass("TrackProbabilityCalibration"));
00304       Int_t size = buffer.Length();
00305       ofile.write(buffer.Buffer(),size);
00306       ofile.close();
00307       
00308       std::ofstream ofile3("3d.dat");
00309       MyTBuffer buffer3(TBuffer::kWrite);
00310       buffer3.StreamObject(const_cast<void*>(static_cast<const void*>(m_calibration[0])),
00311                            TClass::GetClass("TrackProbabilityCalibration"));
00312       Int_t size3 = buffer3.Length();
00313       ofile3.write(buffer3.Buffer(),size3);
00314       ofile3.close();
00315     }
00316   
00317   
00318   
00319   
00320   
00321 }
00322 
00323 //define this as a plug-in
00324 DEFINE_FWK_MODULE(SumHistoCalibration);