Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "SingleMbdTriggerInput.h"
0002 
0003 #include "Fun4AllPrdfInputTriggerManager.h"
0004 #include "InputManagerType.h"
0005 
0006 #include <ffarawobjects/CaloPacketContainerv1.h>
0007 #include <ffarawobjects/CaloPacketv1.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 SingleMbdTriggerInput::SingleMbdTriggerInput(const std::string &name)
0033   : SingleTriggerInput(name)
0034 {
0035   SubsystemEnum(InputManagerType::MBD);
0036 }
0037 
0038 SingleMbdTriggerInput::~SingleMbdTriggerInput()
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 SingleMbdTriggerInput::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 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       CaloPacket *newhit = new CaloPacketv1();
0111       int nr_modules = packet->iValue(0, "NRMODULES");
0112       int nr_channels = packet->iValue(0, "CHANNELS");
0113       int nr_samples = packet->iValue(0, "SAMPLES");
0114       if (nr_modules > 3)
0115       {
0116         std::cout << PHWHERE << " too many modules, need to adjust arrays" << std::endl;
0117         gSystem->Exit(1);
0118       }
0119 
0120       uint64_t gtm_bco = packet->lValue(0, "CLOCK");
0121       newhit->setNrModules(nr_modules);
0122       newhit->setNrSamples(nr_samples);
0123       newhit->setNrChannels(nr_channels);
0124       newhit->setBCO(gtm_bco);
0125       newhit->setPacketEvtSequence(packet->iValue(0, "EVTNR"));
0126       newhit->setIdentifier(packet_id);
0127       newhit->setHitFormat(packet->getHitFormat());
0128       newhit->setEvtSequence(CorrectedEventSequence);
0129       newhit->setEvenChecksum(packet->iValue(0, "EVENCHECKSUM"));
0130       newhit->setCalcEvenChecksum(packet->iValue(0, "CALCEVENCHECKSUM"));
0131       newhit->setOddChecksum(packet->iValue(0, "ODDCHECKSUM"));
0132       newhit->setCalcOddChecksum(packet->iValue(0, "CALCODDCHECKSUM"));
0133       newhit->setModuleAddress(packet->iValue(0, "MODULEADDRESS"));
0134       newhit->setDetId(packet->iValue(0, "DETID"));
0135       for (int ifem = 0; ifem < nr_modules; ifem++)
0136       {
0137         newhit->setFemClock(ifem, packet->iValue(ifem, "FEMCLOCK"));
0138         newhit->setFemEvtSequence(ifem, packet->iValue(ifem, "FEMEVTNR"));
0139         newhit->setFemSlot(ifem, packet->iValue(ifem, "FEMSLOT"));
0140         newhit->setChecksumLsb(ifem, packet->iValue(ifem, "CHECKSUMLSB"));
0141         newhit->setChecksumMsb(ifem, packet->iValue(ifem, "CHECKSUMMSB"));
0142         newhit->setCalcChecksumLsb(ifem, packet->iValue(ifem, "CALCCHECKSUMLSB"));
0143         newhit->setCalcChecksumMsb(ifem, packet->iValue(ifem, "CALCCHECKSUMMSB"));
0144       }
0145       for (int ipmt = 0; ipmt < nr_channels; ipmt++)
0146       {
0147         // store pre/post only for suppressed channels, the array in the packet routines is not
0148         // initialized so reading pre/post for not zero suppressed channels returns garbage
0149         bool isSuppressed = packet->iValue(ipmt, "SUPPRESSED");
0150         newhit->setSuppressed(ipmt, isSuppressed);
0151         if (isSuppressed)
0152         {
0153           newhit->setPre(ipmt, packet->iValue(ipmt, "PRE"));
0154           newhit->setPost(ipmt, packet->iValue(ipmt, "POST"));
0155         }
0156         else
0157         {
0158           for (int isamp = 0; isamp < nr_samples; isamp++)
0159           {
0160             newhit->setSample(ipmt, isamp, packet->iValue(isamp, ipmt));
0161           }
0162         }
0163       }
0164       if (Verbosity() > 2)
0165       {
0166         std::cout << PHWHERE << "corrected evtno: " << CorrectedEventSequence
0167                   << ", original evtno: " << EventSequence
0168                   << ", bco: 0x" << std::hex << gtm_bco << std::dec
0169                   << std::endl;
0170       }
0171       if (TriggerInputManager())
0172       {
0173         TriggerInputManager()->AddMbdPacket(CorrectedEventSequence, newhit);
0174       }
0175       m_PacketMap[CorrectedEventSequence].push_back(newhit);
0176       m_EventStack.insert(CorrectedEventSequence);
0177       if (ddump_enabled())
0178       {
0179         ddumppacket(packet);
0180       }
0181       delete packet;
0182     }
0183   }
0184 }
0185 
0186 void SingleMbdTriggerInput::Print(const std::string &what) const
0187 {
0188   if (what == "ALL" || what == "STORAGE")
0189   {
0190     for (const auto &bcliter : m_PacketMap)
0191     {
0192       std::cout << PHWHERE << "Event: " << bcliter.first << std::endl;
0193     }
0194   }
0195   if (what == "ALL" || what == "STACK")
0196   {
0197     for (auto iter : m_EventStack)
0198     {
0199       std::cout << PHWHERE << "stacked event: " << iter << std::endl;
0200     }
0201   }
0202 }
0203 
0204 void SingleMbdTriggerInput::CleanupUsedPackets(const int eventno)
0205 {
0206   std::vector<int> toclearevents;
0207   for (const auto &iter : m_PacketMap)
0208   {
0209     if (iter.first <= eventno)
0210     {
0211       for (auto pktiter : iter.second)
0212       {
0213         delete pktiter;
0214       }
0215       toclearevents.push_back(iter.first);
0216     }
0217     else
0218     {
0219       break;
0220     }
0221   }
0222 
0223   for (auto iter : toclearevents)
0224   {
0225     m_EventStack.erase(iter);
0226     m_PacketMap.erase(iter);
0227   }
0228 }
0229 
0230 void SingleMbdTriggerInput::ClearCurrentEvent()
0231 {
0232   // called interactively, to get rid of the current event
0233   int currentevent = *m_EventStack.begin();
0234   //  std::cout << PHWHERE << "clearing bclk 0x" << std::hex << currentbclk << std::dec << std::endl;
0235   CleanupUsedPackets(currentevent);
0236   return;
0237 }
0238 
0239 void SingleMbdTriggerInput::CreateDSTNode(PHCompositeNode *topNode)
0240 {
0241   PHNodeIterator iter(topNode);
0242   PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
0243   if (!dstNode)
0244   {
0245     dstNode = new PHCompositeNode("DST");
0246     topNode->addNode(dstNode);
0247   }
0248   PHNodeIterator iterDst(dstNode);
0249   PHCompositeNode *detNode = dynamic_cast<PHCompositeNode *>(iterDst.findFirst("PHCompositeNode", "MBD"));
0250   if (!detNode)
0251   {
0252     detNode = new PHCompositeNode("MBD");
0253     dstNode->addNode(detNode);
0254   }
0255   CaloPacketContainer *mbdpacketcont = findNode::getClass<CaloPacketContainer>(detNode, "MBDPackets");
0256   if (!mbdpacketcont)
0257   {
0258     mbdpacketcont = new CaloPacketContainerv1();
0259     PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(mbdpacketcont, "MBDPackets", "PHObject");
0260     detNode->addNode(newNode);
0261   }
0262 }