NAIA
 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 // Mute-unmute routines.
23 // From: https://bbs.archlinux.org/viewtopic.php?id=79378
24 // Modified to mute also C-style output.
25 
26 FILE *muteOut(fopen("/dev/null", "w"));
27 std::ofstream fout("/dev/null");
28 
29 FILE *stdoutSave = nullptr;
30 FILE *stderrSave = nullptr;
31 std::streambuf *cout_sbuf = nullptr;
32 std::streambuf *cerr_sbuf = nullptr;
33 
34 bool isMuted = false;
35 
36 void MuteOutput() {
37  if (!std::exchange(isMuted, true)) {
38  stdoutSave = stdout;
39  stdout = muteOut;
40  stderrSave = stderr;
41  stderr = muteOut;
42 
43  cout_sbuf = std::cout.rdbuf();
44  cerr_sbuf = std::cerr.rdbuf();
45  std::cout.rdbuf(fout.rdbuf());
46  std::cerr.rdbuf(fout.rdbuf());
47  }
48 }
49 
50 void UnmuteOutput() {
51  if (std::exchange(isMuted, false)) {
52  if (stdoutSave) {
53  stdout = stdoutSave;
54  stdoutSave = nullptr;
55  }
56  if (stderrSave) {
57  stderr = stderrSave;
58  stderrSave = nullptr;
59  }
60 
61  if (cout_sbuf) {
62  std::cout.rdbuf(cout_sbuf);
63  cout_sbuf = nullptr;
64  }
65  if (cerr_sbuf) {
66  std::cerr.rdbuf(cerr_sbuf);
67  cerr_sbuf = nullptr;
68  }
69  }
70 }
71 
72 } // namespace NtpTools
73 } // namespace NAIA
void MuteOutput()
Definition: NtpTools.cpp:36
std::pair< unsigned int, unsigned int > GetInnerNHits(TrTrackR *track)
Definition: NtpTools.cpp:7
std::streambuf * cerr_sbuf
Definition: NtpTools.cpp:32
FILE * muteOut(fopen("/dev/null","w"))
std::ofstream fout("/dev/null")
FILE * stdoutSave
Definition: NtpTools.cpp:29
FILE * stderrSave
Definition: NtpTools.cpp:30
std::streambuf * cout_sbuf
Definition: NtpTools.cpp:31
void UnmuteOutput()
Definition: NtpTools.cpp:50