00001 #include "SimG4Core/Notification/interface/TrackWithHistory.h"
00002 #include "SimG4Core/Notification/interface/G4TrackToParticleID.h"
00003 #include "SimG4Core/Notification/interface/TrackInformationExtractor.h"
00004 #include "SimG4Core/Notification/interface/SimG4Exception.h"
00005 #include "SimG4Core/Notification/interface/GenParticleInfoExtractor.h"
00006
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008
00009 #include "G4VProcess.hh"
00010
00011 #include <iostream>
00012
00013 using std::cout;
00014 using std::endl;
00015
00016
00017
00018 G4TrackToParticleID * TrackWithHistory::theG4TrackToParticleID(0);
00019
00020 TrackWithHistory::TrackWithHistory(const G4Track * g4trk)
00021 : trackID_(0),particleID_(0),parentID_(0),momentum_(math::XYZVectorD(0.,0.,0.)),totalEnergy_(0),
00022 vertexPosition_(math::XYZVectorD(0.,0.,0.)),globalTime_(0),localTime_(0),properTime_(0),
00023 creatorProcess_(0),weight_(0),storeTrack_(false),saved_(false)
00024 {
00025 if (theG4TrackToParticleID == 0) theG4TrackToParticleID = new G4TrackToParticleID;
00026 if (g4trk!=0)
00027 {
00028 TrackInformationExtractor extractor;
00029 trackID_ = g4trk->GetTrackID();
00030 particleID_ = theG4TrackToParticleID->particleID(g4trk);
00031 parentID_ = g4trk->GetParentID();
00032 momentum_ = math::XYZVectorD(g4trk->GetMomentum().x(),g4trk->GetMomentum().y(),g4trk->GetMomentum().z());
00033 totalEnergy_ = g4trk->GetTotalEnergy();
00034 vertexPosition_ = math::XYZVectorD(g4trk->GetPosition().x(),g4trk->GetPosition().y(),g4trk->GetPosition().z());
00035 globalTime_ = g4trk->GetGlobalTime();
00036 localTime_ = g4trk->GetLocalTime();
00037 properTime_ = g4trk->GetProperTime();
00038 creatorProcess_ = g4trk->GetCreatorProcess();
00039 weight_ = g4trk->GetWeight();
00040 storeTrack_ = extractor(g4trk).storeTrack();
00041 saved_ = false;
00042 genParticleID_ = extractGenID( g4trk);
00043 #ifdef DEBUG
00044 LogDebug("TrackWithHistory") << " TrackWithHistory : created history for " << trackID_
00045 << " with mother " << parentID_;
00046 #endif
00047 }
00048 }
00049
00050 void TrackWithHistory::checkAtEnd(const G4Track * gt)
00051 {
00052
00053 math::XYZVectorD vposdir(gt->GetVertexPosition().x(),gt->GetVertexPosition().y(),gt->GetVertexPosition().z());
00054 math::XYZVectorD vmomdir(gt->GetVertexMomentumDirection().x(),gt->GetVertexMomentumDirection().y(),gt->GetVertexMomentumDirection().z());
00055 bool ok = true;
00056 double epsilon = 1.e-6;
00057 double eps2 = epsilon*epsilon;
00058 if ((vertexPosition_-vposdir).Mag2() > eps2)
00059 {
00060 cout << "TrackWithHistory vertex position check failed" << endl;
00061 cout << "At construction: " << vertexPosition_ << endl;
00062 cout << "At end: " << vposdir << endl;
00063 ok = false;
00064 }
00065 math::XYZVectorD dirDiff = momentum_.Unit() - vmomdir;
00066 if (dirDiff.Mag2() > eps2)
00067 {
00068 cout << "TrackWithHistory momentum direction check failed" << endl;
00069 cout << "At construction: " << momentum_.Unit() << endl;
00070 cout << "At end: " << vmomdir << endl;
00071 ok = false;
00072 }
00073 if (!ok) throw SimG4Exception("TrackWithHistory::checkAtEnd failed");
00074 }
00075
00076 int TrackWithHistory::extractGenID(const G4Track* gt) const
00077 {
00078 void * vgprimary = gt->GetDynamicParticle()->GetPrimaryParticle();
00079 if (vgprimary == 0) return -1;
00080
00081 G4PrimaryParticle* gprimary = (G4PrimaryParticle*) vgprimary;
00082 GenParticleInfoExtractor ext;
00083 return ext(gprimary).id();
00084 }