NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
NtpTools.cpp
Go to the documentation of this file.
1 #include "NtpMaker/NtpTools.h"
2 
3 #include <fstream>
4 
5 namespace NAIA {
6 namespace NtpTools {
7 std::pair<unsigned int, unsigned int> GetInnerNHits(TrTrackR *track) {
8  if (!track)
9  return std::make_pair(0, 0);
10  unsigned int patt_xy = track->GetBitPatternXYJ();
11  unsigned int patt_y = track->GetBitPatternJ();
12  unsigned int nxy = 0, ny = 0;
13  for (int ilay = 0 + 1; ilay < 9 - 1; ilay++) {
14  if ((patt_xy & (1 << ilay)) > 0)
15  nxy++;
16  if ((patt_y & (1 << ilay)) > 0)
17  ny++;
18  }
19  return std::make_pair(nxy, ny);
20 }
21 
22 std::vector<std::string> ExpandTextFilelist(const std::string &txtFile) {
23  using namespace std::string_literals;
24 
25  std::ifstream infilelist(txtFile);
26  std::vector<std::string> flist;
27  for (std::string bufname; std::getline(infilelist, bufname);) {
28  if (bufname.substr(bufname.length() - 4, 4) == ".txt"s) {
29  auto tmp = ExpandTextFilelist(bufname);
30  flist.insert(end(flist), begin(tmp), end(tmp));
31  } else {
32  flist.push_back(bufname);
33  }
34  }
35 
36  return flist;
37 }
38 
39 std::vector<std::string> LoadInputFiles(std::vector<std::string> &inputFiles) {
40 
41  using namespace std::string_literals;
42  if (inputFiles.size() > 1 && std::any_of(begin(inputFiles), end(inputFiles), [](const std::string &filename) {
43  return filename.substr(filename.length() - 4, 4) == ".txt"s;
44  })) {
45  throw std::runtime_error("Input file list cannot contain mixed txt and root files");
46  }
47 
48  // we need to read the filelist from disk if the only file ends with ".txt"
49  std::vector<std::string> fileList;
50  if (inputFiles.size() == 1 && inputFiles[0].substr(inputFiles[0].length() - 4, 4) == ".txt") {
51  fileList = ExpandTextFilelist(inputFiles[0]);
52  } else {
53  fileList = inputFiles;
54  }
55 
56  return fileList;
57 }
58 
59 // Mute-unmute routines.
60 // From: https://bbs.archlinux.org/viewtopic.php?id=79378
61 // Modified to mute also C-style output.
62 
63 FILE *muteOut(fopen("/dev/null", "w"));
64 std::ofstream fout("/dev/null");
65 
66 FILE *stdoutSave = nullptr;
67 FILE *stderrSave = nullptr;
68 std::streambuf *cout_sbuf = nullptr;
69 std::streambuf *cerr_sbuf = nullptr;
70 
71 bool isMuted = false;
72 
73 void MuteOutput() {
74  if (!std::exchange(isMuted, true)) {
75  stdoutSave = stdout;
76  stdout = muteOut;
77  stderrSave = stderr;
78  stderr = muteOut;
79 
80  cout_sbuf = std::cout.rdbuf();
81  cerr_sbuf = std::cerr.rdbuf();
82  std::cout.rdbuf(fout.rdbuf());
83  std::cerr.rdbuf(fout.rdbuf());
84  }
85 }
86 
87 void UnmuteOutput() {
88  if (std::exchange(isMuted, false)) {
89  if (stdoutSave) {
90  stdout = stdoutSave;
91  stdoutSave = nullptr;
92  }
93  if (stderrSave) {
94  stderr = stderrSave;
95  stderrSave = nullptr;
96  }
97 
98  if (cout_sbuf) {
99  std::cout.rdbuf(cout_sbuf);
100  cout_sbuf = nullptr;
101  }
102  if (cerr_sbuf) {
103  std::cerr.rdbuf(cerr_sbuf);
104  cerr_sbuf = nullptr;
105  }
106  }
107 }
108 
109 } // namespace NtpTools
110 } // namespace NAIA
void MuteOutput()
Definition: NtpTools.cpp:73
std::pair< unsigned int, unsigned int > GetInnerNHits(TrTrackR *track)
Definition: NtpTools.cpp:7
NAIAChain::EventItr begin(NAIAChain &chain)
Definition: NAIAChain.h:297
std::vector< std::string > LoadInputFiles(std::vector< std::string > &inputFiles)
Definition: NtpTools.cpp:39
std::streambuf * cerr_sbuf
Definition: NtpTools.cpp:69
std::vector< std::string > ExpandTextFilelist(const std::string &txtFile)
Definition: NtpTools.cpp:22
FILE * muteOut(fopen("/dev/null","w"))
std::ofstream fout("/dev/null")
FILE * stdoutSave
Definition: NtpTools.cpp:66
FILE * stderrSave
Definition: NtpTools.cpp:67
NAIAChain::EventItr end(NAIAChain &chain)
Definition: NAIAChain.h:298
std::streambuf * cout_sbuf
Definition: NtpTools.cpp:68
void UnmuteOutput()
Definition: NtpTools.cpp:87