CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/EgammaAnalysis/ElectronTools/src/EpCombinationTool.cc

Go to the documentation of this file.
00001 #include "EgammaAnalysis/ElectronTools/interface/EpCombinationTool.h"
00002 #include "CondFormats/EgammaObjects/interface/GBRForest.h"
00003 #include <TFile.h>
00004 #include <TSystem.h>
00005 #include <math.h>
00006 #include <vector>
00007 #include <iostream>
00008 
00009 using namespace std;
00010 
00011 
00012 /*****************************************************************/
00013 EpCombinationTool::EpCombinationTool():
00014     m_forest(NULL)
00015 /*****************************************************************/
00016 {
00017 }
00018 
00019 
00020 
00021 /*****************************************************************/
00022 EpCombinationTool::~EpCombinationTool()
00023 /*****************************************************************/
00024 {
00025     if(m_forest) delete m_forest;
00026 }
00027 
00028 
00029 /*****************************************************************/
00030 bool EpCombinationTool::init(const string& regressionFileName, const string& bdtName)
00031 /*****************************************************************/
00032 {
00033     TFile* regressionFile = TFile::Open(regressionFileName.c_str());
00034     if(!regressionFile)
00035     {
00036         cout<<"ERROR: Cannot open regression file "<<regressionFileName<<"\n";
00037         return false;
00038     }
00039     m_forest = (GBRForest*) regressionFile->Get(bdtName.c_str());
00040     //regressionFile->GetObject(bdtName.c_str(), m_forest); 
00041     if(!m_forest)
00042     {
00043         cout<<"ERROR: Cannot find forest "<<bdtName<<" in "<<regressionFileName<<"\n";
00044         regressionFile->Close();
00045         return false;
00046     }
00047     regressionFile->Close();
00048     return true;
00049 }
00050 
00051 
00052 
00053 /*****************************************************************/
00054 void EpCombinationTool::combine(SimpleElectron & mySimpleElectron)
00055 /*****************************************************************/
00056 {
00057     if(!m_forest)
00058     {
00059         cout<<"ERROR: The combination tool is not initialized\n";
00060         return;
00061     }
00062 
00063     float energy = mySimpleElectron.getNewEnergy();
00064     float energyError = mySimpleElectron.getNewEnergyError();
00065     float momentum = mySimpleElectron.getTrackerMomentum();
00066     float momentumError = mySimpleElectron.getTrackerMomentumError();
00067     int electronClass = mySimpleElectron.getElClass();
00068     bool isEcalDriven = mySimpleElectron.isEcalDriven();
00069     bool isTrackerDriven =  mySimpleElectron.isTrackerDriven();
00070     bool isEB = mySimpleElectron.isEB();
00071 
00072     // compute relative errors and ratio of errors
00073     float energyRelError = energyError / energy;
00074     float momentumRelError = momentumError / momentum;
00075     float errorRatio = energyRelError / momentumRelError;
00076 
00077     // calculate E/p and corresponding error
00078     float eOverP = energy / momentum;
00079     float eOverPerror = sqrt(
00080             (energyError/momentum)*(energyError/momentum) +
00081             (energy*momentumError/momentum/momentum)*
00082             (energy*momentumError/momentum/momentum));
00083 
00084     // fill input variables
00085     float* regressionInputs = new float[11];
00086     regressionInputs[0]  = energy;
00087     regressionInputs[1]  = energyRelError;
00088     regressionInputs[2]  = momentum;
00089     regressionInputs[3]  = momentumRelError;
00090     regressionInputs[4]  = errorRatio;
00091     regressionInputs[5]  = eOverP;
00092     regressionInputs[6]  = eOverPerror;
00093     regressionInputs[7]  = static_cast<float>(isEcalDriven);
00094     regressionInputs[8]  = static_cast<float>(isTrackerDriven);
00095     regressionInputs[9]  = static_cast<float>(electronClass);
00096     regressionInputs[10] = static_cast<float>(isEB);
00097     
00098     // retrieve combination weight
00099     float weight = 0.;
00100     if(eOverP>0.025 
00101        &&fabs(momentum-energy)<15.*sqrt(momentumError*momentumError + energyError*energyError)
00102            ) // protection against crazy track measurement
00103    {
00104         weight = m_forest->GetResponse(regressionInputs);
00105         if(weight>1.) weight = 1.;
00106         else if(weight<0.) weight = 0.;
00107     }
00108 
00109     float combinedMomentum = weight*momentum + (1.-weight)*energy;
00110     float combinedMomentumError = sqrt(weight*weight*momentumError*momentumError + (1.-weight)*(1.-weight)*energyError*energyError);
00111 
00112     // FIXME : pure tracker electrons have track momentum error of 999.
00113     // If the combination try to combine such electrons then the original combined momentum is kept
00114     if(momentumError!=999. || weight==0.)
00115     {
00116         mySimpleElectron.setCombinedMomentum(combinedMomentum);
00117         mySimpleElectron.setCombinedMomentumError(combinedMomentumError);
00118     }
00119 
00120     delete[] regressionInputs;
00121 }