CMS 3D CMS Logo

SignAlgoResolutions.cc
Go to the documentation of this file.
2 // -*- C++ -*-
3 //
4 // Package: METAlgorithms
5 // Class: SignAlgoResolutions
6 //
14 //
15 // Original Author: Kyle Story, Freya Blekman (Cornell University)
16 // Created: Fri Apr 18 11:58:33 CEST 2008
17 //
18 //
25 
26 #include <cmath>
27 
28 #include <cstdlib>
29 #include <iostream>
30 #include <sstream>
31 #include <string>
32 #include <sys/stat.h>
33 
35  : functionmap_(), ptResol_(nullptr), phiResol_(nullptr) {
36  addResolutions(iConfig);
37 }
38 
40  const resolutionFunc &func,
41  const double &et,
42  const double &phi,
43  const double &eta) const {
44  // derive p from et and eta;
45  double theta = 2 * atan(exp(-eta));
46  double p = et / sin(theta); // rough assumption: take e and p equivalent...
47  return eval(type, func, et, phi, eta, p);
48 }
49 
51  const resolutionFunc &func,
52  const double &et,
53  const double &phi,
54  const double &eta,
55  const double &p) const {
56  functionPars x(4);
57  x[0] = et;
58  x[1] = phi;
59  x[2] = eta;
60  x[3] = p;
61  // std::cout << "getting function of type " << type << " " << func << " " << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << std::endl;
62  return getfunc(type, func, x);
63 }
64 
66  double eta = candidate->eta();
67  double phi = candidate->phi();
68  double et = candidate->energy() * sin(candidate->theta());
69  resolutionType thetype;
71  int type = candidate->particleId();
72  switch (type) {
73  case 1:
74  thetype = PFtype1;
75  name = "PFChargedHadron";
76  break;
77  case 2:
78  thetype = PFtype2;
79  name = "PFChargedEM";
80  break;
81  case 3:
82  thetype = PFtype3;
83  name = "PFMuon";
84  break;
85  case 4:
86  thetype = PFtype4;
87  name = "PFNeutralEM";
88  break;
89  case 5:
90  thetype = PFtype5;
91  name = "PFNeutralHadron";
92  break;
93  case 6:
94  thetype = PFtype6;
95  name = "PFtype6";
96  break;
97  case 7:
98  thetype = PFtype7;
99  name = "PFtype7";
100  break;
101  default:
102  thetype = PFtype7;
103  name = "PFunknown";
104  break;
105  }
106 
107  double d_et = 0, d_phi = 0; //d_phi here is the error on phi component of the et
108  reco::TrackRef trackRef = candidate->trackRef();
109  if (!trackRef.isNull()) {
110  d_phi = et * trackRef->phiError();
111  d_et = (type == 2) ? ElectronPtResolution(candidate) : trackRef->ptError();
112  //if(type==2) std::cout << eval(thetype,ET,et,phi,eta) << " " << trackRef->ptError() << " "<< ElectronPtResolution(candidate) << std::endl;
113  } else {
114  d_et = eval(thetype, ET, et, phi, eta);
115  d_phi = eval(thetype, PHI, et, phi, eta);
116  }
117 
118  metsig::SigInputObj resultingobj(name, et, phi, d_et, d_phi);
119  return resultingobj;
120 }
121 
123  double jpt = jet->pt();
124  double jphi = jet->phi();
125  double jeta = jet->eta();
126  double jdeltapt = 999.;
127  double jdeltapphi = 999.;
128 
129  if (jpt < ptResolThreshold_ && jpt < 20.) { //use temporary fix for low pT jets
130  double feta = TMath::Abs(jeta);
131  int ieta = feta < 5. ? int(feta / 0.5) : 9; //bin size = 0.5
132  int ipt = jpt > 3. ? int(jpt - 3. / 2) : 0; //bin size =2, starting from ptmin=3GeV
133  jdeltapt = jdpt[ieta][ipt];
134  jdeltapphi = jpt * jdphi[ieta][ipt];
135  } else {
136  //use the resolution functions at |eta|=5 to avoid crash for jets with large eta.
137  if (jeta > 5)
138  jeta = 5;
139  if (jeta < -5)
140  jeta = -5;
141  double jptForEval = jpt > ptResolThreshold_ ? jpt : ptResolThreshold_;
142  jdeltapt = jpt * ptResol_->parameterEtaEval("sigma", jeta, jptForEval);
143  jdeltapphi = jpt * phiResol_->parameterEtaEval("sigma", jeta, jptForEval);
144  }
145 
146  std::string inputtype = "jet";
147  metsig::SigInputObj obj_jet(inputtype, jpt, jphi, jdeltapt, jdeltapphi);
148  //std::cout << "RESOLUTIONS JET: " << jpt << " " << jphi<< " " <<jdeltapt << " " << jdeltapphi << std::endl;
149  return obj_jet;
150 }
151 
153  using namespace std;
154 
155  // Jet Resolutions - for now load from the files. Migrate to EventSetup asap.
157 
158  ptResolThreshold_ = iConfig.getParameter<double>("ptresolthreshold");
159 
160  //get temporary low pT pfjet resolutions
161  for (int ieta = 0; ieta < 10; ieta++) {
162  jdpt[ieta] = iConfig.getParameter<std::vector<double> >(Form("jdpt%d", ieta));
163  jdphi[ieta] = iConfig.getParameter<std::vector<double> >(Form("jdphi%d", ieta));
164  }
165 
166  // for now: do this by hand - this can obviously also be done via ESSource etc.
167  functionPars etparameters(3, 0);
168  functionPars phiparameters(1, 0);
169  // set the parameters per function:
170  // ECAL, BARREL:
171  std::vector<double> ebet = iConfig.getParameter<std::vector<double> >("EB_EtResPar");
172  std::vector<double> ebphi = iConfig.getParameter<std::vector<double> >("EB_PhiResPar");
173 
174  etparameters[0] = ebet[0];
175  etparameters[1] = ebet[1];
176  etparameters[2] = ebet[2];
177  phiparameters[0] = ebphi[0];
178  addfunction(caloEB, ET, etparameters);
179  addfunction(caloEB, PHI, phiparameters);
180  // ECAL, ENDCAP:
181  std::vector<double> eeet = iConfig.getParameter<std::vector<double> >("EE_EtResPar");
182  std::vector<double> eephi = iConfig.getParameter<std::vector<double> >("EE_PhiResPar");
183 
184  etparameters[0] = eeet[0];
185  etparameters[1] = eeet[1];
186  etparameters[2] = eeet[2];
187  phiparameters[0] = eephi[0];
188  addfunction(caloEE, ET, etparameters);
189  addfunction(caloEE, PHI, phiparameters);
190  // HCAL, BARREL:
191  std::vector<double> hbet = iConfig.getParameter<std::vector<double> >("HB_EtResPar");
192  std::vector<double> hbphi = iConfig.getParameter<std::vector<double> >("HB_PhiResPar");
193 
194  etparameters[0] = hbet[0];
195  etparameters[1] = hbet[1];
196  etparameters[2] = hbet[2];
197  phiparameters[0] = hbphi[0];
198  addfunction(caloHB, ET, etparameters);
199  addfunction(caloHB, PHI, phiparameters);
200  // HCAL, ENDCAP:
201  std::vector<double> heet = iConfig.getParameter<std::vector<double> >("HE_EtResPar");
202  std::vector<double> hephi = iConfig.getParameter<std::vector<double> >("HE_PhiResPar");
203 
204  etparameters[0] = heet[0];
205  etparameters[1] = heet[1];
206  etparameters[2] = heet[2];
207  phiparameters[0] = hephi[0];
208  addfunction(caloHE, ET, etparameters);
209  addfunction(caloHE, PHI, phiparameters);
210  // HCAL, Outer
211  std::vector<double> hoet = iConfig.getParameter<std::vector<double> >("HO_EtResPar");
212  std::vector<double> hophi = iConfig.getParameter<std::vector<double> >("HO_PhiResPar");
213 
214  etparameters[0] = hoet[0];
215  etparameters[1] = hoet[1];
216  etparameters[2] = hoet[2];
217  phiparameters[0] = hophi[0];
218  addfunction(caloHO, ET, etparameters);
219  addfunction(caloHO, PHI, phiparameters);
220  // HCAL, Forward
221  std::vector<double> hfet = iConfig.getParameter<std::vector<double> >("HF_EtResPar");
222  std::vector<double> hfphi = iConfig.getParameter<std::vector<double> >("HF_PhiResPar");
223 
224  etparameters[0] = hfet[0];
225  etparameters[1] = hfet[1];
226  etparameters[2] = hfet[2];
227  phiparameters[0] = hfphi[0];
228  addfunction(caloHF, ET, etparameters);
229  addfunction(caloHF, PHI, phiparameters);
230 
231  // PF objects:
232  // type 1:
233  std::vector<double> pf1et = iConfig.getParameter<std::vector<double> >("PF_EtResType1");
234  std::vector<double> pf1phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType1");
235  etparameters[0] = pf1et[0];
236  etparameters[1] = pf1et[1];
237  etparameters[2] = pf1et[2];
238  phiparameters[0] = pf1phi[0];
239  addfunction(PFtype1, ET, etparameters);
240  addfunction(PFtype1, PHI, phiparameters);
241 
242  // PF objects:
243  // type 2:
244  std::vector<double> pf2et = iConfig.getParameter<std::vector<double> >("PF_EtResType2");
245  std::vector<double> pf2phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType2");
246  etparameters[0] = pf2et[0];
247  etparameters[1] = pf2et[1];
248  etparameters[2] = pf2et[2];
249  phiparameters[0] = pf2phi[0];
250  addfunction(PFtype2, ET, etparameters);
251  addfunction(PFtype2, PHI, phiparameters);
252 
253  // PF objects:
254  // type 3:
255  std::vector<double> pf3et = iConfig.getParameter<std::vector<double> >("PF_EtResType3");
256  std::vector<double> pf3phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType3");
257  etparameters[0] = pf3et[0];
258  etparameters[1] = pf3et[1];
259  etparameters[2] = pf3et[2];
260  phiparameters[0] = pf3phi[0];
261  addfunction(PFtype3, ET, etparameters);
262  addfunction(PFtype3, PHI, phiparameters);
263 
264  // PF objects:
265  // type 4:
266  std::vector<double> pf4et = iConfig.getParameter<std::vector<double> >("PF_EtResType4");
267  std::vector<double> pf4phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType4");
268  etparameters[0] = pf4et[0];
269  etparameters[1] = pf4et[1];
270  etparameters[2] = pf4et[2];
271  //phiparameters[0]=pf4phi[0];
272  addfunction(PFtype4, ET, etparameters);
273  addfunction(PFtype4, PHI, pf4phi); //use the same functional form for photon phi error as for pT, pass whole vector
274 
275  // PF objects:
276  // type 5:
277  std::vector<double> pf5et = iConfig.getParameter<std::vector<double> >("PF_EtResType5");
278  std::vector<double> pf5phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType5");
279  etparameters[0] = pf5et[0];
280  etparameters[1] = pf5et[1];
281  etparameters[2] = pf5et[2];
282  phiparameters[0] = pf5phi[0];
283  addfunction(PFtype5, ET, etparameters);
284  addfunction(PFtype5, PHI, pf5phi);
285 
286  // PF objects:
287  // type 6:
288  std::vector<double> pf6et = iConfig.getParameter<std::vector<double> >("PF_EtResType6");
289  std::vector<double> pf6phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType6");
290  etparameters[0] = pf6et[0];
291  etparameters[1] = pf6et[1];
292  etparameters[2] = pf6et[2];
293  phiparameters[0] = pf6phi[0];
294  addfunction(PFtype6, ET, etparameters);
295  addfunction(PFtype6, PHI, phiparameters);
296 
297  // PF objects:
298  // type 7:
299  std::vector<double> pf7et = iConfig.getParameter<std::vector<double> >("PF_EtResType7");
300  std::vector<double> pf7phi = iConfig.getParameter<std::vector<double> >("PF_PhiResType7");
301  etparameters[0] = pf7et[0];
302  etparameters[1] = pf7et[1];
303  etparameters[2] = pf7et[2];
304  phiparameters[0] = pf7phi[0];
305  addfunction(PFtype7, ET, etparameters);
306  addfunction(PFtype7, PHI, phiparameters);
307 
308  return;
309 }
310 
313  const functionPars &parameters) {
314  functionCombo mypair(type, func);
315  functionmap_[mypair] = parameters;
316 }
317 
320  functionPars &x) const {
321  double result = 0;
322  functionCombo mypair(type, func);
323 
324  if (functionmap_.count(mypair) == 0) {
325  return result;
326  }
327 
328  functionPars values = (functionmap_.find(mypair))->second;
329  switch (func) {
330  case metsig::ET:
331  return EtFunction(x, values);
332  case metsig::PHI:
333  return PhiFunction(x, values);
334  case metsig::TRACKP:
335  return PFunction(x, values);
336  case metsig::CONSTPHI:
337  return PhiConstFunction(x, values);
338  }
339 
340  // std::cout << "returning function " << type << " " << func << " " << result << " " << x[0] << std::endl;
341 
342  return result;
343 }
344 
346  if (par.size() < 3)
347  return 0.;
348  if (x.empty())
349  return 0.;
350  double et = x[0];
351  if (et <= 0.)
352  return 0.;
353  double result = et * sqrt((par[2] * par[2]) + (par[1] * par[1] / et) + (par[0] * par[0] / (et * et)));
354  return result;
355 }
356 
358  double et = x[0];
359  if (et <= 0.) {
360  return 0.;
361  }
362 
363  //if 1 parameter is C provided, returns C*pT, if three parameters N, S, C are provided, it returns the usual resolution value, as for sigmaPt
364  if (par.size() != 1 && par.size() != 3) { //only 1 or 3 parameters supported for phi function
365  return 0.;
366  } else if (par.size() == 1) {
367  return par[0] * et;
368  } else {
369  return et * sqrt((par[2] * par[2]) + (par[1] * par[1] / et) + (par[0] * par[0] / (et * et)));
370  }
371 }
373  // not currently implemented
374  return 0;
375 }
376 
378  return par[0];
379 }
380 
382  using namespace std;
383 
384  // only reinitialize the resolutsion if the pointers are zero
385  if (ptResol_ == nullptr) {
386  string resolutionsAlgo = iConfig.getParameter<std::string>("resolutionsAlgo");
387  string resolutionsEra = iConfig.getParameter<std::string>("resolutionsEra");
388  string ptFileName = edm::FileInPath("CondFormats/JetMETObjects/data/" + resolutionsEra + "_PtResolution_" +
389  resolutionsAlgo + ".txt")
390  .fullPath();
391  string phiFileName = edm::FileInPath("CondFormats/JetMETObjects/data/" + resolutionsEra + "_PhiResolution_" +
392  resolutionsAlgo + ".txt")
393  .fullPath();
394  ptResol_ = new JetResolution(ptFileName, false);
395  phiResol_ = new JetResolution(phiFileName, false);
396  }
397 }
398 
400  double eta = c->eta();
401  double energy = c->energy();
402  double dEnergy = pfresol_->getEnergyResolutionEm(energy, eta);
403 
404  return dEnergy / cosh(eta);
405 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
double EtFunction(const functionPars &x, const functionPars &par) const
void addfunction(const resolutionType type, const resolutionFunc func, const std::vector< double > &parameters)
metsig::SigInputObj evalPFJet(const reco::Jet *jet) const
Base class for all types of Jets.
Definition: Jet.h:20
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
double PFunction(const functionPars &x, const functionPars &par) const
U second(std::pair< T, U > const &p)
double theta() const final
momentum polar angle
T sqrt(T t)
Definition: SSEVec.h:23
double PhiFunction(const functionPars &x, const functionPars &par) const
metsig::SigInputObj evalPF(const reco::PFCandidate *candidate) const
bool isNull() const
Checks for null.
Definition: Ref.h:229
std::vector< double > functionPars
double eval(const resolutionType &type, const resolutionFunc &func, const double &et, const double &phi, const double &eta, const double &p) const
double ElectronPtResolution(const reco::PFCandidate *c) const
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
void initializeJetResolutions(const edm::ParameterSet &iConfig)
float x
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:437
const std::string & fullPath() const
Definition: FileInPath.cc:144
double PhiConstFunction(const functionPars &x, const functionPars &par) const
double phi() const final
momentum azimuthal angle
std::pair< metsig::resolutionType, metsig::resolutionFunc > functionCombo
double getfunc(const resolutionType &type, const resolutionFunc &func, std::vector< double > &x) const
void addResolutions(const edm::ParameterSet &iConfig)
double energy() const final
energy
virtual ParticleType particleId() const
Definition: PFCandidate.h:392
double eta() const final
momentum pseudorapidity