CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RegressionHelper.cc
Go to the documentation of this file.
3 
8 #include "TVector2.h"
9 #include "TFile.h"
10 
12  cfg_(config),ecalRegressionInitialized_(false),combinationRegressionInitialized_(false),
13  caloTopologyCacheId_(0), caloGeometryCacheId_(0),regressionCacheId_(0)
14 {;}
15 
17 
20  const edm::Handle<EcalRecHitCollection>& rechitsEB,
21  const edm::Handle<EcalRecHitCollection>& rechitsEE) const {
22  double cor, err;
23  getEcalRegression(*ele.superCluster(), vertices, rechitsEB, rechitsEE, cor, err);
24  ele.setCorrectedEcalEnergy(cor * ele.superCluster()->correctedEnergy());
25  ele.setCorrectedEcalEnergyError( err * ele.superCluster()->correctedEnergy());
26 
27 }
28 
30 
31  // Topology
32  unsigned long long newCaloTopologyCacheId
33  = es.get<CaloTopologyRecord>().cacheIdentifier() ;
34  if (!caloTopologyCacheId_ || ( caloTopologyCacheId_ != newCaloTopologyCacheId ) )
35  {
36  caloTopologyCacheId_ = newCaloTopologyCacheId;
38  es.get<CaloTopologyRecord>().get(caloTopo);
39  caloTopology_ = &(*caloTopo);
40  }
41 
42  // Geometry
43  unsigned long long newCaloGeometryCacheId
44  = es.get<CaloGeometryRecord>().cacheIdentifier() ;
45  if (!caloGeometryCacheId_ || ( caloGeometryCacheId_ != newCaloGeometryCacheId ) )
46  {
47  caloGeometryCacheId_ = newCaloGeometryCacheId;
49  es.get<CaloGeometryRecord>().get(caloGeom);
50  caloGeometry_ = &(*caloGeom);
51  }
52 
53  // Ecal regression
54 
55  // if at least one of the set of weights come from the DB
57  {
58  unsigned long long newRegressionCacheId
59  = es.get<GBRWrapperRcd>().cacheIdentifier() ;
60  if ( !regressionCacheId_ || (newRegressionCacheId != regressionCacheId_ ) ) {
61 
62  const GBRWrapperRcd& gbrRcd = es.get<GBRWrapperRcd>();
63 
64  // ECAL barrel
65  edm::ESHandle<GBRForest> ecalRegBarrelH;
66  gbrRcd.get(cfg_.ecalRegressionWeightLabels[0].c_str(),ecalRegBarrelH);
67  ecalRegBarrel_ = &(*ecalRegBarrelH);
68 
69  // ECAL endcaps
70  edm::ESHandle<GBRForest> ecalRegEndcapH;
71  gbrRcd.get(cfg_.ecalRegressionWeightLabels[1].c_str(),ecalRegEndcapH);
72  ecalRegEndcap_= &(*ecalRegEndcapH);
73 
74  // ECAL barrel error
75  edm::ESHandle<GBRForest> ecalRegErrorBarrelH;
76  gbrRcd.get(cfg_.ecalRegressionWeightLabels[2].c_str(),ecalRegErrorBarrelH);
77  ecalRegErrorBarrel_ = &(*ecalRegErrorBarrelH);
78 
79  // ECAL endcap error
80  edm::ESHandle<GBRForest> ecalRegErrorEndcapH;
81  gbrRcd.get(cfg_.ecalRegressionWeightLabels[3].c_str(),ecalRegErrorEndcapH);
82  ecalRegErrorEndcap_ = &(*ecalRegErrorEndcapH);
83 
85  }
86  }
88  {
89 
90  // Combination
92  {
93  unsigned long long newRegressionCacheId
94  = es.get<GBRWrapperRcd>().cacheIdentifier() ;
95  if ( !regressionCacheId_ || (newRegressionCacheId != regressionCacheId_ ) ) {
96 
97  const GBRWrapperRcd& gbrRcd = es.get<GBRWrapperRcd>();
98 
99  edm::ESHandle<GBRForest> combinationRegH;
100  gbrRcd.get(cfg_.combinationRegressionWeightLabels[0].c_str(),combinationRegH);
101  combinationReg_ = &(*combinationRegH);
102 
104  }
105  }
106  }
107 
108  // read weights from file - for debugging. Even if it is one single files, 4 files should b set in the vector
111  TFile file0 (edm::FileInPath(cfg_.ecalRegressionWeightFiles[0].c_str()).fullPath().c_str());
112  ecalRegBarrel_ = (const GBRForest*)file0.Get(cfg_.ecalRegressionWeightLabels[0].c_str());
113  file0.Close();
114  TFile file1 (edm::FileInPath(cfg_.ecalRegressionWeightFiles[1].c_str()).fullPath().c_str());
116  file1.Close();
117  TFile file2 (edm::FileInPath(cfg_.ecalRegressionWeightFiles[2].c_str()).fullPath().c_str());
118  ecalRegErrorBarrel_ = (const GBRForest*)file2.Get(cfg_.ecalRegressionWeightLabels[2].c_str());
119  file2.Close();
120  TFile file3 (edm::FileInPath(cfg_.ecalRegressionWeightFiles[3].c_str()).fullPath().c_str());
121  ecalRegErrorEndcap_ = (const GBRForest*)file3.Get(cfg_.ecalRegressionWeightLabels[3].c_str());
123  file3.Close();
124  }
125 
128  {
129  TFile file0 (edm::FileInPath(cfg_.combinationRegressionWeightFiles[0].c_str()).fullPath().c_str());
130  combinationReg_ = (const GBRForest*)file0.Get(cfg_.combinationRegressionWeightLabels[0].c_str());
132  file0.Close();
133  }
134 
135 }
136 
138 
141  const edm::Handle<EcalRecHitCollection>& rechitsEB,
142  const edm::Handle<EcalRecHitCollection>& rechitsEE,
143  double & energyFactor, double & errorFactor) const {
144 
145  energyFactor = -999.;
146  errorFactor = -999.;
147 
148  std::vector<float> rInputs;
149  EcalRegressionData regData;
150  regData.fill(sc,rechitsEB.product(),rechitsEE.product(),caloGeometry_,caloTopology_,vertices.product());
151  regData.fillVec(rInputs);
152  if(sc.seed()->hitsAndFractions()[0].first.subdetId()==EcalBarrel){
153  energyFactor = ecalRegBarrel_->GetResponse(&rInputs[0]);
154  errorFactor = ecalRegErrorBarrel_->GetResponse(&rInputs[0]);
155  }else if(sc.seed()->hitsAndFractions()[0].first.subdetId()==EcalEndcap){
156  energyFactor = ecalRegEndcap_->GetResponse(&rInputs[0]);
157  errorFactor = ecalRegErrorEndcap_->GetResponse(&rInputs[0]);
158  }else{
159  throw cms::Exception("RegressionHelper::calculateRegressedEnergy")
160  << "Supercluster seed is either EB nor EE!" << std::endl;
161  }
162 
163 }
164 
166 {
167  float energy = ele.correctedEcalEnergy();
169  float momentum = ele.trackMomentumAtVtx().R();
170  float momentumError = ele.trackMomentumError();
171  int elClass = -.1;
172 
173  switch (ele.classification()) {
175  elClass = 0;
176  break;
178  elClass = 1;
179  break;
181  elClass = 2;
182  break;
184  elClass = 3;
185  break;
187  elClass = 4;
188  break;
189  default:
190  elClass = -1;
191 
192 
193  bool isEcalDriven = ele.ecalDriven();
194  bool isTrackerDriven = ele.trackerDrivenSeed();
195  bool isEB = ele.isEB();
196 
197  // compute relative errors and ratio of errors
198  float energyRelError = energyError / energy;
199  float momentumRelError = momentumError / momentum;
200  float errorRatio = energyRelError / momentumRelError;
201 
202  // calculate E/p and corresponding error
203  float eOverP = energy / momentum;
204  float eOverPerror = eOverP*std::hypot(energyRelError,momentumRelError);
205 
206  // fill input variables
207  std::vector<float> regressionInputs ;
208  regressionInputs.resize(11,0.);
209 
210  regressionInputs[0] = energy;
211  regressionInputs[1] = energyRelError;
212  regressionInputs[2] = momentum;
213  regressionInputs[3] = momentumRelError;
214  regressionInputs[4] = errorRatio;
215  regressionInputs[5] = eOverP;
216  regressionInputs[6] = eOverPerror;
217  regressionInputs[7] = static_cast<float>(isEcalDriven);
218  regressionInputs[8] = static_cast<float>(isTrackerDriven);
219  regressionInputs[9] = static_cast<float>(elClass);
220  regressionInputs[10] = static_cast<float>(isEB);
221 
222  // retrieve combination weight
223  float weight = 0.;
224  if(eOverP>0.025
225  &&fabs(momentum-energy)<15.*sqrt(momentumError*momentumError + energyError*energyError)
226  ) // protection against crazy track measurement
227  {
228  weight = combinationReg_->GetResponse(regressionInputs.data());
229  if(weight>1.) weight = 1.;
230  else if(weight<0.) weight = 0.;
231  }
232 
233  float combinedMomentum = weight*momentum + (1.-weight)*energy;
234  float combinedMomentumError = sqrt(weight*weight*momentumError*momentumError + (1.-weight)*(1.-weight)*energyError*energyError);
235 
236  // FIXME : pure tracker electrons have track momentum error of 999.
237  // If the combination try to combine such electrons then the original combined momentum is kept
238  if(momentumError!=999. || weight==0.)
239  {
240  math::XYZTLorentzVector oldMomentum = ele.p4() ;
241  math::XYZTLorentzVector newMomentum(oldMomentum.x()*combinedMomentum/oldMomentum.t(),
242  oldMomentum.y()*combinedMomentum/oldMomentum.t(),
243  oldMomentum.z()*combinedMomentum/oldMomentum.t(),
244  combinedMomentum);
245 
246  ele.setP4(reco::GsfElectron::P4_COMBINATION,newMomentum,combinedMomentumError,true);
247  }
248  }
249 }
double GetResponse(const float *vector) const
Definition: GBRForest.h:55
std::vector< std::string > ecalRegressionWeightFiles
unsigned long long caloGeometryCacheId_
void applyCombinationRegression(reco::GsfElectron &ele) const
void setP4(P4Kind kind, const LorentzVector &p4, float p4Error, bool setCandidate)
Definition: GsfElectron.cc:197
std::vector< std::string > combinationRegressionWeightLabels
unsigned long long regressionCacheId_
float trackMomentumError() const
Definition: GsfElectron.h:759
const GBRForest * ecalRegEndcap_
const LorentzVector & p4(P4Kind kind) const
Definition: GsfElectron.cc:223
std::vector< std::string > ecalRegressionWeightLabels
unsigned long long caloTopologyCacheId_
void fill(const reco::SuperCluster &superClus, const EcalRecHitCollection *ebRecHits, const EcalRecHitCollection *eeRecHits, const CaloGeometry *geom, const CaloTopology *topology, const reco::VertexCollection *vertices)
math::XYZVectorF trackMomentumAtVtx() const
Definition: GsfElectron.h:289
const Configuration cfg_
bool isEB() const
Definition: GsfElectron.h:350
const CaloTopology * caloTopology_
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
const GBRForest * ecalRegBarrel_
const GBRForest * ecalRegErrorBarrel_
void setCorrectedEcalEnergyError(float newEnergyError)
Definition: GsfElectron.cc:177
T sqrt(T t)
Definition: SSEVec.h:48
virtual SuperClusterRef superCluster() const
reference to a SuperCluster
Definition: GsfElectron.h:182
void get(HolderT &iHolder) const
void fillVec(std::vector< float > &inputVec) const
std::vector< std::string > combinationRegressionWeightFiles
float correctedEcalEnergy() const
Definition: GsfElectron.h:757
T const * product() const
Definition: Handle.h:81
void getEcalRegression(const reco::SuperCluster &sc, const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< EcalRecHitCollection > &rechitsEB, const edm::Handle< EcalRecHitCollection > &rechitsEE, double &energyFactor, double &errorFactor) const
Classification classification() const
Definition: GsfElectron.h:680
const T & get() const
Definition: EventSetup.h:55
void readEvent(const edm::Event &)
const GBRForest * ecalRegErrorEndcap_
float correctedEcalEnergyError() const
Definition: GsfElectron.h:758
void setCorrectedEcalEnergy(float newEnergy)
Definition: GsfElectron.cc:180
void applyEcalRegression(reco::GsfElectron &electron, const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< EcalRecHitCollection > &rechitsEB, const edm::Handle< EcalRecHitCollection > &rechitsEE) const
bool trackerDrivenSeed() const
Definition: GsfElectron.h:187
const CaloClusterPtr & seed() const
seed BasicCluster
Definition: SuperCluster.h:66
volatile std::atomic< bool > shutdown_flag false
RegressionHelper(const Configuration &)
int weight
Definition: histoStyle.py:50
bool ecalDriven() const
Definition: GsfElectron.cc:172
bool combinationRegressionInitialized_
void checkSetup(const edm::EventSetup &)
float energyError(float E, float *par)
const CaloGeometry * caloGeometry_
const GBRForest * combinationReg_