CMS 3D CMS Logo

ApeSettingAlgorithm.cc
Go to the documentation of this file.
1 
9 /*
10  * The APE record and the ASCII file contain the covariance matrix elements
11  * in units of cm^2
12  *# Parameters:
13  *# saveApeToASCII -- Do we write out an APE text file?
14  *# saveComposites -- Do we write APEs for composite detectors?
15  *# saveLocalNotGlobal -- Do we write the APEs in the local or global coordinates?
16  *# apeASCIISaveFile -- The name of the save-file.
17  *# readApeFromASCII -- Do we read in APEs from a text file?
18  *# readLocalNotGlobal -- Do we read APEs in the local or the global frame?
19  *# readFullLocalMatrix -- Do we read the full local matrix or just the diagonal elements?
20  *# -- Always write full matrix
21  *# Full matrix format: DetID dxx dxy dyy dxz dyz dzz
22  *# Diagonal element format: DetID sqrt(dxx) sqrt(dyy) sqrt(dzz)
23  *# setComposites -- Do we set the APEs for composite detectors or just ignore them?
24  *# apeASCIIReadFile -- Input file name.
25  *# Also note:
26  *# process.AlignmentProducer.saveApeToDB -- to save as an sqlite file
27  *# and associated entries in _cfg.py
28  */
29 
33 
35 
38 #include "CLHEP/Matrix/SymMatrix.h"
39 
40 #include <fstream>
41 #include <string>
42 #include <set>
43 
46 
50 
51 // includes to make known that they inherit from Alignable:
55 
57 
60 
62 public:
65 
67  ~ApeSettingAlgorithm() override;
68 
70  void initialize(const edm::EventSetup &setup,
73  AlignableExtras *extras,
74  AlignmentParameterStore *store) override;
75 
77  void terminate(const edm::EventSetup &iSetup) override;
78 
80  void run(const edm::EventSetup &setup, const EventInfo &eventInfo) override;
81 
82 private:
89 };
90 
91 //____________________________________________________
92 //____________________________________________________
93 //____________________________________________________
94 //____________________________________________________
95 
96 // Constructor ----------------------------------------------------------------
97 //____________________________________________________
99  : AlignmentAlgorithmBase(cfg, iC), theConfig(cfg), theAlignableNavigator(nullptr) {
100  edm::LogInfo("Alignment") << "@SUB=ApeSettingAlgorithm"
101  << "Start.";
102  saveApeToAscii_ = theConfig.getUntrackedParameter<bool>("saveApeToASCII");
103  saveComposites_ = theConfig.getUntrackedParameter<bool>("saveComposites");
104  saveLocalNotGlobal_ = theConfig.getUntrackedParameter<bool>("saveLocalNotGlobal");
105  readApeFromAscii_ = theConfig.getParameter<bool>("readApeFromASCII");
106  readLocalNotGlobal_ = theConfig.getParameter<bool>("readLocalNotGlobal");
107  readFullLocalMatrix_ = theConfig.getParameter<bool>("readFullLocalMatrix");
108  setComposites_ = theConfig.getParameter<bool>("setComposites");
109 }
110 
111 // Destructor ----------------------------------------------------------------
112 //____________________________________________________
114 
115 // Call at beginning of job ---------------------------------------------------
116 //____________________________________________________
120  AlignableExtras *extras,
121  AlignmentParameterStore *store) {
124 
125  if (readApeFromAscii_) {
126  std::ifstream apeReadFile(
127  theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath().c_str()); //requires <fstream>
128  if (!apeReadFile.good()) {
129  edm::LogInfo("Alignment") << "@SUB=initialize"
130  << "Problem opening APE file: skipping"
131  << theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath();
132  return;
133  }
134  std::set<int> apeList; //To avoid duplicates
135  while (!apeReadFile.eof()) {
136  int apeId = 0;
137  double x11, x21, x22, x31, x32, x33, ignore;
139  apeReadFile >> apeId >> x11 >> x21 >> x22 >> x31 >> x32 >> x33 >> ignore >> ignore >> ignore >> ignore >>
140  ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >>
141  ignore >> std::ws;
142  } else {
143  apeReadFile >> apeId >> x11 >> x22 >> x33 >> std::ws;
144  }
145  //idr What sanity checks do we need to put here?
146  if (apeId != 0) //read appears valid?
147  {
148  if (apeList.find(apeId) == apeList.end()) //Not previously done
149  {
150  DetId id(apeId);
152  if (alidet) {
153  if ((alidet->components().empty()) || setComposites_) //the problem with glued dets...
154  {
155  GlobalErrorExtended globErr;
156  if (readLocalNotGlobal_) {
158  if (readFullLocalMatrix_) {
159  as[0][0] = x11;
160  as[1][0] = x21;
161  as[1][1] = x22;
162  as[2][0] = x31;
163  as[2][1] = x32;
164  as[2][2] = x33;
165  } else {
166  as[0][0] = x11 * x11;
167  as[1][1] = x22 * x22;
168  as[2][2] = x33 * x33;
169  } //local cov.
170  align::RotationType rt = alidet->globalRotation();
172  am[0][0] = rt.xx();
173  am[0][1] = rt.xy();
174  am[0][2] = rt.xz();
175  am[1][0] = rt.yx();
176  am[1][1] = rt.yy();
177  am[1][2] = rt.yz();
178  am[2][0] = rt.zx();
179  am[2][1] = rt.zy();
180  am[2][2] = rt.zz();
181  globErr = GlobalErrorExtended(ROOT::Math::SimilarityT(am, as));
182  } else {
184  globErr =
185  GlobalErrorExtended(x11, x21, x31, 0, 0, 0, x22, x32, 0, 0, 0, x33, 0, 0, 0, 0, 0, 0, 0, 0, 0);
186  else {
187  globErr = GlobalErrorExtended(
188  x11 * x11, 0, 0, 0, 0, 0, x22 * x22, 0, 0, 0, 0, x33 * x33, 0, 0, 0, 0, 0, 0, 0, 0, 0);
189  }
190  }
191  alidet->setAlignmentPositionError(globErr, false); // do not propagate down!
192  apeList.insert(apeId); //Flag it's been set
193  } else {
194  edm::LogInfo("Alignment") << "@SUB=initialize"
195  << "Not Setting APE for Composite DetId " << apeId;
196  }
197  }
198  } else {
199  edm::LogInfo("Alignment") << "@SUB=initialize"
200  << "Skipping duplicate APE for DetId " << apeId;
201  }
202  }
203  }
204  apeReadFile.close();
205  edm::LogInfo("Alignment") << "@SUB=initialize"
206  << "Set " << apeList.size() << " APE values.";
207  }
208 }
209 
210 // Call at end of job ---------------------------------------------------------
211 //____________________________________________________
213  if (saveApeToAscii_) {
215  int theSize = aliErr->m_alignError.size();
216  std::ofstream apeSaveFile(
217  theConfig.getUntrackedParameter<std::string>("apeASCIISaveFile").c_str()); //requires <fstream>
218  for (int i = 0; i < theSize; ++i) {
219  int id = aliErr->m_alignError[i].rawId();
221  if (alidet && ((alidet->components().empty()) || saveComposites_)) {
222  apeSaveFile << id;
223  CLHEP::HepSymMatrix sm = aliErr->m_alignError[i].matrix();
224  if (saveLocalNotGlobal_) {
225  align::RotationType rt = alidet->globalRotation();
226  AlgebraicMatrix am(3, 3);
227  am[0][0] = rt.xx();
228  am[0][1] = rt.xy();
229  am[0][2] = rt.xz();
230  am[1][0] = rt.yx();
231  am[1][1] = rt.yy();
232  am[1][2] = rt.yz();
233  am[2][0] = rt.zx();
234  am[2][1] = rt.zy();
235  am[2][2] = rt.zz();
236  sm = sm.similarity(am); //symmetric matrix
237  } //transform to local
238  for (int j = 0; j < sm.num_row(); ++j)
239  for (int k = 0; k <= j; ++k)
240  apeSaveFile << " " << sm[j][k]; //always write full matrix
241 
242  apeSaveFile << std::endl;
243  }
244  }
245  delete aliErr;
246  apeSaveFile.close();
247  }
248  // clean up at end: // FIXME: should we delete here or in destructor?
249  delete theAlignableNavigator;
250  theAlignableNavigator = nullptr;
251 }
252 
253 // Run the algorithm on trajectories and tracks -------------------------------
254 //____________________________________________________
256  // nothing to do here?
257 }
258 
259 // Plugin definition for the algorithm
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepStd< double, 3, 3 > > AlgebraicMatrix33
AlignableTracker * theTracker
std::string fullPath() const
Definition: FileInPath.cc:161
~ApeSettingAlgorithm() override
Destructor.
edm::ParameterSet theConfig
void run(const edm::EventSetup &setup, const EventInfo &eventInfo) override
Run the algorithm.
Interface/Base class for alignment algorithms, each alignment algorithm has to be derived from this c...
define event information passed to algorithms
T getUntrackedParameter(std::string const &, T const &) const
auto &__restrict__ ws
CLHEP::HepMatrix AlgebraicMatrix
AlignableNavigator * theAlignableNavigator
def ignore(seq)
GlobalErrorBaseExtended< double, ErrorMatrixTag > GlobalErrorExtended
Definition: GlobalError.h:14
Log< level::Info, false > LogInfo
Definition: DetId.h:17
std::vector< AlignTransformErrorExtended > m_alignError
AlignmentErrorsExtended * alignmentErrors() const override
Return alignment errors, sorted by DetId.
virtual void terminate()
Called at end of job (must be implemented in derived class)
ApeSettingAlgorithm(const edm::ParameterSet &cfg, const edm::ConsumesCollector &iC)
Constructor.
ROOT::Math::SMatrix< double, 3, 3, ROOT::Math::MatRepSym< double, 3 > > AlgebraicSymMatrix33
#define DEFINE_EDM_PLUGIN(factory, type, name)
eventInfo
add run, event number and lumi section
Constructor of the full muon geometry.
Definition: AlignableMuon.h:38
AlignableDetOrUnitPtr alignableFromDetId(const DetId &detid)
Returns AlignableDetOrUnitPtr corresponding to given DetId.
void initialize(const edm::EventSetup &setup, AlignableTracker *tracker, AlignableMuon *muon, AlignableExtras *extras, AlignmentParameterStore *store) override
Call at beginning of job.