Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:20

0001 #include "SingleGl1PoolInputv2.h"
0002 
0003 #include <fun4allraw/Fun4AllStreamingInputManager.h>
0004 #include <fun4allraw/InputManagerType.h>
0005 #include "Fun4AllStreamingLumiCountingInputManager.h"
0006 
0007 #include <ffarawobjects/Gl1Packetv3.h>
0008 
0009 #include <phool/PHCompositeNode.h>
0010 #include <phool/PHIODataNode.h>    // for PHIODataNode
0011 #include <phool/PHNode.h>          // for PHNode
0012 #include <phool/PHNodeIterator.h>  // for PHNodeIterator
0013 #include <phool/PHObject.h>        // for PHObject
0014 #include <phool/getClass.h>
0015 #include <phool/phool.h>
0016 
0017 #include <Event/Event.h>
0018 #include <Event/EventTypes.h>
0019 #include <Event/Eventiterator.h>
0020 #include <Event/packet.h>  // for Packet
0021 
0022 #include <cstdint>   // for uint64_t
0023 #include <iostream>  // for operator<<, basic_ostream<...
0024 #include <iterator>  // for reverse_iterator
0025 #include <limits>    // for numeric_limits
0026 #include <memory>
0027 #include <set>
0028 #include <utility>  // for pair
0029 
0030 SingleGl1PoolInputv2::SingleGl1PoolInputv2(const std::string &name)
0031   : SingleStreamingInputv2(name)
0032 {
0033   SubsystemEnum(InputManagerType::GL1);
0034 }
0035 
0036 SingleGl1PoolInputv2::~SingleGl1PoolInputv2()
0037 {
0038   CleanupUsedPackets(std::numeric_limits<uint64_t>::max());
0039 }
0040 
0041 void SingleGl1PoolInputv2::FillPool(const unsigned int /*nbclks*/)
0042 {
0043   if (AllDone())  // no more files and all events read
0044   {
0045     return;
0046   }
0047   while (GetEventiterator() == nullptr)  // at startup this is a null pointer
0048   {
0049     if (!OpenNextFile())
0050     {
0051       AllDone(1);
0052       return;
0053     }
0054   }
0055   //  std::set<uint64_t> saved_beamclocks;
0056   while (GetSomeMoreEvents())
0057   {
0058     std::unique_ptr<Event> evt(GetEventiterator()->getNextEvent());
0059     while (!evt)
0060     {
0061       fileclose();
0062       if (!OpenNextFile())
0063       {
0064         AllDone(1);
0065         return;
0066       }
0067       evt.reset(GetEventiterator()->getNextEvent());
0068     }
0069     if (Verbosity() > 2)
0070     {
0071       std::cout << PHWHERE << "Fetching next Event" << evt->getEvtSequence() << std::endl;
0072     }
0073     if ((m_total_event == 0 && evt->getEvtType() == ENDRUNEVENT) ||
0074         (m_total_event != 0 && evt->getEvtSequence() - 2 == m_total_event))
0075     {
0076       m_alldone_flag = true;
0077       m_lastevent_flag = true;
0078     }
0079     if (evt->getEvtSequence() % 5000 == 0)
0080     {
0081       m_alldone_flag = true;
0082       m_lastevent_flag = true;
0083     }
0084     if (Verbosity() > 2)
0085     {
0086       if (m_alldone_flag)
0087       {
0088         std::cout << "gl1 all done is true" << std::endl;
0089       }
0090       // else{std::cout<<"gl1 all done is false"<<std::endl;}
0091     }
0092     RunNumber(evt->getRunNumber());
0093     if (GetVerbosity() > 1)
0094     {
0095       evt->identify();
0096     }
0097     if (evt->getEvtType() != DATAEVENT)
0098     {
0099       m_NumSpecialEvents++;
0100       if (evt->getEvtType() == ENDRUNEVENT)
0101       {
0102         AllDone(1);
0103         std::unique_ptr<Event> nextevt(GetEventiterator()->getNextEvent());
0104         if (nextevt)
0105         {
0106           std::cout << PHWHERE << " Found event after End Run Event " << std::endl;
0107           std::cout << "End Run Event identify: " << std::endl;
0108           evt->identify();
0109           std::cout << "Next event identify: " << std::endl;
0110           nextevt->identify();
0111         }
0112         return;
0113       }
0114       continue;
0115     }
0116     int EventSequence = evt->getEvtSequence();
0117     Packet *packet = evt->getPacket(14001);
0118     if (!packet)
0119     {
0120       std::cout << PHWHERE << "Packet 14001 is null ptr" << std::endl;
0121       evt->identify();
0122       m_alldone_flag = true;
0123       m_lastevent_flag = true;
0124       if (StreamingLumiInputManager())
0125       {
0126         StreamingLumiInputManager()->SetEndofEvent(m_alldone_flag, m_lastevent_flag);
0127         StreamingLumiInputManager()->SetEventNumber(EventSequence);
0128       }
0129       m_alldone_flag = false;
0130       m_lastevent_flag = false;
0131       continue;
0132     }
0133     if (Verbosity() > 1)
0134     {
0135       packet->identify();
0136     }
0137 
0138     Gl1Packet *newhit = new Gl1Packetv3();
0139     uint64_t gtm_bco = packet->lValue(0, "BCO");
0140     uint64_t bco_trim = gtm_bco & 0xFFFFFFFFFFU;
0141     //  std::cout<<bco_trim<<std::endl;
0142     m_BCOWindows[bco_trim] = std::make_pair(bco_trim - m_negative_bco_window, bco_trim + m_positive_bco_window);
0143     //    std::cout<<"BCO "<< m_BCOWindows.begin()->first<<" left "<<m_BCOWindows.begin()->second.first<<" right "<< m_BCOWindows.begin()->second.second<<std::endl;
0144     m_BCOBunchNumber[bco_trim] = packet->lValue(0, "BunchNumber");
0145     //  std::cout<<"BCO "<<bco_trim << " Bunch Number "<< m_BCOBunchNumber[bco_trim]<<std::endl;
0146     if (StreamingLumiInputManager())
0147     {
0148       StreamingLumiInputManager()->AddGl1Window(bco_trim, m_negative_bco_window, m_positive_bco_window);
0149       StreamingLumiInputManager()->AddGl1BunchNumber(bco_trim, m_BCOBunchNumber[bco_trim]);
0150       StreamingLumiInputManager()->SetEndofEvent(m_alldone_flag, m_lastevent_flag);
0151       StreamingLumiInputManager()->SetEventNumber(EventSequence);
0152       StreamingLumiInputManager()->SetNegativeWindow(m_negative_bco_window);
0153       StreamingLumiInputManager()->SetPositiveWindow(m_positive_bco_window);
0154     }
0155     if (evt->getEvtSequence() % 5000 == 0)
0156     {
0157       m_alldone_flag = false;
0158       m_lastevent_flag = false;
0159     }
0160 
0161     m_FEEBclkMap.insert(gtm_bco);
0162     newhit->setBCO(packet->lValue(0, "BCO"));
0163     newhit->setHitFormat(packet->getHitFormat());
0164     newhit->setIdentifier(packet->getIdentifier());
0165     newhit->setEvtSequence(EventSequence);
0166     newhit->setPacketNumber(packet->iValue(0));
0167     newhit->setBunchNumber(packet->lValue(0, "BunchNumber"));
0168     newhit->setTriggerInput(packet->lValue(0, "TriggerInput"));
0169     newhit->setLiveVector(packet->lValue(0, "LiveVector"));
0170     newhit->setScaledVector(packet->lValue(0, "ScaledVector"));
0171     newhit->setGTMBusyVector(packet->lValue(0, "GTMBusyVector"));
0172     newhit->setGTMAllBusyVector(packet->lValue(0, "GTMAllBusyVector"));
0173     for (int i = 0; i < 64; i++)
0174     {
0175       for (int j = 0; j < 3; j++)
0176       {
0177         newhit->setScaler(i, j, packet->lValue(i, j));
0178       }
0179     }
0180     for (int i = 0; i < 12; i++)
0181     {
0182       newhit->setGl1pScaler(i, 0, packet->lValue(i, "GL1PRAW"));
0183       newhit->setGl1pScaler(i, 1, packet->lValue(i, "GL1PLIVE"));
0184       newhit->setGl1pScaler(i, 2, packet->lValue(i, "GL1PSCALED"));
0185     }
0186     if (Verbosity() > 2)
0187     {
0188       std::cout << PHWHERE << " Packet: " << packet->getIdentifier()
0189                 << " evtno: " << EventSequence
0190                 << ", bco: 0x" << std::hex << gtm_bco << std::dec
0191                 << ", bunch no: " << packet->lValue(0, "BunchNumber")
0192                 << std::endl;
0193       std::cout << PHWHERE << " RB Packet: " << newhit->getIdentifier()
0194                 << " evtno: " << newhit->getEvtSequence()
0195                 << ", bco: 0x" << std::hex << newhit->getBCO() << std::dec
0196                 << ", bunch no: " << +newhit->getBunchNumber()
0197                 << std::endl;
0198     }
0199     if (Verbosity() > 2)
0200     {
0201       std::cout << PHWHERE << "evtno: " << EventSequence
0202                 << ", bco: 0x" << std::hex << gtm_bco << std::dec
0203                 << std::endl;
0204     }
0205     if (StreamingLumiInputManager())
0206     {
0207       StreamingLumiInputManager()->AddGl1RawHit(gtm_bco, newhit);
0208     }
0209 
0210     m_Gl1RawHitMap[gtm_bco].push_back(newhit);
0211     m_BclkStack.insert(gtm_bco);
0212 
0213     delete packet;
0214   }
0215 }
0216 
0217 void SingleGl1PoolInputv2::Print(const std::string &what) const
0218 {
0219   if (what == "ALL" || what == "FEEBCLK")
0220   {
0221     for (auto bcliter : m_FEEBclkMap)
0222     {
0223       std::cout << PHWHERE << " bclk: 0x"
0224                 << std::hex << bcliter << std::dec << std::endl;
0225     }
0226   }
0227   if (what == "ALL" || what == "STORAGE")
0228   {
0229     for (const auto &bcliter : m_Gl1RawHitMap)
0230     {
0231       std::cout << PHWHERE << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
0232       for (auto feeiter : bcliter.second)
0233       {
0234         std::cout << PHWHERE << "fee: " << feeiter->getBCO()
0235                   << " at " << std::hex << feeiter << std::dec << std::endl;
0236       }
0237     }
0238   }
0239   if (what == "ALL" || what == "STACK")
0240   {
0241     for (auto iter : m_BclkStack)
0242     {
0243       std::cout << PHWHERE << "stacked bclk: 0x" << std::hex << iter << std::dec << std::endl;
0244     }
0245   }
0246 }
0247 
0248 void SingleGl1PoolInputv2::CleanupUsedPackets(const uint64_t bclk)
0249 {
0250   std::vector<uint64_t> toclearbclk;
0251   for (const auto &iter : m_Gl1RawHitMap)
0252   {
0253     if (iter.first <= bclk)
0254     {
0255       for (auto pktiter : iter.second)
0256       {
0257         delete pktiter;
0258       }
0259       toclearbclk.push_back(iter.first);
0260     }
0261     else
0262     {
0263       break;
0264     }
0265   }
0266 
0267   for (auto iter : toclearbclk)
0268   {
0269     m_FEEBclkMap.erase(iter);
0270     m_BclkStack.erase(iter);
0271     m_Gl1RawHitMap.erase(iter);
0272   }
0273 }
0274 
0275 bool SingleGl1PoolInputv2::CheckPoolDepth(const uint64_t bclk)
0276 {
0277   // if (m_FEEBclkMap.size() < 10)
0278   // {
0279   //   std::cout << PHWHERE << "not all FEEs in map: " << m_FEEBclkMap.size() << std::endl;
0280   //   return true;
0281   // }
0282   for (auto iter : m_FEEBclkMap)
0283   {
0284     if (Verbosity() > 2)
0285     {
0286       std::cout << PHWHERE << "my bclk 0x" << std::hex << iter
0287                 << " req: 0x" << bclk << std::dec << std::endl;
0288     }
0289     if (iter < bclk)
0290     {
0291       if (Verbosity() > 1)
0292       {
0293         std::cout << PHWHERE << "FEE " << iter << " beamclock 0x" << std::hex << iter
0294                   << " smaller than req bclk: 0x" << bclk << std::dec << std::endl;
0295       }
0296       return false;
0297     }
0298   }
0299   return true;
0300 }
0301 
0302 void SingleGl1PoolInputv2::ClearCurrentEvent()
0303 {
0304   // called interactively, to get rid of the current event
0305   uint64_t currentbclk = *m_BclkStack.begin();
0306   //  std::cout << PHWHERE << "clearing bclk 0x" << std::hex << currentbclk << std::dec << std::endl;
0307   CleanupUsedPackets(currentbclk);
0308   // m_BclkStack.erase(currentbclk);
0309   return;
0310 }
0311 
0312 bool SingleGl1PoolInputv2::GetSomeMoreEvents()
0313 {
0314   if (AllDone())
0315   {
0316     return false;
0317   }
0318   if (m_Gl1RawHitMap.empty())
0319   {
0320     return true;
0321   }
0322 
0323   uint64_t lowest_bclk = m_Gl1RawHitMap.begin()->first;
0324   lowest_bclk += m_BcoRange;
0325   uint64_t last_bclk = m_Gl1RawHitMap.rbegin()->first;
0326   if (Verbosity() > 1)
0327   {
0328     std::cout << PHWHERE << "first bclk 0x" << std::hex << lowest_bclk
0329               << " last bco: 0x" << last_bclk
0330               << std::dec << std::endl;
0331   }
0332   if (lowest_bclk >= last_bclk)
0333   {
0334     return true;
0335   }
0336   return false;
0337 }
0338 
0339 void SingleGl1PoolInputv2::CreateDSTNode(PHCompositeNode *topNode)
0340 {
0341   PHNodeIterator iter(topNode);
0342   PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
0343   if (!dstNode)
0344   {
0345     dstNode = new PHCompositeNode("DST");
0346     topNode->addNode(dstNode);
0347   }
0348   PHNodeIterator iterDst(dstNode);
0349   PHCompositeNode *detNode = dynamic_cast<PHCompositeNode *>(iterDst.findFirst("PHCompositeNode", "GL1"));
0350   if (!detNode)
0351   {
0352     detNode = new PHCompositeNode("GL1");
0353     dstNode->addNode(detNode);
0354   }
0355   Gl1Packet *gl1hitcont = findNode::getClass<Gl1Packet>(detNode, "GL1RAWHIT");
0356   if (!gl1hitcont)
0357   {
0358     gl1hitcont = new Gl1Packetv3();
0359     PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(gl1hitcont, "GL1RAWHIT", "PHObject");
0360     detNode->addNode(newNode);
0361   }
0362 }