CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/DataFormats/ParticleFlowReco/src/CaloWindow.cc

Go to the documentation of this file.
00001 #include "DataFormats/ParticleFlowReco/interface/CaloWindow.h"
00002 #include "DataFormats/Math/interface/deltaR.h"
00003 #include <TMath.h>
00004 
00005 using namespace pftools;
00006 //using namespace edm;
00007 using namespace std;
00008 
00009 //Let's do CaloRing first
00010 CaloRing::CaloRing() :
00011         panes_(1) {
00012         myPanes_.push_back(0);
00013 }
00014 
00015 CaloRing::CaloRing(unsigned nPanes) :
00016         panes_(nPanes) {
00017         for (unsigned j(0); j < nPanes; ++j)
00018                 myPanes_.push_back(0);
00019 }
00020 
00021 double CaloRing::getEnergy(unsigned pane) const {
00022         if (pane < panes_)
00023                 return myPanes_[pane];
00024         else
00025                 return 0;
00026 }
00027 
00028 bool CaloRing::setEnergy(unsigned pane, double energy) {
00029         if (pane < panes_) {
00030                 myPanes_[pane] = energy;
00031                 return true;
00032         }
00033         return false;
00034 }
00035 
00036 bool CaloRing::addEnergy(unsigned pane, double energy) {
00037         if (pane < panes_) {
00038                 myPanes_[pane] += energy;
00039                 return true;
00040         }
00041         return false;
00042 }
00043 
00044 double CaloRing::totalE() const {
00045         double ans(0.0);
00046         for (std::vector<double>::const_iterator cit = myPanes_.begin(); cit
00047                         != myPanes_.end(); ++cit) {
00048                 ans += *cit;
00049         }
00050         return ans;
00051 }
00052 
00053 void CaloRing::reset() {
00054         for (std::vector<double>::iterator cit = myPanes_.begin(); cit
00055                         != myPanes_.end(); ++cit)
00056                 *cit = 0.0;
00057 
00058 }
00059 
00060 void CaloRing::printEnergies(std::ostream& s, double range) {
00061         for (std::vector<double>::iterator cit = myPanes_.begin(); cit
00062                         != myPanes_.end(); ++cit)
00063                 s << (*cit)/range << "\t";
00064 }
00065 
00066 std::ostream& pftools::operator<<(std::ostream& s,
00067                 const pftools::CaloRing& caloRing) {
00068         for (std::vector<double>::const_iterator it = caloRing.myPanes_.begin(); it
00069                         != caloRing.myPanes_.end(); ++it) {
00070                 s << *it << "\t";
00071         }
00072         s << " => ring E = " << caloRing.totalE();
00073         return s;
00074 }
00075 
00076 std::ostream& pftools::operator<<(std::ostream& s,
00077                 const pftools::CaloWindow& caloWindow) {
00078 
00079         s << "CaloWindow at (" << caloWindow.baryEta_ << ", "
00080                         << caloWindow.baryPhi_ << "):\n";
00081         double totalE(0.0);
00082         for (std::map<unsigned, pftools::CaloRing>::const_iterator cit =
00083                         caloWindow.energies_.begin(); cit != caloWindow.energies_.end(); ++cit) {
00084                 unsigned ring = (*cit).first;
00085                 const CaloRing& cr = (*cit).second;
00086                 s << "Ring " << ring << ":\t" << cr << "\n";
00087                 totalE += cr.totalE();
00088         }
00089 
00090         s << "\tTotal E = " << totalE << std::endl;
00091         return s;
00092 
00093 }
00094 
00095 vector<double> CaloWindow::stream(double normalisation) const {
00096         vector<double> stream;
00097         for (map<unsigned, pftools::CaloRing>::const_iterator cit =
00098                         energies_.begin(); cit != energies_.end(); ++cit) {
00099                 CaloRing c = (*cit).second;
00100                 std::vector<double> ringE = c.getEnergies();
00101                 stream.insert(stream.end(), ringE.begin(), ringE.end());
00102         }
00103         if(normalisation != 1.0){
00104                 for(vector<double>::iterator i = stream.begin(); i != stream.end(); ++i) {
00105                         double& entry = *i;
00106                         entry /= normalisation;
00107                 }
00108         }
00109         return stream;
00110 
00111 }
00112 
00113 void CaloWindow::printEnergies(std::ostream& s, double range) {
00114 
00115         for (std::map<unsigned, pftools::CaloRing>::const_iterator cit =
00116                         energies_.begin(); cit != energies_.end(); ++cit) {
00117                 CaloRing c = (*cit).second;
00118                 c.printEnergies(s, range);
00119                 s << "\t";
00120         }
00121 
00122 }
00123 
00124 CaloWindow::CaloWindow() :
00125         baryEta_(0.0), baryPhi_(0.0), nRings_(1), deltaR_(0.1), nPanes_(1),  axis_(0.0) {
00126 
00127 }
00128 
00129 CaloWindow::CaloWindow(double eta, double phi, unsigned nRings, double deltaR, unsigned nPanes, double axis) {
00130 
00131         init(eta, phi, nRings, deltaR, nPanes, axis);
00132 }
00133 
00134 void CaloWindow::reset() {
00135         energies_.clear();
00136 }
00137 
00138 void CaloWindow::init(double eta, double phi, unsigned nRings, double deltaR, unsigned nPanes, double axis) {
00139         baryEta_ = eta;
00140         baryPhi_ = phi;
00141         nRings_ = nRings;
00142         deltaR_ = deltaR;
00143         nPanes_ = nPanes;
00144         axis_ = axis;
00145 
00146         energies_.clear();
00147         CaloRing c(1);
00148         energies_[0] = c;
00149         for (unsigned j(1); j < nRings_; ++j) {
00150 
00151                 CaloRing r(nPanes);
00152                 energies_[j] = r;
00153         }
00154 }
00155 
00156 CaloWindow::~CaloWindow() {
00157 
00158 }
00159 
00160 std::pair<unsigned, unsigned> CaloWindow::relativePosition(double eta,
00161                 double phi) const {
00162         //How far is this hit from the barycentre in deltaR?
00163         double dEta = eta - baryEta_;
00164         double dPhi = deltaPhi(phi, baryPhi_);
00165 
00166         double dR = deltaR(eta, phi, baryEta_, baryPhi_);
00167 
00168         unsigned ring = static_cast<unsigned> (floor(dR / deltaR_));
00169 
00170         if (ring == 0) {
00171                 //LogDebug("CaloWindow") << "Relative position: adding to central ring\n";
00172                 return std::pair<unsigned, unsigned>(0, 0);
00173         }
00174 
00175         if (ring >= nRings_) {
00176                 return std::pair<unsigned, unsigned>(ring, 0);
00177         }
00178 
00179         double dTheta = 0;
00180 
00181         if (dEta > 0) {
00182                 if (dPhi > 0)
00183                         dTheta = TMath::ATan(dPhi / dEta);
00184                 else
00185                         dTheta = 2 * TMath::Pi() + TMath::ATan(dPhi / dEta);
00186         } else {
00187                 if (dPhi > 0)
00188                         dTheta = TMath::Pi() + TMath::ATan(dPhi / dEta);
00189                 else
00190                         dTheta = TMath::Pi() + TMath::ATan(dPhi / dEta);
00191         }
00192         //Rotation. Rotate theta into axis of caloWindow
00193         // and also by half a window pane.
00194         //TODO: bug check!!
00195         //double dThetaOrig(dTheta);
00196         double paneOn2 = TMath::Pi() / (*energies_.find(ring)).second.getNPanes();
00197         dTheta =  dTheta - axis_ - paneOn2;
00198         //Original theta between 0 and 2 Pi, but transform above might move us beyond this, so...
00199         //double dThetaCopy(dTheta);
00200         //std::cout << "dTheta " << dThetaOrig << ", axis " << axis_ << ", paneOn2 " << paneOn2 << ", thetaPrime " << dTheta << "\n";
00201         if(dTheta > 2 *TMath::Pi()) {
00202                 //std::cout << "To infinity and beyond...\n";
00203                 while(dTheta > 2 * TMath::Pi()) {
00204                         dTheta -= 2 * TMath::Pi();
00205                         //std::cout << "dTheta1 " << dTheta << "\n";
00206                 }
00207         }
00208         else if (dTheta < 0) {
00209                 //std::cout << "To infinity and beyond 2... dTheta = " << dTheta << "\n";
00210                 while(dTheta < 0) {
00211                         dTheta += 2 * TMath::Pi();
00212                         //std::cout << "dTheta2 " << dTheta << "\n";
00213                 }
00214         }
00215 
00216 //      std::cout << "\tdTheta is " << dTheta << " rad \n";
00217         unsigned division = static_cast<unsigned> (floor((*energies_.find(ring)).second.getNPanes() * dTheta
00218                         / (TMath::Pi() * 2)));
00219         //LogDebug("CaloWindow") << "Ring is " << ring << ", pane is "
00220         //              << division << "\n";
00221 
00222         return std::pair<unsigned, unsigned>(ring, division);
00223 
00224 }
00225 
00226 bool CaloWindow::addHit(double eta, double phi, double energy) {
00227 
00228         std::pair<unsigned, unsigned> position = relativePosition(eta, phi);
00229         if (position.first >= nRings_) {
00230 /*              double dEta = eta - baryEta_;
00231                 double dPhi = deltaPhi(phi, baryPhi_);
00232                 double dR = deltaR(eta, phi, baryEta_, baryPhi_);
00233                 LogDebug("CaloWindow")
00234                                 << "Hit is outside my range - it would be in ring "
00235                                 << position.first << ", with dR = " << dR << std::endl;*/
00236                 return false;
00237         }
00238 //      std::cout << "Adding hit to ring " << position.first << " in position "
00239 //                      << position.second << " with energy " << energy << "\n";
00240 
00241         CaloRing& c = energies_[position.first];
00242         c.addEnergy(position.second, energy);
00243 
00244         return true;
00245 }
00246 std::map<unsigned, double> CaloWindow::getRingEnergySummations() const {
00247         std::map<unsigned, double> answer;
00248         for (std::map<unsigned, CaloRing>::const_iterator cit = energies_.begin(); cit
00249                         != energies_.end(); ++cit) {
00250                 std::pair<unsigned, CaloRing> pair = *cit;
00251                 answer[pair.first] = pair.second.totalE();
00252         }
00253 
00254         return answer;
00255 }
00256 
00257 void TestCaloWindow::doTest() {
00258 
00259         CaloWindow cw(0.0, 0.0, 3, 0.1, 4);
00260         std::cout << cw << std::endl;
00261         cw.addHit(0, 0.05, 1.0);
00262         cw.addHit(0.22, 0.05, 2.0);
00263         cw.addHit(0, 0.8, 4.0);
00264         cw.addHit(0.2, 0, 1.0);
00265         cw.addHit(0.15, 0.15, 2.0);
00266         cw.addHit(0.0, 0.2, 3.0);
00267         cw.addHit(-0.15, 0.15, 4.0);
00268         cw.addHit(-0.2, 0, 5.0);
00269         cw.addHit(-0.15, -0.15, 6.0);
00270         cw.addHit(-0.0, -0.2, 7.0);
00271         cw.addHit(0.15, -0.15, 8.0);
00272 
00273         std::cout << cw << std::endl;
00274         std::cout << "bye bye" << std::endl;
00275 
00276 }