File indexing completed on 2024-04-06 12:02:29
0001 #include "CondFormats/PPSObjects/interface/PPSDirectSimulationData.h"
0002
0003 #include "DataFormats/CTPPSDetId/interface/CTPPSDetId.h"
0004 #include "DataFormats/CTPPSDetId/interface/TotemRPDetId.h"
0005 #include "DataFormats/CTPPSDetId/interface/CTPPSPixelDetId.h"
0006 #include "DataFormats/CTPPSDetId/interface/CTPPSDiamondDetId.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 #include "FWCore/Utilities/interface/FileInPath.h"
0009
0010 #include "TFile.h"
0011
0012 #include <iostream>
0013 #include <string>
0014
0015 PPSDirectSimulationData::PPSDirectSimulationData()
0016 : empiricalAperture45_(""),
0017 empiricalAperture56_(""),
0018
0019 timeResolutionDiamonds45_(""),
0020 timeResolutionDiamonds56_("") {}
0021
0022 PPSDirectSimulationData::~PPSDirectSimulationData() {}
0023
0024
0025 const std::string &PPSDirectSimulationData::getEmpiricalAperture45() const { return empiricalAperture45_; }
0026 const std::string &PPSDirectSimulationData::getEmpiricalAperture56() const { return empiricalAperture56_; }
0027
0028 const std::string &PPSDirectSimulationData::getTimeResolutionDiamonds45() const { return timeResolutionDiamonds45_; }
0029 const std::string &PPSDirectSimulationData::getTimeResolutionDiamonds56() const { return timeResolutionDiamonds56_; }
0030
0031 std::map<unsigned int, PPSDirectSimulationData::FileObject> &PPSDirectSimulationData::getEfficienciesPerRP() {
0032 return efficienciesPerRP_;
0033 }
0034 std::map<unsigned int, PPSDirectSimulationData::FileObject> &PPSDirectSimulationData::getEfficienciesPerPlane() {
0035 return efficienciesPerPlane_;
0036 };
0037
0038
0039 void PPSDirectSimulationData::setEmpiricalAperture45(std::string s) { empiricalAperture45_ = s; }
0040 void PPSDirectSimulationData::setEmpiricalAperture56(std::string s) { empiricalAperture56_ = s; }
0041
0042 void PPSDirectSimulationData::setTimeResolutionDiamonds45(std::string s) { timeResolutionDiamonds45_ = s; }
0043 void PPSDirectSimulationData::setTimeResolutionDiamonds56(std::string s) { timeResolutionDiamonds56_ = s; }
0044
0045 std::map<unsigned int, std::unique_ptr<TH2F>> PPSDirectSimulationData::loadEffeciencyHistogramsPerRP() const {
0046 std::map<unsigned int, std::unique_ptr<TH2F>> result;
0047
0048 for (const auto &it : efficienciesPerRP_)
0049 result[it.first] = loadObject(it.second.first, it.second.second);
0050
0051 return result;
0052 }
0053
0054 std::map<unsigned int, std::unique_ptr<TH2F>> PPSDirectSimulationData::loadEffeciencyHistogramsPerPlane() const {
0055 std::map<unsigned int, std::unique_ptr<TH2F>> result;
0056
0057 for (const auto &it : efficienciesPerPlane_) {
0058 CTPPSDetId rpId(it.first);
0059
0060 if (rpId.subdetId() == CTPPSDetId::sdTrackingStrip) {
0061 for (unsigned int pl = 0; pl < 10; ++pl) {
0062 TotemRPDetId plId(rpId.arm(), rpId.station(), rpId.rp(), pl);
0063 result[plId] = loadObject(it.second.first, replace(it.second.second, "<detid>", std::to_string(pl)));
0064 }
0065 }
0066
0067 if (rpId.subdetId() == CTPPSDetId::sdTrackingPixel) {
0068 for (unsigned int pl = 0; pl < 6; ++pl) {
0069 CTPPSPixelDetId plId(rpId.arm(), rpId.station(), rpId.rp(), pl);
0070 result[plId] = loadObject(it.second.first, replace(it.second.second, "<detid>", std::to_string(pl)));
0071 }
0072 }
0073
0074 if (rpId.subdetId() == CTPPSDetId::sdTimingDiamond) {
0075 for (unsigned int pl = 0; pl < 4; ++pl) {
0076 CTPPSDiamondDetId plId(rpId.arm(), rpId.station(), rpId.rp(), pl);
0077 result[plId] = loadObject(it.second.first, replace(it.second.second, "<detid>", std::to_string(pl)));
0078 }
0079 }
0080 }
0081
0082 return result;
0083 }
0084
0085 std::unique_ptr<TH2F> PPSDirectSimulationData::loadObject(const std::string &file, const std::string &object) {
0086 edm::FileInPath fip(file.c_str());
0087 TFile *f_in = TFile::Open(fip.fullPath().c_str());
0088 if (!f_in)
0089 throw cms::Exception("PPS") << "Cannot open file '" << fip.fullPath() << "'.";
0090
0091 TH2F *o_in = (TH2F *)f_in->Get(object.c_str());
0092 if (!o_in)
0093 throw cms::Exception("PPS") << "Cannot load object '" << object << "' from file '" << fip.fullPath() << "'.";
0094
0095
0096 o_in->SetDirectory(nullptr);
0097
0098 delete f_in;
0099
0100 return std::unique_ptr<TH2F>(o_in);
0101 }
0102
0103 std::string PPSDirectSimulationData::replace(std::string input, const std::string &from, const std::string &to) {
0104 size_t start_pos = 0;
0105 while ((start_pos = input.find(from, start_pos)) != std::string::npos) {
0106 input.replace(start_pos, from.length(), to);
0107 start_pos += to.length();
0108 }
0109 return input;
0110 }