21#include "G4TrackingManager.hh"
23#include "G4VProcess.hh"
26#include "HepMC/GenEvent.h"
28#include "GaudiKernel/ISvcLocator.h"
29#include "GaudiKernel/Bootstrap.h"
30#include "GaudiKernel/MsgStream.h"
31#include "GaudiKernel/IMessageSvc.h"
32#include "GaudiKernel/IDataProviderSvc.h"
35#include "GaudiKernel/SmartDataPtr.h"
61 (*iter)->BeginOfTruthEvent(evt);
69 for( G4int i=0; i<1; i++ )
71 G4PrimaryVertex* primaryVertex = anEvent->GetPrimaryVertex(i);
72 m_pos0 = primaryVertex->GetPosition();
73 m_t0 = primaryVertex->GetT0();
82 for(HepMC::GenEvent::vertex_const_iterator vitr= hepmcevt->vertices_begin();
83 vitr != hepmcevt->vertices_end(); ++vitr ) {
84 for (HepMC::GenVertex::particle_iterator
85 pitr= (*vitr)->particles_begin(HepMC::children);
86 pitr != (*vitr)->particles_end(HepMC::children); ++pitr) {
87 if((*pitr)->end_vertex())
95 G4cout<<G4endl<<
"generator is GENBES!"<<G4endl;
97 G4cout<<G4endl<<
"generator is not GENBES!"<<G4endl;
104 IDataProviderSvc* p_evtSvc=0;
105 ISvcLocator* svcLocator = Gaudi::svcLocator();
106 StatusCode sc=svcLocator->service(
"EventDataSvc", p_evtSvc);
108 std::cout<<
"BesHepMCInterface could not access EventDataSvc!!"<<std::endl;
110 SmartDataPtr<McGenEventCol> mcCollptr( p_evtSvc,
"/Event/Gen");
112 if ( mcCollptr == 0 )
113 std::cout <<
"no McGenEventCollection found." << std::endl;
117 McGenEventCol::const_iterator it = mcCollptr->begin();
123 for(HepMC::GenEvent::vertex_const_iterator vitr=
m_hepmcevt->vertices_begin();
126 G4int barcodeVtx = (*vitr)->barcode();
128 G4cout<<G4endl<<
"barcodeVtx:"<<barcodeVtx<<
" ";
130 G4LorentzVector xvtx((*vitr)->position().x(),(*vitr)->position().y(),(*vitr)->position().z(),(*vitr)->position().t());
131 G4ThreeVector
v(xvtx.x(), xvtx.y(), xvtx.z());
134 newVertex->
SetTime(xvtx.t()*mm/c_light);
136 G4cout<<
"xyzt:"<<xvtx.x()<<
" "<<xvtx.y()<<
" "
137 <<xvtx.z()<<
" "<<xvtx.t()*mm/c_light<<
" ";
140 G4int parentTrackIndex= -99;
141 for(
int i=0;i<nTrack;i++)
143 parentTrack = (*m_trackList)[i];
147 parentTrackIndex = parentTrack->
GetIndex();
149 G4cout<<
"parentTrack:"<<parentTrackIndex<<
" ";
156 G4int pOut = (*vitr)->particles_out_size();
157 HepMC::GenVertex::particle_iterator pitr = (*vitr)->particles_begin(HepMC::children);
158 G4int pdgcode= (*pitr)-> pdg_id();
159 if(pOut == 1 && pdgcode == -11 && typeFlag==1)
169 for (HepMC::GenVertex::particle_iterator pitr= (*vitr)->particles_begin(HepMC::children); pitr != (*vitr)->particles_end(HepMC::children); ++pitr)
171 G4LorentzVector p((*pitr)->momentum().x(), (*pitr)->momentum().y(),(*pitr)->momentum().z(),(*pitr)->momentum().t());
172 G4LorentzVector pGeV(p.x()*GeV,p.y()*GeV,p.z()*GeV,p.t()*GeV);
173 G4int pdgcode = (*pitr)->pdg_id();
176 newTrack->
SetP4(pGeV);
180 G4cout<<
"pdg:"<<pdgcode<<
" ";
181 G4cout<<
"p:"<<p.x()<<
" "<<p.y()<<
" "<<p.z()<<
" "<<p.t()<<
" ";
182 G4double
mass = sqrt(p.t()*p.t()-p.x()*p.x()-p.y()*p.y()-p.z()*p.z());
191 G4int barcodeEndVtx=0;
192 if((*pitr)->end_vertex())
194 barcodeEndVtx = (*pitr)->end_vertex()->barcode();
196 G4cout<<
"endVtx:"<<barcodeEndVtx<<
" ";
206 if(parentTrackIndex>=0)
209 G4cout<<
"mother:"<<parentTrackIndex;
210 (*m_trackList)[parentTrackIndex]->AddDaughterIndex(
m_trackList->size()-1);
229 (*iter)->EndOfTruthEvent(evt);
275 (*iter)->BeginOfTrack( track );
281 if(
chain.back().savedByDefault==
true)
284 G4int endVtxFlag = 0;
295 G4StepPoint *finalStep = track->GetStep()->GetPostStepPoint();
296 G4StepStatus stepStatus = finalStep->GetStepStatus();
297 if (track->GetNextVolume() != 0 ||
298 (stepStatus != fGeomBoundary && stepStatus != fWorldBoundary) )
301 G4cout<<
"Particle died. make a terminal vertex: ";
303 const G4ThreeVector pos(finalStep->GetPosition());
306 G4cout<<vindex<<G4endl;
307 stat.vertices.push_back(vindex);
310 newVertex->
SetTime( finalStep->GetGlobalTime());
322 G4TrackVector* trackVector = trackingManager->GimmeSecondaries();
323 G4int nSecon = trackVector->size();
328 for(G4int i=0;i<nSecon;i++)
330 seconTrack = (*trackVector)[i];
331 G4String processName = seconTrack->GetCreatorProcess()->GetProcessName();
332 if(processName ==
"Decay")
356 (*iter)->EndOfTrack( track );
366 G4cout<<
"updateVertex:"<<terminalIndex<<
" ";
368 G4StepPoint *finalStep = track->GetStep()->GetPostStepPoint();
369 const G4ThreeVector pos(finalStep->GetPosition());
370 G4double
time = finalStep->GetGlobalTime();
372 G4cout<<
"newPos:"<<pos<<
" "<<
"newTime:"<<
time<<G4endl;
383 if (stat.originVertex < 0 &&
chain.size() > 0)
386 G4cout<<
"MakeNewTrack for decayed particles,";
390 G4int parentVstat =
chain.back().vertices.size();
391 while( --parentVstat >= 0)
393 G4int vindex =
chain.back().vertices[parentVstat];
395 G4ThreeVector diff((*
m_vertexList)[vindex]->GetPosition());
396 diff = diff - track->GetPosition();
397 if (diff.mag2() < 1E-9)
399 stat.originVertex = vindex;
401 G4cout<<
"find a vertex:"<<vindex;
402 (*m_vertexList)[vindex]->AddCurrentDau();
409 if(stat.originVertex < 0 &&
chain.size() == 0)
413 for(G4int i=0;i<nVertex;i++)
416 diff = diff -track->GetPosition();
417 if(diff.mag2() < 1E-9)
419 stat.originVertex = i;
421 G4cout<<
"MakeNewTrack for primary particles,find a vertex:"
428 if (stat.originVertex < 0)
431 G4cout<<
"MakeNewTrack for primary particles,make new vertex:"
435 const G4ThreeVector
v(track->GetPosition());
441 newVertex->
SetTime( track->GetGlobalTime());
452 newVertex->
SetIndex( stat.originVertex );
453 if(track->GetCreatorProcess())
454 newVertex->
SetProcessName(track->GetCreatorProcess()->GetProcessName());
465 newTrack->
SetP4( stat.p4 );
466 newTrack->
SetPDGCode(track->GetDefinition()->GetPDGEncoding());
467 newTrack->
SetPDGCharge(track->GetDefinition()->GetPDGCharge());
473 if(track->GetParentID()==0)
481 newTrack->
SetIndex( stat.trackIndex );
494 G4cout<<
"add this daughter to:"<<parent->
GetIndex()<<G4endl;
509 HepLorentzVector( track->GetMomentum(), track->GetTotalEnergy() ),
510 track->GetGlobalTime());
513 G4int parentId = track->GetParentID();
514 G4int pdg = track->GetDefinition()->GetPDGEncoding();
515 G4String particleName = track->GetDefinition()->GetParticleName();
516 G4ThreeVector trackPos = track->GetPosition();
517 G4double diffT =
m_t0-track->GetGlobalTime();
518 G4double diffPos = (
m_pos0-trackPos).mag2();
525 G4cout<<
"trackId:"<<track->GetTrackID()<<
" ";
526 G4cout<<
"parentId:"<<parentId<<
" ";
527 G4cout<<
"pdg:"<<pdg<<
" ";
528 G4cout<<
"name:"<<particleName<<
" ";
529 G4cout<<
"timeDiff:"<<diffT<<
" ";
530 G4cout<<
"posDiff:"<<diffPos<<G4endl;
536 stat.savedByDefault =
true;
552 if (
chain.back().G4index == parentId)
break;
558 if (
chain.back().savedByDefault)
564 G4String processName = track->GetCreatorProcess()->GetProcessName();
565 if (processName==
"Decay")
570 G4cout<<
"trackId: "<<track->GetTrackID()<<
" ";
571 G4cout<<
"parentId: "<<parentId<<
" ";
572 G4cout<<
"pdg: "<<pdg<<
" ";
573 G4cout<<
"pname: "<<particleName<<G4endl;
575 stat.savedByDefault =
true;
586 stat.savedByDefault =
false;
593 G4int trackID = track->GetTrackID();
594 G4int parentID = track->GetParentID();
595 G4int pdgcode = track->GetDefinition()->GetPDGEncoding();
596 G4ThreeVector
p3 = track->GetMomentum();
598 G4cout<<
"CheckDecayTrack p3: "<<
p3<<
" "<<track->GetTotalEnergy()<<G4endl;
604 G4int parentTrackIndex=-99;
605 G4int terminalVertexIndex=-99;
607 for(
int i=0;i<nTrack;i++)
609 truthTrack = (*m_trackList)[i];
613 parentTrackIndex = truthTrack->
GetIndex();
620 G4cout<<
"parentTrackIndex:"<<parentTrackIndex<<
" "
621 <<
"parent terminate at vertex: "<<terminalVertexIndex<<G4endl;
622 if(parentTrackIndex != i)
623 G4cout<<
"i: "<<i<<std::endl;
629 if(parentTrackIndex==-99 || terminalVertexIndex==-99)
631 G4cout<<
"MatchDecayError!"<<G4endl;
644 G4double minDiffP=1e99;
695 std::cout<<
"chain.back().vertices.size(): "<<
chain.back().vertices.size()<<std::endl;
698 if(
chain.back().vertices.size()<1)
701 std::cout<<
"trackList size: "<<
m_trackList->size()<<std::endl;
702 std::vector<int>* vDau =
new std::vector<int>;
704 G4int sizev = vDau->size();
707 for(
int i=0;i<nTrack;i++)
709 truthTrack = (*m_trackList)[i];
712 if(pdgT==-22) pdgT = 22;
718 HepLorentzVector tp4 = truthTrack->
GetP4();
719 G4ThreeVector tp3(tp4.x(), tp4.y(), tp4.z());
720 G4double diffP = (
p3-tp3).mag2();
723 G4cout<<
"index: "<<truthTrack->
GetIndex()<<
"tp3: "<<tp3<<G4endl;
724 G4cout<<
"diffP: "<<diffP<<G4endl;
728 minDiffP = diffP; truthI = i;
730 G4cout<<
"truthI: "<<i<<G4endl;
745 G4cout<<
"MatchDecayedTrackWithTrueMother"<<G4endl;
746 G4cout<<
"MatchWithTrueMother m_trackIndex: "<<
m_trackIndex<<G4endl;
748 G4cout<<
"large minDiffP: "<<minDiffP<<G4endl;
750 truthTrack=(*m_trackList)[truthI];
753 truthTrack->
GetP4().setX(
p3.x());
754 truthTrack->
GetP4().setY(
p3.y());
755 truthTrack->
GetP4().setZ(
p3.z());
756 truthTrack->
GetP4().setE(E);
757 HepLorentzVector
p4 = truthTrack->
GetP4();
760 G4cout<<
"primary p: "<<
p4.x()<<
" "<<
p4.y()<<
" "<<
p4.z()<<
" "<<
p4.m()<<G4endl;
762 truthTrack->
SetPDGCharge(track->GetDefinition()->GetPDGCharge());
763 truthTrack->
SetParticleName(track->GetDefinition()->GetParticleName());
781 for(G4int dau=minDau;dau<=maxDau;dau++)
783 if(dau<m_trackList->size())
798 G4int size = vDau->size();
801 for(G4int i=0;i<size;i++)
803 if(vIndex==(*vDau)[i])
862 G4cout<<
"UpdatePrimaryTrack! ";
863 G4cout<<
"position: "<<track->GetPosition()<<G4endl;
864 G4cout<<
"time: "<<track->GetGlobalTime()<<G4endl;
865 G4cout<<
"momentum: "<<track->GetMomentum()<<
" "<<track->GetTotalEnergy()<<G4endl;
867 G4int pdgcode = track->GetDefinition()->GetPDGEncoding();
868 G4ThreeVector p = track->GetMomentum();
873 G4double minDiffP=1e99;
874 for(
int i=0;i<nTrack;i++)
876 truthTrack = (*m_trackList)[i];
877 HepLorentzVector tp4 = truthTrack->
GetP4();
878 G4ThreeVector tp3(tp4.x(), tp4.y(), tp4.z());
879 G4double diffP=(p-tp3).mag2();
881 if(pdgT==-22) pdgT=22;
882 if(pdgcode == pdgT && diffP<minDiffP)
884 if(pdgcode == pdgT && diffP < 1E-9 &&
890 truthTrack->
SetPDGCharge(track->GetDefinition()->GetPDGCharge());
891 truthTrack->
SetParticleName(track->GetDefinition()->GetParticleName());
895 G4double E = sqrt(
mass*
mass+p.x()*p.x()+p.y()*p.y()+p.z()*p.z());
896 truthTrack->
GetP4().setX(p.x());
897 truthTrack->
GetP4().setY(p.y());
898 truthTrack->
GetP4().setZ(p.z());
899 truthTrack->
GetP4().setT(E);
902 HepLorentzVector
p4 = truthTrack->
GetP4();
903 G4cout<<
"primary p: "<<
p4.x()<<
" "<<
p4.y()<<
" "<<
p4.z()<<
" "<<
p4.m()<<G4endl;
911 G4cout<<
"daughters: "<<minDau<<
" "<<maxDau<<G4endl;
923 G4cout<<
"MatchError! parentID=0, but match no track in trackList"<<G4endl;
924 G4cout<<
"pdgcode: "<<pdgcode<<
" min p diff: "<<minDiffP<<G4endl;
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
void EndOfTruthEvent(const G4Event *)
BesTStats FollowTrack(const G4Track *track)
std::vector< BesSensitiveDetector * >::iterator iter_end
void UpdatePrimaryTrack(const G4Track *)
HepMC::GenEvent * m_hepmcevt
void SetVertex0(const G4Event *)
static BesSensitiveManager * m_sensitiveManager
G4bool MatchDaughterTrack(const G4Track *)
std::vector< BesSensitiveDetector * > clients
void MakeNewTrack(BesTStats &stat, const G4Track *track)
void BeginOfTrack(const G4Track *track)
void UpdateVertex(BesTStats, const G4Track *)
std::vector< BesTStats > chain
std::vector< BesTruthTrack * > * m_trackList
void EndOfTrack(const G4Track *track, G4TrackingManager *)
void BeginOfTruthEvent(const G4Event *)
G4bool MatchVertex(G4int vIndex, std::vector< int > *vDau)
std::vector< BesTruthVertex * > * m_vertexList
void GetDaughterVertexes(BesTruthTrack *pTrack, std::vector< int > *vDau)
std::vector< BesSensitiveDetector * >::iterator iter
G4bool CheckDecayTrack(const G4Track *)
void SaveParticlesFromGenerator()
G4int CheckType(const HepMC::GenEvent *hepmcevt)
void SetSource(G4String source)
HepLorentzVector GetP4() const
BesTruthVertex * GetTerminalVertex() const
BesTruthVertex * GetVertex() const
void SetPDGCode(G4int code)
void SetTerminalVertex(BesTruthVertex *vertex)
void SetIndex(G4int index)
G4int GetG4TrackId() const
void SetVertex(BesTruthVertex *vertex)
void SetBarcodeEndVtx(G4int vtx)
void SetParticleName(G4String name)
vector< int > GetDaughterIndexes() const
void SetP4(const HepLorentzVector &p4)
void SetG4TrackId(G4int trackId)
void SetPDGCharge(G4double charge)
void AddDaughterIndex(G4int index)
void SetPosition(const G4ThreeVector &p)
BesTruthTrack * GetParentTrack() const
void SetParentTrack(BesTruthTrack *newParent)
void SetTerminal(bool wasTerminal)
void SetTime(const G4double &t)
void SetIndex(signed long newIndex)
void SetMinDau(G4int dau)
G4int GetCurrentDau() const
void SetProcessName(const G4String name)
GenEvent * getGenEvt() const
double double double * p4