CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DataFormats/ParticleFlowReco/src/CaloBox.cc

Go to the documentation of this file.
00001 /*
00002  * CaloBox.cc
00003  *
00004  *  Created on: 11-May-2009
00005  *      Author: jamie
00006  */
00007 
00008 #include "DataFormats/ParticleFlowReco/interface/CaloBox.h"
00009 
00010 #include <iostream>
00011 #include <cmath>
00012 using namespace pftools;
00013 using namespace std;
00014 
00015 CaloBox::CaloBox(double centerEta, double centerPhi, double dEta, double dPhi,
00016                 unsigned nEta, unsigned nPhi) :
00017         centerEta_(centerEta), centerPhi_(centerPhi), dEta_(dEta), dPhi_(dPhi),
00018                         nEta_(nEta), nPhi_(nPhi) {
00019         if (nEta_ % 2 != 1) {
00020                 cout << __PRETTY_FUNCTION__
00021                                 << ": should use odd numbers for nEta and nPhi as CaloBox won't be symmetric otherwise!\n";
00022                 etaPosBound_ = (nEta / 2);
00023                 etaNegBound_ = -1 * static_cast<int> ((nEta / 2) + 1);
00024 
00025         } else {
00026                 etaPosBound_ = (nEta - 1) / 2;
00027                 etaNegBound_ = -1 * static_cast<int> ((nEta - 1) / 2);
00028         }
00029 
00030         if (nPhi_ % 2 != 1) {
00031                 cout << __PRETTY_FUNCTION__
00032                                 << ": should use odd numbers for nEta and nPhi as CaloBox won't be symmetric otherwise!\n";
00033 
00034                 phiPosBound_ = (nPhi / 2);
00035                 phiNegBound_ = -1 * static_cast<int> ((nPhi / 2) + 1);
00036         } else {
00037                 phiPosBound_ = (nPhi - 1) / 2;
00038                 phiNegBound_ = -1 * static_cast<int> ((nPhi - 1) / 2);
00039         }
00040 
00041         reset();
00042 
00043         //cout << "CaloBox with n+ " << etaPosBound_ << ", n- " << etaNegBound_
00044         //              << ", p+ " << phiPosBound_ << ", p-" << phiNegBound_ << "\n";
00045 
00046 }
00047 
00048 void CaloBox::reset() {
00049         for (int r = phiPosBound_; r >= phiNegBound_; --r) {
00050                 for (int c = etaNegBound_; c <= etaPosBound_; ++c) {
00051                         std::pair<int, int> pos(c, r);
00052                         energies_[pos] = 0.0;
00053                 }
00054         }
00055 }
00056 
00057 CaloBox::~CaloBox() {
00058 
00059 }
00060 
00061 bool CaloBox::fill(double eta, double phi, double energy) {
00062         int etaDiv = static_cast<int>(floor((eta - centerEta_ + dEta_ / 2.0) / dEta_));
00063         int phiDiv = static_cast<int>(floor((phi - centerPhi_ + dPhi_ / 2.0) / dPhi_));
00064         //cout << "Testing for " << etaDiv << ", " << phiDiv << "\n";
00065 
00066         if (etaDiv >= 0 && etaDiv > etaPosBound_)
00067                 return false;
00068         if (etaDiv < 0 && etaDiv < etaNegBound_)
00069                 return false;
00070         if (phiDiv >= 0 && phiDiv > phiPosBound_)
00071                 return false;
00072         if (phiDiv < 0 && phiDiv < phiNegBound_)
00073                 return false;
00074 
00075         pair<int, int> key(etaDiv, phiDiv);
00076 
00077         energies_[key] += energy;
00078         return true;
00079 
00080 }
00081 
00082 std::ostream& CaloBox::dump(std::ostream& stream, double norm, string rowDelim) const {
00083 
00084         for (int r = phiPosBound_; r >= phiNegBound_; --r) {
00085                 for (int c = etaNegBound_; c <= etaPosBound_; ++c) {
00086                         pair<int, int> pos(c, r);
00087                         double energy = energies_.find(pos)->second/norm;
00088                         stream << energy << "\t";
00089                 }
00090                 stream << rowDelim;
00091         }
00092 //
00093 //      for (map<pair<int, int> , double>::const iterator i = energies_.begin(); i
00094 //                      != energies_.end(); ++i) {
00095 //              pair<int, int> loc = i->first;
00096 //              double en = i->second;
00097 //              stream << loc.first << ", " << loc.second << ": " << en << "\n";
00098 //      }
00099 
00100         return stream;
00101 }
00102 
00103 void CaloBox::test() {
00104 
00105         CaloBox cb(0.0, 0.0, 1.0, 1.0, 5, 5);
00106 
00107         unsigned count(0);
00108         bool ok(false);
00109 
00110         ok = cb.fill(2, 1, 10);
00111         if (!ok)
00112                 cout << "Box fill failed! Count = " << count << "\n";
00113         ++count;
00114 
00115         ok = cb.fill(-1, 2, 20);
00116         if (!ok)
00117                 cout << "Box fill failed! Count = " << count << "\n";
00118         ++count;
00119 
00120         ok = cb.fill(-2, 5, 10);
00121         if (!ok)
00122                 cout << "Box fill failed! Count = " << count << "\n";
00123         ++count;
00124 
00125         ok = cb.fill(0.1, 1.3, 10);
00126         if (!ok)
00127                 cout << "Box fill failed! Count = " << count << "\n";
00128         ++count;
00129 
00130         ok = cb.fill(-1.4, 1.6, 10);
00131         if (!ok)
00132                 cout << "Box fill failed! Count = " << count << "\n";
00133         ++count;
00134 
00135         cout << cb;
00136 
00137 }
00138 
00139 std::ostream& pftools::operator<<(std::ostream& stream, const CaloBox& cb) {
00140         stream << "CaloBox at " << cb.centerEta_ << ", " << cb.centerPhi_ << ":\n";
00141         cb.dump(stream, 1.0, "\n");
00142         return stream;
00143 }
00144