00001
00002
00003
00004 #include "FluxMgr.h"
00005 #include "FluxSvc/FluxSource.h"
00006 #include "SpectrumFactoryTable.h"
00007 #include "GPS.h"
00008 #include "FluxException.h"
00009 #include "CompositeDiffuse.h"
00010
00011 #include "dom/DOM_Document.hpp"
00012 #include "dom/DOM_Element.hpp"
00013 #include "xml/Dom.h"
00014 #include "xml/IFile.h"
00015
00016 #include "Orbit.h"
00017
00018
00019 #define DLL_DECL_SPECTRUM(x) extern const ISpectrumFactory& x##Factory; x##Factory.addRef();
00020
00021
00022
00023 FluxMgr::FluxMgr(const std::vector<std::string>& fileList, std::string dtdname)
00024 : m_dtd(dtdname.empty()? "$(FLUXSVCROOT)/xml/source.dtd" : dtdname )
00025 {
00026 if( fileList.empty() ){
00027 defaultFile();
00028 }else{
00029 init(fileList);
00030 }
00031 }
00032
00033 void FluxMgr::defaultFile(){
00034
00035 std::vector<std::string> input;
00036
00037
00038 static const char* initialization_document="source_library.xml";
00039 const char* flux_root = ::getenv("FLUXSVCROOT");
00040 std::string doc_path= (flux_root? std::string(flux_root)+"/xml/" : "");
00041 input.push_back("$(FLUXSVCROOT)/xml/source_library.xml");
00042 init(input);
00043 }
00044
00045 void FluxMgr::init(const std::vector<std::string>& fileList){
00046
00047
00048
00049 std::string fileName;
00050
00051 xml::XmlParser parser;
00052
00053 std::string xmlFileIn = writeXmlFile(fileList);
00054
00055
00056
00057
00058 m_library_doc = parser.parse(xmlFileIn);
00059
00060 if (m_library_doc == DOM_Document()) {
00061 FATAL_MACRO("Parse error: processing the document" << std::endl
00062 << xmlFileIn << std::endl);
00063 return;
00064 }
00065
00066
00067
00068
00069 s_library = m_library_doc.getDocumentElement();
00070
00071
00072 if (s_library != DOM_Element()) {
00073
00074 DOM_Element child = xml::Dom::getFirstChildElement(s_library);
00075 DOM_Element toplevel = xml::Dom::getFirstChildElement(s_library);
00076
00077 while (child != DOM_Element()) {
00078 while (child.getAttribute("name") == DOMString()) {
00079 s_library = child;
00080 child = xml::Dom::getFirstChildElement(s_library);
00081 }
00082
00083 while (child != DOM_Element()) {
00084 std::string name = xml::Dom::transToChar(child.getAttribute("name"));
00085
00086 std::string parentfilename = xml::Dom::transToChar(toplevel.getAttribute("title"));
00087 m_sources[name]=std::make_pair<DOM_Element,std::string>(child,parentfilename);
00088 child = xml::Dom::getSiblingElement(child);
00089 }
00090
00091 child = xml::Dom::getSiblingElement(toplevel);
00092 toplevel=child;
00093 }
00094
00095 }
00096
00097 DLL_DECL_SPECTRUM( CHIMESpectrum);
00098 DLL_DECL_SPECTRUM( AlbedoPSpectrum);
00099 DLL_DECL_SPECTRUM( HeSpectrum);
00100 DLL_DECL_SPECTRUM( GalElSpectrum);
00101 DLL_DECL_SPECTRUM( FILESpectrum);
00102 DLL_DECL_SPECTRUM( TimeCandle);
00103
00104 }
00105
00106
00107 FluxMgr::~FluxMgr(){
00108 }
00109
00110 EventSource* FluxMgr::source(std::string name)
00111 {
00112
00113
00114
00115 if( m_sources.find(name)==m_sources.end() ) {
00116
00117 ISpectrum* s = SpectrumFactoryTable::instance()->instantiate(name);
00118
00119 return s? new FluxSource(s) : (EventSource*)0;
00120 }
00121 return getSourceFromXML(m_sources[name].first);
00122 }
00123
00124
00125 EventSource* FluxMgr::getSourceFromXML(const DOM_Element& src)
00126 {
00127
00128
00129
00130 DOM_Node childNode = src.getFirstChild();
00131 if (childNode == DOM_Node()) {
00132
00133
00134
00135
00136
00137 return new FluxSource(src);
00138 }
00139
00140 DOM_Element sname = xml::Dom::getFirstChildElement(src);
00141 if (sname == DOM_Element() ) {
00142 FATAL_MACRO("Improperly formed XML event source");
00143 return 0;
00144 }
00145
00146 if ((sname.getTagName()).equals("spectrum")) {
00147 return new FluxSource(src);
00148 }
00149 else if ((sname.getTagName()).equals("nestedSource")) {
00150
00151 double flux = atof (xml::Dom::getAttribute(src, "flux").c_str());
00152
00153
00154
00155
00156
00157 CompositeSource* cs;
00158 if(flux == 1.0){
00159 cs = new CompositeSource();
00160 }else{ cs = new CompositeDiffuse(flux);
00161 }
00162 do {
00163
00164 DOM_Element selem =
00165 getLibrarySource(sname.getAttribute("sourceRef"));
00166 if (selem == DOM_Element()) {
00167 FATAL_MACRO("source name" <<
00168 xml::Dom::transToChar(sname.getAttribute("sourceRef")) <<
00169 "' not in source library");
00170 }
00171 cs->addSource(getSourceFromXML(selem));
00172 sname = xml::Dom::getSiblingElement(sname);
00173 }
00174 while (sname != DOM_Element() );
00175 return cs;
00176 }
00177 else {
00178 FATAL_MACRO("Unexpected element: "<<
00179 xml::Dom::transToChar(sname.getTagName()) );
00180 }
00181 return 0;
00182 }
00183
00184
00185
00186
00187 DOM_Element FluxMgr::getLibrarySource(const DOMString& id)
00188 {
00189
00190
00191
00192
00193 if (s_library == DOM_Element() ) return DOM_Element();
00194
00195 return m_library_doc.getElementById(id);
00196 }
00197
00198 std::list<std::string> FluxMgr::sourceList() const
00199 {
00200 std::list<std::string> s;
00201 for( std::map<std::string, std::pair<DOM_Element,std::string> >::const_iterator it = m_sources.begin();
00202 it != m_sources.end();
00203 ++it){
00204 s.push_back((*it).first);
00205 }
00206 return s;
00207 }
00208
00209 std::vector<std::pair< std::string ,std::list<std::string> > > FluxMgr::sourceOriginList() const
00210 {
00211 std::vector<std::pair< std::string ,std::list<std::string> > > originList;
00212 for( std::map<std::string, std::pair<DOM_Element,std::string> >::const_iterator it = m_sources.begin();
00213 it != m_sources.end();
00214 ++it){
00215
00216 std::vector<std::pair< std::string ,std::list<std::string> > >::iterator topiter;
00217 for(topiter = originList.begin() ; topiter != originList.end() ; topiter++){
00218 if( (*topiter).first==(*it).second.second) break;
00219 }
00220
00221 if( topiter != originList.end() ){ (*topiter).second.push_back((*it).first);
00222 }else{
00223 std::list<std::string> abc;
00224 abc.push_back((*it).first);
00225 originList.push_back(std::make_pair< std::string ,std::list<std::string> >((*it).second.second,abc) );
00226 }
00227
00228 }
00229
00230 return originList;
00231 }
00232
00234 void FluxMgr::test(std::ostream& cout, std::string source_name, int count)
00235 {
00236 EventSource* e = source(source_name);
00237 setExpansion(1.);
00238 double time=0.;
00239
00240 const int howMany = e->howManySources();
00241
00242 std::vector<int> counts;
00243
00244
00245
00246 int q;
00247 for(q=0 ; q<=howMany+2 ; q++){
00248 counts.push_back(0);
00249
00250 }
00251
00252 cout << "running source: " << e->fullTitle() << std::endl;
00253 cout << " Total rate is: " << e->rate(time) << " Hz into " << e->totalArea() << " m^2" << std::endl;
00254
00255 cout << " there are " << howMany << " Sources total..." << std::endl;
00256 cout << " Generating " << count << " trials " << std::endl;
00257 cout << " --------------------------------" << std::endl;
00258
00259
00260 GPS::instance()->rotateAngles(std::make_pair<double,double>(0.0,0.3));
00261
00262 FluxSource* f;
00263 double totalinterval;
00264 for( int i = 0; i< count; ++i) {
00265
00266 f = e->event(time);
00267
00268
00269
00270
00271
00272 double interval=e->interval(time);
00273
00274
00275
00276
00277 time+=interval;
00278 pass(interval);
00279 int sourceNumber = e->numSource();
00280 if(sourceNumber==-1){counts[0]++;
00281 }else{counts[sourceNumber]++;}
00282
00283 totalinterval+=interval;
00284 cout << "LaunchType = " << f->refLaunch() << " , Pointtype = " << f->refPoint() <<std::endl;
00285 cout << f->spectrum()->particleName();
00286 cout << "(" << f->energy();
00287 cout << " GeV), Launch: " << f->launchPoint()
00288 << " Dir " << f->launchDir() << " ,Flux="
00289 << f->flux(time) << " ,Interval="
00290 << interval << " Event ID number: "<< sourceNumber <<std::endl
00291 << "elapsed time = " << totalinterval << std::endl;
00292 }
00293 cout << "------------------------------------------------------" << std::endl;
00294
00295 cout << std::endl << "Average Interval=" << totalinterval/count <<" , "
00296 << "Average rate = " << count/totalinterval <<std::endl;
00297
00298 cout << "Source Statistics: " << std::endl;
00299 for(q=0 ; q<howMany ; q++){
00300 cout << "source #" << q+1 << ": " << counts[q] << " events counted." << std::endl;
00301
00302 }
00303
00304 delete e;
00305
00306 }
00307
00308
00309 void FluxMgr::addFactory(std::string name, const ISpectrumFactory* factory ) {
00310 SpectrumFactoryTable::instance()->addFactory(name,factory);
00311 }
00312
00313 void FluxMgr::setOrientation(std::pair<double,double> ang){
00314 GPS::instance()->rotateAngles(ang);
00315 }
00316
00317 std::pair<double,double> FluxMgr::getOrientation(){
00318 return GPS::instance()->rotateAngles();
00319 }
00320
00321 void FluxMgr::setGlastPosition(std::pair<double,double> pos){
00322 GPS::instance()->ascendingLon(pos.first);
00323 GPS::instance()->ascendingLon(pos.second);
00324 }
00325
00326 void FluxMgr::setExpansion (double p){
00327
00328 GPS::instance()->expansion(p);
00329 }
00330
00331
00332 void FluxMgr::pass(double t){
00333 GPS::instance()->pass(t);
00334 synch();
00335 }
00336
00337 GPStime FluxMgr::time () const{
00338 return GPS::instance()->time();
00339 }
00340
00341 void FluxMgr::synch(){
00342 GPS::instance()->synch();
00343 }
00344
00345 void sampleintvl ( double t ){
00346 GPS::instance()->sampleintvl(t);
00347 }
00348
00349
00350 std::pair<double,double> FluxMgr::location(){
00351 return std::make_pair<double,double>(GPS::instance()->lat(),GPS::instance()->lon());
00352 }
00353
00354
00355 Rotation FluxMgr::CELTransform(double time){
00356 return GPS::instance()->CELTransform(time);
00357 }
00358
00359
00360 Rotation FluxMgr::orientTransform(double time){
00361 return GPS::instance()->rockingAngleTransform(time);
00362 }
00363
00365 HepRotation FluxMgr::transformGlastToGalactic(double time){
00366 return GPS::instance()->transformGlastToGalactic(time);
00367 }
00368
00370 void FluxMgr::setRockType(GPS::RockType rockType){
00371 GPS::instance()->setRockType(rockType);
00372 }
00373
00375 void FluxMgr::setRockType(int rockType){
00376 GPS::instance()->setRockType(rockType);
00377 }
00378
00379 std::string FluxMgr::writeXmlFile(const std::vector<std::string>& fileList) {
00393 std::strstream fileString;
00394
00395 char libchar = 'a';
00396 std::string inFileName;
00397
00398 std::vector<std::string>::const_iterator iter = fileList.begin();
00399
00400
00401 inFileName=m_dtd;
00402
00403 xml::IFile::extractEnvVar(&inFileName);
00404
00405
00406 fileString << "<?xml version='1.0' ?>" << std::endl << "<!DOCTYPE source_library"
00407 << " SYSTEM " << '"' << inFileName << '"' << " [" << std::endl;
00408
00409
00410 for (;iter != fileList.end(); iter++) {
00411
00412
00413 inFileName=(*iter).c_str();
00414 xml::IFile::extractEnvVar(&inFileName);
00415
00416
00417 fileString << "<!ENTITY " << "library" << libchar << " SYSTEM " << '"'
00418 << inFileName << "\" >" << std::endl;
00419 libchar++;
00420 }
00421
00422 fileString << "]>" << std::endl << "<source_library>" << std::endl;
00423 iter = fileList.begin();
00424 libchar = 'a';
00425
00426
00427 for (;iter != fileList.end(); iter++) {
00428
00429 fileString << "&library" << libchar << ";" << std::endl;
00430 libchar++;
00431 }
00432
00433 fileString << "</source_library>" << '\0';
00434 return fileString.str();
00435
00436 }