00001
00002
00003
00004
00005
00006
00007
00008
00014 #include "flux/FluxMgr.h"
00015 #include "flux/EventSource.h"
00016 #include "flux/ISpectrumFactory.h"
00017 #include "../SpectrumFactoryTable.h"
00018
00019 #include "rootEnergyHist.h"
00020 #include "rootAngleHist.h"
00021
00022 #include <fstream>
00023
00024 const int NUM_BINS = 30;
00025 const int LOOP = 30000;
00026
00027 const double ENERGY_MIN = 0.01;
00028 const double ENERGY_MAX = 100.0;
00029
00030 static const char * default_arg="default";
00031 static const char * default_graph="log";
00032
00033 void help() {
00034 std::cout <<
00035 " Simple test program for a particle source.\n"
00036 " Command line args are \n"
00037 " '-sum' sums up the histograms'\n"
00038 " '-bins <number of bins for histogram>'\n"
00039 " '-events <number of events to create>'\n"
00040 " '-min' or '-energy_min' sets minimum energy\n"
00041 " '-max' or '-energy_max' sets maximum energy\n"
00042 " '-list' lists the available spectra\n"
00043 " '-file <file name>' writes energy vs. flux to filename.txt\n"
00044 " '-trueflux' graphs using flux using per steradian\n"
00045 " '-flux' graphs the flux vs. E instead of E*flux vs E\n"
00046 " '-flux_min' manually set lower limit for graph\n"
00047 " '-flux_max' manually set upper limit for graph\n"
00048 " '-graph <log | semilogx | semilogy | linear>'\n"
00049 " '-longsrc <sourcename>' for long-term energy averaging"
00050 " '-help' for this help"
00051 << std::endl;
00052 }
00053
00054 void listSources(const std::list<std::string>& source_list ) {
00055 std::cout << "List of available sources:" << std::endl;
00056 for( std::list<std::string>::const_iterator it = source_list.begin();
00057 it != source_list.end(); ++it) {
00058 std::cout << '\t'<< *it << std::endl;
00059 }
00060 }
00061
00062 void listSpectra() {
00063 std::cout << "List of loaded Spectrum objects: " << std::endl;
00064 std::list<std::string> spectra(SpectrumFactoryTable::instance()->spectrumList());
00065 for( std::list<std::string>::const_iterator it = spectra.begin();
00066 it != spectra.end(); ++it) {
00067 std::cout << '\t'<< *it << std::endl;
00068 }
00069 }
00070
00071 #define DLL_DECL_SPECTRUM(x) extern const ISpectrumFactory& x##Factory; x##Factory.addRef();
00072
00073 void flux_load() {
00074
00075
00076 DLL_DECL_SPECTRUM( CHIMESpectrum);
00077 DLL_DECL_SPECTRUM( AlbedoPSpectrum);
00078 DLL_DECL_SPECTRUM( HeSpectrum);
00079 DLL_DECL_SPECTRUM( GalElSpectrum);
00080 DLL_DECL_SPECTRUM( CrElectron);
00081 DLL_DECL_SPECTRUM( CrProton);
00082 DLL_DECL_SPECTRUM( FILESpectrum);
00083
00084 }
00085
00086 void WARNING (const char * text ){ std::cerr << "WARNING: " << text << '\n';}
00087 void FATAL(const char* s){std::cerr << "\nERROR: "<< s;}
00088
00098 int main(int argc, char** argv)
00099 {
00100 int num_bins = NUM_BINS;
00101 int loop = LOOP;
00102 int num_sources = 0;
00103 int num_longsources = 0;
00104 int current_arg = 1;
00105 int longtime=1;
00106 int longtimemax=20;
00107 double energy_min = ENERGY_MIN;
00108 double energy_max = ENERGY_MAX;
00109 bool use_trueflux = false;
00110 bool use_flux = false;
00111 bool use_flux_min = false;
00112 bool use_flux_max = false;
00113 bool write_to_file = false;
00114 bool sum_mode = false;
00115 bool longterm=false;
00116 double flux_min;
00117 double flux_max;
00118 std::string arg_name(default_arg);
00119 std::string output_file_name;
00120 std::string ylabel_flux = "Flux (particles/m^2/s/MeV/sr)";
00121 std::string ylabel_eflux = "E*Flux (particles/m^2/s/sr)";
00122 std::string ylabel_sflux = "Flux (particles/m^2/s/MeV)";
00123 std::string ylabel_seflux = "E*Flux (particles/m^2/s)";
00124
00125 std::vector<std::string> sources;
00126 std::vector<int> longsources;
00127
00128 flux_load();
00129
00130 FluxMgr fm;
00131
00132
00133
00134
00135 std::cout << "------------------------------------------------------" << std::endl;
00136 std::cout << " Flux test program: type 'rootplot -help' for help" << std::endl;
00137 std::cout << ( ( argc == 1)? " No command line args, using defaults"
00138 : "") << std::endl;
00139
00140
00141
00142 while(current_arg < argc)
00143 {
00144 arg_name = argv[current_arg];
00145 if("-help" == arg_name || "help" == arg_name)
00146 {
00147 help();
00148 return 0;
00149 }
00150 else if("-bins" == arg_name)
00151 num_bins = atoi(argv[++current_arg]);
00152 else if("-events" == arg_name)
00153 loop = atoi(argv[++current_arg]);
00154 else if("-min" == arg_name || "-energy_min" == arg_name)
00155 energy_min = atof(argv[++current_arg]);
00156 else if("-max" == arg_name || "-energy_max" == arg_name)
00157 energy_max = atof(argv[++current_arg]);
00158 else if("-flux_min" == arg_name)
00159 {
00160 use_flux_min = true;
00161 flux_min = atof(argv[++current_arg]);
00162 }
00163 else if("-flux_max" == arg_name)
00164 {
00165 use_flux_max = true;
00166 flux_max = atof(argv[++current_arg]);
00167 }
00168 else if("-flux" == arg_name)
00169 use_flux = true;
00170 else if("-trueflux" == arg_name)
00171 use_trueflux = true;
00172 else if("-file" == arg_name)
00173 {
00174 write_to_file = true;
00175 output_file_name = argv[++current_arg];
00176 }
00177 else if("-sum" == arg_name)
00178 sum_mode = true;
00179 else if("-list" == arg_name)
00180 {
00181 listSources(fm.sourceList());
00182 listSpectra();
00183 return 0;
00184 }
00185 else if("-graph" == arg_name)
00186 default_graph = argv[++current_arg];
00187 else if("-liny" == arg_name) default_graph = "semilogx";
00188 else if("-longsrc" == arg_name) {
00189 sources.push_back(argv[++current_arg]);
00190
00191 longsources.push_back(num_sources);
00192 num_sources++;
00193 }
00194 else if('-' == arg_name[0]) {std::cerr << "Unrecognized option "<< arg_name << ", -help for help" << std::endl;}
00195 else
00196 {
00197 sources.push_back(arg_name);
00198 num_sources++;
00199 }
00200 current_arg++;
00201 }
00202
00203
00204
00205 if(0 == sources.size())
00206 {
00207 sources.push_back(default_arg);
00208 num_sources++;
00209 }
00210
00211 rootEnergyHist energy_hist(num_bins,energy_min,energy_max);
00212 rootAngleHist angle_hist(num_bins);
00213
00214
00215 for(int i = 0; i < num_sources; i++)
00216 {
00217 int j;
00218
00219
00220 longterm = false;
00221 std::vector<int>::iterator longiter;
00222 for( longiter=longsources.begin(); longiter!=longsources.end() ;longiter++){
00223 if(*longiter==i) longterm=true;
00224 }
00225
00226 if(longterm)
00227 fm.setExpansion(-1.);
00228
00229
00230 if((false == sum_mode && false==longterm)||(true==longterm && (longtime==1)))
00231 {
00232
00233 energy_hist.reset();
00234 angle_hist.reset();
00235 }
00236
00237 EventSource *e = fm.source(sources[i]);
00238
00239 if(longterm)
00240 fm.pass(2.);
00241
00242 std::pair<double,double> loc=fm.location();
00243 std::cout << loc.first << " " << loc.second << std::endl;
00244
00245
00246 if( 0==e ) {std::cerr << "Source \"" << sources[i] << "\" not found: -list for a list" << std::endl;
00247 return -1;}
00248
00249 energy_hist.setGraphType(default_graph);
00250 energy_hist.setTitle( sources[i] );
00251
00252 energy_hist.setXLabel("Kinetic Energy (GeV)");
00253
00254 if(true == use_flux)
00255 {
00256 energy_hist.setFluxMode();
00257 if(false == use_trueflux)
00258 energy_hist.setYLabel(ylabel_sflux);
00259 else
00260 energy_hist.setYLabel(ylabel_flux);
00261 }
00262 else
00263 {
00264 if(false == use_trueflux)
00265 energy_hist.setYLabel(ylabel_seflux);
00266 else
00267 energy_hist.setYLabel(ylabel_eflux);
00268 }
00269
00270 if(true == use_flux_min)
00271 energy_hist.setFluxMin(flux_min);
00272
00273 if(true == use_flux_max)
00274 energy_hist.setFluxMax(flux_max);
00275
00276 angle_hist.setGraphType(default_graph);
00277 angle_hist.setTitle( sources[i] );
00278 angle_hist.setPhiXLabel("Angle (degrees)");
00279 angle_hist.setPhiYLabel("Particles");
00280 angle_hist.setThetaXLabel("Cos(Theta)");
00281 angle_hist.setThetaYLabel("Particles");
00282
00283 double scale_factor;
00284
00285 if(false == use_trueflux)
00286 {
00287 double rate = e->rate()/e->totalArea();
00288 scale_factor = rate/loop*num_bins/log(energy_max/energy_min);
00289 std::cout << "RATE IS: " << rate << std::endl;
00290 }
00291 else
00292 {
00293 double flux = e->flux();
00294 scale_factor = flux/loop*num_bins/log(energy_max/energy_min);
00295 std::cout << "FLUX IS: " << flux << std::endl;
00296 }
00297
00298 for(j = 0; j < loop; j++)
00299 {
00300 FluxSource *f = e->event();
00301 double energy = f->energy();
00302 Vector dir = f->launchDir();
00303 double cos_theta = dir.z();
00304
00305 double phi = atan2(dir.y(),dir.x());
00306 if(phi < 0)
00307 phi = 2*M_PI + phi;
00308
00309 energy_hist.store(energy);
00310 angle_hist.storeTheta(cos_theta);
00311 angle_hist.storePhi(phi);
00312
00313 if(j % 10000 == 0) std::cerr << "\r" << j << ": " << energy << "...";
00314 }
00315
00316 std::cerr << "\n";
00317
00318 energy_hist.apply(scale_factor);
00319 angle_hist.apply(scale_factor);
00320
00321 for(j = 0; j < num_bins; j++)
00322 {
00323 std::cout << energy_min*pow(10,((j + 0.5)/num_bins) * energy_hist.retrieveRange() )
00324 << " \t" << energy_hist.retrieveFlux(j) << "\t";
00325 std::cout << std::endl;
00326 }
00327
00328
00329 if(false == sum_mode && false==longterm)
00330 {
00331 angle_hist.draw(1,"begin",i,num_sources);
00332 energy_hist.draw(1,"end",i,num_sources);
00333
00334 delete e;
00335 std::cout << "Normal method" << std::endl;
00336 }
00337 else if(true == sum_mode && i == num_sources - 1)
00338 {
00339 angle_hist.draw(1,"begin",0,1);
00340 energy_hist.draw(1,"end",0,1);
00341
00342 delete e;
00343 std::cout << "Sum Mode method" << std::endl;
00344 }
00345 else if(longterm==true && longtime<=longtimemax)
00346 {
00347 longtime++;
00348 i--;
00349 std::cout << "Longterm run number " << longtime << std::endl;
00350 }
00351 else
00352 {
00353 if(true == write_to_file)
00354 {
00355 std::ofstream output_file;
00356 output_file_name += ".txt";
00357 output_file.open(output_file_name.c_str());
00358
00359 energy_hist.writeFile(1./(longtime),output_file);
00360 output_file.close();
00361 }
00362 angle_hist.draw(1./double(longtime),"begin",i,num_sources);
00363 energy_hist.draw(1./double(longtime),"end",i,num_sources);
00364
00365 delete e;
00366 longtime=1;
00367
00368 }
00369
00370 }
00371
00372 return 0;
00373 }