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