File indexing completed on 2025-01-18 09:14:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_DDG4_PROPERTYTESTACTION_H
0014 #define DD4HEP_DDG4_PROPERTYTESTACTION_H
0015
0016
0017 #include <DDG4/Geant4Action.h>
0018 #include <DD4hep/ComponentProperties.h>
0019
0020
0021 #include <Math/Point3D.h>
0022 #include <Math/Vector4D.h>
0023 #include <Math/Vector3D.h>
0024
0025
0026 #include <map>
0027 #include <set>
0028 #include <list>
0029 #include <vector>
0030
0031
0032 namespace DD4hepTests {
0033
0034 using namespace std;
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 class PropertyTestAction : public dd4hep::sim::Geant4Action {
0045 public:
0046 using XYZPoint = ROOT::Math::XYZPoint;
0047 using XYZVector = ROOT::Math::XYZVector;
0048 using PxPyPzE = ROOT::Math::PxPyPzEVector;
0049 typedef unsigned long ulong;
0050
0051 string prop_str;
0052 bool prop_bool;
0053 int prop_int;
0054 long prop_long;
0055 unsigned long prop_ulong;
0056 float prop_float;
0057 double prop_double;
0058 XYZPoint prop_XYZPoint;
0059 XYZVector prop_XYZVector;
0060 PxPyPzE prop_PxPyPzEVector;
0061
0062 map<string, string> map_str_str;
0063 map<string, bool> map_str_bool;
0064 map<string, int> map_str_int;
0065 map<string, long> map_str_long;
0066
0067 map<string, float> map_str_float;
0068 map<string, double> map_str_double;
0069
0070 map<int, string> map_int_str;
0071 map<int, bool> map_int_bool;
0072 map<int, int> map_int_int;
0073 map<int, long> map_int_long;
0074
0075 map<int, float> map_int_float;
0076 map<int, double> map_int_double;
0077
0078 set<string> set_str;
0079 set<bool> set_bool;
0080 set<int> set_int;
0081 set<long> set_long;
0082
0083 set<float> set_float;
0084 set<double> set_double;
0085 set<XYZPoint> set_XYZPoint;
0086 set<XYZVector> set_XYZVector;
0087 set<PxPyPzE> set_PxPyPzEVector;
0088
0089 vector<string> vector_str;
0090 vector<bool> vector_bool;
0091 vector<int> vector_int;
0092 vector<long> vector_long;
0093 vector<ulong> vector_ulong;
0094 vector<float> vector_float;
0095 vector<double> vector_double;
0096 vector<XYZPoint> vector_XYZPoint;
0097 vector<XYZVector> vector_XYZVector;
0098 vector<PxPyPzE> vector_PxPyPzEVector;
0099
0100 list<string> list_str;
0101 list<bool> list_bool;
0102 list<int> list_int;
0103 list<long> list_long;
0104 list<ulong> list_ulong;
0105 list<float> list_float;
0106 list<double> list_double;
0107 list<XYZPoint> list_XYZPoint;
0108 list<XYZVector> list_XYZVector;
0109 list<PxPyPzE> list_PxPyPzEVector;
0110
0111 public:
0112
0113 PropertyTestAction(dd4hep::sim::Geant4Context* context, const std::string& nam);
0114
0115 virtual ~PropertyTestAction() = default;
0116
0117 void dumpProperties() const;
0118
0119 virtual void installCommandMessenger() override;
0120 };
0121 }
0122 #endif
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 #include <DDG4/Geant4UIMessenger.h>
0139
0140 #include <iostream>
0141 #include <sstream>
0142
0143 using namespace std;
0144
0145 namespace {
0146 string str_rep(string in, const string& pat, const string& rep) {
0147 size_t idx = string::npos;
0148 string res = in;
0149 while( (idx=res.find(pat)) != string::npos )
0150 res.replace(idx, pat.length(), rep);
0151 return res;
0152 }
0153 void _print(stringstream& log) {
0154 auto res = str_rep(log.str(), "( ", "(");
0155 cout << res << endl;
0156 }
0157
0158 template <typename T> int dump_item(const string& tag, const T& value) {
0159 try {
0160 stringstream log;
0161 log << "| " << setw(32) << left << tag << " " << setw(6) << left << value;
0162 _print(log);
0163 }
0164 catch(const exception& e) {
0165 cout << "Test FAILED: " << tag << " Exception: " << e.what() << endl;
0166 return 1;
0167 }
0168 return 0;
0169 }
0170 template <typename T> int dump_cont(const string& tag, const T& value) {
0171 try {
0172 stringstream log;
0173 log << "| " << setw(32) << left << tag << " ";
0174 for(const auto& val : value)
0175 log << setw(6) << left << val << " ";
0176 _print(log);
0177 }
0178 catch(const exception& e) {
0179 cout << "Test FAILED: " << tag << " Exception: " << e.what() << endl;
0180 return 1;
0181 }
0182 return 0;
0183 }
0184 template <typename T> int dump_map(const string& tag, const T& value) {
0185 try {
0186 stringstream log;
0187 log << "| " << setw(32) << left << tag << " ";
0188 for(const auto& p : value)
0189 log << setw(6) << left << p.first << " = " << setw(10) << left << p.second << " ";
0190 _print(log);
0191 }
0192 catch(const exception& e) {
0193 cout << "Test FAILED: " << tag << " Exception: " << e.what() << endl;
0194 return 1;
0195 }
0196 return 0;
0197 }
0198 }
0199
0200
0201 DD4hepTests::PropertyTestAction::PropertyTestAction(dd4hep::sim::Geant4Context* ctxt, const string& nam)
0202 : dd4hep::sim::Geant4Action(ctxt, nam)
0203 {
0204 declareProperty("prop_str", prop_str);
0205 declareProperty("prop_bool", prop_bool);
0206 declareProperty("prop_int", prop_int);
0207 declareProperty("prop_long", prop_long);
0208 declareProperty("prop_ulong", prop_ulong);
0209 declareProperty("prop_float", prop_float);
0210 declareProperty("prop_double", prop_double);
0211 declareProperty("prop_XYZPoint", prop_XYZPoint);
0212 declareProperty("prop_XYZVector", prop_XYZVector);
0213 declareProperty("prop_PxPyPzEVector", prop_PxPyPzEVector);
0214
0215 declareProperty("map_str_str", map_str_str);
0216 declareProperty("map_str_bool", map_str_bool);
0217 declareProperty("map_str_int", map_str_int);
0218 declareProperty("map_str_long", map_str_long);
0219
0220 declareProperty("map_str_float", map_str_float);
0221 declareProperty("map_str_double", map_str_double);
0222
0223 declareProperty("map_int_str", map_int_str);
0224 declareProperty("map_int_bool", map_int_bool);
0225 declareProperty("map_int_int", map_int_int);
0226 declareProperty("map_int_long", map_int_long);
0227
0228 declareProperty("map_int_float", map_int_float);
0229 declareProperty("map_int_double", map_int_double);
0230
0231 declareProperty("set_str", set_str);
0232 declareProperty("set_bool", set_bool);
0233 declareProperty("set_int", set_int);
0234 declareProperty("set_long", set_long);
0235
0236 declareProperty("set_float", set_float);
0237 declareProperty("set_double", set_double);
0238 declareProperty("set_XYZPoint", set_XYZPoint);
0239 declareProperty("set_XYZVector", set_XYZVector);
0240 declareProperty("set_PxPyPzEVector", set_PxPyPzEVector);
0241
0242 declareProperty("vector_str", vector_str);
0243 declareProperty("vector_bool", vector_bool);
0244 declareProperty("vector_int", vector_int);
0245 declareProperty("vector_long", vector_long);
0246 declareProperty("vector_ulong", vector_ulong);
0247 declareProperty("vector_float", vector_float);
0248 declareProperty("vector_double", vector_double);
0249 declareProperty("vector_XYZPoint", vector_XYZPoint);
0250 declareProperty("vector_XYZVector", vector_XYZVector);
0251 declareProperty("vector_PxPyPzEVector", vector_PxPyPzEVector);
0252
0253 declareProperty("list_str", list_str);
0254 declareProperty("list_bool", list_bool);
0255 declareProperty("list_int", list_int);
0256 declareProperty("list_long", list_long);
0257 declareProperty("list_ulong", list_ulong);
0258 declareProperty("list_float", list_float);
0259 declareProperty("list_double", list_double);
0260 declareProperty("list_XYZPoint", list_XYZPoint);
0261 declareProperty("list_XYZVector", list_XYZVector);
0262 declareProperty("list_PxPyPzEVector", list_PxPyPzEVector);
0263 }
0264
0265
0266 void DD4hepTests::PropertyTestAction::installCommandMessenger() {
0267 this->Geant4Action::installCommandMessenger();
0268 m_control->addCall("dumpProperties", "Execute property check",
0269 dd4hep::Callback(this).make(&PropertyTestAction::dumpProperties),0);
0270 }
0271
0272
0273 void DD4hepTests::PropertyTestAction::dumpProperties() const {
0274 int result = 0;
0275 const char* line = "+----------------------------------------------------------------------------------------------------------";
0276 cout << line << endl;
0277 result += dump_item("prop_str", prop_str);
0278 result += dump_item("prop_bool", prop_bool);
0279 result += dump_item("prop_int", prop_int);
0280 result += dump_item("prop_long", prop_long);
0281 result += dump_item("prop_ulong", prop_ulong);
0282 result += dump_item("prop_float", prop_float);
0283 result += dump_item("prop_double", prop_double);
0284 result += dump_item("prop_XYZPoint", prop_XYZPoint);
0285 result += dump_item("prop_XYZVector", prop_XYZVector);
0286 result += dump_item("prop_PxPyPzEVector", prop_PxPyPzEVector);
0287 cout << line << endl;
0288 result += dump_map("map_str_str", map_str_str);
0289 result += dump_map("map_str_bool", map_str_bool);
0290 result += dump_map("map_str_int", map_str_int);
0291 result += dump_map("map_str_long", map_str_long);
0292
0293 result += dump_map("map_str_float", map_str_float);
0294 result += dump_map("map_str_double", map_str_double);
0295 cout << line << endl;
0296 result += dump_map("map_int_str", map_int_str);
0297 result += dump_map("map_int_bool", map_int_bool);
0298 result += dump_map("map_int_int", map_int_int);
0299 result += dump_map("map_int_long", map_int_long);
0300
0301 result += dump_map("map_int_float", map_int_float);
0302 result += dump_map("map_int_double", map_int_double);
0303 cout << line << endl;
0304 result += dump_cont("set_str", set_str);
0305 result += dump_cont("set_bool", set_bool);
0306 result += dump_cont("set_int", set_int);
0307 result += dump_cont("set_long", set_long);
0308
0309 result += dump_cont("set_float", set_float);
0310 result += dump_cont("set_double", set_double);
0311 result += dump_cont("set_XYZPoint", set_XYZPoint);
0312 result += dump_cont("set_XYZVector", set_XYZVector);
0313 result += dump_cont("set_PxPyPzEVector", set_PxPyPzEVector);
0314 cout << line << endl;
0315 result += dump_cont("vector_str", vector_str);
0316 result += dump_cont("vector_bool", vector_bool);
0317 result += dump_cont("vector_int", vector_int);
0318 result += dump_cont("vector_long", vector_long);
0319 result += dump_cont("vector_ulong", vector_ulong);
0320 result += dump_cont("vector_float", vector_float);
0321 result += dump_cont("vector_double", vector_double);
0322 result += dump_cont("vector_XYZPoint", vector_XYZPoint);
0323 result += dump_cont("vector_XYZVector", vector_XYZVector);
0324 result += dump_cont("vector_PxPyPzEVector", vector_PxPyPzEVector);
0325 cout << line << endl;
0326 result += dump_cont("list_str", list_str);
0327 result += dump_cont("list_bool", list_bool);
0328 result += dump_cont("list_int", list_int);
0329 result += dump_cont("list_long", list_long);
0330 result += dump_cont("list_ulong", list_ulong);
0331 result += dump_cont("list_float", list_float);
0332 result += dump_cont("list_double", list_double);
0333 result += dump_cont("list_XYZPoint", list_XYZPoint);
0334 result += dump_cont("list_XYZVector", list_XYZVector);
0335 result += dump_cont("list_PxPyPzEVector", list_PxPyPzEVector);
0336 cout << line << endl;
0337
0338 if ( 0 == result )
0339 cout << endl << "Test PASSED" << endl << endl;
0340 else
0341 cout << endl << "Test FAILED" << endl << "===> " << result << " Subtests FAILED" << endl;
0342 cout << line << endl;
0343 }
0344
0345 #include "DDG4/Factories.h"
0346 DECLARE_GEANT4ACTION_NS(DD4hepTests,PropertyTestAction)