CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFClusterCalibration.cc
Go to the documentation of this file.
4 
5 #include <cmath>
6 #include <cassert>
7 #include <TBranch.h>
8 #include <TF1.h>
9 #include <TTree.h>
10 
11 using namespace pftools;
12 
14 
15  //std::cout << __PRETTY_FUNCTION__ << std::endl;
16  correction_ = new TF1("correction",
17  "((x-[0])/[1])*(x>[4])+((x-[2])/[3])*(x<[4])");
19  = new TF1( "etaCorrection",
20  "(1-[0]*x-[1]*x*x)*(x<[2])+([3]+[4]*x)*(x>[2]&&x<[5])+(1-[6]*x-[7]*x*x)*(x>[5])");
21 
22  correction_->FixParameter(0, globalP0_);
23  correction_->FixParameter(1, globalP1_);
24  correction_->FixParameter(2, lowEP0_);
25  correction_->FixParameter(3, lowEP1_);
26  correction_->FixParameter(4, correctionLowLimit_);
27 
28  /* These are the types of calibration I know about:
29  * ecalOnly_elementName
30  * etc. Sorry, it's not very nice, but well, neither is ROOT... */
31 
32  std::string eheb("ecalHcalEcalBarrel");
33  names_.push_back(eheb);
34  std::string ehee("ecalHcalEcalEndcap");
35  names_.push_back(ehee);
36  std::string ehhb("ecalHcalHcalBarrel");
37  names_.push_back(ehhb);
38  std::string ehhe("ecalHcalHcalEndcap");
39  names_.push_back(ehhe);
40 
41  /* char
42  * funcString("([0]*[5]*x*([1]-[5]*x)/pow(([2]+[5]*x),3)+[3]*pow([5]*x, 0.1))*([5]*x<[8] && [5]*x>[7])+[4]*([5]*x>[8])+([6]*[5]*x)*([5]*x<[7])");
43  */
44 
45  const char*
46  funcString("([0]*[5]*x)*([5]*x<=[1])+([2]+[3]*exp([4]*[5]*x))*([5]*x>[1])");
47 
48  //Create functions for each sector
49  for (std::vector<std::string>::const_iterator cit = names_.begin(); cit
50  != names_.end(); ++cit) {
51  std::string name = *cit;
52  TF1 func(name.c_str(), funcString);
53  //some sensible defaults
54  func.FixParameter(0, 1);
55  func.FixParameter(1, 0);
56  func.FixParameter(2, 1);
57  func.FixParameter(3, 0);
58  func.FixParameter(4, 0);
59  func.FixParameter(5, 1);
60 
61  func.SetMinimum(0);
62  //Store in map
63  namesAndFunctions_[name] = func;
64 
65  }
66 }
67 
68 /*PFClusterCalibration::PFClusterCalibration(IO* options_) :
69  barrelEndcapEtaDiv_(1.0), ecalOnlyDiv_(0.3), hcalOnlyDiv_(0.5),
70  doCorrection_(1), allowNegativeEnergy_(0), doEtaCorrection_(1),
71  maxEToCorrect_(-1.0), correctionLowLimit_(0.), globalP0_(0.0),
72  globalP1_(1.0), lowEP0_(0.0), lowEP1_(1.0) {
73 
74  init();
75 
76  double g0, g1, e0, e1;
77  options_->GetOpt("correction", "globalP0", g0);
78  options_->GetOpt("correction", "globalP1", g1);
79  options_->GetOpt("correction", "lowEP0", e0);
80  options_->GetOpt("correction", "lowEP1", e1);
81  setCorrections(e0, e1, g0, g1);
82 
83  options_->GetOpt("correction", "allowNegativeEnergy", allowNegativeEnergy_);
84  options_->GetOpt("correction", "doCorrection", doCorrection_);
85  options_->GetOpt("evolution", "barrelEndcapEtaDiv", barrelEndcapEtaDiv_);
86 
87  std::vector<std::string>* names = getKnownSectorNames();
88  for (std::vector<std::string>::iterator i = names->begin(); i
89  != names->end(); ++i) {
90  std::string sector = *i;
91  std::vector<double> params;
92  options_->GetOpt("evolution", sector.c_str(), params);
93  setEvolutionParameters(sector, params);
94  }
95 
96  options_->GetOpt("evolution", "doEtaCorrection", doEtaCorrection_);
97 
98  std::vector<double> etaParams;
99  options_->GetOpt("evolution", "etaCorrection", etaParams);
100  setEtaCorrectionParameters(etaParams);
101 
102 } */
103 
105  barrelEndcapEtaDiv_(1.0), ecalOnlyDiv_(0.3), hcalOnlyDiv_(0.5),
106  doCorrection_(1), allowNegativeEnergy_(0), doEtaCorrection_(1),
107  maxEToCorrect_(-1.0), correctionLowLimit_(0.), globalP0_(0.0),
108  globalP1_(1.0), lowEP0_(0.0), lowEP1_(1.0) {
109  // std::cout << __PRETTY_FUNCTION__ << std::endl;
110  init();
111  // std::cout
112  // << "WARNING! PFClusterCalibration evolution functions have not yet been initialised - ensure this is done.\n";
113  // std::cout << "PFClusterCalibration construction complete."<< std::endl;
114 
115 }
116 
118  delete correction_;
119  delete etaCorrection_;
120 }
121 
122 void PFClusterCalibration::setEtaCorrectionParameters(const std::vector<double>& params) {
123  if (params.size() != 6) {
124  std::cout << __PRETTY_FUNCTION__ << ": params is of the wrong length."
125  << std::endl;
126  return;
127  }
128  // std::cout << "Fixing eta correction:\n\t";
129  unsigned count(0);
130  for (std::vector<double>::const_iterator dit = params.begin(); dit
131  != params.end(); ++dit) {
132  //std::cout << *dit << "\t";
133  etaCorrection_->FixParameter(count, *dit);
134  ++count;
135  }
136  // std::cout << std::endl;
137  /*for(double eta(0); eta < 2.5; eta += 0.05) {
138  std::cout << "Eta = " << eta << ",\tcorr = " << etaCorrection_->Eval(eta) << "\n";
139  }*/
140 }
141 
143  const std::vector<double>& params) {
144  TF1* func = &(namesAndFunctions_.find(sector)->second);
145  unsigned count(0);
146  //std::cout << "Fixing for "<< sector << "\n";
147  for (std::vector<double>::const_iterator dit = params.begin(); dit
148  != params.end(); ++dit) {
149  func->FixParameter(count, *dit);
150  //std::cout << "\t"<< count << ": "<< *dit;
151  ++count;
152  }
153  // std::cout << std::endl;
154  func->SetMinimum(0);
155 }
156 
157 void PFClusterCalibration::setCorrections(const double& lowEP0,
158  const double& lowEP1, const double& globalP0, const double& globalP1) {
159  //'a' term is -globalP0/globalP1
160  globalP0_ = globalP0;
161  globalP1_ = globalP1;
162  //Specifically for low energies...
163  lowEP0_ = lowEP0;
164  lowEP1_ = lowEP1;
165  //Intersection of two straight lines => matching at...
167 
168  correction_->FixParameter(0, globalP0_);
169  correction_->FixParameter(1, globalP1_);
170  correction_->FixParameter(2, lowEP0_);
171  correction_->FixParameter(3, lowEP1_);
172  correction_->FixParameter(4, correctionLowLimit_);
173 
174  // std::cout << __PRETTY_FUNCTION__ << ": setting correctionLowLimit_ = "
175  // << correctionLowLimit_ << "\n";
176 }
177 
179  const double& hcalE, const double& eta, const double& phi) const {
180  const TF1* theFunction(0);
181 
182  if (fabs(eta) < barrelEndcapEtaDiv_) {
183  //barrel
184  theFunction = &(namesAndFunctions_.find("ecalHcalEcalBarrel")->second);
185  } else {
186  //endcap
187  theFunction = &(namesAndFunctions_.find("ecalHcalEcalEndcap")->second);
188  }
189 
190  assert(theFunction != 0);
191  double totalE(ecalE + hcalE);
192  double bCoeff = theFunction->Eval(totalE);
193  return ecalE * bCoeff;
194 }
195 
197  const double& hcalE, const double& eta, const double& phi) const {
198  const TF1* theFunction(0);
199 
200  if (fabs(eta) < barrelEndcapEtaDiv_) {
201  //barrel
202  theFunction = &(namesAndFunctions_.find("ecalHcalHcalBarrel")->second);
203  } else {
204  //endcap
205  theFunction = &(namesAndFunctions_.find("ecalHcalHcalEndcap")->second);
206  }
207 
208  double totalE(ecalE + hcalE);
209  assert(theFunction != 0);
210  double cCoeff = theFunction->Eval(totalE);
211  return hcalE * cCoeff;
212 }
213 
214 double PFClusterCalibration::getCalibratedEnergy(const double& ecalE,
215  const double& hcalE, const double& eta, const double& phi) const {
216  double totalE(ecalE + hcalE);
217  double answer(totalE);
218 
219  answer = getCalibratedEcalEnergy(ecalE, hcalE, eta, phi)
220  + getCalibratedHcalEnergy(ecalE, hcalE, eta, phi);
221  if (doEtaCorrection_)
222  answer = answer/etaCorrection_->Eval(eta);
223 
224  if (maxEToCorrect_> 0 && answer < maxEToCorrect_)
225  return correction_->Eval(answer);
226  if (doCorrection_) {
227  if (maxEToCorrect_> 0 && answer < maxEToCorrect_)
228  answer = correction_->Eval(answer);
229  else if (maxEToCorrect_ < 0) {
230  answer = correction_->Eval(answer);
231  }
232  }
233  if (!allowNegativeEnergy_ && answer < 0)
234  return 0;
235  return answer;
236 
237 }
238 
240  double& hcalE, const double& eta, const double& phi) const {
241 
242  double ecalEOld(ecalE);
243  double hcalEOld(hcalE);
244 
245  ecalE = getCalibratedEcalEnergy(ecalEOld, hcalEOld, eta, phi);
246  hcalE = getCalibratedHcalEnergy(ecalEOld, hcalEOld, eta, phi);
247 
248  double preCorrection(ecalE + hcalE);
249  if (doEtaCorrection_)
250  preCorrection = preCorrection/etaCorrection_->Eval(eta);
251 
252  if (doCorrection_) {
253  double corrE = correction_->Eval(preCorrection);
254  //a term = difference
255  double a = corrE - preCorrection;
256  hcalE += a;
257  }
258  if (hcalE < 0 && !allowNegativeEnergy_)
259  hcalE = 0;
260 
261 }
262 
266  c.calibrations_.push_back(crw);
267 
268 }
269 
272 
275  fabs(c.cluster_meanEcal_.phi_));
276 
279  fabs(c.cluster_meanEcal_.phi_));
280 
283  fabs(c.cluster_meanEcal_.phi_));
284 
285  crw.provenance_ = LINEARCORR;
286  crw.b_ = crw.ecalEnergy_ / c.cluster_energyEcal_;
287  crw.c_ = crw.hcalEnergy_ / c.cluster_energyHcal_;
289 
290 }
291 
293  std::cout << __PRETTY_FUNCTION__
294  << ": WARNING! This isn't working properly yet!\n";
295  TBranch* calibBr = input->GetBranch("Calibratable");
296  Calibratable* calib_ptr = new Calibratable();
297  calibBr->SetAddress(&calib_ptr);
298 
299  // TBranch* newBranch = input->Branch("NewCalibratable",
300  // "pftools::Calibratable", &calib_ptr, 32000, 2);
301 
302  std::cout << "Looping over tree's "<< input->GetEntries()
303  << " entries...\n";
304  for (unsigned entries(0); entries < 20000; entries++) {
305  if (entries % 10000== 0)
306  std::cout << "\tProcessing entry "<< entries << "\n";
307  input->GetEntry(entries);
308  calibrate(*calib_ptr);
309  input->Fill();
310  }
311  //input.Write("",TObject::kOverwrite);
312 }
313 
314 std::ostream& pftools::operator<<(std::ostream& s, const PFClusterCalibration& cc) {
315  s << "PFClusterCalibration: dump...\n";
316  s << "barrelEndcapEtaDiv:\t" << cc.barrelEndcapEtaDiv_ << ", ecalOnlyDiv:\t" << cc.ecalOnlyDiv_;
317  s << ", \nhcalOnlyDiv:\t" << cc.hcalOnlyDiv_ << ", doCorrection:\t" << cc.doCorrection_;
318  s << ", \nallowNegativeEnergy:\t" << cc.allowNegativeEnergy_;
319  s << ", \ncorrectionLowLimit:\t" << cc.correctionLowLimit_ << ",parameters:\t" << cc.globalP0_ << ", ";
320  s << cc.globalP1_ << ", " << cc.lowEP0_ << ", " << cc.lowEP1_;
321  s << "\ndoEtaCorrection:\t" << cc.doEtaCorrection_;
322  return s;
323 }
324 
answer
Definition: submit.py:44
std::vector< std::string > names_
Wraps essential single particle calibration data ready for export to a Root file. ...
Definition: Calibratable.h:122
std::map< std::string, TF1 > namesAndFunctions_
CalibratableElement cluster_meanEcal_
Definition: Calibratable.h:186
std::vector< CalibrationResultWrapper > calibrations_
Definition: Calibratable.h:206
void getCalibrationResultWrapper(const Calibratable &c, CalibrationResultWrapper &crw)
double getCalibratedEnergy(const double &ecalE, const double &hcalE, const double &eta, const double &phi) const
T eta() const
void getCalibratedEnergyEmbedAInHcal(double &ecalE, double &hcalE, const double &eta, const double &phi) const
double getCalibratedHcalEnergy(const double &ecalE, const double &hcalE, const double &eta, const double &phi) const
void setEvolutionParameters(const std::string &sector, const std::vector< double > &params)
double getCalibratedEcalEnergy(const double &ecalE, const double &hcalE, const double &eta, const double &phi) const
A small class designed to hold the result of a calibration of a SingleParticleWrapper.
void setCorrections(const double &lowEP0, const double &lowEP1, const double &globalP0, const double &globalP1)
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:121
std::ostream & operator<<(std::ostream &s, const Calibratable &calib_)
Definition: Calibratable.cc:6
void setEtaCorrectionParameters(const std::vector< double > &params)
Definition: DDAxes.h:10