CMS 3D CMS Logo

EpCombinationTool.cc
Go to the documentation of this file.
3 #include <TFile.h>
4 #include <TSystem.h>
5 #include <math.h>
6 #include <vector>
7 #include <iostream>
8 
9 using namespace std;
10 
11 /*****************************************************************/
13  m_forest(NULL), m_ownForest(false)
14 /*****************************************************************/
15 {
16 }
17 
18 
19 
20 /*****************************************************************/
22 /*****************************************************************/
23 {
24  if(m_ownForest) delete m_forest;
25 }
26 
27 
28 /*****************************************************************/
29 bool EpCombinationTool::init(const std::string& regressionFileName, const std::string& bdtName)
30 /*****************************************************************/
31 {
32  TFile* regressionFile = TFile::Open(regressionFileName.c_str());
33  if(!regressionFile)
34  {
35  cout<<"ERROR: Cannot open regression file "<<regressionFileName<<"\n";
36  return false;
37  }
38  if(m_ownForest) delete m_forest;
39  m_forest = (GBRForest*) regressionFile->Get(bdtName.c_str());
40  m_ownForest = true;
41  //regressionFile->GetObject(bdtName.c_str(), m_forest);
42  if(!m_forest)
43  {
44  cout<<"ERROR: Cannot find forest "<<bdtName<<" in "<<regressionFileName<<"\n";
45  regressionFile->Close();
46  return false;
47  }
48  regressionFile->Close();
49  return true;
50 }
51 
52 bool EpCombinationTool::init(const GBRForest *forest)
53 {
54  if(m_ownForest) delete m_forest;
55  m_forest = forest;
56  m_ownForest = false;
57  return true;
58 }
59 
60 
61 /*****************************************************************/
62 void EpCombinationTool::combine(SimpleElectron & mySimpleElectron) const
63 /*****************************************************************/
64 {
65  if(!m_forest)
66  {
67  cout<<"ERROR: The combination tool is not initialized\n";
68  return;
69  }
70 
71  float energy = mySimpleElectron.getNewEnergy();
72  float energyError = mySimpleElectron.getNewEnergyError();
73  float momentum = mySimpleElectron.getTrackerMomentum();
74  float momentumError = mySimpleElectron.getTrackerMomentumError();
75  int electronClass = mySimpleElectron.getElClass();
76  bool isEcalDriven = mySimpleElectron.isEcalDriven();
77  bool isTrackerDriven = mySimpleElectron.isTrackerDriven();
78  bool isEB = mySimpleElectron.isEB();
79 
80  // compute relative errors and ratio of errors
81  float energyRelError = energyError / energy;
82  float momentumRelError = momentumError / momentum;
83  float errorRatio = energyRelError / momentumRelError;
84 
85  // calculate E/p and corresponding error
86  float eOverP = energy / momentum;
87  float eOverPerror = sqrt(
88  (energyError/momentum)*(energyError/momentum) +
89  (energy*momentumError/momentum/momentum)*
90  (energy*momentumError/momentum/momentum));
91 
92  // fill input variables
93  float regressionInputs[11];
94  regressionInputs[0] = energy;
95  regressionInputs[1] = energyRelError;
96  regressionInputs[2] = momentum;
97  regressionInputs[3] = momentumRelError;
98  regressionInputs[4] = errorRatio;
99  regressionInputs[5] = eOverP;
100  regressionInputs[6] = eOverPerror;
101  regressionInputs[7] = static_cast<float>(isEcalDriven);
102  regressionInputs[8] = static_cast<float>(isTrackerDriven);
103  regressionInputs[9] = static_cast<float>(electronClass);
104  regressionInputs[10] = static_cast<float>(isEB);
105 
106  // retrieve combination weight
107  float weight = 0.;
108  if(eOverP>0.025
109  &&fabs(momentum-energy)<15.*sqrt(momentumError*momentumError + energyError*energyError)
110  && ( (momentumError < 10.*momentum) || (energy < 200.) )
111  ) // protection against crazy track measurement
112  {
113  weight = m_forest->GetResponse(regressionInputs);
114  if(weight>1.) weight = 1.;
115  else if(weight<0.) weight = 0.;
116  }
117 
118  float combinedMomentum = weight*momentum + (1.-weight)*energy;
119  float combinedMomentumError = sqrt(weight*weight*momentumError*momentumError + (1.-weight)*(1.-weight)*energyError*energyError);
120 
121  // FIXME : pure tracker electrons have track momentum error of 999.
122  // If the combination try to combine such electrons then the original combined momentum is kept
123  if(momentumError!=999. || weight==0.)
124  {
125  mySimpleElectron.setCombinedMomentum(combinedMomentum);
126  mySimpleElectron.setCombinedMomentumError(combinedMomentumError);
127  }
128 }
double GetResponse(const float *vector) const
Definition: GBRForest.h:59
bool init(const GBRForest *forest)
const GBRForest * m_forest
void combine(SimpleElectron &mySimpleElectron) const
#define NULL
Definition: scimark2.h:8
Definition: weight.py:1
T sqrt(T t)
Definition: SSEVec.h:18
float energyError(float E, float *par)