CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Exercises3.cc
Go to the documentation of this file.
12 
13 #include <TFile.h>
14 #include <TTree.h>
15 #include <TH3F.h>
16 #include <TF2.h>
17 #include <TH1F.h>
18 #include <TGraph.h>
19 #include <TF1.h>
20 #include <vector>
21 #include <TROOT.h>
22 
23 using namespace pftools;
24 
26  de->setCalib(1.0);
27 }
28 
30  calibResultsFile_.close();
31 }
32 
34  withOffset_(false), target_(CLUSTER), threshold_(30), options_(options),
35  debug_(0) {
36 
37  options_->GetOpt("exercises", "withOffset", withOffset_);
38  options_->GetOpt("exercises", "threshold", threshold_);
39  options_->GetOpt("exercises", "debug", debug_);
40 
41  /* Initialise PFClusterCalibration appropriately. */
42  double g0, g1, e0, e1;
43  options_->GetOpt("correction", "globalP0", g0);
44  options_->GetOpt("correction", "globalP1", g1);
45  options_->GetOpt("correction", "lowEP0", e0);
46  options_->GetOpt("correction", "lowEP1", e1);
47  clusterCalibration_.setCorrections(e0, e1, g0, g1);
48 
49  double ecalECut, hcalECut;
50  options_->GetOpt("evolution", "ecalECut", ecalECut);
51  options_->GetOpt("evolution", "hcalECut", hcalECut);
52  clusterCalibration_.setEcalHcalEnergyCuts(ecalECut, hcalECut);
53 
54  int allowNegative(0);
55  options_->GetOpt("correction", "allowNegativeEnergy", allowNegative);
57 
58  int doCorrection(1);
59  options_->GetOpt("correction", "doCorrection", doCorrection);
61 
62  int doEtaCorrection(0);
63  options_->GetOpt("correction", "doEtaCorrection", doEtaCorrection);
64  clusterCalibration_.setDoEtaCorrection(doEtaCorrection);
65 
66  double barrelEta;
67  options_->GetOpt("evolution", "barrelEndcapEtaDiv", barrelEta);
69 
70  double maxEToCorrect(100.0);
71  options_->GetOpt("correction", "maxEToCorrect", maxEToCorrect);
73 
74  std::vector<std::string>* names = clusterCalibration_.getKnownSectorNames();
75  for (std::vector<std::string>::iterator i = names->begin(); i
76  != names->end(); ++i) {
77  std::string sector = *i;
78  std::vector<double> params;
79  options_->GetOpt("evolution", sector.c_str(), params);
81  }
82 
83  std::vector<double> etaParams;
84  options_->GetOpt("evolution", "etaCorrection", etaParams);
86 
87  std::cout << clusterCalibration_ << "\n";
88 
90  options_->GetOpt("results", "calibParamOutput", outputFileName);
91  calibResultsFile_.open(outputFileName.c_str());
92  calibResultsFile_ << "//Hello from your friendly PFClusterTools!\n";
93  if (debug_ > 0)
94  std::cout << __PRETTY_FUNCTION__ << ": finished.\n";
95 
96 }
97 
98 void Exercises3::calibrateCalibratables(TChain& sourceTree,
99  const std::string& exercisefile) {
100 
101  if (debug_ > 0) {
102  std::cout << "Welcome to "<< __PRETTY_FUNCTION__ << "\n";
103  std::cout << "Opening TTree...\n";
104  }
105 // //open tfile
106 // TFile* source = new TFile(sourcefile.c_str());
107 // if (source == 0) {
108 // std::string desc("Couldn't open file ");
109 // desc += sourcefile;
110 // PFToolsException e(desc);
111 // throw e;
112 // }
113 // if (debug_ > 0)
114 // std::cout << "Extracting calibratables...\n";
115  //use tree utility to extract calibratables
116  TreeUtility tu;
117  std::vector<Calibratable> calibVec;
118 
119  tu.getCalibratablesFromRootFile(sourceTree, calibVec);
120  if (debug_ > 0)
121  std::cout << "Got a vector of calibratables of size "<< calibVec.size()
122  << "\n";
123  //initialise detector elements
127 
128  //convert calibratables to particle deposits
129  std::vector<ParticleDepositPtr> pdVec;
130  tu.convertCalibratablesToParticleDeposits(calibVec, pdVec, target_, offset,
131  ecal, hcal, withOffset_);
132  //source->Close();
133 
134  TFile* exercises = new TFile(exercisefile.c_str(), "recreate");
135  TH1F droppedParticles("droppedParticles", "droppedParticles", 100000, 0,
136  100000);
137  if (debug_ > 0)
138  std::cout << "Particle deposit vec has "<< pdVec.size() << " entries\n";
139 
140  //calibrate
141  if (debug_ > 1)
142  std::cout << "Creating calibrator clones and space managers\n";
143  boost::shared_ptr<Calibrator> linCal(new LinearCalibrator());
144 
145  //Tell the calibrator which detector elements should be calibrated
146  if (withOffset_) {
147  linCal->addDetectorElement(offset);
148 
149  }
150  linCal->addDetectorElement(ecal);
151  linCal->addDetectorElement(hcal);
152 
153  double barrelEta;
154  options_->GetOpt("evolution", "barrelEndcapEtaDiv", barrelEta);
155  boost::shared_ptr<SpaceManager> sm(new SpaceManager("ecalAndHcal"));
156  sm->setBarrelLimit(barrelEta);
157  sm->createCalibrators(*linCal);
158 
159  if (debug_ > 1)
160  std::cout << "Initialised SpaceManager and calibrators.\n";
161  elements_.clear();
162  if (withOffset_)
163  elements_.push_back(offset);
164  elements_.push_back(ecal);
165  elements_.push_back(hcal);
166 
167  //Initialise calibrators with particles
168  int dropped(0);
169 
170  double eCut(0.0);
171  double hCut(0.0);
172  options_->GetOpt("evolution", "ecalECut", eCut);
173  options_->GetOpt("evolution", "hcalECut", hCut);
174  if (debug_ > 0)
175  std::cout << "Using a ECAL and HCAL energy cuts of "<< eCut << " and "
176  << hCut << " GeV\n";
177  if (debug_ > 1)
178  std::cout << "Assigning particles to space managers and calibrators.\n";
179 
180  //This is just a convenience plot to check on the hcal
181  for (std::vector<ParticleDepositPtr>::const_iterator cit = pdVec.begin(); cit
182  != pdVec.end(); ++cit) {
183  ParticleDepositPtr pd = *cit;
184  CalibratorPtr c = sm->findCalibrator(pd->getEta(), pd->getPhi(),
185  pd->getTruthEnergy());
186  //std::cout << *pd << "\n";
187  if (c == 0) {
188  std::cout << "Couldn't find calibrator for particle?!\n";
189  std::cout << "\t"<< *pd << "\n";
190 
191  dropped++;
192  } else {
193  c->addParticleDeposit(pd);
194  }
195 
196  }
197 
198  if (debug_ > 1)
199  std::cout << "Dropped "<< dropped << " particles.\n";
200 
201  /* Done assignments, now calibrate */
202  if (debug_ > 1)
203  std::cout
204  << "Assignments complete, starting calibration and analysis.\n";
205 
206  //calibrate
207  std::map<SpaceVoxelPtr, CalibratorPtr> * smCalibrators =
208  sm->getCalibrators();
209 
210  TTree tree("CalibratedParticles", "");
211  Calibratable* calibrated = new Calibratable();
212  tree.Branch("Calibratable", "pftools::Calibratable", &calibrated, 32000, 2);
213  if (debug_ > 1)
214  std::cout << "Initialised tree.\n";
215 
216  /* ECAL and HCAL */
217  std::cout << "*** Performance for ECAL + HCAL calibration ***\n";
218  getCalibrations(sm);
219  exercises->cd("/");
220  exercises->mkdir("ecalAndHcal");
221  exercises->cd("/ecalAndHcal");
223  for (std::map<SpaceVoxelPtr, CalibratorPtr>::iterator it =
224  smCalibrators->begin(); it != smCalibrators->end(); ++it) {
225  SpaceVoxelPtr sv = (*it).first;
226  CalibratorPtr c = (*it).second;
227  std::for_each(elements_.begin(), elements_.end(), resetElement3);
228  evaluateCalibrator(sm, c, tree, calibrated, ecal, hcal, offset, LINEAR,
229  LINEARCORR);
230 
231  std::for_each(elements_.begin(), elements_.end(), resetElement3);
232  }
233  sm->printCalibrations(std::cout);
234 
235  exercises->cd("/");
236 
237  //save results
238  std::cout << "Writing output tree...\n";
239  tree.Write();
240  droppedParticles.Write();
241  //gaussianFits(*exercises, calibVec);
242  exercises->Write();
243  exercises->Close();
244  std::cout << "Done."<< std::endl;
245 
246 }
247 
249 
250  std::map<SpaceVoxelPtr, CalibratorPtr>* smCalibrators = s->getCalibrators();
251 
252  for (std::map<SpaceVoxelPtr, CalibratorPtr>::iterator it =
253  smCalibrators->begin(); it != smCalibrators->end(); ++it) {
254  CalibratorPtr c= (*it).second;
255  std::for_each(elements_.begin(), elements_.end(), resetElement3);
256  if (c->hasParticles() > static_cast<int>(threshold_)) {
257  std::map<DetectorElementPtr, double> calibs =
258  c->getCalibrationCoefficients();
259  s->assignCalibration(c, calibs);
260  }
261  }
262 }
263 
265  const std::vector<DetectorElementPtr>& detEls) {
266 
267  int autoFit(0);
268  options_->GetOpt("evolution", "autoFit", autoFit);
269  std::cout << "AutoFit option = "<< autoFit << "\n";
270 
271  std::vector<double> ecalBarrel;
272  std::vector<double> ecalEndcap;
273  std::vector<double> hcalBarrel;
274  std::vector<double> hcalEndcap;
275 
276  double minE, maxE;
277  options_->GetOpt("evolution", "evolutionFunctionMinE", minE);
278  options_->GetOpt("evolution", "evolutionFunctionMaxE", maxE);
279 
280  int basePlots(0);
281  options_->GetOpt("evolution", "basePlots", basePlots);
282 
283  int useTruth(1);
284  options_->GetOpt("evolution", "basePlotsUseTruth", useTruth);
285 
286  if (debug_ > 1&& basePlots > 0)
287  std::cout << "Option to generate evolution plots invoked.\n";
288 
289  if (autoFit == 0) {
290  std::cout << "Fixing parameters for evolution functions.\n";
291 
292  options_->GetOpt("evolution", "ecalHcalEcalBarrel", ecalBarrel);
293  options_->GetOpt("evolution", "ecalHcalEcalEndcap", ecalEndcap);
294  options_->GetOpt("evolution", "ecalHcalHcalBarrel", hcalBarrel);
295  options_->GetOpt("evolution", "ecalHcalHcalEndcap", hcalEndcap);
296  assert(ecalBarrel.size() == 6 && ecalEndcap.size() == 6);
297  assert(hcalBarrel.size() == 6 && hcalEndcap.size() == 6);
298 
299  for (std::vector<DetectorElementPtr>::const_iterator i = detEls.begin(); i
300  != detEls.end(); ++i) {
302  std::cout << "Fixing evolution for "<< *d << "\n";
303  int mode(0);
304  options_->GetOpt("spaceManager", "interpolationMode", mode);
305 
306  std::string name("Func_");
307  name.append(DetElNames[d->getType()]);
308  name.append("_");
309 
310  /* Fitting for barrel */
311  std::string barrelName(name);
312  barrelName.append(RegionNames[BARREL_POS]);
313  std::cout << "\tFixing "<< RegionNames[BARREL_POS]<< "\n";
314  TF1
315  fBarrel(barrelName.c_str(),
316  "([0]*[5]*x)*([5]*x<[1])+([2]+[3]*exp([4]*[5]*x))*([5]*x>[1])");
317 
318  if (d->getType() == ECAL) {
319  unsigned count(0);
320  for (std::vector<double>::const_iterator dit =
321  ecalBarrel.begin(); dit!= ecalBarrel.end(); ++dit) {
322  fBarrel.FixParameter(count, *dit);
323  ++count;
324  }
325 
326  }
327  if (d->getType() == HCAL) {
328  unsigned count(0);
329  for (std::vector<double>::const_iterator dit =
330  hcalBarrel.begin(); dit!= hcalBarrel.end(); ++dit) {
331  fBarrel.FixParameter(count, *dit);
332  ++count;
333  }
334 
335  }
336  if (useTruth)
337  fBarrel.FixParameter(5, 1.0);
338 
339  fBarrel.SetMinimum(0);
340  s->addEvolution(d, BARREL_POS, fBarrel);
341 
342  if (basePlots > 0) {
343  TH1* slices = s->extractEvolution(d, BARREL_POS, fBarrel,
344  useTruth);
345  slices->Write();
346  }
347  fBarrel.Write();
348 
349  /* Fitting for endcap */
350  std::string endcapName(name);
351  endcapName.append(RegionNames[ENDCAP_POS]);
352  std::cout << "\tFixing "<< RegionNames[ENDCAP_POS]<< "\n";
353  TF1
354  fEndcap(endcapName.c_str(),
355  "([0]*[5]*x)*([5]*x<[1])+([2]+[3]*exp([4]*[5]*x))*([5]*x>[1])");
356 
357 
358  if (d->getType() == ECAL) {
359  unsigned count(0);
360  for (std::vector<double>::const_iterator dit =
361  ecalEndcap.begin(); dit!= ecalEndcap.end(); ++dit) {
362  fEndcap.FixParameter(count, *dit);
363  ++count;
364  }
365 
366  }
367  if (d->getType() == HCAL) {
368  unsigned count(0);
369  for (std::vector<double>::const_iterator dit =
370  hcalEndcap.begin(); dit!= hcalEndcap.end(); ++dit) {
371  fEndcap.FixParameter(count, *dit);
372  ++count;
373  }
374 
375  }
376  if (useTruth)
377  fEndcap.FixParameter(5, 1.0);
378 
379  fEndcap.SetMinimum(0);
380  s->addEvolution(d, ENDCAP_POS, fEndcap);
381  if (basePlots > 0) {
382  TH1* slices = s->extractEvolution(d, ENDCAP_POS, fEndcap,
383  useTruth);
384  slices->Write();
385  }
386  fEndcap.Write();
387  }
388 
389  }
390 
391 }
392 
394  TTree& tree, Calibratable* calibrated, DetectorElementPtr ecal,
397 
398  if (c->hasParticles() > static_cast<int>(threshold_)) {
399  std::map<DetectorElementPtr, double> calibs = s->getCalibration(c);
400 
401  std::vector<ParticleDepositPtr> csParticles = c->getParticles();
402  unsigned count(0);
403  int mode(0);
404  options_->GetOpt("spaceManager", "interpolationMode", mode);
405  if (debug_ > 1) {
406  std::cout << "Using interpolation mode " << mode << "\n";
407  }
408 
409  for (std::vector<ParticleDepositPtr>::iterator zit = csParticles.begin(); zit
410  != csParticles.end(); ++zit) {
411  ParticleDepositPtr pd = *zit;
412  calibrated->reset();
413  calibrated->rechits_meanEcal_.energy_ = pd->getRecEnergy(ecal);
414  calibrated->rechits_meanHcal_.energy_ = pd->getRecEnergy(hcal);
415  calibrated->sim_energyEvent_ = pd->getTruthEnergy();
416  calibrated->sim_etaEcal_ = pd->getEta();
417 
418  for (std::map<DetectorElementPtr, double>::iterator deit =
419  calibs.begin(); deit != calibs.end(); ++deit) {
420  DetectorElementPtr de = (*deit).first;
421  de->setCalib(1.0);
422  }
423 
425  crwPre.ecalEnergy_ = pd->getRecEnergy(ecal);
426  crwPre.hcalEnergy_ = pd->getRecEnergy(hcal);
427  crwPre.particleEnergy_ = pd->getRecEnergy();
428  crwPre.truthEnergy_ = pd->getTruthEnergy();
429  crwPre.provenance_ = UNCALIBRATED;
430  crwPre.targetFuncContrib_ = pd->getTargetFunctionContrib();
431  crwPre.target_ = target_;
432  crwPre.compute();
433  calibrated->calibrations_.push_back(crwPre);
434 
435  double tempEnergy = pd->getRecEnergy();
436  //evaluate calibration
437  for (std::map<DetectorElementPtr, double>::iterator deit =
438  calibs.begin(); deit != calibs.end(); ++deit) {
439  DetectorElementPtr de = (*deit).first;
440 
441  if (mode == 1)
442  de->setCalib(s->interpolateCoefficient(de,
443  pd->getTruthEnergy(), pd->getEta(), pd->getPhi()));
444  else if (mode == 2|| mode == 3|| mode == 4)
445  de->setCalib(s->evolveCoefficient(de, tempEnergy,
446  pd->getEta(), pd->getPhi()));
447  else
448  de->setCalib((*deit).second);
449  }
450  if (debug_ > 8) {
451  std::cout << "POST ECAL HCAL coeff: " << ecal->getCalib() << ", " << hcal->getCalib() << "\n";
452  }
453 
455  crwPos.ecalEnergy_ = pd->getRecEnergy(ecal);
456  crwPos.hcalEnergy_ = pd->getRecEnergy(hcal);
457  crwPos.b_ = ecal->getCalib();
458  crwPos.c_ = hcal->getCalib();
459  crwPos.particleEnergy_ = pd->getRecEnergy();
460  crwPos.truthEnergy_ = pd->getTruthEnergy();
461  crwPos.provenance_ = cp;
462  crwPos.compute();
463 
464  crwPos.targetFuncContrib_ = pd->getTargetFunctionContrib();
465  crwPos.target_ = target_;
466  calibrated->calibrations_.push_back(crwPos);
467 
468  //same again, but applying correction
469  if (cpCorr != NONE) {
470  CalibrationResultWrapper crwCorr;
471 
472  crwCorr.ecalEnergy_
474  crwPre.ecalEnergy_, crwPre.hcalEnergy_,
475  pd->getEta(), pd->getPhi());
476  crwCorr.hcalEnergy_
478  crwPre.ecalEnergy_, crwPre.hcalEnergy_,
479  pd->getEta(), pd->getPhi());
480  if (debug_ > 8) {
481  if(crwPre.ecalEnergy_ > 0 && crwPre.hcalEnergy_ >0)
482  std::cout << "CORR ECAL HCAL coeff: " << crwCorr.ecalEnergy_ / crwPre.ecalEnergy_ << ", " << crwCorr.hcalEnergy_/ crwPre.hcalEnergy_ << "\n\n";
483  }
484 
485  crwCorr.particleEnergy_
487  crwPre.ecalEnergy_, crwPre.hcalEnergy_,
488  pd->getEta(), pd->getPhi());
489 
490  crwCorr.b_ = ecal->getCalib();
491  crwCorr.c_ = hcal->getCalib();
492 
493  crwCorr.truthEnergy_ = pd->getTruthEnergy();
494  crwCorr.provenance_ = cpCorr;
495  crwCorr.targetFuncContrib_ = pd->getTargetFunctionContrib();
496  crwCorr.target_ = target_;
497  crwCorr.compute();
498  calibrated->calibrations_.push_back(crwCorr);
499 
500  crwPos.targetFuncContrib_ = pd->getTargetFunctionContrib();
501  crwPos.target_ = target_;
502  calibrated->calibrations_.push_back(crwPos);
503  }
504  tree.Fill();
505  ++count;
506 
507  }
508  }
509 }
510 
CalibratableElement rechits_meanHcal_
Definition: Calibratable.h:193
int i
Definition: DBlmapReader.cc:9
std::vector< std::string > * getKnownSectorNames()
Wraps essential single particle calibration data ready for export to a Root file. ...
Definition: Calibratable.h:122
unsigned convertCalibratablesToParticleDeposits(const std::vector< Calibratable > &input, std::vector< ParticleDepositPtr > &toBeFilled, CalibrationTarget target, DetectorElementPtr offset, DetectorElementPtr ecal, DetectorElementPtr hcal, bool includeOffset=false)
Definition: TreeUtility.cc:238
static const HistoName names[]
void setBarrelBoundary(const double &eta)
std::ofstream calibResultsFile_
Definition: Exercises3.h:60
virtual ~Exercises3()
Definition: Exercises3.cc:29
assert(m_qm.get())
std::vector< CalibrationResultWrapper > calibrations_
Definition: Calibratable.h:206
Represents an energy-measuring region of our detector.
bool GetOpt(const char *tag, const char *key, std::vector< T > &values) const
reads a vector of T
Definition: IO.h:108
A tool to associate SpaceVoxels with Calibrator objects.
Definition: SpaceManager.h:23
double getCalibratedEnergy(const double &ecalE, const double &hcalE, const double &eta, const double &phi) const
const char *const RegionNames[]
Definition: Region.h:11
PFClusterCalibration clusterCalibration_
Definition: Exercises3.h:62
tuple d
Definition: ztail.py:151
void evaluateCalibrator(SpaceManagerPtr s, CalibratorPtr c, TTree &tree, Calibratable *calibrated, DetectorElementPtr ecal, DetectorElementPtr hcal, DetectorElementPtr offset, CalibrationProvenance cp, CalibrationProvenance cpCorr=NONE)
Definition: Exercises3.cc:393
void setEcalHcalEnergyCuts(const double &ecalCut, const double &hcalCut)
boost::shared_ptr< SpaceManager > SpaceManagerPtr
Definition: SpaceManager.h:126
double getCalibratedHcalEnergy(const double &ecalE, const double &hcalE, const double &eta, const double &phi) const
boost::shared_ptr< ParticleDeposit > ParticleDepositPtr
unsigned getCalibratablesFromRootFile(TChain &tree, std::vector< Calibratable > &toBeFilled)
Definition: TreeUtility.cc:21
CalibratableElement rechits_meanEcal_
Definition: Calibratable.h:193
const char *const DetElNames[]
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
CalibrationTarget target_
Definition: Exercises3.h:56
virtual void reset()
boost::shared_ptr< Calibrator > CalibratorPtr
Definition: Calibrator.h:65
boost::shared_ptr< DetectorElement > DetectorElementPtr
void resetElement3(DetectorElementPtr de)
Definition: Exercises3.cc:25
A small class designed to hold the result of a calibration of a SingleParticleWrapper.
Definition: IO.h:29
void setDoCorrection(const int &doCorrection)
void setCorrections(const double &lowEP0, const double &lowEP1, const double &globalP0, const double &globalP1)
void setAllowNegativeEnergy(const bool &allowIt)
Exercises3(IO *options)
Definition: Exercises3.cc:33
Utility class to create particles and detector elements from a Root file.
Definition: TreeUtility.h:27
tuple cout
Definition: gather_cfg.py:121
std::vector< DetectorElementPtr > elements_
Definition: Exercises3.h:58
volatile std::atomic< bool > shutdown_flag false
void calibrateCalibratables(TChain &sourceTree, const std::string &exercisefile)
Definition: Exercises3.cc:98
void setEtaCorrectionParameters(const std::vector< double > &params)
unsigned threshold_
Definition: Exercises3.h:57
void setDoEtaCorrection(const int doEtaCorrection)
void getCalibrations(SpaceManagerPtr s)
Definition: Exercises3.cc:248
void evaluateSpaceManager(SpaceManagerPtr s, const std::vector< DetectorElementPtr > &detEls)
Definition: Exercises3.cc:264
boost::shared_ptr< SpaceVoxel > SpaceVoxelPtr
Definition: SpaceVoxel.h:89
tuple g0
Definition: corrVsCorr.py:108