Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:10

0001 #include "SimG4CMS/Muon/interface/SimHitPrinter.h"
0002 
0003 #include <G4ios.hh>
0004 #include <iomanip>
0005 #include <iostream>
0006 #include <memory>
0007 #include <mutex>
0008 
0009 std::atomic<std::ofstream*> SimHitPrinter::theFile(nullptr);
0010 
0011 namespace {
0012   std::mutex fileMutex;
0013 }
0014 
0015 SimHitPrinter::SimHitPrinter(std::string filename) {
0016   if (theFile)
0017     return;
0018   const char* theName = filename.c_str();
0019   auto f = std::make_unique<std::ofstream>(theName, std::ios::out);
0020 
0021   std::ofstream* previous = nullptr;
0022   if (theFile.compare_exchange_strong(previous, f.get())) {
0023     //this thread was the first one to try to set the value
0024     f.release();
0025   }
0026 }
0027 
0028 SimHitPrinter::~SimHitPrinter() {
0029   //  theFile->close();
0030 }
0031 
0032 void SimHitPrinter::startNewSimHit(std::string s) {
0033   G4cout.width(10);
0034   G4cout.setf(std::ios::right, std::ios::adjustfield);
0035   G4cout.setf(std::ios::scientific, std::ios::floatfield);
0036   G4cout.precision(6);
0037   G4cout << "SimHit in " << s << G4endl;
0038 
0039   std::lock_guard<std::mutex> guard{fileMutex};
0040   (*theFile).width(10);
0041   (*theFile).setf(std::ios::right, std::ios::adjustfield);
0042   (*theFile).setf(std::ios::scientific | std::ios::uppercase | std::ios::showpos, std::ios::floatfield);
0043   (*theFile).precision(5);
0044   (*theFile) << "SimHit in " << s;
0045 }
0046 
0047 void SimHitPrinter::startNewEvent(int num) {
0048   std::lock_guard<std::mutex> guard{fileMutex};
0049   (*theFile) << "Event " << num << std::endl;
0050 }
0051 
0052 void SimHitPrinter::printId(int id) const {
0053   G4cout << " Id: " << id << G4endl;
0054   std::lock_guard<std::mutex> guard{fileMutex};
0055   (*theFile) << " id ";
0056   (*theFile).width(10);
0057   (*theFile).setf(std::ios::right, std::ios::adjustfield);
0058   (*theFile) << id;
0059 }
0060 
0061 void SimHitPrinter::printTrack(int id) const {
0062   G4cout << " Track: " << id << G4endl;
0063   std::lock_guard<std::mutex> guard{fileMutex};
0064   (*theFile) << " trk ";
0065   (*theFile).width(10);
0066   (*theFile).setf(std::ios::right, std::ios::adjustfield);
0067   (*theFile) << id;
0068 }
0069 
0070 void SimHitPrinter::printPabs(float pabs) const {
0071   G4cout << " Pabs: " << pabs << G4endl;
0072   std::lock_guard<std::mutex> guard{fileMutex};
0073   (*theFile) << " p " << pabs;
0074 }
0075 
0076 void SimHitPrinter::printEloss(float eloss) const {
0077   G4cout << " Eloss: " << eloss << G4endl;
0078   std::lock_guard<std::mutex> guard{fileMutex};
0079   (*theFile) << " e " << eloss;
0080 }
0081 
0082 void SimHitPrinter::printLocal(LocalPoint localen, LocalPoint localex) const {
0083   G4cout << " Local(en/ex): " << localen.x() << " " << localen.y() << " " << localen.z() << " / " << localex.x() << " "
0084          << localex.y() << " " << localex.z() << G4endl;
0085   std::lock_guard<std::mutex> guard{fileMutex};
0086   (*theFile).width(10);
0087   (*theFile).setf(std::ios::right, std::ios::adjustfield);
0088   (*theFile).setf(std::ios::floatfield);
0089   (*theFile).precision(6);
0090   (*theFile) << " en/ex " << localen.x() << " " << localen.y() << " " << localen.z() << " / " << localex.x() << " "
0091              << localex.y() << " " << localex.z();
0092 }
0093 
0094 void SimHitPrinter::printGlobal(GlobalPoint global) const {
0095   G4cout << " Global(en): " << global.x() << " " << global.y() << " " << global.z() << G4endl;
0096   std::lock_guard<std::mutex> guard{fileMutex};
0097   (*theFile).width(10);
0098   (*theFile).setf(std::ios::right, std::ios::adjustfield);
0099   (*theFile).setf(std::ios::floatfield);
0100   (*theFile).precision(6);
0101   (*theFile) << " gl " << global.x() << " " << global.y() << " " << global.z() << std::endl;
0102 }