Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

DistRandomiser2D.cxx

Go to the documentation of this file.
00001 #include "FastShowerUtils/Normalisers/DistRandomiser2D.h"
00002 
00003 #include <cmath>
00004 #include <string>
00005 #include <vector>
00006 #include <algorithm>
00007 #include <iostream>
00008 #include <fstream>
00009 namespace FastShower{
00010 
00011   DistRandomiser2D::DistRandomiser2D(const std::string& f){
00012 
00013     // get data form file (histogram)
00014     std::vector<int> v;
00015     readData(f,v);
00016     
00017     // needed to initialise RandGeneral
00018     std::vector<int>::size_type nn =  v.size(); // # of xbins*ybins
00019     int n = (int) std::sqrt(nn); // # of bins
00020     // make sure vector has 50 rows, each with 50 elements
00021     assert(n == 50);
00022 
00023     // histograms
00024     double yDist[50], xDist[50], xyDist[50][50];
00025 
00026     // map vector into a matrix
00027     std::vector<int>::const_iterator itr = v.begin();
00028     std::vector<int>::const_iterator end = v.end();
00029     for (int j=0; j<n; ++j){
00030       for (int i=0; i<n; ++i){
00031         xyDist[i][j] = *itr;
00032         ++itr; 
00033         if (itr == end) itr=v.begin();
00034       }
00035     }
00036     // extract distributions
00037     for (int j=0; j<n; ++j) {
00038       xDist[j] = 0;
00039       for (int i=0; i<n; ++i){
00040         yDist[i] = 0;
00041         // projection of y onto x-axis
00042         xDist[j] += xyDist[i][j];
00043         // slice x-axis, project each x-slice onto y-axis
00044         yDist[i] = xyDist[i][j];
00045       }
00046       // setup distributions of xSlices
00047       m_xSlices.push_back(new RandGeneral(yDist, n, 0));
00048       //for (int nb=0; nb<n; ++nb) cout<<yDist[nb]<<" "; cout<<endl;
00049     }
00050     // setup distribution
00051     m_yProjection=new RandGeneral(xDist, n, 0);
00052     //for (int nb=0; nb<n; ++nb) cout<<xDist[nb]<<" "; cout<<endl;
00053   }
00054   //
00055   void
00056   DistRandomiser2D::readData(const std::string& f, std::vector<int>& v) const {
00057     ifstream data(f.c_str());
00058     istream_iterator<int> dataItr(data);
00059     istream_iterator<int> endOfData;
00060     copy(dataItr,endOfData,back_inserter(v));
00061   }
00062   //
00063   std::pair<double,double> DistRandomiser2D::sample() const {
00064     // sample Ecal fraction
00065     double eFrac = 2.0 * m_yProjection->fire();
00066     // map to bin number (gives Hcal distribution)
00067     int xSlice = (int) (eFrac/0.04); ++xSlice;
00068     // 
00069     std::vector<RandGeneral*>::const_iterator itr = m_xSlices.begin(); 
00070     for (int i=1; i<xSlice; ++i){++itr;}
00071     assert(itr<m_xSlices.end());
00072     // sample Hcal fraction
00073     double hFrac = 2.0 * (*itr)->fire();
00074     std::pair<double,double> ehFracs(eFrac,hFrac);
00075     return ehFracs;
00076   }
00077   //
00078   DistRandomiser2D::~DistRandomiser2D(){
00079   }
00080 }//namespace
00081 

Generated on Tue Mar 18 11:49:56 2003 for FastShowerUtils by doxygen1.3-rc1