CMS 3D CMS Logo

RegressionHelper.cc
Go to the documentation of this file.
3 
8 #include "TVector2.h"
9 #include "TFile.h"
10 
12  : cfg_(config),
13  ecalRegressionInitialized_(false),
14  combinationRegressionInitialized_(false),
15  caloTopologyCacheId_(0),
16  caloGeometryCacheId_(0),
17  regressionCacheId_(0) {
18  ;
19 }
20 
22 
25  const edm::Handle<EcalRecHitCollection>& rechitsEB,
26  const edm::Handle<EcalRecHitCollection>& rechitsEE) const {
27  double cor, err;
28  getEcalRegression(*ele.superCluster(), vertices, rechitsEB, rechitsEE, cor, err);
29  ele.setCorrectedEcalEnergy(cor * ele.superCluster()->correctedEnergy());
30  ele.setCorrectedEcalEnergyError(err * ele.superCluster()->correctedEnergy());
31 }
32 
34  // Topology
35  unsigned long long newCaloTopologyCacheId = es.get<CaloTopologyRecord>().cacheIdentifier();
36  if (!caloTopologyCacheId_ || (caloTopologyCacheId_ != newCaloTopologyCacheId)) {
37  caloTopologyCacheId_ = newCaloTopologyCacheId;
39  es.get<CaloTopologyRecord>().get(caloTopo);
40  caloTopology_ = &(*caloTopo);
41  }
42 
43  // Geometry
44  unsigned long long newCaloGeometryCacheId = es.get<CaloGeometryRecord>().cacheIdentifier();
45  if (!caloGeometryCacheId_ || (caloGeometryCacheId_ != newCaloGeometryCacheId)) {
46  caloGeometryCacheId_ = newCaloGeometryCacheId;
48  es.get<CaloGeometryRecord>().get(caloGeom);
49  caloGeometry_ = &(*caloGeom);
50  }
51 
52  // Ecal regression
53 
54  // if at least one of the set of weights come from the DB
55  if (cfg_.ecalWeightsFromDB) {
56  unsigned long long newRegressionCacheId = es.get<GBRWrapperRcd>().cacheIdentifier();
57  if (!regressionCacheId_ || (newRegressionCacheId != regressionCacheId_)) {
58  const GBRWrapperRcd& gbrRcd = es.get<GBRWrapperRcd>();
59 
60  // ECAL barrel
61  edm::ESHandle<GBRForest> ecalRegBarrelH;
62  gbrRcd.get(cfg_.ecalRegressionWeightLabels[0].c_str(), ecalRegBarrelH);
63  ecalRegBarrel_ = &(*ecalRegBarrelH);
64 
65  // ECAL endcaps
66  edm::ESHandle<GBRForest> ecalRegEndcapH;
67  gbrRcd.get(cfg_.ecalRegressionWeightLabels[1].c_str(), ecalRegEndcapH);
68  ecalRegEndcap_ = &(*ecalRegEndcapH);
69 
70  // ECAL barrel error
71  edm::ESHandle<GBRForest> ecalRegErrorBarrelH;
72  gbrRcd.get(cfg_.ecalRegressionWeightLabels[2].c_str(), ecalRegErrorBarrelH);
73  ecalRegErrorBarrel_ = &(*ecalRegErrorBarrelH);
74 
75  // ECAL endcap error
76  edm::ESHandle<GBRForest> ecalRegErrorEndcapH;
77  gbrRcd.get(cfg_.ecalRegressionWeightLabels[3].c_str(), ecalRegErrorEndcapH);
78  ecalRegErrorEndcap_ = &(*ecalRegErrorEndcapH);
79 
81  }
82  }
84  // Combination
86  unsigned long long newRegressionCacheId = es.get<GBRWrapperRcd>().cacheIdentifier();
87  if (!regressionCacheId_ || (newRegressionCacheId != regressionCacheId_)) {
88  const GBRWrapperRcd& gbrRcd = es.get<GBRWrapperRcd>();
89 
90  edm::ESHandle<GBRForest> combinationRegH;
91  gbrRcd.get(cfg_.combinationRegressionWeightLabels[0].c_str(), combinationRegH);
92  combinationReg_ = &(*combinationRegH);
93 
95  }
96  }
97  }
98 
99  // read weights from file - for debugging. Even if it is one single files, 4 files should b set in the vector
101  TFile file0(edm::FileInPath(cfg_.ecalRegressionWeightFiles[0].c_str()).fullPath().c_str());
102  ecalRegBarrel_ = (const GBRForest*)file0.Get(cfg_.ecalRegressionWeightLabels[0].c_str());
103  file0.Close();
104  TFile file1(edm::FileInPath(cfg_.ecalRegressionWeightFiles[1].c_str()).fullPath().c_str());
106  file1.Close();
107  TFile file2(edm::FileInPath(cfg_.ecalRegressionWeightFiles[2].c_str()).fullPath().c_str());
109  file2.Close();
110  TFile file3(edm::FileInPath(cfg_.ecalRegressionWeightFiles[3].c_str()).fullPath().c_str());
111  ecalRegErrorEndcap_ = (const GBRForest*)file3.Get(cfg_.ecalRegressionWeightLabels[3].c_str());
113  file3.Close();
114  }
115 
118  TFile file0(edm::FileInPath(cfg_.combinationRegressionWeightFiles[0].c_str()).fullPath().c_str());
119  combinationReg_ = (const GBRForest*)file0.Get(cfg_.combinationRegressionWeightLabels[0].c_str());
121  file0.Close();
122  }
123 }
124 
126 
129  const edm::Handle<EcalRecHitCollection>& rechitsEB,
130  const edm::Handle<EcalRecHitCollection>& rechitsEE,
131  double& energyFactor,
132  double& errorFactor) const {
133  energyFactor = -999.;
134  errorFactor = -999.;
135 
136  std::vector<float> rInputs;
137  EcalRegressionData regData;
138  regData.fill(sc, rechitsEB.product(), rechitsEE.product(), caloGeometry_, caloTopology_, vertices.product());
139  regData.fillVec(rInputs);
140  if (sc.seed()->hitsAndFractions()[0].first.subdetId() == EcalBarrel) {
141  energyFactor = ecalRegBarrel_->GetResponse(&rInputs[0]);
142  errorFactor = ecalRegErrorBarrel_->GetResponse(&rInputs[0]);
143  } else if (sc.seed()->hitsAndFractions()[0].first.subdetId() == EcalEndcap) {
144  energyFactor = ecalRegEndcap_->GetResponse(&rInputs[0]);
145  errorFactor = ecalRegErrorEndcap_->GetResponse(&rInputs[0]);
146  } else {
147  throw cms::Exception("RegressionHelper::calculateRegressedEnergy")
148  << "Supercluster seed is either EB nor EE!" << std::endl;
149  }
150 }
151 
153  float energy = ele.correctedEcalEnergy();
154  float energyError = ele.correctedEcalEnergyError();
155  float momentum = ele.trackMomentumAtVtx().R();
156  float momentumError = ele.trackMomentumError();
157  int elClass = -1;
158 
159  switch (ele.classification()) {
161  elClass = 0;
162  break;
164  elClass = 1;
165  break;
167  elClass = 2;
168  break;
170  elClass = 3;
171  break;
173  elClass = 4;
174  break;
175  default:
176  elClass = -1;
177  }
178 
179  bool isEcalDriven = ele.ecalDriven();
180  bool isTrackerDriven = ele.trackerDrivenSeed();
181  bool isEB = ele.isEB();
182 
183  // compute relative errors and ratio of errors
184  float energyRelError = energyError / energy;
185  float momentumRelError = momentumError / momentum;
186  float errorRatio = energyRelError / momentumRelError;
187 
188  // calculate E/p and corresponding error
189  float eOverP = energy / momentum;
190  float eOverPerror = eOverP * std::hypot(energyRelError, momentumRelError);
191 
192  // fill input variables
193  std::vector<float> regressionInputs;
194  regressionInputs.resize(11, 0.);
195 
196  regressionInputs[0] = energy;
197  regressionInputs[1] = energyRelError;
198  regressionInputs[2] = momentum;
199  regressionInputs[3] = momentumRelError;
200  regressionInputs[4] = errorRatio;
201  regressionInputs[5] = eOverP;
202  regressionInputs[6] = eOverPerror;
203  regressionInputs[7] = static_cast<float>(isEcalDriven);
204  regressionInputs[8] = static_cast<float>(isTrackerDriven);
205  regressionInputs[9] = static_cast<float>(elClass);
206  regressionInputs[10] = static_cast<float>(isEB);
207 
208  // retrieve combination weight
209  float weight = 0.;
210  if (eOverP > 0.025 &&
211  fabs(momentum - energy) < 15. * sqrt(momentumError * momentumError +
212  energyError * energyError)) // protection against crazy track measurement
213  {
214  weight = combinationReg_->GetResponse(regressionInputs.data());
215  if (weight > 1.)
216  weight = 1.;
217  else if (weight < 0.)
218  weight = 0.;
219  }
220 
221  float combinedMomentum = weight * momentum + (1. - weight) * energy;
222  float combinedMomentumError =
223  sqrt(weight * weight * momentumError * momentumError + (1. - weight) * (1. - weight) * energyError * energyError);
224 
225  // FIXME : pure tracker electrons have track momentum error of 999.
226  // If the combination try to combine such electrons then the original combined momentum is kept
227  if (momentumError != 999. || weight == 0.) {
228  math::XYZTLorentzVector oldMomentum = ele.p4();
229  math::XYZTLorentzVector newMomentum(oldMomentum.x() * combinedMomentum / oldMomentum.t(),
230  oldMomentum.y() * combinedMomentum / oldMomentum.t(),
231  oldMomentum.z() * combinedMomentum / oldMomentum.t(),
232  combinedMomentum);
233 
234  ele.setP4(reco::GsfElectron::P4_COMBINATION, newMomentum, combinedMomentumError, true);
235  }
236 }
double GetResponse(const float *vector) const
Definition: GBRForest.h:49
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:188
std::vector< std::string > combinationRegressionWeightLabels
unsigned long long regressionCacheId_
float trackMomentumError() const
Definition: GsfElectron.h:800
const GBRForest * ecalRegEndcap_
const LorentzVector & p4(P4Kind kind) const
Definition: GsfElectron.cc:211
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:268
Definition: weight.py:1
Definition: config.py:1
const Configuration cfg_
PRODUCT const & get(ESGetToken< PRODUCT, T > const &iToken) const
bool isEB() const
Definition: GsfElectron.h:328
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:170
T sqrt(T t)
Definition: SSEVec.h:19
void fillVec(std::vector< float > &inputVec) const
std::vector< std::string > combinationRegressionWeightFiles
float correctedEcalEnergy() const
Definition: GsfElectron.h:798
T const * product() const
Definition: Handle.h:69
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:722
void readEvent(const edm::Event &)
const GBRForest * ecalRegErrorEndcap_
float correctedEcalEnergyError() const
Definition: GsfElectron.h:799
void setCorrectedEcalEnergy(float newEnergy)
Definition: GsfElectron.cc:174
void applyEcalRegression(reco::GsfElectron &electron, const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< EcalRecHitCollection > &rechitsEB, const edm::Handle< EcalRecHitCollection > &rechitsEE) const
T get() const
Definition: EventSetup.h:73
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:155
bool trackerDrivenSeed() const
Definition: GsfElectron.h:159
const CaloClusterPtr & seed() const
seed BasicCluster
Definition: SuperCluster.h:77
RegressionHelper(const Configuration &)
bool ecalDriven() const
Definition: GsfElectron.cc:168
bool combinationRegressionInitialized_
void checkSetup(const edm::EventSetup &)
const CaloGeometry * caloGeometry_
const GBRForest * combinationReg_