test
CMS 3D CMS Logo

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