Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-06 07:55:54

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