Back to home page

sPhenix code displayed by LXR

 
 

    


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

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