Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:21:41

0001 #include "CaloEvaluator.h"
0002 
0003 #include "CaloEvalStack.h"
0004 #include "CaloRawClusterEval.h"
0005 #include "CaloRawTowerEval.h"
0006 #include "CaloTruthEval.h"
0007 
0008 #include <g4main/PHG4Particle.h>
0009 #include <g4main/PHG4Shower.h>
0010 #include <g4main/PHG4TruthInfoContainer.h>
0011 #include <g4main/PHG4VtxPoint.h>
0012 
0013 #include <globalvertex/GlobalVertex.h>
0014 #include <globalvertex/GlobalVertexMap.h>
0015 
0016 #include <calobase/RawCluster.h>
0017 #include <calobase/RawClusterContainer.h>
0018 #include <calobase/RawClusterUtility.h>
0019 #include <calobase/RawTower.h>
0020 #include <calobase/RawTowerContainer.h>
0021 #include <calobase/RawTowerGeom.h>
0022 #include <calobase/RawTowerGeomContainer.h>
0023 #include <calobase/TowerInfo.h>
0024 #include <calobase/TowerInfoContainer.h>
0025 
0026 #include <fun4all/Fun4AllReturnCodes.h>
0027 #include <fun4all/SubsysReco.h>
0028 
0029 #include <phool/getClass.h>
0030 #include <phool/phool.h>
0031 
0032 #include <TFile.h>
0033 #include <TNtuple.h>
0034 #include <TTree.h>
0035 
0036 #include <CLHEP/Vector/ThreeVector.h>
0037 
0038 #include <cmath>
0039 #include <cstdlib>
0040 #include <iostream>
0041 #include <set>
0042 #include <utility>
0043 
0044 CaloEvaluator::CaloEvaluator(const std::string& name, const std::string& caloname, const std::string& filename)
0045   : SubsysReco(name)
0046   , _caloname(caloname)
0047   , _filename(filename)
0048 {
0049 }
0050 
0051 int CaloEvaluator::Init(PHCompositeNode* /*topNode*/)
0052 {
0053   delete _tfile;  // make cppcheck happy
0054   _tfile = new TFile(_filename.c_str(), "RECREATE");
0055 
0056   if (_do_gpoint_eval)
0057   {
0058     _ntp_gpoint = new TNtuple("ntp_gpoint", "primary vertex => best (first) vertex",
0059                               "event:gvx:gvy:gvz:"
0060                               "vx:vy:vz");
0061   }
0062 
0063   if (_do_gshower_eval)
0064   {
0065     _ntp_gshower = new TNtuple("ntp_gshower", "truth shower => best cluster",
0066                                "event:gparticleID:gflavor:gnhits:"
0067                                "geta:gphi:ge:gpt:gvx:gvy:gvz:gembed:gedep:"
0068                                "clusterID:ntowers:eta:x:y:z:phi:e:efromtruth");
0069   }
0070 
0071   // Barak: Added TTree to will allow the TowerID to be set correctly as integer
0072   if (_do_tower_eval)
0073   {
0074     _ntp_tower = new TNtuple("ntp_tower", "tower => max truth primary",
0075                              "event:towerID:ieta:iphi:eta:phi:e:x:y:z:"
0076                              "gparticleID:gflavor:gnhits:"
0077                              "geta:gphi:ge:gpt:gvx:gvy:gvz:"
0078                              "gembed:gedep:"
0079                              "efromtruth");
0080 
0081     // Make Tree
0082     _tower_debug = new TTree("tower_debug", "tower => max truth primary");
0083 
0084     _tower_debug->Branch("event", &_ievent, "event/I");
0085     _tower_debug->Branch("towerID", &_towerID_debug, "towerID/I");
0086     _tower_debug->Branch("ieta", &_ieta_debug, "ieta/I");
0087     _tower_debug->Branch("iphi", &_iphi_debug, "iphi/I");
0088     _tower_debug->Branch("eta", &_eta_debug, "eta/F");
0089     _tower_debug->Branch("phi", &_phi_debug, "phi/F");
0090     _tower_debug->Branch("e", &_e_debug, "e/F");
0091     _tower_debug->Branch("x", &_x_debug, "x/F");
0092     _tower_debug->Branch("y", &_y_debug, "y/F");
0093     _tower_debug->Branch("z", &_z_debug, "z/F");
0094   }
0095 
0096   if (_do_cluster_eval)
0097   {
0098     _ntp_cluster = new TNtuple("ntp_cluster", "cluster => max truth primary",
0099                                "event:clusterID:ntowers:eta:x:y:z:phi:e:"
0100                                "gparticleID:gflavor:gnhits:"
0101                                "geta:gphi:ge:gpt:gvx:gvy:gvz:"
0102                                "gembed:gedep:"
0103                                "efromtruth");
0104   }
0105 
0106   return Fun4AllReturnCodes::EVENT_OK;
0107 }
0108 
0109 int CaloEvaluator::process_event(PHCompositeNode* topNode)
0110 {
0111   if (!_caloevalstack)
0112   {
0113     _caloevalstack = new CaloEvalStack(topNode, _caloname);
0114     _caloevalstack->set_strict(_strict);
0115     _caloevalstack->set_verbosity(Verbosity() + 1);
0116   }
0117   else
0118   {
0119     _caloevalstack->next_event(topNode);
0120   }
0121 
0122   //-----------------------------------
0123   // print what is coming into the code
0124   //-----------------------------------
0125 
0126   printInputInfo(topNode);
0127 
0128   //---------------------------
0129   // fill the Evaluator NTuples
0130   //---------------------------
0131 
0132   fillOutputNtuples(topNode);
0133 
0134   //--------------------------------------------------
0135   // Print out the ancestry information for this event
0136   //--------------------------------------------------
0137 
0138   printOutputInfo(topNode);
0139 
0140   ++_ievent;
0141   ++m_EvtCounter;
0142 
0143   return Fun4AllReturnCodes::EVENT_OK;
0144 }
0145 
0146 int CaloEvaluator::End(PHCompositeNode* /*topNode*/)
0147 {
0148   _tfile->cd();
0149 
0150   if (_do_gpoint_eval)
0151   {
0152     _ntp_gpoint->Write();
0153   }
0154   if (_do_gshower_eval)
0155   {
0156     _ntp_gshower->Write();
0157   }
0158   if (_do_tower_eval)
0159   {
0160     _ntp_tower->Write();
0161     _tower_debug->Write();
0162   }
0163   if (_do_cluster_eval)
0164   {
0165     _ntp_cluster->Write();
0166   }
0167 
0168   _tfile->Close();
0169 
0170   delete _tfile;
0171 
0172   if (Verbosity() > 0)
0173   {
0174     std::cout << "========================= " << Name() << "::End() ============================" << std::endl;
0175     std::cout << " " << m_EvtCounter << " events of output written to: " << _filename << std::endl;
0176     std::cout << "===========================================================================" << std::endl;
0177   }
0178 
0179   delete _caloevalstack;
0180 
0181   return Fun4AllReturnCodes::EVENT_OK;
0182 }
0183 
0184 void CaloEvaluator::printInputInfo(PHCompositeNode* topNode)
0185 {
0186   if (Verbosity() > 2)
0187   {
0188     std::cout << "CaloEvaluator::printInputInfo() entered" << std::endl;
0189   }
0190 
0191   // print out the truth container
0192 
0193   if (Verbosity() > 1)
0194   {
0195     std::cout << std::endl;
0196     std::cout << PHWHERE << "   NEW INPUT FOR EVENT " << _ievent << std::endl;
0197     std::cout << std::endl;
0198 
0199     // need things off of the DST...
0200     PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0201     if (!truthinfo)
0202     {
0203       std::cout << PHWHERE << " ERROR: Can't find G4TruthInfo" << std::endl;
0204       exit(-1);
0205     }
0206 
0207     std::cout << Name() << ": PHG4TruthInfoContainer contents: " << std::endl;
0208 
0209     PHG4TruthInfoContainer::Range truthrange = truthinfo->GetParticleRange();
0210     for (PHG4TruthInfoContainer::Iterator truthiter = truthrange.first;
0211          truthiter != truthrange.second;
0212          ++truthiter)
0213     {
0214       PHG4Particle* particle = truthiter->second;
0215 
0216       std::cout << truthiter->first << " => pid: " << particle->get_pid()
0217                 << " pt: " << sqrt(pow(particle->get_px(), 2) + pow(particle->get_py(), 2)) << std::endl;
0218     }
0219   }
0220 
0221   return;
0222 }
0223 
0224 void CaloEvaluator::printOutputInfo(PHCompositeNode* topNode)
0225 {
0226   if (Verbosity() > 2)
0227   {
0228     std::cout << "CaloEvaluator::printOutputInfo() entered" << std::endl;
0229   }
0230 
0231   CaloRawClusterEval* clustereval = _caloevalstack->get_rawcluster_eval();
0232   clustereval->set_usetowerinfo(_use_towerinfo);
0233   CaloTruthEval* trutheval = _caloevalstack->get_truth_eval();
0234 
0235   //==========================================
0236   // print out some useful stuff for debugging
0237   //==========================================
0238 
0239   if (Verbosity() > 1)
0240   {
0241     // event information
0242     std::cout << std::endl;
0243     std::cout << PHWHERE << "   NEW OUTPUT FOR EVENT " << _ievent << std::endl;
0244     std::cout << std::endl;
0245 
0246     // need things off of the DST...
0247     PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0248     if (!truthinfo)
0249     {
0250       std::cout << PHWHERE << " ERROR: Can't find G4TruthInfo" << std::endl;
0251       exit(-1);
0252     }
0253 
0254     // need things off of the DST...
0255     GlobalVertexMap* vertexmap = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
0256 
0257     PHG4VtxPoint* gvertex = truthinfo->GetPrimaryVtx(truthinfo->GetPrimaryVertexIndex());
0258     float gvx = gvertex->get_x();
0259     float gvy = gvertex->get_y();
0260     float gvz = gvertex->get_z();
0261 
0262     float vx = std::numeric_limits<float>::quiet_NaN();
0263     float vy = std::numeric_limits<float>::quiet_NaN();
0264     float vz = std::numeric_limits<float>::quiet_NaN();
0265     if (vertexmap)
0266     {
0267       if (!vertexmap->empty())
0268       {
0269         GlobalVertex* vertex = (vertexmap->begin()->second);
0270 
0271         vx = vertex->get_x();
0272         vy = vertex->get_y();
0273         vz = vertex->get_z();
0274       }
0275     }
0276 
0277     std::cout << "vtrue = (" << gvx << "," << gvy << "," << gvz << ") => vreco = (" << vx << "," << vy << "," << vz << ")" << std::endl;
0278 
0279     PHG4TruthInfoContainer::ConstRange range = truthinfo->GetPrimaryParticleRange();
0280     for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
0281          iter != range.second;
0282          ++iter)
0283     {
0284       PHG4Particle* primary = iter->second;
0285 
0286       std::cout << std::endl;
0287 
0288       std::cout << "===Primary PHG4Particle=========================================" << std::endl;
0289       std::cout << " particle id = " << primary->get_track_id() << std::endl;
0290       std::cout << " flavor = " << primary->get_pid() << std::endl;
0291       std::cout << " (px,py,pz,e) = (";
0292 
0293       float gpx = primary->get_px();
0294       float gpy = primary->get_py();
0295       float gpz = primary->get_pz();
0296       float ge = primary->get_e();
0297 
0298       std::cout.width(5);
0299       std::cout << gpx;
0300       std::cout << ",";
0301       std::cout.width(5);
0302       std::cout << gpy;
0303       std::cout << ",";
0304       std::cout.width(5);
0305       std::cout << gpz;
0306       std::cout << ",";
0307       std::cout.width(5);
0308       std::cout << ge;
0309       std::cout << ")" << std::endl;
0310 
0311       float gpt = std::sqrt(gpx * gpx + gpy * gpy);
0312       float geta = std::numeric_limits<float>::quiet_NaN();
0313       if (gpt != 0.0)
0314       {
0315         geta = std::asinh(gpz / gpt);
0316       }
0317       float gphi = std::atan2(gpy, gpx);
0318 
0319       std::cout << "(eta,phi,e,pt) = (";
0320       std::cout.width(5);
0321       std::cout << geta;
0322       std::cout << ",";
0323       std::cout.width(5);
0324       std::cout << gphi;
0325       std::cout << ",";
0326       std::cout.width(5);
0327       std::cout << ge;
0328       std::cout << ",";
0329       std::cout.width(5);
0330       std::cout << gpt;
0331       std::cout << ")" << std::endl;
0332 
0333       PHG4VtxPoint* vtx = trutheval->get_vertex(primary);
0334       float local_gvx = vtx->get_x();
0335       float local_gvy = vtx->get_y();
0336       float local_gvz = vtx->get_z();
0337 
0338       std::cout << " vtrue = (";
0339       std::cout.width(5);
0340       std::cout << local_gvx;
0341       std::cout << ",";
0342       std::cout.width(5);
0343       std::cout << local_gvy;
0344       std::cout << ",";
0345       std::cout.width(5);
0346       std::cout << local_gvz;
0347       std::cout << ")" << std::endl;
0348 
0349       std::cout << " embed = " << trutheval->get_embed(primary) << std::endl;
0350       std::cout << " edep = " << trutheval->get_shower_energy_deposit(primary) << std::endl;
0351 
0352       std::set<RawCluster*> clusters = clustereval->all_clusters_from(primary);
0353       for (auto* cluster : clusters)
0354       {
0355         float ntowers = cluster->getNTowers();
0356         float x = cluster->get_x();
0357         float y = cluster->get_y();
0358         float z = cluster->get_z();
0359         float phi = cluster->get_phi();
0360         float e = cluster->get_energy();
0361 
0362         float efromtruth = clustereval->get_energy_contribution(cluster, primary);
0363 
0364         std::cout << " => #" << cluster->get_id() << " (x,y,z,phi,e) = (";
0365         std::cout.width(5);
0366         std::cout << x;
0367         std::cout << ",";
0368         std::cout.width(5);
0369         std::cout << y;
0370         std::cout << ",";
0371         std::cout.width(5);
0372         std::cout << z;
0373         std::cout << ",";
0374         std::cout.width(5);
0375         std::cout << phi;
0376         std::cout << ",";
0377         std::cout.width(5);
0378         std::cout << e;
0379         std::cout << "), ntowers = " << ntowers << ", efromtruth = " << efromtruth << std::endl;
0380       }
0381     }
0382     std::cout << std::endl;
0383   }
0384 
0385   return;
0386 }
0387 
0388 void CaloEvaluator::fillOutputNtuples(PHCompositeNode* topNode)
0389 {
0390   if (Verbosity() > 2)
0391   {
0392     std::cout << "CaloEvaluator::fillOutputNtuples() entered" << std::endl;
0393   }
0394 
0395   CaloRawClusterEval* clustereval = _caloevalstack->get_rawcluster_eval();
0396   CaloRawTowerEval* towereval = _caloevalstack->get_rawtower_eval();
0397   CaloTruthEval* trutheval = _caloevalstack->get_truth_eval();
0398 
0399   //----------------------
0400   // fill the Event NTuple
0401   //----------------------
0402 
0403   if (_do_gpoint_eval)
0404   {
0405     // need things off of the DST...
0406     PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0407     if (!truthinfo)
0408     {
0409       std::cout << PHWHERE << " ERROR: Can't find G4TruthInfo" << std::endl;
0410       exit(-1);
0411     }
0412 
0413     // need things off of the DST...
0414     GlobalVertexMap* vertexmap = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
0415 
0416     PHG4VtxPoint* gvertex = truthinfo->GetPrimaryVtx(truthinfo->GetPrimaryVertexIndex());
0417     float gvx = gvertex->get_x();
0418     float gvy = gvertex->get_y();
0419     float gvz = gvertex->get_z();
0420 
0421     float vx = std::numeric_limits<float>::quiet_NaN();
0422     float vy = std::numeric_limits<float>::quiet_NaN();
0423     float vz = std::numeric_limits<float>::quiet_NaN();
0424     if (vertexmap)
0425     {
0426       if (!vertexmap->empty())
0427       {
0428         GlobalVertex* vertex = (vertexmap->begin()->second);
0429 
0430         vx = vertex->get_x();
0431         vy = vertex->get_y();
0432         vz = vertex->get_z();
0433       }
0434     }
0435 
0436     float gpoint_data[7] = {(float) _ievent,
0437                             gvx,
0438                             gvy,
0439                             gvz,
0440                             vx,
0441                             vy,
0442                             vz};
0443 
0444     _ntp_gpoint->Fill(gpoint_data);
0445   }
0446 
0447   //------------------------
0448   // fill the Gshower NTuple
0449   //------------------------
0450 
0451   if (_ntp_gshower)
0452   {
0453     if (Verbosity() > 1)
0454     {
0455       std::cout << Name() << " CaloEvaluator::filling gshower ntuple..." << std::endl;
0456     }
0457 
0458     GlobalVertexMap* vertexmap = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
0459 
0460     PHG4TruthInfoContainer* truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0461     if (!truthinfo)
0462     {
0463       std::cout << PHWHERE << " ERROR: Can't find G4TruthInfo" << std::endl;
0464       exit(-1);
0465     }
0466 
0467     PHG4TruthInfoContainer::ConstRange range = truthinfo->GetPrimaryParticleRange();
0468     for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
0469          iter != range.second;
0470          ++iter)
0471     {
0472       PHG4Particle* primary = iter->second;
0473 
0474       if (primary->get_e() < _truth_e_threshold)
0475       {
0476         continue;
0477       }
0478 
0479       if (!_truth_trace_embed_flags.empty())
0480       {
0481         if (!_truth_trace_embed_flags.contains(trutheval->get_embed(primary)))
0482         {
0483           continue;
0484         }
0485       }
0486 
0487       float gparticleID = primary->get_track_id();
0488       float gflavor = primary->get_pid();
0489 
0490       PHG4Shower* shower = trutheval->get_primary_shower(primary);
0491       float gnhits = std::numeric_limits<float>::quiet_NaN();
0492       if (shower)
0493       {
0494         gnhits = shower->get_nhits(trutheval->get_caloid());
0495       }
0496       else
0497       {
0498         gnhits = 0.0;
0499       }
0500       float gpx = primary->get_px();
0501       float gpy = primary->get_py();
0502       float gpz = primary->get_pz();
0503       float ge = primary->get_e();
0504 
0505       float gpt = std::sqrt(gpx * gpx + gpy * gpy);
0506       float geta = std::numeric_limits<float>::quiet_NaN();
0507       if (gpt != 0.0)
0508       {
0509         geta = std::asinh(gpz / gpt);
0510       }
0511       float gphi = std::atan2(gpy, gpx);
0512 
0513       PHG4VtxPoint* vtx = trutheval->get_vertex(primary);
0514       float gvx = vtx->get_x();
0515       float gvy = vtx->get_y();
0516       float gvz = vtx->get_z();
0517 
0518       float gembed = trutheval->get_embed(primary);
0519       float gedep = trutheval->get_shower_energy_deposit(primary);
0520 
0521       RawCluster* cluster = clustereval->best_cluster_from(primary);
0522 
0523       float clusterID = std::numeric_limits<float>::quiet_NaN();
0524       float ntowers = std::numeric_limits<float>::quiet_NaN();
0525       float eta = std::numeric_limits<float>::quiet_NaN();
0526       float x = std::numeric_limits<float>::quiet_NaN();
0527       float y = std::numeric_limits<float>::quiet_NaN();
0528       float z = std::numeric_limits<float>::quiet_NaN();
0529       float phi = std::numeric_limits<float>::quiet_NaN();
0530       float e = std::numeric_limits<float>::quiet_NaN();
0531 
0532       float efromtruth = std::numeric_limits<float>::quiet_NaN();
0533 
0534       if (cluster)
0535       {
0536         clusterID = cluster->get_id();
0537         ntowers = cluster->getNTowers();
0538         x = cluster->get_x();
0539         y = cluster->get_y();
0540         z = cluster->get_z();
0541         phi = cluster->get_phi();
0542         e = cluster->get_energy();
0543 
0544         efromtruth = clustereval->get_energy_contribution(cluster, primary);
0545 
0546         // require vertex for cluster eta calculation
0547         if (vertexmap)
0548         {
0549           if (!vertexmap->empty())
0550           {
0551             GlobalVertex* vertex = (vertexmap->begin()->second);
0552 
0553             eta =
0554                 RawClusterUtility::GetPseudorapidity(
0555                     *cluster,
0556                     CLHEP::Hep3Vector(vertex->get_x(), vertex->get_y(), vertex->get_z()));
0557           }
0558         }
0559       }
0560 
0561       float shower_data[] = {(float) _ievent,
0562                              gparticleID,
0563                              gflavor,
0564                              gnhits,
0565                              geta,
0566                              gphi,
0567                              ge,
0568                              gpt,
0569                              gvx,
0570                              gvy,
0571                              gvz,
0572                              gembed,
0573                              gedep,
0574                              clusterID,
0575                              ntowers,
0576                              eta,
0577                              x,
0578                              y,
0579                              z,
0580                              phi,
0581                              e,
0582                              efromtruth};
0583 
0584       _ntp_gshower->Fill(shower_data);
0585     }
0586   }
0587 
0588   //----------------------
0589   // fill the Tower NTuple
0590   //----------------------
0591 
0592   if (_do_tower_eval)
0593   {
0594     if (!_use_towerinfo)
0595     {
0596       if (Verbosity() > 1)
0597       {
0598         std::cout << "CaloEvaluator::filling tower ntuple..." << std::endl;
0599       }
0600 
0601       std::string towernode = "TOWER_CALIB_" + _caloname;
0602       RawTowerContainer* towers = findNode::getClass<RawTowerContainer>(topNode, towernode);
0603       if (!towers)
0604       {
0605         std::cout << PHWHERE << " ERROR: Can't find " << towernode << std::endl;
0606         exit(-1);
0607       }
0608 
0609       std::string towergeomnode = "TOWERGEOM_" + _caloname;
0610       RawTowerGeomContainer* towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, towergeomnode);
0611       if (!towergeom)
0612       {
0613         std::cout << PHWHERE << " ERROR: Can't find " << towergeomnode << std::endl;
0614         exit(-1);
0615       }
0616 
0617       RawTowerContainer::ConstRange begin_end = towers->getTowers();
0618       RawTowerContainer::ConstIterator rtiter;
0619       for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter)
0620       {
0621         RawTower* tower = rtiter->second;
0622 
0623         if (tower->get_energy() < _reco_e_threshold)
0624         {
0625           continue;
0626         }
0627 
0628         RawTowerGeom* tower_geom = towergeom->get_tower_geometry(tower->get_id());
0629         if (!tower_geom)
0630         {
0631           std::cout << PHWHERE << " ERROR: Can't find tower geometry for this tower hit: ";
0632           tower->identify();
0633           exit(-1);
0634         }
0635 
0636         // std::cout<<"Tower ID = "<<tower->get_id()<<" for bin(j,k)= "<<tower->get_bineta()<<","<<tower->get_binphi()<<std::endl; //Added by Barak
0637         const float towerid = tower->get_id();
0638         const float ieta = tower->get_bineta();
0639         const float iphi = tower->get_binphi();
0640         const float eta = tower_geom->get_eta();
0641         const float phi = tower_geom->get_phi();
0642         const float e = tower->get_energy();
0643         const float x = tower_geom->get_center_x();
0644         const float y = tower_geom->get_center_y();
0645         const float z = tower_geom->get_center_z();
0646 
0647         // Added by Barak
0648         _towerID_debug = tower->get_id();
0649         _ieta_debug = tower->get_bineta();
0650         _iphi_debug = tower->get_binphi();
0651         _eta_debug = tower_geom->get_eta();
0652         _phi_debug = tower_geom->get_phi();
0653         _e_debug = tower->get_energy();
0654         _x_debug = tower_geom->get_center_x();
0655         _y_debug = tower_geom->get_center_y();
0656         _z_debug = tower_geom->get_center_z();
0657 
0658         PHG4Particle* primary = towereval->max_truth_primary_particle_by_energy(tower);
0659 
0660         float gparticleID = std::numeric_limits<float>::quiet_NaN();
0661         float gflavor = std::numeric_limits<float>::quiet_NaN();
0662         float gnhits = std::numeric_limits<float>::quiet_NaN();
0663         float gpx = std::numeric_limits<float>::quiet_NaN();
0664         float gpy = std::numeric_limits<float>::quiet_NaN();
0665         float gpz = std::numeric_limits<float>::quiet_NaN();
0666         float ge = std::numeric_limits<float>::quiet_NaN();
0667 
0668         float gpt = std::numeric_limits<float>::quiet_NaN();
0669         float geta = std::numeric_limits<float>::quiet_NaN();
0670         float gphi = std::numeric_limits<float>::quiet_NaN();
0671 
0672         float gvx = std::numeric_limits<float>::quiet_NaN();
0673         float gvy = std::numeric_limits<float>::quiet_NaN();
0674         float gvz = std::numeric_limits<float>::quiet_NaN();
0675 
0676         float gembed = std::numeric_limits<float>::quiet_NaN();
0677         float gedep = std::numeric_limits<float>::quiet_NaN();
0678 
0679         float efromtruth = std::numeric_limits<float>::quiet_NaN();
0680 
0681         if (primary)
0682         {
0683           gparticleID = primary->get_track_id();
0684           gflavor = primary->get_pid();
0685 
0686           PHG4Shower* shower = trutheval->get_primary_shower(primary);
0687           if (shower)
0688           {
0689             gnhits = shower->get_nhits(trutheval->get_caloid());
0690           }
0691           else
0692           {
0693             gnhits = 0.0;
0694           }
0695           gpx = primary->get_px();
0696           gpy = primary->get_py();
0697           gpz = primary->get_pz();
0698           ge = primary->get_e();
0699 
0700           gpt = std::sqrt(gpx * gpx + gpy * gpy);
0701           if (gpt != 0.0)
0702           {
0703             geta = std::asinh(gpz / gpt);
0704           }
0705           gphi = std::atan2(gpy, gpx);
0706 
0707           PHG4VtxPoint* vtx = trutheval->get_vertex(primary);
0708 
0709           if (vtx)
0710           {
0711             gvx = vtx->get_x();
0712             gvy = vtx->get_y();
0713             gvz = vtx->get_z();
0714           }
0715 
0716           gembed = trutheval->get_embed(primary);
0717           gedep = trutheval->get_shower_energy_deposit(primary);
0718 
0719           efromtruth = towereval->get_energy_contribution(tower, primary);
0720         }
0721 
0722         float tower_data[] = {(float) _ievent,
0723                               towerid,
0724                               ieta,
0725                               iphi,
0726                               eta,
0727                               phi,
0728                               e,
0729                               x,
0730                               y,
0731                               z,
0732                               gparticleID,
0733                               gflavor,
0734                               gnhits,
0735                               geta,
0736                               gphi,
0737                               ge,
0738                               gpt,
0739                               gvx,
0740                               gvy,
0741                               gvz,
0742                               gembed,
0743                               gedep,
0744                               efromtruth};
0745 
0746         _ntp_tower->Fill(tower_data);
0747         _tower_debug->Fill();  // Added by Barak (see above for explanation)
0748       }
0749     }
0750     else
0751     {
0752       if (Verbosity() > 1)
0753       {
0754         std::cout << "CaloEvaluator::filling tower ntuple..." << std::endl;
0755       }
0756 
0757       std::string towernode = "TOWERINFO_CALIB_" + _caloname;
0758       TowerInfoContainer* towers = findNode::getClass<TowerInfoContainer>(topNode, towernode);
0759       if (!towers)
0760       {
0761         std::cout << PHWHERE << " ERROR: Can't find " << towernode << std::endl;
0762         exit(-1);
0763       }
0764 
0765       std::string towergeomnode = "TOWERGEOM_" + _caloname;
0766       RawTowerGeomContainer* towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, towergeomnode);
0767       if (!towergeom)
0768       {
0769         std::cout << PHWHERE << " ERROR: Can't find " << towergeomnode << std::endl;
0770         exit(-1);
0771       }
0772 
0773       unsigned int ntowers = towers->size();
0774 
0775       for (unsigned int channel = 0; channel < ntowers; channel++)
0776       {
0777         TowerInfo* tower = towers->get_tower_at_channel(channel);
0778 
0779         if (tower->get_energy() < _reco_e_threshold)
0780         {
0781           continue;
0782         }
0783 
0784         unsigned int towerkey = towers->encode_key(channel);
0785         unsigned int ieta_bin = towers->getTowerEtaBin(towerkey);
0786         unsigned int iphi_bin = towers->getTowerPhiBin(towerkey);
0787         const RawTowerDefs::keytype key = RawTowerDefs::encode_towerid(
0788             RawTowerDefs::convert_name_to_caloid("CEMC"), ieta_bin, iphi_bin);
0789         RawTowerGeom* tower_geom = towergeom->get_tower_geometry(key);
0790         if (!tower_geom)
0791         {
0792           std::cout << PHWHERE << " ERROR: Can't find tower geometry for this tower hit: ";
0793           tower->identify();
0794           exit(-1);
0795         }
0796 
0797         // std::cout<<"Tower ID = "<<tower->get_id()<<" for bin(j,k)= "<<tower->get_bineta()<<","<<tower->get_binphi()<<std::endl; //Added by Barak
0798         const float towerid = key;
0799         const float ieta = ieta_bin;
0800         const float iphi = iphi_bin;
0801         const float eta = tower_geom->get_eta();
0802         const float phi = tower_geom->get_phi();
0803         const float e = tower->get_energy();
0804         const float x = tower_geom->get_center_x();
0805         const float y = tower_geom->get_center_y();
0806         const float z = tower_geom->get_center_z();
0807 
0808         // Added by Barak
0809         _towerID_debug = towerid;
0810         _ieta_debug = ieta;
0811         _iphi_debug = iphi;
0812         _eta_debug = eta;
0813         _phi_debug = phi;
0814         _e_debug = e;
0815         _x_debug = x;
0816         _y_debug = y;
0817         _z_debug = z;
0818 
0819         PHG4Particle* primary = towereval->max_truth_primary_particle_by_energy(tower);
0820 
0821         float gparticleID = std::numeric_limits<float>::quiet_NaN();
0822         float gflavor = std::numeric_limits<float>::quiet_NaN();
0823         float gnhits = std::numeric_limits<float>::quiet_NaN();
0824         float gpx = std::numeric_limits<float>::quiet_NaN();
0825         float gpy = std::numeric_limits<float>::quiet_NaN();
0826         float gpz = std::numeric_limits<float>::quiet_NaN();
0827         float ge = std::numeric_limits<float>::quiet_NaN();
0828 
0829         float gpt = std::numeric_limits<float>::quiet_NaN();
0830         float geta = std::numeric_limits<float>::quiet_NaN();
0831         float gphi = std::numeric_limits<float>::quiet_NaN();
0832 
0833         float gvx = std::numeric_limits<float>::quiet_NaN();
0834         float gvy = std::numeric_limits<float>::quiet_NaN();
0835         float gvz = std::numeric_limits<float>::quiet_NaN();
0836 
0837         float gembed = std::numeric_limits<float>::quiet_NaN();
0838         float gedep = std::numeric_limits<float>::quiet_NaN();
0839 
0840         float efromtruth = std::numeric_limits<float>::quiet_NaN();
0841 
0842         if (primary)
0843         {
0844           gparticleID = primary->get_track_id();
0845           gflavor = primary->get_pid();
0846 
0847           PHG4Shower* shower = trutheval->get_primary_shower(primary);
0848           if (shower)
0849           {
0850             gnhits = shower->get_nhits(trutheval->get_caloid());
0851           }
0852           else
0853           {
0854             gnhits = 0.0;
0855           }
0856           gpx = primary->get_px();
0857           gpy = primary->get_py();
0858           gpz = primary->get_pz();
0859           ge = primary->get_e();
0860 
0861           gpt = std::sqrt(gpx * gpx + gpy * gpy);
0862           if (gpt != 0.0)
0863           {
0864             geta = std::asinh(gpz / gpt);
0865           }
0866           gphi = std::atan2(gpy, gpx);
0867 
0868           PHG4VtxPoint* vtx = trutheval->get_vertex(primary);
0869 
0870           if (vtx)
0871           {
0872             gvx = vtx->get_x();
0873             gvy = vtx->get_y();
0874             gvz = vtx->get_z();
0875           }
0876 
0877           gembed = trutheval->get_embed(primary);
0878           gedep = trutheval->get_shower_energy_deposit(primary);
0879 
0880           efromtruth = towereval->get_energy_contribution(tower, primary);
0881         }
0882 
0883         float tower_data[] = {(float) _ievent,
0884                               towerid,
0885                               ieta,
0886                               iphi,
0887                               eta,
0888                               phi,
0889                               e,
0890                               x,
0891                               y,
0892                               z,
0893                               gparticleID,
0894                               gflavor,
0895                               gnhits,
0896                               geta,
0897                               gphi,
0898                               ge,
0899                               gpt,
0900                               gvx,
0901                               gvy,
0902                               gvz,
0903                               gembed,
0904                               gedep,
0905                               efromtruth};
0906 
0907         _ntp_tower->Fill(tower_data);
0908         _tower_debug->Fill();  // Added by Barak (see above for explanation)
0909       }
0910     }
0911   }
0912 
0913   //------------------------
0914   // fill the Cluster NTuple
0915   //------------------------
0916 
0917   if (_do_cluster_eval)
0918   {
0919     if (Verbosity() > 1)
0920     {
0921       std::cout << "CaloEvaluator::filling gcluster ntuple..." << std::endl;
0922     }
0923 
0924     GlobalVertexMap* vertexmap = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
0925 
0926     std::string clusternode = "CLUSTER_" + _caloname;
0927     if (_use_towerinfo)
0928     {
0929       clusternode = "CLUSTER_CALIB_" + _caloname;
0930     }
0931     RawClusterContainer* clusters = findNode::getClass<RawClusterContainer>(topNode, clusternode);
0932     if (!clusters)
0933     {
0934       std::cout << PHWHERE << " ERROR: Can't find " << clusternode << std::endl;
0935       exit(-1);
0936     }
0937 
0938     // for every cluster
0939 
0940     for (const auto& iterator : clusters->getClustersMap())
0941     {
0942       RawCluster* cluster = iterator.second;
0943 
0944       //    for (unsigned int icluster = 0; icluster < clusters->size(); icluster++)
0945       //    {
0946       //      RawCluster* cluster = clusters->getCluster(icluster);
0947 
0948       if (cluster->get_energy() < _reco_e_threshold)
0949       {
0950         continue;
0951       }
0952 
0953       float clusterID = cluster->get_id();
0954       float ntowers = cluster->getNTowers();
0955       float x = cluster->get_x();
0956       float y = cluster->get_y();
0957       float z = cluster->get_z();
0958       float eta = std::numeric_limits<float>::quiet_NaN();
0959       float phi = cluster->get_phi();
0960       float e = cluster->get_energy();
0961 
0962       // require vertex for cluster eta calculation
0963       if (vertexmap)
0964       {
0965         if (!vertexmap->empty())
0966         {
0967           GlobalVertex* vertex = (vertexmap->begin()->second);
0968 
0969           eta =
0970               RawClusterUtility::GetPseudorapidity(
0971                   *cluster,
0972                   CLHEP::Hep3Vector(vertex->get_x(), vertex->get_y(), vertex->get_z()));
0973         }
0974       }
0975 
0976       PHG4Particle* primary = clustereval->max_truth_primary_particle_by_energy(cluster);
0977 
0978       float gparticleID = std::numeric_limits<float>::quiet_NaN();
0979       float gflavor = std::numeric_limits<float>::quiet_NaN();
0980 
0981       float gnhits = std::numeric_limits<float>::quiet_NaN();
0982       float gpx = std::numeric_limits<float>::quiet_NaN();
0983       float gpy = std::numeric_limits<float>::quiet_NaN();
0984       float gpz = std::numeric_limits<float>::quiet_NaN();
0985       float ge = std::numeric_limits<float>::quiet_NaN();
0986 
0987       float gpt = std::numeric_limits<float>::quiet_NaN();
0988       float geta = std::numeric_limits<float>::quiet_NaN();
0989       float gphi = std::numeric_limits<float>::quiet_NaN();
0990 
0991       float gvx = std::numeric_limits<float>::quiet_NaN();
0992       float gvy = std::numeric_limits<float>::quiet_NaN();
0993       float gvz = std::numeric_limits<float>::quiet_NaN();
0994 
0995       float gembed = std::numeric_limits<float>::quiet_NaN();
0996       float gedep = std::numeric_limits<float>::quiet_NaN();
0997 
0998       float efromtruth = std::numeric_limits<float>::quiet_NaN();
0999 
1000       if (primary)
1001       {
1002         gparticleID = primary->get_track_id();
1003         gflavor = primary->get_pid();
1004 
1005         PHG4Shower* shower = trutheval->get_primary_shower(primary);
1006         if (shower)
1007         {
1008           gnhits = shower->get_nhits(trutheval->get_caloid());
1009         }
1010         else
1011         {
1012           gnhits = 0.0;
1013         }
1014         gpx = primary->get_px();
1015         gpy = primary->get_py();
1016         gpz = primary->get_pz();
1017         ge = primary->get_e();
1018 
1019         gpt = std::sqrt(gpx * gpx + gpy * gpy);
1020         if (gpt != 0.0)
1021         {
1022           geta = std::asinh(gpz / gpt);
1023         }
1024         gphi = std::atan2(gpy, gpx);
1025 
1026         PHG4VtxPoint* vtx = trutheval->get_vertex(primary);
1027 
1028         if (vtx)
1029         {
1030           gvx = vtx->get_x();
1031           gvy = vtx->get_y();
1032           gvz = vtx->get_z();
1033         }
1034 
1035         gembed = trutheval->get_embed(primary);
1036         gedep = trutheval->get_shower_energy_deposit(primary);
1037 
1038         efromtruth = clustereval->get_energy_contribution(cluster,
1039                                                           primary);
1040       }
1041 
1042       float cluster_data[] = {(float) _ievent,
1043                               clusterID,
1044                               ntowers,
1045                               eta,
1046                               x,
1047                               y,
1048                               z,
1049                               phi,
1050                               e,
1051                               gparticleID,
1052                               gflavor,
1053                               gnhits,
1054                               geta,
1055                               gphi,
1056                               ge,
1057                               gpt,
1058                               gvx,
1059                               gvy,
1060                               gvz,
1061                               gembed,
1062                               gedep,
1063                               efromtruth};
1064 
1065       _ntp_cluster->Fill(cluster_data);
1066     }
1067   }
1068 
1069   return;
1070 }