Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-17 07:39:34

0001 
0002 #include "TrackingTest_processor.h"
0003 
0004 #include <JANA/JApplication.h>
0005 #include <JANA/JApplicationFwd.h>
0006 #include <JANA/JEvent.h>
0007 #include <JANA/Services/JGlobalRootLock.h>
0008 #include <JANA/Services/JParameterManager.h>
0009 #include <Math/GenVector/Cartesian3D.h>
0010 #include <Math/GenVector/PxPyPzM4D.h>
0011 #include <edm4eic/MCRecoParticleAssociationCollection.h>
0012 #include <edm4eic/ReconstructedParticleCollection.h>
0013 #include <edm4hep/MCParticleCollection.h>
0014 #include <edm4hep/Vector3d.h>
0015 #include <edm4hep/Vector3f.h>
0016 #include <fmt/format.h>
0017 #include <podio/ObjectID.h>
0018 #include <spdlog/logger.h>
0019 #include <cstddef>
0020 #include <map>
0021 #include <string>
0022 #include <utility>
0023 
0024 #include "services/io/podio/datamodel_glue_compat.h" // IWYU pragma: keep (templated JEvent::GetCollection<T> needs PodioTypeMap)
0025 #include "services/log/Log_service.h"
0026 #include "services/rootfile/RootFile_service.h"
0027 
0028 using namespace fmt;
0029 
0030 //------------------
0031 // Init
0032 //------------------
0033 void TrackingTest_processor::Init() {
0034   std::string plugin_name = ("tracking_test");
0035 
0036   // Get JANA application
0037   auto* app = GetApplication();
0038 
0039   // Ask service locator a file to write histograms to
0040   auto root_file_service = app->GetService<RootFile_service>();
0041 
0042   // Get TDirectory for histograms root file
0043   auto globalRootLock = app->GetService<JGlobalRootLock>();
0044   globalRootLock->acquire_write_lock();
0045   auto* file = root_file_service->GetHistFile();
0046   globalRootLock->release_lock();
0047 
0048   // Create a directory for this plugin. And subdirectories for series of histograms
0049   m_dir_main = file->mkdir(plugin_name.c_str());
0050 
0051   // Get log level from user parameter or default
0052   m_log = app->GetService<Log_service>()->logger(plugin_name);
0053   for (auto pair : app->GetJParameterManager()->GetAllParameters()) {
0054     m_log->info("{:<20} | {}", pair.first, pair.second->GetDescription());
0055   }
0056 }
0057 
0058 //------------------
0059 // Process
0060 //------------------
0061 void TrackingTest_processor::Process(const std::shared_ptr<const JEvent>& event) {
0062   m_log->debug("---- TrackingTest_processor {} ----", event->GetEventNumber());
0063 
0064   ProcessTrackingMatching(event);
0065 }
0066 
0067 //------------------
0068 // Finish
0069 //------------------
0070 void TrackingTest_processor::Finish() { fmt::print("OccupancyAnalysis::Finish() called\n"); }
0071 
0072 void TrackingTest_processor::ProcessTrackingResults(const std::shared_ptr<const JEvent>& event) {
0073   const auto* reco_particles =
0074       event->GetCollection<edm4eic::ReconstructedParticle>("outputParticles");
0075 
0076   m_log->debug("Tracking reconstructed particles N={}: ", reco_particles->size());
0077   m_log->debug("   {:<5} {:>8} {:>8} {:>8} {:>8} {:>8}", "[i]", "[px]", "[py]", "[pz]", "[P]",
0078                "[P*3]");
0079 
0080   for (std::size_t i = 0; i < reco_particles->size(); i++) {
0081     auto particle = (*reco_particles)[i];
0082 
0083     double px = particle.getMomentum().x;
0084     double py = particle.getMomentum().y;
0085     double pz = particle.getMomentum().z;
0086     // ROOT::Math::PxPyPzM4D p4v(px, py, pz, particle.getMass());
0087     ROOT::Math::Cartesian3D p(px, py, pz);
0088     m_log->debug("   {:<5} {:>8.2f} {:>8.2f} {:>8.2f} {:>8.2f} {:>8.2f}", i, px, py, pz, p.R(),
0089                  p.R() * 3);
0090   }
0091 
0092   const auto* mc_particles = event->GetCollection<edm4hep::MCParticle>("MCParticles");
0093 
0094   m_log->debug("MC particles N={}: ", mc_particles->size());
0095   m_log->debug("   {:<5} {:<6} {:<7} {:>8} {:>8} {:>8} {:>8}", "[i]", "status", "[PDG]", "[px]",
0096                "[py]", "[pz]", "[P]");
0097   for (std::size_t i = 0; i < mc_particles->size(); i++) {
0098 
0099     const auto& particle = (*mc_particles)[i];
0100 
0101     if (particle.getGeneratorStatus() != 1) {
0102       continue;
0103     }
0104     //
0105     double px = particle.getMomentum().x;
0106     double py = particle.getMomentum().y;
0107     double pz = particle.getMomentum().z;
0108     ROOT::Math::PxPyPzM4D p4v(px, py, pz, particle.getMass());
0109     ROOT::Math::Cartesian3D p(px, py, pz);
0110     if (p.R() < 1) {
0111       continue;
0112     }
0113 
0114     m_log->debug("   {:<5} {:<6} {:<7} {:>8.2f} {:>8.2f} {:>8.2f} {:>8.2f}", i,
0115                  particle.getGeneratorStatus(), particle.getPDG(), px, py, pz, p.R());
0116   }
0117 }
0118 
0119 void TrackingTest_processor::ProcessTrackingMatching(const std::shared_ptr<const JEvent>& event) {
0120   m_log->debug("Associations [simId] [recID] [simE] [recE] [simPDG] [recPDG]");
0121 
0122   const auto* associations = event->GetCollection<edm4eic::MCRecoParticleAssociation>(
0123       "ReconstructedChargedParticleAssociations");
0124 
0125   for (auto assoc : *associations) {
0126     auto sim = assoc.getSim();
0127     auto rec = assoc.getRec();
0128 
0129     m_log->debug("  {:<6} {:<6} {:>8d} {:>8d}", assoc.getSim().getObjectID().index,
0130                  assoc.getRec().getObjectID().index, sim.getPDG(), rec.getPDG());
0131   }
0132 
0133   //    m_log->debug("Particles [objID] [PDG] [simE] [recE] [simPDG] [recPDG]");
0134   //    auto prt_with_assoc = event->GetSingle<edm4hep::ReconstructedParticle>("ChargedParticlesWithAssociations");
0135   //    for(auto part: prt_with_assoc->particles()) {
0136   //
0137   //        // auto sim = assoc->getSim();
0138   //        // auto rec = assoc->getRec();
0139   //
0140   //        m_log->debug("  {:<6} {:<6}  {:>8.2f} {:>8.2f}", part->getObjectID().index, part->getPDG(), part->getCharge(), part->getEnergy());
0141   //
0142   //    }
0143   //
0144   //
0145   //    m_log->debug("ReconstructedChargedParticles [objID] [PDG] [charge] [energy]");
0146   //    auto reco_charged_particles = event->Get<edm4eic::ReconstructedParticle>("ReconstructedChargedParticles");
0147   //    for(auto part: reco_charged_particles) {
0148   //        m_log->debug("  {:<6} {:<6}  {:>8.2f} {:>8.2f}", part->getObjectID().index, part->getPDG(), part->getCharge(), part->getEnergy());
0149   //    }
0150 }
0151 
0152 void TrackingTest_processor::ProcessGloablMatching(const std::shared_ptr<const JEvent>& event) {
0153 
0154   m_log->debug("ReconstructedParticles (FINAL) [objID] [PDG] [charge] [energy]");
0155   const auto* final_reco_particles =
0156       event->GetCollection<edm4eic::ReconstructedParticle>("ReconstructedParticlesWithAssoc");
0157   for (const auto& part : *final_reco_particles) {
0158     m_log->debug("  {:<6} {:<6}  {:>8.2f} {:>8.2f}", part.getObjectID().index, part.getPDG(),
0159                  part.getCharge(), part.getEnergy());
0160   }
0161 }