File indexing completed on 2024-05-10 02:21:17
0001 #include "catch.hpp"
0002 #include "SimG4CMS/Calo/interface/HFShowerLibrary.h"
0003 #include <CLHEP/Units/GlobalPhysicalConstants.h>
0004 #include <CLHEP/Units/SystemOfUnits.h>
0005 #include "FWCore/PluginManager/interface/PresenceFactory.h"
0006 #include "FWCore/PluginManager/interface/PluginManager.h"
0007 #include "FWCore/PluginManager/interface/standard.h"
0008 #include <CLHEP/Random/MixMaxRng.h>
0009 #include "Randomize.hh"
0010
0011 using CLHEP::deg;
0012 using CLHEP::GeV;
0013
0014 namespace {
0015 std::shared_ptr<edm::Presence> setupMessageLogger(bool iDoSetup) {
0016 if (not iDoSetup) {
0017 return std::shared_ptr<edm::Presence>();
0018 }
0019
0020 edmplugin::PluginManager::configure(edmplugin::standard::config());
0021
0022 try {
0023 return std::shared_ptr<edm::Presence>(
0024 edm::PresenceFactory::get()->makePresence("SingleThreadMSPresence").release());
0025 } catch (cms::Exception& e) {
0026 std::cerr << e.explainSelf() << std::endl;
0027 throw;
0028 }
0029 }
0030
0031 class SetRandomEngine {
0032 public:
0033 explicit SetRandomEngine(long iSeed) : m_current(iSeed), m_previous(G4Random::getTheEngine()) {
0034 G4Random::setTheEngine(&m_current);
0035 }
0036 ~SetRandomEngine() { G4Random::setTheEngine(m_previous); }
0037
0038 private:
0039 CLHEP::MixMaxRng m_current;
0040 decltype(G4Random::getTheEngine()) m_previous;
0041 };
0042
0043 }
0044
0045 bool operator==(HFShowerLibrary::Hit const& iLHS, HFShowerLibrary::Hit const& iRHS) {
0046 return iLHS.position == iRHS.position and iLHS.depth == iRHS.depth and iLHS.time == iRHS.time;
0047 }
0048
0049 namespace test_hffibre {
0050 HFFibre::Params defaultParams();
0051 }
0052
0053 TEST_CASE("test HFShowerLibrary", "[HFShowerLibrary]") {
0054
0055 static std::shared_ptr<edm::Presence> gobbleUpTheGoop = setupMessageLogger(false);
0056
0057 auto fibreParams = test_hffibre::defaultParams();
0058
0059
0060 HFShowerLibrary::Params params;
0061 params.probMax_ = 1.;
0062 params.backProb_ = 1.;
0063 params.dphi_ = 10 * deg;
0064 params.equalizeTimeShift_ = false;
0065 params.verbose_ = false;
0066 params.applyFidCut_ = true;
0067
0068 SECTION("v4 file") {
0069 HFShowerLibrary::FileParams fileParams;
0070 edm::FileInPath p("SimG4CMS/Calo/data/HFShowerLibrary_npmt_noatt_eta4_16en_v4.root");
0071 fileParams.fileName_ = p.fullPath();
0072 fileParams.emBranchName_ = "emParticles";
0073 fileParams.hadBranchName_ = "hadParticles";
0074 fileParams.branchEvInfo_ = "";
0075 fileParams.fileVersion_ = 0;
0076 fileParams.cacheBranches_ = false;
0077
0078 HFShowerLibrary showerLibrary(params, fileParams, fibreParams);
0079 SECTION("fillHits") {
0080 SECTION("non EM or Hadron") {
0081 SetRandomEngine guard(11);
0082 bool ok = false;
0083 auto hits = showerLibrary.fillHits({0, 0, 0}, {0, 0, 0}, 0, 0., ok, 1., 0.);
0084 REQUIRE(hits.empty());
0085 REQUIRE(not ok);
0086 }
0087 SECTION("photon within threshold") {
0088 SetRandomEngine guard(11);
0089 bool ok = false;
0090 auto hits = showerLibrary.fillHits(
0091 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 22, 2.2 * GeV, ok, 1., 0.);
0092 REQUIRE(ok);
0093 REQUIRE(hits.size() == 2);
0094 }
0095 SECTION("pion within threshold") {
0096 SetRandomEngine guard(11);
0097 bool ok = false;
0098 auto hits = showerLibrary.fillHits(
0099 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 211, 2.2 * GeV, ok, 1., 0.);
0100 REQUIRE(ok);
0101 REQUIRE(hits.size() == 2);
0102 }
0103 }
0104 SECTION("em cache") {
0105 std::vector<HFShowerLibrary::Hit> nonCached;
0106 {
0107 SetRandomEngine guard(11);
0108 bool ok = false;
0109 nonCached = showerLibrary.fillHits(
0110 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 22, 2.2 * GeV, ok, 1., 0.);
0111 }
0112 std::vector<HFShowerLibrary::Hit> cached;
0113 {
0114 auto newFileParams = fileParams;
0115 newFileParams.cacheBranches_ = true;
0116 HFShowerLibrary showerLibrary(params, newFileParams, fibreParams);
0117
0118 SetRandomEngine guard(11);
0119 bool ok = false;
0120 cached = showerLibrary.fillHits(
0121 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 22, 2.2 * GeV, ok, 1., 0.);
0122 }
0123 REQUIRE(nonCached == cached);
0124 }
0125
0126 SECTION("had cache") {
0127 std::vector<HFShowerLibrary::Hit> nonCached;
0128 {
0129 SetRandomEngine guard(11);
0130 bool ok = false;
0131 nonCached = showerLibrary.fillHits(
0132 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 211, 2.2 * GeV, ok, 1., 0.);
0133 }
0134 std::vector<HFShowerLibrary::Hit> cached;
0135 {
0136 auto newFileParams = fileParams;
0137 newFileParams.cacheBranches_ = true;
0138 HFShowerLibrary showerLibrary(params, newFileParams, fibreParams);
0139
0140 SetRandomEngine guard(11);
0141 bool ok = false;
0142 cached = showerLibrary.fillHits(
0143 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 211, 2.2 * GeV, ok, 1., 0.);
0144 }
0145 REQUIRE(nonCached == cached);
0146 }
0147 }
0148 SECTION("v3 file") {
0149 HFShowerLibrary::FileParams fileParams;
0150 edm::FileInPath p("SimG4CMS/Calo/data/HFShowerLibrary_oldpmt_noatt_eta4_16en_v3.root");
0151 fileParams.fileName_ = p.fullPath();
0152 fileParams.emBranchName_ = "emParticles";
0153 fileParams.hadBranchName_ = "hadParticles";
0154 fileParams.branchEvInfo_ = "";
0155 fileParams.fileVersion_ = 0;
0156 fileParams.cacheBranches_ = false;
0157
0158 HFShowerLibrary showerLibrary(params, fileParams, std::move(fibreParams));
0159 SECTION("fillHits") {
0160 SECTION("non EM or Hadron") {
0161 SetRandomEngine guard(11);
0162 bool ok = false;
0163 auto hits = showerLibrary.fillHits({0, 0, 0}, {0, 0, 0}, 0, 0., ok, 1., 0.);
0164 REQUIRE(hits.empty());
0165 REQUIRE(not ok);
0166 }
0167 SECTION("photon within threshold") {
0168 SetRandomEngine guard(11);
0169 bool ok = false;
0170 auto hits = showerLibrary.fillHits(
0171 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 22, 5.2 * GeV, ok, 1., 0.);
0172 REQUIRE(ok);
0173 REQUIRE(hits.size() == 1);
0174 }
0175 SECTION("pion within threshold") {
0176 SetRandomEngine guard(11);
0177 bool ok = false;
0178 auto hits = showerLibrary.fillHits(
0179 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 211, 5.2 * GeV, ok, 1., 0.);
0180 REQUIRE(ok);
0181 REQUIRE(hits.size() == 1);
0182 }
0183 }
0184 }
0185 SECTION("v6 file") {
0186 HFShowerLibrary::FileParams fileParams;
0187 edm::FileInPath p("SimG4CMS/Calo/data/HFShowerLibrary_run3_v6.root");
0188 fileParams.fileName_ = p.fullPath();
0189 fileParams.emBranchName_ = "emParticles";
0190 fileParams.hadBranchName_ = "hadParticles";
0191 fileParams.branchEvInfo_ = "";
0192 fileParams.fileVersion_ = 2;
0193 fileParams.cacheBranches_ = false;
0194
0195 params.equalizeTimeShift_ = true;
0196
0197 HFShowerLibrary showerLibrary(params, fileParams, std::move(fibreParams));
0198 SECTION("fillHits") {
0199 SECTION("non EM or Hadron") {
0200 SetRandomEngine guard(11);
0201 bool ok = false;
0202 auto hits = showerLibrary.fillHits({0, 0, 0}, {0, 0, 0}, 0, 0., ok, 1., 0.);
0203 REQUIRE(hits.empty());
0204 REQUIRE(not ok);
0205 }
0206 SECTION("photon within threshold") {
0207 SetRandomEngine guard(11);
0208 bool ok = false;
0209 auto hits = showerLibrary.fillHits(
0210 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 22, 2.2 * GeV, ok, 1., 0.);
0211 REQUIRE(ok);
0212 REQUIRE(hits.size() == 5);
0213 }
0214 SECTION("pion within threshold") {
0215 SetRandomEngine guard(11);
0216 bool ok = false;
0217 auto hits = showerLibrary.fillHits(
0218 {-470.637, -618.696, 11150}, {0.000467326, -0.00804975, 0.999967}, 211, 5.2 * GeV, ok, 1., 0.);
0219 REQUIRE(ok);
0220 REQUIRE(hits.size() == 2);
0221 }
0222 }
0223 }
0224 }