Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InputFileHandler.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file InputFileHandler.cc
1 #include "InputFileHandler.h"
2 
3 #include <phool/phool.h>
4 
5 #include <algorithm>
6 #include <filesystem>
7 #include <fstream>
8 #include <iostream>
9 
11 {
12  if (GetVerbosity() > 0)
13  {
14  std::cout << "Adding " << filename << " to list of input files" << std::endl;
15  }
16  m_FileList.push_back(filename);
17  m_FileListCopy.push_back(filename);
18  return 0;
19 }
20 
22 {
23  // checking filesize to see if we have a text file
24  if (std::filesystem::exists(filename.c_str()))
25  {
26  if (std::filesystem::is_regular_file(filename.c_str()))
27  {
28  // uintmax_t fsize = std::filesystem::file_size(filename.c_str());
29  }
30  else
31  {
32  std::cout << filename << " is not a regular file" << std::endl;
33  return -1;
34  }
35  }
36  else
37  {
38  std::cout << PHWHERE << "Could not open " << filename << std::endl;
39  return -1;
40  }
41  std::ifstream infile;
42  infile.open(filename, std::ios_base::in);
43  if (!infile)
44  {
45  std::cout << PHWHERE << "Could not open " << filename << std::endl;
46  return -1;
47  }
48  std::string FullLine;
49  int nfiles = 0;
50  getline(infile, FullLine);
51  while (!infile.eof())
52  {
53  if (!std::all_of(FullLine.begin(), FullLine.end(), ::isprint))
54  {
55  std::cout << PHWHERE << "file " << filename
56  << " contains non printable characters, it is likely a binary file" << std::endl;
57  return -1;
58  }
59  if (!FullLine.empty() && FullLine[0] != '#') // remove comments
60  {
61  AddFile(FullLine);
62  nfiles++;
63  }
64  else if (!FullLine.empty())
65  {
66  if (GetVerbosity() > 0)
67  {
68  std::cout << "Found Comment: " << FullLine << std::endl;
69  }
70  }
71  getline(infile, FullLine);
72  }
73  infile.close();
74  if (nfiles == 0)
75  {
76  std::cout << " listfile " << filename << " does not contain filenames "
77  << "if this is the only list you load into this Input Manager your code will exit very soon" << std::endl;
78  }
79  return 0;
80 }
81 
83 {
84  while (!m_FileList.empty())
85  {
86  std::list<std::string>::const_iterator iter = m_FileList.begin();
87  if (GetVerbosity())
88  {
89  std::cout << PHWHERE << " opening next file: " << *iter << std::endl;
90  }
91  if (fileopen(*iter))
92  {
93  std::cout << PHWHERE << " could not open file: " << *iter << std::endl;
94  m_FileList.pop_front();
95  }
96  else
97  {
98  return 1;
99  }
100  }
101  return 0;
102 }
103 
104 void InputFileHandler::Print(const std::string & /* what */) const
105 {
106  std::cout << "file list: " << std::endl;
107  for (const auto &iter : m_FileList)
108  {
109  std::cout << iter << std::endl;
110  }
111 }
112 
114 {
115  if (!m_FileList.empty())
116  {
117  if (m_Repeat)
118  {
119  m_FileList.push_back(*(m_FileList.begin()));
120  if (m_Repeat > 0)
121  {
122  m_Repeat--;
123  }
124  }
125  m_FileList.pop_front();
126  }
127  return;
128 }