File indexing completed on 2025-03-13 08:19:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <Parsers/Parsers.h>
0016 #include <DD4hep/Printout.h>
0017 #include <DD4hep/ConditionsData.h>
0018 #include <DD4hep/ConditionsPrinter.h>
0019 #include <DD4hep/ConditionsProcessor.h>
0020
0021 #include <DD4hep/detail/ConditionsInterna.h>
0022
0023
0024 #include <sstream>
0025
0026 using namespace dd4hep;
0027 using namespace dd4hep::cond;
0028
0029 namespace {
0030
0031 std::string str_replace(const std::string& str, const std::string& pattern, const std::string& replacement) {
0032 std::string res = str;
0033 for(size_t id=res.find(pattern); id != std::string::npos; id = res.find(pattern) )
0034 res.replace(id,pattern.length(),replacement);
0035 return res;
0036 }
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 class ConditionsPrinter::ParamPrinter {
0048 protected:
0049
0050 ConditionsPrinter* m_parent = 0;
0051 public:
0052
0053 std::string prefix;
0054
0055 PrintLevel printLevel = INFO;
0056 public:
0057
0058 ParamPrinter(const ParamPrinter& copy) = default;
0059
0060 ParamPrinter(ConditionsPrinter* p, PrintLevel lvl);
0061
0062 virtual ~ParamPrinter() = default;
0063
0064 ParamPrinter& operator=(const ParamPrinter& copy) = default;
0065
0066 virtual void operator()(const cond::AbstractMap::Params::value_type& obj) const;
0067 };
0068
0069
0070
0071 ConditionsPrinter::ParamPrinter::ParamPrinter(ConditionsPrinter* printer, PrintLevel lvl)
0072 : m_parent(printer), printLevel(lvl)
0073 {
0074 }
0075
0076
0077 void ConditionsPrinter::ParamPrinter::operator()(const AbstractMap::Params::value_type& obj) const {
0078 const std::type_info& type = obj.second.typeInfo();
0079 ++m_parent->numParam;
0080 if ( type == typeid(std::string) ) {
0081 std::string value = obj.second.get<std::string>().c_str();
0082 std::size_t len = value.length();
0083 if ( len > m_parent->lineLength ) {
0084 value.erase(m_parent->lineLength);
0085 value += "...";
0086 }
0087 printout(m_parent->printLevel,m_parent->name,"++ %s\t-> Param: %-16s %-8s -> %s",
0088 prefix.c_str(),
0089 obj.first.c_str(),
0090 obj.second.dataType().c_str(),
0091 value.c_str());
0092 }
0093 else if ( type == typeid(AbstractMap) ) {
0094 const AbstractMap& d= obj.second.get<AbstractMap>();
0095 printout(m_parent->printLevel,m_parent->name,"++ %s\t-> [%s] CL:%d %-8s -> %s",
0096 prefix.c_str(),
0097 obj.first.c_str(), d.classID,
0098 obj.second.dataType().c_str(),
0099 obj.second.str().c_str());
0100 }
0101 else {
0102 std::string value = obj.second.str();
0103 std::size_t len = value.length();
0104 if ( len > m_parent->lineLength ) {
0105 value.erase(m_parent->lineLength);
0106 value += "...";
0107 }
0108 printout(m_parent->printLevel,m_parent->name,"++ %s\t-> [%s] %-8s -> %s",
0109 prefix.c_str(),
0110 obj.first.c_str(),
0111 obj.second.dataType().c_str(),
0112 value.c_str());
0113 }
0114 }
0115
0116
0117 ConditionsPrinter::ConditionsPrinter(ConditionsMap* cond_map, const std::string& pref, int flg)
0118 : mapping(cond_map), m_flag(flg), name("Condition"), prefix(pref)
0119 {
0120 m_print = new ParamPrinter(this, printLevel);
0121 }
0122
0123
0124 ConditionsPrinter::~ConditionsPrinter() {
0125 if ( summary ) {
0126 printout(INFO,name,"++ %s +++++++++++++ Printout summary:", prefix.c_str());
0127 printout(INFO,name,"++ %s Number of conditions: %8ld [ dto. empty:%ld]",
0128 prefix.c_str(), numCondition, numEmptyCondition);
0129 printout(INFO,name,"++ %s Total Number of parameters: %8ld [%7.3f Parameters/Condition]",
0130 prefix.c_str(), numParam, double(numParam)/std::max(double(numCondition),1e0));
0131 }
0132 detail::deletePtr(m_print);
0133 }
0134
0135
0136 int ConditionsPrinter::operator()(Condition cond) const {
0137 m_print->printLevel = printLevel;
0138 if ( cond.isValid() ) {
0139 std::string repr = cond.str(m_flag);
0140 Condition::Object* ptr = cond.ptr();
0141
0142 if ( repr.length() > lineLength )
0143 repr = repr.substr(0,lineLength)+"...";
0144 printout(this->printLevel,name, "++ %s%s", prefix.c_str(), repr.c_str());
0145 std::string new_prefix = prefix;
0146 new_prefix.assign(prefix.length(),' ');
0147 if ( !cond.is_bound() ) {
0148 printout(this->printLevel,name,"++ %s \tPath:%s Key:%16llX Type:%s (%s)",
0149 new_prefix.c_str(), cond.name(), cond.key(), "<Unbound-Condition>",
0150 typeName(typeid(*ptr)).c_str());
0151 return 1;
0152 }
0153 const std::type_info& type = cond.typeInfo();
0154 const OpaqueData& opaque = cond.data();
0155 printout(this->printLevel,name,"++ %s \tPath:%s Key:%16llX Type:%s",
0156 new_prefix.c_str(), cond.name(), cond.key(), opaque.dataType().c_str());
0157
0158
0159
0160 if ( type == typeid(AbstractMap) ) {
0161 const AbstractMap& data = cond.get<AbstractMap>();
0162 printout(printLevel,name,"++ %s Path:%s Class:%d [%s]",
0163 prefix.c_str(),
0164 cond.name(),
0165 data.classID,
0166 cond.data().dataType().c_str());
0167 ++numCondition;
0168 if ( !data.params.empty() ) {
0169 m_print->printLevel = printLevel;
0170 m_print->prefix.assign(prefix.length(),' ');
0171 for_each(data.params.begin(), data.params.end(),*m_print);
0172 }
0173 else {
0174 ++numEmptyCondition;
0175 }
0176 }
0177 else if ( type == typeid(Delta) ) {
0178 std::string piv;
0179 std::stringstream str_tr, str_rot, str_piv;
0180 const Delta& D = cond.get<Delta>();
0181 if ( D.hasTranslation() ) {
0182 Position copy(D.translation * (1./dd4hep::cm));
0183 Parsers::toStream(copy, str_tr);
0184 }
0185 if ( D.hasRotation() ) {
0186 Parsers::toStream(D.rotation, str_rot);
0187 }
0188 if ( D.hasPivot() ) {
0189 Position copy(D.pivot.Vect() * (1./dd4hep::cm));
0190 Parsers::toStream(copy, str_piv);
0191 piv = str_replace(str_piv.str(),"\n","");
0192 piv = str_replace(piv," "," , ");
0193 }
0194 printout(printLevel,name,"++ %s \t[%p] Typ:%s",
0195 prefix.c_str(), cond.ptr(),
0196 typeName(typeid(*ptr)).c_str());
0197 printout(printLevel,name,"++ %s \tData(%11s-%8s-%5s): [%s [cm], %s [rad], %s [cm]]",
0198 prefix.c_str(),
0199 D.hasTranslation() ? "Translation" : "",
0200 D.hasRotation() ? "Rotation(Phi,Theta,Psi)" : "",
0201 D.hasPivot() ? "Pivot" : "",
0202 str_replace(str_tr.str(),"\n","").c_str(),
0203 str_replace(str_rot.str(),"\n","").c_str(),
0204 piv.c_str()
0205 );
0206 }
0207 else if ( type == typeid(AlignmentData) ) {
0208 std::string piv;
0209 std::stringstream str_tr, str_rot, str_piv;
0210 const Delta& D = cond.get<AlignmentData>().delta;
0211 if ( D.hasTranslation() ) Parsers::toStream(D.translation, str_tr);
0212 if ( D.hasRotation() ) Parsers::toStream(D.rotation, str_rot);
0213 if ( D.hasPivot() ) {
0214 Parsers::toStream(D.pivot, str_piv);
0215 piv = str_replace(str_piv.str(),"\n","");
0216 piv = str_replace(piv," "," , ");
0217 }
0218
0219 printout(printLevel,name,"++ %s \t[%p] Typ:%s",
0220 prefix.c_str(), cond.ptr(),
0221 typeName(typeid(*ptr)).c_str());
0222 printout(printLevel,name,"++ %s \tData(%11s-%8s-%5s): [%s, %s, %s]",
0223 prefix.c_str(),
0224 D.hasTranslation() ? "Translation" : "",
0225 D.hasRotation() ? "Rotation(Phi,Theta,Psi)" : "",
0226 D.hasPivot() ? "Pivot" : "",
0227 str_replace(str_tr.str(),"\n","").c_str(),
0228 str_replace(str_rot.str(),"\n","").c_str(),
0229 piv.c_str()
0230 );
0231 }
0232 else if ( type == typeid(std::string) ) {
0233 std::string value = cond.get<std::string>().c_str();
0234 std::size_t len = value.length();
0235 if ( len > lineLength ) {
0236 value = value.substr(0,lineLength);
0237 value += "...";
0238 value = str_replace(value,"\n","");
0239 }
0240 printout(printLevel,name,"++ %s \tString [%s]: %s",
0241 prefix.c_str(),
0242 cond.data().dataType().c_str(),
0243 value.c_str());
0244 }
0245 else {
0246 std::string value = cond.str();
0247 std::size_t len = value.length();
0248 if ( len > lineLength ) {
0249 value = value.substr(0,lineLength);
0250 value += "...";
0251 value = str_replace(value,"\n","");
0252 }
0253 printout(printLevel,name,"++ %s \t[%s]: %s",
0254 prefix.c_str(),
0255 cond.data().dataType().c_str(),
0256 value.c_str());
0257 }
0258 return 1;
0259 }
0260 return 0;
0261 }
0262
0263
0264 int ConditionsPrinter::operator()(DetElement de, int level) const {
0265 if ( mapping ) {
0266 std::vector<Condition> conditions;
0267 conditionsCollector(*mapping,conditions)(de,level);
0268 printout(this->printLevel, name, "++ %s %-3ld Conditions for DE %s",
0269 prefix.c_str(), conditions.size(), de.path().c_str());
0270 for( auto cond : conditions )
0271 (*this)(cond);
0272 return int(conditions.size());
0273 }
0274 except(name,"Failed to dump conditions for DetElement:%s [No slice availible]",
0275 de.path().c_str());
0276 return 0;
0277 }