Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:20:07

0001 #include "InttCombinedRawDataDecoder.h"
0002 
0003 #include <trackbase/InttDefs.h>
0004 #include <trackbase/InttEventInfov1.h>
0005 #include <trackbase/TrkrDefs.h>  // for hitkey, hitsetkey
0006 #include <trackbase/TrkrHit.h>
0007 #include <trackbase/TrkrHitSet.h>
0008 #include <trackbase/TrkrHitSetContainer.h>
0009 #include <trackbase/TrkrHitSetContainerv1.h>
0010 #include <trackbase/TrkrHitv2.h>
0011 
0012 #include <ffarawobjects/Gl1RawHit.h>
0013 #include <ffarawobjects/Gl1Packet.h>
0014 #include <ffarawobjects/InttRawHit.h>
0015 #include <ffarawobjects/InttRawHitContainer.h>
0016 
0017 #include <cdbobjects/CDBTTree.h>
0018 
0019 #include <fun4all/Fun4AllReturnCodes.h>
0020 #include <fun4all/Fun4AllServer.h>
0021 
0022 #include <phool/PHCompositeNode.h>
0023 #include <phool/PHIODataNode.h>  // for PHIODataNode
0024 #include <phool/PHNodeIterator.h>
0025 #include <phool/getClass.h>
0026 #include <phool/phool.h>  // for PHWHERE
0027 
0028 #include <TSystem.h>
0029 
0030 #include <cstdlib>     // for exit
0031 #include <filesystem>  // for filesystem::exist
0032 #include <format>
0033 #include <iostream>    // for operator<<, endl, bas...
0034 #include <map>         // for _Rb_tree_iterator
0035 
0036 InttCombinedRawDataDecoder::InttCombinedRawDataDecoder(std::string const& name)
0037   : SubsysReco(name)
0038   , m_calibinfoDAC({"INTT_DACMAP", CDB})
0039   , m_calibinfoBCO({"INTT_BCOMAP", CDB})
0040 {
0041   // Do nothing
0042   // Consider calling LoadHotChannelMapRemote()
0043 }
0044 
0045 int InttCombinedRawDataDecoder::InitRun(PHCompositeNode* topNode)
0046 {
0047   if (!topNode)
0048   {
0049     std::cout
0050       << PHWHERE "\n"
0051       << "\tCould not retrieve topNode; doing nothing\n"
0052       << std::flush;
0053     exit(1);
0054     gSystem->Exit(1);
0055 
0056     return 1;
0057   }
0058 
0059   PHNodeIterator dst_itr(topNode);
0060   PHCompositeNode* dst_node = dynamic_cast<PHCompositeNode*>(dst_itr.findFirst("PHCompositeNode", "DST"));
0061   if (!dst_node)
0062   {
0063     std::cout
0064       << PHWHERE << "\n"
0065       << "\tCould not retrieve dst_node; doing nothing\n"
0066       << std::flush;
0067     exit(1);
0068     gSystem->Exit(1);
0069 
0070     return 1;
0071   }
0072 
0073   PHNodeIterator trkr_itr(dst_node);
0074   PHCompositeNode* trkr_node = dynamic_cast<PHCompositeNode*>(trkr_itr.findFirst("PHCompositeNode", "TRKR"));
0075   if (!trkr_node)
0076   {
0077     trkr_node = new PHCompositeNode("TRKR");
0078     dst_node->addNode(trkr_node);
0079   }
0080 
0081   TrkrHitSetContainer* trkr_hit_set_container = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
0082   if (!trkr_hit_set_container)
0083   {
0084     if (Verbosity())
0085     {
0086       std::cout
0087         << PHWHERE << "\n"
0088         << "\tMaking TrkrHitSetContainer\n"
0089         << std::flush;
0090     }
0091 
0092     trkr_hit_set_container = new TrkrHitSetContainerv1;
0093     PHIODataNode<PHObject>* new_node = new PHIODataNode<PHObject>(trkr_hit_set_container, "TRKR_HITSET", "PHObject");
0094     trkr_node->addNode(new_node);
0095   }
0096 
0097   // Check if INTT event header already exists
0098   if (m_writeInttEventHeader)
0099   {
0100     auto *inttNode = dynamic_cast<PHCompositeNode*>(trkr_itr.findFirst("PHCompositeNode", "INTT"));
0101     if (!inttNode)
0102     {
0103       inttNode = new PHCompositeNode("INTT");
0104       dst_node->addNode(inttNode);
0105     }
0106 
0107     intt_event_header = findNode::getClass<InttEventInfo>(inttNode, "INTTEVENTHEADER");
0108     if (!intt_event_header)
0109     {
0110       intt_event_header = new InttEventInfov1();
0111       auto *newHeader = new PHIODataNode<PHObject>(intt_event_header, "INTTEVENTHEADER", "PHObject");
0112       inttNode->addNode(newHeader);
0113     }
0114   }
0115 
0116   InttRawHitContainer* inttcont = findNode::getClass<InttRawHitContainer>(topNode, m_InttRawNodeName);
0117   if (!inttcont)
0118   {
0119     std::cout
0120       << PHWHERE << "\n"
0121       << "Could not get \"" << m_InttRawNodeName << "\" from Node Tree\n"
0122       << "removing module\n"
0123       << std::flush;
0124 
0125     Fun4AllServer* se = Fun4AllServer::instance();
0126     se->unregisterSubsystem(this);
0127     return Fun4AllReturnCodes::EVENT_OK;
0128   }
0129 
0130   ///////////////////////////////////////
0131   std::cout << "calibinfo DAC : " << m_calibinfoDAC.first << " " << (m_calibinfoDAC.second == CDB ? "CDB" : "FILE") << std::endl;
0132   m_dacmap.Verbosity(Verbosity());
0133   if (m_calibinfoDAC.second == CDB)
0134   {
0135     m_dacmap.LoadFromCDB(m_calibinfoDAC.first);
0136   }
0137   else
0138   {
0139     m_dacmap.LoadFromFile(m_calibinfoDAC.first);
0140   }
0141 
0142   ///////////////////////////////////////
0143   std::cout << "calibinfo BCO : " << m_calibinfoBCO.first << " " << (m_calibinfoBCO.second == CDB ? "CDB" : "FILE") << std::endl;
0144   m_bcomap.Verbosity(Verbosity());
0145   int temp_offset = 0;
0146   if (m_calibinfoBCO.second == CDB)
0147   {
0148     temp_offset = m_bcomap.LoadFromCDB(m_calibinfoBCO.first);
0149   }
0150   else
0151   {
0152     temp_offset = m_bcomap.LoadFromFile(m_calibinfoBCO.first);
0153   }
0154   if(m_triggeredMode)
0155   {
0156     set_inttFeeOffset(temp_offset);
0157   }
0158   else
0159   {
0160     std::string calibdir = CDBInterface::instance()->getUrl("INTT_STREAMING_FEE_OFFSET");
0161     auto* cdbtree = new CDBTTree(calibdir);
0162     cdbtree->LoadCalibrations();
0163     m_inttFeeOffset = cdbtree->GetSingleIntValue("INTT_STREAMING_FEE_OFFSET");
0164     if(Verbosity() > 0)
0165     {
0166       std::cout << "Loaded intt fee offset of " << m_inttFeeOffset << " from CDB" << std::endl;
0167     }
0168   }
0169   /// If user hasn't called with custom calibration, load default
0170   if (!m_badmap.OfflineLoaded() && !m_badmap.RawDataLoaded())
0171   {
0172     m_badmap.Load(); // Method loads with default tag
0173   }
0174   if (Verbosity())
0175   {
0176     std::cout << "InttBadChannelMap size: " << m_badmap.size() << std::endl;
0177   }
0178   if (1 < Verbosity())
0179   {
0180     m_badmap.Print();
0181   }
0182 
0183   return Fun4AllReturnCodes::EVENT_OK;
0184 }
0185 
0186 int InttCombinedRawDataDecoder::process_event(PHCompositeNode* topNode)
0187 {
0188   evt_inttHits_vec.clear();
0189   evt_ChipHit_count_map.clear();
0190 
0191   TrkrHitSetContainer* trkr_hit_set_container = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
0192   if (!trkr_hit_set_container)
0193   {
0194     std::cout
0195       << PHWHERE << "\n"
0196       << "\tCould not get \"TRKR_HITSET\" from Node Tree\n"
0197       << "\tExiting\n"
0198       << std::flush;
0199     gSystem->Exit(1);
0200     exit(1);
0201 
0202     return Fun4AllReturnCodes::DISCARDEVENT;
0203   }
0204 
0205   InttRawHitContainer* inttcont = findNode::getClass<InttRawHitContainer>(topNode, m_InttRawNodeName);
0206   if (!inttcont)
0207   {
0208     std::cout << PHWHERE << std::endl;
0209     std::cout << "InttCombinedRawDataDecoder::process_event(PHCompositeNode* topNode)" << std::endl;
0210     std::cout << "Could not get \"" << m_InttRawNodeName << "\" from Node Tree" << std::endl;
0211     std::cout << "Exiting" << std::endl;
0212 
0213     gSystem->Exit(1);
0214     exit(1);
0215   }
0216   // Gl1RawHit* gl1 = nullptr;
0217   Gl1Packet* gl1 = nullptr;
0218   uint64_t gl1rawhitbco = 0;
0219   if (!m_runStandAlone)
0220   {
0221     gl1 = findNode::getClass<Gl1Packet>(topNode, "GL1RAWHIT");
0222     if (gl1)
0223     {
0224       gl1rawhitbco = gl1->lValue(0, "BCO");
0225     }
0226     else
0227     {
0228       auto *oldgl1 = findNode::getClass<Gl1RawHit>(topNode, "GL1RAWHIT");
0229       if(!oldgl1)
0230       {
0231         std::cout << PHWHERE << " no gl1 container, exiting" << std::endl;
0232         return Fun4AllReturnCodes::ABORTEVENT;
0233       }
0234       gl1rawhitbco = oldgl1->get_bco();
0235     }
0236   }
0237 
0238   // get the last 40 bits by bit shifting left then right to match
0239   // to the mvtx bco
0240   auto lbshift = gl1rawhitbco << 24U;  // clang-tidy: mark as unsigned
0241   auto gl1bco = lbshift >> 24U;        // clang-tidy: mark as unsigned
0242 
0243   if (m_writeInttEventHeader)
0244   {
0245     intt_event_header = findNode::getClass<InttEventInfo>(topNode, "INTTEVENTHEADER");
0246     assert(intt_event_header);
0247     if (inttcont->get_nhits() > 0)
0248     {
0249       intt_event_header->set_bco_full(inttcont->get_hit(0)->get_bco());
0250     }
0251     else
0252     {
0253       intt_event_header->set_bco_full(0);
0254     }
0255   }
0256 
0257   TrkrDefs::hitsetkey hit_set_key = 0;
0258   TrkrDefs::hitkey hit_key = 0;
0259   TrkrHitSetContainer::Iterator hit_set_container_itr;
0260   TrkrHit* hit = nullptr;
0261 
0262   unsigned int loop_start = (m_SaturatedChipRejection) ? 0 : 1; // note : if m_SaturatedChipRejection, then first loop for counting, otherwise, start with the second loop
0263   for (unsigned int loop = loop_start; loop < 2; loop++){
0264     
0265     for (unsigned int i = 0; i < inttcont->get_nhits(); i++)
0266     {
0267       InttRawHit* intthit = inttcont->get_hit(i);
0268       InttNameSpace::RawData_s raw = InttNameSpace::RawFromHit(intthit);
0269 
0270       int adc = intthit->get_adc();
0271       // amp = intthit->get_amplitude();
0272       uint64_t bco_full = intthit->get_bco();
0273       int bco = intthit->get_FPHX_BCO();
0274 
0275       InttNameSpace::Offline_s ofl = InttNameSpace::ToOffline(raw);
0276 
0277       ////////////////////////
0278       // bad channel filter
0279       if (m_badmap.OfflineLoaded() && m_badmap.IsBad(ofl))
0280       {
0281         if (1 < Verbosity())
0282         {
0283           std::cout
0284             << PHWHERE << "\n"
0285             << "\tMasking channel:\n"
0286             << "\t" << ofl.layer << " " << ofl.ladder_phi << " " << ofl.ladder_z << " " << ofl.strip_y << " " << ofl.strip_x << "\n"
0287             << std::endl;
0288         }
0289         continue;
0290       }
0291 
0292       if (m_badmap.RawDataLoaded() && m_badmap.IsBad(raw))
0293       {
0294         if (1 < Verbosity())
0295         {
0296           std::cout
0297             << PHWHERE << "\n"
0298             << "\tMasking (raw) channel:\n"
0299             << "\t" << raw.felix_server << " " << raw.felix_channel << " " << raw.chip << " " << raw.channel << "\n"
0300             << std::endl;
0301         }
0302         continue;
0303       }
0304 
0305       ////////////////////////
0306       // bco filter
0307       if (m_bcomap.IsBad(raw, bco_full, bco) && m_bcoFilter)
0308       {
0309         // std::cout<<"bad bco removed : "<<raw.felix_server<<" "<<raw.felix_channel<<" "<<raw.chip<<" "<<raw.channel<<std::endl;
0310         continue;
0311       }
0312 
0313       int time_bucket = 0;
0314 
0315       // Note: Case 1: Local, Triggered, With BCO filter
0316       if ( m_runStandAlone && m_triggeredMode && m_bcoFilter )
0317       { // NOLINT (bugprone-branch-clone)
0318     time_bucket = 0;
0319       }
0320 
0321       // Note: Case 2: Local, Triggered, No BCO filter
0322       else if ( m_runStandAlone && m_triggeredMode && !m_bcoFilter )
0323       { // NOLINT (bugprone-branch-clone)
0324     time_bucket = (intthit->get_FPHX_BCO() - (intthit->get_bco() & 0x7fU) - m_inttFeeOffset + 128) % 128;
0325       }
0326 
0327       // Note: Case 3: Local, Streaming, With BCO filter
0328       else if ( m_runStandAlone && !m_triggeredMode && m_bcoFilter )
0329       {
0330           std::cout<< PHWHERE << "\n" << "You selected INTT local mode, streaming mode, and BCO_filter, which is not supported. Exiting."<< std::endl;
0331           gSystem->Exit(1);
0332           exit(1);
0333       }
0334 
0335       // Note: Case 4: Local, Streaming, No BCO filter
0336       else if ( m_runStandAlone && !m_triggeredMode && !m_bcoFilter )
0337       {
0338           std::cout<< PHWHERE << "\n"<< "You selected INTT local mode, streaming mode, and WITHOUT BCO_filter, but GL1 is not available for time-bucket calculation. Exiting."<< std::endl;
0339           gSystem->Exit(1);
0340           exit(1);
0341       }
0342 
0343       // Note: Case 5: Global, Triggered, With BCO filter
0344       else if ( !m_runStandAlone && m_triggeredMode && m_bcoFilter )
0345       { // NOLINT (bugprone-branch-clone)
0346     time_bucket = 0;
0347       }
0348 
0349       // Note: Case 6: Global, Triggered, No BCO filter
0350       else if ( !m_runStandAlone && m_triggeredMode && !m_bcoFilter )
0351       { // NOLINT (bugprone-branch-clone)
0352     time_bucket = (intthit->get_FPHX_BCO() - (intthit->get_bco() & 0x7fU) - m_inttFeeOffset + 128) % 128;
0353       }
0354 
0355       // Note: Case 7: Global, Streaming, With BCO filter
0356       else if ( !m_runStandAlone && !m_triggeredMode && m_bcoFilter )
0357       {
0358           std::cout<< PHWHERE << "\n"<< "You selected INTT global mode, streaming mode, and BCO_filter, which is not supported. Exiting."<< std::endl;
0359           gSystem->Exit(1);
0360           exit(1);
0361       }
0362 
0363       // Note: Case 8: Global, Streaming, No BCO filter
0364       else if ( !m_runStandAlone && !m_triggeredMode && !m_bcoFilter )
0365       {
0366     time_bucket = intthit->get_FPHX_BCO() + intthit->get_bco() - gl1bco -  m_inttFeeOffset;
0367       }
0368 
0369       if(m_outputBcoDiff)
0370       {
0371         int bco_diff = 0;
0372         if(m_triggeredMode)
0373           {
0374             bco_diff = (intthit->get_FPHX_BCO() - (intthit->get_bco() & 0x7fU) + 128) % 128;
0375           }
0376         else
0377           {
0378             bco_diff =  intthit->get_FPHX_BCO() + intthit->get_bco() - gl1bco;
0379           }
0380 
0381         std::cout << " bco: " << " fee " << intthit->get_fee() 
0382             << " rawhitbco " <<  intthit->get_bco() 
0383             << " gl1bco " << gl1bco 
0384             << "  intthit->get_FPHX_BCO() " <<  intthit->get_FPHX_BCO()
0385             << " bcodiff " << bco_diff 
0386             << " time_bucket " << time_bucket 
0387             << std::endl;
0388       }
0389 
0390       int used_timing = (m_bcoFilter) ? intthit->get_FPHX_BCO() : time_bucket;
0391       std::string text_to_hit =  std::format("{}_{}_{}_{}_{}",int(intthit->get_packetid() - 3001), int(intthit->get_fee()), ((intthit->get_chip_id() - 1) % 26), int(intthit->get_channel_id()), used_timing);
0392       std::string text_to_chip = std::format("{}_{}_{}_{}",   int(intthit->get_packetid() - 3001), int(intthit->get_fee()), ((intthit->get_chip_id() - 1) % 26), used_timing);
0393 
0394       if (loop == 0) { // note : the first "loop" is for counting the number of chip hit, so we don't touch the hit set key things 
0395 
0396         if (std::find(evt_inttHits_vec.begin(), evt_inttHits_vec.end(),text_to_hit) == evt_inttHits_vec.end()){ // note : this is a new hit to the evt_inttHits_vec
0397           evt_inttHits_vec.push_back(text_to_hit);
0398           
0399           if (!evt_ChipHit_count_map.contains(text_to_chip)){
0400             evt_ChipHit_count_map[text_to_chip] = 1;
0401           }
0402           else{
0403             evt_ChipHit_count_map[text_to_chip] += 1;
0404           }
0405         }
0406       
0407         continue;
0408       }
0409 
0410       if (loop == 1 && m_SaturatedChipRejection && evt_ChipHit_count_map[text_to_chip] >= HighChipMultiplicityCut){
0411         
0412         if (Verbosity() > 10000){
0413           std::cout << PHWHERE << " hit in Saturated chip removed, the hit: "<<text_to_hit<<", the chip: " << text_to_chip << " with " << evt_ChipHit_count_map[text_to_chip] << " hits "<< std::endl;
0414         }
0415       
0416         continue;
0417       }
0418 
0419       hit_key = InttDefs::genHitKey(ofl.strip_y, ofl.strip_x);  // col, row <trackbase/InttDefs.h>
0420 
0421       hit_set_key = InttDefs::genHitSetKey(ofl.layer, ofl.ladder_z, ofl.ladder_phi, time_bucket);
0422       hit_set_container_itr = trkr_hit_set_container->findOrAddHitSet(hit_set_key);
0423       hit = hit_set_container_itr->second->getHit(hit_key);
0424 
0425       if (hit)
0426       {
0427         continue;
0428       }
0429 
0430       ////////////////////////
0431       // dac conversion
0432       int dac = m_dacmap.GetDAC(raw, adc);
0433 
0434       hit = new TrkrHitv2;
0435       //--hit->setAdc(adc);
0436       hit->setAdc(dac);
0437       hit_set_container_itr->second->addHitSpecificKey(hit_key, hit);
0438     }
0439 
0440   }
0441   
0442   return Fun4AllReturnCodes::EVENT_OK;
0443 }