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 }
EgHLTOffHistBins_cfi.eOverP
eOverP
Definition: EgHLTOffHistBins_cfi.py:37
reco::GsfElectron::isEB
bool isEB() const
Definition: GsfElectron.h:336
RegressionHelper::Configuration::ecalRegressionWeightLabels
std::vector< std::string > ecalRegressionWeightLabels
Definition: RegressionHelper.h:27
funct::false
false
Definition: Factorize.h:34
edm::Handle::product
T const * product() const
Definition: Handle.h:70
RegressionHelper::ecalRegEndcap_
const GBRForest * ecalRegEndcap_
Definition: RegressionHelper.h:67
RegressionHelper::RegressionHelper
RegressionHelper(const Configuration &)
Definition: RegressionHelper.cc:11
RegressionHelper::Configuration::combinationRegressionWeightLabels
std::vector< std::string > combinationRegressionWeightLabels
Definition: RegressionHelper.h:30
reco::SuperCluster
Definition: SuperCluster.h:18
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
CaloGeometryRecord
Definition: CaloGeometryRecord.h:30
RegressionHelper::caloGeometryCacheId_
unsigned long long caloGeometryCacheId_
Definition: RegressionHelper.h:62
GBRWrapperRcd.h
mps_merge.weight
weight
Definition: mps_merge.py:88
GBRForest
Definition: GBRForest.h:25
RegressionHelper::combinationReg_
const GBRForest * combinationReg_
Definition: RegressionHelper.h:70
RegressionHelper::caloTopologyCacheId_
unsigned long long caloTopologyCacheId_
Definition: RegressionHelper.h:61
reco::GsfElectron::P4_COMBINATION
Definition: GsfElectron.h:769
RegressionHelper::readEvent
void readEvent(const edm::Event &)
Definition: RegressionHelper.cc:125
EcalRegressionData
Definition: EcalRegressionData.h:17
RegressionHelper::ecalRegErrorBarrel_
const GBRForest * ecalRegErrorBarrel_
Definition: RegressionHelper.h:68
CaloTopologyRecord
Definition: CaloTopologyRecord.h:10
RegressionHelper::ecalRegBarrel_
const GBRForest * ecalRegBarrel_
Definition: RegressionHelper.h:66
edmOneToOneComparison.file2
file2
Definition: edmOneToOneComparison.py:107
edm::Handle< reco::VertexCollection >
EcalBarrel
Definition: EcalSubdetector.h:10
RegressionHelper::Configuration::combinationWeightsFromDB
bool combinationWeightsFromDB
Definition: RegressionHelper.h:31
RegressionHelper::applyCombinationRegression
void applyCombinationRegression(reco::GsfElectron &ele) const
Definition: RegressionHelper.cc:152
RegressionHelper::applyEcalRegression
void applyEcalRegression(reco::GsfElectron &electron, const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< EcalRecHitCollection > &rechitsEB, const edm::Handle< EcalRecHitCollection > &rechitsEE) const
Definition: RegressionHelper.cc:23
deltaR.h
GBRForest::GetResponse
double GetResponse(const float *vector) const
Definition: GBRForest.h:49
reco::GsfElectron::SHOWERING
Definition: GsfElectron.h:724
config
Definition: config.py:1
edm::FileInPath
Definition: FileInPath.h:64
reco::GsfElectron::setP4
void setP4(P4Kind kind, const LorentzVector &p4, float p4Error, bool setCandidate)
Definition: GsfElectron.cc:188
EcalRegressionData::fill
void fill(const reco::SuperCluster &superClus, const EcalRecHitCollection *ebRecHits, const EcalRecHitCollection *eeRecHits, const CaloGeometry *geom, const CaloTopology *topology, const reco::VertexCollection *vertices)
Definition: EcalRegressionData.h:82
edm::EventSetup::get
T get() const
Definition: EventSetup.h:73
RegressionHelper::cfg_
const Configuration cfg_
Definition: RegressionHelper.h:55
RegressionHelper::caloTopology_
const CaloTopology * caloTopology_
Definition: RegressionHelper.h:56
RegressionHelper.h
RegressionHelper::Configuration::combinationRegressionWeightFiles
std::vector< std::string > combinationRegressionWeightFiles
Definition: RegressionHelper.h:32
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
reco::GsfElectron::trackMomentumAtVtx
math::XYZVectorF trackMomentumAtVtx() const
Definition: GsfElectron.h:276
reco::GsfElectron::GOLDEN
Definition: GsfElectron.h:724
edm::ESHandle< CaloTopology >
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
reco::GsfElectron
Definition: GsfElectron.h:35
reco::GsfElectron::correctedEcalEnergyError
float correctedEcalEnergyError() const
Definition: GsfElectron.h:807
RegressionHelper::combinationRegressionInitialized_
bool combinationRegressionInitialized_
Definition: RegressionHelper.h:59
RegressionHelper::ecalRegErrorEndcap_
const GBRForest * ecalRegErrorEndcap_
Definition: RegressionHelper.h:69
EcalEndcap
Definition: EcalSubdetector.h:10
RegressionHelper::regressionCacheId_
unsigned long long regressionCacheId_
Definition: RegressionHelper.h:63
RegressionHelper::caloGeometry_
const CaloGeometry * caloGeometry_
Definition: RegressionHelper.h:57
reco::GsfElectron::setCorrectedEcalEnergyError
void setCorrectedEcalEnergyError(float newEnergyError)
Definition: GsfElectron.cc:170
reco::SuperCluster::seed
const CaloClusterPtr & seed() const
seed BasicCluster
Definition: SuperCluster.h:77
runTheMatrix.err
err
Definition: runTheMatrix.py:288
EcalClusterTools.h
EcalRegressionData::fillVec
void fillVec(std::vector< float > &inputVec) const
Definition: EcalRegressionData.cc:158
reco::GsfElectron::ecalDriven
bool ecalDriven() const
Definition: GsfElectron.cc:168
edm::EventSetup
Definition: EventSetup.h:57
get
#define get
reco::GsfElectron::p4
const LorentzVector & p4(P4Kind kind) const
Definition: GsfElectron.cc:211
RegressionHelper::checkSetup
void checkSetup(const edm::EventSetup &)
Definition: RegressionHelper.cc:33
reco::GsfElectron::BIGBREM
Definition: GsfElectron.h:724
CaloTopologyRecord.h
reco::GsfElectron::correctedEcalEnergy
float correctedEcalEnergy() const
Definition: GsfElectron.h:806
timingPdfMaker.file1
file1
Definition: timingPdfMaker.py:79
EcalRegressionData.h
math::XYZTLorentzVector
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
RegressionHelper::getEcalRegression
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
Definition: RegressionHelper.cc:127
Exception
Definition: hltDiff.cc:246
RegressionHelper::Configuration::ecalWeightsFromDB
bool ecalWeightsFromDB
Definition: RegressionHelper.h:28
reco::GsfElectron::classification
Classification classification() const
Definition: GsfElectron.h:730
RegressionHelper::ecalRegressionInitialized_
bool ecalRegressionInitialized_
Definition: RegressionHelper.h:58
GBRWrapperRcd
Definition: GBRWrapperRcd.h:24
reco::GsfElectron::GAP
Definition: GsfElectron.h:724
reco::GsfElectron::trackerDrivenSeed
bool trackerDrivenSeed() const
Definition: GsfElectron.h:167
reco::GsfElectron::superCluster
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:163
reco::GsfElectron::trackMomentumError
float trackMomentumError() const
Definition: GsfElectron.h:808
edm::eventsetup::EventSetupRecordImplementation::get
PRODUCT const & get(ESGetToken< PRODUCT, T > const &iToken) const
Definition: EventSetupRecordImplementation.h:74
reco::GsfElectron::setCorrectedEcalEnergy
void setCorrectedEcalEnergy(float newEnergy)
Definition: GsfElectron.cc:174
RegressionHelper::~RegressionHelper
~RegressionHelper()
Definition: RegressionHelper.cc:21
reco::GsfElectron::BADTRACK
Definition: GsfElectron.h:724
edm::Event
Definition: Event.h:73
RegressionHelper::Configuration::ecalRegressionWeightFiles
std::vector< std::string > ecalRegressionWeightFiles
Definition: RegressionHelper.h:29
weight
Definition: weight.py:1
RegressionHelper::Configuration
Definition: RegressionHelper.h:25
pwdgSkimBPark_cfi.vertices
vertices
Definition: pwdgSkimBPark_cfi.py:7