17 #include <Geant4/G4Event.hh>
18 #include <Geant4/G4TrajectoryContainer.hh>
19 #include <Geant4/G4VTrajectory.hh>
20 #include <Geant4/G4VTrajectoryPoint.hh>
21 #include <Geant4/G4ParticleTable.hh>
22 #include <Geant4/G4ParticleDefinition.hh>
23 #include <Geant4/globals.hh>
27 #include <Eigen/Dense>
34 prev_existing_lower_key( 0 ),
35 prev_existing_upper_key( 0 )
42 if ( !truthInfoList_ ) {
43 std::cout <<
"PHG4TruthEventAction::EndOfEventAction - unable to find G4TruthInfo node" << std::endl;
49 prev_existing_lower_key = map.begin()->first;
50 prev_existing_upper_key = map.rbegin()->first;
58 if ( !truthInfoList_ ) {
59 std::cout <<
"PHG4TruthEventAction::EndOfEventAction - unable to find G4TruthInfo node" << std::endl;
66 std::set<G4int> savelist;
67 std::set<int> savevtxlist;
69 for (std::set<G4int>::const_iterator write_iter = writeList_.begin();
70 write_iter != writeList_.end();
73 std::vector<G4int> wrttracks;
74 std::vector<int> wrtvtx;
77 G4int mytrkid = *write_iter;
81 if (savelist.find(mytrkid) != savelist.end()) {
84 wrttracks.push_back(mytrkid);
91 while (savelist.find(parentid) == savelist.end() && parentid != 0) {
93 wrttracks.push_back(parentid);
100 while (wrttracks.begin() != wrttracks.end()) {
101 savelist.insert(wrttracks.back());
102 wrttracks.pop_back();
105 while (wrtvtx.begin() != wrtvtx.end()) {
106 savevtxlist.insert(wrtvtx.back());
117 int removed[4] = {0};
120 while (truthiter != truth_range.second) {
123 int trackid = truthiter->first;
124 if (savelist.find(trackid) == savelist.end()) {
133 if (((trackid < prev_existing_lower_key)||(trackid > prev_existing_upper_key)) && ((truthiter->second)->get_parent_id() != 0)) {
139 savevtxlist.insert((truthiter->second)->get_vtx_id());
151 while (vtxiter != vtxrange.second)
154 if (savevtxlist.find(vtxiter->first) == savevtxlist.end())
167 G4PrimaryVertex *pvtx = evt->GetPrimaryVertex();
170 G4PrimaryParticle *part = pvtx->GetPrimary();
180 part = part->GetNext();
182 pvtx = pvtx->GetNext();
193 writeList_.insert(trackid);
200 truthInfoList_ = findNode::getClass<PHG4TruthInfoContainer>( topNode ,
"G4TruthInfo" );
203 if ( !truthInfoList_ ) {
204 std::cout <<
"PHG4TruthEventAction::SetInterfacePointers - unable to find G4TruthInfo" << std::endl;
219 while ((thisNode = iter())) {
221 if (thisNode->
getType() ==
"PHCompositeNode") {
223 }
else if (thisNode->
getType() ==
"PHIODataNode") {
224 if (thisNode->
getName().find(
"G4HIT_") == 0) {
229 hitmap_[
object->GetID()] = object;
242 void PHG4TruthEventAction::PruneShowers() {
246 iter != range.second;
250 std::set<int> remove_ids;
254 int g4particle_id = *jter;
257 remove_ids.insert(g4particle_id);
262 for (std::set<int>::iterator jter = remove_ids.begin();
263 jter != remove_ids.end();
268 std::set<int> remove_more_ids;
269 for (std::map<
int,std::set<PHG4HitDefs::keytype> >::iterator jter = shower->
begin_g4hit_id();
272 int g4hitmap_id = jter->first;
273 std::map<int,PHG4HitContainer*>::iterator mapiter = hitmap_.find(g4hitmap_id);
274 if (mapiter == hitmap_.end()) {
279 for (std::set<PHG4HitDefs::keytype>::iterator kter = jter->second.begin();
280 kter != jter->second.end();
284 PHG4Hit* g4hit = mapiter->second->findHit(g4hit_id);
287 jter->second.erase(kter++);
294 if (jter->second.empty()) {
295 remove_more_ids.insert(g4hitmap_id);
299 for (std::set<int>::iterator jter = remove_more_ids.begin();
300 jter != remove_more_ids.end();
308 iter != range.second;
322 void PHG4TruthEventAction::ProcessShowers() {
326 iter != range.second;
331 std::vector<std::vector<float> > points;
332 std::vector<float> weights;
336 for (std::map<
int,std::set<PHG4HitDefs::keytype> >::iterator iter = shower->
begin_g4hit_id();
339 int g4hitmap_id = iter->first;
340 std::map<int,PHG4HitContainer*>::iterator mapiter = hitmap_.find(g4hitmap_id);
341 if (mapiter == hitmap_.end()) {
347 unsigned int nhits = 0;
350 float light_yield = 0.0;
355 for (std::set<PHG4HitDefs::keytype>::iterator kter = iter->second.begin();
356 kter != iter->second.end();
362 cout <<
PHWHERE <<
" missing g4hit" << endl;
368 cout <<
PHWHERE <<
" missing g4particle for track "
375 cout <<
PHWHERE <<
" missing g4vertex" << endl;
381 if (!isnan(g4hit->
get_x(0)) &&
382 !isnan(g4hit->
get_y(0)) &&
383 !isnan(g4hit->
get_z(0))) {
385 std::vector<float> entry(3);
386 entry[0] = g4hit->
get_x(0);
387 entry[1] = g4hit->
get_y(0);
388 entry[2] = g4hit->
get_z(0);
390 points.push_back(entry);
392 weights.push_back(w);
397 if (!isnan(g4hit->
get_x(1)) &&
398 !isnan(g4hit->
get_y(1)) &&
399 !isnan(g4hit->
get_z(1))) {
401 std::vector<float> entry(3);
402 entry[0] = g4hit->
get_x(1);
403 entry[1] = g4hit->
get_y(1);
404 entry[2] = g4hit->
get_z(1);
406 points.push_back(entry);
408 weights.push_back(w);
416 if (abs(particle->
get_pid()) == 11) {
433 if (nhits) shower->
set_nhits(g4hitmap_id,nhits);
434 if (edep != 0.0) shower->
set_edep(g4hitmap_id,edep);
435 if (eion != 0.0) shower->
set_eion(g4hitmap_id,eion);
436 if (light_yield != 0.0) shower->
set_light_yield(g4hitmap_id,light_yield);
437 if (edep_h != 0.0) shower->
set_eh_ratio(g4hitmap_id,edep_e/edep_h);
443 Eigen::Matrix<double, Eigen::Dynamic, 3> X(points.size(),3);
444 Eigen::Matrix<double, Eigen::Dynamic, 1> W(weights.size(),1);
446 for (
unsigned int i=0; i<points.size(); ++i) {
447 for (
unsigned int j=0; j<3; ++j) {
448 X(i,j) = points[i][j];
454 double prefactor = 1.0 / sumw;
455 Eigen::Matrix<double, 1, 3> mean = prefactor * W.transpose() * X;
458 for (
unsigned int i=0; i<points.size(); ++i) {
459 for (
unsigned int j=0; j<3; ++j) X(i,j) = points[i][j] - mean(0,j);
463 prefactor = sumw / (pow(sumw,2) - sumw2);
464 Eigen::Matrix<double, 3, 3> covar = prefactor * (X.transpose() * W.asDiagonal() * X);
466 shower->
set_x(mean(0,0));
467 shower->
set_y(mean(0,1));
468 shower->
set_z(mean(0,2));
470 for (
unsigned int i = 0; i < 3; ++i) {
471 for (
unsigned int j = 0; j <= i; ++j) {
PHG4Hit * findHit(PHG4HitDefs::keytype key)
virtual float get_z(const int i) const
virtual float get_eion() const
virtual float get_edep() const
virtual float get_light_yield() const
virtual float get_y(const int i) const
virtual float get_x(const int i) const
virtual int get_trkid() const
virtual int get_pid() const
virtual int get_parent_id() const
virtual int get_vtx_id() const
virtual ParticleIdIter begin_g4particle_id()
ParticleIdSet::iterator ParticleIdIter
virtual void set_y(float y)
virtual void set_eh_ratio(int volume, float eh_ratio)
virtual void set_eion(int volume, float eion)
virtual bool empty_g4particle_id() const
virtual void set_x(float x)
virtual bool empty_g4hit_id() const
virtual void set_z(float x)
virtual size_t remove_g4hit_volume(int volume)
virtual size_t remove_g4particle_id(int id)
virtual void set_light_yield(int volume, float light_yield)
virtual void set_covar(unsigned int i, unsigned int j, float entry)
virtual HitIdIter begin_g4hit_id()
virtual ParticleIdIter end_g4particle_id()
virtual void set_nhits(int volume, unsigned int nhits)
virtual void set_edep(int volume, float edep)
virtual HitIdIter end_g4hit_id()
void BeginOfEventAction(const G4Event *)
int ResetEvent(PHCompositeNode *)
PHG4TruthEventAction(void)
constructor
void EndOfEventAction(const G4Event *)
void AddTrackidToWritelist(const G4int trackid)
add id into track list
void SetInterfacePointers(PHCompositeNode *)
get relevant nodes from top node passed as argument
VtxRange GetVtxRange()
Get a range of iterators covering the entire vertex container.
std::pair< ShowerIterator, ShowerIterator > ShowerRange
void AddEmbededTrkId(const int id, const int flag)
void delete_shower(ShowerIterator piter)
Range GetParticleRange()
Get a range of iterators covering the entire container.
std::pair< Iterator, Iterator > Range
PHG4VtxPoint * GetVtx(const int vtxid)
std::map< int, PHG4Particle * > Map
void delete_particle(Iterator piter)
VtxMap::iterator VtxIterator
std::pair< VtxIterator, VtxIterator > VtxRange
PHG4Particle * GetParticle(const int particleid)
ShowerRange GetShowerRange()
Get a range of iterators covering the entire container.
void delete_vtx(VtxIterator viter)
ShowerMap::iterator ShowerIterator
void AddEmbededVtxId(const int id, const int flag)
const Map & GetMap() const
Get the Particle Map storage.
int get_user_vtx_id() const
int get_user_track_id() const
const std::string getType() const
const std::string getName() const