Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "SingleLL1TriggerInput.h"
0002 
0003 #include "Fun4AllPrdfInputTriggerManager.h"
0004 #include "InputManagerType.h"
0005 
0006 #include <ffarawobjects/LL1PacketContainerv1.h>
0007 #include <ffarawobjects/LL1Packetv1.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 <TSystem.h>
0023 
0024 #include <cstdint>   // for uint64_t
0025 #include <iostream>  // for operator<<, basic_ostream<...
0026 #include <iterator>  // for reverse_iterator
0027 #include <limits>    // for numeric_limits
0028 #include <memory>
0029 #include <set>
0030 #include <utility>  // for pair
0031 
0032 SingleLL1TriggerInput::SingleLL1TriggerInput(const std::string &name)
0033   : SingleTriggerInput(name)
0034 {
0035   SubsystemEnum(InputManagerType::LL1);
0036 }
0037 
0038 SingleLL1TriggerInput::~SingleLL1TriggerInput()
0039 {
0040   CleanupUsedPackets(std::numeric_limits<int>::max());
0041   // some events are already in the m_EventStack but they haven't been put
0042   // into the m_PacketMap
0043   while (m_EventStack.begin() != m_EventStack.end())
0044   {
0045     m_EventStack.erase(m_EventStack.begin());
0046   }
0047 }
0048 
0049 void SingleLL1TriggerInput::FillPool(const unsigned int keep)
0050 {
0051   if (AllDone())  // no more files and all events read
0052   {
0053     return;
0054   }
0055   while (GetEventiterator() == nullptr)  // at startup this is a null pointer
0056   {
0057     if (!OpenNextFile())
0058     {
0059       AllDone(1);
0060       return;
0061     }
0062   }
0063   while (GetSomeMoreEvents(keep))
0064   {
0065     std::unique_ptr<Event> evt(GetEventiterator()->getNextEvent());
0066     while (!evt)
0067     {
0068       fileclose();
0069       if (!OpenNextFile())
0070       {
0071         AllDone(1);
0072         return;
0073       }
0074       evt.reset(GetEventiterator()->getNextEvent());
0075     }
0076     if (Verbosity() > 2)
0077     {
0078       std::cout << PHWHERE << "Fetching next Event" << evt->getEvtSequence() << std::endl;
0079     }
0080     RunNumber(evt->getRunNumber());
0081     if (GetVerbosity() > 1)
0082     {
0083       evt->identify();
0084     }
0085     if (evt->getEvtType() != DATAEVENT)
0086     {
0087       m_NumSpecialEvents++;
0088       continue;
0089     }
0090     int EventSequence = evt->getEvtSequence();
0091     if (EventSequence < SkipToEvent())
0092     {
0093       continue;
0094     }
0095     std::vector<Packet *> pktvec = evt->getPacketVector();
0096     for (auto packet : pktvec)
0097     {
0098       int packet_id = packet->getIdentifier();
0099       // The call to  EventNumberOffset(identifier) will initialize it to our default (zero) if it wasn't set already
0100       // if we encounter a misalignemt, the Fun4AllPrdfInputTriggerManager will adjust this. But the event
0101       // number of the adjustment depends on its pooldepth. Events in its pools will be moved to the correct slots
0102       // and only when the pool gets refilled, this correction kicks in
0103       // SO DO NOT BE CONFUSED when printing this out - seeing different events where this kicks in
0104       int CorrectedEventSequence = EventSequence + EventNumberOffset(packet_id);
0105       if (Verbosity() > 2)
0106       {
0107         packet->identify();
0108       }
0109 
0110       // by default use previous bco clock for gtm bco
0111       LL1Packet *newhit = new LL1Packetv1();
0112       int nr_channels = packet->iValue(0, "CHANNELS");
0113       int nr_samples = packet->iValue(0, "SAMPLES");
0114       uint64_t gtm_bco = packet->iValue(0, "CLOCK");
0115       // offline packet content
0116       newhit->setEvtSequence(CorrectedEventSequence);
0117       newhit->setIdentifier(packet_id);
0118       newhit->setHitFormat(packet->getHitFormat());
0119       newhit->setBCO(gtm_bco);
0120       newhit->setPacketEvtSequence(packet->iValue(0, "EVTNR"));
0121       // ll1 packet additions
0122       newhit->setNrSamples(nr_samples);
0123       newhit->setNrChannels(nr_channels);
0124       newhit->setTriggerWords(packet->iValue(0, "TRIGGERWORDS"));
0125       newhit->setSlotNr(packet->iValue(0, "SLOTNR"));
0126       newhit->setCardNr(packet->iValue(0, "CARDNR"));
0127       newhit->setMonitor(packet->iValue(0, "MONITOR"));
0128       newhit->setFemWords(packet->iValue(0, "FEMWORDS"));
0129       newhit->setFibers(packet->iValue(0, "FIBERS"));
0130       newhit->setSums(packet->iValue(0, "SUMS"));
0131       for (int ichan = 0; ichan < nr_channels; ichan++)
0132       {
0133         for (int isamp = 0; isamp < nr_samples; isamp++)
0134         {
0135           if (isamp >= newhit->getMaxNumSamples() || ichan >= newhit->getMaxNumChannels())
0136           {
0137             std::cout << "Packet: " << newhit->getIdentifier()
0138                       << ", samples: " << isamp
0139                       << ", channels: " << ichan << std::endl;
0140             gSystem->Exit(1);
0141           }
0142           else
0143           {
0144             newhit->setSample(ichan, isamp, packet->iValue(isamp, ichan));
0145           }
0146         }
0147       }
0148       // newhit->identify();
0149       //       newhit->dump();
0150       if (Verbosity() > 2)
0151       {
0152         std::cout << PHWHERE << "corrected evtno: " << CorrectedEventSequence
0153                   << ", original evtno: " << EventSequence
0154                   << ", bco: 0x" << std::hex << gtm_bco << std::dec
0155                   << std::endl;
0156       }
0157       if (TriggerInputManager())
0158       {
0159         TriggerInputManager()->AddLL1Packet(CorrectedEventSequence, newhit);
0160       }
0161       m_PacketMap[CorrectedEventSequence].push_back(newhit);
0162       m_EventStack.insert(CorrectedEventSequence);
0163       if (ddump_enabled())
0164       {
0165         ddumppacket(packet);
0166       }
0167       delete packet;
0168     }
0169   }
0170 }
0171 
0172 void SingleLL1TriggerInput::Print(const std::string &what) const
0173 {
0174   if (what == "ALL" || what == "STORAGE")
0175   {
0176     for (const auto &bcliter : m_PacketMap)
0177     {
0178       std::cout << PHWHERE << "Event: " << bcliter.first << std::endl;
0179     }
0180   }
0181   if (what == "ALL" || what == "STACK")
0182   {
0183     for (auto iter : m_EventStack)
0184     {
0185       std::cout << PHWHERE << "stacked event: " << iter << std::endl;
0186     }
0187   }
0188 }
0189 
0190 void SingleLL1TriggerInput::CleanupUsedPackets(const int eventno)
0191 {
0192   std::vector<int> toclearevents;
0193   for (const auto &iter : m_PacketMap)
0194   {
0195     if (iter.first <= eventno)
0196     {
0197       for (auto pktiter : iter.second)
0198       {
0199         delete pktiter;
0200       }
0201       toclearevents.push_back(iter.first);
0202     }
0203     else
0204     {
0205       break;
0206     }
0207   }
0208 
0209   for (auto iter : toclearevents)
0210   {
0211     m_EventStack.erase(iter);
0212     m_PacketMap.erase(iter);
0213   }
0214 }
0215 
0216 void SingleLL1TriggerInput::ClearCurrentEvent()
0217 {
0218   // called interactively, to get rid of the current event
0219   int currentevent = *m_EventStack.begin();
0220   //  std::cout << PHWHERE << "clearing bclk 0x" << std::hex << currentbclk << std::dec << std::endl;
0221   CleanupUsedPackets(currentevent);
0222   return;
0223 }
0224 
0225 void SingleLL1TriggerInput::CreateDSTNode(PHCompositeNode *topNode)
0226 {
0227   PHNodeIterator iter(topNode);
0228   PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
0229   if (!dstNode)
0230   {
0231     dstNode = new PHCompositeNode("DST");
0232     topNode->addNode(dstNode);
0233   }
0234   PHNodeIterator iterDst(dstNode);
0235   PHCompositeNode *detNode = dynamic_cast<PHCompositeNode *>(iterDst.findFirst("PHCompositeNode", "LL1"));
0236   if (!detNode)
0237   {
0238     detNode = new PHCompositeNode("LL1");
0239     dstNode->addNode(detNode);
0240   }
0241   LL1PacketContainer *ll1packetcont = findNode::getClass<LL1PacketContainer>(detNode, "LL1Packets");
0242   if (!ll1packetcont)
0243   {
0244     ll1packetcont = new LL1PacketContainerv1();
0245     PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(ll1packetcont, "LL1Packets", "PHObject");
0246     detNode->addNode(newNode);
0247   }
0248 }