00001
00002
00003 #ifdef HAVE_CONFIG_H
00004 #include "config.h"
00005 #endif
00006
00007 #include "../flux/CompositeSource.h"
00008
00009 #include "dom/DOM_Element.hpp"
00010 #include "facilities/Scheduler.h"
00011 #include "facilities/SimpleEvent.h"
00012 #include "CLHEP/Random/RandFlat.h"
00013
00014
00015
00016
00017 #include <strstream>
00018 #include <cassert>
00019 #include <numeric>
00020 #include <functional>
00021 #include <iomanip>
00022
00023 CompositeSource::CompositeSource (double aRate)
00024 : EventSource(aRate), m_recent(0),m_numofiters(0)
00025 {
00026 }
00027
00028 CompositeSource::~CompositeSource()
00029 {
00030 for (std::vector<EventSource*>::iterator it = m_sourceList.begin();
00031 it != m_sourceList.end(); ++it ) delete (*it);
00032 }
00033
00034
00035 void CompositeSource::addSource (EventSource* aSource)
00036 {
00037 m_sourceList.push_back(aSource);
00038 EventSource::flux( flux() );
00039 }
00040
00041 void CompositeSource::rmvSource (EventSource* aSource)
00042 {
00043 std::vector<EventSource*>::iterator it = m_sourceList.begin();
00044 for (;it != m_sourceList.end(); ++it) {
00045 if ((*it) == aSource) break;
00046 }
00047 if (it != m_sourceList.end()) {
00048 m_sourceList.erase(it);
00049 EventSource::flux( flux() );
00050 }
00051 }
00052
00053 FluxSource* CompositeSource::event ()
00054 {
00055 m_numofiters=0;
00056 double mr = rate();
00057
00058 if( m_sourceList.size()==1 || mr ==0) {
00059 m_recent = m_sourceList.front();
00060 }else {
00061
00062
00063 double x = RandFlat::shoot(mr), y = 0;
00064 std::vector<EventSource*>::iterator it = m_sourceList.begin();
00065 for (; it != m_sourceList.end(); ++it) {
00066 y += fabs((*it)->rate());
00067 if (x <= y) {
00068 m_recent = (*it);
00069 break;
00070 }
00071 m_numofiters++;
00072 }
00073 }
00074
00075 return m_recent->event();
00076 }
00077
00078 std::string CompositeSource::fullTitle () const
00079 {
00080 std::strstream s;
00081 std::vector<EventSource*>::const_iterator it = m_sourceList.begin();
00082
00083 while (it != m_sourceList.end()) {
00084
00085 s << (*it)->fullTitle() << " ";
00086 ++it;
00087 if (it != m_sourceList.end()) s << "+ ";
00088 }
00089 s << '\0';
00090 std::string t(s.str());
00091 s.freeze(false);
00092 return t;
00093 }
00094
00095 std::string CompositeSource::displayTitle () const
00096 {
00097 return (m_recent == 0) ? "" : m_recent->displayTitle();
00098 }
00099
00100 double CompositeSource::rate() const
00101 {
00102 std::vector<EventSource*>::const_iterator it = m_sourceList.begin();
00103 double total_rate = 0.;
00104 for(;it != m_sourceList.end();++it) {
00105 double rr = fabs((*it)->rate());
00106 total_rate += rr;
00107 }
00108 return total_rate;
00109 }
00110
00111 void CompositeSource::rate ( double value )
00112 {
00113 double f = rate();
00114 if (f == 0.) return;
00115
00116 std::vector<float> fvec;
00117 std::vector<EventSource*>::iterator it = m_sourceList.begin();
00118
00119 while (it != m_sourceList.end()) {
00120
00121 (*it)->rate( value * (*it)->rate()/f );
00122 ++it;
00123 }
00124 EventSource::rate( value );
00125 }
00126
00127
00128 void CompositeSource::setupXML (const DOM_Element&) {}
00129
00130 void CompositeSource::printOn(std::ostream& out)const
00131 {
00132 out << "Source(s), total rate="<< rate() << std::endl;
00133
00134 for( std::vector<EventSource*>::const_iterator it = m_sourceList.begin();
00135 it != m_sourceList.end();++it) {
00136 out << std::setw(8) << std::setprecision(4) << (*it)->rate() <<" Hz, "
00137 << '#' << std::setw(6) << (*it)->eventNumber() <<' '
00138 << (*it)->name() << ' '<< (*it)->fullTitle() << std::endl;
00139
00140 }
00141
00142 }
00143
00144 std::string CompositeSource::findSource()const
00145 {
00146 return m_recent->fullTitle();
00147 }
00148
00150 int CompositeSource::numSource()const
00151 {
00152 return m_numofiters;
00153 }
00154