std::set<SvtxHit*> SvtxHitEval::all_hits_from(PHG4Hit* g4hit) {

  if (!has_node_pointers()) {++_errors; return std::set<SvtxHit*>();}
  
  if (_strict) {assert(g4hit);}
  else if (!g4hit) {++_errors; return std::set<SvtxHit*>();}
  
  if (_do_cache) {
    std::map<PHG4Hit*,std::set<SvtxHit*> >::iterator iter =
      _cache_all_hits_from_g4hit.find(g4hit);
    if (iter != _cache_all_hits_from_g4hit.end()) {
      return iter->second;
    }
  }
  
  std::set<SvtxHit*> hits;

  unsigned int hit_layer = g4hit->get_layer();
  
  // loop over all the hits
  for (SvtxHitMap::Iter iter = _hitmap->begin();
       iter != _hitmap->end();
       ++iter) {

    SvtxHit* hit = iter->second;

    if (hit->get_layer() != hit_layer) continue;
    
    // loop over all truth hits connected to this hit
    std::set<PHG4Hit*> g4hits = all_truth_hits(hit);
    for (std::set<PHG4Hit*>::iterator jter = g4hits.begin();
	 jter != g4hits.end();
	 ++jter) {
      PHG4Hit* candidate = *jter;
      if (candidate->get_hit_id() == g4hit->get_hit_id()) {
	hits.insert(hit);
      }    
    }
  }

  if (_do_cache) _cache_all_hits_from_g4hit.insert(make_pair(g4hit,hits));
  
  return hits;
}
int PHG4SvtxThresholds::process_event(PHCompositeNode *topNode)
{
  _timer.get()->restart();

  std::vector<unsigned int> remove_hits;
  
  for (SvtxHitMap::Iter iter = _hits->begin();
       iter != _hits->end();
       ++iter) {
    SvtxHit* hit = iter->second;
   
    if (hit->get_e() < get_threshold_by_layer(hit->get_layer())) {
      if(verbosity > 2)
	cout << "Removing hit with energy " << hit->get_e() <<  " in layer " << hit->get_layer() << " threshold = " << get_threshold_by_layer(hit->get_layer()) << endl;
      remove_hits.push_back(hit->get_id());
    }
  }

  for (unsigned int i = 0; i < remove_hits.size(); ++i) {
    _hits->erase(remove_hits[i]);
  }
  
  _timer.get()->stop();
  return Fun4AllReturnCodes::EVENT_OK;
}
int PHG4SvtxDeadArea::process_event(PHCompositeNode *topNode) {
  
  _timer.get()->restart();
 
  std::vector<unsigned int> remove_hits;
  
  for (SvtxHitMap::Iter iter = _hits->begin();
       iter != _hits->end();
       ++iter) {
    SvtxHit* hit = &iter->second;

    if (_rand->Rndm() > get_hit_efficiency(hit->get_layer())) {
      remove_hits.push_back(hit->get_id());
    }
  }

  for (unsigned int i = 0; i < remove_hits.size(); ++i) {
    _hits->erase(remove_hits[i]);
  }
  
  _timer.get()->stop();
  return Fun4AllReturnCodes::EVENT_OK;
}
void SvtxEvaluator::fillOutputNtuples(PHCompositeNode *topNode) {

  if (verbosity > 1) cout << "SvtxEvaluator::fillOutputNtuples() entered" << endl;

  SvtxVertexEval*   vertexeval = _svtxevalstack->get_vertex_eval();
  SvtxTrackEval*     trackeval = _svtxevalstack->get_track_eval();
  SvtxClusterEval* clustereval = _svtxevalstack->get_cluster_eval();
  SvtxHitEval*         hiteval = _svtxevalstack->get_hit_eval();
  SvtxTruthEval*     trutheval = _svtxevalstack->get_truth_eval();
  
  //-----------------------
  // fill the Vertex NTuple
  //-----------------------

  if (_ntp_vertex) {
    //cout << "Filling ntp_vertex " << endl;
    SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");
    if (vertexmap && truthinfo) {
      for (SvtxVertexMap::Iter iter = vertexmap->begin();
	   iter != vertexmap->end();
	   ++iter) {
	SvtxVertex* vertex = iter->second;
	PHG4VtxPoint* point = vertexeval->max_truth_point_by_ntracks(vertex);

	float vx         = vertex->get_x();
	float vy         = vertex->get_y();
	float vz         = vertex->get_z();
	float ntracks    = vertex->size_tracks();

	float gvx        = NAN;
	float gvy        = NAN;
	float gvz        = NAN;
	float gvt        = NAN;
	float gntracks   = truthinfo->GetNumPrimaryVertexParticles();
	float nfromtruth = NAN;
	
	if (point) {
	  gvx        = point->get_x();
	  gvy        = point->get_y();
	  gvz        = point->get_z();
	  gvt        = point->get_t();
	  gntracks   = truthinfo->GetNumPrimaryVertexParticles();
	  nfromtruth = vertexeval->get_ntracks_contribution(vertex,point);
	}
	  
	float vertex_data[11] = {(float) _ievent,
				 vx,
				 vy,
				 vz,
				 ntracks,
				 gvx,
				 gvy,
				 gvz,
				 gvt,
				 gntracks,
				 nfromtruth
	};

	/*
	cout << "vertex: " 
	     << " ievent " << vertex_data[0]
	     << " vx " << vertex_data[1]
	     << " vy " << vertex_data[2]
	     << " vz " << vertex_data[3]
	     << endl;
	*/

	_ntp_vertex->Fill(vertex_data);      
      }
    }
  }
  
  //-----------------------
  // fill the gpoint NTuple
  //-----------------------
  
  if (_ntp_gpoint) {
    //cout << "Filling ntp_gpoint " << endl;    
    SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");

    if (vertexmap && truthinfo) {

      PHG4VtxPoint* point =  truthinfo->GetPrimaryVtx(truthinfo->GetPrimaryVertexIndex());

      if (point) {
      
	SvtxVertex* vertex = vertexeval->best_vertex_from(point);
    
	float gvx        = point->get_x();
	float gvy        = point->get_y();
	float gvz        = point->get_z();
	float gvt        = point->get_t();
	float gntracks   = truthinfo->GetNumPrimaryVertexParticles();
	float vx         = NAN;
	float vy         = NAN;
	float vz         = NAN;
	float ntracks    = NAN;
	float nfromtruth = NAN;

	if (vertex) {
	  vx         = vertex->get_x();
	  vy         = vertex->get_y();
	  vz         = vertex->get_z();
	  ntracks    = vertex->size_tracks();
	  nfromtruth = vertexeval->get_ntracks_contribution(vertex,point);
	}
	
	float gpoint_data[11] = {(float) _ievent,
				 gvx,
				 gvy,
				 gvz,
				 gvt,
				 gntracks,
				 vx,
				 vy,
				 vz,
				 ntracks,
				 nfromtruth
	};

	_ntp_gpoint->Fill(gpoint_data);      
      }
    }
  }
  
  //---------------------
  // fill the G4hit NTuple
  //---------------------

  if (_ntp_g4hit) {
    //cout << "Filling ntp_g4hit " << endl;
    std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits();
    for (std::set<PHG4Hit*>::iterator iter = g4hits.begin();
	 iter != g4hits.end();
	 ++iter) {
            
      PHG4Hit *g4hit = *iter;
      PHG4Particle *g4particle = trutheval->get_particle(g4hit);
      
      float g4hitID   = g4hit->get_hit_id();
      float gx        = g4hit->get_avg_x();
      float gy        = g4hit->get_avg_y();
      float gz        = g4hit->get_avg_z();
      float gt        = g4hit->get_avg_t();
      float gedep     = g4hit->get_edep();
      float glayer    = g4hit->get_layer();
  
      float gtrackID  = g4hit->get_trkid();

      float gflavor   = NAN;
      float gpx       = NAN;
      float gpy       = NAN;
      float gpz       = NAN;

      float gvx       = NAN;
      float gvy       = NAN;
      float gvz       = NAN;

      float gembed  = NAN;
      float gprimary = NAN;

      float gfpx      = 0.;
      float gfpy      = 0.;
      float gfpz      = 0.;
      float gfx       = 0.;
      float gfy       = 0.;
      float gfz       = 0.;

      if (g4particle) {

	if (_scan_for_embedded) {
	  if (trutheval->get_embed(g4particle) == 0) continue;
	}
	
	gflavor   = g4particle->get_pid();
	gpx       = g4particle->get_px();
	gpy       = g4particle->get_py();
	gpz       = g4particle->get_pz();

	PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);	

	if (vtx) {
	  gvx       = vtx->get_x();
	  gvy       = vtx->get_y();
	  gvz       = vtx->get_z();
	}
    
	PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	if (outerhit) {
	  gfpx      = outerhit->get_px(1);
	  gfpy      = outerhit->get_py(1);
	  gfpz      = outerhit->get_pz(1);
	  gfx       = outerhit->get_x(1);
	  gfy       = outerhit->get_y(1);
	  gfz       = outerhit->get_z(1);
	}
	
	gembed    = trutheval->get_embed(g4particle);
	gprimary  = trutheval->is_primary(g4particle);
      } //       if (g4particle)
      
      std::set<SvtxCluster*> clusters = clustereval->all_clusters_from(g4hit);  
      float nclusters = clusters.size();

      // best cluster reco'd
      SvtxCluster* cluster = clustereval->best_cluster_from(g4hit);

      float clusID     = NAN;
      float x          = NAN;
      float y          = NAN;
      float z          = NAN;
      float e          = NAN;
      float adc        = NAN;
      float layer      = NAN;
      float size       = NAN;
      float phisize    = NAN;
      float zsize      = NAN;
      float efromtruth = NAN;

      if (cluster) {
	clusID     = cluster->get_id();
	x          = cluster->get_x();
	y          = cluster->get_y();
	z          = cluster->get_z();
	e          = cluster->get_e();
	adc        = cluster->get_adc();
	layer      = cluster->get_layer();
	size       = cluster->size_hits();
	phisize    = cluster->get_phi_size();
	zsize      = cluster->get_z_size();
	if (g4particle) {
	  efromtruth = clustereval->get_energy_contribution(cluster,g4particle);
	}
      }

      float g4hit_data[36] = {(float) _ievent,
			      g4hitID,
			      gx,
			      gy,
			      gz,
			      gt,
			      gedep,
			      glayer,
			      gtrackID,
			      gflavor,
			      gpx,
			      gpy,
			      gpz,
			      gvx,
			      gvy,
			      gvz,
			      gfpx,
			      gfpy,
			      gfpz,
			      gfx,
			      gfy,
			      gfz,
			      gembed,
			      gprimary,
			      nclusters,
			      clusID,  
			      x,      
			      y,      
			      z,      
			      e,      
			      adc,    
			      layer,  
			      size,   
			      phisize,
			      zsize,  
			      efromtruth
      };

      _ntp_g4hit->Fill(g4hit_data);
    }
  }
  
  //--------------------
  // fill the Hit NTuple
  //--------------------

  if (_ntp_hit) {
    //cout << "Filling ntp_hit " << endl;
    // need things off of the DST...
    SvtxHitMap* hitmap = findNode::getClass<SvtxHitMap>(topNode,"SvtxHitMap");
    if (hitmap) {

      for (SvtxHitMap::Iter iter = hitmap->begin();
	   iter != hitmap->end();
	   ++iter) {

	SvtxHit* hit             = iter->second;
	PHG4Hit* g4hit           = hiteval->max_truth_hit_by_energy(hit);
	PHG4CylinderCell* g4cell = hiteval->get_cell(hit);
	PHG4Particle* g4particle = trutheval->get_particle(g4hit);

	float event  = _ievent;
	float hitID  = hit->get_id();
	float e      = hit->get_e();
	float adc    = hit->get_adc();
	float layer  = hit->get_layer();
	float cellID = hit->get_cellid();
	float ecell  = g4cell->get_edep();

	float g4hitID  = NAN;
	float gedep    = NAN;
	float gx       = NAN;
	float gy       = NAN;
	float gz       = NAN;
	float gt       = NAN;
	float gtrackID = NAN;
	float gflavor  = NAN;
	float gpx      = NAN;
	float gpy      = NAN;
	float gpz      = NAN;
	float gvx      = NAN;
	float gvy      = NAN;
	float gvz      = NAN;
	float gfpx     = NAN;
	float gfpy     = NAN;
	float gfpz     = NAN;
	float gfx      = NAN;
	float gfy      = NAN;
	float gfz      = NAN;
	float gembed   = NAN;
	float gprimary = NAN;
      
	float efromtruth = NAN;
      
	if (g4hit) {
	  g4hitID  = g4hit->get_hit_id();
	  gedep    = g4hit->get_edep();
	  gx       = g4hit->get_avg_x();
	  gy       = g4hit->get_avg_y();
	  gz       = g4hit->get_avg_z();
	  gt       = g4hit->get_avg_t();

	  if (g4particle) {

	    if (_scan_for_embedded) {
	      if (trutheval->get_embed(g4particle) == 0) continue;
	    }
	    
	    gtrackID = g4particle->get_track_id();
	    gflavor  = g4particle->get_pid();
	    gpx      = g4particle->get_px();
	    gpy      = g4particle->get_py();
	    gpz      = g4particle->get_pz();

	    PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);

	    if (vtx) {
	      gvx      = vtx->get_x();
	      gvy      = vtx->get_y();
	      gvz      = vtx->get_z();
	    }

	    PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	    if (outerhit) {
	      gfpx     = outerhit->get_px(1);
	      gfpy     = outerhit->get_py(1);
	      gfpz     = outerhit->get_pz(1);
	      gfx      = outerhit->get_x(1);
	      gfy      = outerhit->get_y(1);
	      gfz      = outerhit->get_z(1);
	    }
	    gembed   = trutheval->get_embed(g4particle);
	    gprimary = trutheval->is_primary(g4particle);
	  } //   if (g4particle){
	}      

	if (g4particle) {
	  efromtruth = hiteval->get_energy_contribution(hit,g4particle);
	}

	float hit_data[33] = {
	  event,
	  hitID,
	  e,
	  adc,
	  layer,
	  cellID,
	  ecell,
	  g4hitID,
	  gedep,
	  gx,
	  gy,
	  gz,
	  gt,
	  gtrackID,
	  gflavor,
	  gpx,
	  gpy,
	  gpz,
	  gvx,
	  gvy,
	  gvz,
	  gfpx,
	  gfpy,
	  gfpz,
	  gfx,
	  gfy,
	  gfz,
	  gembed,
	  gprimary,
	  efromtruth
	};
	  
	_ntp_hit->Fill(hit_data);     
      }
    }
  }
  
  //------------------------
  // fill the Cluster NTuple
  //------------------------

  //cout << "check for ntp_cluster" << endl;

  if (_ntp_cluster && !_scan_for_embedded) {
    //cout << "Filling ntp_cluster 1 " << endl;
    // need things off of the DST...
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (clustermap) {

      for (SvtxClusterMap::Iter iter = clustermap->begin();
	   iter != clustermap->end();
	   ++iter) {
    
	SvtxCluster* cluster     = iter->second;   
	SvtxTrack* track = trackeval->best_track_from(cluster);
	
	PHG4Hit *g4hit           = clustereval->max_truth_hit_by_energy(cluster); 
	PHG4Particle *g4particle = trutheval->get_particle(g4hit);
    
	float hitID    = cluster->get_id();
	float x        = cluster->get_x();
	float y        = cluster->get_y();
	float z        = cluster->get_z();

	float ex       = sqrt(cluster->get_error(0,0));
	float ey       = sqrt(cluster->get_error(1,1));
	float ez       = cluster->get_z_error();

	float ephi     = cluster->get_phi_error();
	
	float e        = cluster->get_e();
	float adc      = cluster->get_adc();
	float layer    = cluster->get_layer();
	float size     = cluster->size_hits();
	float phisize  = cluster->get_phi_size();
	float zsize    = cluster->get_z_size();

	float trackID  = NAN;
	if (track) trackID = track->get_id();

	float g4hitID  = NAN;
	float gx       = NAN;
	float gy       = NAN;
	float gz       = NAN;
	float gt       = NAN;
	float gtrackID = NAN;
	float gflavor  = NAN;
	float gpx      = NAN;
	float gpy      = NAN;
	float gpz      = NAN;
	float gvx      = NAN;
	float gvy      = NAN;
	float gvz      = NAN;
	float gfpx     = NAN;
	float gfpy     = NAN;
	float gfpz     = NAN;
	float gfx      = NAN;
	float gfy      = NAN;
	float gfz      = NAN;
	float gembed   = NAN;
	float gprimary = NAN;
    
	float efromtruth = NAN;
      
	if (g4hit) {
	  g4hitID  = g4hit->get_hit_id();
	  gx       = g4hit->get_avg_x();
	  gy       = g4hit->get_avg_y();
	  gz       = g4hit->get_avg_z();
	  gt       = g4hit->get_avg_t();

	  if (g4particle) {

	    gtrackID = g4particle->get_track_id();
	    gflavor  = g4particle->get_pid();
	    gpx      = g4particle->get_px();
	    gpy      = g4particle->get_py();
	    gpz      = g4particle->get_pz();

	    PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);
	    if (vtx) {
	      gvx      = vtx->get_x();
	      gvy      = vtx->get_y();
	      gvz      = vtx->get_z();
	    }

	    PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	    if (outerhit) {
	      gfpx     = outerhit->get_px(1);
	      gfpy     = outerhit->get_py(1);
	      gfpz     = outerhit->get_pz(1);
	      gfx      = outerhit->get_x(1);
	      gfy      = outerhit->get_y(1);
	      gfz      = outerhit->get_z(1);
	    }
	    
	    gembed   = trutheval->get_embed(g4particle);
	    gprimary = trutheval->is_primary(g4particle);
	  }      //   if (g4particle){
	} //  if (g4hit) {

	if (g4particle){
	  efromtruth = clustereval->get_energy_contribution(cluster,g4particle);
	}

	float cluster_data[38] = {(float) _ievent,
				  hitID,
				  x,
				  y,
				  z,
				  ex,
				  ey,
				  ez,
				  ephi,
				  e,
				  adc,
				  layer,
				  size,
				  phisize,
				  zsize,
				  trackID,
				  g4hitID,
				  gx,
				  gy,
				  gz,
				  gt,
				  gtrackID,
				  gflavor,
				  gpx,
				  gpy,
				  gpz,
				  gvx,
				  gvy,
				  gvz,
				  gfpx,
				  gfpy,
				  gfpz,
				  gfx,
				  gfy,
				  gfz,
				  gembed,
				  gprimary,
				  efromtruth};

	_ntp_cluster->Fill(cluster_data);
      }		  
    }
    
  } else if (_ntp_cluster && _scan_for_embedded) {

    //cout << "Filling ntp_cluster 2 " << endl;

    // if only scanning embedded signals, loop over all the tracks from
    // embedded particles and report all of their clusters, including those
    // from other sources (noise hits on the embedded track)
    
    // need things off of the DST...
    SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (trackmap) {
      
      for (SvtxTrackMap::Iter iter = trackmap->begin();
	   iter != trackmap->end();
	   ++iter) {
	
	SvtxTrack* track = iter->second;
	PHG4Particle* truth = trackeval->max_truth_particle_by_nclusters(track);
	if (truth) {	  
	  if (trutheval->get_embed(truth) == 0) continue;
	}
	
	for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
	     iter != track->end_clusters();
	     ++iter) {

	  unsigned int cluster_id = *iter;
	  SvtxCluster* cluster = clustermap->get(cluster_id);

	  PHG4Hit *g4hit           = clustereval->max_truth_hit_by_energy(cluster); 
	  PHG4Particle *g4particle = trutheval->get_particle(g4hit);
    
	  float hitID    = cluster->get_id();
	  float x        = cluster->get_x();
	  float y        = cluster->get_y();
	  float z        = cluster->get_z();

	  float ex       = sqrt(cluster->get_error(0,0));
	  float ey       = sqrt(cluster->get_error(1,1));
	  float ez       = cluster->get_z_error();

	  float ephi     = cluster->get_phi_error();
	  
	  float e        = cluster->get_e();
	  float adc      = cluster->get_adc();
	  float layer    = cluster->get_layer();
	  float size     = cluster->size_hits();
	  float phisize  = cluster->get_phi_size();
	  float zsize    = cluster->get_z_size();

	  float trackID  = NAN;
	  if (track) trackID = track->get_id();

	  float g4hitID  = NAN;
	  float gx       = NAN;
	  float gy       = NAN;
	  float gz       = NAN;
	  float gt       = NAN;
	  float gtrackID = NAN;
	  float gflavor  = NAN;
	  float gpx      = NAN;
	  float gpy      = NAN;
	  float gpz      = NAN;
	  float gvx      = NAN;
	  float gvy      = NAN;
	  float gvz      = NAN;
	  float gfpx     = NAN;
	  float gfpy     = NAN;
	  float gfpz     = NAN;
	  float gfx      = NAN;
	  float gfy      = NAN;
	  float gfz      = NAN;
	  float gembed   = NAN;
	  float gprimary = NAN;
    
	  float efromtruth = NAN;
      
	  if (g4hit) {
	    g4hitID  = g4hit->get_hit_id();
	    gx       = g4hit->get_avg_x();
	    gy       = g4hit->get_avg_y();
      gz       = g4hit->get_avg_z();
      gt       = g4hit->get_avg_t();

	    if (g4particle) {
	    
	      gtrackID = g4particle->get_track_id();
	      gflavor  = g4particle->get_pid();
	      gpx      = g4particle->get_px();
	      gpy      = g4particle->get_py();
	      gpz      = g4particle->get_pz();

	      PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);
	      if (vtx) {
		gvx      = vtx->get_x();
		gvy      = vtx->get_y();
		gvz      = vtx->get_z();
	      }

	      PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	      if (outerhit) {
		gfpx     = outerhit->get_px(1);
		gfpy     = outerhit->get_py(1);
		gfpz     = outerhit->get_pz(1);
		gfx      = outerhit->get_x(1);
		gfy      = outerhit->get_y(1);
		gfz      = outerhit->get_z(1);
	      }
	    
	      gembed   = trutheval->get_embed(g4particle);
	      gprimary = trutheval->is_primary(g4particle);
	    }      //   if (g4particle){
	  } //  if (g4hit) {

	  if (g4particle){
	    efromtruth = clustereval->get_energy_contribution(cluster,g4particle);
	  }

	  float cluster_data[38] = {(float) _ievent,
				    hitID,
				    x,
				    y,
				    z,
				    ex,
				    ey,
				    ez,
				    ephi,
				    e,
				    adc,
				    layer,
				    size,
				    phisize,
				    zsize,
				    trackID,
				    g4hitID,
				    gx,
				    gy,
				    gz,
				    gt,
				    gtrackID,
				    gflavor,
				    gpx,
				    gpy,
				    gpz,
				    gvx,
				    gvy,
				    gvz,
				    gfpx,
				    gfpy,
				    gfpz,
				    gfx,
				    gfy,
				    gfz,
				    gembed,
				    gprimary,
				    efromtruth};

	  _ntp_cluster->Fill(cluster_data);
	}		  
      }
    }
  }
    
  //------------------------
  // fill the Gtrack NTuple
  //------------------------

  // need things off of the DST...

  //cout << "check for ntp_gtrack" << endl;

  if (_ntp_gtrack) {
    //cout << "Filling ntp_gtrack " << endl;

    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");   
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (truthinfo) {

      PHG4TruthInfoContainer::ConstRange range = truthinfo->GetPrimaryParticleRange();
      for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
	   iter != range.second; 
	 ++iter) {
	
	PHG4Particle* g4particle = iter->second;

	if (_scan_for_embedded) {
	  if (trutheval->get_embed(g4particle) == 0) continue;
	}
	
	float gtrackID = g4particle->get_track_id();
	float gflavor  = g4particle->get_pid();
      
	std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);     
	float ng4hits = g4hits.size();  
	float gpx      = g4particle->get_px();
	float gpy      = g4particle->get_py();
	float gpz      = g4particle->get_pz();

	PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);	
	float gvx      = vtx->get_x();
	float gvy      = vtx->get_y();
	float gvz      = vtx->get_z();
	float gvt      = vtx->get_t();

	float gfpx      = 0.;
	float gfpy      = 0.;
	float gfpz      = 0.;
	float gfx       = 0.;
	float gfy       = 0.;
	float gfz       = 0.;
    
	PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	if (outerhit) {
	  gfpx      = outerhit->get_px(1);
	  gfpy      = outerhit->get_py(1);
	  gfpz      = outerhit->get_pz(1);
	  gfx       = outerhit->get_x(1);
	  gfy       = outerhit->get_y(1);
	  gfz       = outerhit->get_z(1);
	}
      
	float gembed   = trutheval->get_embed(g4particle);
	float gprimary = trutheval->is_primary(g4particle);

	SvtxTrack* track = trackeval->best_track_from(g4particle);

	float trackID       = NAN;
	float charge        = NAN;
	float quality       = NAN;
	float chisq         = NAN;
	float ndf           = NAN;
	float nhits         = NAN;
	unsigned int layers = 0x0;
	float dca2d         = NAN;
	float dca2dsigma    = NAN;
	float px            = NAN;
	float py            = NAN;
	float pz            = NAN;
	float pcax          = NAN;
	float pcay          = NAN;
	float pcaz          = NAN;

	float nfromtruth    = NAN;
	float layersfromtruth = NAN;

	if (track) {
	  trackID   = track->get_id();     
	  charge    = track->get_charge();
	  quality   = track->get_quality();
	  chisq     = track->get_chisq();
	  ndf       = track->get_ndf();
	  nhits     = track->size_clusters();
	  
	  for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
	       iter != track->end_clusters();
	       ++iter) {
	    unsigned int cluster_id = *iter;
	    SvtxCluster* cluster = clustermap->get(cluster_id);
	    unsigned int layer = cluster->get_layer();
	    if (layer < 32) layers |= (0x1 << layer);
	  }

	  dca2d     = track->get_dca2d();
	  dca2dsigma = track->get_dca2d_error();
	  px        = track->get_px();
	  py        = track->get_py();
	  pz        = track->get_pz();
	  pcax      = track->get_x();
	  pcay      = track->get_y();
	  pcaz      = track->get_z();

	  nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);
	  layersfromtruth = trackeval->get_nclusters_contribution_by_layer(track,g4particle);
	}
      
	float gtrack_data[36] = {(float) _ievent,
				 gtrackID,
				 gflavor,
				 ng4hits,
				 gpx,
				 gpy,
				 gpz,
				 gvx,
				 gvy,
				 gvz,
				 gvt,
				 gfpx,
				 gfpy,
				 gfpz,
				 gfx,
				 gfy,
				 gfz,
				 gembed,
				 gprimary,
				 trackID,
				 px,         
				 py,         
				 pz,  
				 charge,     
				 quality,    
				 chisq,      
				 ndf,        
				 nhits,      
				 (float) layers,     
				 dca2d,      
				 dca2dsigma, 			       
				 pcax,       
				 pcay,       
				 pcaz,
				 nfromtruth,
				 layersfromtruth
	};

	/*
	cout << " ievent " << _ievent
	     << " gtrackID " << gtrackID
	     << " gflavor " << gflavor
	     << " ng4hits " << ng4hits
	     << endl;
	*/

	_ntp_gtrack->Fill(gtrack_data);

      }	     
    }
  }
  
  //------------------------
  // fill the Track NTuple
  //------------------------



  if (_ntp_track) {
    //cout << "Filling ntp_track " << endl;

    // need things off of the DST...
    SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (trackmap) {

      for (SvtxTrackMap::Iter iter = trackmap->begin();
	   iter != trackmap->end();
	   ++iter) {
    
	SvtxTrack* track = iter->second;

	float trackID   = track->get_id();     
	float charge    = track->get_charge();
	float quality   = track->get_quality();
	float chisq     = track->get_chisq();
	float ndf       = track->get_ndf();
	float nhits     = track->size_clusters();

	unsigned int layers = 0x0;
	for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
	     iter != track->end_clusters();
	     ++iter) {
	  unsigned int cluster_id = *iter;
	  SvtxCluster* cluster = clustermap->get(cluster_id);
	  unsigned int layer = cluster->get_layer();
	  if (layer < 31) layers |= (0x1 << layer);
	}
      
	float dca2d     = track->get_dca2d();
	float dca2dsigma = track->get_dca2d_error();
	float px        = track->get_px();
	float py        = track->get_py();
	float pz        = track->get_pz();
	float pcax      = track->get_x();
	float pcay      = track->get_y();
	float pcaz      = track->get_z();

	float presdphi = track->get_cal_dphi(SvtxTrack::PRES);
	float presdeta = track->get_cal_deta(SvtxTrack::PRES);
	float prese3x3 = track->get_cal_energy_3x3(SvtxTrack::PRES);
	float prese    = track->get_cal_cluster_e(SvtxTrack::PRES);

	float cemcdphi = track->get_cal_dphi(SvtxTrack::CEMC);
	float cemcdeta = track->get_cal_deta(SvtxTrack::CEMC);
	float cemce3x3 = track->get_cal_energy_3x3(SvtxTrack::CEMC);
	float cemce    = track->get_cal_cluster_e(SvtxTrack::CEMC);

	float hcalindphi = track->get_cal_dphi(SvtxTrack::HCALIN);
	float hcalindeta = track->get_cal_deta(SvtxTrack::HCALIN);
	float hcaline3x3 = track->get_cal_energy_3x3(SvtxTrack::HCALIN);
	float hcaline    = track->get_cal_cluster_e(SvtxTrack::HCALIN);

	float hcaloutdphi = track->get_cal_dphi(SvtxTrack::HCALOUT);
	float hcaloutdeta = track->get_cal_deta(SvtxTrack::HCALOUT);
	float hcaloute3x3 = track->get_cal_energy_3x3(SvtxTrack::HCALOUT);
	float hcaloute    = track->get_cal_cluster_e(SvtxTrack::HCALOUT);

	float gtrackID = NAN;
	float gflavor  = NAN;     
	float ng4hits  = NAN;
	float gpx      = NAN;
	float gpy      = NAN;
	float gpz      = NAN;
	float gvx      = NAN;
	float gvy      = NAN;
	float gvz      = NAN;
	float gvt      = NAN;
	float gfpx     = NAN;
	float gfpy     = NAN;
	float gfpz     = NAN;
	float gfx      = NAN;
	float gfy      = NAN;
	float gfz      = NAN;
	float gembed   = NAN;
	float gprimary = NAN;

	float nfromtruth = NAN;
	float layersfromtruth = NAN;
      
	PHG4Particle* g4particle = trackeval->max_truth_particle_by_nclusters(track);	
	
	if (g4particle) {

	  if (_scan_for_embedded) {
	    if (trutheval->get_embed(g4particle) == 0) continue;
	  }
	  
	  gtrackID = g4particle->get_track_id();
	  gflavor  = g4particle->get_pid();
      
	  std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);     
	  ng4hits = g4hits.size();  
	  gpx      = g4particle->get_px();
	  gpy      = g4particle->get_py();
	  gpz      = g4particle->get_pz();
	
	  PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);	
	  gvx      = vtx->get_x();
	  gvy      = vtx->get_y();
	  gvz      = vtx->get_z();
	  gvt      = vtx->get_t();

	  PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	      
	  if (outerhit) {
	    gfpx     = outerhit->get_px(1);
	    gfpy     = outerhit->get_py(1);
	    gfpz     = outerhit->get_pz(1);
	    gfx      = outerhit->get_x(1);
	    gfy      = outerhit->get_y(1);
	    gfz      = outerhit->get_z(1);
	  }
	  gembed   = trutheval->get_embed(g4particle);
	  gprimary = trutheval->is_primary(g4particle);

	  nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);
	  layersfromtruth = trackeval->get_nclusters_contribution_by_layer(track,g4particle);
	}
      
	float track_data[52] = {(float) _ievent,
				trackID, 
				px,        
				py,        
				pz,      
				charge,  
				quality, 
				chisq,   
				ndf,     
				nhits,   
				(float) layers,
				dca2d,     
				dca2dsigma,      
				pcax,      
				pcay,      
				pcaz,      
				presdphi,
				presdeta,
				prese3x3,
				prese,   
				cemcdphi,
				cemcdeta,
				cemce3x3,
				cemce,   
				hcalindphi,
				hcalindeta,
				hcaline3x3,
				hcaline,   
				hcaloutdphi,
				hcaloutdeta,
				hcaloute3x3,
				hcaloute,
				gtrackID,
				gflavor,
				ng4hits,
				gpx,
				gpy,
				gpz,
				gvx,
				gvy,
				gvz,
				gvt,
				gfpx,
				gfpy,
				gfpz,
				gfx,
				gfy,
				gfz,
				gembed,
				gprimary,
				nfromtruth,
				layersfromtruth
	};

	/*
	cout << "ievent " << _ievent
	     << " trackID " << trackID
	     << " nhits " << nhits
	     << " px " << px
	     << " py " << py
	     << " pz " << pz
	     << " gembed " << gembed
	     << " gprimary " << gprimary 
	     << endl;
	*/
	_ntp_track->Fill(track_data);
      }
    }
  }
  
  return;
}
void SvtxEvaluator::printOutputInfo(PHCompositeNode *topNode) {
  
  if (verbosity > 1) cout << "SvtxEvaluator::printOutputInfo() entered" << endl;

  //==========================================
  // print out some useful stuff for debugging
  //==========================================

  if (verbosity > 0) {
    
    SvtxTrackEval*     trackeval = _svtxevalstack->get_track_eval();
    SvtxClusterEval* clustereval = _svtxevalstack->get_cluster_eval();
    SvtxTruthEval*     trutheval = _svtxevalstack->get_truth_eval();
  
    // event information
    cout << endl;
    cout << PHWHERE << "   NEW OUTPUT FOR EVENT " << _ievent << endl;
    cout << endl;

    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");
    
    PHG4VtxPoint *gvertex = truthinfo->GetPrimaryVtx( truthinfo->GetPrimaryVertexIndex() );
    float gvx = gvertex->get_x();
    float gvy = gvertex->get_y();
    float gvz = gvertex->get_z();

    float vx = NAN;
    float vy = NAN;
    float vz = NAN;

    SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");    
    if (vertexmap) {
      if (!vertexmap->empty()) {
	SvtxVertex* vertex = (vertexmap->begin()->second);
	
	vx = vertex->get_x();
	vy = vertex->get_y();
	vz = vertex->get_z();
      }
    }

    cout << "===Vertex Reconstruction=======================" << endl;
    cout << "vtrue = (" << gvx << "," << gvy << "," << gvz << ") => vreco = (" << vx << "," << vy << "," << vz << ")" << endl;
    cout << endl;

    cout << "===Tracking Summary============================" << endl;
    unsigned int ng4hits[100] = {0};
    std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits();
    for (std::set<PHG4Hit*>::iterator iter = g4hits.begin();
	 iter != g4hits.end();
	 ++iter) {
      PHG4Hit *g4hit = *iter;
      ++ng4hits[g4hit->get_layer()];
    }

    SvtxHitMap* hitmap = findNode::getClass<SvtxHitMap>(topNode,"SvtxHitMap");
    unsigned int nhits[100] = {0};
    if (hitmap) {
      for (SvtxHitMap::Iter iter = hitmap->begin();
	   iter != hitmap->end();
	   ++iter) {
	SvtxHit* hit = iter->second;
	++nhits[hit->get_layer()];
      }
    }
    
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    unsigned int nclusters[100] = {0};
    if (clustermap) {
      for (SvtxClusterMap::Iter iter = clustermap->begin();
	   iter != clustermap->end();
	   ++iter) {
	SvtxCluster* cluster = iter->second;
	++nclusters[cluster->get_layer()];
      }
    }

    for (unsigned int ilayer = 0; ilayer < 100; ++ilayer) {
      cout << "layer " << ilayer << ": nG4hits = " << ng4hits[ilayer]
	   << " => nHits = " << nhits[ilayer]
	   << " => nClusters = " << nclusters[ilayer] << endl;
    }

    SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
    
    cout << "nGtracks = " << std::distance(truthinfo->GetPrimaryParticleRange().first,
					  truthinfo->GetPrimaryParticleRange().second);
    cout << " => nTracks = ";
    if (trackmap) cout << trackmap->size() << endl;
    else cout << 0 << endl;

    // cluster wise information
    if (verbosity > 1) {
 
      for(std::set<PHG4Hit*>::iterator iter = g4hits.begin();
	  iter != g4hits.end();
	  ++iter) {
	PHG4Hit *g4hit = *iter;

	cout << endl;
        cout << "===PHG4Hit===================================" << endl;
	cout << " PHG4Hit: "; g4hit->identify();

	std::set<SvtxCluster*> clusters = clustereval->all_clusters_from(g4hit);

	for (std::set<SvtxCluster*>::iterator jter = clusters.begin();
	     jter != clusters.end();
	     ++jter) {
	  SvtxCluster *cluster = *jter;
	  cout << "===Created-SvtxCluster================" << endl;      
	  cout << "SvtxCluster: "; cluster->identify();
	}
      }

      PHG4TruthInfoContainer::ConstRange range = truthinfo->GetPrimaryParticleRange();
      for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
	   iter != range.second; 
	   ++iter) {
	
	PHG4Particle *particle = iter->second;

	// track-wise information
	cout << endl;

	cout << "=== Gtrack ===================================================" << endl;
	cout << " PHG4Particle id = " << particle->get_track_id() << endl;
	particle->identify();
	cout << " ptrue = (";
	cout.width(5); cout << particle->get_px();
	cout << ",";
	cout.width(5); cout << particle->get_py();
	cout << ",";
	cout.width(5); cout << particle->get_pz();
	cout << ")" << endl;

	cout << " vtrue = (";
	cout.width(5); cout << truthinfo->GetVtx(particle->get_vtx_id())->get_x();
	cout << ",";
	cout.width(5); cout << truthinfo->GetVtx(particle->get_vtx_id())->get_y();
	cout << ",";
	cout.width(5); cout << truthinfo->GetVtx(particle->get_vtx_id())->get_z();
	cout << ")" << endl;
	  
	cout << " pt = " << sqrt(pow(particle->get_px(),2)+pow(particle->get_py(),2)) << endl;
	cout << " phi = " << atan2(particle->get_py(),particle->get_px()) << endl;
	cout << " eta = " << asinh(particle->get_pz()/sqrt(pow(particle->get_px(),2)+pow(particle->get_py(),2))) << endl;
	  
	cout << " embed flag = " << truthinfo->isEmbeded(particle->get_track_id()) << endl;

	cout << " ---Associated-PHG4Hits-----------------------------------------" << endl;
	std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(particle);
	for(std::set<PHG4Hit*>::iterator jter = g4hits.begin();
	    jter != g4hits.end();
	    ++jter) {
	  PHG4Hit *g4hit = *jter;

	  float x = 0.5*(g4hit->get_x(0)+g4hit->get_x(1));
	  float y = 0.5*(g4hit->get_y(0)+g4hit->get_y(1));
	  float z = 0.5*(g4hit->get_z(0)+g4hit->get_z(1));
	      
	  cout << " #" << g4hit->get_hit_id() << " xtrue = (";
	  cout.width(5); cout << x;
	  cout << ",";
	  cout.width(5); cout << y;
	  cout << ",";
	  cout.width(5); cout << z;
	  cout << ")";

	  std::set<SvtxCluster*> clusters = clustereval->all_clusters_from(g4hit);
	  for (std::set<SvtxCluster*>::iterator kter = clusters.begin();
	       kter != clusters.end();
	       ++kter) {
	  
	    SvtxCluster *cluster = *kter;

	    float x = cluster->get_x();
	    float y = cluster->get_y();
	    float z = cluster->get_z();
		 
	    cout << " => #" << cluster->get_id(); 
	    cout << " xreco = (";
	    cout.width(5); cout << x;
	    cout << ",";
	    cout.width(5); cout << y;
	    cout << ",";
	    cout.width(5); cout << z;
	    cout << ")";
	  }

	  cout << endl;
	}

	if (trackmap&&clustermap) {

	  std::set<SvtxTrack*> tracks = trackeval->all_tracks_from(particle);
	  for (std::set<SvtxTrack*>::iterator jter = tracks.begin();
	       jter != tracks.end();
	       ++jter) {
	  
	    SvtxTrack *track = *jter;

	    float px = track->get_px();
	    float py = track->get_py();
	    float pz = track->get_pz();

	    cout << "===Created-SvtxTrack==========================================" << endl;
	    cout << " SvtxTrack id = " << track->get_id() << endl;
	    cout << " preco = (";
	    cout.width(5); cout << px;
	    cout << ",";
	    cout.width(5); cout << py;
	    cout << ",";
	    cout.width(5); cout << pz;
	    cout << ")" << endl;
	    cout << " quality = " << track->get_quality() << endl;
	    cout << " nfromtruth = " << trackeval->get_nclusters_contribution(track,particle) << endl;

	    cout << " ---Associated-SvtxClusters-to-PHG4Hits-------------------------" << endl;    

	    for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
		 iter != track->end_clusters();
		 ++iter) {
	      unsigned int cluster_id = *iter;
	      SvtxCluster* cluster = clustermap->get(cluster_id);
	      		  
	      float x = cluster->get_x();
	      float y = cluster->get_y();
	      float z = cluster->get_z();
			  
	      cout << " #" << cluster->get_id() << " xreco = (";
	      cout.width(5); cout << x;
	      cout << ",";
	      cout.width(5); cout << y;
	      cout << ",";
	      cout.width(5); cout << z;
	      cout << ") =>";

	      PHG4Hit* g4hit = clustereval->max_truth_hit_by_energy(cluster);
	      if ((g4hit) && (g4hit->get_trkid() == particle->get_track_id())) {
			  
		x = 0.5*(g4hit->get_x(0)+g4hit->get_x(1));
		y = 0.5*(g4hit->get_y(0)+g4hit->get_y(1));
		z = 0.5*(g4hit->get_z(0)+g4hit->get_z(1));
			    
		cout << " #" << g4hit->get_hit_id()
		     << " xtrue = (";
		cout.width(5); cout << x;
		cout << ",";
		cout.width(5); cout << y;
		cout << ",";
		cout.width(5); cout << z;
		cout << ") => Gtrack id = " << g4hit->get_trkid();
	      } else {
		cout << " noise hit";
	      }
	    }
  
	    cout << endl;
	  }
	}
      }
    }
      
    cout << endl;

  } // if verbosity

  return;
}
void SvtxEvaluator::fillOutputNtuples(PHCompositeNode *topNode) {

  if (verbosity > 1) cout << "SvtxEvaluator::fillOutputNtuples() entered" << endl;

  SvtxVertexEval*   vertexeval = _svtxevalstack->get_vertex_eval();
  SvtxTrackEval*     trackeval = _svtxevalstack->get_track_eval();
  SvtxClusterEval* clustereval = _svtxevalstack->get_cluster_eval();
  SvtxHitEval*         hiteval = _svtxevalstack->get_hit_eval();
  SvtxTruthEval*     trutheval = _svtxevalstack->get_truth_eval();
  
  //-----------------------
  // fill the Vertex NTuple
  //-----------------------

  if (_ntp_vertex) {
    SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");
    if (vertexmap && truthinfo) {
      for (SvtxVertexMap::Iter iter = vertexmap->begin();
	   iter != vertexmap->end();
	   ++iter) {
	SvtxVertex* vertex = iter->second;
	PHG4VtxPoint* point = vertexeval->max_truth_point_by_ntracks(vertex);

	float vx         = vertex->get_x();
	float vy         = vertex->get_y();
	float vz         = vertex->get_z();
	float ntracks    = vertex->size_tracks();

	float gvx        = NAN;
	float gvy        = NAN;
	float gvz        = NAN;
	float gntracks   = truthinfo->GetNumPrimaryVertexParticles();
	float nfromtruth = NAN;
	
	if (point) {
	  gvx        = point->get_x();
	  gvy        = point->get_y();
	  gvz        = point->get_z();
	  gntracks   = truthinfo->GetNumPrimaryVertexParticles();
	  nfromtruth = vertexeval->get_ntracks_contribution(vertex,point);
	}
	  
	float vertex_data[10] = {_ievent,
				 vx,
				 vy,
				 vz,
				 ntracks,
				 gvx,
				 gvy,
				 gvz,
				 gntracks,
				 nfromtruth
	};

	_ntp_vertex->Fill(vertex_data);      
      }
    }
  }
  
  //-----------------------
  // fill the gpoint NTuple
  //-----------------------

  if (_ntp_gpoint) {
    SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");
    if (vertexmap && truthinfo) {

      PHG4VtxPoint* point =  truthinfo->GetPrimaryVtx(truthinfo->GetPrimaryVertexIndex());
      SvtxVertex* vertex = vertexeval->best_vertex_from(point);
    
      float gvx        = point->get_x();
      float gvy        = point->get_y();
      float gvz        = point->get_z();
      float gntracks   = truthinfo->GetNumPrimaryVertexParticles();
      float vx         = NAN;
      float vy         = NAN;
      float vz         = NAN;
      float ntracks    = NAN;
      float nfromtruth = NAN;

      if (vertex) {
	vx         = vertex->get_x();
	vy         = vertex->get_y();
	vz         = vertex->get_z();
	ntracks    = vertex->size_tracks();
	nfromtruth = vertexeval->get_ntracks_contribution(vertex,point);
      }
	
      float gpoint_data[10] = {_ievent,
			       gvx,
			       gvy,
			       gvz,
			       gntracks,
			       vx,
			       vy,
			       vz,
			       ntracks,
			       nfromtruth
      };

      _ntp_gpoint->Fill(gpoint_data);      
    }
  }
  
  //---------------------
  // fill the G4hit NTuple
  //---------------------

  if (_ntp_g4hit) {
    std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits();
    for (std::set<PHG4Hit*>::iterator iter = g4hits.begin();
	 iter != g4hits.end();
	 ++iter) {
            
      PHG4Hit *g4hit = *iter;
      PHG4Particle *g4particle = trutheval->get_particle(g4hit);
      
      float g4hitID   = g4hit->get_hit_id();
      float gx        = 0.5*(g4hit->get_x(0)+g4hit->get_x(1));
      float gy        = 0.5*(g4hit->get_y(0)+g4hit->get_y(1));
      float gz        = 0.5*(g4hit->get_z(0)+g4hit->get_z(1));
      float gedep     = g4hit->get_edep();
      float glayer    = g4hit->get_layer();
  
      float gtrackID  = g4hit->get_trkid();


      float gflavor   = NAN;
      float gpx       = NAN;
      float gpy       = NAN;
      float gpz       = NAN;

      float gvx       = NAN;
      float gvy       = NAN;
      float gvz       = NAN;

      float gembed  = NAN;
      float gprimary = NAN;

      float gfpx      = NULL;
      float gfpy      = NULL;
      float gfpz      = NULL;
      float gfx       = NULL;
      float gfy       = NULL;
      float gfz       = NULL;

      if (g4particle) {

	gflavor   = g4particle->get_pid();
	gpx       = g4particle->get_px();
	gpy       = g4particle->get_py();
	gpz       = g4particle->get_pz();

	PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);	

	if (vtx) {
	  gvx       = vtx->get_x();
	  gvy       = vtx->get_y();
	  gvz       = vtx->get_z();
	}
    
	PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	if (outerhit) {
	  gfpx      = outerhit->get_px(1);
	  gfpy      = outerhit->get_py(1);
	  gfpz      = outerhit->get_pz(1);
	  gfx       = outerhit->get_x(1);
	  gfy       = outerhit->get_y(1);
	  gfz       = outerhit->get_z(1);
	}
	
	gembed    = trutheval->get_embed(g4particle);
	gprimary  = trutheval->is_primary(g4particle);
      } //       if (g4particle)
      
      std::set<SvtxCluster*> clusters = clustereval->all_clusters_from(g4hit);  
      float nclusters = clusters.size();

      // best cluster reco'd
      SvtxCluster* cluster = clustereval->best_cluster_from(g4hit);

      float clusID     = NAN;
      float x          = NAN;
      float y          = NAN;
      float z          = NAN;
      float e          = NAN;
      float adc        = NAN;
      float layer      = NAN;
      float size       = NAN;
      float phisize    = NAN;
      float zsize      = NAN;
      float efromtruth = NAN;

      if (cluster) {
	clusID     = cluster->get_id();
	x          = cluster->get_x();
	y          = cluster->get_y();
	z          = cluster->get_z();
	e          = cluster->get_e();
	adc        = cluster->get_adc();
	layer      = cluster->get_layer();
	size       = cluster->size_hits();
	phisize    = cluster->get_phi_size();
	zsize      = cluster->get_z_size();
	if (g4particle) {
	  efromtruth = clustereval->get_energy_contribution(cluster,g4particle);
	}
      }

      float g4hit_data[35] = {_ievent,
			      g4hitID,
			      gx,
			      gy,
			      gz,
			      gedep,
			      glayer,
			      gtrackID,
			      gflavor,
			      gpx,
			      gpy,
			      gpz,
			      gvx,
			      gvy,
			      gvz,
			      gfpx,
			      gfpy,
			      gfpz,
			      gfx,
			      gfy,
			      gfz,
			      gembed,
			      gprimary,
			      nclusters,
			      clusID,  
			      x,      
			      y,      
			      z,      
			      e,      
			      adc,    
			      layer,  
			      size,   
			      phisize,
			      zsize,  
			      efromtruth
      };

      _ntp_g4hit->Fill(g4hit_data);
    }
  }
  
  //--------------------
  // fill the Hit NTuple
  //--------------------

  if (_ntp_hit) {
    // need things off of the DST...
    SvtxHitMap* hitmap = findNode::getClass<SvtxHitMap>(topNode,"SvtxHitMap");
    if (hitmap) {

      for (SvtxHitMap::Iter iter = hitmap->begin();
	   iter != hitmap->end();
	   ++iter) {

	SvtxHit* hit             = iter->second;
	PHG4Hit* g4hit           = hiteval->max_truth_hit_by_energy(hit);
	PHG4CylinderCell* g4cell = hiteval->get_cell(hit);
	PHG4Particle* g4particle = trutheval->get_particle(g4hit);

	float event  = _ievent;
	float hitID  = hit->get_id();
	float e      = hit->get_e();
	float adc    = hit->get_adc();
	float layer  = hit->get_layer();
	float cellID = hit->get_cellid();
	float ecell  = g4cell->get_edep();

	float g4hitID  = NAN;
	float gedep    = NAN;
	float gx       = NAN;
	float gy       = NAN;
	float gz       = NAN;
	float gtrackID = NAN;
	float gflavor  = NAN;
	float gpx      = NAN;
	float gpy      = NAN;
	float gpz      = NAN;
	float gvx      = NAN;
	float gvy      = NAN;
	float gvz      = NAN;
	float gfpx     = NAN;
	float gfpy     = NAN;
	float gfpz     = NAN;
	float gfx      = NAN;
	float gfy      = NAN;
	float gfz      = NAN;
	float glast    = NAN;
	float gembed   = NAN;
	float gprimary = NAN;
      
	float efromtruth = NAN;
      
	if (g4hit) {
	  g4hitID  = g4hit->get_hit_id();
	  gedep    = g4hit->get_edep();
	  gx       = 0.5*(g4hit->get_x(0)+g4hit->get_x(1));
	  gy       = 0.5*(g4hit->get_y(0)+g4hit->get_y(1));
	  gz       = 0.5*(g4hit->get_z(0)+g4hit->get_z(1));
	  gx       = g4hit->get_x(0);
	  gy       = g4hit->get_y(0);
	  gz       = g4hit->get_z(0);

	  if (g4particle) {

	    gtrackID = g4particle->get_track_id();
	    gflavor  = g4particle->get_pid();
	    gpx      = g4particle->get_px();
	    gpy      = g4particle->get_py();
	    gpz      = g4particle->get_pz();

	    PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);

	    if (vtx) {
	      gvx      = vtx->get_x();
	      gvy      = vtx->get_y();
	      gvz      = vtx->get_z();
	    }

	    PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	    if (outerhit) {
	      gfpx     = outerhit->get_px(1);
	      gfpy     = outerhit->get_py(1);
	      gfpz     = outerhit->get_pz(1);
	      gfx      = outerhit->get_x(1);
	      gfy      = outerhit->get_y(1);
	      gfz      = outerhit->get_z(1);
	    }
	    glast    = NAN;
	    gembed   = trutheval->get_embed(g4particle);
	    gprimary = trutheval->is_primary(g4particle);
	  } //   if (g4particle){
	}      

	if (g4particle) {
	  efromtruth = hiteval->get_energy_contribution(hit,g4particle);
	}

	float hit_data[32] = {
	  event,
	  hitID,
	  e,
	  adc,
	  layer,
	  cellID,
	  ecell,
	  g4hitID,
	  gedep,
	  gx,
	  gy,
	  gz,
	  gtrackID,
	  gflavor,
	  gpx,
	  gpy,
	  gpz,
	  gvx,
	  gvy,
	  gvz,
	  gfpx,
	  gfpy,
	  gfpz,
	  gfx,
	  gfy,
	  gfz,
	  gembed,
	  gprimary,
	  efromtruth
	};
	  
	_ntp_hit->Fill(hit_data);     
      }
    }
  }
  
  //------------------------
  // fill the Cluster NTuple
  //------------------------

  if (_ntp_cluster) {
    // need things off of the DST...
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (clustermap) {

      for (SvtxClusterMap::Iter iter = clustermap->begin();
	   iter != clustermap->end();
	   ++iter) {
    
	SvtxCluster* cluster     = iter->second;   
	PHG4Hit *g4hit           = clustereval->max_truth_hit_by_energy(cluster); 
	PHG4Particle *g4particle = trutheval->get_particle(g4hit);
    
	float hitID    = cluster->get_id();
	float x        = cluster->get_x();
	float y        = cluster->get_y();
	float z        = cluster->get_z();
	float e        = cluster->get_e();
	float adc      = cluster->get_adc();
	float layer    = cluster->get_layer();
	float size     = cluster->size_hits();
	float phisize  = cluster->get_phi_size();
	float zsize    = cluster->get_z_size();

	float g4hitID  = NAN;
	float gx       = NAN;
	float gy       = NAN;
	float gz       = NAN;
	float gtrackID = NAN;
	float gflavor  = NAN;
	float gpx      = NAN;
	float gpy      = NAN;
	float gpz      = NAN;
	float gvx      = NAN;
	float gvy      = NAN;
	float gvz      = NAN;
	float gfpx     = NAN;
	float gfpy     = NAN;
	float gfpz     = NAN;
	float gfx      = NAN;
	float gfy      = NAN;
	float gfz      = NAN;
	float glast    = NAN;
	float gembed   = NAN;
	float gprimary = NAN;
    
	float nhits      = NAN;
	float efromtruth = NAN;
      
	if (g4hit) {
	  g4hitID  = g4hit->get_hit_id();
	  gx       = 0.5*(g4hit->get_x(0)+g4hit->get_x(1));
	  gy       = 0.5*(g4hit->get_y(0)+g4hit->get_y(1));
	  gz       = 0.5*(g4hit->get_z(0)+g4hit->get_z(1));
	  gx       = g4hit->get_x(0);
	  gy       = g4hit->get_y(0);
	  gz       = g4hit->get_z(0);

	  if (g4particle) {

	    gtrackID = g4particle->get_track_id();
	    gflavor  = g4particle->get_pid();
	    gpx      = g4particle->get_px();
	    gpy      = g4particle->get_py();
	    gpz      = g4particle->get_pz();

	    PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);
	    if (vtx) {
	      gvx      = vtx->get_x();
	      gvy      = vtx->get_y();
	      gvz      = vtx->get_z();
	    }

	    PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	    if (outerhit) {
	      gfpx     = outerhit->get_px(1);
	      gfpy     = outerhit->get_py(1);
	      gfpz     = outerhit->get_pz(1);
	      gfx      = outerhit->get_x(1);
	      gfy      = outerhit->get_y(1);
	      gfz      = outerhit->get_z(1);
	    }
	    
	    glast    = NAN;
	    gembed   = trutheval->get_embed(g4particle);
	    gprimary = trutheval->is_primary(g4particle);
	  }      //   if (g4particle){
	} //  if (g4hit) {

	if (g4particle){
	  efromtruth = clustereval->get_energy_contribution(cluster,g4particle);
	}

	float cluster_data[33] = {_ievent,
				  hitID,
				  x,
				  y,
				  z,
				  e,
				  adc,
				  layer,
				  size,
				  phisize,
				  zsize,
				  g4hitID,
				  gx,
				  gy,
				  gz,
				  gtrackID,
				  gflavor,
				  gpx,
				  gpy,
				  gpz,
				  gvx,
				  gvy,
				  gvz,
				  gfpx,
				  gfpy,
				  gfpz,
				  gfx,
				  gfy,
				  gfz,
				  gembed,
				  gprimary,
				  nhits,
				  efromtruth};

	_ntp_cluster->Fill(cluster_data);
      }		  
    }
  }
  
  //------------------------
  // fill the Gtrack NTuple
  //------------------------

  // need things off of the DST...

  if (_ntp_gtrack) {
    PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");   
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (truthinfo) {

      PHG4TruthInfoContainer::ConstRange range = truthinfo->GetPrimaryParticleRange();
      for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
	   iter != range.second; 
	 ++iter) {
	
	PHG4Particle* g4particle = iter->second;
    
	float gtrackID = g4particle->get_track_id();
	float gflavor  = g4particle->get_pid();
      
	std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);     
	float ng4hits = g4hits.size();  
	float gpx      = g4particle->get_px();
	float gpy      = g4particle->get_py();
	float gpz      = g4particle->get_pz();

	PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);	
	float gvx      = vtx->get_x();
	float gvy      = vtx->get_y();
	float gvz      = vtx->get_z();

	float gfpx      = NULL;
	float gfpy      = NULL;
	float gfpz      = NULL;
	float gfx       = NULL;
	float gfy       = NULL;
	float gfz       = NULL;
    
	PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	
	if (outerhit) {
	  gfpx      = outerhit->get_px(1);
	  gfpy      = outerhit->get_py(1);
	  gfpz      = outerhit->get_pz(1);
	  gfx       = outerhit->get_x(1);
	  gfy       = outerhit->get_y(1);
	  gfz       = outerhit->get_z(1);
	}
      
	float gembed   = trutheval->get_embed(g4particle);
	float gprimary = trutheval->is_primary(g4particle);

	SvtxTrack* track = trackeval->best_track_from(g4particle);

	float trackID       = NAN;
	float charge        = NAN;
	float quality       = NAN;
	float chisq         = NAN;
	float ndf           = NAN;
	float nhits         = NAN;
	unsigned int layers = 0x0;
	float dca           = NAN;
	float dca2d         = NAN;
	float dca2dsigma    = NAN;
	float px            = NAN;
	float py            = NAN;
	float pz            = NAN;
	float pcax          = NAN;
	float pcay          = NAN;
	float pcaz          = NAN;

	float nfromtruth    = NAN;

	if (track) {
	  trackID   = track->get_id();     
	  charge    = track->get_charge();
	  quality   = track->get_quality();
	  chisq     = track->get_chisq();
	  ndf       = track->get_ndf();
	  nhits     = track->size_clusters();
	  
	  for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
	       iter != track->end_clusters();
	       ++iter) {
	    unsigned int cluster_id = *iter;
	    SvtxCluster* cluster = clustermap->get(cluster_id);
	    unsigned int layer = cluster->get_layer();
	    if (layer < 32) layers |= (0x1 << layer);
	  }

	  dca       = track->get_dca();
	  dca2d     = track->get_dca2d();
	  dca2dsigma = track->get_dca2d_error();
	  px        = track->get_px();
	  py        = track->get_py();
	  pz        = track->get_pz();
	  pcax      = track->get_x();
	  pcay      = track->get_y();
	  pcaz      = track->get_z();

	  nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);
	}
      
	float gtrack_data[34] = {_ievent,
				 gtrackID,
				 gflavor,
				 ng4hits,
				 gpx,
				 gpy,
				 gpz,
				 gvx,
				 gvy,
				 gvz,
				 gfpx,
				 gfpy,
				 gfpz,
				 gfx,
				 gfy,
				 gfz,
				 gembed,
				 gprimary,
				 trackID,
				 px,         
				 py,         
				 pz,  
				 charge,     
				 quality,    
				 chisq,      
				 ndf,        
				 nhits,      
				 layers,     
				 dca2d,      
				 dca2dsigma, 			       
				 pcax,       
				 pcay,       
				 pcaz,
				 nfromtruth
	};

	_ntp_gtrack->Fill(gtrack_data);
      }	     
    }
  }
  
  //------------------------
  // fill the Track NTuple
  //------------------------

  if (_ntp_track) {
    // need things off of the DST...
    SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (trackmap) {

      for (SvtxTrackMap::Iter iter = trackmap->begin();
	   iter != trackmap->end();
	   ++iter) {
    
	SvtxTrack* track = iter->second;

	float trackID   = track->get_id();     
	float charge    = track->get_charge();
	float quality   = track->get_quality();
	float chisq     = track->get_chisq();
	float ndf       = track->get_ndf();
	float nhits     = track->size_clusters();

	unsigned int layers = 0x0;
	for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
	     iter != track->end_clusters();
	     ++iter) {
	  unsigned int cluster_id = *iter;
	  SvtxCluster* cluster = clustermap->get(cluster_id);
	  unsigned int layer = cluster->get_layer();
	  if (layer < 32) layers |= (0x1 << layer);
	}
      
	float dca2d     = track->get_dca2d();
	float dca2dsigma = track->get_dca2d_error();
	float px        = track->get_px();
	float py        = track->get_py();
	float pz        = track->get_pz();
	float pcax      = track->get_x();
	float pcay      = track->get_y();
	float pcaz      = track->get_z();

	float presdphi = track->get_cal_dphi(SvtxTrack::PRES);
	float presdeta = track->get_cal_deta(SvtxTrack::PRES);
	float prese3x3 = track->get_cal_energy_3x3(SvtxTrack::PRES);
	float prese    = track->get_cal_cluster_e(SvtxTrack::PRES);

	float cemcdphi = track->get_cal_dphi(SvtxTrack::CEMC);
	float cemcdeta = track->get_cal_deta(SvtxTrack::CEMC);
	float cemce3x3 = track->get_cal_energy_3x3(SvtxTrack::CEMC);
	float cemce    = track->get_cal_cluster_e(SvtxTrack::CEMC);

	float hcalindphi = track->get_cal_dphi(SvtxTrack::HCALIN);
	float hcalindeta = track->get_cal_deta(SvtxTrack::HCALIN);
	float hcaline3x3 = track->get_cal_energy_3x3(SvtxTrack::HCALIN);
	float hcaline    = track->get_cal_cluster_e(SvtxTrack::HCALIN);

	float hcaloutdphi = track->get_cal_dphi(SvtxTrack::HCALOUT);
	float hcaloutdeta = track->get_cal_deta(SvtxTrack::HCALOUT);
	float hcaloute3x3 = track->get_cal_energy_3x3(SvtxTrack::HCALOUT);
	float hcaloute    = track->get_cal_cluster_e(SvtxTrack::HCALOUT);

	float gtrackID = NAN;
	float gflavor  = NAN;     
	float ng4hits  = NAN;
	float gpx      = NAN;
	float gpy      = NAN;
	float gpz      = NAN;
	float gvx      = NAN;
	float gvy      = NAN;
	float gvz      = NAN;
	float gfpx     = NAN;
	float gfpy     = NAN;
	float gfpz     = NAN;
	float gfx      = NAN;
	float gfy      = NAN;
	float gfz      = NAN;
	float gembed   = NAN;
	float gprimary = NAN;

	float nfromtruth = NAN;
      
	PHG4Particle* g4particle = trackeval->max_truth_particle_by_nclusters(track);
      
	if (g4particle) {
	  gtrackID = g4particle->get_track_id();
	  gflavor  = g4particle->get_pid();
      
	  std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);     
	  ng4hits = g4hits.size();  
	  gpx      = g4particle->get_px();
	  gpy      = g4particle->get_py();
	  gpz      = g4particle->get_pz();
	
	  PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);	
	  gvx      = vtx->get_x();
	  gvy      = vtx->get_y();
	  gvz      = vtx->get_z();

	  PHG4Hit* outerhit = trutheval->get_outermost_truth_hit(g4particle);	      
	  if (outerhit) {
	    gfpx     = outerhit->get_px(1);
	    gfpy     = outerhit->get_py(1);
	    gfpz     = outerhit->get_pz(1);
	    gfx      = outerhit->get_x(1);
	    gfy      = outerhit->get_y(1);
	    gfz      = outerhit->get_z(1);
	  }
	  gembed   = trutheval->get_embed(g4particle);
	  gprimary = trutheval->is_primary(g4particle);

	  nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);
	}
      
	float track_data[50] = {_ievent,
				trackID, 
				px,        
				py,        
				pz,      
				charge,  
				quality, 
				chisq,   
				ndf,     
				nhits,   
				layers,
				dca2d,     
				dca2dsigma,      
				pcax,      
				pcay,      
				pcaz,      
				presdphi,
				presdeta,
				prese3x3,
				prese,   
				cemcdphi,
				cemcdeta,
				cemce3x3,
				cemce,   
				hcalindphi,
				hcalindeta,
				hcaline3x3,
				hcaline,   
				hcaloutdphi,
				hcaloutdeta,
				hcaloute3x3,
				hcaloute,
				gtrackID,
				gflavor,
				ng4hits,
				gpx,
				gpy,
				gpz,
				gvx,
				gvy,
				gvz,
				gfpx,
				gfpy,
				gfpz,
				gfx,
				gfy,
				gfz,
				gembed,
				gprimary,
				nfromtruth
	};
      
	_ntp_track->Fill(track_data);
      }
    }
  }
  
  return;
}
void PHG4SvtxClusterizer::ClusterLadderCells(PHCompositeNode *topNode) {

  //----------
  // Get Nodes
  //----------

  // get the SVX geometry object
  PHG4CylinderGeomContainer* geom_container = findNode::getClass<PHG4CylinderGeomContainer>(topNode,"CYLINDERGEOM_SILICON_TRACKER");
  if (!geom_container) return;
  
  PHG4HitContainer* g4hits =  findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_SILICON_TRACKER");
  if (!g4hits) return;
  
  PHG4CylinderCellContainer* cells =  findNode::getClass<PHG4CylinderCellContainer>(topNode,"G4CELL_SILICON_TRACKER");
  if (!cells) return; 
 
  //-----------
  // Clustering
  //-----------

  // sort hits layer by layer
  std::multimap<int,SvtxHit*> layer_hits_mmap;
  for (SvtxHitMap::Iter iter = _hits->begin();
       iter != _hits->end();
       ++iter) {
    SvtxHit* hit = &iter->second;
    layer_hits_mmap.insert(make_pair(hit->get_layer(),hit));
  }
  
  PHG4CylinderGeomContainer::ConstRange layerrange = geom_container->get_begin_end();
  for(PHG4CylinderGeomContainer::ConstIterator layeriter = layerrange.first;
      layeriter != layerrange.second;
      ++layeriter) {

    int layer = layeriter->second->get_layer();

    std::map<PHG4CylinderCell*,SvtxHit*> cell_hit_map;
    vector<PHG4CylinderCell*> cell_list;
    for (std::multimap<int,SvtxHit*>::iterator hiter = layer_hits_mmap.lower_bound(layer);
	 hiter != layer_hits_mmap.upper_bound(layer);
	 ++hiter) {
      SvtxHit* hit = hiter->second;
      PHG4CylinderCell* cell = cells->findCylinderCell(hit->get_cellid());
      cell_list.push_back(cell);
      cell_hit_map.insert(make_pair(cell,hit));
    }
    
    if (cell_list.size() == 0) continue; // if no cells, go to the next layer
    
    // i'm not sure this sorting is ever really used
    sort(cell_list.begin(), cell_list.end(), PHG4SvtxClusterizer::ladder_lessthan);

    typedef adjacency_list <vecS, vecS, undirectedS> Graph;
    typedef graph_traits<Graph>::vertex_descriptor Vertex;
    Graph G;

    for(unsigned int i=0; i<cell_list.size(); i++) {
      for(unsigned int j=i+1; j<cell_list.size(); j++) {
        if(ladder_are_adjacent(cell_list[i], cell_list[j]) )
          add_edge(i,j,G);
      }
      
      add_edge(i,i,G);
    }

    // Find the connections between the vertices of the graph (vertices are the rawhits, 
    // connections are made when they are adjacent to one another)
    vector<int> component(num_vertices(G));
    
    // this is the actual clustering, performed by boost
    connected_components(G, &component[0]); 

    // Loop over the components(hit cells) compiling a list of the
    // unique connected groups (ie. clusters).
    set<int> cluster_ids; // unique components
    multimap<int, PHG4CylinderCell*> clusters;
    for (unsigned int i=0; i<component.size(); i++) {
      cluster_ids.insert( component[i] );
      clusters.insert( make_pair(component[i], cell_list[i]) );
    }
    
    // 
    for (set<int>::iterator clusiter = cluster_ids.begin(); 
	 clusiter != cluster_ids.end(); 
	 clusiter++ ) {
      
      int clusid = *clusiter;
      pair<multimap<int, PHG4CylinderCell*>::iterator,
	   multimap<int, PHG4CylinderCell*>::iterator> clusrange = clusters.equal_range(clusid);
      
      multimap<int, PHG4CylinderCell*>::iterator mapiter = clusrange.first;
      
      int layer = mapiter->second->get_layer();
      PHG4CylinderGeom* geom = geom_container->GetLayerGeom(layer);
      
      SvtxCluster clus;
      clus.set_layer( layer );
      float clus_energy = 0.0;
      unsigned int clus_adc = 0;

      // determine the size of the cluster in phi and z
      // useful for track fitting the cluster

      set<int> phibins;
      set<int> zbins;
      for (mapiter = clusrange.first; mapiter != clusrange.second; mapiter++ ) {
	PHG4CylinderCell* cell = mapiter->second;     
	
	phibins.insert(cell->get_binphi());
	zbins.insert(cell->get_binz());
      }

      float thickness = geom->get_thickness();
      float pitch = geom->get_strip_y_spacing();
      float length = geom->get_strip_z_spacing();
      float phisize = phibins.size()*pitch;
      float zsize = zbins.size()*length;
      float tilt = geom->get_strip_tilt();

      // determine the cluster position...
      double xsum = 0.0;
      double ysum = 0.0;
      double zsum = 0.0;
      unsigned nhits = 0;

      int ladder_z_index = -1;
      int ladder_phi_index = -1;
      
      for(mapiter = clusrange.first; mapiter != clusrange.second; mapiter++ ) {
        PHG4CylinderCell* cell = mapiter->second;
	SvtxHit* hit = cell_hit_map[cell];
	
	clus.insert_hit(hit->get_id());
	
        clus_energy += hit->get_e();
	clus_adc    += hit->get_adc();

	double hit_location[3] = {0.0,0.0,0.0};
	geom->find_strip_center(cell->get_ladder_z_index(),
				cell->get_ladder_phi_index(),
				cell->get_binz(),
				cell->get_binphi(),
				hit_location);

	ladder_z_index = cell->get_ladder_z_index();
	ladder_phi_index = cell->get_ladder_phi_index();

	if (_make_e_weights[layer]) {
	  xsum += hit_location[0] * hit->get_adc();
	  ysum += hit_location[1] * hit->get_adc();
	  zsum += hit_location[2] * hit->get_adc();  
	} else {
	  xsum += hit_location[0];
	  ysum += hit_location[1];
	  zsum += hit_location[2];
	}
	++nhits;
      }

      double clusx = NAN;
      double clusy = NAN;
      double clusz = NAN;

      if (_make_e_weights[layer]) {
	clusx = xsum / clus_adc;
	clusy = ysum / clus_adc;
	clusz = zsum / clus_adc;	
      } else {
	clusx = xsum / nhits;
	clusy = ysum / nhits;
	clusz = zsum / nhits;
      }
      
      double ladder_location[3] = {0.0,0.0,0.0};
      geom->find_segment_center(ladder_z_index,
				ladder_phi_index,
				ladder_location);
      double ladderphi = atan2( ladder_location[1], ladder_location[0] );
      
      clus.set_position(0, clusx);
      clus.set_position(1, clusy);
      clus.set_position(2, clusz);

      clus.set_e(clus_energy);
      clus.set_adc(clus_adc);

      float invsqrt12 = 1.0/sqrt(12.0);
      
      TMatrixF DIM(3,3);
      DIM[0][0] = pow(0.5*thickness,2);
      DIM[0][1] = 0.0;
      DIM[0][2] = 0.0;
      DIM[1][0] = 0.0;
      DIM[1][1] = pow(0.5*phisize,2);
      DIM[1][2] = 0.0;
      DIM[2][0] = 0.0;
      DIM[2][1] = 0.0;
      DIM[2][2] = pow(0.5*zsize,2);

      TMatrixF ERR(3,3);
      ERR[0][0] = pow(0.5*thickness*invsqrt12,2);
      ERR[0][1] = 0.0;
      ERR[0][2] = 0.0;
      ERR[1][0] = 0.0;
      ERR[1][1] = pow(0.5*phisize*invsqrt12,2);
      ERR[1][2] = 0.0;
      ERR[2][0] = 0.0;
      ERR[2][1] = 0.0;
      ERR[2][2] = pow(0.5*zsize*invsqrt12,2);
      
      TMatrixF ROT(3,3);
      ROT[0][0] = cos(ladderphi);
      ROT[0][1] = -1.0*sin(ladderphi);
      ROT[0][2] = 0.0;
      ROT[1][0] = sin(ladderphi);
      ROT[1][1] = cos(ladderphi);
      ROT[1][2] = 0.0;
      ROT[2][0] = 0.0;
      ROT[2][1] = 0.0;
      ROT[2][2] = 1.0;

      TMatrixF TILT(3,3);
      TILT[0][0] = 1.0;
      TILT[0][1] = 0.0;
      TILT[0][2] = 0.0;
      TILT[1][0] = 0.0;
      TILT[1][1] = cos(tilt);
      TILT[1][2] = -1.0*sin(tilt);
      TILT[2][0] = 0.0;
      TILT[2][1] = sin(tilt);
      TILT[2][2] = cos(tilt);

      TMatrixF R(3,3);
      R = ROT * TILT;
      
      TMatrixF R_T(3,3);
      R_T.Transpose(R);
          
      TMatrixF COVAR_DIM(3,3);
      COVAR_DIM = R * DIM * R_T;
      
      clus.set_size( 0 , 0 , COVAR_DIM[0][0] );
      clus.set_size( 0 , 1 , COVAR_DIM[0][1] );
      clus.set_size( 0 , 2 , COVAR_DIM[0][2] );
      clus.set_size( 1 , 0 , COVAR_DIM[1][0] );
      clus.set_size( 1 , 1 , COVAR_DIM[1][1] );
      clus.set_size( 1 , 2 , COVAR_DIM[1][2] );
      clus.set_size( 2 , 0 , COVAR_DIM[2][0] );
      clus.set_size( 2 , 1 , COVAR_DIM[2][1] );
      clus.set_size( 2 , 2 , COVAR_DIM[2][2] );

      TMatrixF COVAR_ERR(3,3);
      COVAR_ERR = R * ERR * R_T;
      
      clus.set_error( 0 , 0 , COVAR_ERR[0][0] );
      clus.set_error( 0 , 1 , COVAR_ERR[0][1] );
      clus.set_error( 0 , 2 , COVAR_ERR[0][2] );
      clus.set_error( 1 , 0 , COVAR_ERR[1][0] );
      clus.set_error( 1 , 1 , COVAR_ERR[1][1] );
      clus.set_error( 1 , 2 , COVAR_ERR[1][2] );
      clus.set_error( 2 , 0 , COVAR_ERR[2][0] );
      clus.set_error( 2 , 1 , COVAR_ERR[2][1] );
      clus.set_error( 2 , 2 , COVAR_ERR[2][2] );
      
      if (clus_energy > get_threshold_by_layer(layer)) {
	SvtxCluster* ptr = _clusterlist->insert(clus);
	if (!ptr->IsValid()) {
	  static bool first = true;
	  if (first) {
	    cout << PHWHERE << "ERROR: Invalid SvtxClusters are being produced" << endl;
	    ptr->identify();
	    first = false;
	  }
	}
	
	if (verbosity>1) {
	  double radius = sqrt(clusx*clusx+clusy*clusy);
	  double clusphi = atan2(clusy,clusx);
	  cout << "r=" << radius << " phi=" << clusphi << " z=" << clusz << endl;
	  cout << "pos=(" << clus.get_position(0) << ", " << clus.get_position(1)
	       << ", " << clus.get_position(2) << ")" << endl;
	  cout << endl;
	}
      }	else if (verbosity>1) {
	double radius = sqrt(clusx*clusx+clusy*clusy);
	double clusphi = atan2(clusy,clusx);
	cout << "removed r=" << radius << " phi=" << clusphi << " z=" << clusz << endl;
	cout << "pos=(" << clus.get_position(0) << ", " << clus.get_position(1)
	     << ", " << clus.get_position(2) << ")" << endl;
	cout << endl;
      } 
    }
  }
  
  return;
}
int PHG4TPCClusterizer::process_event(PHCompositeNode* topNode) {

  PHNodeIterator iter(topNode);

  PHCompositeNode* dstNode =
      static_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "DST"));
  if (!dstNode) {
    cout << PHWHERE << "DST Node missing, doing nothing." << endl;
    return Fun4AllReturnCodes::ABORTRUN;
  }
  PHNodeIterator iter_dst(dstNode);

  SvtxHitMap* hits = findNode::getClass<SvtxHitMap>(dstNode, "SvtxHitMap");
  if (!hits) {
    cout << PHWHERE << "ERROR: Can't find node SvtxHitMap" << endl;
    return Fun4AllReturnCodes::ABORTRUN;
  }

  PHCompositeNode* svxNode =
      dynamic_cast<PHCompositeNode*>(iter_dst.findFirst("PHCompositeNode", "SVTX"));
  if (!svxNode) {
    svxNode = new PHCompositeNode("SVTX");
    dstNode->addNode(svxNode);
  }

  SvtxClusterMap* svxclusters =
      findNode::getClass<SvtxClusterMap>(dstNode, "SvtxClusterMap");
  if (!svxclusters) {
    svxclusters = new SvtxClusterMap_v1();
    PHIODataNode<PHObject>* SvtxClusterMapNode =
        new PHIODataNode<PHObject>(svxclusters, "SvtxClusterMap", "PHObject");
    svxNode->addNode(SvtxClusterMapNode);
  }

  PHG4CylinderCellGeomContainer* geom_container =
    findNode::getClass<PHG4CylinderCellGeomContainer>(topNode,"CYLINDERCELLGEOM_SVTX");
  if (!geom_container) return Fun4AllReturnCodes::ABORTRUN;

  PHG4CylinderCellContainer* cells =  findNode::getClass<PHG4CylinderCellContainer>(dstNode,"G4CELL_SVTX");
  if (!cells) return Fun4AllReturnCodes::ABORTRUN;

  std::vector<std::vector<const SvtxHit*> > layer_sorted;
  PHG4CylinderCellGeomContainer::ConstRange layerrange = geom_container->get_begin_end();
  for (PHG4CylinderCellGeomContainer::ConstIterator layeriter = layerrange.first;
       layeriter != layerrange.second;
       ++layeriter) {
    // We only need TPC layers here, so skip the layers below _min_layer
    // This if statement is needed because although the maps ladder layers are not included in the cylinder cell geom container, 
    // the cylinder Svx layers are, so they have to be dropped here if they are present
    if( (unsigned int) layeriter->second->get_layer() < _min_layer)
      continue;
    layer_sorted.push_back(std::vector<const SvtxHit*>());
  }
  for (SvtxHitMap::Iter iter = hits->begin(); iter != hits->end(); ++iter) {
    SvtxHit* hit = iter->second;
    if( (unsigned int) hit->get_layer() < _min_layer)
      continue;
    layer_sorted[hit->get_layer() - _min_layer].push_back(hit);
  }
  
  for (PHG4CylinderCellGeomContainer::ConstIterator layeriter =
           layerrange.first;
       layeriter != layerrange.second; ++layeriter) {

    unsigned int layer = (unsigned int)layeriter->second->get_layer();
    
    // exit on the MAPS layers...
    // needed in case cylinder svtx layers are present      
    if (layer < _min_layer) continue;
    if (layer > _max_layer) continue;
    
    PHG4CylinderCellGeom* geo = geom_container->GetLayerCellGeom(layer);
    const int nphibins = layeriter->second->get_phibins();
    const int nzbins = layeriter->second->get_zbins();

    nhits.clear();
    nhits.assign(nzbins, 0);
    amps.clear();
    amps.assign(nphibins * nzbins, 0.);
    cellids.clear();
    cellids.assign(nphibins * nzbins, 0);

    for (unsigned int i = 0; i < layer_sorted[layer - _min_layer].size(); ++i) {

      const SvtxHit* hit = layer_sorted[layer - _min_layer][i];
      if (hit->get_e() <= 0.) continue;
      
      PHG4CylinderCell* cell = cells->findCylinderCell(hit->get_cellid());
      int phibin = cell->get_binphi();
      int zbin = cell->get_binz();
      nhits[zbin] += 1;
      amps[zbin * nphibins + phibin] += hit->get_e();
      cellids[zbin * nphibins + phibin] = hit->get_id();
    }

    int nhits_tot = 0;
    for (int zbin = 0; zbin < nzbins; ++zbin) {
      nhits_tot += nhits[zbin];
    }

    while (nhits_tot > 0) {

      for (int zbin = 0; zbin < nzbins; ++zbin) {

        if (nhits[zbin] <= 0) continue;

        for (int phibin = 0; phibin < nphibins; ++phibin) {

          if (is_local_maximum(amps, nphibins, nzbins, phibin, zbin) == false) {
            continue;
          }

          float phi = 0.;
          float z = 0.;
          float e = 0.;

          fit_cluster(amps, nphibins, nzbins, nhits_tot, nhits, phibin, zbin,
                      geo, phi, z, e);

          if ((layer > 2) && (e < energy_cut)) {
            continue;
          }

          SvtxCluster_v1 clus;
          clus.set_layer(layer);
          clus.set_e(e);
          double radius = geo->get_radius() + 0.5*geo->get_thickness();
          clus.set_position(0, radius * cos(phi));
          clus.set_position(1, radius * sin(phi));
          clus.set_position(2, z);
	  
          clus.insert_hit(cellids[zbin * nphibins + phibin]);

	  float invsqrt12 = 1.0/sqrt(12.);
      
	  TMatrixF DIM(3,3);
	  DIM[0][0] = 0.0;//pow(0.0*0.5*thickness,2);
	  DIM[0][1] = 0.0;
	  DIM[0][2] = 0.0;
	  DIM[1][0] = 0.0;
	  DIM[1][1] = pow(0.5*0.011,2);
	  DIM[1][2] = 0.0;
	  DIM[2][0] = 0.0;
	  DIM[2][1] = 0.0;
	  DIM[2][2] = pow(0.5*0.03,2);

	  TMatrixF ERR(3,3);
	  ERR[0][0] = 0.0;//pow(0.0*0.5*thickness*invsqrt12,2);
	  ERR[0][1] = 0.0;
	  ERR[0][2] = 0.0;
	  ERR[1][0] = 0.0;
	  ERR[1][1] = pow(0.5*0.011*invsqrt12,2);
	  ERR[1][2] = 0.0;
	  ERR[2][0] = 0.0;
	  ERR[2][1] = 0.0;
	  ERR[2][2] = pow(0.5*0.03*invsqrt12,2);

	  TMatrixF ROT(3,3);
	  ROT[0][0] = cos(phi);
	  ROT[0][1] = -sin(phi);
	  ROT[0][2] = 0.0;
	  ROT[1][0] = sin(phi);
	  ROT[1][1] = cos(phi);
	  ROT[1][2] = 0.0;
	  ROT[2][0] = 0.0;
	  ROT[2][1] = 0.0;
	  ROT[2][2] = 1.0;

	  TMatrixF ROT_T(3,3);
	  ROT_T.Transpose(ROT);
      
	  TMatrixF COVAR_DIM(3,3);
	  COVAR_DIM = ROT * DIM * ROT_T;
	  
	  clus.set_size( 0 , 0 , COVAR_DIM[0][0] );
	  clus.set_size( 0 , 1 , COVAR_DIM[0][1] );
	  clus.set_size( 0 , 2 , COVAR_DIM[0][2] );
	  clus.set_size( 1 , 0 , COVAR_DIM[1][0] );
	  clus.set_size( 1 , 1 , COVAR_DIM[1][1] );
	  clus.set_size( 1 , 2 , COVAR_DIM[1][2] );
	  clus.set_size( 2 , 0 , COVAR_DIM[2][0] );
	  clus.set_size( 2 , 1 , COVAR_DIM[2][1] );
	  clus.set_size( 2 , 2 , COVAR_DIM[2][2] );

	  TMatrixF COVAR_ERR(3,3);
	  COVAR_ERR = ROT * ERR * ROT_T;
	  
	  clus.set_error( 0 , 0 , COVAR_ERR[0][0] );
	  clus.set_error( 0 , 1 , COVAR_ERR[0][1] );
	  clus.set_error( 0 , 2 , COVAR_ERR[0][2] );
	  clus.set_error( 1 , 0 , COVAR_ERR[1][0] );
	  clus.set_error( 1 , 1 , COVAR_ERR[1][1] );
	  clus.set_error( 1 , 2 , COVAR_ERR[1][2] );
	  clus.set_error( 2 , 0 , COVAR_ERR[2][0] );
	  clus.set_error( 2 , 1 , COVAR_ERR[2][1] );
	  clus.set_error( 2 , 2 , COVAR_ERR[2][2] );
      
          svxclusters->insert(&clus);
        }
      }
    }
  }

  reset();
  return Fun4AllReturnCodes::EVENT_OK;
}