File indexing completed on 2025-01-30 09:16:55
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/AlignmentsPrinter.h>
0018 #include <DD4hep/AlignmentsProcessor.h>
0019 #include <DD4hep/detail/AlignmentsInterna.h>
0020 #include <TClass.h>
0021
0022
0023 #include <sstream>
0024
0025 using namespace dd4hep;
0026 using namespace dd4hep::align;
0027
0028
0029 AlignmentsPrinter::AlignmentsPrinter(ConditionsMap* cond_map, const std::string& pref, int flg)
0030 : mapping(cond_map), name("Alignment"), prefix(pref), printLevel(INFO), m_flag(flg)
0031 {
0032 }
0033
0034
0035 int AlignmentsPrinter::operator()(Alignment a) const {
0036 printAlignment(printLevel, name, a);
0037 return 1;
0038 }
0039
0040
0041 int AlignmentsPrinter::operator()(DetElement de, int level) const {
0042 if ( mapping ) {
0043 std::vector<Alignment> alignments;
0044 alignmentsCollector(*mapping,alignments)(de, level);
0045 printout(printLevel, name, "++ %s %-3ld Alignments for DE %s",
0046 prefix.c_str(), alignments.size(), de.path().c_str());
0047 for( auto alignment : alignments )
0048 (*this)(alignment);
0049 return int(alignments.size());
0050 }
0051 except(name,"Failed to dump conditions for DetElement:%s [No slice availible]",
0052 de.path().c_str());
0053 return 0;
0054 }
0055
0056
0057 AlignedVolumePrinter::AlignedVolumePrinter(ConditionsMap* cond_map, const std::string& pref,int flg)
0058 : AlignmentsPrinter(cond_map, pref, flg)
0059 {
0060 name = "Alignment";
0061 }
0062
0063
0064 int AlignedVolumePrinter::operator()(Alignment a) const {
0065 printAlignment(printLevel, name, a);
0066 return 1;
0067 }
0068
0069
0070 void dd4hep::align::printAlignment(PrintLevel lvl, const std::string& prefix, Alignment a) {
0071 if ( a.isValid() ) {
0072 Alignment::Object* ptr = a.ptr();
0073 const AlignmentData& data = a.data();
0074 const Delta& D = data.delta;
0075 std::string new_prefix = prefix;
0076 new_prefix.assign(prefix.length(),' ');
0077 printout(lvl,prefix,"++ %s \t [%p] Typ:%s",
0078 new_prefix.c_str(), a.ptr(),
0079 typeName(typeid(*ptr)).c_str());
0080 printout(lvl,prefix,"++ %s \tData:(%11s-%8s-%5s)",
0081 new_prefix.c_str(),
0082 D.hasTranslation() ? "Translation" : "",
0083 D.hasRotation() ? "Rotation" : "",
0084 D.hasPivot() ? "Pivot" : "");
0085 if ( isActivePrintLevel(lvl) ) {
0086 printf("WorldTrafo: "); data.worldTrafo.Print();
0087 printf("DetTrafo: "); data.detectorTrafo.Print();
0088 }
0089 }
0090 }
0091
0092 static std::string replace_all(const std::string& in, const std::string& from, const std::string& to) {
0093 std::string res = in;
0094 std::size_t idx;
0095 while( std::string::npos != (idx=res.find(from)) )
0096 res.replace(idx,from.length(),to);
0097 return res;
0098 }
0099 static std::string _transformPoint2World(const AlignmentData& data, const Position& local) {
0100 char text[256];
0101 Position world = data.localToWorld(local);
0102 ::snprintf(text,sizeof(text),"Local: (%7.3f , %7.3f , %7.3f ) -- > World: (%7.3f , %7.3f , %7.3f )",
0103 local.x(), local.y(), local.z(), world.x(), world.y(), world.z());
0104 return text;
0105 }
0106
0107 static std::string _transformPoint2Detector(const AlignmentData& data, const Position& local) {
0108 char text[256];
0109 Position world = data.localToDetector(local);
0110 ::snprintf(text,sizeof(text),"Local: (%7.3f , %7.3f , %7.3f ) -- > Parent: (%7.3f , %7.3f , %7.3f )",
0111 local.x(), local.y(), local.z(), world.x(), world.y(), world.z());
0112 return text;
0113 }
0114
0115 void dd4hep::align::printAlignment(PrintLevel lvl, const std::string& prefix,
0116 const std::string& opt, DetElement de, Alignment alignment)
0117 {
0118 const std::string& tag = prefix;
0119 const AlignmentData& align_data = alignment.data();
0120 Condition align_cond;
0121 const Delta& align_delta = align_data.delta;
0122 std::string par = de.parent().isValid() ? de.parent().path() : std::string();
0123 Box bbox = de.placement().volume().solid();
0124
0125 Position p1( bbox.x(), bbox.y(), bbox.z());
0126 Position p2( bbox.x(),-bbox.y(), bbox.z());
0127 Position p3(-bbox.x(), bbox.y(), bbox.z());
0128 Position p4(-bbox.x(),-bbox.y(), bbox.z());
0129 Position p5( bbox.x(), bbox.y(),-bbox.z());
0130 Position p6( bbox.x(),-bbox.y(),-bbox.z());
0131 Position p7(-bbox.x(), bbox.y(),-bbox.z());
0132 Position p8(-bbox.x(),-bbox.y(),-bbox.z());
0133
0134 if ( align_cond.isValid() ) {
0135 printout(lvl,tag,"++ %s DATA: (%11s-%8s-%5s) %p IOV:%s", opt.c_str(),
0136 align_delta.hasTranslation() ? "Translation" : "",
0137 align_delta.hasRotation() ? "Rotation" : "",
0138 align_delta.hasPivot() ? "Pivot" : "",
0139 alignment.ptr(),
0140 align_cond.iov().str().c_str());
0141 }
0142 else {
0143 printout(lvl,tag,"++ %s DATA: (%11s-%8s-%5s) %p", opt.c_str(),
0144 align_delta.hasTranslation() ? "Translation" : "",
0145 align_delta.hasRotation() ? "Rotation" : "",
0146 align_delta.hasPivot() ? "Pivot" : "",
0147 alignment.ptr());
0148 }
0149 if ( align_delta.hasTranslation() ) {
0150 std::stringstream str;
0151 Position copy(align_delta.translation * (1./dd4hep::cm));
0152 Parsers::toStream(copy, str);
0153 printout(lvl,tag,"++ %s DELTA Translation: %s [cm]",
0154 opt.c_str(), replace_all(str.str(),"\n","").c_str());
0155 }
0156 if ( align_delta.hasPivot() ) {
0157 std::stringstream str;
0158 Delta::Pivot copy(align_delta.pivot.Vect() * (1./dd4hep::cm));
0159 Parsers::toStream(copy, str);
0160 std::string res = replace_all(str.str(),"\n","");
0161 res = "( "+replace_all(res," "," , ")+" )";
0162 printout(lvl,tag,"++ %s DELTA Pivot: %s [cm]", opt.c_str(), res.c_str());
0163 }
0164 if ( align_delta.hasRotation() ) {
0165 std::stringstream str;
0166 Parsers::toStream(align_delta.rotation, str);
0167 printout(lvl,tag,"++ %s DELTA Rotation: %s [rad]", opt.c_str(), replace_all(str.str(),"\n","").c_str());
0168 }
0169 if ( isActivePrintLevel(lvl) ) {
0170 printf("%s %s WorldTrafo (to %s): ",opt.c_str(), tag.c_str(), de.world().path().c_str());
0171 align_data.worldTrafo.Print();
0172 printf("%s %s DetTrafo (to %s): ",opt.c_str(), tag.c_str(), par.c_str());
0173 align_data.detectorTrafo.Print();
0174 }
0175 printout(PrintLevel(lvl-1),tag,"++ %s: P1(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p1).c_str());
0176 printout(PrintLevel(lvl-1),tag,"++ %s: P2(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p2).c_str());
0177 printout(PrintLevel(lvl-1),tag,"++ %s: P3(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p3).c_str());
0178 printout(PrintLevel(lvl-1),tag,"++ %s: P4(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p4).c_str());
0179 printout(PrintLevel(lvl-1),tag,"++ %s: P5(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p5).c_str());
0180 printout(PrintLevel(lvl-1),tag,"++ %s: P6(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p6).c_str());
0181 printout(PrintLevel(lvl-1),tag,"++ %s: P7(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p7).c_str());
0182 printout(PrintLevel(lvl-1),tag,"++ %s: P8(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p8).c_str());
0183
0184 printout(PrintLevel(lvl-1),tag,"++ %s: P1(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p1).c_str());
0185 printout(PrintLevel(lvl-1),tag,"++ %s: P2(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p2).c_str());
0186 printout(PrintLevel(lvl-1),tag,"++ %s: P3(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p3).c_str());
0187 printout(PrintLevel(lvl-1),tag,"++ %s: P4(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p4).c_str());
0188 printout(PrintLevel(lvl-1),tag,"++ %s: P5(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p5).c_str());
0189 printout(PrintLevel(lvl-1),tag,"++ %s: P6(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p6).c_str());
0190 printout(PrintLevel(lvl-1),tag,"++ %s: P7(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p7).c_str());
0191 printout(PrintLevel(lvl-1),tag,"++ %s: P8(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p8).c_str());
0192 }
0193
0194
0195 void dd4hep::align::printElement(PrintLevel prt_level, const std::string& prefix, DetElement de, ConditionsMap& pool) {
0196 std::string tag = prefix+"Element";
0197 if ( de.isValid() ) {
0198 std::vector<Alignment> alignments;
0199 alignmentsCollector(pool,alignments)(de);
0200 printout(prt_level,tag,"++ Alignments of DE %s [%d entries]",
0201 de.path().c_str(), int(alignments.size()));
0202 for(const auto& align : alignments )
0203 printAlignment(prt_level, prefix, align);
0204 return;
0205 }
0206 except(tag,"Cannot process alignments of an invalid detector element");
0207 }
0208
0209
0210 void dd4hep::align::printElementPlacement(PrintLevel lvl, const std::string& prefix, DetElement de, ConditionsMap& pool) {
0211 std::string tag = prefix+"Element";
0212 if ( de.isValid() ) {
0213 char text[132];
0214 Alignment nominal = de.nominal();
0215 Box bbox = de.placement().volume().solid();
0216 std::vector<Alignment> alignments;
0217
0218 alignmentsCollector(pool,alignments)(de);
0219 ::memset(text,'=',sizeof(text));
0220 text[sizeof(text)-1] = 0;
0221 printout(lvl, tag, text);
0222 printout(lvl, tag, "++ Alignments of DE %s [%d entries]",
0223 de.path().c_str(), int(alignments.size()));
0224 printout(lvl, tag, "++ Volume: %s BBox: x=%7.3f y=%7.3f z=%7.3f",
0225 bbox.type(), bbox.x(), bbox.y(), bbox.z());
0226 printAlignment(lvl, tag, "NOMINAL", de, nominal);
0227
0228 for(const auto& align : alignments ) {
0229 AlignmentCondition cond(align);
0230 try {
0231 printout(lvl, tag, "++ Alignment %p [%16llX]", align.ptr(), cond.key());
0232 printout(lvl, prefix, "++ \tPath:%s [%p]", cond.name(), align.ptr());
0233 printAlignment(lvl, tag, "ALIGNMENT", de, align);
0234 }
0235 catch(...) {
0236 printout(ERROR, tag, "++ %s %s [%16llX]",
0237 prefix.c_str(), "FAILED Alignment:", cond.key());
0238 }
0239 }
0240 return;
0241 }
0242 except(tag, "Cannot process alignments of an invalid detector element");
0243 }