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

protons.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/flux/src/test/protons.cxx,v 1.2 2001/06/24 16:42:46 burnett Exp $
00002 #include <iostream>
00003 #include <fstream>
00004 #include <stdlib.h>
00005 #include <utility>
00006 
00007 // CLHEP
00008 #include "CLHEP/Random/JamesRandom.h"
00009 
00010 #include "../CrProton.h"
00011 #include "../CrElectron.h"
00012 
00013 const int BIN_NUM = 25;
00014 const int LOOP    = 10000;
00015 
00016 const double energy_min = 0.1;
00017 const double energy_max = 100.0;
00018 
00019 static const char * default_source="default";
00020 
00021 void help() {
00022    std::cout << 
00023       "   Simple test program for a proton source.\n"
00024       "   Command line args are \n"
00025       "      'nbins <number of bins for histogram>'\n"
00026       "      'nevents <number of events to create>'\n"
00027       "      'graph' to generate a c++ script for ROOT\n"
00028       "      'help' for this help"
00029       << std::endl;
00030 }
00031 
00032 int main(int argc, char** argv)
00033 {
00034    int bin_num = BIN_NUM;  // Initialize to default number of bins
00035    int loop = LOOP;        // Initialize to default number of iterations
00036    
00037    //CrProton src;
00038    CrElectron src;
00039    
00040    HepRandomEngine* engine = new HepJamesRandom;
00041    
00042    std::string source_name(default_source);
00043    
00044    if ( argc > 1 ) source_name = argv[1];
00045    if( source_name =="help") { help(); return 0; }
00046    if( source_name =="nbins") bin_num = atoi(argv[2]);
00047    if( source_name =="nevents") loop = atoi(argv[2]);
00048    if(argc > 3 ) source_name = argv[3];
00049    if( source_name =="nbins") bin_num = atoi(argv[4]);
00050    if( source_name =="nevents") loop = atoi(argv[4]);
00051 
00052    
00053    // Dynamically create and initialize bin array
00054    int *hist = new int[bin_num];
00055    for (int i = 0; i < bin_num; i++) hist[i] = 0;
00056 
00057 
00058    // Calculate constant logs
00059    double log10_energy_min = log10(energy_min);
00060    double log10_energy_max = log10(energy_max), range=log10_energy_max-log10_energy_min;
00061 
00062    double flux = src.flux(), scale_factor = flux/loop*bin_num/range;
00063 
00064    {for (int i = 0; i < loop; i++){
00065       src.selectComponent(engine);
00066       double energy = src.energySrc(engine); 
00067       std::pair<double,double> dir = src.dir(energy, engine);
00068       // Notice: dir consists of cos(zenith_angle) and azimuth [rad]
00069   
00070      hist[int(bin_num * (log10(energy) - log10_energy_min) / range)]++;
00071 
00072 
00073       if (i % 10000 == 0) std::cerr << '\015' << i << ": " << energy << "...";
00074    }}
00075    std::cerr << "\n";
00076    
00077    {for (int i = 0; i < bin_num; i++){
00078        std::cout << pow(10,((i + 0.5)/bin_num) * range + log10_energy_min) 
00079            << "   \t" << hist[i] << "\t";
00080        std::cout << std::endl;
00081    }}
00082    
00083    // Create file that can be run with ROOT to generate a graph
00084 
00085    std::ofstream out_file("graph.cxx");
00086 
00087    out_file << "{\n"
00088                "   gROOT->Reset();\n"
00089                "   double emin= "<< energy_min << ", emax=" << energy_max << ";\n"; 
00090    out_file << "   double count[] = {\n";
00091    {for(int i = 0; i < bin_num; i++)
00092        out_file << std::setw(5) << hist[i] << (i%5==4? ",\n" : ",");
00093    }
00094 
00095    out_file << "      };\n\n"
00096                "   int bin_num = "<< bin_num << ";\n"
00097                "   double range = " << range << ";\n"
00098                "   double scale_factor =" << scale_factor << ", log10_energy_min=log10(emin);\n"
00099                "   double e_energy[bin_num];\n"
00100                "   double e_count[bin_num], energy[bin_num];\n\n"
00101                "   for(int i = 0; i < bin_num; i++) {\n"
00102                "      energy[i] = pow(10,((i+0.5)/bin_num) * range+ log10_energy_min);\n"
00103                "      e_count[i] = sqrt(count[i])*scale_factor;\n"
00104                "      e_energy[i] = 0;\n"
00105                "      count[i] *=scale_factor;\n"
00106                "   }\n"
00107                "   \n"
00108                "   //                 name, title, wtopx, wtopy, ww, wh\n"
00109                "   c1 = new TCanvas(\"c1\",\"Graph Window\", 200, 200, 700, 500);\n"
00110                "   c1->SetGrid();\n"
00111                "   c1->GetFrame()->SetFillColor(21);\n"
00112                "   c1->GetFrame()->SetBorderSize(12);\n"
00113                "   c1->SetLogx(1); c1->SetLogy(1);\n"
00114                "   gr = new TGraphErrors(bin_num,energy,count,e_energy,e_count);\n"
00115                "   gr->SetTitle(\"Proton Count vs. Energy\");\n"
00116                "   gr->SetMarkerColor(4);\n"
00117                "   gr->SetMarkerStyle(21);\n"
00118                "   gr->Draw(\"AP\");\n"
00119                "   TAxis* ax = gr->GetXaxis(), *ay= gr->GetYaxis();\n"
00120                "   ay->SetTitle(\"E*Flux (particles/m^2/s/)\"); ay->CenterTitle(1);\n"
00121                "   ax->SetLimits(emin, emax);\n"
00122                "   ax->SetTitle(\"Kinetic Energy (GeV)\"); ax->CenterTitle(1); \n"
00123                "   ax->SetTitleOffset(1.2);\n"
00124                "   c1->Modified();\n"
00125                "}\n";
00126 
00127    // Clean up
00128    
00129    out_file.close();
00130    system("root -l graph.cxx");
00131 
00132    delete engine;
00133    delete [] hist;
00134    
00135    return 0;
00136 }

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