00001
00002
00003
00004 #include "LcioPrimaryGenerator.hh"
00005 #include "LcioManager.hh"
00006 #include "LcioMcpManager.hh"
00007 #include "LcioMcpUtil.hh"
00008 #include "StringUtil.hh"
00009
00010
00011 #include <sstream>
00012
00013 using IMPL::MCParticleImpl;
00014 using EVENT::LCCollection;
00015 using EVENT::LCIO;
00016
00017 namespace slic
00018 {
00019
00020 LcioPrimaryGenerator::LcioPrimaryGenerator(LcioManager* mgr)
00021 : Module( "LcioPrimaryGenerator" )
00022 {
00023 m_mgr = mgr;
00024
00025 m_mcpManager = LcioMcpManager::instance();
00026 }
00027
00028 LcioPrimaryGenerator::~LcioPrimaryGenerator()
00029 {}
00030
00031 void LcioPrimaryGenerator::generatePrimaryVertexFromMcpCollection( LCCollection* mcpVec, G4Event* anEvent )
00032 {
00033
00034 #ifdef SLIC_LOG
00035 log() << LOG::debug << LOG::debug << "********** Generating Event from LCIO MCParticles **********" << LOG::done;
00036 #endif
00037
00038 assert( mcpVec );
00039 assert( mcpVec->getTypeName() == LCIO::MCPARTICLE );
00040
00041 G4int nhep = mcpVec->getNumberOfElements();
00042
00043 if ( nhep < 1 ) {
00044 return;
00045 }
00046
00047 #ifdef SLIC_LOG
00048 log() << LOG::debug << "Number of particles in Lcio input Mcp <" << nhep << ">." << LOG::done;
00049 #endif
00050
00051 for ( int i=0; i < nhep; i++ ) {
00052
00053 #ifdef SLIC_LOG
00054 log() << LOG::debug << "processing particle # <" << i << ">" << LOG::done;
00055 #endif
00056
00057 MCParticleImpl* mcp = dynamic_cast<MCParticleImpl*>
00058 (mcpVec->getElementAt( i ) );
00059
00060 assert( mcp );
00061
00062 #ifdef SLIC_LOG
00063 log() << LOG::debug << "pdg <" << mcp->getPDG() << ">" << LOG::done;
00064 #endif
00065
00066 G4int numPar = mcp->getParents().size();
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 MCParticleImpl* parMcp = 0;
00077 G4PrimaryParticle* g4parent = 0;
00078
00079 G4PrimaryParticle* thePrimary = 0;
00080 G4PrimaryVertex* theVertex = 0;
00081
00082 G4bool isPreDecay = false;
00083 G4bool createPrimary = false;
00084 G4bool createVertex = false;
00085
00086
00087 if ( numPar > 0 ) {
00088 parMcp = dynamic_cast<MCParticleImpl*>(mcp->getParents()[0]);
00089 g4parent = m_mcpManager->getMaps()->findPrimaryFromMcp( parMcp );
00090
00091 if ( g4parent != 0 ) {
00092
00093 #ifdef SLIC_LOG
00094 log() << LOG::debug << "primary is preDecay" << LOG::done;
00095 std::istringstream ss;
00096 log() << LOG::debug << "g4parent <" << g4parent << ">" << LOG::done;
00097 #endif
00098
00099 isPreDecay = true;
00100 }
00101 }
00102
00103
00104 if ( isPreDecay ) {
00105
00106 #ifdef SLIC_LOG
00107 log() << LOG::debug << "case 1: predecay" << LOG::done;
00108 #endif
00109
00110 createPrimary = true;
00111
00112 }
00113 else {
00114
00115 if ( mcp->getGeneratorStatus() == 1 ) {
00116
00117 #ifdef SLIC_LOG
00118 log() << LOG::debug << "case 2: final state" << LOG::done;
00119 #endif
00120
00121 createPrimary = true;
00122 createVertex = true;
00123 }
00124 else {
00125 G4double dist = 0;
00126
00127
00128 if ( mcp->getDaughters().size() > 0 ) {
00129
00130 IMPL::MCParticleImpl* firstDau = dynamic_cast<IMPL::MCParticleImpl*>
00131 ( mcp->getDaughters()[0] );
00132
00133 dist = LcioMcpUtil::computeMcpDistance( mcp, firstDau );
00134
00135 #ifdef SLIC_LOG
00136 log() << LOG::debug << "mcpDistance=" << dist << LOG::done;
00137 log() << LOG::debug << "minTrackingDist=" << m_mcpManager->getMinimumTrackingDistance() << LOG::done;
00138 #endif
00139
00140
00141 if ( dist > m_mcpManager->getMinimumTrackingDistance() ) {
00142
00143 #ifdef SLIC_LOG
00144 log() << LOG::debug << "case 3: intermediate or doc TO BE TRACKED" << LOG::done;
00145 #endif
00146
00147 createPrimary = true;
00148 createVertex = true;
00149 }
00150
00151
00152 #ifdef SLIC_LOG
00153 else {
00154 log() << LOG::debug << "case 4: intermediate or doc WILL NOT BE TRACKED" << LOG::done;
00155 }
00156 #endif
00157 }
00158
00159 }
00160 }
00161
00162
00163 if ( createPrimary ) {
00164
00165 thePrimary = createPrimaryParticleFromMcp( mcp );
00166
00167
00168 if ( isPreDecay ) {
00169
00170 assert( g4parent );
00171
00172
00173 G4ThreeVector parMom = g4parent->GetMomentum();
00174 G4double E = sqrt(pow(g4parent->GetMass(), 2) + pow( parMom.x(), 2 ) + pow( parMom.y(), 2) + pow( parMom.z(), 2 ) );
00175 G4double proper_time = ( ( mcp->getTime() - parMcp->getTime() ) * g4parent->GetMass() ) / E;
00176
00177 #ifdef SLIC_LOG
00178 log() << LOG::debug << "parMom " << parMom << LOG::done;
00179 log() << LOG::debug << "e <" << E << ">" << LOG::done;
00180 log() << LOG::debug << "proper_time <" << proper_time << ">" << LOG::done;
00181 #endif
00182
00183 g4parent->SetDaughter( thePrimary );
00184 g4parent->SetProperTime( proper_time );
00185
00186 #ifdef SLIC_LOG
00187 log() << LOG::debug << "mcp decay time <" << mcp->getTime() - parMcp->getTime() << ">" << LOG::done;
00188 #endif
00189 }
00190 }
00191
00192
00193 if ( createVertex ) {
00194 theVertex = createPrimaryVertexFromMcp( mcp );
00195 theVertex->SetPrimary( thePrimary );
00196 anEvent->AddPrimaryVertex( theVertex );
00197 }
00198
00199
00200 if ( thePrimary ) {
00201
00202 #ifdef SLIC_LOG
00203 log() << LOG::debug << "adding mcp <" << mcp << "> to primary <" << thePrimary << "> link" << LOG::done;
00204 #endif
00205 m_mcpManager->getMaps()->addMcpToPrimaryLink(mcp, thePrimary );
00206 }
00207
00208 #ifdef SLIC_LOG
00209 log() << LOG::debug << LOG::endl;
00210 #endif
00211 }
00212 }
00213
00214 G4PrimaryParticle* LcioPrimaryGenerator::createPrimaryParticleFromMcp(IMPL::MCParticleImpl* mcp)
00215 {
00216 assert( mcp );
00217
00218 G4PrimaryParticle* primary = new G4PrimaryParticle(mcp->getPDG(),
00219 mcp->getMomentum()[0] * GeV,
00220 mcp->getMomentum()[1] * GeV,
00221 mcp->getMomentum()[2] * GeV
00222 );
00223 primary->SetMass( mcp->getMass() * GeV );
00224
00225 return primary;
00226 }
00227
00228 G4PrimaryVertex* LcioPrimaryGenerator::createPrimaryVertexFromMcp(IMPL::MCParticleImpl* mcp)
00229 {
00230 G4ThreeVector pos = G4ThreeVector( mcp->getVertex()[0],
00231 mcp->getVertex()[1],
00232 mcp->getVertex()[2]);
00233
00234 return new G4PrimaryVertex(pos,
00235 mcp->getTime() );
00236
00237 }
00238 }