CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuScleFitCorrector.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Heppy_MuScleFitCorrector_h
2 #define PhysicsTools_Heppy_MuScleFitCorrector_h
3 
4 
12 #include <iostream>
13 #include <fstream>
14 #include <sstream>
16 #include "TLorentzVector.h"
17 #include "TRandom3.h"
18 
19 
26 namespace heppy {
27 
29 {
30  public:
37  MuScleFitCorrector(const TString& identifier)
38  {
39  fileName_ = identifier;
40  readParameters( identifier );
41  gRandom_ = new TRandom3();
42  }
43  // dummy empty constructor to allow dictionaries
46 
47  // Returns a pointer to the selected function
50 
51  // Method to get correct pt
52  double getCorrectPt(const TLorentzVector &lorentzVector, const int & chg){
53 
54  // Loop on all the functions and apply them iteratively on the pt corrected by the previous function.
55  double pt = lorentzVector.Pt();
56  pt = ( scaleFunction_->scale( pt, lorentzVector.Eta(), lorentzVector.Phi(), chg, scaleParArray_) );
57  return pt;
58  };
59 
60  // Return the squared difference of relative pT resolutions data-MC
61  double getSigmaPtDiffSquared(const double & pt, const double & eta){
62 
63  double sigmaPtData = resolutionFunction_->sigmaPt(pt, eta, resolDataParArray_);
64  double sigmaPtMC = resolutionFunction_->sigmaPt(pt, eta, resolMCParArray_);
65  return (sigmaPtData*sigmaPtData - sigmaPtMC*sigmaPtMC);
66 
67  };
68 
69 
70  // Method to get correct pt (baseline)
71  double getSmearedPt(const TLorentzVector & lorentzVector, const int & chg, bool fake){
72 
73  double pt = lorentzVector.Pt();
74  double eta = lorentzVector.Eta();
75  double squaredDiff = getSigmaPtDiffSquared(pt,eta);
76  if (squaredDiff < 0) return pt;
77  double Cfact = 0.;
78  if (fileName_.Contains("smearPrompt")) Cfact = 0.85; // smear Summer12 against 2012 Prompt data
79  else if (fileName_.Contains("smearReReco")) Cfact = 0.75; // smear Summer12 against 2012 ReReco data
80  else if (fileName_.Contains("2011_MC")) Cfact = 0.8; // smear Fall11 against 2011 ReReco data
81  else {
82  std::cout<<"Are you sure you want to smear data??"<<std::endl;
83  exit(1);
84  }
85  double sPar = Cfact*sqrt(squaredDiff);
86  double curv = ((double)chg/pt);
87  double normSmearFact = gRandom_->Gaus(0,sPar);
88  if (fake) normSmearFact = sPar;
89  double smearedCurv = curv + fabs(curv)*normSmearFact;
90 
91  return 1./((double)chg*smearedCurv);
92 
93  };
94 
95  // Method to apply correction to a TLorentzVector
96  void applyPtCorrection(TLorentzVector& lorentzVector, const int & chg){
97 
98  double eta = lorentzVector.Eta();
99  double phi = lorentzVector.Phi();
100  double m = lorentzVector.M();
101  double corrPt = getCorrectPt(lorentzVector, chg);
102  lorentzVector.SetPtEtaPhiM(corrPt,eta,phi,m);
103 
104  };
105 
106  // Method to apply smearing to a TLorentzVector
107  void applyPtSmearing(TLorentzVector& lorentzVector, const int & chg, bool fake=false){
108  double eta = lorentzVector.Eta();
109  double phi = lorentzVector.Phi();
110  double m = lorentzVector.M();
111  double smearedPt = getSmearedPt(lorentzVector, chg, fake);
112  lorentzVector.SetPtEtaPhiM(smearedPt,eta,phi,m);
113  };
114 
115  std::vector<double> getResolMCParVec(){ return resolMCParVec_; }
116 
117 
118  protected:
119 
120  // File name
121  TString fileName_;
122 
123  // Identification numbers
126 
127  // Vectors of parameters
128  std::vector<double> scaleParVec_;
129  std::vector<double> resolDataParVec_;
130  std::vector<double> resolMCParVec_;
131 
132  // We will use the array for the function calls because it is faster than the vector for random access.
133  double * scaleParArray_;
136 
137  // Convert vectors to arrays for faster random access. The first pointer is replaced, thus it is taken by reference.
139 
140  int resolParNum = resolutionFunction_->parNum();
141  int scaleParNum = scaleFunction_->parNum();
142 
143  int resolDataParVecSize = resolDataParVec_.size();
144  int resolMCParVecSize = resolMCParVec_.size();
145  int scaleParVecSize = scaleParVec_.size();
146 
147  useResol_ = false;
148  if (resolDataParVecSize!=0 && resolMCParVecSize!=0) useResol_ = true;
149 
150  if( resolParNum != resolDataParVecSize && useResol_) {
151  std::cout << "Error: inconsistent number of parameters: resolutionFunction has "<<resolParNum<<" parameters but "<<resolDataParVecSize<<" have been read from file" << std::endl;
152  exit(1);
153  }
154 
155  if( resolParNum != resolMCParVecSize && useResol_) {
156  std::cout << "Error: inconsistent number of parameters: resolutionFunction has "<<resolParNum<<" parameters but "<<resolMCParVecSize<<" have been read from file" << std::endl;
157  exit(1);
158  }
159 
160  if( scaleParNum != scaleParVecSize ) {
161  std::cout << "Error: inconsistent number of parameters: scaleFunction has "<<scaleParNum<<" parameters but "<<scaleParVecSize<<" have been read from file" << std::endl;
162  exit(1);
163  }
164 
165 
166  std::vector<double>::const_iterator scaleParIt = scaleParVec_.begin();
167  std::vector<double>::const_iterator resolDataParIt = resolDataParVec_.begin();
168  std::vector<double>::const_iterator resolMCParIt = resolMCParVec_.begin();
169 
170  scaleParArray_ = new double[scaleParNum];
171  resolDataParArray_ = new double[resolParNum];
172  resolMCParArray_ = new double[resolParNum];
173 
174  for ( int iPar=0; iPar<scaleParNum; ++iPar) {
175  double parameter = *scaleParIt;
176  scaleParArray_[iPar] = parameter;
177  ++scaleParIt;
178  }
179 
180  if (useResol_){
181  for ( int iPar=0; iPar<resolParNum; ++iPar) {
182  double parameter = *resolDataParIt;
183  resolDataParArray_[iPar] = parameter;
184  ++resolDataParIt;
185  }
186 
187  for ( int iPar=0; iPar<resolParNum; ++iPar) {
188  double parameter = *resolMCParIt;
189  resolMCParArray_[iPar] = parameter;
190  ++resolMCParIt;
191  }
192  }
193 
194  }
195  //----------------------------//
196 
197 
198  // Parser of the parameters file
199  void readParameters(const TString& fileName){
200  scaleParArray_ = 0;
201  resolDataParArray_ = 0;
202  resolMCParArray_ = 0;
203 
204  // Read the parameters file
205  ifstream parametersFile(fileName.Data());
206 
207  if( !parametersFile.is_open() ) {
208  std::cout << "Error: file " << fileName << " not found. Aborting." << std::endl;
209  abort();
210  }
211 
213  int nReadPar = 0;
214  std::string iteration("Iteration ");
215  // Loop on the file lines
216  while (parametersFile) {
217 
218  getline( parametersFile, line );
219  size_t lineInt = line.find("value");
220  size_t iterationSubStr = line.find(iteration);
221 
222  if( iterationSubStr != std::string::npos ) {
223 
224  std::stringstream sLine(line);
226  int scaleFunctionNum = 0;
227  int resolutionFunctionNum = 0;
228  int wordCounter = 0;
229 
230  // Warning: this strongly depends on the parameters file structure.
231  while( sLine >> num ) {
232  ++wordCounter;
233  if( wordCounter == 8 ) {
234  std::stringstream in(num);
235  in >> resolutionFunctionNum;
236  }
237 
238  if( wordCounter == 9 ) {
239  std::stringstream in(num);
240  in >> scaleFunctionNum;
241  }
242  }
243 
244  scaleFunctionId_ = scaleFunctionNum;
245  scaleFunction_ = scaleFunctionService(scaleFunctionNum);
246  resolutionFunctionId_ = resolutionFunctionNum;
247  resolutionFunction_ = resolutionFunctionService(resolutionFunctionNum);
248 
249  /* std::cout<<"Function IDs: "<<std::endl; */
250  /* std::cout<<" scale function number "<<scaleFunctionId_<<std::endl; */
251  /* std::cout<<" resolution function number "<<resolutionFunctionId_<<std::endl; */
252 
253  }
254 
255  int nScalePar = scaleFunction_->parNum();
256  int nResolPar = resolutionFunction_->parNum();
257 
258  if ( (lineInt != std::string::npos) ) {
259  size_t subStr1 = line.find("value");
260  std::stringstream paramStr;
261  double param = 0;
262  paramStr << line.substr(subStr1+5);
263  paramStr >> param;
264  // Fill the last vector of parameters, which corresponds to this iteration.
265  if (nReadPar<nScalePar) {
266  scaleParVec_.push_back(param);
267  }
268  else if (nReadPar < (nScalePar+nResolPar) ) {
269  resolDataParVec_.push_back(param);
270  }
271  else if (nReadPar < (nScalePar+2*nResolPar) ) {
272  resolMCParVec_.push_back(param);
273  }
274  ++nReadPar;
275  }
276  }
277  convertToArrays();
278 
279  /* std::cout<<"Scale function n. "<<scaleFunctionId_<<" has "<<scaleFunction_->parNum()<<"parameters:"<<std::endl; */
280  /* for (int ii=0; ii<scaleFunction_->parNum(); ++ii){ */
281  /* std::cout<<"par["<<ii<<"] = "<<scaleParArray_[ii]<<std::endl; */
282  /* } */
283 
284  if (useResol_){
285 
286  /* std::cout<<"Resolution data function n. "<<resolutionFunctionId_<<" has "<<resolutionFunction_->parNum()<<"parameters:"<<std::endl; */
287  /* for (int ii=0; ii<resolutionFunction_->parNum(); ++ii){ */
288  /* std::cout<<"par["<<ii<<"] = "<<resolDataParArray_[ii]<<std::endl; */
289  /* } */
290 
291  /* std::cout<<"Resolution MC function n. "<<resolutionFunctionId_<<" has "<<resolutionFunction_->parNum()<<"parameters:"<<std::endl; */
292  /* for (int ii=0; ii<resolutionFunction_->parNum(); ++ii){ */
293  /* std::cout<<"par["<<ii<<"] = "<<resolMCParArray_[ii]<<std::endl; */
294  /* } */
295 
296  }
297  }
298 
299 
300  // Functions
303 
304  // Pointer for TRandom3 access
305  TRandom3 * gRandom_;
306 
307  // Bool for using resolution function or not (value depends from the information on the parameters txt file)
308  bool useResol_;
309 };
310 }
311 
312 #endif
void readParameters(const TString &fileName)
scaleFunctionBase< double * > * scaleFunctionService(const int identifier)
Service to build the scale functor corresponding to the passed identifier.
Definition: Functions.cc:3
MuScleFitCorrector(const TString &identifier)
std::vector< double > resolDataParVec_
const float chg[109]
Definition: CoreSimTrack.cc:5
double getSmearedPt(const TLorentzVector &lorentzVector, const int &chg, bool fake)
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
std::vector< double > scaleParVec_
virtual double sigmaPt(const double &pt, const double &eta, const T &parval)=0
std::vector< double > resolMCParVec_
resolutionFunctionBase< double * > * getResolFunction()
scaleFunctionBase< double * > * scaleFunction_
resolutionFunctionBase< double * > * resolutionFunction_
tuple iteration
Definition: align_cfg.py:5
virtual int parNum() const
Definition: Functions.h:704
T sqrt(T t)
Definition: SSEVec.h:48
std::vector< double > getResolMCParVec()
virtual double scale(const double &pt, const double &eta, const double &phi, const int chg, const T &parScale) const =0
Geom::Phi< T > phi() const
void applyPtSmearing(TLorentzVector &lorentzVector, const int &chg, bool fake=false)
double getSigmaPtDiffSquared(const double &pt, const double &eta)
resolutionFunctionBase< double * > * resolutionFunctionService(const int identifier)
Service to build the resolution functor corresponding to the passed identifier.
Definition: Functions.cc:38
tuple cout
Definition: gather_cfg.py:121
virtual int parNum() const
Definition: Functions.h:66
double getCorrectPt(const TLorentzVector &lorentzVector, const int &chg)
void applyPtCorrection(TLorentzVector &lorentzVector, const int &chg)
scaleFunctionBase< double * > * getScaleFunction()