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

ExtraGalacticDiffuse.cxx

Go to the documentation of this file.
00001 // ExtraGalacticDiffuse.cxx: implementation of the ExtraGalacticDiffuse class.
00002 
00003 //#include "StdAfx.h"
00004 #include <cmath>
00005 #include <iostream>
00006 #include "ExtraGalacticDiffuse.h"
00007 #include "time.h"
00008 #include<vector>
00009 #include "SpectrumFactory.h"
00010 
00011 static SpectrumFactory<ExtraGalacticDiffuse> factory;
00012 
00013 
00014 long double ExtraGalacticDiffuse::pofi(long double intensity){  //this function gives P(I).  see documentation.
00015     long double p;
00016     //printf("\nabout to calculate pofi...");
00017     if(intensity>=pow(10.0,-7)){
00018         p=2.49555*pow(10.0,-13)*pow(intensity,-2.5);//egret range value for pofi
00019     }else{
00020         p=2.49555*pow(10.0,-13)*pow(intensity,-2.5-0.05*(7.0+(log(intensity)/log(10.0))));      
00021     }   //glast range value for pofi
00022     //printf("\np=%12.10e, intensity=%12.10e",p,intensity);
00023     
00024     return p;
00025 }
00026 
00027 //long double ExtraGalacticDiffuse::pofi(long double intensity);
00028 
00029 
00030 long double ExtraGalacticDiffuse::random(){
00031     long double r=rand()/32767.0;
00032     return r;
00033 }
00034 
00035 
00036 ExtraGalacticDiffuse::DELTAX ExtraGalacticDiffuse::gaussianspread(){  //gives uncertainty to locations of incoming photons.
00037     DELTAX spread;
00038     long double x,y,theta,phi;
00039     
00040     theta=log(1.0/random());
00041     phi=random()*2.0*3.141592653589793238;
00042     
00043     x=theta*sin(phi);
00044     y=theta*cos(phi);
00045     
00046     spread.x=x;
00047     spread.y=y;
00048     return spread;
00049 }
00050 
00051 
00052 void ExtraGalacticDiffuse::addtophotons(long double x,long double y){
00053     int n;
00054     DELTAX smear;
00055     
00056     for(n=0 ; list[n].x!='\0' ; n++){
00057     }
00058     smear=gaussianspread();
00059     list[n].x=x+smear.x;
00060     list[n+1].x='\0';
00061     list[n].y=y+smear.y;
00062     list[n+1].y='\0';
00063     //  printf("\nAdded Photon to list, x=%lf, y=%lf",x,y);
00064 }
00065 
00066 
00067 void ExtraGalacticDiffuse::addtoctlg(long double x,long double y,long double intsty){
00068     int n;
00069     for(n=0 ; ctlg[n].x!='\0' ; n++){
00070     }
00071     ctlg[n].x=x;
00072     ctlg[n+1].x='\0';
00073     ctlg[n].y=y;
00074     ctlg[n].intensity=intsty;
00075 }
00076 
00077 void ExtraGalacticDiffuse::findandaddnew(){  //finds a new source to add from the catalog, adds it, 
00078     //and sends a photon from it.
00079     long double x=random()*skysizex;
00080     long double y=random()*skysizey;
00081     long double prob=random();
00082     //  cout << "x,y="<<  x  << '\n' << y  << '\n';
00083     long double dx=0.0000001;
00084     long double i=lowthresholdofintensity;
00085     
00086     while(prob > 0 && i<highthresholdofintensity){
00087         dx=0.01*pow(10.0,log10(i));
00088         prob-=(dx)*pofi(i);
00089         i+=dx;
00090         //      printf("\nin the findandaddnew loop; i=%12.10e, prob=%12.10e, dx=%12.10e , logi=%lf\n",i,prob,dx,log10(i));
00091     }
00092     
00093     addtophotons(x,y);
00094     addtoctlg(x,y,i-dx);
00095     RemInt-=i;
00096 }
00097 
00098 void ExtraGalacticDiffuse::sendphotonfromcatalog(){
00099     int i=0;
00100     long double num;
00101     long double catprob=1.0-NewProb;
00102     
00103     num=random()*catprob;
00104     for(i=0 ; num>0 ; i++){
00105         num-=ctlg[i].intensity/(TotInt-RemInt);}
00106     addtophotons(ctlg[i-1].x,ctlg[i-1].y);
00107 }
00108 
00109 
00110 ExtraGalacticDiffuse::PHOTON *ExtraGalacticDiffuse::create()
00111 {
00112     long double rand;
00113     PHOTON *pointpho=NULL;
00114     
00115     int g=0;
00116     
00117     srand((unsigned)time(NULL));
00118     //                                  addtoctlg(0.5,2.9704,0.0001);
00119     //                                  RemInt-=0.0001;
00120     while(RemInt>=threshold || g<maxnumofphotons){
00121         //this sends a photon from the catalog if the random number was below the 
00122         //percentage of intensity already accounted for..
00123         g++;
00124         rand=random();
00125         NewProb=RemInt/TotInt;
00126         //      cout << "newprob=" << NewProb << '\n';
00127         if(rand<=NewProb){
00128             findandaddnew();
00129             //          printf("\ncompleted printandaddnew, Remint=%12.10e",RemInt);
00130         }
00131         else{sendphotonfromcatalog();  //and finds a new source if not.
00132         //printf("\ncompleted sendphotonfromcatalog");
00133         }
00134     }
00135     
00136     
00137     return pointpho;
00138 };
00139 
00140 
00141 
00142 //here's the constructor - grabs the data and makes a constant 
00143 
00144 ExtraGalacticDiffuse::ExtraGalacticDiffuse(const char* name,float Emin, float Emax, float index){
00145     TotInt=.0000976759*multiplierduetosizeofsky;
00146     RemInt=TotInt;
00147     list[0].x='\0';
00148     list[0].y='\0';
00149     ctlg[0].x='\0';
00150     ctlg[0].y='\0';
00151     
00152     //Initialize the subclass with the appropriate values (from this object)
00153     SimpleSpectrum(name, Emin, Emax, index);
00154     
00155     //Call the create() function to initialize this object
00156     const ExtraGalacticDiffuse::PHOTON* photonlist=ExtraGalacticDiffuse::create();
00157     
00158     
00159     std::vector<std::pair<double,double> > listofphotons;
00160     
00161     
00162     //then put the photons in list into listofphotons
00163     
00164     int lenlst;
00165     
00166     std::pair<double,double> holder;
00167     //  cout << "totint=" << gms.TotInt << '\n';
00168     
00169     for(lenlst=0 ; list[lenlst].x!='\0' ; lenlst++){
00170         holder.first=list[lenlst].x;
00171         holder.second=list[lenlst].y;
00172         listofphotons.push_back (holder);
00173     }
00174     
00175     
00176     srcpnt = listofphotons.begin();
00177     //cout << "totint=" << TotInt << '\n';
00178     
00179 };
00180 
00181 
00182 //and our destructor:
00183 ExtraGalacticDiffuse::~ExtraGalacticDiffuse(){
00184 };
00185 
00186 
00187 std::pair<double,double> ExtraGalacticDiffuse::dir(double e){
00188     
00189     std::pair<double,double> one;
00190     std::pair<double,double> coords;
00191     
00192     one=*srcpnt;
00193     
00194     coords.first=one.first;
00195     coords.second=one.second;
00196     srcpnt++;
00197     return coords;
00198 }
00199 
00200 
00201 

Generated at Wed Nov 21 12:20:33 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000