CMS 3D CMS Logo

Exercises2.cc

Go to the documentation of this file.
00001 #include "RecoParticleFlow/PFClusterTools/interface/Exercises2.h"
00002 #include "RecoParticleFlow/PFClusterTools/interface/PFToolsException.h"
00003 #include "RecoParticleFlow/PFClusterTools/interface/TreeUtility.h"
00004 #include "RecoParticleFlow/PFClusterTools/interface/Calibratable.h"
00005 #include "RecoParticleFlow/PFClusterTools/interface/DetectorElement.h"
00006 #include "RecoParticleFlow/PFClusterTools/interface/Calibrator.h"
00007 #include "RecoParticleFlow/PFClusterTools/interface/LinearCalibrator.h"
00008 #include "RecoParticleFlow/PFClusterTools/interface/SpaceManager.h"
00009 #include "RecoParticleFlow/PFClusterTools/interface/CalibrationTarget.h"
00010 #include "RecoParticleFlow/PFClusterTools/interface/Region.h"
00011 
00012 #include <TFile.h>
00013 #include <TTree.h>
00014 #include <TH3F.h>
00015 #include <TF2.h>
00016 #include <TH1F.h>
00017 #include <TGraph.h>
00018 #include <TF1.h>
00019 #include <vector>
00020 #include <TROOT.h>
00021 
00022 using namespace pftools;
00023 
00024 void resetElement(DetectorElementPtr de) {
00025         de->setCalib(1.0);
00026 }
00027 
00028 Exercises2::~Exercises2() {
00029         calibResultsFile_.close();
00030 }
00031 
00032 Exercises2::Exercises2(IO* options) :
00033         withOffset_(false), target_(CLUSTER), threshold_(30),
00034                  options_(options), debug_(0) {
00035 
00036         options_->GetOpt("exercises", "withOffset", withOffset_);
00037         options_->GetOpt("exercises", "threshold", threshold_);
00038         options_->GetOpt("exercises", "debug", debug_);
00039         
00040         /* Initialise PFClusterCalibration appropriately. */
00041         double g0, g1, e0, e1;
00042         options_->GetOpt("correction", "globalP0", g0);
00043         options_->GetOpt("correction", "globalP1", g1);
00044         options_->GetOpt("correction", "lowEP0", e0);
00045         options_->GetOpt("correction", "lowEP1", e1);
00046         clusterCalibration_.setCorrections(e0, e1, g0, g1);
00047         
00048         int allowNegative(0);
00049         options_->GetOpt("correction", "allowNegativeEnergy", allowNegative);
00050         clusterCalibration_.setAllowNegativeEnergy(allowNegative);
00051         
00052         int doCorrection(1);
00053         options_->GetOpt("correction", "doCorrection", doCorrection);
00054         clusterCalibration_.setDoCorrection(doCorrection);
00055         
00056         double barrelEta;
00057         options_->GetOpt("evolution", "barrelEndcapEtaDiv", barrelEta);
00058         clusterCalibration_.setBarrelBoundary(barrelEta);
00059         
00060         double maxEToCorrect(100.0);
00061         options_->GetOpt("correction", "maxEToCorrect", maxEToCorrect);
00062         clusterCalibration_.setMaxEToCorrect(maxEToCorrect);
00063         
00064         std::vector<std::string>* names = clusterCalibration_.getKnownSectorNames();
00065         for(std::vector<std::string>::iterator i = names->begin(); i != names->end(); ++i) {
00066                 std::string sector = *i;
00067                 std::vector<double> params;
00068                 options_->GetOpt("evolution", sector.c_str(), params);
00069                 clusterCalibration_.setEvolutionParameters(sector, params);
00070         }
00071         
00072         int doEtaCorrection(1);
00073         options_->GetOpt("evolution", "doEtaCorrection", doEtaCorrection);
00074         clusterCalibration_.setDoEtaCorrection(doEtaCorrection);
00075         
00076         std::vector<double> etaParams;
00077         options_->GetOpt("evolution", "etaCorrection", etaParams);
00078         clusterCalibration_.setEtaCorrectionParameters(etaParams);
00079         
00080         std::cout << clusterCalibration_ << "\n";
00081         
00082         std::string outputFileName;
00083         options_->GetOpt("results", "calibParamOutput", outputFileName);
00084         calibResultsFile_.open(outputFileName.c_str());
00085         calibResultsFile_ << "//Hello from your friendly PFClusterTools!\n";
00086         if (debug_ > 0)
00087                 std::cout << __PRETTY_FUNCTION__ << ": finished.\n";
00088 
00089 }
00090 
00091 
00092 void Exercises2::calibrateCalibratables(const std::string& sourcefile,
00093                 const std::string& exercisefile) {
00094 
00095         if (debug_ > 0) {
00096                 std::cout << "Welcome to "<< __PRETTY_FUNCTION__ << "\n";
00097                 std::cout << "Opening TFile...\n";
00098         }
00099         //open tfile
00100         TFile* source = new TFile(sourcefile.c_str());
00101         if (source == 0) {
00102                 std::string desc("Couldn't open file ");
00103                 desc += sourcefile;
00104                 PFToolsException e(desc);
00105                 throw e;
00106         }
00107         if (debug_ > 0)
00108                 std::cout << "Extracting calibratables...\n";
00109         //use tree utility to extract calibratables
00110         TreeUtility tu;
00111         std::vector<Calibratable> calibVec;
00112         PFToolsException e("TreeUtility has moved on, fix this up!");
00113         throw e;
00114         //tu.getCalibratablesFromRootFile(*source, calibVec);
00115         if (debug_ > 0)
00116                 std::cout << "Got a vector of calibratables of size "<< calibVec.size()
00117                                 << "\n";
00118         //initialise detector elements
00119         DetectorElementPtr ecal(new DetectorElement(ECAL, 1.0));
00120         DetectorElementPtr hcal(new DetectorElement(HCAL, 1.0));
00121         DetectorElementPtr offset(new DetectorElement(OFFSET, 1.0));
00122 
00123         //convert calibratables to particle deposits
00124         std::vector<ParticleDepositPtr> pdVec;
00125         tu.convertCalibratablesToParticleDeposits(calibVec, pdVec, target_, offset,
00126                         ecal, hcal, withOffset_);
00127         source->Close();
00128         if (debug_ > 0)
00129                 std::cout << "Closed source file. Opening exercises file...\n";
00130         TFile* exercises = new TFile(exercisefile.c_str(), "recreate");
00131         TH1F droppedParticles("droppedParticles", "droppedParticles", 100000, 0,
00132                         100000);
00133         if (debug_ > 0)
00134                 std::cout << "Particle deposit vec has "<< pdVec.size() << " entries\n";
00135 
00136         //calibrate
00137         if (debug_ > 1)
00138                 std::cout << "Creating calibrator clones and space managers\n";
00139         boost::shared_ptr<Calibrator> linCal(new LinearCalibrator());
00140         boost::shared_ptr<Calibrator> hcalCal(new LinearCalibrator());
00141         boost::shared_ptr<Calibrator> ecalCal(new LinearCalibrator());
00142 
00143         //Tell the calibrator which detector elements should be calibrated
00144         if (withOffset_) {
00145                 linCal->addDetectorElement(offset);
00146                 hcalCal->addDetectorElement(offset);
00147                 ecalCal->addDetectorElement(offset);
00148         }
00149         linCal->addDetectorElement(ecal);
00150         linCal->addDetectorElement(hcal);
00151 
00152         hcalCal->addDetectorElement(hcal);
00153         ecalCal->addDetectorElement(ecal);
00154 
00155         double barrelEta;
00156         options_->GetOpt("evolution", "barrelEndcapEtaDiv", barrelEta);
00157         boost::shared_ptr<SpaceManager> sm(new SpaceManager("ecalAndHcal"));
00158         sm->setBarrelLimit(barrelEta);
00159         sm->createCalibrators(*linCal);
00160         boost::shared_ptr<SpaceManager> esm(new SpaceManager("ecalOnly"));
00161         esm->createCalibrators(*ecalCal);
00162         esm->setBarrelLimit(barrelEta);
00163         boost::shared_ptr<SpaceManager> hsm(new SpaceManager("hcalOnly"));
00164         hsm->createCalibrators(*hcalCal);
00165         hsm->setBarrelLimit(barrelEta);
00166 
00167         if (debug_ > 1)
00168                 std::cout << "Initialised SpaceManager and calibrators.\n";
00169         elements_.clear();
00170         if (withOffset_)
00171                 elements_.push_back(offset);
00172         elements_.push_back(ecal);
00173         elements_.push_back(hcal);
00174 
00175         //Initialise calibrators with particles
00176         int count(0);
00177         int dropped(0);
00178 
00179         double eCut(0.3);
00180         double hCut(0.5);
00181         options_->GetOpt("evolution", "ecalECut", eCut);
00182         options_->GetOpt("evolution", "hcalECut", hCut);
00183         if (debug_ > 0)
00184                 std::cout << "Using a ECAL MIP cut of "<< eCut << " GeV\n";
00185         if (debug_ > 1)
00186                 std::cout << "Assigning particles to space managers and calibrators.\n";
00187 
00188         //This is just a convenience plot to check on the hcal
00189         TH2F hcalOnlyInput("hcalOnlyInput", "hcalOnlyInput", 30, 0, 3, 50, 0, 5);
00190         for (std::vector<ParticleDepositPtr>::const_iterator cit = pdVec.begin(); cit
00191                         != pdVec.end(); ++cit) {
00192                 ParticleDepositPtr pd = *cit;
00193                 //              if (count%1000== 0)
00194                 //                      std::cout << *pd;
00195 
00196                 if (pd->getRecEnergy(ecal) > eCut && pd->getRecEnergy(hcal) > hCut) {
00197                         CalibratorPtr c = sm->findCalibrator(pd->getEta(), pd->getPhi(),
00198                                         pd->getTruthEnergy());
00199                         //std::cout << *pd << "\n";
00200                         if (c == 0) {
00201                                 if (debug_ > 1) {
00202                                         std::cout << "Couldn't find calibrator for particle?!\n";
00203                                         std::cout << "\t"<< *pd << "\n";
00204                                 }
00205                                 dropped++;
00206                         } else {
00207                                 c->addParticleDeposit(pd);
00208                         }
00209                         /* HCAL fulfillment */
00210                 } else if (pd->getRecEnergy(ecal) < eCut && pd->getRecEnergy(hcal)
00211                                 > hCut) {
00212                         CalibratorPtr c = hsm->findCalibrator(pd->getEta(), pd->getPhi(),
00213                                         pd->getTruthEnergy());
00214                         if (pd->getTruthEnergy() < 3.0) {
00215                                 hcalOnlyInput.Fill(pd->getTruthEnergy(), pd->getRecEnergy(hcal));
00216                                 //std::cout << *pd << "\n";
00217                         }
00218 
00219                         if (c == 0) {
00220                                 if (debug_ > 1) {
00221                                         std::cout << "Couldn't find calibrator for particle?!\n";
00222                                         std::cout << "\t"<< *pd << "\n";
00223                                 }
00224                                 dropped++;
00225                         } else {
00226                                 pd->setRecEnergy(ecal, 0.0);
00227                                 c->addParticleDeposit(pd);
00228                         }
00229                         //std::cout << "Dropping deposit: \n" << *pd;
00230                         /* ECAL fulfillment */
00231                 } else if (pd->getRecEnergy(hcal) < hCut && pd->getRecEnergy(ecal)
00232                                 > eCut) {
00233                         CalibratorPtr c = esm->findCalibrator(pd->getEta(), pd->getPhi(),
00234                                         pd->getTruthEnergy());
00235 
00236                         //std::cout << *pd << "\n";
00237                         if (c == 0) {
00238                                 if (debug_ > 1) {
00239                                         std::cout << "Couldn't find calibrator for particle?!\n";
00240                                         std::cout << "\t"<< *pd << "\n";
00241                                 }
00242                                 dropped++;
00243                         } else {
00244                                 pd->setRecEnergy(hcal, 0.0);
00245                                 c->addParticleDeposit(pd);
00246                         }
00247                         //std::cout << "Dropping deposit: \n" << *pd;
00248                 } else {
00249                         ++dropped;
00250                         droppedParticles.Fill(count);
00251                 }
00252 
00253                 ++count;
00254         }
00255 
00256         hcalOnlyInput.Write();
00257 
00258         if (debug_ > 1)
00259                 std::cout << "Dropped "<< dropped << " particles.\n";
00260 
00261         /* Done assignments, now calibrate */
00262         if (debug_ > 1)
00263                 std::cout
00264                                 << "Assignments complete, starting calibration and analysis.\n";
00265 
00266         //calibrate
00267         std::map<SpaceVoxelPtr, CalibratorPtr>
00268                         * smCalibrators = sm->getCalibrators();
00269         std::map<SpaceVoxelPtr, CalibratorPtr>
00270                         * hsmCalibrators = hsm->getCalibrators();
00271         std::map<SpaceVoxelPtr, CalibratorPtr>
00272                         * esmCalibrators = esm->getCalibrators();
00273 
00274         TTree tree("CalibratedParticles", "");
00275         Calibratable* calibrated = new Calibratable();
00276         tree.Branch("Calibratable", "pftools::Calibratable", &calibrated, 32000, 2);
00277         if (debug_ > 1)
00278                 std::cout << "Initialised tree.\n";
00279 
00280         /* ECAL and HCAL */
00281         std::cout << "*** Performance for ECAL + HCAL calibration ***\n";
00282         getCalibrations(sm);
00283         exercises->cd("/");
00284         exercises->mkdir("ecalAndHcal");
00285         exercises->cd("/ecalAndHcal");
00286         evaluateSpaceManager(sm, elements_);
00287         for (std::map<SpaceVoxelPtr, CalibratorPtr>::iterator
00288                         it = smCalibrators->begin(); it != smCalibrators->end(); ++it) {
00289                 SpaceVoxelPtr sv = (*it).first;
00290                 CalibratorPtr c = (*it).second;
00291                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00292                 evaluateCalibrator(sm, c, tree, calibrated, ecal, hcal, offset, LINEAR,
00293                                 NONE);
00294                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00295         }
00296         sm->printCalibrations(std::cout);
00297 
00298         /* HCAL */
00299         std::cout << "*** Performace of HCAL ONLY calibration ***\n";
00300         getCalibrations(hsm);
00301         exercises->cd("/");
00302         exercises->mkdir("hcal");
00303         exercises->cd("/hcal");
00304         evaluateSpaceManager(hsm, elements_);
00305         for (std::map<SpaceVoxelPtr, CalibratorPtr>::iterator
00306                         it = hsmCalibrators->begin(); it != hsmCalibrators->end(); ++it) {
00307                 SpaceVoxelPtr sv = (*it).first;
00308                 CalibratorPtr c = (*it).second;
00309                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00310                 evaluateCalibrator(hsm, c, tree, calibrated, ecal, hcal, offset,
00311                                 LINEAR, NONE);
00312                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00313         }
00314         hsm->printCalibrations(std::cout);
00315 
00316         /* ECAL */
00317         exercises->cd("/");
00318         std::cout << "*** Performace of ECAL ONLY calibration ***\n";
00319         getCalibrations(esm);
00320         exercises->cd("/");
00321         exercises->mkdir("ecal");
00322         exercises->cd("/ecal");
00323         evaluateSpaceManager(esm, elements_);
00324         for (std::map<SpaceVoxelPtr, CalibratorPtr>::iterator
00325                         it = esmCalibrators->begin(); it != esmCalibrators->end(); ++it) {
00326                 SpaceVoxelPtr sv = (*it).first;
00327                 CalibratorPtr c = (*it).second;
00328                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00329                 evaluateCalibrator(esm, c, tree, calibrated, ecal, hcal, offset,
00330                                 LINEAR, NONE);
00331                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00332         }
00333         esm->printCalibrations(std::cout);
00334 
00335         exercises->cd("/");
00336 
00337         //Reevaluate correction parameters
00338         TF1* f1;
00339         TF1* f2;
00340         determineCorrection(*exercises, tree, f1, f2);
00341 
00342         //save results
00343         std::cout << "Writing output tree...\n";
00344         tree.Write();
00345         droppedParticles.Write();
00346         //gaussianFits(*exercises, calibVec);
00347         exercises->Write();
00348         exercises->Close();
00349         std::cout << "Done."<< std::endl;
00350 
00351 }
00352 
00353 void Exercises2::getCalibrations(SpaceManagerPtr s) {
00354 
00355         std::map<SpaceVoxelPtr, CalibratorPtr>* smCalibrators = s->getCalibrators();
00356 
00357         for (std::map<SpaceVoxelPtr, CalibratorPtr>::iterator
00358                         it = smCalibrators->begin(); it != smCalibrators->end(); ++it) {
00359                 CalibratorPtr c= (*it).second;
00360                 std::for_each(elements_.begin(), elements_.end(), resetElement);
00361                 if (c->hasParticles() > static_cast<int>(threshold_)) {
00362                         std::map<DetectorElementPtr, double>
00363                                         calibs = c->getCalibrationCoefficients();
00364                         s->assignCalibration(c, calibs);
00365                 }
00366         }
00367 }
00368 
00369 void Exercises2::evaluateSpaceManager(SpaceManagerPtr s,
00370                 std::vector<DetectorElementPtr> detEls) {
00371 
00372         int autoFit(0);
00373         options_->GetOpt("evolution", "autoFit", autoFit);
00374         std::cout << "AutoFit option = "<< autoFit << "\n";
00375 
00376         std::vector<double> ecalBarrel;
00377         std::vector<double> ecalEndcap;
00378         std::vector<double> hcalBarrel;
00379         std::vector<double> hcalEndcap;
00380 
00381         double minE, maxE;
00382         options_->GetOpt("evolution", "evolutionFunctionMinE", minE);
00383         options_->GetOpt("evolution", "evolutionFunctionMaxE", maxE);
00384 
00385         int basePlots(0);
00386         options_->GetOpt("evolution", "basePlots", basePlots);
00387         
00388         int useTruth(1);
00389         options_->GetOpt("evolution", "basePlotsUseTruth", useTruth);
00390 
00391         if (debug_ > 1&& basePlots > 0)
00392                 std::cout << "Option to generate evolution plots invoked.\n";
00393 
00394         if (autoFit == 0) {
00395                 std::cout << "Fixing parameters for evolution functions.\n";
00396                 if (s->getName() == "ecalOnly") {
00397                         options_->GetOpt("evolution", "ecalOnlyEcalBarrel", ecalBarrel);
00398                         options_->GetOpt("evolution", "ecalOnlyEcalEndcap", ecalEndcap);
00399                         //assert(ecalBarrel.size() == 9 && ecalEndcap.size() == 9);
00400                 } else if (s->getName() == "hcalOnly") {
00401                         options_->GetOpt("evolution", "hcalOnlyHcalBarrel", hcalBarrel);
00402                         options_->GetOpt("evolution", "hcalOnlyHcalEndcap", hcalEndcap);
00403                         //assert(hcalBarrel.size() == 9 && hcalEndcap.size() == 9);
00404                 } else {
00405                         options_->GetOpt("evolution", "ecalHcalEcalBarrel", ecalBarrel);
00406                         options_->GetOpt("evolution", "ecalHcalEcalEndcap", ecalEndcap);
00407                         options_->GetOpt("evolution", "ecalHcalHcalBarrel", hcalBarrel);
00408                         options_->GetOpt("evolution", "ecalHcalHcalEndcap", hcalEndcap);
00409                         //assert(ecalBarrel.size() == 9 && ecalEndcap.size() == 9);
00410                         //assert(hcalBarrel.size() == 9 && hcalEndcap.size() == 9);
00411                 }
00412 
00413                 for (std::vector<DetectorElementPtr>::iterator i = detEls.begin(); i
00414                                 != detEls.end(); ++i) {
00415                         DetectorElementPtr d = *i;
00416                         std::cout << "Fixing evolution for "<< *d << "\n";
00417                         int mode(0);
00418                         options_->GetOpt("spaceManager", "interpolationMode", mode);
00419 
00420                         std::string name("Func_");
00421                         name.append(DetElNames[d->getType()]);
00422                         name.append("_");
00423 
00424                         /* Fitting for barrel */
00425                         std::string barrelName(name);
00426                         barrelName.append(RegionNames[BARREL_POS]);
00427                         std::cout << "\tFixing "<< RegionNames[BARREL_POS]<< "\n";
00428                         TF1
00429                                         //fBarrel(
00430                                                 //      barrelName.c_str(),
00431                                                         //"([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])");
00432                         fBarrel(barrelName.c_str(),
00433                                                                                 "([0]*[5]*x)*([5]*x<[1])+([2]+[3]*exp([4]*[5]*x))*([5]*x>[1])");
00434 
00435 
00436                         if (d->getType() == ECAL) {
00437                                 unsigned count(0);
00438                                 for (std::vector<double>::const_iterator
00439                                                 dit = ecalBarrel.begin(); dit!= ecalBarrel.end(); ++dit) {
00440                                         fBarrel.FixParameter(count, *dit);
00441                                         ++count;
00442                                 }
00443                                 
00444                         }
00445                         if (d->getType() == HCAL) {
00446                                 unsigned count(0);
00447                                 for (std::vector<double>::const_iterator
00448                                                 dit = hcalBarrel.begin(); dit!= hcalBarrel.end(); ++dit) {
00449                                         fBarrel.FixParameter(count, *dit);
00450                                         ++count;
00451                                 }
00452                                 
00453                         }
00454                         if(useTruth)
00455                                 fBarrel.FixParameter(5, 1.0);
00456 
00457                         fBarrel.SetMinimum(0);
00458                         s->addEvolution(d, BARREL_POS, fBarrel);
00459 
00460                         if (basePlots > 0) {
00461                                 TH1* slices = s->extractEvolution(d, BARREL_POS, fBarrel, useTruth);
00462                                 slices->Write();
00463                         }
00464                         fBarrel.Write();
00465 
00466                         /* Fitting for endcap */
00467                         std::string endcapName(name);
00468                         endcapName.append(RegionNames[ENDCAP_POS]);
00469                         std::cout << "\tFixing "<< RegionNames[ENDCAP_POS]<< "\n";
00470                         TF1
00471                                 //      fEndcap(
00472                                         //              endcapName.c_str(),
00473                                                 //      "([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])");
00474                         fEndcap(endcapName.c_str(),
00475                                                                                 "([0]*[5]*x)*([5]*x<[1])+([2]+[3]*exp([4]*[5]*x))*([5]*x>[1])");
00476 
00477                         if (d->getType() == ECAL) {
00478                                 unsigned count(0);
00479                                 for (std::vector<double>::const_iterator
00480                                                 dit = ecalEndcap.begin(); dit!= ecalEndcap.end(); ++dit) {
00481                                         fEndcap.FixParameter(count, *dit);
00482                                         ++count;
00483                                 }
00484                                 
00485                         }
00486                         if (d->getType() == HCAL) {
00487                                 unsigned count(0);
00488                                 for (std::vector<double>::const_iterator
00489                                                 dit = hcalEndcap.begin(); dit!= hcalEndcap.end(); ++dit) {
00490                                         fEndcap.FixParameter(count, *dit);
00491                                         ++count;
00492                                 }
00493                                 
00494                         }
00495                         if(useTruth)
00496                                 fEndcap.FixParameter(5, 1.0);
00497                         
00498                         fEndcap.SetMinimum(0);
00499                         s->addEvolution(d, ENDCAP_POS, fEndcap);
00500                         if (basePlots > 0) {
00501                                 TH1* slices = s->extractEvolution(d, ENDCAP_POS, fEndcap, useTruth);
00502                                 slices->Write();
00503                         }
00504                         fEndcap.Write();
00505                 }
00506 
00507         } else if (s->getNCalibrations() > 0) {
00508                 std::cout << "Using autofit functionality...\n";
00509                 for (std::vector<DetectorElementPtr>::iterator i = detEls.begin(); i
00510                                 != detEls.end(); ++i) {
00511                         DetectorElementPtr d = *i;
00512 
00513                         std::string name("Func");
00514 
00515                         name.append(DetElNames[d->getType()]);
00516                         name.append("_");
00517 
00518                         /* Fitting for barrel */
00519                         std::string barrelName(name);
00520                         barrelName.append(RegionNames[BARREL_POS]);
00521                         std::cout << "\tFitting "<< RegionNames[BARREL_POS]<< "\n";
00522                         TF1 fBarrel(barrelName.c_str(),
00523                                         "[0]*x*([1]-x)/pow(([2]+x),3)+[3]*pow(x, 0.1)");
00524                         barrelName.append("Slices");
00525 
00526                         TH1* slices = s->extractEvolution(d, BARREL_POS, fBarrel, useTruth);
00527                         slices->Write();
00528                         fBarrel.Write();
00529 
00530                         if (slices != 0) {
00531                                 slices->SetName(barrelName.c_str());
00532                                 slices->Write();
00533                                 //                              if (mode == 2) {
00534                                 //                                      //Use fit to truth rather than reco energies
00535                                 //                                      //(Clearly cheating and impossible with real data!)
00536                                 //                                      s->extractTruthEvolution(d, tg, &fBarrel);
00537                                 //                              }
00538                                 s->addEvolution(d, BARREL_POS, fBarrel);
00539                         } else {
00540                                 std::cout << __PRETTY_FUNCTION__
00541                                                 << ": WARNING! Couldn't get fitted slices!\n";
00542                         }
00543 
00544                         /* Fitting for endcaps */
00545                         std::string endcapName(name.c_str());
00546                         endcapName.append(RegionNames[ENDCAP_POS]);
00547                         std::cout << "\nFitting "<< RegionNames[ENDCAP_POS]<< "\n";
00548                         TF1 fEndcap(endcapName.c_str(),
00549                                         "[0]*x*([1]-x)/pow(([2]+x),3)+[3]*pow(x, 0.1)");
00550 
00551                         endcapName.append("Slices");
00552 
00553                         TH1* slicesEndcap = s->extractEvolution(d, ENDCAP_POS, fEndcap, useTruth);
00554                         slicesEndcap->Write();
00555                         fEndcap.Write();
00556 
00557                         if (slicesEndcap != 0) {
00558                                 slicesEndcap->SetName(endcapName.c_str());
00559                                 slicesEndcap->Write();
00560                                 //                              if (mode == 2) {
00561                                 //                                      //Use fit to truth rather than reco energies
00562                                 //                                      //(Clearly cheating and impossible with real data!)
00563                                 //                                      s->extractTruthEvolution(d, tg, &fEndcap);
00564                                 //                              }
00565                                 s->addEvolution(d, ENDCAP_POS, fEndcap);
00566                         } else {
00567                                 std::cout << __PRETTY_FUNCTION__
00568                                                 << ": WARNING! Couldn't get fitted slices!\n";
00569                         }
00570                 }
00571         }
00572 
00573 }
00574 
00575 void Exercises2::evaluateCalibrator(SpaceManagerPtr s, CalibratorPtr c,
00576                 TTree& tree, Calibratable* calibrated, DetectorElementPtr ecal,
00577                 DetectorElementPtr hcal, DetectorElementPtr offset,
00578                 CalibrationProvenance cp, CalibrationProvenance cpCorr) {
00579 
00580         if (c->hasParticles() > static_cast<int>(threshold_)) {
00581                 std::map<DetectorElementPtr, double>calibs = s->getCalibration(c);
00582 
00583                 std::vector<ParticleDepositPtr> csParticles = c->getParticles();
00584                 unsigned count(0);
00585                 for (std::vector<ParticleDepositPtr >::iterator
00586                                 zit = csParticles.begin(); zit!= csParticles.end(); ++zit) {
00587                         ParticleDepositPtr pd = *zit;
00588                         calibrated->reset();
00589                         calibrated->rechits_meanEcal_.energy_ = pd->getRecEnergy(ecal);
00590                         calibrated->rechits_meanHcal_.energy_ = pd->getRecEnergy(hcal);
00591                         calibrated->sim_energyEvent_ = pd->getTruthEnergy();
00592                         calibrated->sim_etaEcal_ = pd->getEta();
00593 
00594                         for (std::map<DetectorElementPtr, double>::iterator
00595                                         deit = calibs.begin(); deit != calibs.end(); ++deit) {
00596                                 DetectorElementPtr de = (*deit).first;
00597                                 de->setCalib(1.0);
00598                         }
00599 
00600                         CalibrationResultWrapper crwPre;
00601                         crwPre.ecalEnergy_ = pd->getRecEnergy(ecal);
00602                         crwPre.hcalEnergy_ = pd->getRecEnergy(hcal);
00603                         crwPre.particleEnergy_ = pd->getRecEnergy();
00604                         crwPre.truthEnergy_ = pd->getTruthEnergy();
00605                         crwPre.provenance_ = UNCALIBRATED;
00606                         crwPre.targetFuncContrib_ = pd->getTargetFunctionContrib();
00607                         crwPre.target_ = target_;
00608                         crwPre.compute();
00609                         calibrated->calibrations_.push_back(crwPre);
00610 
00611                         //evaluate calibration
00612                         for (std::map<DetectorElementPtr, double>::iterator
00613                                         deit = calibs.begin(); deit != calibs.end(); ++deit) {
00614                                 DetectorElementPtr de = (*deit).first;
00615 
00616                                 int mode(0);
00617                                 options_->GetOpt("spaceManager", "interpolationMode", mode);
00618 
00619                                 if (mode == 1)
00620                                         de->setCalib(s->interpolateCoefficient(de,
00621                                                         pd->getTruthEnergy(), pd->getEta(), pd->getPhi()));
00622                                 else if (mode == 2|| mode == 3|| mode == 4)
00623                                         de->setCalib(s->evolveCoefficient(de, pd->getRecEnergy(),
00624                                                         pd->getEta(), pd->getPhi()));
00625                                 else
00626                                         de->setCalib((*deit).second);
00627                         }
00628 
00629                         CalibrationResultWrapper crwPos;
00630                         crwPos.ecalEnergy_ = pd->getRecEnergy(ecal);
00631                         crwPos.hcalEnergy_ = pd->getRecEnergy(hcal);
00632                         crwPos.b_ = ecal->getCalib();
00633                         crwPos.c_ = hcal->getCalib();
00634                         crwPos.particleEnergy_ = pd->getRecEnergy();
00635                         crwPos.truthEnergy_ = pd->getTruthEnergy();
00636                         crwPos.provenance_ = cp;
00637                         crwPos.compute();
00638                         
00639                         crwPos.targetFuncContrib_ = pd->getTargetFunctionContrib();
00640                         crwPos.target_ = target_;
00641                         calibrated->calibrations_.push_back(crwPos);
00642 
00643                         //same again, but applying correction
00644                         if (cpCorr != NONE) {
00645                                 CalibrationResultWrapper crwCorr;
00646 
00647                                 crwCorr.ecalEnergy_
00648                                                 = clusterCalibration_.getCalibratedEcalEnergy(crwPre.ecalEnergy_,
00649                                                                 crwPre.hcalEnergy_, pd->getEta(), pd->getPhi());
00650                                 crwCorr.hcalEnergy_
00651                                                 = clusterCalibration_.getCalibratedHcalEnergy(crwPre.ecalEnergy_,
00652                                                                 crwPre.hcalEnergy_, pd->getEta(), pd->getPhi());
00653                                 crwCorr.particleEnergy_
00654                                                 = clusterCalibration_.getCalibratedEnergy(crwPre.ecalEnergy_,
00655                                                                 crwPre.hcalEnergy_, pd->getEta(), pd->getPhi());
00656 
00657                                 crwCorr.b_ = ecal->getCalib();
00658                                 crwCorr.c_ = hcal->getCalib();
00659 
00660                                 crwCorr.truthEnergy_ = pd->getTruthEnergy();
00661                                 crwCorr.provenance_ = cpCorr;
00662                                 crwCorr.targetFuncContrib_ = pd->getTargetFunctionContrib();
00663                                 crwCorr.target_ = target_;
00664                                 crwCorr.compute();
00665                                 calibrated->calibrations_.push_back(crwCorr);
00666                         }
00667                         tree.Fill();
00668                         ++count;
00669 
00670                 }
00671         } 
00672 }
00673 
00674 void Exercises2::determineCorrection(TFile& f, TTree& tree, TF1*& f1, TF1*& f2) {
00675         std::cout << __PRETTY_FUNCTION__ << "\n";
00676         f.cd("/");
00677         f.mkdir("corrections");
00678         f.cd("/corrections");
00679         std::cout << "------------------------------------\nUncorrected curves:\n";
00680         tree.Draw(
00681                         "sim_energyEvent_:calibrations_.particleEnergy_>>correctionCurve",
00682                         "calibrations_.provenance_ > 0", "box");
00683         TH2D* correctionCurve = (TH2D*) gDirectory->Get("correctionCurve");
00684         correctionCurve->FitSlicesX();
00685         correctionCurve->Write();
00686 
00687         TH1F* correctionCurve_1 = (TH1F*) gDirectory->Get("correctionCurve_1");
00688         correctionCurve_1->Write();
00689         double correctionLowLimit(0);
00690         options_->GetOpt("exercises", "correctionLowLimit", correctionLowLimit);
00691 
00692         f1 = new TF1("f1", "pol1");
00693         correctionCurve_1->Fit("f1");
00694         f2 = new TF1("f2", "pol2");
00695         correctionCurve_1->Fit("f2");
00696 
00697         std::cout
00698                         << "------------------------------------\nAlready corrected curve fits:\n";
00699         tree.Draw(
00700                         "sim_energyEvent_:calibrations_.particleEnergy_>>correctionCurveCorr",
00701                         "calibrations_.provenance_ < 0", "box");
00702         TH2D* correctionCurveCorr = (TH2D*) gDirectory->Get("correctionCurveCorr");
00703         correctionCurveCorr->FitSlicesX();
00704         correctionCurveCorr->Write();
00705 
00706         TH1F
00707                         * correctionCurveCorr_1 = (TH1F*) gDirectory->Get("correctionCurveCorr_1");
00708         correctionCurveCorr_1->Write();
00709         correctionCurveCorr_1->Fit("f1");
00710         correctionCurveCorr_1->Fit("f2");
00711 
00712         f.cd("/");
00713 }

Generated on Tue Jun 9 17:44:41 2009 for CMSSW by  doxygen 1.5.4