CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/RecoEgamma/ElectronIdentification/src/LikelihoodPdf.cc

Go to the documentation of this file.
00001 #include "RecoEgamma/ElectronIdentification/interface/LikelihoodPdf.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 #include "FWCore/Utilities/interface/Exception.h"
00004 #include <iostream>
00005 
00006 
00007 
00008 
00009 LikelihoodPdf::LikelihoodPdf(const char* name, const char* species, int ecalsubdet, int ptbin) {
00010   _name = std::string(name);
00011   _species = std::string(species);
00012   _ecalsubdet = ecalsubdet;
00013   _ptbin = ptbin;
00014 }
00015 
00016 
00017 
00018 LikelihoodPdf::~LikelihoodPdf() {
00019 }
00020 
00021 
00022 
00023 void 
00024 LikelihoodPdf::split(std::map<std::string,float> splitFractions, 
00025                      bool splitPdf) {
00026 
00027   char buffer[100];
00030   if(splitFractions.size()>0 && splitPdf) {
00031     std::map<std::string,float>::const_iterator splitCatItr;
00032     for(splitCatItr=splitFractions.begin();splitCatItr!=splitFractions.end();splitCatItr++) {
00033       snprintf(buffer, 100, "%s_%s_subdet%d_ptbin%d_%s",_name.c_str(),_species.c_str(),_ecalsubdet,_ptbin,splitCatItr->first.c_str());
00034       std::string totPdfName = std::string(buffer);
00035       _splitRule.insert( std::make_pair(splitCatItr->first,totPdfName) );
00036     }
00037   }
00038 
00040   else if(splitFractions.size()>0) {
00041     std::map<std::string,float>::const_iterator splitCatItr;
00042     for(splitCatItr=splitFractions.begin();splitCatItr!=splitFractions.end();splitCatItr++) {
00043       snprintf(buffer, 100, "%s_%s_subdet%d_ptbin%d",_name.c_str(),_species.c_str(),_ecalsubdet,_ptbin);
00044       std::string totPdfName = std::string(buffer);
00045       _splitRule.insert( std::make_pair(splitCatItr->first,totPdfName) );
00046     }
00047   }
00048   
00050   else {
00051     snprintf(buffer, 100, "%s_%s_subdet%d_ptbin%d",_name.c_str(),_species.c_str(),_ecalsubdet,_ptbin);
00052     std::string totPdfName = std::string(buffer);
00053     _splitRule.insert( std::make_pair("NOSPLIT",totPdfName) );
00054   }
00055 }
00056 
00057 
00058 
00059 void 
00060 LikelihoodPdf::initFromDB(const ElectronLikelihoodCalibration *calibration) {
00061 
00062   std::map<std::string,std::string>::const_iterator ruleItr;
00063   for(ruleItr=_splitRule.begin();ruleItr!=_splitRule.end();ruleItr++) {
00064     // look for the requested PDF in the CondDB
00065     std::vector<ElectronLikelihoodCalibration::Entry>::const_iterator entryItr;
00066     bool foundPdf=false;
00067     for(entryItr=calibration->data.begin(); entryItr!=calibration->data.end(); entryItr++) {
00068       if(entryItr->category.label.compare(ruleItr->second)==0) { 
00069         const PhysicsTools::Calibration::HistogramF *histo = &(entryItr->histogram);
00070         _splitPdf.insert( std::make_pair(ruleItr->first,histo) );
00071         foundPdf=true;
00072       }
00073     }
00074     if(!foundPdf) {
00075       throw cms::Exception("LikelihoodPdf") << "The pdf requested: " << _name
00076                                             << " for species: " << _species
00077                                             << " is not present in the Conditions DB!";
00078     }
00079   }
00080 }
00081 
00082 
00083 
00084 float 
00085 LikelihoodPdf::getVal(float x, std::string gsfClass, 
00086                       bool normalized) {
00087   const PhysicsTools::Calibration::HistogramF *thePdf=0;
00088   if(_splitPdf.size()>1) {
00089     edm::LogInfo("LikelihoodPdf") << "The PDF " << _name
00090                                   << " is SPLITTED by category " << gsfClass;
00091     thePdf=_splitPdf.find(gsfClass)->second;
00092   }
00093   else {
00094     edm::LogInfo("LikelihoodPdf") << "The PDF " << _name
00095                                   << " is UNSPLITTED";
00096     thePdf=_splitPdf.find("NOSPLIT")->second;
00097   }
00098   
00099   float prob=-1;
00100 
00101   if(normalized)
00102     // using thePdf->normalization() neglects the overflows... calculating it manually.
00103     // prob=thePdf->value(x)/thePdf->normalization();
00104     prob=thePdf->value(x)/normalization(thePdf);
00105   else
00106     prob=thePdf->value(x);
00107 
00108   edm::LogInfo("LikelihoodPdf") << "sanity check: PDF name = " << _name
00109                                 << " for species = " << _species
00110                                 << " for class = " << gsfClass 
00111                                 << " bin content = " << thePdf->binContent(thePdf->findBin(x))
00112                                 << " normalization (std) = " << thePdf->normalization()
00113                                 << " normalization (manual) = " << normalization(thePdf)
00114                                 << " prob = " << prob;
00115   edm::LogInfo("LikelihoodPdf") << "From likelihood with ecalsubdet = " << _ecalsubdet
00116                                 << " ptbin = " << _ptbin;
00117  
00118 
00119   return prob;
00120 }
00121 
00122 // Histogram::normalization() gives the integral excluding the over-underflow...
00123 float
00124 LikelihoodPdf::normalization(const PhysicsTools::Calibration::HistogramF *thePdf) {
00125   int nBins = thePdf->numberOfBins();
00126   float sum=0.;
00127   for(int i=0; i<=nBins+1; i++) {
00128     sum += thePdf->binContent(i);
00129   }
00130   return sum;
00131 }