Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:43

0001 
0002 #include "JANA/Components/JOmniFactory.h"
0003 #include "JANA/Components/JWiredFactoryGeneratorT.h"
0004 #include "JANA/JException.h"
0005 #include "JANA/Utils/JEventLevel.h"
0006 #include <JANA/Services/JWiringService.h>
0007 #include <catch.hpp>
0008 #include <memory>
0009 #include <toml.hpp>
0010 #include <iostream>
0011 
0012 
0013 static constexpr std::string_view some_wiring = R"(
0014     include = ["asdf.toml"]
0015 
0016     [[factory]]
0017     plugin_name = "BCAL"
0018     type_name = "MyFac"
0019     prefix = "myfac"
0020     input_names = ["input_coll_1", "input_coll_2"]
0021     input_levels = ["Run", "Subrun"]
0022     output_names = ["output_coll_1", "output_coll_2"]
0023     
0024         [factory.configs]
0025         x = "22"
0026         y = "verbose"
0027 
0028     [[factory]]
0029     plugin_name = "BCAL"
0030     type_name = "MyFac"
0031     prefix = "myfac_modified"
0032     input_names = ["input_coll_1", "input_coll_3"]
0033     output_names = ["output_coll_1_modified", "output_coll_2_modified"]
0034 
0035         [factory.configs]
0036         x = "100"
0037         y = "verbose"
0038 
0039 )";
0040 
0041 TEST_CASE("WiringTests") {
0042 
0043     jana::services::JWiringService sut;
0044     toml::table table = toml::parse(some_wiring);
0045     sut.AddWirings(table, "testcase");
0046 
0047     const auto& wirings = sut.GetWirings();
0048     REQUIRE(wirings.size() == 2);
0049     REQUIRE(wirings[0]->prefix == "myfac");
0050     REQUIRE(wirings[1]->prefix == "myfac_modified");
0051     REQUIRE(wirings[0]->plugin_name == "BCAL");
0052     REQUIRE(wirings[0]->input_names[1] == "input_coll_2");
0053     REQUIRE(wirings[1]->input_names[1] == "input_coll_3");
0054     REQUIRE(wirings[0]->input_levels.size() == 2);
0055     REQUIRE(wirings[0]->input_levels[0] == JEventLevel::Run);
0056     REQUIRE(wirings[0]->input_levels[1] == JEventLevel::Subrun);
0057     REQUIRE(wirings[1]->input_levels.size() == 0);
0058     REQUIRE(wirings[0]->configs.size() == 2);
0059     REQUIRE(wirings[0]->configs["x"] == "22");
0060     REQUIRE(wirings[0]->configs["y"] == "verbose");
0061 
0062 }
0063 
0064 static constexpr std::string_view duplicate_prefixes = R"(
0065     [[factory]]
0066     plugin_name = "BCAL"
0067     type_name = "MyFac"
0068     prefix = "myfac"
0069 
0070     [[factory]]
0071     plugin_name = "BCAL"
0072     type_name = "MyFac"
0073     prefix = "myfac"
0074 )";
0075 
0076 TEST_CASE("WiringTests_DuplicatePrefixes") {
0077 
0078     jana::services::JWiringService sut;
0079     toml::table table = toml::parse(duplicate_prefixes);
0080     try {
0081         sut.AddWirings(table,"testcase");
0082         REQUIRE(1 == 0);
0083     }
0084     catch (const JException& e) {
0085         std::cout << e << std::endl;
0086     }
0087 }
0088 
0089 TEST_CASE("WiringTests_Overlay") {
0090     using Wiring = jana::services::JWiringService::Wiring;
0091     auto above = std::make_unique<Wiring>();
0092     above->prefix = "myfac";
0093     above->type_name = "ClusteringFac";
0094     above->plugin_name = "FCAL";
0095     above->input_names = {"this", "should", "make", "it"};
0096     above->configs["x"] = "6.18";
0097 
0098     auto below = std::make_unique<Wiring>();
0099     below->prefix = "myfac";
0100     below->type_name = "ClusteringFac";
0101     below->plugin_name = "FCAL";
0102     below->input_names = {"this", "should", "NOT", "make", "it"};
0103     below->input_levels = {JEventLevel::Run, JEventLevel::PhysicsEvent};
0104     below->configs["x"] = "7.6";
0105     below->configs["y"] = "42";
0106 
0107     auto sut = jana::services::JWiringService();
0108     sut.Overlay(*above, *below);
0109     REQUIRE(above->input_names[2] == "make");
0110     REQUIRE(above->input_levels[1] == JEventLevel::PhysicsEvent);
0111     REQUIRE(above->configs["x"] == "6.18");
0112     REQUIRE(above->configs["y"] == "42");
0113 }
0114 
0115 static constexpr std::string_view fake_wiring_file = R"(
0116     [[factory]]
0117     plugin_name = "ECAL"
0118     type_name = "ClusteringFac"
0119     prefix = "myfac"
0120 
0121         [factory.configs]
0122         x = "22"
0123         y = "verbose"
0124 
0125     [[factory]]
0126     plugin_name = "ECAL"
0127     type_name = "ClusteringFac"
0128     prefix = "variantfac"
0129 
0130         [factory.configs]
0131         x = "49"
0132         y = "silent"
0133 
0134     [[factory]]
0135     plugin_name = "BCAL"
0136     type_name = "ClusteringFac"
0137     prefix = "sillyfac"
0138 
0139         [factory.configs]
0140         x = "618"
0141         y = "mediocre"
0142 )";
0143 
0144 
0145 TEST_CASE("WiringTests_FakeFacGen") {
0146     jana::services::JWiringService sut;
0147     toml::table table = toml::parse(fake_wiring_file);
0148     sut.AddWirings(table, "testcase");
0149 
0150     using Wiring = jana::services::JWiringService::Wiring;
0151     std::vector<std::unique_ptr<Wiring>> fake_facgen_wirings;
0152     
0153     // One gets overlaid with an existing wiring
0154     auto a = std::make_unique<Wiring>();
0155     a->plugin_name = "ECAL";
0156     a->type_name = "ClusteringFac";
0157     a->prefix = "variantfac";
0158     a->configs["x"] = "42";
0159     fake_facgen_wirings.push_back(std::move(a));
0160     
0161     // The other is brand new
0162     auto b = std::make_unique<Wiring>();
0163     b->plugin_name = "ECAL";
0164     b->type_name = "ClusteringFac";
0165     b->prefix = "exuberantfac";
0166     b->configs["x"] = "27";
0167     fake_facgen_wirings.push_back(std::move(b));
0168 
0169     // We should end up with three in total
0170     sut.AddWirings(fake_facgen_wirings, "fake_facgen");
0171     auto final_wirings = sut.GetWirings("ECAL", "ClusteringFac");
0172 
0173     REQUIRE(final_wirings.size() == 3);
0174 
0175     REQUIRE(final_wirings[0]->prefix == "myfac");
0176     REQUIRE(final_wirings[1]->prefix == "variantfac");
0177     REQUIRE(final_wirings[2]->prefix == "exuberantfac");
0178 
0179     REQUIRE(final_wirings[0]->configs["x"] == "22"); // from file only
0180     REQUIRE(final_wirings[1]->configs["x"] == "49"); // file overrides facgen
0181     REQUIRE(final_wirings[2]->configs["x"] == "27"); // from facgen only
0182 
0183 }
0184 
0185 struct Cluster { double x,y,E; };
0186 
0187 struct WiredOmniFac : jana::components::JOmniFactory<WiredOmniFac> {
0188     Input<Cluster> m_protoclusters_in {this};
0189     Output<Cluster> m_clusters_out {this};
0190 
0191     Parameter<int> m_x {this, "x", 1, "x"};
0192     Parameter<std::string> m_y {this, "y", "silent", "y" };
0193 
0194     void Configure() {
0195     }
0196 
0197     void ChangeRun(int32_t /*run_nr*/) {
0198     }
0199 
0200     void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) {
0201 
0202         for (auto protocluster : *m_protoclusters_in) {
0203             m_clusters_out().push_back(new Cluster {protocluster->x, protocluster->y, protocluster->E + 1});
0204         }
0205     }
0206 };
0207 
0208 static constexpr std::string_view realfacgen_wiring = R"(
0209     [[factory]]
0210     type_name = "WiredOmniFac"
0211     prefix = "myfac"
0212     input_names = ["usual_input"]
0213     output_names = ["usual_output"]
0214 
0215         [factory.configs]
0216         x = "22"
0217         y = "verbose"
0218 
0219     [[factory]]
0220     type_name = "WiredOmniFac"
0221     prefix = "myfac_modified"
0222     input_names = ["different_input"]
0223     output_names = ["different_output"]
0224 
0225         [factory.configs]
0226         x = "100"
0227         y = "silent"
0228 
0229 )";
0230 
0231 TEST_CASE("WiringTests_RealFacGen") {
0232 
0233     JApplication app;
0234 
0235     auto wiring_svc = app.GetService<jana::services::JWiringService>();
0236     toml::table table = toml::parse(realfacgen_wiring);
0237     wiring_svc->AddWirings(table, "testcase");
0238 
0239     auto gen = new jana::components::JWiredFactoryGeneratorT<WiredOmniFac>;
0240     app.Add(gen);
0241     app.Initialize();
0242 
0243     auto& summary = app.GetComponentSummary();
0244     jout << summary;
0245     auto vf = summary.FindComponents("myfac");
0246     REQUIRE(vf.size() == 1);
0247     REQUIRE(vf.at(0)->GetOutputs().at(0)->GetName() == "usual_output");
0248 
0249     auto ef = summary.FindComponents("myfac_modified");
0250     REQUIRE(ef.size() == 1);
0251     REQUIRE(ef.at(0)->GetOutputs().at(0)->GetName() == "different_output");
0252 
0253     // Check that parameter values propagated from wiring file to parameter manager
0254     REQUIRE(app.GetParameterValue<int>("myfac:x") == 22);
0255     REQUIRE(app.GetParameterValue<std::string>("myfac:y") == "verbose");
0256     REQUIRE(app.GetParameterValue<int>("myfac_modified:x") == 100);
0257     REQUIRE(app.GetParameterValue<std::string>("myfac_modified:y") == "silent");
0258 
0259     //app.GetJParameterManager()->PrintParameters(2, 0);
0260     //auto event = std::make_shared<JEvent>(&app);
0261     //auto facs = event->GetFactorySet()->GetAllMultifactories();
0262 
0263 }
0264 
0265