CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetResolution.cc
Go to the documentation of this file.
1 //
3 // JetResolution
4 // -------------
5 //
6 // 11/05/2010 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
8 
9 
12 
14 
15 #include <TMath.h>
16 
17 
18 #include <iostream>
19 #include <sstream>
20 #include <cassert>
21 
22 
23 using namespace std;
24 
25 
27 // GLOBAL FUNCTION DEFINITIONS
29 
30 //______________________________________________________________________________
31 double fnc_dscb(double*xx,double*pp);
32 double fnc_gaussalpha(double*xx,double*pp);
33 double fnc_gaussalpha1alpha2(double*xx,double*pp);
34 
35 
37 // CONSTRUCTION / DESTRUCTION
39 
40 //______________________________________________________________________________
42  : resolutionFnc_(0)
43 {
44  resolutionFnc_ = new TF1();
45 }
46 
47 
48 //______________________________________________________________________________
49 JetResolution::JetResolution(const string& fileName,bool doGaussian)
50  : resolutionFnc_(0)
51 {
52  initialize(fileName,doGaussian);
53 }
54 
55 
56 //______________________________________________________________________________
58 {
59  delete resolutionFnc_;
60  for (unsigned i=0;i<parameterFncs_.size();i++) delete parameterFncs_[i];
61  for (unsigned i=0;i<parameters_.size();i++) delete parameters_[i];
62 }
63 
64 
66 // IMPLEMENTATION OF MEMBER FUNCTIONS
68 
69 //______________________________________________________________________________
70 void JetResolution::initialize(const string& fileName,bool doGaussian)
71 {
72  size_t pos;
73 
74  name_ = fileName;
75  pos = name_.find_last_of('.'); name_ = name_.substr(0,pos);
76  pos = name_.find_last_of('/'); name_ = name_.substr(pos+1);
77 
78  JetCorrectorParameters resolutionPars(fileName,"resolution");
79  string fncname = "fResolution_" + name_;
80  string formula = resolutionPars.definitions().formula();
81  if (doGaussian) resolutionFnc_=new TF1(fncname.c_str(),"gaus",0.,5.);
82  else if (formula=="DSCB") resolutionFnc_=new TF1(fncname.c_str(),fnc_dscb,0.,5.,7);
83  else if (formula=="GaussAlpha1Alpha2") resolutionFnc_=new TF1(fncname.c_str(),fnc_gaussalpha1alpha2,-5.,5.,5);
84  else if (formula=="GaussAlpha") resolutionFnc_=new TF1(fncname.c_str(),fnc_gaussalpha,-5.,5.,4);
85  else resolutionFnc_=new TF1(fncname.c_str(),formula.c_str(),0.,5.);
86 
87  resolutionFnc_->SetNpx(200);
88  resolutionFnc_->SetParName(0,"N");
89  resolutionFnc_->SetParameter(0,1.0);
90  unsigned nPar(1);
91 
92  string tmp = resolutionPars.definitions().level();
93  pos = tmp.find(':');
94  while (!tmp.empty()) {
95  string paramAsStr = tmp.substr(0,pos);
96  if (!doGaussian||paramAsStr=="mean"||paramAsStr=="sigma") {
97  parameters_.push_back(new JetCorrectorParameters(fileName,paramAsStr));
98  formula = parameters_.back()->definitions().formula();
99  parameterFncs_.push_back(new TF1(("f"+paramAsStr+"_"+name()).c_str(),formula.c_str(),
100  parameters_.back()->record(0).parameters()[0],
101  parameters_.back()->record(0).parameters()[1]));
102  resolutionFnc_->SetParName(nPar,parameters_.back()->definitions().level().c_str());
103  nPar++;
104  }
105  tmp = (pos==string::npos) ? "" : tmp.substr(pos+1);
106  pos = tmp.find(':');
107  }
108 
109  assert(nPar==(unsigned)resolutionFnc_->GetNpar());
110  assert(!doGaussian||nPar==3);
111 }
112 
113 
114 //______________________________________________________________________________
115 TF1* JetResolution::resolutionEtaPt(float eta, float pt) const
116 {
117  vector<float> x; x.push_back(eta);
118  vector<float> y; y.push_back(pt);
119  return resolution(x,y);
120 }
121 
122 
123 //______________________________________________________________________________
124 TF1* JetResolution::resolution(const vector<float>& x,
125  const vector<float>& y) const
126 {
127  unsigned N(y.size());
128  for (unsigned iPar=0;iPar<parameters_.size();iPar++) {
129  int bin = parameters_[iPar]->binIndex(x);
130  assert(bin>=0);
131  assert(bin<(int)parameters_[iPar]->size());
132  const std::vector<float>& pars = parameters_[iPar]->record(bin).parameters();
133  for (unsigned i=2*N;i<pars.size();i++)
134  parameterFncs_[iPar]->SetParameter(i-2*N,pars[i]);
135  float yy[4];
136  for (unsigned i=0;i<N;i++)
137  yy[i] = (y[i] < pars[2*i]) ? pars[2*i] : (y[i] > pars[2*i+1]) ? pars[2*i+1] : y[i];
138  resolutionFnc_->SetParameter(iPar+1,
139  parameterFncs_[iPar]->Eval(yy[0],yy[1],yy[2],yy[3]));
140  }
141  return resolutionFnc_;
142 }
143 
144 
145 //______________________________________________________________________________
146 TF1* JetResolution::parameterEta(const string& parameterName, float eta)
147 {
148  vector<float> x; x.push_back(eta);
149  return parameter(parameterName,x);
150 }
151 
152 
153 //______________________________________________________________________________
154 TF1* JetResolution::parameter(const string& parameterName,const vector<float>& x)
155 {
156  TF1* result(0);
157  for (unsigned i=0;i<parameterFncs_.size()&&result==0;i++) {
158  string fncname = parameterFncs_[i]->GetName();
159  if (fncname.find("f"+parameterName)==0) {
160  stringstream ssname; ssname<<parameterFncs_[i]->GetName();
161  for (unsigned ii=0;ii<x.size();ii++)
162  ssname<<"_"<<parameters_[i]->definitions().binVar(ii)<<x[ii];
163  result = (TF1*)parameterFncs_[i]->Clone();
164  result->SetName(ssname.str().c_str());
165  int N = parameters_[i]->definitions().nParVar();
166  int bin = parameters_[i]->binIndex(x);
167  assert(bin>=0);
168  assert(bin<(int)parameters_[i]->size());
169  const std::vector<float>& pars = parameters_[i]->record(bin).parameters();
170  for (unsigned ii=2*N;ii<pars.size();ii++) result->SetParameter(ii-2*N,pars[ii]);
171  }
172  }
173 
174  if (0==result) cerr<<"JetResolution::parameter() ERROR: no parameter "
175  <<parameterName<<" found."<<endl;
176 
177  return result;
178 }
179 
180 
181 //______________________________________________________________________________
182 double JetResolution::parameterEtaEval(const std::string& parameterName, float eta, float pt)
183 {
184  TF1* func(0);
185  JetCorrectorParameters* params(0);
186  for (std::vector<TF1*>::size_type ifunc = 0; ifunc < parameterFncs_.size(); ++ifunc)
187  {
188  std::string fncname = parameterFncs_[ifunc]->GetName();
189  if ( !(fncname.find("f"+parameterName) == 0) ) continue;
190  params = parameters_[ifunc];
191  func = (TF1*)parameterFncs_[ifunc];
192  break;
193  }
194 
195  if (!func)
196  edm::LogError("ParameterNotFound") << "JetResolution::parameterEtaEval(): no parameter \""
197  << parameterName << "\" found" << std::endl;
198 
199  std::vector<float> etas; etas.push_back(eta);
200  int bin = params->binIndex(etas);
201 
202  if ( !(0 <= bin && bin < (int)params->size() ) )
203  edm::LogError("ParameterNotFound") << "JetResolution::parameterEtaEval(): bin out of range: "
204  << bin << std::endl;
205 
206  const std::vector<float>& pars = params->record(bin).parameters();
207 
208  int N = params->definitions().nParVar();
209  for (unsigned ii = 2*N; ii < pars.size(); ++ii)
210  {
211  func->SetParameter(ii-2*N, pars[ii]);
212  }
213 
214  return func->Eval(pt);
215 }
216 
217 
219 // IMPLEMENTATION OF GLOBAL FUNCTIONS
221 
222 //______________________________________________________________________________
223 double fnc_dscb(double*xx,double*pp)
224 {
225  double x = xx[0];
226  double N = pp[0];
227  double mu = pp[1];
228  double sig = pp[2];
229  double a1 = pp[3];
230  double p1 = pp[4];
231  double a2 = pp[5];
232  double p2 = pp[6];
233 
234  double u = (x-mu)/sig;
235  double A1 = TMath::Power(p1/TMath::Abs(a1),p1)*TMath::Exp(-a1*a1/2);
236  double A2 = TMath::Power(p2/TMath::Abs(a2),p2)*TMath::Exp(-a2*a2/2);
237  double B1 = p1/TMath::Abs(a1) - TMath::Abs(a1);
238  double B2 = p2/TMath::Abs(a2) - TMath::Abs(a2);
239 
240  double result(N);
241  if (u<-a1) result *= A1*TMath::Power(B1-u,-p1);
242  else if (u<a2) result *= TMath::Exp(-u*u/2);
243  else result *= A2*TMath::Power(B2+u,-p2);
244  return result;
245 }
246 
247 
248 //______________________________________________________________________________
249 double fnc_gaussalpha(double *v, double *par)
250 {
251  double N =par[0];
252  double mean =par[1];
253  double sigma=par[2];
254  double alpha=par[3];
255  double t =TMath::Abs((v[0]-mean)/sigma);
256  double cut = 1.0;
257  return (t<=cut) ? N*TMath::Exp(-0.5*t*t) : N*TMath::Exp(-0.5*(alpha*(t-cut)+cut*cut));
258 }
259 
260 
261 //______________________________________________________________________________
262 double fnc_gaussalpha1alpha2(double *v, double *par)
263 {
264  double N =par[0];
265  double mean =par[1];
266  double sigma =par[2];
267  double alpha1=par[3];
268  double alpha2=par[4];
269  double t =TMath::Abs((v[0]-mean)/sigma);
270  double cut = 1.0;
271  return
272  (t<=cut) ? N*TMath::Exp(-0.5*t*t) :
273  ((v[0]-mean)>=0) ? N*TMath::Exp(-0.5*(alpha1*(t-cut)+cut*cut)) :
274  N*TMath::Exp(-0.5*(alpha2*(t-cut)+cut*cut));
275 }
276 
const std::string & name() const
Definition: JetResolution.h:32
int i
Definition: DBlmapReader.cc:9
float alpha
Definition: AMPTWrapper.h:95
void initialize(const std::string &fileName, bool doGaussian=false)
std::string name_
Definition: JetResolution.h:47
TF1 * resolutionFnc_
Definition: JetResolution.h:48
tuple pp
Definition: createTree.py:15
double fnc_dscb(double *xx, double *pp)
TF1 * parameterEta(const std::string &parameterName, float eta)
double fnc_gaussalpha(double *xx, double *pp)
std::vector< float > parameters() const
const Definitions & definitions() const
std::vector< TF1 * > parameterFncs_
Definition: JetResolution.h:49
virtual ~JetResolution()
const Record & record(unsigned fBin) const
std::vector< JetCorrectorParameters * > parameters_
Definition: JetResolution.h:50
T eta() const
int ii
Definition: cuy.py:588
uint16_t size_type
double parameterEtaEval(const std::string &parameterName, float eta, float pt)
int binIndex(const std::vector< float > &fX) const
TF1 * resolution(const std::vector< float > &x, const std::vector< float > &y) const
tuple result
Definition: query.py:137
const int mu
Definition: Constants.h:22
double p2[4]
Definition: TauolaWrapper.h:90
TF1 * parameter(const std::string &parameterName, const std::vector< float > &x)
#define N
Definition: blowfish.cc:9
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double p1[4]
Definition: TauolaWrapper.h:89
string func
Definition: statics.py:48
Definition: DDAxes.h:10
TF1 * resolutionEtaPt(float eta, float pt) const
double fnc_gaussalpha1alpha2(double *xx, double *pp)
tuple size
Write out results.