Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "Fun4AllInputManager.h"
0002 
0003 #include "Fun4AllServer.h"
0004 #include "SubsysReco.h"
0005 
0006 #include <phool/phool.h>
0007 
0008 #include <boost/filesystem.hpp>
0009 
0010 #include <cstdint>  // for uintmax_t
0011 #include <fstream>
0012 #include <iostream>
0013 
0014 Fun4AllInputManager::Fun4AllInputManager(const std::string &name, const std::string &nodename, const std::string &topnodename)
0015   : Fun4AllBase(name)
0016   , m_InputNode(nodename)
0017   , m_TopNodeName(topnodename)
0018 {
0019   return;
0020 }
0021 
0022 Fun4AllInputManager::~Fun4AllInputManager()
0023 {
0024   while (m_SubsystemsVector.begin() != m_SubsystemsVector.end())
0025   {
0026     if (Verbosity())
0027     {
0028       m_SubsystemsVector.back()->Verbosity(Verbosity());
0029     }
0030     delete m_SubsystemsVector.back();
0031     m_SubsystemsVector.pop_back();
0032   }
0033 }
0034 
0035 int Fun4AllInputManager::AddFile(const std::string &filename)
0036 {
0037   if (Verbosity() > 0)
0038   {
0039     std::cout << "Adding " << filename << " to list of input files for "
0040               << Name() << std::endl;
0041   }
0042   m_FileList.push_back(filename);
0043   m_FileListCopy.push_back(filename);
0044   return 0;
0045 }
0046 
0047 int Fun4AllInputManager::AddListFile(const std::string &filename, const int do_it)
0048 {
0049   // checking filesize to see if we have a text file
0050   if (boost::filesystem::exists(filename.c_str()))
0051   {
0052     if (boost::filesystem::is_regular_file(filename.c_str()))
0053     {
0054       uintmax_t fsize = boost::filesystem::file_size(filename.c_str());
0055       if (fsize > 1000000 && !do_it)
0056       {
0057         std::cout << "size of " << filename
0058                   << " is suspiciously large for a text file: "
0059                   << fsize << " bytes" << std::endl;
0060         std::cout << "if you really want to use " << filename
0061                   << " as list file (it will be used as a text file containing a list of input files), use AddListFile(\""
0062                   << filename << "\",1)" << std::endl;
0063         return -1;
0064       }
0065     }
0066     else
0067     {
0068       std::cout << filename << " is not a regular file" << std::endl;
0069       return -1;
0070     }
0071   }
0072   else
0073   {
0074     std::cout << PHWHERE << "Could not open " << filename << std::endl;
0075     return -1;
0076   }
0077   std::ifstream infile;
0078   infile.open(filename, std::ios_base::in);
0079   if (!infile)
0080   {
0081     std::cout << PHWHERE << "Could not open " << filename << std::endl;
0082     return -1;
0083   }
0084   std::string FullLine;
0085   int nfiles = 0;
0086   getline(infile, FullLine);
0087   while (!infile.eof())
0088   {
0089     if (!FullLine.empty() && FullLine[0] != '#')  // remove comments
0090     {
0091       AddFile(FullLine);
0092       nfiles++;
0093     }
0094     else if (!FullLine.empty())
0095     {
0096       if (Verbosity() > 0)
0097       {
0098         std::cout << "Found Comment: " << FullLine << std::endl;
0099       }
0100     }
0101     getline(infile, FullLine);
0102   }
0103   infile.close();
0104   if (nfiles == 0)
0105   {
0106     std::cout << Name() << " listfile " << filename << " does not contain filenames "
0107               << "if this is the only list you load into this Input Manager your code will exit very soon" << std::endl;
0108   }
0109   return 0;
0110 }
0111 
0112 void Fun4AllInputManager::Print(const std::string &what) const
0113 {
0114   if (what == "ALL" || what == "FILELIST")
0115   {
0116     std::cout << "--------------------------------------" << std::endl
0117               << std::endl;
0118     std::cout << "List of input files in Fun4AllInputManager " << Name() << ":" << std::endl;
0119 
0120     for (const std::string &file : m_FileList)
0121     {
0122       std::cout << file << std::endl;
0123     }
0124   }
0125   if (what == "ALL" || what == "SUBSYSTEMS")
0126   {
0127     // loop over the map and print out the content (name and location in memory)
0128     std::cout << "--------------------------------------" << std::endl
0129               << std::endl;
0130     std::cout << "List of SubsysRecos in Fun4AllInputManager " << Name() << ":" << std::endl;
0131 
0132     for (SubsysReco *subsys : m_SubsystemsVector)
0133     {
0134       std::cout << subsys->Name() << std::endl;
0135     }
0136     std::cout << std::endl;
0137   }
0138   return;
0139 }
0140 
0141 int Fun4AllInputManager::registerSubsystem(SubsysReco *subsystem)
0142 {
0143   Fun4AllServer *se = Fun4AllServer::instance();
0144   int iret = subsystem->Init(se->topNode(m_TopNodeName));
0145   if (iret)
0146   {
0147     std::cout << PHWHERE << " Error initializing subsystem "
0148               << subsystem->Name() << ", return code: " << iret << std::endl;
0149     return iret;
0150   }
0151   if (Verbosity() > 0)
0152   {
0153     std::cout << "Registering Subsystem " << subsystem->Name() << std::endl;
0154   }
0155   m_SubsystemsVector.push_back(subsystem);
0156   return 0;
0157 }
0158 
0159 int Fun4AllInputManager::RejectEvent()
0160 {
0161   if (!m_SubsystemsVector.empty())
0162   {
0163     Fun4AllServer *se = Fun4AllServer::instance();
0164     for (SubsysReco *subsys : m_SubsystemsVector)
0165     {
0166       if (!m_InitRun)
0167       {
0168         subsys->InitRun(se->topNode(m_TopNodeName));
0169         m_InitRun = 1;
0170       }
0171       if (Verbosity() > 0)
0172       {
0173         std::cout << Name() << ": Fun4AllInpuManager::EventReject processing " << subsys->Name() << std::endl;
0174       }
0175       if (subsys->process_event(se->topNode(m_TopNodeName)) != Fun4AllReturnCodes::EVENT_OK)
0176       {
0177         return Fun4AllReturnCodes::DISCARDEVENT;
0178       }
0179     }
0180   }
0181   return Fun4AllReturnCodes::EVENT_OK;
0182 }
0183 
0184 int Fun4AllInputManager::ResetFileList()
0185 {
0186   if (m_FileListCopy.empty())
0187   {
0188     std::cout << Name() << ": ResetFileList can only be used with filelists" << std::endl;
0189     return -1;
0190   }
0191   m_FileList.clear();
0192   m_FileList = m_FileListCopy;
0193   return 0;
0194 }
0195 
0196 void Fun4AllInputManager::UpdateFileList()
0197 {
0198   if (!m_FileList.empty())
0199   {
0200     if (m_Repeat)
0201     {
0202       m_FileList.push_back(*(m_FileList.begin()));
0203       if (m_Repeat > 0)
0204       {
0205         m_Repeat--;
0206       }
0207     }
0208     m_FileList.pop_front();
0209   }
0210   return;
0211 }
0212 
0213 int Fun4AllInputManager::OpenNextFile()
0214 {
0215   while (!m_FileList.empty())
0216   {
0217     std::list<std::string>::const_iterator iter = m_FileList.begin();
0218     if (Verbosity())
0219     {
0220       std::cout << PHWHERE << " opening next file: " << *iter << std::endl;
0221     }
0222     if (fileopen(*iter))
0223     {
0224       std::cout << PHWHERE << " could not open file: " << *iter << std::endl;
0225       m_FileList.pop_front();
0226     }
0227     else
0228     {
0229       return 0;
0230     }
0231   }
0232   return -1;
0233 }