Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "Fun4AllPrdfInputManager.h"
0002 
0003 #include <fun4all/Fun4AllInputManager.h>  // for Fun4AllInputManager
0004 #include <fun4all/Fun4AllReturnCodes.h>
0005 #include <fun4all/Fun4AllServer.h>
0006 #include <fun4all/Fun4AllSyncManager.h>
0007 #include <fun4all/Fun4AllUtils.h>
0008 
0009 #include <ffaobjects/SyncObject.h>  // for SyncObject
0010 #include <ffaobjects/SyncObjectv1.h>
0011 
0012 #include <frog/FROG.h>
0013 
0014 #include <phool/PHCompositeNode.h>
0015 #include <phool/PHDataNode.h>
0016 #include <phool/PHNode.h>          // for PHNode
0017 #include <phool/PHNodeIterator.h>  // for PHNodeIterator
0018 #include <phool/PHObject.h>        // for PHObject
0019 #include <phool/phool.h>           // for PHWHERE
0020 
0021 #include <Event/Event.h>
0022 #include <Event/Eventiterator.h>  // for Eventiterator
0023 #include <Event/EventTypes.h>
0024 #include <Event/fileEventiterator.h>
0025 
0026 #include <cassert>
0027 #include <cstdlib>
0028 #include <iostream>  // for operator<<, basic_ostream, endl
0029 #include <utility>   // for pair
0030 
0031 using namespace std;
0032 
0033 Fun4AllPrdfInputManager::Fun4AllPrdfInputManager(const std::string &name, const std::string &prdfnodename, const std::string &topnodename)
0034   : Fun4AllInputManager(name, prdfnodename, topnodename)
0035   , m_SyncObject(new SyncObjectv1())
0036   , m_PrdfNodeName(prdfnodename)
0037 {
0038   Fun4AllServer *se = Fun4AllServer::instance();
0039   m_topNode = se->topNode(TopNodeName());
0040   PHNodeIterator iter(m_topNode);
0041   PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
0042   if (!PrdfNode)
0043   {
0044     PHDataNode<Event> *newNode = new PHDataNode<Event>(m_Event, m_PrdfNodeName, "Event");
0045     m_topNode->addNode(newNode);
0046   }
0047   return;
0048 }
0049 
0050 Fun4AllPrdfInputManager::~Fun4AllPrdfInputManager()
0051 {
0052   if (IsOpen())
0053   {
0054     fileclose();
0055   }
0056   delete m_SyncObject;
0057 }
0058 
0059 int Fun4AllPrdfInputManager::fileopen(const std::string &filenam)
0060 {
0061   if (IsOpen())
0062   {
0063     std::cout << "Closing currently open file "
0064               << FileName()
0065               << " and opening " << filenam << std::endl;
0066     fileclose();
0067   }
0068   FileName(filenam);
0069   FROG frog;
0070   std::string fname = frog.location(FileName());
0071   if (Verbosity() > 0)
0072   {
0073     std::cout << Name() << ": opening file " << FileName() << std::endl;
0074   }
0075   int status = 0;
0076   m_EventIterator = new fileEventiterator(fname.c_str(), status);
0077   m_EventsThisFile = 0;
0078   if (status)
0079   {
0080     delete m_EventIterator;
0081     m_EventIterator = nullptr;
0082     std::cout << PHWHERE << Name() << ": could not open file " << fname << std::endl;
0083     return -1;
0084   }
0085   pair<int, int> runseg = Fun4AllUtils::GetRunSegment(fname);
0086   m_Segment = runseg.second;
0087   IsOpen(1);
0088   AddToFileOpened(fname);  // add file to the list of files which were opened
0089   return 0;
0090 }
0091 
0092 int Fun4AllPrdfInputManager::run(const int /*nevents*/)
0093 {
0094 readagain:
0095   if (!IsOpen())
0096   {
0097     if (FileListEmpty())
0098 
0099     {
0100       if (Verbosity() > 0)
0101       {
0102         std::cout << Name() << ": No Input file open" << std::endl;
0103       }
0104       return -1;
0105     }
0106     else
0107     {
0108       if (OpenNextFile())
0109       {
0110         std::cout << Name() << ": No Input file from filelist opened" << std::endl;
0111         return -1;
0112       }
0113     }
0114   }
0115   if (Verbosity() > 3)
0116   {
0117     std::cout << "Getting Event from " << Name() << std::endl;
0118   }
0119   //  std::cout << "running event " << nevents << std::endl;
0120   PHNodeIterator iter(m_topNode);
0121   PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
0122   if (m_SaveEvent)  // if an event was pushed back, copy saved pointer and reset m_SaveEvent pointer
0123   {
0124     m_Event = m_SaveEvent;
0125     m_SaveEvent = nullptr;
0126     m_EventsThisFile--;
0127     m_EventsTotal--;
0128   }
0129   else
0130   {
0131     m_Event = m_EventIterator->getNextEvent();
0132   }
0133   if (!m_Event || m_Event->getEvtType() == ENDRUNEVENT)
0134   {
0135     PrdfNode->setData(nullptr);
0136     fileclose();
0137     // NOLINTNEXTLINE(hicpp-avoid-goto)
0138     goto readagain;
0139   }
0140   PrdfNode->setData(m_Event);
0141   if (Verbosity() > 1)
0142   {
0143     std::cout << Name() << " PRDF run " << m_Event->getRunNumber() << ", evt no: " << m_Event->getEvtSequence() << std::endl;
0144   }
0145   m_EventsTotal++;
0146   m_EventsThisFile++;
0147   SetRunNumber(m_Event->getRunNumber());
0148   MySyncManager()->PrdfEvents(m_EventsThisFile);
0149   MySyncManager()->SegmentNumber(m_Segment);
0150   MySyncManager()->CurrentEvent(m_Event->getEvtSequence());
0151   m_SyncObject->EventCounter(m_EventsThisFile);
0152   m_SyncObject->SegmentNumber(m_Segment);
0153   m_SyncObject->RunNumber(m_Event->getRunNumber());
0154   m_SyncObject->EventNumber(m_Event->getEvtSequence());
0155   // check if the local SubsysReco discards this event
0156   if (RejectEvent() != Fun4AllReturnCodes::EVENT_OK)
0157   {
0158     ResetEvent();
0159     // NOLINTNEXTLINE(hicpp-avoid-goto)
0160     goto readagain;
0161   }
0162   return 0;
0163 }
0164 
0165 int Fun4AllPrdfInputManager::fileclose()
0166 {
0167   if (!IsOpen())
0168   {
0169     std::cout << Name() << ": fileclose: No Input file open" << std::endl;
0170     return -1;
0171   }
0172   delete m_EventIterator;
0173   m_EventIterator = nullptr;
0174   IsOpen(0);
0175   // if we have a file list, move next entry to top of the list
0176   // or repeat the same entry again
0177   UpdateFileList();
0178   return 0;
0179 }
0180 
0181 void Fun4AllPrdfInputManager::Print(const std::string &what) const
0182 {
0183   Fun4AllInputManager::Print(what);
0184   return;
0185 }
0186 
0187 int Fun4AllPrdfInputManager::ResetEvent()
0188 {
0189   PHNodeIterator iter(m_topNode);
0190   PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
0191   PrdfNode->setData(nullptr);  // set pointer in Node to nullptr before deleting it
0192   delete m_Event;
0193   m_Event = nullptr;
0194   m_SyncObject->Reset();
0195   return 0;
0196 }
0197 
0198 int Fun4AllPrdfInputManager::PushBackEvents(const int i)
0199 {
0200   // PushBackEvents is supposedly pushing events back on the stack which works
0201   // easily with root trees (just grab a different entry) but hard in these HepMC ASCII files.
0202   // A special case is when the synchronization fails and we need to only push back a single
0203   // event. In this case we save the m_Event pointer as m_SaveEvent which is used in the run method
0204   // instead of getting the next event.
0205   if (i > 0)
0206   {
0207     if (i == 1 && m_Event)  // check on m_Event pointer makes sure it is not done from the cmd line
0208     {
0209       m_SaveEvent = m_Event;
0210       return 0;
0211     }
0212     std::cout << PHWHERE << Name()
0213               << " Fun4AllPrdfInputManager cannot push back " << i << " events into file"
0214               << std::endl;
0215     return -1;
0216   }
0217   if (!m_EventIterator)
0218   {
0219     std::cout << PHWHERE << Name()
0220               << " no file open" << std::endl;
0221     return -1;
0222   }
0223   // Skipping events is implemented as
0224   // pushing a negative number of events on the stack, so in order to implement
0225   // the skipping of events we read -i events.
0226   int nevents = -i;  // negative number of events to push back -> skip num events
0227   int errorflag = 0;
0228   while (nevents > 0 && !errorflag)
0229   {
0230     m_Event = m_EventIterator->getNextEvent();
0231     if (!m_Event)
0232     {
0233       std::cout << "Error after skipping " << i - nevents
0234                 << " file exhausted?" << std::endl;
0235       errorflag = -1;
0236       fileclose();
0237     }
0238     else
0239     {
0240       if (Verbosity() > 3)
0241       {
0242         std::cout << "Skipping evt no: " << m_Event->getEvtSequence() << std::endl;
0243       }
0244     }
0245     delete m_Event;
0246     m_Event = nullptr;
0247     nevents--;
0248   }
0249   return errorflag;
0250 }
0251 
0252 int Fun4AllPrdfInputManager::GetSyncObject(SyncObject **mastersync)
0253 {
0254   // here we copy the sync object from the current file to the
0255   // location pointed to by mastersync. If mastersync is a 0 pointer
0256   // the syncobject is cloned. If mastersync allready exists the content
0257   // of syncobject is copied
0258   if (!(*mastersync))
0259   {
0260     if (m_SyncObject)
0261     {
0262       *mastersync = dynamic_cast<SyncObject *>(m_SyncObject->CloneMe());
0263       assert(*mastersync);
0264     }
0265   }
0266   else
0267   {
0268     *(*mastersync) = *m_SyncObject;  // copy syncobject content
0269   }
0270   return Fun4AllReturnCodes::SYNC_OK;
0271 }
0272 
0273 int Fun4AllPrdfInputManager::SyncIt(const SyncObject *mastersync)
0274 {
0275   if (!mastersync)
0276   {
0277     std::cout << PHWHERE << Name() << " No MasterSync object, cannot perform synchronization" << std::endl;
0278     std::cout << "Most likely your first file does not contain a SyncObject and the file" << std::endl;
0279     std::cout << "opened by the Fun4AllDstInputManager with Name " << Name() << " has one" << std::endl;
0280     std::cout << "Change your macro and use the file opened by this input manager as first input" << std::endl;
0281     std::cout << "and you will be okay. Fun4All will not process the current configuration" << std::endl
0282               << std::endl;
0283     return Fun4AllReturnCodes::SYNC_FAIL;
0284   }
0285   int iret = m_SyncObject->Different(mastersync);
0286   if (iret)
0287   {
0288     std::cout << "big problem" << std::endl;
0289     exit(1);
0290   }
0291   return Fun4AllReturnCodes::SYNC_OK;
0292 }
0293 
0294 std::string Fun4AllPrdfInputManager::GetString(const std::string &what) const
0295 {
0296   if (what == "PRDFNODENAME")
0297   {
0298     return m_PrdfNodeName;
0299   }
0300   return "";
0301 }