int PHG4SvtxMomentumRecal::process_event(PHCompositeNode *topNode)
{
  if(Verbosity() > 1) cout << "PHG4SvtxMomentumRecal::process_event -- entered" << endl;

  if (!_corr) return Fun4AllReturnCodes::EVENT_OK;
  
  //---------------------------------
  // Get Objects off of the Node Tree
  //---------------------------------

  // Pull the reconstructed track information off the node tree...
  SvtxTrackMap* _g4tracks = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
  if (!_g4tracks) {
    cerr << PHWHERE << " ERROR: Can't find SvtxTrackMap." << endl;
    return Fun4AllReturnCodes::ABORTRUN;
  }
    
  // loop over all tracks
  for (SvtxTrackMap::Iter iter = _g4tracks->begin();
       iter != _g4tracks->end();
       ++iter) {
    SvtxTrack *track = iter->second;
    
    double rescale = 1.0;

    double pt = track->get_pt();
    
    double xmin = 0.0;
    double xmax = 0.0;
    _corr->GetRange(xmin,xmax);
      
    if ((pt > xmin)&&(pt < xmax)) {
      rescale = _corr->Eval(pt);
    }

    track->set_px( track->get_px() * rescale );
    track->set_py( track->get_py() * rescale );
    track->set_pz( track->get_pz() * rescale );
  } // end track loop

  if (Verbosity() > 1) cout << "PHG4SvtxMomentumRecal::process_event -- exited" << endl;

  return Fun4AllReturnCodes::EVENT_OK;
}
int PHG4SvtxTrackProjection::process_event(PHCompositeNode *topNode)
{
  if(verbosity > 1) cout << "PHG4SvtxTrackProjection::process_event -- entered" << endl;

  //---------------------------------
  // Get Objects off of the Node Tree
  //---------------------------------

  // Pull the reconstructed track information off the node tree...
  SvtxTrackMap* _g4tracks = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
  if (!_g4tracks) {
    cerr << PHWHERE << " ERROR: Can't find SvtxTrackMap." << endl;
    return Fun4AllReturnCodes::ABORTRUN;
  }

  for (int i=0;i<_num_cal_layers;++i) {

    if (isnan(_cal_radii[i])) continue;

    if (verbosity > 1) cout << "Projecting tracks into: " << _cal_names[i] << endl;

    // pull the tower geometry
    string towergeonodename = "TOWERGEOM_" + _cal_names[i];
    RawTowerGeom *towergeo = findNode::getClass<RawTowerGeom>(topNode,towergeonodename.c_str());
    if (!towergeo) {
      cerr << PHWHERE << " ERROR: Can't find node " << towergeonodename << endl;
      return Fun4AllReturnCodes::ABORTRUN;
    }

    // pull the towers
    string towernodename = "TOWER_CALIB_" + _cal_names[i];
    RawTowerContainer *towerList = findNode::getClass<RawTowerContainer>(topNode,towernodename.c_str());
    if (!towerList) {
      cerr << PHWHERE << " ERROR: Can't find node " << towernodename << endl;
      return Fun4AllReturnCodes::ABORTRUN;
    }

    // pull the clusters
    string clusternodename = "CLUSTER_" + _cal_names[i];
    RawClusterContainer *clusterList = findNode::getClass<RawClusterContainer>(topNode,clusternodename.c_str());
    if (!clusterList) {
      cerr << PHWHERE << " ERROR: Can't find node " << clusternodename << endl;
      return Fun4AllReturnCodes::ABORTRUN;
    }    
    
    // loop over all tracks
    for (SvtxTrackMap::Iter iter = _g4tracks->begin();
	 iter != _g4tracks->end();
	 ++iter) {
      SvtxTrack *track = iter->second;

      if (verbosity > 1) cout << "projecting track id " << track->get_id() << endl;

      if (verbosity > 1) {
	cout << " track pt = " << track->get_pt() << endl;
      }

      // curved tracks inside mag field
      // straight projections thereafter
      std::vector<double> point;
      point.assign(3,-9999.);
      //if (_cal_radii[i] < _mag_extent) {
      // curved projections inside field

      _hough.projectToRadius(track,_magfield,_cal_radii[i],point);

      if (isnan(point[0])) continue;
      if (isnan(point[1])) continue;
      if (isnan(point[2])) continue;
      // } else {
      // 	// straight line projections after mag field exit
      // 	_hough.projectToRadius(track,_mag_extent-0.05,point);
      // 	if (isnan(point[0])) continue;
      // 	if (isnan(point[1])) continue;
      // 	if (isnan(point[2])) continue;

      // 	std::vector<double> point2;
      // 	point2.assign(3,-9999.);
      // 	_hough.projectToRadius(track,_mag_extent+0.05,point2);
      // 	if (isnan(point2[0])) continue;
      // 	if (isnan(point2[1])) continue;
      // 	if (isnan(point2[2])) continue;

      // 	// find intersection of r and z


      // find x,y of intersection
      //}
      double x = point[0];
      double y = point[1];
      double z = point[2];

      double phi = atan2(y,x);
      double eta = asinh(z/sqrt(x*x+y*y));

      if (verbosity > 1) {
	cout << " initial track phi = " << track->get_phi();
	cout << ", eta = " << track->get_eta() << endl;
	cout << " calorimeter phi = " << phi << ", eta = " << eta << endl;
      }

      // projection is outside the detector extent
      // \todo towergeo doesn't make this easy to extract, but this should be
      // fetched from the node tree instead of hardcoded
      if (fabs(eta) >= 1.0) continue;

      // calculate 3x3 tower energy
      int binphi = towergeo->get_phibin(phi);
      int bineta = towergeo->get_etabin(eta);

      double energy_3x3 = 0.0;
      for (int iphi = binphi-1; iphi < binphi+2; ++iphi) { 
      	for (int ieta = bineta-1; ieta < bineta+2; ++ieta) { 

	  // wrap around
	  int wrapphi = iphi;
	  if (wrapphi < 0) {
	    wrapphi = towergeo->get_phibins() + wrapphi;
	  }
	  if (wrapphi >= towergeo->get_phibins()) {
	    wrapphi = wrapphi - towergeo->get_phibins();
	  }

	  // edges
	  if (ieta < 0) continue;
	  if (ieta >= towergeo->get_etabins()) continue;

	  RawTower* tower = towerList->getTower(ieta,wrapphi);
	  if (tower) {
	    energy_3x3 += tower->get_energy();

	    if (verbosity > 1) cout << " tower " << ieta << " " << wrapphi << " energy = " << tower->get_energy() << endl;
	  }
      	}
      }

      track->set_cal_energy_3x3(_cal_types[i],energy_3x3);

      // loop over all clusters and find nearest
      double min_r = DBL_MAX;
      double min_index = -9999;
      double min_dphi = NAN;
      double min_deta = NAN;
      double min_e = NAN;
      for (unsigned int k = 0; k < clusterList->size(); ++k) {

	RawCluster *cluster = clusterList->getCluster(k);

	double dphi = atan2(sin(phi-cluster->get_phi()),cos(phi-cluster->get_phi()));
	double deta = eta-cluster->get_eta();
	double r = sqrt(pow(dphi,2)+pow(deta,2));

	if (r < min_r) {
	  min_index = k;
	  min_r = r;
	  min_dphi = dphi;
	  min_deta = deta;
	  min_e = cluster->get_energy();
	}
      }

      if (min_index != -9999) {
	track->set_cal_dphi(_cal_types[i],min_dphi);
	track->set_cal_deta(_cal_types[i],min_deta);
	track->set_cal_cluster_id(_cal_types[i],min_index);
	track->set_cal_cluster_e(_cal_types[i],min_e);

	if (verbosity > 1) {
	  cout << " nearest cluster dphi = " << min_dphi << " deta = " << min_deta << " e = " << min_e << endl;
	}
      }

    } // end track loop
  } // end calorimeter layer loop

 
  if(verbosity > 1) cout << "PHG4SvtxTrackProjection::process_event -- exited" << endl;

  return Fun4AllReturnCodes::EVENT_OK;
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
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;
}
Esempio n. 5
0
void SvtxEvaluator::printInputInfo(PHCompositeNode *topNode) {
  
  if (verbosity > 1) cout << "SvtxEvaluator::printInputInfo() entered" << endl;

  if (verbosity > 3) {
    
    // event information
    cout << endl;
    cout << PHWHERE << "   INPUT FOR EVENT " << _ievent << endl;

    cout << endl;
    cout << "---PHG4HITS-------------" << endl;
    _svtxevalstack->get_truth_eval()->set_strict(_strict);
    std::set<PHG4Hit*> g4hits = _svtxevalstack->get_truth_eval()->all_truth_hits();
    unsigned int ig4hit = 0;
    for(std::set<PHG4Hit*>::iterator iter = g4hits.begin();
	iter != g4hits.end();
	++iter) {
      PHG4Hit *g4hit = *iter;
      cout << ig4hit << " of " << g4hits.size();
      cout << ": PHG4Hit: " << endl;
      g4hit->identify();
      ++ig4hit;
    }

    cout << "---SVTXCLUSTERS-------------" << endl;
    SvtxClusterMap* clustermap = findNode::getClass<SvtxClusterMap>(topNode,"SvtxClusterMap");
    if (clustermap) {
      unsigned int icluster = 0;
      for (SvtxClusterMap::Iter iter = clustermap->begin();
	   iter != clustermap->end();
	   ++iter) {
	SvtxCluster* cluster = iter->second;
	cout << icluster << " of " << clustermap->size();	  
	cout << ": SvtxCluster: " << endl;
	cluster->identify();
	++icluster;
      }
    }

    cout << "---SVXTRACKS-------------" << endl;
    SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
    if (trackmap) {
      unsigned int itrack = 0;
      for (SvtxTrackMap::Iter iter = trackmap->begin();
	   iter != trackmap->end();
	   ++iter) {
	cout << itrack << " of " << trackmap->size();
	SvtxTrack *track = iter->second;
	cout << " : SvtxTrack:" << endl;
	track->identify();
	cout << endl;
      }
    }
    
    cout << "---SVXVERTEXES-------------" << endl;
    SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
    if (vertexmap) {
      unsigned int ivertex = 0;
      for (SvtxVertexMap::Iter iter = vertexmap->begin();
	   iter != vertexmap->end();
	   ++iter) {
	cout << ivertex << " of " << vertexmap->size();
	SvtxVertex *vertex = iter->second;
	cout << " : SvtxVertex:" << endl;
	vertex->identify();
	cout << endl;
      }
    }
  }

  return;
}
Esempio n. 6
0
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;
}
int PHG4GenFitTrackProjection::process_event(PHCompositeNode *topNode) {
	if (verbosity > 1)
		cout << "PHG4GenFitTrackProjection::process_event -- entered" << endl;

	//---------------------------------
	// Get Objects off of the Node Tree
	//---------------------------------

	// Pull the reconstructed track information off the node tree...
	SvtxTrackMap* _g4tracks = findNode::getClass<SvtxTrackMap>(topNode,
			"SvtxTrackMap");
	if (!_g4tracks) {
		cerr << PHWHERE << " ERROR: Can't find SvtxTrackMap." << endl;
		return Fun4AllReturnCodes::ABORTRUN;
	}

	for (int i = 0; i < _num_cal_layers; ++i) {

		if (std::isnan(_cal_radii[i]))
			continue;

		if (verbosity > 1)
			cout << "Projecting tracks into: " << _cal_names[i] << endl;

		// pull the tower geometry
		string towergeonodename = "TOWERGEOM_" + _cal_names[i];
		RawTowerGeomContainer *towergeo = findNode::getClass<
				RawTowerGeomContainer>(topNode, towergeonodename.c_str());
		if (!towergeo) {
			cerr << PHWHERE << " ERROR: Can't find node " << towergeonodename
					<< endl;
			return Fun4AllReturnCodes::ABORTRUN;
		}

		// pull the towers
		string towernodename = "TOWER_CALIB_" + _cal_names[i];
		RawTowerContainer *towerList = findNode::getClass<RawTowerContainer>(
				topNode, towernodename.c_str());
		if (!towerList) {
			cerr << PHWHERE << " ERROR: Can't find node " << towernodename
					<< endl;
			return Fun4AllReturnCodes::ABORTRUN;
		}

		// pull the clusters
		string clusternodename = "CLUSTER_" + _cal_names[i];
		RawClusterContainer *clusterList = findNode::getClass<
				RawClusterContainer>(topNode, clusternodename.c_str());
		if (!clusterList) {
			cerr << PHWHERE << " ERROR: Can't find node " << clusternodename
					<< endl;
			return Fun4AllReturnCodes::ABORTRUN;
		}

		// loop over all tracks
		for (SvtxTrackMap::Iter iter = _g4tracks->begin();
				iter != _g4tracks->end(); ++iter) {
			SvtxTrack *track = iter->second;
#ifdef DEBUG
			cout
			<<__LINE__
			<<": track->get_charge(): "<<track->get_charge()
			<<endl;
#endif
			if(!track) {
				if(verbosity >= 2) LogWarning("!track");
				continue;
			}

			if (verbosity > 1)
				cout << "projecting track id " << track->get_id() << endl;

			if (verbosity > 1) {
				cout << " track pt = " << track->get_pt() << endl;
			}

			std::vector<double> point;
			point.assign(3, -9999.);

			auto last_state_iter = --track->end_states();

			SvtxTrackState * trackstate = last_state_iter->second;

			if(!trackstate) {
				if(verbosity >= 2) LogWarning("!trackstate");
				continue;
			}

			auto pdg = unique_ptr<TDatabasePDG> (TDatabasePDG::Instance());
			int reco_charge = track->get_charge();
			int gues_charge = pdg->GetParticle(_pid_guess)->Charge();
			if(reco_charge*gues_charge<0) _pid_guess *= -1;
#ifdef DEBUG
			cout
			<<__LINE__
			<<": guess charge: " << gues_charge
			<<": reco charge: " << reco_charge
			<<": pid: " << _pid_guess
			<<": pT: " << sqrt(trackstate->get_px()*trackstate->get_px() + trackstate->get_py()*trackstate->get_py())
			<<endl;
#endif

			auto rep = unique_ptr<genfit::AbsTrackRep> (new genfit::RKTrackRep(_pid_guess));

			unique_ptr<genfit::MeasuredStateOnPlane> msop80 = nullptr;

			{
				TVector3 pos(trackstate->get_x(), trackstate->get_y(), trackstate->get_z());
				//pos.SetXYZ(0.01,0,0);

				TVector3 mom(trackstate->get_px(), trackstate->get_py(), trackstate->get_pz());
				//mom.SetXYZ(1,0,0);

				TMatrixDSym cov(6);
				for (int i = 0; i < 6; ++i) {
					for (int j = 0; j < 6; ++j) {
						cov[i][j] = trackstate->get_error(i, j);
					}
				}

				msop80 = unique_ptr<genfit::MeasuredStateOnPlane> (new genfit::MeasuredStateOnPlane(rep.get()));

				msop80->setPosMomCov(pos, mom, cov);
			}

#ifdef DEBUG
			{
				double x = msop80->getPos().X();
				double y = msop80->getPos().Y();
				double z = msop80->getPos().Z();
//				double px = msop80->getMom().X();
//				double py = msop80->getMom().Y();
				double pz = msop80->getMom().Z();
				genfit::FieldManager *field_mgr = genfit::FieldManager::getInstance();
				double Bx=0, By=0, Bz=0;
				field_mgr->getFieldVal(x,y,z,Bx,By,Bz);
				cout
				<< __LINE__
				<< ": { " << msop80->getPos().Perp() << ", " << msop80->getPos().Phi() << ", " << msop80->getPos().Eta() << "} @ "
				//<< "{ " << Bx << ", " << By << ", " << Bz << "}"
				<< "{ " << msop80->getMom().Perp() << ", " << msop80->getMom().Phi() << ", " << pz << "} "
				<<endl;
				//msop80->Print();
			}
#endif
			try {
				rep->extrapolateToCylinder(*msop80, _cal_radii[i], TVector3(0,0,0),  TVector3(0,0,1));
				//rep->extrapolateToCylinder(*msop80, 5., TVector3(0,0,0),  TVector3(0,0,1));
			} catch (...) {
				if(verbosity >= 2) LogWarning("extrapolateToCylinder failed");
				continue;
			}

#ifdef DEBUG
			{
				cout<<__LINE__<<endl;
				//msop80->Print();
				double x = msop80->getPos().X();
				double y = msop80->getPos().Y();
				double z = msop80->getPos().Z();
//				double px = msop80->getMom().X();
//				double py = msop80->getMom().Y();
				double pz = msop80->getMom().Z();
				genfit::FieldManager *field_mgr = genfit::FieldManager::getInstance();
				double Bx=0, By=0, Bz=0;
				field_mgr->getFieldVal(x,y,z,Bx,By,Bz);
				cout
				<< __LINE__
				<< ": { " << msop80->getPos().Perp() << ", " << msop80->getPos().Phi() << ", " << msop80->getPos().Eta() << "} @ "
				//<< "{ " << Bx << ", " << By << ", " << Bz << "}"
				<< "{ " << msop80->getMom().Perp() << ", " << msop80->getMom().Phi() << ", " << pz << "} "
				<<endl;
			}
#endif

			point[0] = msop80->getPos().X();
			point[1] = msop80->getPos().Y();
			point[2] = msop80->getPos().Z();

#ifdef DEBUG
			cout
			<<__LINE__
			<<": GenFit: {"
			<< point[0] <<", "
			<< point[1] <<", "
			<< point[2] <<" }"
			<<endl;
#endif

			if (std::isnan(point[0]))
				continue;
			if (std::isnan(point[1]))
				continue;
			if (std::isnan(point[2]))
				continue;

			double x = point[0];
			double y = point[1];
			double z = point[2];

			double phi = atan2(y, x);
			double eta = asinh(z / sqrt(x * x + y * y));

			if (verbosity > 1) {
				cout << " initial track phi = " << track->get_phi();
				cout << ", eta = " << track->get_eta() << endl;
				cout << " calorimeter phi = " << phi << ", eta = " << eta
						<< endl;
			}

			// projection is outside the detector extent
			// TODO towergeo doesn't make this easy to extract, but this should be
			// fetched from the node tree instead of hardcoded
			if (fabs(eta) >= 1.0)
				continue;

			// calculate 3x3 tower energy
			int binphi = towergeo->get_phibin(phi);
			int bineta = towergeo->get_etabin(eta);

			double energy_3x3 = 0.0;
			double energy_5x5 = 0.0;
			for (int iphi = binphi - 2; iphi <= binphi + 2; ++iphi) {
				for (int ieta = bineta - 2; ieta <= bineta + 2; ++ieta) {

					// wrap around
					int wrapphi = iphi;
					if (wrapphi < 0) {
						wrapphi = towergeo->get_phibins() + wrapphi;
					}
					if (wrapphi >= towergeo->get_phibins()) {
						wrapphi = wrapphi - towergeo->get_phibins();
					}

					// edges
					if (ieta < 0)
						continue;
					if (ieta >= towergeo->get_etabins())
						continue;

					RawTower* tower = towerList->getTower(ieta, wrapphi);
					if (tower) {

						energy_5x5 += tower->get_energy();
						if (abs(iphi - binphi) <= 1 and abs(ieta - bineta) <= 1)
							energy_3x3 += tower->get_energy();

						if (verbosity > 1)
							cout << " tower " << ieta << " " << wrapphi
									<< " energy = " << tower->get_energy()
									<< endl;
					}
				}
			}

			track->set_cal_energy_3x3(_cal_types[i], energy_3x3);
			track->set_cal_energy_5x5(_cal_types[i], energy_5x5);

			// loop over all clusters and find nearest
			double min_r = DBL_MAX;
			double min_index = -9999;
			double min_dphi = NAN;
			double min_deta = NAN;
			double min_e = NAN;
#ifdef DEBUG
			double min_cluster_phi = NAN;
#endif
			for (unsigned int k = 0; k < clusterList->size(); ++k) {

				RawCluster *cluster = clusterList->getCluster(k);

				double dphi = atan2(sin(phi - cluster->get_phi()),
						cos(phi - cluster->get_phi()));
				double deta = eta - cluster->get_eta();
				double r = sqrt(pow(dphi, 2) + pow(deta, 2));

				if (r < min_r) {
					min_index = k;
					min_r = r;
					min_dphi = dphi;
					min_deta = deta;
					min_e = cluster->get_energy();
#ifdef DEBUG
					min_cluster_phi = cluster->get_phi();
#endif
				}
			}

			if (min_index != -9999) {
				track->set_cal_dphi(_cal_types[i], min_dphi);
				track->set_cal_deta(_cal_types[i], min_deta);
				track->set_cal_cluster_id(_cal_types[i], min_index);
				track->set_cal_cluster_e(_cal_types[i], min_e);

#ifdef DEBUG
			cout
			<<__LINE__
			<<": min_cluster_phi: "<<min_cluster_phi
			<<endl;
#endif

				if (verbosity > 1) {
					cout << " nearest cluster dphi = " << min_dphi << " deta = "
							<< min_deta << " e = " << min_e << endl;
				}
			}

		} // end track loop
	} // end calorimeter layer loop

	if (verbosity > 1)
		cout << "PHG4GenFitTrackProjection::process_event -- exited" << endl;

	return Fun4AllReturnCodes::EVENT_OK;
}
int PHG4HoughTransform::process_event(PHCompositeNode *topNode)
{
  _timer.get()->restart();

  if(verbosity > 0) cout << "PHG4HoughTransform::process_event -- entered" << endl;

  // moving clearing to the beginning of event or we will have
  // return bugs from early exits!
  _clusters_init.clear();
  _clusters.clear();
  _tracks.clear();
  
  //---------------------------------
  // Get Objects off of the Node Tree
  //---------------------------------
  
  GetNodes(topNode);
  
  // Translate into Helix_Hough objects
  //-----------------------------------
  //wrap_clusters_timer.get()->restart();

  for (SvtxClusterMap::Iter iter = _g4clusters->begin();
       iter != _g4clusters->end();
       ++iter) {
    SvtxCluster* cluster = &iter->second;

    //cluster->identify();
    
    float phi = atan2(cluster->get_position(1),cluster->get_position(0));
    unsigned int ilayer = _layer_ilayer_map[cluster->get_layer()];
    
    float xy_error=0.;float z_error=0.;
    if (_use_cell_size) {
      xy_error = _smear_xy_layer[ilayer] * _vote_error_scale[ilayer];
      z_error  = _smear_z_layer[ilayer] * _vote_error_scale[ilayer];
      
    }
    else {
      if( cluster->get_phi_size() <= _max_cluster_error*_smear_xy_layer[ilayer] ){xy_error = cluster->get_phi_size() * _vote_error_scale[ilayer];}
      else{xy_error = _max_cluster_error*_smear_xy_layer[ilayer] * _vote_error_scale[ilayer];}
      if(cluster->get_z_size() <= _max_cluster_error*_smear_z_layer[ilayer]){z_error  = cluster->get_z_size() * _vote_error_scale[ilayer];}
      else{z_error  = _max_cluster_error*_smear_z_layer[ilayer] * _vote_error_scale[ilayer];}
    }

    vector<SimpleHit3D>* which_vec = &_clusters;
    if (ilayer<_seed_layers) {which_vec=&_clusters_init;}

    //SimpleHit3D(float xx, float dxx, float yy, float dyy, float zz, float dzz, unsigned int ind, int lyr=-1)
    SimpleHit3D hit3d(cluster->get_x(),fabs(xy_error*sin(phi)),
		      cluster->get_y(),fabs(xy_error*cos(phi)),
		      cluster->get_z(),z_error,
		      cluster->get_id(),ilayer);

    // copy covariance over
    for (int i=0; i<3; ++i) {
      for (int j=i; j<3; ++j) {
	hit3d.set_error(i,j,cluster->get_error(i,j));
      }
    }

    which_vec->push_back(hit3d);
  }

  if (verbosity > 20) {
    cout << "-------------------------------------------------------------------" << endl;
    cout << "PHG4HoughTransform::process_event has the following input clusters:" << endl;

    if (!_clusters_init.empty()) {
      for (unsigned int i = 0; i < _clusters_init.size(); ++i) {
	cout << "n init clusters = "<<_clusters_init.size() << endl;
	_clusters_init[i].print();
      }
    } else {
      for (unsigned int i = 0; i < _clusters.size(); ++i) {
	cout << "n clusters = "<<_clusters.size() << endl;
	_clusters[i].print();
      }
    }
    
    cout << "-------------------------------------------------------------------" << endl;
  }
  
  //------------------------------------
  // Perform the initial zvertex finding
  //------------------------------------

  if(verbosity > 0) cout << "PHG4HoughTransform::process_event -- initial vertex finding..." << endl;

  // Grab some initial tracks for initial z-vertex finding
  _tracks.clear();

  _vertex.clear();
  _vertex.push_back(0.0); // x guess
  _vertex.push_back(0.0); // y guess
  _vertex.push_back(0.0); // z guess

  if(_use_vertex) {
    
    unsigned int nz0 = _tracker_vertex.size();
    double z0min = -10.;
    double z0_step = 20./((double)nz0);
    
    TH1D npairs("npairs", "npairs", nz0, z0min, z0min + z0_step*((double)nz0));
    
    float extrap_scale = _radii[0]/(_radii[1] - _radii[0]);
    for(unsigned int i=0;i<_clusters_init.size();++i)
    {
      if(_clusters_init[i].layer != 0){continue;}
      float z1 = _clusters_init[i].z;
      for(unsigned int j=0;j<_clusters_init.size();++j)
      {
        if(_clusters_init[j].layer != 1){continue;}
        float z2 = _clusters_init[j].z;
        float zint = z1 + (z1-z2)*extrap_scale;
        npairs.Fill(zint);
      }
    }
    
    int which_vtx_bin = (npairs.GetMaximumBin() - 1);
    if( (which_vtx_bin < 0) || (which_vtx_bin >= (int)nz0) ){which_vtx_bin = -1;}
    
    unsigned int maxtracks = 100 * _clusters_init.size() / ( 400*_seed_layers) ;
    //if(maxtracks<100){maxtracks=0;}
    maxtracks = 80;
    
    // find maxtracks tracks
    vector<SimpleTrack3D> temptracks;
    vector<Matrix<float,5,5> > covariances;
    for(unsigned int i=0;i<_tracker_vertex.size();++i)
    {
      if( (which_vtx_bin != -1) && (which_vtx_bin != ((int)i) ) ){continue;}
      _tracker_vertex[i]->clear();
      _tracker_vertex[i]->findHelices(_clusters_init, _req_seed, _max_hits_init, temptracks, maxtracks);
      for(unsigned int t=0;t<temptracks.size();++t)
      {
        _tracks.push_back(temptracks[t]);
        covariances.push_back( (_tracker_vertex[i]->getKalmanStates())[t].C );
      }
    }
    
    if(_tracks.size() == 0){return Fun4AllReturnCodes::EVENT_OK;}
    else if(_tracks.size() == 1)
    {
      _vertex[0] = cos(_tracks[0].phi) * _tracks[0].d;
      _vertex[1] = sin(_tracks[0].phi) * _tracks[0].d;
      _vertex[2] = _tracks[0].z0;
    }
    else
    {
      vector<vector<double> > pTmap;
      for(unsigned int i=0;i<_tracks.size();++i)
      {
        if(_tracks[i].kappa == 0.0){continue;}
        double pT = kappaToPt(_tracks[i].kappa);
        pTmap.push_back(vector<double>());
        pTmap.back().push_back(pT);
        pTmap.back().push_back((double)i);
      }
      sort(pTmap.begin(), pTmap.end());
      vector<SimpleTrack3D> vtxtracks;
      vector<Matrix<float,5,5> > vtxcovariances;
      unsigned int maxvtxtracks=100;
      if(_tracks.size() < maxvtxtracks)
      {
        vtxtracks = _tracks;
        vtxcovariances = covariances;
      }
      else
      {
        for(unsigned int i=0;i<maxvtxtracks;++i)
        {
          vtxtracks.push_back(_tracks[ (int)(pTmap[pTmap.size()-1-i][1]) ]);
          vtxcovariances.push_back(covariances[ (int)(pTmap[pTmap.size()-1-i][1]) ] );
        }
      }
      
      
      vector<double> zvertices(3,0.);
      vector<float> temp_vertex(3,0.);
      vector<unsigned int> vtracks(3,0);
      for(unsigned int iter = 0;iter < 3; ++iter)
      {
        temp_vertex[2] = 0.;
        
        TH1D z0_hist("z0_hist","z0_hist", 20, -10., 10.);
        for(unsigned int i=0;i<vtxtracks.size();++i)
        {
          z0_hist.Fill(vtxtracks[i].z0);
        }
        temp_vertex[2] = z0_hist.GetBinCenter( z0_hist.GetMaximumBin() );
        
        _vertexFinder.findVertex(vtxtracks, vtxcovariances, temp_vertex, 3., true);
        _vertexFinder.findVertex(vtxtracks, vtxcovariances, temp_vertex, 0.1, true);
        _vertexFinder.findVertex(vtxtracks, vtxcovariances, temp_vertex, 0.02, false);
        
        
        vector<SimpleTrack3D> ttracks;
        for(unsigned int t=0;t<vtxtracks.size();++t)
        {
          if( fabs(vtxtracks[t].z0 - temp_vertex[2]) < 0.1 ){vtracks[iter] += 1;}
          else{ttracks.push_back(vtxtracks[t]);}
        }
        vtxtracks = ttracks;
        zvertices[iter] = temp_vertex[2];
      }
      _vertex[2] = zvertices[0];
      unsigned int zbest = 0;
      for(unsigned int iter = 1;iter < 3; ++iter)
      {
        if(vtracks[iter] > vtracks[zbest])
        {
          _vertex[2] = zvertices[iter];
          zbest = iter;
        }
      }
    }
    
    
    
    
    
    
    if(verbosity > 0) cout << "PHG4HoughTransform::process_event -- found initial vertex : " << _vertex[0] << " " << _vertex[1] << " " << _vertex[2] << endl;
    
    _tracks.clear();
    
    // shift the vertex to the origin
    for(unsigned int ht=0;ht<_clusters_init.size();++ht)
    {
      _clusters_init[ht].x -= _vertex[0];
      _clusters_init[ht].y -= _vertex[1];
      _clusters_init[ht].z -= _vertex[2];
    }
    for(unsigned int ht=0;ht<_clusters.size();++ht)
    {
      _clusters[ht].x -= _vertex[0];
      _clusters[ht].y -= _vertex[1];
      _clusters[ht].z -= _vertex[2];
    }
    
    

    
  }  // if(_use_vertex)
  
  //----------------------------------
  // Preform the track finding
  //----------------------------------
  _tracker->clear();
  _tracks.clear();
  _timer_initial_hough.get()->restart();
  _tracker->findHelices(_clusters_init, _min_hits_init, _max_hits_init, _tracks);
  _timer_initial_hough.get()->stop();
  
  

  if(verbosity > 0)
  {
    cout << "PHG4HoughTransform::process_event -- full track finding pass found: " << _tracks.size() << " tracks" << endl;
  }    
   
  //----------------------------
  // Re-center event on detector
  //----------------------------

  if(verbosity > 0) cout << "PHG4HoughTransform::process_event -- recentering event on detector..." << endl;
  vector<double> chi_squareds;
  for(unsigned int tt=0;tt<_tracks.size();tt++)
  {
    // move the hits in the track back to their original position                
    for(unsigned int hh=0;hh<_tracks[tt].hits.size();hh++)
    {
      _tracks[tt].hits[hh].x = _tracks[tt].hits[hh].x + _vertex[0];
      _tracks[tt].hits[hh].y = _tracks[tt].hits[hh].y + _vertex[1];
      _tracks[tt].hits[hh].z = _tracks[tt].hits[hh].z + _vertex[2];
//       _tracks[tt].z0 += _vertex[2];
    }
    chi_squareds.push_back(_tracker->getKalmanStates()[tt].chi2);}

  if(verbosity > 0)
  {
    cout << "PHG4HoughTransform::process_event -- final track count: " << _tracks.size() << endl;
  }

  //---------------------------
  // Final vertex determination
  //---------------------------
  
  // final best guess of the primary vertex position here...
  if(verbosity > 0)
  {
    cout<< "PHG4HoughTransform::process_event -- calculating final vertex" << endl;
  }
  
  // sort the tracks by pT
  vector<vector<double> > pTmap;
  for(unsigned int i=0;i<_tracks.size();++i)
  {
    double pT = kappaToPt(_tracks[i].kappa);
    pTmap.push_back(vector<double>());
    pTmap.back().push_back(pT);
    pTmap.back().push_back((double)i);
  }
  sort(pTmap.begin(), pTmap.end());
  vector<SimpleTrack3D> vtxtracks;
  vector<Matrix<float,5,5> > vtxcovariances;
  unsigned int maxvtxtracks=100;
  if(_tracks.size() < maxvtxtracks){vtxtracks = _tracks;}
  else
  {
    for(unsigned int i=0;i<maxvtxtracks;++i)
    {
      vtxtracks.push_back(_tracks[ (int)(pTmap[pTmap.size()-1-i][1]) ]);
      vtxcovariances.push_back( (_tracker->getKalmanStates())[ (int)(pTmap[pTmap.size()-1-i][1]) ].C );
    }
  }
  
  double vx = _vertex[0];
  double vy = _vertex[1];
  double vz = _vertex[2];
  
  _vertex[0] = 0.;
  _vertex[1] = 0.;
  _vertex[2] = 0.;
  
  _vertexFinder.findVertex(vtxtracks, vtxcovariances, _vertex, 0.3, false);
  _vertexFinder.findVertex(vtxtracks, vtxcovariances, _vertex, 0.1, false);
  _vertexFinder.findVertex(vtxtracks, vtxcovariances, _vertex, 0.02, false);
  _vertexFinder.findVertex(vtxtracks, vtxcovariances, _vertex, 0.005, false);
  
  _vertex[0] += vx;
  _vertex[1] += vy;
  _vertex[2] += vz;
  
  if(verbosity > 0)
  {
    cout << "PHG4HoughTransform::process_event -- final vertex: " << _vertex[0] << " " << _vertex[1] << " " << _vertex[2] << endl;
  }

  //--------------------------------
  // Translate back into PHG4 objects
  //--------------------------------

  if(verbosity > 0)
  {
    cout << "PHG4HoughTransform::process_event -- producing PHG4Track objects..." << endl;
  }

  SvtxVertex vertex;
  vertex.set_t0(0.0);
  for (int i=0;i<3;++i) vertex.set_position(i,_vertex[i]);
  vertex.set_chisq(0.0);
  vertex.set_ndof(0); 
  vertex.set_error(0,0,0.0);
  vertex.set_error(0,1,0.0);
  vertex.set_error(0,2,0.0);
  vertex.set_error(1,0,0.0);
  vertex.set_error(1,1,0.0);
  vertex.set_error(1,2,0.0);
  vertex.set_error(2,0,0.0);
  vertex.set_error(2,1,0.0);
  vertex.set_error(2,2,0.0);
  
  // copy out the reconstructed vertex position
  //_g4tracks->setVertex(_vertex[0],_vertex[1],_vertex[2]);
  //_g4tracks->setVertexError(0.0,0.0,0.0);
 
  // at this point we should already have an initial pt and pz guess...
  // need to translate this into the PHG4Track object...

  vector<SimpleHit3D> track_hits;
  int clusterID;
  int clusterLayer;
  float cluster_x;
  float cluster_y;
  float cluster_z;
  //  float dEdx1;
  //  float dEdx2;

  for(unsigned int itrack=0; itrack<_tracks.size();itrack++)
  {
    SvtxTrack track;
    track.setTrackID(itrack);
    track_hits.clear();
    track_hits = _tracks.at(itrack).hits;
    
    for(unsigned int ihit = 0; ihit<track_hits.size();ihit++)
    {
      //      dEdx1=0;
      //      dEdx2=0;
      if( (track_hits.at(ihit).index) >= _g4clusters->size()){continue;}
      SvtxCluster *cluster = _g4clusters->get(track_hits.at(ihit).index);
      clusterID = cluster->get_id();
      clusterLayer = cluster->get_layer();
      cluster_x = cluster->get_x();
      cluster_y = cluster->get_y();
      cluster_z = cluster->get_z();
      if( (clusterLayer < (int)_seed_layers) && (clusterLayer >= 0) )
      {
        track.setClusterID(clusterLayer, clusterID);
        track.setHitPosition(clusterLayer,cluster_x,cluster_y,cluster_z);
      }
    }
    float kappa = _tracks.at(itrack).kappa;
    float d = _tracks.at(itrack).d;
    float phi = _tracks.at(itrack).phi;

    float dzdl = _tracks.at(itrack).dzdl;
    float z0 = _tracks.at(itrack).z0;
    
    track.phi = phi;
    track.kappa = kappa;
    track.d = d;
    track.z0 = z0;
    track.dzdl = dzdl;
    
    float pT = kappaToPt(kappa);

    float x_center = cos(phi)*(d+1/kappa); // x coordinate of circle center
    float y_center = sin(phi)*(d+1/kappa); // y    "      "     "      "

    // find helicity from cross product sign
    short int helicity;
    if((track_hits[0].x-x_center)*(track_hits[track_hits.size()-1].y-y_center) -
       (track_hits[0].y-y_center)*(track_hits[track_hits.size()-1].x-x_center) > 0)
    {
      helicity = 1;
    }
    else
    { 
      helicity = -1;
    }
    float pZ = 0;
    if(dzdl != 1)
    {
      pZ = pT * dzdl / sqrt(1.0 - dzdl*dzdl);
    }
    int ndf = 2*_tracks.at(itrack).hits.size() - 5;
    track.setQuality(chi_squareds[itrack]/((float)ndf));
    track.setChisq(chi_squareds[itrack]);
    track.setNDF(ndf);
    track.set3Momentum( pT*cos(phi-helicity*M_PI/2), pT*sin(phi-helicity*M_PI/2), pZ);

    track.setDCA2D( d );
    track.setDCA2Dsigma(sqrt(_tracker->getKalmanStates()[itrack].C(1,1)));  

    if(_magField > 0)
    {
      track.setCharge( -1.0*helicity );
    }
    else
    {
      track.setCharge( helicity );
    }
    
    for(unsigned int row=0;row<5;++row)
    {
      for(unsigned int col=0;col<5;++col)
      {
        (*(track.getCovariance()))[row][col] = _tracker->getKalmanStates()[itrack].C(row,col);
      }
    }
    
    _g4tracks->insert(track);
    vertex.insert_track(track.getTrackID());

    if (verbosity > 5) {
      cout << "track " << itrack << " quality = "
           << track.getQuality() << endl;
      cout << "px = " << track.get3Momentum(0)
           << " py = " << track.get3Momentum(1)
           << " pz = " << track.get3Momentum(2) << endl;
    }
  } // track loop

  SvtxVertex *vtxptr = _g4vertexes->insert(vertex);
  if (verbosity > 5) vtxptr->identify();
  
  if(verbosity > 0)
  {
    cout << "PHG4HoughTransform::process_event -- leaving process_event" << endl;
  }

  _timer.get()->stop();
  return Fun4AllReturnCodes::EVENT_OK;
}
SvtxTrack* TrackProjectionTools::FindClosestTrack( RawCluster* cluster, float& best_track_dr )
{

  /* best matching track */
  SvtxTrack* best_track = NULL;
  best_track_dr = NAN;

  /* find name of calorimeter for this cluster */
  string caloname = "NONE";

  /* C++11 range loop */
  for (auto& towit : cluster->get_towermap() )
    {
      caloname = RawTowerDefs::convert_caloid_to_name( RawTowerDefs::decode_caloid(towit.first) );
      break;
    }

  /* Get track collection with all tracks in this event */
  SvtxTrackMap* trackmap =
    findNode::getClass<SvtxTrackMap>(_topNode,"SvtxTrackMap_FastSim");
  if (!trackmap)
    {
      cout << PHWHERE << "SvtxTrackMap node not found on node tree"
           << endl;
    }

  /* Loop over all tracks from BARREL tracking and see if one points to the same
   * cluster as the reference clusters (i.e. matching ID in the same calorimeter) */
  /*
  for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
       track_itr != trackmap->end(); track_itr++)
    {
      SvtxTrack* track =  dynamic_cast<SvtxTrack*>(track_itr->second);
  
      if ( caloname == "CEMC" &&
           track->get_cal_cluster_id(SvtxTrack::CEMC) == cluster->get_id() )
        {
          best_track = track;
        }
    }
  */

  /* If track found with barrel tracking, return it here- if not, proceed with forward tracking below. */
  if ( best_track )
    return best_track;
  
  /* Cluster / track matching for barrel calorimeters and tracking */
  float max_dr = 10;

  /* cluster position for easy reference */
  float cx = cluster->get_x();
  float cy = cluster->get_y();

  /* If track map found: Loop over all tracks to find best match for cluster (forward calorimeters)*/
  if ( trackmap &&
       ( caloname == "FEMC" || caloname == "EEMC" ) )
    {
      for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
           track_itr != trackmap->end(); track_itr++)
        {
          /* get pointer to track */
          SvtxTrack* track =  dynamic_cast<SvtxTrack*>(track_itr->second);

          /* distance between track and cluster */
          float dr = NAN;

          /* loop over track states (projections) sotred for this track */
          for (SvtxTrack::ConstStateIter state_itr = track->begin_states();
               state_itr != track->end_states(); state_itr++)
            {
              /* get pointer to current track state */
              SvtxTrackState *temp = dynamic_cast<SvtxTrackState*>(state_itr->second);

              /* check if track state projection name matches calorimeter where cluster was found */
              if( (temp->get_name()==caloname) )
                {
                  dr = sqrt( pow( cx - temp->get_x(), 2 ) + pow( cy - temp->get_y(), 2 ) );
		  break;
                }
            }
	  
          /* check dr and update best_track and best_track_dr if this track is closest to cluster */
          if ( ( best_track_dr != best_track_dr ) ||
	       ( dr < max_dr &&
		 dr < best_track_dr )
	       )
            {
              best_track = track;
              best_track_dr = dr;
            }
        }
    }


  /* If track found with barrel tracking, return it here- if not, proceed with alternative barrel cluster-track matching below. */
  if ( best_track )
    return best_track;
  

  /* Cluster / track matching for barrel calorimeters and tracking */
  float max_dr_barrel = 10;

  float ctheta = atan2( cluster->get_r() , cluster->get_z() );
  float ceta =  -log( tan( ctheta / 2.0 ) );
  float cphi = cluster->get_phi();

  /* If track map found: Loop over all tracks to find best match for cluster (barrel calorimeters)*/
  if ( trackmap &&
       ( caloname == "CEMC" ) )
    {
      for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
           track_itr != trackmap->end(); track_itr++)
        {
          /* get pointer to track */
          SvtxTrack* track =  dynamic_cast<SvtxTrack*>(track_itr->second);

          /* distance between track and cluster */
          float dr = NAN;

          /* loop over track states (projections) sotred for this track */
          for (SvtxTrack::ConstStateIter state_itr = track->begin_states();
               state_itr != track->end_states(); state_itr++)
            {
              /* get pointer to current track state */
              SvtxTrackState *temp = dynamic_cast<SvtxTrackState*>(state_itr->second);

              /* check if track state projection name matches calorimeter where cluster was found */
              if( (temp->get_name()==caloname) )
                {
                  dr = sqrt( pow( ceta - temp->get_eta(), 2 ) + pow( cphi - temp->get_phi(), 2 ) );
                  break;
                }
            }

          /* check dr and update best_track and best_track_dr if this track is closest to cluster */
	  if ( ( best_track_dr != best_track_dr ) || 
	       (dr < max_dr_barrel &&
               dr < best_track_dr) )
            {
              best_track = track;
              best_track_dr = dr;
            }
        }
    }

  return best_track;
}
int
LeptoquarksReco::AddTrackInformation( type_map_tcan& tauCandidateMap, SvtxTrackMap* trackmap, SvtxVertexMap* vertexmap, SvtxEvalStack *svtxevalstack, double R_max )
{
  
  // Pointers for tracks //
  SvtxTrackEval* trackeval = svtxevalstack->get_track_eval();
  SvtxTruthEval* trutheval = svtxevalstack->get_truth_eval();

  /* Loop over tau candidates */
  for (type_map_tcan::iterator iter = tauCandidateMap.begin();
       iter != tauCandidateMap.end();
       ++iter)
    {
      uint tracks_count_r02 = 0;
      int tracks_chargesum_r02 = 0;
      float tracks_rmax_r02 = 0;

      uint tracks_count_r04 = 0;
      int tracks_chargesum_r04 = 0;
      float tracks_rmax_r04 = 0;

      uint tracks_count_R = 0;
      int tracks_chargesum_R = 0;
      float tracks_rmax_R = 0;

      vector<float> tracks_vertex;
      vector<float> temp_vertex;

      float jet_eta = (iter->second)->get_property_float( PidCandidate::jet_eta );
      float jet_phi = (iter->second)->get_property_float( PidCandidate::jet_phi );
      
      /* Loop over tracks
       * (float) track->get_eta(),     //eta of the track
       * (float) track->get_phi(),     //phi of the track
       * (float) track->get_pt(),      //transverse momentum of track
       * (float) track->get_p(),       //total momentum of track
       * (float) track->get_charge(),  //electric charge of track
       * (float) track->get_quality()  //track quality */
  
      //Loop over tracks in event //
    for (SvtxTrackMap::ConstIter track_itr = trackmap->begin();
           track_itr != trackmap->end(); track_itr++) {

        SvtxTrack* track = dynamic_cast<SvtxTrack*>(track_itr->second);

	// Get track variables //
        float track_eta = track->get_eta();
        float track_phi = track->get_phi();
        int track_charge = track->get_charge();
	double gvx,gvy,gvz;
	  
        float delta_R = CalculateDeltaR( track_eta, track_phi, jet_eta, jet_phi );

	PHG4Particle* g4particle = trackeval->max_truth_particle_by_nclusters(track);

	// Get true vertex distances //	  
	PHG4VtxPoint* vtx = trutheval->get_vertex(g4particle);
	gvx = vtx->get_x();
	gvy = vtx->get_y();
	gvz = vtx->get_z();
	

	
	// If charged track is within jet then use its vertex //
	if(delta_R < 0.5 && trutheval->is_primary(g4particle)) tracks_vertex.push_back(sqrt(pow(gvx,2)+pow(gvy,2)+pow(gvz,2)));
	
	/* if save_tracks set true: add track to tree */
        if ( _save_tracks )
          {
            float track_data[17] = {(float) _ievent,
                                    (float) (iter->second)->get_property_uint( PidCandidate::jet_id ),
                                    (float) (iter->second)->get_property_int( PidCandidate::evtgen_pid ),
                                    (float) (iter->second)->get_property_float( PidCandidate::evtgen_etotal ),
                                    (float) (iter->second)->get_property_float( PidCandidate::evtgen_eta ),
                                    (float) (iter->second)->get_property_float( PidCandidate::evtgen_phi ),
                                    (float) (iter->second)->get_property_uint( PidCandidate::evtgen_decay_prong ),
                                    (float) (iter->second)->get_property_uint( PidCandidate::evtgen_decay_hcharged ),
                                    (float) (iter->second)->get_property_uint( PidCandidate::evtgen_decay_lcharged ),
                                    (float) (iter->second)->get_property_float( PidCandidate::jet_eta ),
                                    (float) (iter->second)->get_property_float( PidCandidate::jet_phi ),
                                    (float) (iter->second)->get_property_float( PidCandidate::jet_etotal ),
                                    (float) track->get_quality(),
                                    (float) track_eta,
                                    (float) track_phi,
                                    (float) delta_R,
                                    (float) track->get_p()
            };

            _ntp_track->Fill(track_data);
          }

        /* If track within search cone, update track information for tau candidate */
        if ( delta_R < 0.2 )
          {
            tracks_count_r02++;
            tracks_chargesum_r02 += track_charge;

            if ( delta_R > tracks_rmax_r02 )
              tracks_rmax_r02 = delta_R;
          }

        if ( delta_R < 0.4 )
          {
            tracks_count_r04++;
            tracks_chargesum_r04 += track_charge;

            if ( delta_R > tracks_rmax_r04 )
              tracks_rmax_r04 = delta_R;
          }

	if ( delta_R < R_max )
          {
            tracks_count_R++;
            tracks_chargesum_R += track_charge;

            if ( delta_R > tracks_rmax_R )
              tracks_rmax_R = delta_R;
          }

    } // end loop over reco tracks //
    // sort vertex array in increasing order //
    std::sort(tracks_vertex.begin(),tracks_vertex.end());
    
    // Compute average vertex distance of tracks in jet //
    float avg = Average(tracks_vertex);

    /* Set track-based properties for tau candidate */
      (iter->second)->set_property( PidCandidate::tracks_count_r02, tracks_count_r02 );
      (iter->second)->set_property( PidCandidate::tracks_chargesum_r02, tracks_chargesum_r02 );
      (iter->second)->set_property( PidCandidate::tracks_rmax_r02, tracks_rmax_r02 );
      (iter->second)->set_property( PidCandidate::tracks_count_r04, tracks_count_r04 );
      (iter->second)->set_property( PidCandidate::tracks_chargesum_r04, tracks_chargesum_r04 );
      (iter->second)->set_property( PidCandidate::tracks_rmax_r04, tracks_rmax_r04 );
      (iter->second)->set_property( PidCandidate::tracks_count_R, tracks_count_R );
      (iter->second)->set_property( PidCandidate::tracks_chargesum_R, tracks_chargesum_R );
      (iter->second)->set_property( PidCandidate::tracks_rmax_R, tracks_rmax_R );
      if(avg == avg) (iter->second)->set_property( PidCandidate::tracks_vertex, avg);

    } // end loop over tau  candidates

  return 0;
}
Esempio n. 11
0
int PhotonJet::process_event(PHCompositeNode *topnode)
{
 
  cout<<"at event number "<<nevents<<endl;


  //get the nodes from the NodeTree
  JetMap* truth_jets = findNode::getClass<JetMap>(topnode,"AntiKt_Truth_r04");
  JetMap *reco_jets = findNode::getClass<JetMap>(topnode,"AntiKt_Tower_r04");
  PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topnode,"G4TruthInfo");
  RawClusterContainer *clusters = findNode::getClass<RawClusterContainer>(topnode,"CLUSTER_CEMC");
  SvtxTrackMap *trackmap = findNode::getClass<SvtxTrackMap>(topnode,"SvtxTrackMap");
  JetEvalStack* _jetevalstack = new JetEvalStack(topnode,"AntiKt_Tower_r04","AntiKt_Truth_r04");
  PHG4TruthInfoContainer::Range range = truthinfo->GetPrimaryParticleRange();

  if(!truth_jets){
      cout<<"no truth jets"<<endl;
      return 0;
    }
  if(!reco_jets){
      cout<<"no reco jets"<<endl;
      return 0;
    }
  if(!truthinfo){
      cout<<"no truth track info"<<endl;
      return 0;
    }
  if(!clusters){
    cout<<"no cluster info"<<endl;
    return 0;
  }
  if(!trackmap){
    cout<<"no track map"<<endl;
    return 0;
  }
  if(!_jetevalstack){
    cout<<"no good truth jets"<<endl;
    return 0;
  }


  JetRecoEval* recoeval = _jetevalstack->get_reco_eval();


 /***********************************************

  GET THE TRUTH PARTICLES

  ************************************************/



  for( PHG4TruthInfoContainer::ConstIterator iter = range.first; iter!=range.second; ++iter){

    PHG4Particle *truth = iter->second;
    
    truthpx = truth->get_px();
    truthpy = truth->get_py();
    truthpz = truth->get_pz();
    truthp = sqrt(truthpx*truthpx+truthpy*truthpy+truthpz*truthpz);
    truthenergy = truth->get_e();
    
    TLorentzVector vec;
    vec.SetPxPyPzE(truthpx,truthpy,truthpz,truthenergy);
    
    truthpt = sqrt(truthpx*truthpx+truthpy*truthpy);
    if(truthpt<0.5)
      continue;
    
    truthphi = vec.Phi();
    trutheta = vec.Eta();
    truthpid = truth->get_pid();
    
    truth_g4particles->Fill();

  }


  /***********************************************

  GET THE EMCAL CLUSTERS

  ************************************************/


  RawClusterContainer::ConstRange begin_end = clusters->getClusters();
  RawClusterContainer::ConstIterator clusiter;
  
  for(clusiter = begin_end.first; clusiter!=begin_end.second; ++clusiter){

    RawCluster *cluster = clusiter->second;

    clus_energy = cluster->get_energy();
    clus_eta = cluster->get_eta();
    clus_theta = 2.*TMath::ATan((TMath::Exp(-1.*clus_eta)));
    clus_pt = clus_energy*TMath::Sin(clus_theta);
    clus_phi = cluster->get_phi();
    
    if(clus_pt<0.5)
      continue;

   
    
    TLorentzVector *clus = new TLorentzVector();
    clus->SetPtEtaPhiE(clus_pt,clus_eta,clus_phi,clus_energy);
    
    float dumarray[4];
    clus->GetXYZT(dumarray);
    clus_x = dumarray[0];
    clus_y = dumarray[1];
    clus_z = dumarray[2];
    clus_t = dumarray[3];

    clus_px = clus_pt*TMath::Cos(clus_phi);
    clus_py = clus_pt*TMath::Sin(clus_phi);
    clus_pz = sqrt(clus_energy*clus_energy-clus_px*clus_px-clus_py*clus_py);

    cluster_tree->Fill();

    //only interested in high pt photons to be isolated
    if(clus_pt<mincluspt)
      continue;
    if(fabs(clus_eta)>(1.0-isoconeradius))
      continue;

    float energysum = ConeSum(cluster,clusters,trackmap,isoconeradius);
    bool conecut = energysum > 0.1*clus_energy;
    if(conecut)
      continue;
   
    isolated_clusters->Fill();
    
    GetRecoHadronsAndJets(cluster, trackmap, reco_jets,recoeval);

  }



  /***********************************************

  GET THE SVTX TRACKS

  ************************************************/
  
  SvtxEvalStack *svtxevalstack = new SvtxEvalStack(topnode);
  svtxevalstack->next_event(topnode);

  SvtxTrackEval *trackeval = svtxevalstack->get_track_eval();

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

    SvtxTrack *track = iter->second;

    //get reco info
    tr_px = track->get_px();
    tr_py = track->get_py();
    tr_pz = track->get_pz();
    tr_p = sqrt(tr_px*tr_px+tr_py*tr_py+tr_pz*tr_pz);
    
    tr_pt = sqrt(tr_px*tr_px+tr_py*tr_py);
    
    if(tr_pt<0.5)
      continue;
  
    tr_phi = track->get_phi();
    tr_eta = track->get_eta();

    if(fabs(tr_eta)>1)
      continue;

    charge = track->get_charge();
    chisq = track->get_chisq();
    ndf = track->get_ndf();
    dca = track->get_dca();
    tr_x = track->get_x();
    tr_y = track->get_y();
    tr_z = track->get_z();
    
   
    //get truth info
    PHG4Particle *truthtrack = trackeval->max_truth_particle_by_nclusters(track);
    truth_is_primary = truthinfo->is_primary(truthtrack);
    
    truthtrackpx = truthtrack->get_px();
    truthtrackpy = truthtrack->get_py();
    truthtrackpz = truthtrack->get_pz();
    truthtrackp = sqrt(truthtrackpx*truthtrackpx+truthtrackpy*truthtrackpy+truthtrackpz*truthtrackpz);
    
    truthtracke = truthtrack->get_e();
    TLorentzVector *Truthtrack = new TLorentzVector();
    Truthtrack->SetPxPyPzE(truthtrackpx,truthtrackpy,truthtrackpz,truthtracke);
    truthtrackpt = Truthtrack->Pt();
    truthtrackphi = Truthtrack->Phi();
    truthtracketa = Truthtrack->Eta();
    truthtrackpid = truthtrack->get_pid();


    tracktree->Fill();

  }







  /***************************************

   TRUTH JETS

   ***************************************/


  for(JetMap::Iter iter = truth_jets->begin(); iter!=truth_jets->end(); ++iter){
    Jet *jet = iter->second;
    
    truthjetpt = jet->get_pt();
    if(truthjetpt<10.)
      continue;
    
    truthjeteta = jet->get_eta();
    if(fabs(truthjeteta)>2.)
      continue;

    truthjetpx = jet->get_px();
    truthjetpy = jet->get_py();
    truthjetpz = jet->get_pz();
    truthjetphi = jet->get_phi();
    truthjetmass = jet->get_mass();
    truthjetp = jet->get_p();
    truthjetenergy = jet->get_e();
    
    truthjettree->Fill();
    


  }



/***************************************

   RECONSTRUCTED JETS

   ***************************************/



  for(JetMap::Iter iter = reco_jets->begin(); iter!=reco_jets->end(); ++iter){
    Jet *jet = iter->second;
    Jet *truthjet = recoeval->max_truth_jet_by_energy(jet);
    recojetpt = jet->get_pt();
    if(recojetpt<4.)
      continue;
    
    recojeteta = jet->get_eta();
    if(fabs(recojeteta)>2.)
      continue;
    recojetid = jet->get_id();
    recojetpx = jet->get_px();
    recojetpy = jet->get_py();
    recojetpz = jet->get_pz();
    recojetphi = jet->get_phi();
    recojetmass = jet->get_mass();
    recojetp = jet->get_p();
    recojetenergy = jet->get_e();

    if(truthjet){
      truthjetid = truthjet->get_id();
      truthjetp = truthjet->get_p();
      truthjetphi = truthjet->get_phi();
      truthjeteta = truthjet->get_eta();
      truthjetpt = truthjet->get_pt();
      truthjetenergy = truthjet->get_e();
      truthjetpx = truthjet->get_px();
      truthjetpy = truthjet->get_py();
      truthjetpz = truthjet->get_pz();
    }
    else{
      truthjetid = 0;
      truthjetp = 0;
      truthjetphi = 0;
      truthjeteta = 0;
      truthjetpt = 0;
      truthjetenergy = 0;
      truthjetpx = 0;
      truthjetpy = 0;
      truthjetpz = 0;
      
    }
    recojettree->Fill();

  }

  


  nevents++;
  tree->Fill();
  return 0;

}
Esempio n. 12
0
float PhotonJet::ConeSum(RawCluster *cluster, 
			 RawClusterContainer *cluster_container,
			 SvtxTrackMap *trackmap,
			 float coneradius)
{

  float energyptsum=0;
  
  RawClusterContainer::ConstRange begin_end = cluster_container->getClusters();
  RawClusterContainer::ConstIterator clusiter;
  
  for(clusiter = begin_end.first; clusiter!=begin_end.second; ++clusiter){
    RawCluster *conecluster = clusiter->second;
    
    //check to make sure that the candidate isolated photon isn't being counted in the energy sum
    if(conecluster->get_energy() == cluster->get_energy())
      if(conecluster->get_phi() == cluster->get_phi())
	if(conecluster->get_eta() == cluster->get_eta())
	  continue;

    float cone_pt = conecluster->get_energy()/TMath::CosH(conecluster->get_eta());
    if(cone_pt<0.2)
      continue;
    
    float cone_e = conecluster->get_energy();
    float cone_eta = conecluster->get_eta();
    float cone_phi = conecluster->get_phi();
    


    float dphi = cluster->get_phi()-cone_phi;
    if(dphi<-1*pi)
      dphi+=2.*pi;
    if(dphi>pi)
      dphi-=2.*pi;
    
    float deta = cluster->get_eta()-cone_eta;

   
    float radius = sqrt(dphi*dphi+deta*deta);
   
    if(radius<coneradius){
      energyptsum+=cone_e;
    
    }
  }
  

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

    float trackpx = track->get_px();
    float trackpy = track->get_py();
    float trackpt = sqrt(trackpx*trackpx+trackpy*trackpy);
    if(trackpt<0.2)
      continue;
    float trackphi = track->get_phi();
    float tracketa = track->get_eta();
    float dphi = cluster->get_phi()-trackphi;
    float deta = cluster->get_eta()-tracketa;
    float radius = sqrt(dphi*dphi+deta*deta);
  
    if(radius<coneradius){
      energyptsum+=trackpt;
    }
  }


  return energyptsum;

}
Esempio n. 13
0
void PhotonJet::GetRecoHadronsAndJets(RawCluster *trig,
				      SvtxTrackMap *tracks,
				      JetMap *jets,
				      JetRecoEval *recoeval)
{

  float trig_phi = trig->get_phi();
  float trig_eta = trig->get_eta();

 
  for(SvtxTrackMap::Iter iter = tracks->begin(); iter!=tracks->end(); ++iter){

    SvtxTrack *track = iter->second;

    _tr_px = track->get_px();
    _tr_py = track->get_py();
    _tr_pz = track->get_pz();
    _tr_pt = sqrt(_tr_px*_tr_px+_tr_py*_tr_py);
    if(_tr_pt<0.5)
      continue;

    _tr_p = sqrt(_tr_px*_tr_px+_tr_py*_tr_py+_tr_pz*_tr_pz);
    _tr_phi = track->get_phi();
    _tr_eta = track->get_eta();
    _charge = track->get_charge();
    _chisq = track->get_chisq();
    _ndf = track->get_ndf();
    _dca = track->get_dca();
    _tr_x = track->get_x();
    _tr_y = track->get_y();
    _tr_z = track->get_z();

    haddphi = trig_phi-_tr_phi;
    if(haddphi<pi2)
      haddphi+=2.*pi;
    if(haddphi>threepi2)
      haddphi-=2.*pi;


    haddeta = trig_eta-_tr_eta;

    hadpout = _tr_p*TMath::Sin(haddphi);

    isophot_had_tree->Fill();
    

  }
  for(JetMap::Iter iter = jets->begin(); iter!=jets->end(); ++iter){
    Jet* jet = iter->second;
    Jet *truthjet = recoeval->max_truth_jet_by_energy(jet);

    _recojetpt = jet->get_pt();
    if(_recojetpt<4.0) 
      continue;
      
    _recojeteta = jet->get_eta();
    if(fabs(_recojeteta)>1.)
      continue;

    _recojetpx = jet->get_px();
    _recojetpy = jet->get_py();
    _recojetpz = jet->get_pz();
    _recojetphi = jet->get_phi();
    _recojetmass = jet->get_mass();
    _recojetp = jet->get_p();
    _recojetenergy = jet->get_e();
    _recojetid = jet->get_id();

    if(truthjet){
      _truthjetid = truthjet->get_id();
      _truthjetp = truthjet->get_p();
      _truthjetphi = truthjet->get_phi();
      _truthjeteta = truthjet->get_eta();
      _truthjetpt = truthjet->get_pt();
      _truthjetenergy = truthjet->get_e();
      _truthjetpx = truthjet->get_px();
      _truthjetpy = truthjet->get_py();
      _truthjetpz = truthjet->get_pz();
    }
    else{
      _truthjetid = 0;
      _truthjetp = 0;
      _truthjetphi = 0;
      _truthjeteta = 0;
      _truthjetpt = 0;
      _truthjetenergy = 0;
      _truthjetpx = 0;
      _truthjetpy = 0;
      _truthjetpz = 0;

    }
    
    jetdphi = trig_phi-_recojetphi;
    if(jetdphi<pi2)
      jetdphi+=2.*pi;
    if(jetdphi>threepi2)
      jetdphi-=2.*pi;
    
    jetdeta = trig_eta-_recojeteta;
    jetpout = _recojetpt*TMath::Sin(jetdphi);

    isophot_jet_tree->Fill();
  }


}
int SvtxSimPerformanceCheckReco::process_event(PHCompositeNode *topNode) {

  ++_event;
  
  // need things off of the DST...
  PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");
  if (!truthinfo) {
    cerr << PHWHERE << " ERROR: Can't find G4TruthInfo" << endl;
    exit(-1);
  }

  SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
  if (!trackmap) {
    cerr << PHWHERE << " ERROR: Can't find SvtxTrackMap" << endl;
    exit(-1);
  }

  SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
  if (!vertexmap) {
    cerr << PHWHERE << " ERROR: Can't find SvtxVertexMap" << endl;
    exit(-1);
  }

  // create SVTX eval stack
  SvtxEvalStack svtxevalstack(topNode);

  SvtxVertexEval*   vertexeval = svtxevalstack.get_vertex_eval();
  SvtxTrackEval*     trackeval = svtxevalstack.get_track_eval();
  SvtxTruthEval*     trutheval = svtxevalstack.get_truth_eval();
  
  // loop over all truth particles
  PHG4TruthInfoContainer::Range range = truthinfo->GetPrimaryParticleRange();
  for (PHG4TruthInfoContainer::ConstIterator iter = range.first; 
       iter != range.second; 
       ++iter) {
    PHG4Particle* g4particle = iter->second;
    if (trutheval->get_embed(g4particle) <= 0) continue;
    
    std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);     
    float ng4hits = g4hits.size();  

    float truept = sqrt(pow(g4particle->get_px(),2)+pow(g4particle->get_py(),2));
    
    // examine truth particles that leave 7 detector hits
    if (ng4hits == _nlayers) {
      _truept_particles_leaving7Hits->Fill(truept);
    
      SvtxTrack* track = trackeval->best_track_from(g4particle);
    
      if (!track) {continue;}
      
      unsigned int nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);
      float recopt = track->get_pt();

      unsigned int ndiff = abs((int)nfromtruth-(int)_nlayers);
      if (ndiff <= 2) {
	_truept_particles_recoWithin2Hits->Fill(truept);
      }
      if (ndiff <= 1) {
	_truept_particles_recoWithin1Hit->Fill(truept);
      }
      if (ndiff == 0) {
	_truept_particles_recoWithExactHits->Fill(truept);
      } 

      unsigned int layersfromtruth = trackeval->get_nclusters_contribution_by_layer(track,g4particle);
      unsigned int innerhits = (layersfromtruth & _inner_layer_mask);
      unsigned int ninnerhitsfromtruth = 0;
      unsigned int ninnerlayers = 0;
      for (unsigned int i = 0; i < 32; ++i) {
	ninnerhitsfromtruth += (0x1 & (innerhits >> i));
	ninnerlayers += (0x1 & (_inner_layer_mask >> i));
      }
      
      ndiff = abs((int)ninnerhitsfromtruth-(int)ninnerlayers);
      if (ndiff <= 2) {
	_truept_particles_recoWithin2InnerHits->Fill(truept);
      }
      if (ndiff <= 1) {
	_truept_particles_recoWithin1InnerHit->Fill(truept);
      }
      if (ndiff == 0) {
	_truept_particles_recoWithExactInnerHits->Fill(truept);
      } 
      
      float diff = fabs(recopt-truept)/truept;
      if (diff < 0.05) {
	_truept_particles_recoWithin5Percent->Fill(truept);
      }
      if (diff < 0.04) {
	_truept_particles_recoWithin4Percent->Fill(truept);
	_truept_quality_particles_recoWithin4Percent->Fill(truept,track->get_quality());
      }
      if (diff < 0.03) {
	_truept_particles_recoWithin3Percent->Fill(truept);
      }      
    }    
  }
Esempio n. 15
0
void PHG4HoughTransform::projectToRadius(const SvtxTrack& track, double radius, vector<double>& intersection)
{
  float phi = track.phi;
  float d = track.d;
  float k = track.kappa;
  float z0 = track.z0;
  float dzdl = track.dzdl;
  
  intersection.clear();intersection.assign(3,0.);
  double& x = intersection[0];
  double& y = intersection[1];
  double& z = intersection[2];
  
  float rad_det = radius;
  
  float cosphi = cos(phi);
  float sinphi = sin(phi);
  
  // get outer hit
  float hitx = d*cosphi;
  float hity = d*sinphi;
  int nhits = track.getNhits();
  for(int l=(nhits-1);l>=0;l-=1)
  {
    if(track.getClusterID(l) >= 0)
    {
      hitx = track.getHitPosition(l, 0);
      hity = track.getHitPosition(l, 1);
      break;
    }
  }
  
  
  
  k = fabs(k);
  
  float kd = (d*k + 1.);
  float kcx = kd*cosphi;
  float kcy = kd*sinphi;
  float kd_inv = 1./kd;
  float R2 = rad_det*rad_det;
  float a = 0.5*(k*R2 + ( d*d*k + 2.*d ))*kd_inv;
  float tmp1 = a*kd_inv;
  float P2x = kcx*tmp1;
  float P2y = kcy*tmp1;
  
  float h = sqrt(R2 - a*a);
  
  float ux = -kcy*kd_inv;
  float uy = kcx*kd_inv;
  
  float x1 = P2x + ux*h;
  float y1 = P2y + uy*h;
  float x2 = P2x - ux*h;
  float y2 = P2y - uy*h;
  float diff1 = (x1-hitx)*(x1-hitx) + (y1-hity)*(y1-hity);
  float diff2 = (x2-hitx)*(x2-hitx) + (y2-hity)*(y2-hity);
  float signk = 0.;
  if(diff1 < diff2){signk = 1.;}
  else{signk = -1.;}
  x = P2x + signk*ux*h;
  y = P2y + signk*uy*h;
  
  double sign_dzdl = sign(dzdl);
  double startx = d*cosphi;
  double starty = d*sinphi;
  double D = sqrt((startx-x)*(startx-x) + (starty-y)*(starty-y));
  double v = 0.5*k*D;
  z = 0.;
  if(v > 0.1)
  {
    if(v >= 0.999999){v=0.999999;}
    double s = 2.*asin(v)/k;
    double dz = sqrt(s*s*dzdl*dzdl/(1. - dzdl*dzdl));
    z = z0 + sign_dzdl*dz;
  }
  else
  {
    double s = 0.;
    double temp1 = k*D*0.5;temp1*=temp1;
    double temp2 = D*0.5;
    s += 2.*temp2;
    temp2*=temp1;
    s += temp2/3.;
    temp2*=temp1;
    s += (3./20.)*temp2;
    temp2*=temp1;
    s += (5./56.)*temp2;
    double dz = sqrt(s*s*dzdl*dzdl/(1. - dzdl*dzdl));
    z = z0 + sign_dzdl*dz;
  }
}
int MomentumEvaluator::process_event( PHCompositeNode *topNode )
{
	PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");

	PHG4HitContainer* g4hits = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_SVTX");
	if(g4hits == nullptr){cout<<"can't find PHG4HitContainer"<<endl;exit(1);}
	PHG4HitContainer::ConstRange g4range = g4hits->getHits();

	// set<int> trkids;
	map<int, pair<unsigned int,unsigned int> > trkids;

	for( PHG4HitContainer::ConstIterator iter = g4range.first; iter != g4range.second; ++iter )
	{
		PHG4Hit* hit = iter->second;

		int layer = hit->get_layer();
		float length = outer_z_length;
		if(((unsigned int)layer)<n_inner_layers){length=inner_z_length;}
		if(fabs(hit->get_z(0))>length){continue;}

		int trk_id = hit->get_trkid();
		if(trkids.find(trk_id) == trkids.end())
		{
			trkids[trk_id].first = 0;
			trkids[trk_id].second = 0;
		}
		if( hit->get_layer() < 32 )
		{
			trkids[trk_id].first = (trkids[trk_id].first | (1<<(hit->get_layer())));
		}
		else
		{
			trkids[trk_id].second = (trkids[trk_id].second | (1<<(hit->get_layer()-32)));
		}
		
		// cout<<"trk_id = "<<trk_id<<endl;
		// cout<<"layer = "<<hit->get_layer()<<endl;
		// cout<<"nlayer = "<<__builtin_popcount(trkids[trk_id].first)+__builtin_popcount(trkids[trk_id].second)<<endl<<endl;
		// trkids.insert(trk_id);
	}


	SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");

	PHG4VtxPoint *gvertex = truthinfo->GetPrimaryVtx( truthinfo->GetPrimaryVertexIndex() );
	float gvx = gvertex->get_x();
	float gvy = gvertex->get_y();
	float gvz = gvertex->get_z();

	RecursiveMomentumContainer true_sorted( -20., 20., -20., 20., -20., 20., 10 );

	// PHG4TruthInfoContainer::Map primarymap = truthinfo->GetPrimaryMap();
	PHG4TruthInfoContainer::Map primarymap = truthinfo->GetMap();
   for(PHG4TruthInfoContainer::Iterator iter = primarymap.begin();iter != primarymap.end();++iter)
   {
   	PHG4Particle *particle = iter->second;

   	float vx = truthinfo->GetVtx(particle->get_vtx_id())->get_x();
   	float vy = truthinfo->GetVtx(particle->get_vtx_id())->get_y();
   	float vz = truthinfo->GetVtx(particle->get_vtx_id())->get_z();
   	
   	TrivialTrack track( particle->get_px(), particle->get_py(), particle->get_pz(), vx-gvx, vy-gvy, vz-gvz );

   	if( ( (track.px * track.px) + (track.py * track.py) ) < (0.1*0.1) ){continue;}

   	if( trkids.find(particle->get_track_id()) == trkids.end() )
   	{
   		continue;
   	}

   	// cout<<"trk, nhits = "<<particle->get_track_id()<<" "<<__builtin_popcount(trkids[particle->get_track_id()].first)+__builtin_popcount(trkids[particle->get_track_id()].second)<<endl;

   	if( __builtin_popcount(trkids[particle->get_track_id()].first)+__builtin_popcount(trkids[particle->get_track_id()].second) < (int)n_required_layers )
   	{
   		continue;
   	}

   	true_sorted.insert( track );
   }


   RecursiveMomentumContainer reco_sorted( -20., 20., -20., 20., -20., 20., 10 );
   for(SvtxTrackMap::Iter iter = trackmap->begin();iter != trackmap->end();++iter)
   {
   	SvtxTrack* track = iter->second;

   	TrivialTrack ttrack( track->get_px(), track->get_py(), track->get_pz(), track->get_x()-gvx, track->get_y()-gvy, track->get_z()-gvz, track->get_quality() );
   	reco_sorted.insert(ttrack);
   }


   TrivialTrack* t_track = true_sorted.begin();
   vector<TrivialTrack*> pointer_list;
   while(t_track != nullptr)
   {
   	pointer_list.clear();

   	float pt = sqrt((t_track->px * t_track->px) + (t_track->py * t_track->py));
   	float pt_diff = pt*pt_search_scale;
   	float px_lo = t_track->px - pt_diff;
   	float px_hi = t_track->px + pt_diff;
   	float py_lo = t_track->py - pt_diff;
   	float py_hi = t_track->py + pt_diff;
   	float pz_diff = fabs( t_track->pz )*pz_search_scale;
   	float pz_lo = t_track->pz - pz_diff;
   	float pz_hi = t_track->pz + pz_diff;

   	reco_sorted.append_list( pointer_list, px_lo,px_hi, py_lo,py_hi, pz_lo,pz_hi );

   	if(pointer_list.size() > 0)
   	{
   		float mom_true = sqrt(pt*pt + (t_track->pz)*(t_track->pz));
   		float best_ind = 0;
   		float mom_reco = sqrt( (pointer_list[0]->px)*(pointer_list[0]->px) + (pointer_list[0]->py)*(pointer_list[0]->py) + (pointer_list[0]->pz)*(pointer_list[0]->pz) );
   		float best_mom = mom_reco;
   		for(unsigned int i=1;i<pointer_list.size();++i)
   		{
   			mom_reco = sqrt( (pointer_list[i]->px)*(pointer_list[i]->px) + (pointer_list[i]->py)*(pointer_list[i]->py) + (pointer_list[i]->pz)*(pointer_list[i]->pz) );
   			if( fabs( mom_true - mom_reco ) < fabs( mom_true - best_mom )  )
   			{
   				best_mom = mom_reco;
   				best_ind = i;
   			}
   		}
   		
   		float ntp_data[14] = { (float) event_counter, t_track->px, t_track->py, t_track->pz, t_track->dcax, t_track->dcay, t_track->dcaz, pointer_list[best_ind]->px, pointer_list[best_ind]->py, pointer_list[best_ind]->pz, pointer_list[best_ind]->dcax, pointer_list[best_ind]->dcay, pointer_list[best_ind]->dcaz, pointer_list[best_ind]->quality };
   		ntp_true->Fill(ntp_data);
   	}
   	else
   	{
   		float ntp_data[14] = { (float) event_counter, t_track->px, t_track->py, t_track->pz, t_track->dcax, t_track->dcay, t_track->dcaz, -9999.,-9999.,-9999.,-9999.,-9999.,-9999., -9999. };
   		ntp_true->Fill(ntp_data);
   	}

   	t_track = true_sorted.next();
   }

   TrivialTrack* r_track = reco_sorted.begin();
   while(r_track != nullptr)
   {
   	pointer_list.clear();

   	float pt = sqrt((r_track->px * r_track->px) + (r_track->py * r_track->py));
   	float pt_diff = pt*pt_search_scale;
   	float px_lo = r_track->px - pt_diff;
   	float px_hi = r_track->px + pt_diff;
   	float py_lo = r_track->py - pt_diff;
   	float py_hi = r_track->py + pt_diff;
   	float pz_diff = fabs( r_track->pz )*pz_search_scale;
   	float pz_lo = r_track->pz - pz_diff;
   	float pz_hi = r_track->pz + pz_diff;

   	true_sorted.append_list( pointer_list, px_lo,px_hi, py_lo,py_hi, pz_lo,pz_hi );

   	if(pointer_list.size() > 0)
   	{
   		float mom_reco = sqrt(pt*pt + (r_track->pz)*(r_track->pz));
   		float best_ind = 0;
   		float mom_true = sqrt( (pointer_list[0]->px)*(pointer_list[0]->px) + (pointer_list[0]->py)*(pointer_list[0]->py) + (pointer_list[0]->pz)*(pointer_list[0]->pz) );
   		float best_mom = mom_true;
   		for(unsigned int i=1;i<pointer_list.size();++i)
   		{
   			mom_true = sqrt( (pointer_list[i]->px)*(pointer_list[i]->px) + (pointer_list[i]->py)*(pointer_list[i]->py) + (pointer_list[i]->pz)*(pointer_list[i]->pz) );
   			if( fabs( mom_reco - mom_true ) < fabs( mom_reco - best_mom )  )
   			{
   				best_mom = mom_true;
   				best_ind = i;
   			}
   		}
   		
   		float ntp_data[14] = { (float) event_counter, r_track->px, r_track->py, r_track->pz, r_track->dcax, r_track->dcay, r_track->dcaz, pointer_list[best_ind]->px, pointer_list[best_ind]->py, pointer_list[best_ind]->pz, pointer_list[best_ind]->dcax, pointer_list[best_ind]->dcay, pointer_list[best_ind]->dcaz, r_track->quality };
   		ntp_reco->Fill(ntp_data);
   	}
   	else
   	{
   		float ntp_data[14] = { (float) event_counter, r_track->px, r_track->py, r_track->pz, r_track->dcax, r_track->dcay, r_track->dcaz, -9999.,-9999.,-9999.,-9999.,-9999.,-9999., r_track->quality };
   		ntp_reco->Fill(ntp_data);
   	}

   	r_track = reco_sorted.next();
   }


   event_counter += 1;
   return Fun4AllReturnCodes::EVENT_OK;
}
int PHG4TrackGhostRejection::process_event(PHCompositeNode *topNode)
{
  if(verbosity > 0) cout << "PHG4TrackGhostRejection::process_event -- entered" << endl;

  //---------------------------------
  // Get Objects off of the Node Tree
  //---------------------------------

  // Pull the reconstructed track information off the node tree...
  _g4tracks = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
  if(!_g4tracks) 
    {
      cerr << PHWHERE << " ERROR: Can't find SvtxTrackMap." << endl;
      return Fun4AllReturnCodes::ABORTEVENT;
    }

  if (verbosity > 1) {
    _g4tracks->identify();
    for (SvtxTrackMap::Iter iter = _g4tracks->begin();
	 iter != _g4tracks->end();
	 ++iter) {
      SvtxTrack *track = iter->second;
      track->identify();
    }
  }

  //----------------------------
  // Sort the hits on each track
  //----------------------------

  _candidates.clear();
  
  for (SvtxTrackMap::Iter iter = _g4tracks->begin();
       iter != _g4tracks->end();
       ++iter) {

    SvtxTrack* track = iter->second;
  
    PHG4TrackCandidate combo;

    combo.trackid = track->get_id();
    combo.nhits = track->size_clusters();

    for (SvtxTrack::ConstClusterIter iter = track->begin_clusters();
	 iter != track->end_clusters();
	 ++iter) {
      unsigned int cluster_id = *iter;
      combo.hitids.push_back(cluster_id);
    }
      
    if (track->get_ndf() != 0) {
      combo.chisq = track->get_chisq()/track->get_ndf();
    }

    combo.keep = true;

    // sort the hits by index
    stable_sort(combo.hitids.begin(),combo.hitids.end(),hit_sort);

    _candidates.push_back(combo);
  }

  //---------------------
  // Fill the overlap map
  //---------------------

  _overlapping.clear();
  for(unsigned int i = 0; i < _candidates.size(); i++)
    {
      for(unsigned int j = i+1; j < _candidates.size(); j++)
  	{ 
	  // determine the maximum length of the anticipated storage
	  unsigned maxhits = _candidates[i].hitids.size();
	  if(_candidates[j].hitids.size() > maxhits)
	    {
	      maxhits = _candidates[j].hitids.size();
	    }

	  // create the difference storage
	  std::vector<unsigned int> diff;
	  diff.assign(maxhits,0);

	  // run the difference algorithm
	  std::vector<unsigned int>::iterator it = diff.begin();
	  it = std::set_difference(_candidates[i].hitids.begin(),_candidates[i].hitids.end(),
				   _candidates[j].hitids.begin(),_candidates[j].hitids.end(),
				   diff.begin());

	  // calculate the overlap
	  unsigned int overlap = maxhits - int(it - diff.begin());

	  // insert an overlapping pair into the map
	  if(overlap > _max_shared_hits) _overlapping.insert(std::make_pair(i,j));	  
   	}
    }

  //----------------------
  // Flag the ghost tracks
  //----------------------

  std::multimap<unsigned,unsigned int>::iterator iter;
  for (iter = _overlapping.begin(); iter != _overlapping.end(); iter++) {

    unsigned int key = iter->first;
    unsigned int value = iter->second;

    if (_candidates[key].nhits > _candidates[value].nhits) {
      // prefer longer track
      _candidates[value].keep = false;
    } else if (_candidates[key].nhits < _candidates[value].nhits) {
      // prefer longer track
      _candidates[key].nhits = false;
    } else {
      // choose between equal length tracks by chisq/dof
      if (_candidates[key].chisq < _candidates[value].chisq) {
	_candidates[value].keep = false;
      } else {
	_candidates[key].keep = false;
      }
    }
  }

  //------------------------
  // Remove the ghost tracks
  //------------------------

  SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap");
  
  int initial_size = _g4tracks->size();

  // loop over container and delete!
  for (unsigned int i = 0; i < _candidates.size(); i++) {
    if (!_candidates[i].keep) {
      // look for the track to delete
      if (_g4tracks->find(_candidates[i].trackid) != _g4tracks->end()) {
	_g4tracks->erase(_candidates[i].trackid);

	// also remove the track id from any vertex that contains this track
	if (!vertexmap) continue;
	for (SvtxVertexMap::Iter iter = vertexmap->begin();
	     iter != vertexmap->end();
	     ++iter) {
	  SvtxVertex* vertex = iter->second;
	  vertex->erase_track(_candidates[i].trackid);
	}
      }
    }
  }

  if (verbosity > 1) {
    _g4tracks->identify();
    for (SvtxTrackMap::Iter iter = _g4tracks->begin();
	 iter != _g4tracks->end();
	 ++iter) {
      SvtxTrack *track = iter->second;
      track->identify();
    }
  }

  if(verbosity > 0)
    cout << "PHG4TrackGhostRejection - rejected and removed " 
         << initial_size - _g4tracks->size()
         << " tracks" << endl;;
  
  if(verbosity > 0) cout << "PHG4TrackGhostRejection::process_event -- exited" << endl;

  return Fun4AllReturnCodes::EVENT_OK;
}
int SimpleTrackingAnalysis::process_event(PHCompositeNode *topNode)
{

  // --- This is the class process_event method
  // --- This is where the bulk of the analysis is done
  // --- Here we get the various data nodes we need to do the analysis
  // --- Then we use variables (accessed through class methods) to perform calculations

  if ( verbosity > -1 )
    {
      cout << endl;
      cout << "------------------------------------------------------------------------------------" << endl;
      cout << "Now processing event number " << nevents << endl; // would be good to add verbosity switch
    }

  ++nevents; // You may as youtself, why ++nevents (pre-increment) rather
  // than nevents++ (post-increment)?  The short answer is performance.
  // For simple types it probably doesn't matter, but it can really help
  // for complex types (like the iterators below).


  // --- Truth level information
  PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode,"G4TruthInfo");
  if ( !truthinfo )
    {
      cerr << PHWHERE << " ERROR: Can't find G4TruthInfo" << endl;
      exit(-1);
    }

  // --- SvtxTrackMap
  SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode,"SvtxTrackMap");
  if ( !trackmap )
    {
      cerr << PHWHERE << " ERROR: Can't find SvtxTrackMap" << endl;
      exit(-1);
    }

  // --- SvtxVertexMap
  SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode,"SvtxVertexMap");
  if ( !vertexmap )
    {
      cerr << PHWHERE << " ERROR: Can't find SvtxVertexMap" << endl;
      exit(-1);
    }



  // --- Create SVTX eval stack
  SvtxEvalStack svtxevalstack(topNode);
  // --- Get evaluator objects from the eval stack
  SvtxVertexEval*   vertexeval = svtxevalstack.get_vertex_eval();
  SvtxTrackEval*     trackeval = svtxevalstack.get_track_eval();
  SvtxTruthEval*     trutheval = svtxevalstack.get_truth_eval();



  if ( verbosity > 0 ) cout << "Now going to loop over truth partcles..." << endl; // need verbosity switch

  // --- Loop over all truth particles
  PHG4TruthInfoContainer::Range range = truthinfo->GetPrimaryParticleRange();
  for ( PHG4TruthInfoContainer::ConstIterator iter = range.first; iter != range.second; ++iter )
    {
      PHG4Particle* g4particle = iter->second; // You may ask yourself, why second?
      // In C++ the iterator is a map, which has two members
      // first is the key (analogous the index of an arry),
      // second is the value (analogous to the value stored for the array index)
      int particleID = g4particle->get_pid();

      if ( trutheval->get_embed(g4particle) <= 0 && fabs(particleID) == 11 && verbosity > 0 )
        {
          cout << "NON EMBEDDED ELECTRON!!!  WHEE!!! " << particleID << " " << iter->first << endl;
        }

      if ( trutheval->get_embed(g4particle) <= 0 ) continue; // only look at embedded particles // no good for hits files
      bool iselectron = fabs(particleID) == 11;
      bool ispion = fabs(particleID) == 211;
      if ( verbosity > 0 ) cout << "embedded particle ID is " << particleID << " ispion " << ispion << " iselectron " << iselectron << " " << iter->first << endl;

      set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);
      float ng4hits = g4hits.size();

      float truept = sqrt(pow(g4particle->get_px(),2)+pow(g4particle->get_py(),2));
      float true_energy = g4particle->get_e();

      // --- Get the reconsructed SvtxTrack based on the best candidate from the truth info
      SvtxTrack* track = trackeval->best_track_from(g4particle);
      if (!track) continue;
      float recopt = track->get_pt();
      float recop = track->get_p();

      if ( verbosity > 0 )
	{
	  cout << "truept is " << truept << endl;
	  cout << "recopt is " << recopt << endl;
	  cout << "true energy is " << true_energy << endl;
	}

      // --- energy variables directly from the track object
      float emc_energy_track = track->get_cal_energy_3x3(SvtxTrack::CEMC);
      float hci_energy_track = track->get_cal_energy_3x3(SvtxTrack::HCALIN);
      float hco_energy_track = track->get_cal_energy_3x3(SvtxTrack::HCALOUT);

      if ( verbosity > 0 )
	{
	  cout << "emc_energy_track is " << emc_energy_track << endl;
	  cout << "hci_energy_track is " << hci_energy_track << endl;
	  cout << "hco_energy_track is " << hco_energy_track << endl;
	}

      // -------------------------------------------------------------------------------------
      // --- IMPORTANT NOTE: according to Jin, dphi and deta will not work correctly in HIJING

      float emc_dphi_track = track->get_cal_dphi(SvtxTrack::CEMC);
      float hci_dphi_track = track->get_cal_dphi(SvtxTrack::HCALIN);
      float hco_dphi_track = track->get_cal_dphi(SvtxTrack::HCALOUT);

      float emc_deta_track = track->get_cal_deta(SvtxTrack::CEMC);
      float hci_deta_track = track->get_cal_deta(SvtxTrack::HCALIN);
      float hco_deta_track = track->get_cal_deta(SvtxTrack::HCALOUT);

      float assoc_dphi = 0.1; // adjust as needed, consider class set method
      float assoc_deta = 0.1; // adjust as needed, consider class set method
      bool good_emc_assoc = fabs(emc_dphi_track) < assoc_dphi && fabs(emc_deta_track) < assoc_deta;
      bool good_hci_assoc = fabs(hci_dphi_track) < assoc_dphi && fabs(hci_deta_track) < assoc_deta;
      bool good_hco_assoc = fabs(hco_dphi_track) < assoc_dphi && fabs(hco_deta_track) < assoc_deta;

      // ------------------------------------------------------------------------------------------


      float hct_energy_track = 0;
      if ( hci_energy_track >= 0 ) hct_energy_track += hci_energy_track;
      if ( hco_energy_track >= 0 ) hct_energy_track += hco_energy_track;

      float total_energy_dumb = 0;
      if ( emc_energy_track >= 0 ) total_energy_dumb += emc_energy_track;
      if ( hci_energy_track >= 0 ) total_energy_dumb += hci_energy_track;
      if ( hco_energy_track >= 0 ) total_energy_dumb += hco_energy_track;

      float total_energy_smart = 0;
      if ( good_emc_assoc ) total_energy_smart += emc_energy_track;
      if ( good_hci_assoc ) total_energy_smart += hci_energy_track;
      if ( good_hco_assoc ) total_energy_smart += hco_energy_track;



      // ----------------------------------------------------------------------
      // ----------------------------------------------------------------------
      // ----------------------------------------------------------------------

      //cout << "starting the main part of the truth analysis" << endl;

      // examine truth particles that leave all (7 or 8 depending on design) detector hits
      if ( ng4hits == nlayers )
	{
	  _truept_particles_leavingAllHits->Fill(truept);

	  unsigned int nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);

	  unsigned int ndiff = abs((int)nfromtruth-(int)nlayers);
	  if ( ndiff <= 2 ) _truept_particles_recoWithin2Hits->Fill(truept);
	  if ( ndiff <= 1 ) _truept_particles_recoWithin1Hit->Fill(truept);
	  if ( ndiff == 0 ) _truept_particles_recoWithExactHits->Fill(truept);

	  float diff = fabs(recopt-truept)/truept;
	  if ( diff < 0.05 ) _truept_particles_recoWithin5Percent->Fill(truept);
	  if ( diff < 0.04 )
	    {
	      _truept_particles_recoWithin4Percent->Fill(truept);
	      _truept_quality_particles_recoWithin4Percent->Fill(truept,track->get_quality());
	    }
	  if ( diff < 0.03 ) _truept_particles_recoWithin3Percent->Fill(truept);

          double good_energy = total_energy_dumb - 3.14;


          double eoverp = good_energy/recop;
          double sigmapt = 0.011 + 0.0008*recopt;
          th2d_truept_particles_withcalocuts_leavingAllHits->Fill(truept,eoverp);
          if ( ndiff <= 2 ) th2d_truept_particles_withcalocuts_recoWithin2Hits->Fill(truept,eoverp);
          if ( ndiff <= 1 ) th2d_truept_particles_withcalocuts_recoWithin1Hit->Fill(truept,eoverp);
          if ( ndiff == 0 ) th2d_truept_particles_withcalocuts_recoWithExactHits->Fill(truept,eoverp);
          if ( diff < 0.05 ) th2d_truept_particles_withcalocuts_recoWithin5Percent->Fill(truept,eoverp);
          if ( diff < 0.04 ) th2d_truept_particles_withcalocuts_recoWithin4Percent->Fill(truept,eoverp);
          if ( diff < 0.03 ) th2d_truept_particles_withcalocuts_recoWithin3Percent->Fill(truept,eoverp);
          if ( diff < 1.0*sigmapt ) th2d_truept_particles_withcalocuts_recoWithin1Sigma->Fill(recopt,eoverp);
          if ( diff < 2.0*sigmapt ) th2d_truept_particles_withcalocuts_recoWithin2Sigma->Fill(recopt,eoverp);
          if ( diff < 3.0*sigmapt ) th2d_truept_particles_withcalocuts_recoWithin3Sigma->Fill(recopt,eoverp);


	} // end of requirement of ng4hits == nlayers

    } // end of loop over truth particles



  // loop over all reco particles
  int ntracks = 0;
  for ( SvtxTrackMap::Iter iter = trackmap->begin(); iter != trackmap->end(); ++iter )
    {

      // --- Get the StxTrack object (from the iterator)
      SvtxTrack* track = iter->second;
      float recopt = track->get_pt();
      float recop = track->get_p();

      // --- Get the truth particle from the evaluator
      PHG4Particle* g4particle = trackeval->max_truth_particle_by_nclusters(track);
      float truept = sqrt(pow(g4particle->get_px(),2)+pow(g4particle->get_py(),2));
      int particleID = g4particle->get_pid();
      if ( verbosity > 5 ) cout << "particle ID is " << particleID << endl;
      bool iselectron = fabs(particleID) == 11;
      bool ispion = fabs(particleID) == 211;

      // ---------------------
      // --- calorimeter stuff
      // ---------------------

      // --- get the energy values directly from the track
      float emc_energy_track = track->get_cal_energy_3x3(SvtxTrack::CEMC);
      float hci_energy_track = track->get_cal_energy_3x3(SvtxTrack::HCALIN);
      float hco_energy_track = track->get_cal_energy_3x3(SvtxTrack::HCALOUT);

      float total_energy = 0;
      if ( emc_energy_track > 0 ) total_energy += emc_energy_track;
      if ( hci_energy_track > 0 ) total_energy += hci_energy_track;
      if ( hco_energy_track > 0 ) total_energy += hco_energy_track;

      if ( verbosity > 2 ) cout << "total calo energy is " << total_energy << endl;

      if (trutheval->get_embed(g4particle) > 0)
	{
	  // embedded results (quality or performance measures)
	  _truept_dptoverpt->Fill(truept,(recopt-truept)/truept);
	  _truept_dca->Fill(truept,track->get_dca2d());
	  _recopt_quality->Fill(recopt,track->get_quality());
          if ( verbosity > 0 ) cout << "embedded particle ID is " << particleID << " ispion " << ispion << " iselectron " << iselectron << endl;
          // ---
	} // end if (embedded results)
      else
	{
          // electron and pion (hadron) id

	  // non-embedded results (purity measures)
	  _recopt_tracks_all->Fill(recopt);
	  _recopt_quality_tracks_all->Fill(recopt,track->get_quality());

	  unsigned int nfromtruth = trackeval->get_nclusters_contribution(track,g4particle);

	  unsigned int ndiff = abs((int)nfromtruth-(int)nlayers);
	  if ( ndiff <= 2 ) _recopt_tracks_recoWithin2Hits->Fill(recopt);
	  if ( ndiff <= 1 ) _recopt_tracks_recoWithin1Hit->Fill(recopt);
	  if ( ndiff == 0 ) _recopt_tracks_recoWithExactHits->Fill(recopt);

	  float diff = fabs(recopt-truept)/truept;
	  if ( diff < 0.05 ) _recopt_tracks_recoWithin5Percent->Fill(recopt);
	  if ( diff < 0.04 )
	    {
	      _recopt_tracks_recoWithin4Percent->Fill(recopt);
	      _recopt_quality_tracks_recoWithin4Percent->Fill(recopt,track->get_quality());
	    }
	  if ( diff < 0.03 ) _recopt_tracks_recoWithin3Percent->Fill(recopt);


	  // --------------------------------------
	  // --- same but now with calorimeter cuts
	  // --------------------------------------

          double good_energy = total_energy - 3.14;

          double eoverp = good_energy/recop;
          double sigmapt = 0.011 + 0.0008*recopt;
          th2d_recopt_tracks_withcalocuts_all->Fill(recopt,eoverp);
          if ( ndiff <= 2 ) th2d_recopt_tracks_withcalocuts_recoWithin2Hits->Fill(recopt,eoverp);
          if ( ndiff <= 1 ) th2d_recopt_tracks_withcalocuts_recoWithin1Hit->Fill(recopt,eoverp);
          if ( ndiff == 0 ) th2d_recopt_tracks_withcalocuts_recoWithExactHits->Fill(recopt,eoverp);
          if ( diff < 0.05 ) th2d_recopt_tracks_withcalocuts_recoWithin5Percent->Fill(recopt,eoverp);
          if ( diff < 0.04 ) th2d_recopt_tracks_withcalocuts_recoWithin4Percent->Fill(recopt,eoverp);
          if ( diff < 0.03 ) th2d_recopt_tracks_withcalocuts_recoWithin3Percent->Fill(recopt,eoverp);
          if ( diff < 1.0*sigmapt ) th2d_recopt_tracks_withcalocuts_recoWithin1Sigma->Fill(recopt,eoverp);
          if ( diff < 2.0*sigmapt ) th2d_recopt_tracks_withcalocuts_recoWithin2Sigma->Fill(recopt,eoverp);
          if ( diff < 3.0*sigmapt ) th2d_recopt_tracks_withcalocuts_recoWithin3Sigma->Fill(recopt,eoverp);

	  // --- done with reco tracks

	} // else (non-embedded results)

      ++ntracks;
    } // loop over reco tracks


  hmult->Fill(ntracks);

  // --- Get the leading vertex
  SvtxVertex* maxvertex = NULL;
  unsigned int maxtracks = 0;
  for ( SvtxVertexMap::Iter iter = vertexmap->begin(); iter != vertexmap->end(); ++iter )
    {
      SvtxVertex* vertex = iter->second;
      if ( vertex->size_tracks() > maxtracks )
	{
	  maxvertex = vertex;
	  maxtracks = vertex->size_tracks();
	}
    }
  if ( !maxvertex )
    {
      cerr << PHWHERE << " ERROR: cannot get reconstructed vertex (event number " << nevents << ")" << endl;
      ++nerrors;
      return Fun4AllReturnCodes::DISCARDEVENT;
    }

  // --- Get the coordinates for the vertex from the evaluator
  PHG4VtxPoint* point = vertexeval->max_truth_point_by_ntracks(maxvertex);
  if ( !point )
    {
      cerr << PHWHERE << " ERROR: cannot get truth vertex (event number " << nevents << ")" << endl;
      ++nerrors;
      return Fun4AllReturnCodes::DISCARDEVENT;
    }
  _dx_vertex->Fill(maxvertex->get_x() - point->get_x());
  _dy_vertex->Fill(maxvertex->get_y() - point->get_y());
  _dz_vertex->Fill(maxvertex->get_z() - point->get_z());

  hmult_vertex->Fill(ntracks);



  return Fun4AllReturnCodes::EVENT_OK;

}