Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:29:27

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