CMS 3D CMS Logo

PhotonEnergyCalibrator.cc
Go to the documentation of this file.
2 
7 #include <CLHEP/Random/RandGaussQ.h>
8 
11 
13  correctionRetriever_(correctionFile),
14  rng_(nullptr),
15  minEt_(1.0)
16 {
17 
18 }
19 
21 {
22  rng_ = rnd;
23 }
24 
25 std::array<float,EGEnergySysIndex::kNrSysErrs> PhotonEnergyCalibrator::
26 calibrate(reco::Photon &photon,const unsigned int runNumber,
27  const EcalRecHitCollection *recHits,
28  edm::StreamID const & id,
29  const PhotonEnergyCalibrator::EventType eventType) const
30 {
31  return calibrate(photon,runNumber,recHits,gauss(id),eventType);
32 }
33 
34 std::array<float,EGEnergySysIndex::kNrSysErrs> PhotonEnergyCalibrator::
35 calibrate(reco::Photon &photon,const unsigned int runNumber,
36  const EcalRecHitCollection *recHits,
37  const float smearNrSigma,
38  const PhotonEnergyCalibrator::EventType eventType) const
39 {
40  const float scEtaAbs = std::abs(photon.superCluster()->eta());
41  const float et = photon.getCorrectedEnergy(reco::Photon::P4type::regression2) / cosh(scEtaAbs);
42 
43  if (et < minEt_ || edm::isNotFinite(et) ) {
44  std::array<float,EGEnergySysIndex::kNrSysErrs> retVal;
45  retVal.fill(photon.getCorrectedEnergy(reco::Photon::P4type::regression2));
46  retVal[EGEnergySysIndex::kScaleValue] = 1.0;
47  retVal[EGEnergySysIndex::kSmearValue] = 0.0;
48  retVal[EGEnergySysIndex::kSmearNrSigma] = smearNrSigma;
49  retVal[EGEnergySysIndex::kEcalErrPreCorr] = photon.getCorrectedEnergyError(reco::Photon::P4type::regression2);
50  retVal[EGEnergySysIndex::kEcalErrPostCorr] = photon.getCorrectedEnergyError(reco::Photon::P4type::regression2);
55 
56  return retVal;
57  }
58 
59  const DetId seedDetId = photon.superCluster()->seed()->seed();
60  EcalRecHitCollection::const_iterator seedRecHit = recHits->find(seedDetId);
61  unsigned int gainSeedSC = 12;
62  if (seedRecHit != recHits->end()) {
63  if(seedRecHit->checkFlag(EcalRecHit::kHasSwitchToGain6)) gainSeedSC = 6;
64  if(seedRecHit->checkFlag(EcalRecHit::kHasSwitchToGain1)) gainSeedSC = 1;
65  }
66 
67  const EnergyScaleCorrection::ScaleCorrection* scaleCorr = correctionRetriever_.getScaleCorr(runNumber, et, scEtaAbs, photon.full5x5_r9(), gainSeedSC);
68  const EnergyScaleCorrection::SmearCorrection* smearCorr = correctionRetriever_.getSmearCorr(runNumber, et, scEtaAbs, photon.full5x5_r9(), gainSeedSC);
69  if(scaleCorr==nullptr) scaleCorr=&defaultScaleCorr_;
70  if(smearCorr==nullptr) smearCorr=&defaultSmearCorr_;
71 
72 
73  std::array<float,EGEnergySysIndex::kNrSysErrs> uncertainties{};
74 
75  uncertainties[EGEnergySysIndex::kScaleValue] = scaleCorr->scale();
76  uncertainties[EGEnergySysIndex::kSmearValue] = smearCorr->sigma(et); //even though we use scale = 1.0, we still store the value returned for MC
77  uncertainties[EGEnergySysIndex::kSmearNrSigma] = smearNrSigma;
78  //MC central values are not scaled (scale = 1.0), data is not smeared (smearNrSigma = 0)
79  //smearing still has a second order effect on data as it enters the E/p combination as an
80  //extra uncertainty on the calo energy
81  //MC gets all the scale systematics
82  if(eventType == EventType::DATA){
83  setEnergyAndSystVarations(scaleCorr->scale(),0.,et,*scaleCorr,*smearCorr,photon,uncertainties);
84  }else if(eventType == EventType::MC){
85  setEnergyAndSystVarations(1.0,smearNrSigma,et,*scaleCorr,*smearCorr,photon,uncertainties);
86  }
87 
88  return uncertainties;
89 
90 }
91 
93 setEnergyAndSystVarations(const float scale,const float smearNrSigma,const float et,
97  std::array<float,EGEnergySysIndex::kNrSysErrs>& energyData)const
98 {
99 
100  const float smear = smearCorr.sigma(et);
101  const float smearRhoUp = smearCorr.sigma(et,1,0);
102  const float smearRhoDn = smearCorr.sigma(et,-1,0);
103  const float smearPhiUp = smearCorr.sigma(et,0,1);
104  const float smearPhiDn = smearCorr.sigma(et,0,-1);
105 
106  const float corr = scale + smear * smearNrSigma;
107  const float corrRhoUp = scale + smearRhoUp * smearNrSigma;
108  const float corrRhoDn = scale + smearRhoDn * smearNrSigma;
109  const float corrPhiUp = scale + smearPhiUp * smearNrSigma;
110  const float corrPhiDn = scale + smearPhiDn * smearNrSigma;
111  const float corrUp = corrRhoUp;
112  const float corrDn = corrRhoDn;
113 
114 
115  const double oldEcalEnergy = photon.getCorrectedEnergy(reco::Photon::P4type::regression2);
116  const double oldEcalEnergyError = photon.getCorrectedEnergyError(reco::Photon::P4type::regression2);
117 
118  energyData[EGEnergySysIndex::kEcalPreCorr] = oldEcalEnergy;
119  energyData[EGEnergySysIndex::kEcalErrPreCorr] = oldEcalEnergyError;
120 
121  const double newEcalEnergy = oldEcalEnergy * corr;
122  const double newEcalEnergyError = std::hypot(oldEcalEnergyError * corr, smear * newEcalEnergy);
123  photon.setCorrectedEnergy(reco::Photon::P4type::regression2, newEcalEnergy, newEcalEnergyError, true);
124 
125  energyData[EGEnergySysIndex::kScaleStatUp] = oldEcalEnergy * (corr + scaleCorr.scaleErrStat());
126  energyData[EGEnergySysIndex::kScaleStatDown] = oldEcalEnergy * (corr - scaleCorr.scaleErrStat());
127  energyData[EGEnergySysIndex::kScaleSystUp] = oldEcalEnergy * (corr + scaleCorr.scaleErrSyst());
128  energyData[EGEnergySysIndex::kScaleSystDown] = oldEcalEnergy * (corr - scaleCorr.scaleErrSyst());
129  energyData[EGEnergySysIndex::kScaleGainUp] = oldEcalEnergy * (corr + scaleCorr.scaleErrGain());
130  energyData[EGEnergySysIndex::kScaleGainDown] = oldEcalEnergy * (corr - scaleCorr.scaleErrGain());
131  energyData[EGEnergySysIndex::kSmearRhoUp] = oldEcalEnergy * corrRhoUp;
132  energyData[EGEnergySysIndex::kSmearRhoDown] = oldEcalEnergy * corrRhoDn;
133  energyData[EGEnergySysIndex::kSmearPhiUp] = oldEcalEnergy * corrPhiUp;
134  energyData[EGEnergySysIndex::kSmearPhiDown] = oldEcalEnergy * corrPhiDn;
135 
136  // The total variation
137  energyData[EGEnergySysIndex::kScaleUp] = oldEcalEnergy * (corr + scaleCorr.scaleErr(EnergyScaleCorrection::kErrStatSystGain));
138  energyData[EGEnergySysIndex::kScaleDown] = oldEcalEnergy * (corr - scaleCorr.scaleErr(EnergyScaleCorrection::kErrStatSystGain));
139  energyData[EGEnergySysIndex::kSmearUp] = oldEcalEnergy * corrUp;
140  energyData[EGEnergySysIndex::kSmearDown] = oldEcalEnergy * corrDn;
141 
142 
143  energyData[EGEnergySysIndex::kEcalPostCorr] = photon.getCorrectedEnergy(reco::Photon::P4type::regression2);
144  energyData[EGEnergySysIndex::kEcalErrPostCorr] = photon.getCorrectedEnergyError(reco::Photon::P4type::regression2);
145 }
146 
147 
149 {
150  if (rng_) {
151  return rng_->Gaus();
152  } else {
154  if ( !rng.isAvailable() ) {
155  throw cms::Exception("Configuration")
156  << "XXXXXXX requires the RandomNumberGeneratorService\n"
157  "which is not present in the configuration file. You must add the service\n"
158  "in the configuration file or remove the modules that require it.";
159  }
160  CLHEP::RandGaussQ gaussDistribution(rng->getEngine(id), 0.0, 1.0);
161  return gaussDistribution.fire();
162  }
163 }
164 
EnergyScaleCorrection correctionRetriever_
void initPrivateRng(TRandom *rnd)
void setCorrectedEnergy(P4type type, float E, float dE, bool toCand=true)
std::vector< EcalRecHit >::const_iterator const_iterator
#define nullptr
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
float full5x5_r9() const
Definition: Photon.h:246
bool isNotFinite(T x)
Definition: isFinite.h:10
const ScaleCorrection * getScaleCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed) const
bool isAvailable() const
Definition: Service.h:46
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const SmearCorrection * getSmearCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed) const
static const EnergyScaleCorrection::ScaleCorrection defaultScaleCorr_
static const EnergyScaleCorrection::SmearCorrection defaultSmearCorr_
float getCorrectedEnergyError(P4type type) const
JetCorrectorParameters corr
Definition: classes.h:5
float scaleErr(const std::bitset< kErrNrBits > &uncBitMask) const
float sigma(const float et, const float nrSigmaRho=0., const float nrSigmaPhi=0.) const
const_iterator end() const
Definition: DetId.h:18
et
define resolution functions of each parameter
float getCorrectedEnergy(P4type type) const
double gauss(edm::StreamID const &id) const
iterator find(key_type k)
void setEnergyAndSystVarations(const float scale, const float smearNrSigma, const float et, const EnergyScaleCorrection::ScaleCorrection &scaleCorr, const EnergyScaleCorrection::SmearCorrection &smearCorr, reco::Photon &photon, std::array< float, EGEnergySysIndex::kNrSysErrs > &energyData) const
std::array< float, EGEnergySysIndex::kNrSysErrs > calibrate(reco::Photon &photon, const unsigned int runNumber, const EcalRecHitCollection *recHits, edm::StreamID const &id, const EventType eventType) const