CMS 3D CMS Logo

SimHitPrinter.cc
Go to the documentation of this file.
2 
3 #include <G4ios.hh>
4 #include <iomanip>
5 #include <iostream>
6 #include <memory>
7 #include <mutex>
8 
9 std::atomic<std::ofstream*> SimHitPrinter::theFile(nullptr);
10 
11 namespace {
12  std::mutex fileMutex;
13 }
14 
16  if (theFile)
17  return;
18  const char* theName = filename.c_str();
19  auto f = std::make_unique<std::ofstream>(theName, std::ios::out);
20 
21  std::ofstream* previous = nullptr;
22  if (theFile.compare_exchange_strong(previous, f.get())) {
23  //this thread was the first one to try to set the value
24  f.release();
25  }
26 }
27 
29  // theFile->close();
30 }
31 
33  G4cout.width(10);
34  G4cout.setf(std::ios::right, std::ios::adjustfield);
35  G4cout.setf(std::ios::scientific, std::ios::floatfield);
36  G4cout.precision(6);
37  G4cout << "SimHit in " << s << G4endl;
38 
39  std::lock_guard<std::mutex> guard{fileMutex};
40  (*theFile).width(10);
41  (*theFile).setf(std::ios::right, std::ios::adjustfield);
42  (*theFile).setf(std::ios::scientific | std::ios::uppercase | std::ios::showpos, std::ios::floatfield);
43  (*theFile).precision(5);
44  (*theFile) << "SimHit in " << s;
45 }
46 
48  std::lock_guard<std::mutex> guard{fileMutex};
49  (*theFile) << "Event " << num << std::endl;
50 }
51 
52 void SimHitPrinter::printId(int id) const {
53  G4cout << " Id: " << id << G4endl;
54  std::lock_guard<std::mutex> guard{fileMutex};
55  (*theFile) << " id ";
56  (*theFile).width(10);
57  (*theFile).setf(std::ios::right, std::ios::adjustfield);
58  (*theFile) << id;
59 }
60 
61 void SimHitPrinter::printTrack(int id) const {
62  G4cout << " Track: " << id << G4endl;
63  std::lock_guard<std::mutex> guard{fileMutex};
64  (*theFile) << " trk ";
65  (*theFile).width(10);
66  (*theFile).setf(std::ios::right, std::ios::adjustfield);
67  (*theFile) << id;
68 }
69 
70 void SimHitPrinter::printPabs(float pabs) const {
71  G4cout << " Pabs: " << pabs << G4endl;
72  std::lock_guard<std::mutex> guard{fileMutex};
73  (*theFile) << " p " << pabs;
74 }
75 
76 void SimHitPrinter::printEloss(float eloss) const {
77  G4cout << " Eloss: " << eloss << G4endl;
78  std::lock_guard<std::mutex> guard{fileMutex};
79  (*theFile) << " e " << eloss;
80 }
81 
82 void SimHitPrinter::printLocal(LocalPoint localen, LocalPoint localex) const {
83  G4cout << " Local(en/ex): " << localen.x() << " " << localen.y() << " " << localen.z() << " / " << localex.x() << " "
84  << localex.y() << " " << localex.z() << G4endl;
85  std::lock_guard<std::mutex> guard{fileMutex};
86  (*theFile).width(10);
87  (*theFile).setf(std::ios::right, std::ios::adjustfield);
88  (*theFile).setf(std::ios::floatfield);
89  (*theFile).precision(6);
90  (*theFile) << " en/ex " << localen.x() << " " << localen.y() << " " << localen.z() << " / " << localex.x() << " "
91  << localex.y() << " " << localex.z();
92 }
93 
95  G4cout << " Global(en): " << global.x() << " " << global.y() << " " << global.z() << G4endl;
96  std::lock_guard<std::mutex> guard{fileMutex};
97  (*theFile).width(10);
98  (*theFile).setf(std::ios::right, std::ios::adjustfield);
99  (*theFile).setf(std::ios::floatfield);
100  (*theFile).precision(6);
101  (*theFile) << " gl " << global.x() << " " << global.y() << " " << global.z() << std::endl;
102 }
void printPabs(float) const
void printTrack(int) const
T z() const
Definition: PV3DBase.h:61
static std::mutex mutex
Definition: Proxy.cc:8
void printEloss(float) const
void printLocal(LocalPoint, LocalPoint) const
void printId(int) const
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
void startNewEvent(int)
double f[11][100]
static std::atomic< std::ofstream * > theFile
Definition: SimHitPrinter.h:39
SimHitPrinter(std::string)
void printGlobal(GlobalPoint) const
void startNewSimHit(std::string)