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),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
110  !cfg_.ecalRegressionWeightFiles.empty() ) {
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());
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 
194  bool isEcalDriven = ele.ecalDriven();
195  bool isTrackerDriven = ele.trackerDrivenSeed();
196  bool isEB = ele.isEB();
197 
198  // compute relative errors and ratio of errors
199  float energyRelError = energyError / energy;
200  float momentumRelError = momentumError / momentum;
201  float errorRatio = energyRelError / momentumRelError;
202 
203  // calculate E/p and corresponding error
204  float eOverP = energy / momentum;
205  float eOverPerror = eOverP*std::hypot(energyRelError,momentumRelError);
206 
207  // fill input variables
208  std::vector<float> regressionInputs ;
209  regressionInputs.resize(11,0.);
210 
211  regressionInputs[0] = energy;
212  regressionInputs[1] = energyRelError;
213  regressionInputs[2] = momentum;
214  regressionInputs[3] = momentumRelError;
215  regressionInputs[4] = errorRatio;
216  regressionInputs[5] = eOverP;
217  regressionInputs[6] = eOverPerror;
218  regressionInputs[7] = static_cast<float>(isEcalDriven);
219  regressionInputs[8] = static_cast<float>(isTrackerDriven);
220  regressionInputs[9] = static_cast<float>(elClass);
221  regressionInputs[10] = static_cast<float>(isEB);
222 
223  // retrieve combination weight
224  float weight = 0.;
225  if(eOverP>0.025
226  &&fabs(momentum-energy)<15.*sqrt(momentumError*momentumError + energyError*energyError)
227  ) // protection against crazy track measurement
228  {
229  weight = combinationReg_->GetResponse(regressionInputs.data());
230  if(weight>1.) weight = 1.;
231  else if(weight<0.) weight = 0.;
232  }
233 
234  float combinedMomentum = weight*momentum + (1.-weight)*energy;
235  float combinedMomentumError = sqrt(weight*weight*momentumError*momentumError + (1.-weight)*(1.-weight)*energyError*energyError);
236 
237  // FIXME : pure tracker electrons have track momentum error of 999.
238  // If the combination try to combine such electrons then the original combined momentum is kept
239  if(momentumError!=999. || weight==0.)
240  {
241  math::XYZTLorentzVector oldMomentum = ele.p4() ;
242  math::XYZTLorentzVector newMomentum(oldMomentum.x()*combinedMomentum/oldMomentum.t(),
243  oldMomentum.y()*combinedMomentum/oldMomentum.t(),
244  oldMomentum.z()*combinedMomentum/oldMomentum.t(),
245  combinedMomentum);
246 
247  ele.setP4(reco::GsfElectron::P4_COMBINATION,newMomentum,combinedMomentumError,true);
248  }
249 }
double GetResponse(const float *vector) const
Definition: GBRForest.h:59
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:199
std::vector< std::string > combinationRegressionWeightLabels
unsigned long long regressionCacheId_
float trackMomentumError() const
Definition: GsfElectron.h:825
const GBRForest * ecalRegEndcap_
const LorentzVector & p4(P4Kind kind) const
Definition: GsfElectron.cc:225
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:291
Definition: weight.py:1
Definition: config.py:1
const Configuration cfg_
bool isEB() const
Definition: GsfElectron.h:352
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:179
T sqrt(T t)
Definition: SSEVec.h:18
void get(HolderT &iHolder) const
void fillVec(std::vector< float > &inputVec) const
std::vector< std::string > combinationRegressionWeightFiles
float correctedEcalEnergy() const
Definition: GsfElectron.h:823
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:746
const T & get() const
Definition: EventSetup.h:58
void readEvent(const edm::Event &)
const GBRForest * ecalRegErrorEndcap_
float correctedEcalEnergyError() const
Definition: GsfElectron.h:824
void setCorrectedEcalEnergy(float newEnergy)
Definition: GsfElectron.cc:182
void applyEcalRegression(reco::GsfElectron &electron, const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< EcalRecHitCollection > &rechitsEB, const edm::Handle< EcalRecHitCollection > &rechitsEE) const
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:184
bool trackerDrivenSeed() const
Definition: GsfElectron.h:189
const CaloClusterPtr & seed() const
seed BasicCluster
Definition: SuperCluster.h:66
RegressionHelper(const Configuration &)
bool ecalDriven() const
Definition: GsfElectron.cc:174
bool combinationRegressionInitialized_
void checkSetup(const edm::EventSetup &)
float energyError(float E, float *par)
const CaloGeometry * caloGeometry_
const GBRForest * combinationReg_