File indexing completed on 2024-09-07 04:34:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmPluginFactory.h"
0031 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmBase.h"
0032 #include "Alignment/CommonAlignment/interface/AlignableModifier.h"
0033
0034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0035
0036 #include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
0037 #include "DataFormats/GeometrySurface/interface/GloballyPositioned.h"
0038 #include "CLHEP/Matrix/SymMatrix.h"
0039
0040 #include <fstream>
0041 #include <string>
0042 #include <set>
0043
0044 #include "DataFormats/GeometryCommonDetAlgo/interface/AlignmentPositionError.h"
0045 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
0046
0047 #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"
0048 #include "Alignment/CommonAlignment/interface/AlignableDetOrUnitPtr.h"
0049 #include "Alignment/CommonAlignment/interface/Alignable.h"
0050
0051
0052 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
0053 #include "Alignment/MuonAlignment/interface/AlignableMuon.h"
0054 #include "Alignment/CommonAlignment/interface/AlignableExtras.h"
0055
0056 #include "DataFormats/CLHEP/interface/AlgebraicObjects.h"
0057
0058 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0059 #include "FWCore/Framework/interface/ConsumesCollector.h"
0060
0061 class ApeSettingAlgorithm : public AlignmentAlgorithmBase {
0062 public:
0063
0064 ApeSettingAlgorithm(const edm::ParameterSet &cfg, const edm::ConsumesCollector &iC);
0065
0066
0067 ~ApeSettingAlgorithm() override;
0068
0069
0070 void initialize(const edm::EventSetup &setup,
0071 AlignableTracker *tracker,
0072 AlignableMuon *muon,
0073 AlignableExtras *extras,
0074 AlignmentParameterStore *store) override;
0075
0076
0077 void terminate(const edm::EventSetup &iSetup) override;
0078
0079
0080 void run(const edm::EventSetup &setup, const EventInfo &eventInfo) override;
0081
0082 private:
0083 edm::ParameterSet theConfig;
0084 AlignableNavigator *theAlignableNavigator;
0085 AlignableTracker *theTracker;
0086 bool saveApeToAscii_, readApeFromAscii_, readFullLocalMatrix_;
0087 bool readLocalNotGlobal_, saveLocalNotGlobal_;
0088 bool setComposites_, saveComposites_;
0089 };
0090
0091
0092
0093
0094
0095
0096
0097
0098 ApeSettingAlgorithm::ApeSettingAlgorithm(const edm::ParameterSet &cfg, const edm::ConsumesCollector &iC)
0099 : AlignmentAlgorithmBase(cfg, iC), theConfig(cfg), theAlignableNavigator(nullptr) {
0100 edm::LogInfo("Alignment") << "@SUB=ApeSettingAlgorithm"
0101 << "Start.";
0102 saveApeToAscii_ = theConfig.getUntrackedParameter<bool>("saveApeToASCII");
0103 saveComposites_ = theConfig.getUntrackedParameter<bool>("saveComposites");
0104 saveLocalNotGlobal_ = theConfig.getUntrackedParameter<bool>("saveLocalNotGlobal");
0105 readApeFromAscii_ = theConfig.getParameter<bool>("readApeFromASCII");
0106 readLocalNotGlobal_ = theConfig.getParameter<bool>("readLocalNotGlobal");
0107 readFullLocalMatrix_ = theConfig.getParameter<bool>("readFullLocalMatrix");
0108 setComposites_ = theConfig.getParameter<bool>("setComposites");
0109 }
0110
0111
0112
0113 ApeSettingAlgorithm::~ApeSettingAlgorithm() { delete theAlignableNavigator; }
0114
0115
0116
0117 void ApeSettingAlgorithm::initialize(const edm::EventSetup &setup,
0118 AlignableTracker *tracker,
0119 AlignableMuon *muon,
0120 AlignableExtras *extras,
0121 AlignmentParameterStore *store) {
0122 theAlignableNavigator = new AlignableNavigator(tracker, muon);
0123 theTracker = tracker;
0124
0125 if (readApeFromAscii_) {
0126 std::ifstream apeReadFile(
0127 theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath().c_str());
0128 if (!apeReadFile.good()) {
0129 edm::LogInfo("Alignment") << "@SUB=initialize"
0130 << "Problem opening APE file: skipping"
0131 << theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath();
0132 return;
0133 }
0134 std::set<int> apeList;
0135 while (!apeReadFile.eof()) {
0136 int apeId = 0;
0137 double x11, x21, x22, x31, x32, x33, ignore;
0138 if (!readLocalNotGlobal_ || readFullLocalMatrix_) {
0139 apeReadFile >> apeId >> x11 >> x21 >> x22 >> x31 >> x32 >> x33 >> ignore >> ignore >> ignore >> ignore >>
0140 ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >>
0141 ignore >> std::ws;
0142 } else {
0143 apeReadFile >> apeId >> x11 >> x22 >> x33 >> std::ws;
0144 }
0145
0146 if (apeId != 0)
0147 {
0148 if (apeList.find(apeId) == apeList.end())
0149 {
0150 DetId id(apeId);
0151 AlignableDetOrUnitPtr alidet(theAlignableNavigator->alignableFromDetId(id));
0152 if (alidet) {
0153 if ((alidet->components().empty()) || setComposites_)
0154 {
0155 GlobalErrorExtended globErr;
0156 if (readLocalNotGlobal_) {
0157 AlgebraicSymMatrix33 as;
0158 if (readFullLocalMatrix_) {
0159 as[0][0] = x11;
0160 as[1][0] = x21;
0161 as[1][1] = x22;
0162 as[2][0] = x31;
0163 as[2][1] = x32;
0164 as[2][2] = x33;
0165 } else {
0166 as[0][0] = x11 * x11;
0167 as[1][1] = x22 * x22;
0168 as[2][2] = x33 * x33;
0169 }
0170 align::RotationType rt = alidet->globalRotation();
0171 AlgebraicMatrix33 am;
0172 am[0][0] = rt.xx();
0173 am[0][1] = rt.xy();
0174 am[0][2] = rt.xz();
0175 am[1][0] = rt.yx();
0176 am[1][1] = rt.yy();
0177 am[1][2] = rt.yz();
0178 am[2][0] = rt.zx();
0179 am[2][1] = rt.zy();
0180 am[2][2] = rt.zz();
0181 globErr = GlobalErrorExtended(ROOT::Math::SimilarityT(am, as));
0182 } else {
0183 if (readFullLocalMatrix_)
0184 globErr =
0185 GlobalErrorExtended(x11, x21, x31, 0, 0, 0, x22, x32, 0, 0, 0, x33, 0, 0, 0, 0, 0, 0, 0, 0, 0);
0186 else {
0187 globErr = GlobalErrorExtended(
0188 x11 * x11, 0, 0, 0, 0, 0, x22 * x22, 0, 0, 0, 0, x33 * x33, 0, 0, 0, 0, 0, 0, 0, 0, 0);
0189 }
0190 }
0191 alidet->setAlignmentPositionError(globErr, false);
0192 apeList.insert(apeId);
0193 } else {
0194 edm::LogInfo("Alignment") << "@SUB=initialize"
0195 << "Not Setting APE for Composite DetId " << apeId;
0196 }
0197 }
0198 } else {
0199 edm::LogInfo("Alignment") << "@SUB=initialize"
0200 << "Skipping duplicate APE for DetId " << apeId;
0201 }
0202 }
0203 }
0204 apeReadFile.close();
0205 edm::LogInfo("Alignment") << "@SUB=initialize"
0206 << "Set " << apeList.size() << " APE values.";
0207 }
0208 }
0209
0210
0211
0212 void ApeSettingAlgorithm::terminate(const edm::EventSetup &iSetup) {
0213 if (saveApeToAscii_) {
0214 AlignmentErrorsExtended *aliErr = theTracker->alignmentErrors();
0215 int theSize = aliErr->m_alignError.size();
0216 std::ofstream apeSaveFile(
0217 theConfig.getUntrackedParameter<std::string>("apeASCIISaveFile").c_str());
0218 for (int i = 0; i < theSize; ++i) {
0219 int id = aliErr->m_alignError[i].rawId();
0220 AlignableDetOrUnitPtr alidet(theAlignableNavigator->alignableFromDetId(DetId(id)));
0221 if (alidet && ((alidet->components().empty()) || saveComposites_)) {
0222 apeSaveFile << id;
0223 CLHEP::HepSymMatrix sm = aliErr->m_alignError[i].matrix();
0224 if (saveLocalNotGlobal_) {
0225 align::RotationType rt = alidet->globalRotation();
0226 AlgebraicMatrix am(3, 3);
0227 am[0][0] = rt.xx();
0228 am[0][1] = rt.xy();
0229 am[0][2] = rt.xz();
0230 am[1][0] = rt.yx();
0231 am[1][1] = rt.yy();
0232 am[1][2] = rt.yz();
0233 am[2][0] = rt.zx();
0234 am[2][1] = rt.zy();
0235 am[2][2] = rt.zz();
0236 sm = sm.similarity(am);
0237 }
0238 for (int j = 0; j < sm.num_row(); ++j)
0239 for (int k = 0; k <= j; ++k)
0240 apeSaveFile << " " << sm[j][k];
0241
0242 apeSaveFile << std::endl;
0243 }
0244 }
0245 delete aliErr;
0246 apeSaveFile.close();
0247 }
0248
0249 delete theAlignableNavigator;
0250 theAlignableNavigator = nullptr;
0251 }
0252
0253
0254
0255 void ApeSettingAlgorithm::run(const edm::EventSetup &setup, const EventInfo &eventInfo) {
0256
0257 }
0258
0259
0260 DEFINE_EDM_PLUGIN(AlignmentAlgorithmPluginFactory, ApeSettingAlgorithm, "ApeSettingAlgorithm");