00001
00002
00003
00004
00005 #include "GaudiKernel/MsgStream.h"
00006 #include "GaudiKernel/AlgFactory.h"
00007 #include "GaudiKernel/IDataProviderSvc.h"
00008 #include "GaudiKernel/SmartDataPtr.h"
00009 #include "GaudiKernel/IParticlePropertySvc.h"
00010 #include "GaudiKernel/ParticleProperty.h"
00011 #include "GaudiKernel/SmartRefVector.h"
00012
00013
00014 #include "Event/TopLevel/Event.h"
00015 #include "Event/TopLevel/MCEvent.h"
00016 #include "Event/MonteCarlo/McParticle.h"
00017 #include "Event/TopLevel/EventModel.h"
00018
00019
00020 #include "FluxSvc.h"
00021 #include "FluxSvc/IFlux.h"
00022 #include "Spectrum.h"
00023 #include "SpectrumFactory.h"
00024
00025 #include "CLHEP/Vector/LorentzVector.h"
00026
00027 #include <cassert>
00028 #include <vector>
00029
00030 #include "FluxAlg.h"
00031
00032
00033
00034 static const AlgFactory<FluxAlg> Factory;
00035 const IAlgFactory& FluxAlgFactory = Factory;
00036
00037
00039
00040 :Algorithm(name, pSvcLocator) , m_sequence(0)
00041 {
00042
00043 declareProperty("source_name", m_source_name="default");
00044 declareProperty("MCrun", m_run=100);
00045
00046 }
00047
00049
00050 StatusCode sc = StatusCode::SUCCESS;
00051 MsgStream log(msgSvc(), name());
00052 log << MSG::INFO << "initialize" << endreq;
00053
00054
00055 setProperties();
00056
00057 if ( service("FluxSvc", m_fluxSvc).isFailure() ){
00058 log << MSG::ERROR << "Couldn't find the FluxSvc!" << endreq;
00059 return StatusCode::FAILURE;
00060 }
00061
00062 log << MSG::INFO << "loading source..." << endreq;
00063
00064 sc = m_fluxSvc->source(m_source_name, m_flux);
00065 if( sc.isFailure()) {
00066 log << MSG::ERROR << "Could not find flux " << m_source_name << endreq;
00067 return sc;
00068 }
00069 log << MSG::INFO << "Source: "<< m_flux->title() << endreq;
00070
00071 log << MSG::INFO << "Source title: " << m_flux->title() << endreq;
00072 log << MSG::INFO << " area: " << m_flux->targetArea() << endreq;
00073 log << MSG::INFO << " rate: " << m_flux->rate() << endreq;
00074
00075 if ( service("ParticlePropertySvc", m_partSvc).isFailure() ){
00076 log << MSG::ERROR << "Couldn't find the ParticlePropertySvc!" << endreq;
00077 return StatusCode::FAILURE;
00078 }
00079 return sc;
00080 }
00081
00082
00083
00085
00086 {
00087 StatusCode sc = StatusCode::SUCCESS;
00088 MsgStream log( msgSvc(), name() );
00089
00090
00091
00092
00093
00094
00095
00096 if(m_fluxSvc->currentFlux() == m_flux){
00097 m_flux->generate();
00098 }else{
00099 m_flux = m_fluxSvc->currentFlux();
00100 m_flux->generate();
00101 }
00102 HepPoint3D p = m_flux->launchPoint();
00103 HepPoint3D d = m_flux->launchDir();
00104 double ke = m_flux->energy();
00105 std::string particleName = m_flux->particleName();
00106
00107 if(particleName == "TimeTick"){
00108 log << MSG::DEBUG << particleName << " particle found, will exit particle creation/reconstruction loops" << endreq;
00109 particleName = "gamma";
00110 setFilterPassed( false );
00111 }
00112
00113
00114
00115 if( particleName=="p") particleName="proton";
00116 ParticleProperty* prop = m_partSvc->find(particleName);
00117
00118 if( prop==0) {
00119 log << MSG::ERROR << "Particle name " << particleName << " not found by particle properties" << endreq;
00120 return StatusCode::FAILURE;
00121 }
00122
00123 int partID = prop->jetsetID();
00124
00125 log << MSG::DEBUG << particleName
00126 << "(" << m_flux->energy()
00127 << " MeV), Launch: "
00128 << "(" << p.x() <<", "<< p.y() <<", "<<p.z()<<")"
00129 << " mm, Dir "
00130 << "(" << d.x() <<", "<< d.y() <<", "<<d.z()<<")"
00131 << endreq;
00132
00133
00134
00135
00136 Event::MCEvent* mch = 0;
00137
00138 SmartDataPtr<Event::MCEvent> mcheader(eventSvc(), EventModel::MC::Event);
00139 if (mcheader == 0) {
00140 sc=eventSvc()->registerObject(EventModel::MC::Event , mch= new Event::MCEvent);
00141 if(sc.isFailure()) {
00142 log << MSG::WARNING << EventModel::MC::Event <<" could not be registered on data store" << endreq;
00143 return sc;
00144 }
00145
00146 }else mch = mcheader;
00147
00148
00149 mch->initialize(mch->getRunNumber(), m_flux->numSource(), mch->getSequence());
00150
00151
00152
00153 Event::McParticleCol* pcol = new Event::McParticleCol;
00154 sc = eventSvc()->registerObject(EventModel::MC::McParticleCol, pcol);
00155 if( sc.isFailure()) {
00156
00157 log << MSG::ERROR << "Could not Register "<< EventModel::MC::McParticleCol << endreq;
00158
00159 return sc;
00160 }
00161 Event::McParticle * parent= new Event::McParticle;
00162 pcol->push_back(parent);
00163
00164 double mass = prop->mass() ,
00165 energy = (ke+mass),
00166 momentum=sqrt(energy*energy - mass*mass);
00167 HepLorentzVector pin(d*momentum,energy);
00168
00169
00170
00171 parent->initialize(parent, partID,
00172 Event::McParticle::PRIMARY,
00173 pin,p);
00174 parent->finalize(pin, p);
00175
00176
00177 Event::EventHeader* h = 0;
00178
00179 SmartDataPtr<Event::EventHeader> header(eventSvc(), EventModel::EventHeader);
00180 if(0==header) {
00181
00182 sc = eventSvc()->registerObject(EventModel::EventHeader, h=new Event::EventHeader);
00183 if( sc.isFailure()) {
00184 log << MSG::WARNING << " could not find or register the event header" << endreq;
00185 }
00186 } else h = header;
00187
00188 h->setTime(m_flux->time());
00189 return StatusCode::SUCCESS;
00190 }
00191
00192
00194
00195 StatusCode sc = StatusCode::SUCCESS;
00196 MsgStream log(msgSvc(), name());
00197
00198 return sc;
00199 }
00200