Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:39

0001 #include "InttArborist.h"
0002 
0003 #include <Event/Event.h>
0004 #include <Event/packet.h>
0005 
0006 #include <ffarawobjects/Gl1RawHit.h>
0007 #include <ffarawobjects/InttRawHit.h>
0008 #include <ffarawobjects/InttRawHitContainer.h>
0009 
0010 #include <fun4all/Fun4AllReturnCodes.h>
0011 #include <fun4all/SubsysReco.h>  // for SubsysReco
0012 
0013 #include <phool/PHCompositeNode.h>
0014 #include <phool/PHNodeIterator.h>
0015 #include <phool/getClass.h>
0016 #include <phool/phool.h>  // for PHWHERE
0017 
0018 #include <cstddef>
0019 #include <cstdlib>
0020 #include <filesystem>
0021 #include <iostream>
0022 
0023 InttArborist::InttArborist()
0024   : m_small_branches{{"adc", new std::vector<small_t>},
0025                      {"amp", new std::vector<small_t>},
0026                      {"bco", new std::vector<small_t>},
0027                      {"chn", new std::vector<small_t>},
0028                      {"chp", new std::vector<small_t>},
0029                      {"fee", new std::vector<small_t>},
0030                      {"pid", new std::vector<small_t>}}
0031   , m_large_branches{{"gtm", 0}}
0032 {
0033 }
0034 
0035 InttArborist::~InttArborist()
0036 {
0037   for (auto& itr : m_small_branches)
0038   {
0039     delete itr.second;
0040   }
0041 
0042   delete m_tree;
0043 }
0044 
0045 int InttArborist::CreateOutputFile(
0046     std::string const& file_name)
0047 {
0048   if (file_name.empty())
0049   {
0050     std::cout << "InttArborist::CreateOutputFile\n"
0051               << "\targument \"file_name\" is empty string" << std::endl;
0052     return EXIT_FAILURE;
0053   }
0054 
0055   if (m_file)
0056   {
0057     m_file->Close();
0058   }
0059   m_file = TFile::Open(file_name.c_str(), "RECREATE");
0060   if (!m_file)
0061   {
0062     std::cout << "InttArborist::CreateOutputFile\n"
0063               << "\tfailed to (re)create file " << file_name << std::endl;
0064     return EXIT_FAILURE;
0065   }
0066 
0067   delete m_tree;
0068   m_tree = new TTree(m_tree_name.c_str(), m_tree_name.c_str());
0069   m_file->cd();
0070   m_tree->SetDirectory(m_file);
0071 
0072   // container branches
0073   for (auto& itr : m_small_branches)
0074   {
0075     m_tree->Branch(itr.first.c_str(), &itr.second);
0076   }
0077 
0078   // POD-type branches
0079   for (auto& itr : m_large_branches)
0080   {
0081     m_tree->Branch(itr.first.c_str(), &itr.second);
0082   }
0083 
0084   return EXIT_SUCCESS;
0085 }
0086 
0087 int InttArborist::WriteOutputFile()
0088 {
0089   if (!m_file)
0090   {
0091     std::cout << "InttArborist::WriteOutputFile\n"
0092               << "\tmember \"m_file\" nullptr at call" << std::endl;
0093     return EXIT_FAILURE;
0094   }
0095 
0096   if (!m_tree)
0097   {
0098     std::cout << "InttArborist::WriteOutputFile\n"
0099               << "\tmember \"m_tree\" nullptr at call" << std::endl;
0100     return EXIT_FAILURE;
0101   }
0102 
0103   m_file->cd();
0104   m_tree->Write();
0105   m_file->Write();
0106   m_file->Close();
0107 
0108   return EXIT_SUCCESS;
0109 }
0110 
0111 int InttArborist::InitRun(
0112     PHCompositeNode* top_node)
0113 {
0114   if (!m_file)
0115   {
0116     std::cout << "InttArborist::process_event\n"
0117               << "\tmember \"m_file\" nullptr at call" << std::endl;
0118     return Fun4AllReturnCodes::ABORTRUN;
0119   }
0120 
0121   if (!m_tree)
0122   {
0123     std::cout << "InttArborist::process_event\n"
0124               << "\tmember \"m_tree\" nullptr at call" << std::endl;
0125     return Fun4AllReturnCodes::ABORTRUN;
0126   }
0127 
0128   if (!top_node)
0129   {
0130     std::cout << "InttArborist::process_event\n"
0131               << "\targument \"top_node\" nullptr at call" << std::endl;
0132     return Fun4AllReturnCodes::ABORTRUN;
0133   }
0134 
0135   return Fun4AllReturnCodes::EVENT_OK;
0136 }
0137 
0138 int InttArborist::process_event(
0139     PHCompositeNode* top_node)
0140 {
0141   if (!top_node)
0142   {
0143     std::cout << "InttArborist::process_event\n"
0144               << "\targument \"top_node\" nullptr at call" << std::endl;
0145     return Fun4AllReturnCodes::ABORTRUN;
0146   }
0147 
0148   InttRawHitContainer* intt_cont = findNode::getClass<InttRawHitContainer>(
0149       top_node, m_intt_raw_node_name);
0150 
0151   if (!intt_cont)
0152   {
0153     std::cout << "InttArborist::process_event\n"
0154               << "\targument \"top_node\" nullptr at call" << std::endl;
0155     return Fun4AllReturnCodes::ABORTRUN;
0156   }
0157 
0158   Gl1RawHit* gl1 = findNode::getClass<Gl1RawHit>(
0159       top_node, m_gl1_raw_node_name);
0160 
0161   m_large_branches["gtm"] = gl1 ? gl1->get_bco() : 0;
0162   m_large_branches["gtm"] &= (large_t{1} << 40U) - 1;
0163 
0164   m_clone_map.clear();
0165   for (auto& itr : m_small_branches)
0166   {
0167     itr.second->clear();
0168   }
0169   for (unsigned int i = 0; i < intt_cont->get_nhits(); ++i)
0170   {
0171     InttRawHit* intt_hit = intt_cont->get_hit(i);
0172     if (!intt_hit)
0173     {
0174       continue;
0175     }
0176 
0177     if (!m_large_branches["gtm"])
0178     {
0179       m_large_branches["gtm"] = intt_hit->get_bco();
0180       m_large_branches["gtm"] &= (large_t{1} << 40U) - 1;
0181     }
0182 
0183     InttNameSpace::RawData_s raw {
0184         .felix_server = intt_hit->get_packetid() - 3001,
0185         .felix_channel = intt_hit->get_fee(),
0186         .chip = (intt_hit->get_chip_id() + 25) % 26,
0187         .channel = intt_hit->get_channel_id()
0188     };
0189 
0190     clone_map_t::iterator clone_itr;
0191     if ((clone_itr = m_clone_map.find(raw)) != m_clone_map.end())
0192     {
0193       // can update our vector-valued branches at position clone_itr->second
0194       // for example, keep parameters of the max adc hit
0195       // for now I'm just doing a continue so there are no cloned hits
0196       // (could even make a counter for clone hits removed)
0197       continue;
0198     }
0199 
0200     m_small_branches["adc"]->push_back(intt_hit->get_adc());
0201     m_small_branches["amp"]->push_back(intt_hit->get_amplitude());
0202     m_small_branches["bco"]->push_back(intt_hit->get_FPHX_BCO());
0203 
0204     m_small_branches["chn"]->push_back(raw.channel);
0205     m_small_branches["chp"]->push_back(raw.chip);
0206     m_small_branches["fee"]->push_back(raw.felix_channel);
0207     m_small_branches["pid"]->push_back(raw.felix_server + 3001);
0208 
0209     m_clone_map.insert({raw, i});
0210   }
0211 
0212   m_tree->Fill();
0213 
0214   return Fun4AllReturnCodes::EVENT_OK;
0215 }
0216 
0217 int InttArborist::EndRun(
0218     int const /*unused*/
0219 )
0220 {
0221   if (WriteOutputFile())
0222   {
0223     return Fun4AllReturnCodes::ABORTRUN;
0224   }
0225   return Fun4AllReturnCodes::EVENT_OK;
0226 }