CMS 3D CMS Logo

CocoaDBMgr.cc
Go to the documentation of this file.
6 #include <DD4hep/DD4hepUnits.h>
11 
19 
28 
32 
34 
36 
37 //----------------------------------------------------------------------
39  if (!instance) {
40  instance = new CocoaDBMgr;
41  }
42  return instance;
43 }
44 
45 //----------------------------------------------------------------------
47 
48 //-----------------------------------------------------------------------
51 
53  int nrcd;
54 
55  cond::Time_t appendTime = Fit::nEvent + 1;
56  if (gomgr->GlobalOptions()["writeDBOptAlign"] > 0) {
57  //----- Build OpticalAlignments
58  std::unique_ptr<OpticalAlignments> optalign = BuildOpticalAlignments();
59 
60  //--- Dump OpticalAlignments
61  nrcd = optalign->opticalAlignments_.size();
62  if (!myDbService.isAvailable()) {
63  throw cms::Exception("CocoaDBMgr::DumpCocoaResults DB not available");
64  }
65  // try{
66  if (myDbService->isNewTagRequest("OpticalAlignmentsRcd")) {
67  std::cout << " new OA to DB "
68  << "begin " << myDbService->beginOfTime() << " current " << myDbService->currentTime() << " end "
69  << myDbService->endOfTime() << std::endl;
70  myDbService->createOneIOV<OpticalAlignments>(*optalign, myDbService->beginOfTime(), "OpticalAlignmentsRcd");
71  } else {
72  std::cout << " old OA to DB "
73  << " current " << myDbService->currentTime() << " end " << myDbService->endOfTime() << std::endl;
74  myDbService->appendOneIOV<OpticalAlignments>(*optalign, appendTime, "OpticalAlignmentsRcd");
75  }
76 
77  /* }catch(const cond::Exception& er) {
78  std::cout<<er.what()<<std::endl;
79  }catch(const std::exception& er){
80  std::cout<<"caught std::exception "<<er.what()<<std::endl;
81  }catch(...){
82  std::cout<<"Funny error"<<std::endl;
83  } */
84 
85  if (ALIUtils::debug >= 2)
86  std::cout << "OpticalAlignmentsRcd WRITTEN TO DB : " << nrcd << std::endl;
87  }
88 
89  if (gomgr->GlobalOptions()["writeDBAlign"] > 0) {
90  // Build DT alignments and errors
91  const auto& dtali = BuildAlignments(true);
92  auto& dt_Alignments = dtali.first;
93  auto& dt_AlignmentErrors = dtali.second;
94 
95  // Dump DT alignments and errors
96  nrcd = dt_Alignments->m_align.size();
97  if (myDbService->isNewTagRequest("DTAlignmentRcd")) {
98  myDbService->createOneIOV<Alignments>(*dt_Alignments, myDbService->beginOfTime(), "DTAlignmentRcd");
99  } else {
100  myDbService->appendOneIOV<Alignments>(*dt_Alignments, appendTime, "DTAlignmentRcd");
101  }
102  if (ALIUtils::debug >= 2)
103  std::cout << "DTAlignmentRcd WRITTEN TO DB : " << nrcd << std::endl;
104 
105  nrcd = dt_AlignmentErrors->m_alignError.size();
106  if (myDbService->isNewTagRequest("DTAlignmentErrorExtendedRcd")) {
108  *dt_AlignmentErrors, myDbService->beginOfTime(), "DTAlignmentErrorExtendedRcd");
109  } else {
111  *dt_AlignmentErrors, appendTime, "DTAlignmentErrorExtendedRcd");
112  }
113  if (ALIUtils::debug >= 2)
114  std::cout << "DTAlignmentErrorExtendedRcd WRITTEN TO DB : " << nrcd << std::endl;
115 
116  // Build CSC alignments and errors
117  const auto& cscali = BuildAlignments(false);
118  auto& csc_Alignments = cscali.first;
119  auto& csc_AlignmentErrors = cscali.second;
120 
121  // Dump CSC alignments and errors
122  nrcd = csc_Alignments->m_align.size();
123  if (myDbService->isNewTagRequest("CSCAlignmentRcd")) {
124  myDbService->createOneIOV<Alignments>(*csc_Alignments, myDbService->beginOfTime(), "CSCAlignmentRcd");
125  } else {
126  myDbService->appendOneIOV<Alignments>(*csc_Alignments, appendTime, "CSCAlignmentRcd");
127  }
128  if (ALIUtils::debug >= 2)
129  std::cout << "CSCAlignmentRcd WRITTEN TO DB : " << nrcd << std::endl;
130 
131  nrcd = csc_AlignmentErrors->m_alignError.size();
132  if (myDbService->isNewTagRequest("CSCAlignmentErrorExtendedRcd")) {
134  *csc_AlignmentErrors, myDbService->beginOfTime(), "CSCAlignmentErrorExtendedRcd");
135  } else {
137  *csc_AlignmentErrors, appendTime, "CSCAlignmentErrorExtendedRcd");
138  }
139  if (ALIUtils::debug >= 2)
140  std::cout << "CSCAlignmentErrorExtendedRcd WRITTEN TO DB : " << nrcd << std::endl;
141 
142  //? gives unreadable error??? std::cout << "@@@@ OPTICALALIGNMENTS WRITTEN TO DB " << *optalign << std::endl;
143 
144  return TRUE;
145  }
146 
147  return TRUE;
148 }
149 
150 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
152  LogDebug("Alignment") << " CocoaDBMgr::GetOptAlignInfoFromOptO " << opto->name();
154  data.ID_ = opto->getCmsswID();
155  data.type_ = opto->type();
156  data.name_ = opto->name();
157 
158  //----- Centre in local coordinates
159  CLHEP::Hep3Vector centreLocal = opto->centreGlob() - opto->parent()->centreGlob();
160  CLHEP::HepRotation parentRmGlobInv = inverseOf(opto->parent()->rmGlob());
161  centreLocal = parentRmGlobInv * centreLocal;
162 
163  const std::vector<Entry*>& theCoordinateEntryVector = opto->CoordinateEntryList();
164  LogDebug("Alignment") << " CocoaDBMgr::GetOptAlignInfoFromOptO starting coord ";
165  if (theCoordinateEntryVector.size() == 6) {
166  const Entry* const translationX = theCoordinateEntryVector.at(0);
167  OpticalAlignParam translationXDataForDB;
168  translationXDataForDB.name_ = translationX->name();
169  translationXDataForDB.dim_type_ = translationX->type();
170  translationXDataForDB.value_ = centreLocal.x() * dd4hep::m; // m in COCOA, dd4hep unit in DB
171  translationXDataForDB.error_ = GetEntryError(translationX) * dd4hep::m; // m in COCOA, dd4hep unit in DB
172  translationXDataForDB.quality_ = translationX->quality();
173  data.x_ = translationXDataForDB;
174 
175  const Entry* const translationY = theCoordinateEntryVector.at(1);
176  OpticalAlignParam translationYDataForDB;
177  translationYDataForDB.name_ = translationY->name();
178  translationYDataForDB.dim_type_ = translationY->type();
179  translationYDataForDB.value_ = centreLocal.y() * dd4hep::m; // m in COCOA, dd4hep unit in DB
180  translationYDataForDB.error_ = GetEntryError(translationY) * dd4hep::m; // m in COCOA, dd4hep unit in DB
181  translationYDataForDB.quality_ = translationY->quality();
182  data.y_ = translationYDataForDB;
183 
184  const Entry* const translationZ = theCoordinateEntryVector.at(2);
185  OpticalAlignParam translationZDataForDB;
186  translationZDataForDB.name_ = translationZ->name();
187  translationZDataForDB.dim_type_ = translationZ->type();
188  translationZDataForDB.value_ = centreLocal.z() * dd4hep::m; // m in COCOA, dd4hep unit in DB
189  translationZDataForDB.error_ = GetEntryError(translationZ) * dd4hep::m; // m in COCOA, dd4hep unit in DB
190  translationZDataForDB.quality_ = translationZ->quality();
191  data.z_ = translationZDataForDB;
192 
193  //----- angles in local coordinates
194  std::vector<double> anglocal = opto->getLocalRotationAngles(theCoordinateEntryVector);
195  if (anglocal.size() == 3) {
196  const Entry* const rotationX = theCoordinateEntryVector.at(3);
197  OpticalAlignParam rotationXDataForDB;
198  rotationXDataForDB.name_ = rotationX->name();
199  rotationXDataForDB.dim_type_ = rotationX->type();
200  rotationXDataForDB.value_ = anglocal.at(0);
201  rotationXDataForDB.error_ = GetEntryError(rotationX);
202  rotationXDataForDB.quality_ = rotationX->quality();
203  data.angx_ = rotationXDataForDB;
204 
205  const Entry* const rotationY = theCoordinateEntryVector.at(4);
206  OpticalAlignParam rotationYDataForDB;
207  rotationYDataForDB.name_ = rotationY->name();
208  rotationYDataForDB.dim_type_ = rotationY->type();
209  rotationYDataForDB.value_ = anglocal.at(1);
210  rotationYDataForDB.error_ = GetEntryError(rotationY);
211  rotationYDataForDB.quality_ = rotationY->quality();
212  data.angy_ = rotationYDataForDB;
213 
214  const Entry* const rotationZ = theCoordinateEntryVector.at(5);
215  OpticalAlignParam rotationZDataForDB;
216  rotationZDataForDB.name_ = rotationZ->name();
217  rotationZDataForDB.dim_type_ = rotationZ->type();
218  rotationZDataForDB.value_ = anglocal.at(2);
219  rotationZDataForDB.error_ = GetEntryError(rotationZ);
220  rotationZDataForDB.quality_ = rotationZ->quality();
221  data.angz_ = rotationZDataForDB;
222  }
223  }
224 
225  std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO starting entry " << std::endl;
226  for (const auto& myDBExtraEntry : opto->ExtraEntryList()) {
227  OpticalAlignParam extraEntry;
228  extraEntry.name_ = myDBExtraEntry->name();
229  extraEntry.dim_type_ = myDBExtraEntry->type();
230  extraEntry.value_ = myDBExtraEntry->value();
231  extraEntry.error_ = myDBExtraEntry->sigma();
232  if (extraEntry.dim_type_ == "centre" || extraEntry.dim_type_ == "length") {
233  extraEntry.value_ *= dd4hep::m; // m in COCOA, dd4hep unit in DB
234  extraEntry.error_ *= dd4hep::m; // m in COCOA, dd4hep unit in DB
235  }
236  extraEntry.quality_ = myDBExtraEntry->quality();
237  data.extraEntries_.emplace_back(extraEntry);
238  std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO done extra entry " << extraEntry.name_ << std::endl;
239  }
240 
241  return data;
242 }
243 
244 //-----------------------------------------------------------------------
246  if (entry->quality() > 0) {
247  return sqrt(Fit::GetAtWAMatrix()->Mat()->me[entry->fitPos()][entry->fitPos()]);
248  } else { //entry not fitted, return original error
249  return entry->sigma();
250  }
251 }
252 
253 //-----------------------------------------------------------------------
254 double CocoaDBMgr::GetEntryError(const Entry* entry1, const Entry* entry2) {
255  if (entry1 == entry2)
256  return GetEntryError(entry1);
257 
258  if (entry1->quality() > 0 && entry2->quality() > 0) {
259  return sqrt(Fit::GetAtWAMatrix()->Mat()->me[entry1->fitPos()][entry2->fitPos()]);
260  } else { //entries not fitted, correlation is 0
261  return 0.;
262  }
263 }
264 
265 //-----------------------------------------------------------------------
266 std::unique_ptr<OpticalAlignments> CocoaDBMgr::BuildOpticalAlignments() {
267  std::unique_ptr<OpticalAlignments> optalign = std::make_unique<OpticalAlignments>();
268 
269  static std::vector<OpticalObject*> optolist = Model::OptOList();
270  static std::vector<OpticalObject*>::const_iterator ite;
271  for (ite = optolist.begin(); ite != optolist.end(); ++ite) {
272  if ((*ite)->type() == "system")
273  continue;
275  optalign->opticalAlignments_.push_back(data);
276  if (ALIUtils::debug >= 5) {
277  std::cout << "@@@@ OPTALIGNINFO TO BE WRITTEN TO DB " << data << std::endl;
278  }
279  }
280  return optalign;
281 }
282 
283 //-----------------------------------------------------------------------
284 std::pair<std::unique_ptr<Alignments>, std::unique_ptr<AlignmentErrorsExtended> > CocoaDBMgr::BuildAlignments(bool bDT) {
285  std::unique_ptr<Alignments> alignments = std::make_unique<Alignments>();
286  std::unique_ptr<AlignmentErrorsExtended> alignmentErrors = std::make_unique<AlignmentErrorsExtended>();
287 
288  //read
289  static std::vector<OpticalObject*> optolist = Model::OptOList();
290  static std::vector<OpticalObject*>::const_iterator ite;
291  for (ite = optolist.begin(); ite != optolist.end(); ++ite) {
292  if ((*ite)->type() == "system")
293  continue;
294  std::cout << "CocoaDBMgr::BuildAlignments getCmsswID " << (*ite) << std::endl;
295  std::cout << "CocoaDBMgr::BuildAlignments getCmsswID " << (*ite)->getCmsswID() << std::endl;
296  //check CMSSW ID
297  if ((*ite)->getCmsswID() > 0) { //put the numbers of DT or CSC objects
298  std::cout << " cal fill alignments " << std::endl;
299  alignments->m_align.push_back(*(GetAlignInfoFromOptO(*ite)));
300  std::cout << " fill alignments " << std::endl;
301  // AlignTransformErrorExtended* err =
302  //GetAlignInfoErrorFromOptO( *ite );
303  alignmentErrors->m_alignError.push_back(*(GetAlignInfoErrorFromOptO(*ite)));
304  std::cout << "CocoaDBMgr::BuildAlignments add alignmentError " << alignmentErrors->m_alignError.size()
305  << std::endl;
306  }
307  }
308 
309  if (ALIUtils::debug >= 4)
310  std::cout << "CocoaDBMgr::BuildAlignments end with n alignment " << alignments->m_align.size()
311  << " n alignmentError " << alignmentErrors->m_alignError.size() << std::endl;
312 
313  return std::make_pair(std::move(alignments), std::move(alignmentErrors));
314 }
315 
316 //-----------------------------------------------------------------------
318  if (ALIUtils::debug >= 3)
319  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO " << opto->name() << std::endl;
320 
321  const AlignTransform::Translation& trans = opto->centreGlob();
322  const AlignTransform::Rotation& rot = opto->rmGlob();
323  align::ID cmsswID = opto->getCmsswID();
324 
325  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO buildalign" << opto->name() << std::endl;
326  AlignTransform* align = new AlignTransform(trans, rot, cmsswID);
327 
328  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO alig built " << opto->name() << std::endl;
329 
330  return align;
331  // return dd;
332 }
333 
334 //-----------------------------------------------------------------------
336  if (ALIUtils::debug >= 3)
337  std::cout << "@@@ CocoaDBMgr::GetAlignInfoErrorFromOptO " << opto->name() << std::endl;
338 
339  align::ID cmsswID = opto->getCmsswID();
340 
341  GlobalError gerr(1., 0., 1., 0., 0., 1.);
342  //double(dx*dx), 0., double(dy*dy), 0., 0., double(dz*dz) ) ;
343  CLHEP::HepSymMatrix errms = asHepMatrix(gerr.matrix());
344  AlignTransformErrorExtended* alignError = new AlignTransformErrorExtended(errms, cmsswID);
345  return alignError;
346 
347  CLHEP::HepMatrix errm(3, 3);
348  const std::vector<Entry*>& theCoordinateEntryVector = opto->CoordinateEntryList();
349  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptOfill errm " << opto->name() << std::endl;
350  errm(0, 0) = GetEntryError(theCoordinateEntryVector[0]) * dd4hep::m; // m in COCOA, dd4hep unit in DB
351  errm(1, 1) = GetEntryError(theCoordinateEntryVector[1]) * dd4hep::m; // m in COCOA, dd4hep unit in DB
352  errm(2, 2) = GetEntryError(theCoordinateEntryVector[2]) * dd4hep::m; // m in COCOA, dd4hep unit in DB
353  errm(0, 1) = GetEntryError(theCoordinateEntryVector[0], theCoordinateEntryVector[1]) *
354  dd4hep::m; // m in COCOA, dd4hep unit in DB
355  errm(0, 2) = GetEntryError(theCoordinateEntryVector[0], theCoordinateEntryVector[2]) *
356  dd4hep::m; // m in COCOA, dd4hep unit in DB
357  errm(1, 2) = GetEntryError(theCoordinateEntryVector[1], theCoordinateEntryVector[2]) *
358  dd4hep::m; // m in COCOA, dd4hep unit in DB
359  // errm(1,0) = errm(0,1);
360  // errm(2,0) = errm(0,2);
361  // errm(2,1) = errm(1,2);
362 
363  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO errm filled" << opto->name() << std::endl;
364  // CLHEP::HepSymMatrix errms(3);
365  // errms.assign(errm);
366 
367  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO errms filled " << opto->name() << std::endl;
368  // AlignTransformErrorExtended* alignError = new AlignTransformErrorExtended( errms, cmsswID );
369  // AlignTransformErrorExtended* alignError = 0;
370 
371  std::cout << alignError << "@@@ CocoaDBMgr::GetAlignInfoFromOptO error built " << opto->name() << std::endl;
372  //t return alignError;
373  return (AlignTransformErrorExtended*)nullptr;
374 }
const CLHEP::Hep3Vector & centreGlob() const
Definition: OpticalObject.h:75
CLHEP::HepMatrix asHepMatrix(const ROOT::Math::SMatrix< double, N1, N2, typename ROOT::Math::MatRepStd< double, N1, N2 > > &rm)
Definition: Migration.h:60
const ALIstring & name() const
Definition: Entry.h:50
#define TRUE
Definition: scimark2.h:12
AlignTransformErrorExtended * GetAlignInfoErrorFromOptO(OpticalObject *opto)
Definition: CocoaDBMgr.cc:335
Definition: Entry.h:18
static ALIMatrix * GetAtWAMatrix()
Definition: Fit.h:152
uint32_t ID
Definition: Definitions.h:24
static std::vector< OpticalObject * > & OptOList()
Definition: Model.h:84
bool DumpCocoaResults()
Definition: CocoaDBMgr.cc:49
CLHEP::Hep3Vector Translation
static ALIint debug
Definition: ALIUtils.h:34
const ALIstring & name() const
Definition: OpticalObject.h:58
static GlobalOptionMgr * getInstance()
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
const std::vector< Entry * > & ExtraEntryList() const
Definition: OpticalObject.h:65
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
static CocoaDBMgr * instance
Definition: CocoaDBMgr.h:60
ALIint fitPos() const
Definition: Entry.h:58
unsigned long long Time_t
Definition: Time.h:14
std::unique_ptr< OpticalAlignments > BuildOpticalAlignments()
Definition: CocoaDBMgr.cc:266
std::vector< double > getLocalRotationAngles(const std::vector< Entry *> &entries) const
bool isNewTagRequest(const std::string &recordName)
T sqrt(T t)
Definition: SSEVec.h:19
ALIint quality() const
Definition: Entry.h:57
const AlgebraicSymMatrix33 matrix() const
static ALIuint nEvent
Definition: Fit.h:208
const std::vector< Entry * > & CoordinateEntryList() const
Definition: OpticalObject.h:63
AlignTransform * GetAlignInfoFromOptO(OpticalObject *opto)
Definition: CocoaDBMgr.cc:317
double GetEntryError(const Entry *entry)
Definition: CocoaDBMgr.cc:245
OpticalAlignInfo GetOptAlignInfoFromOptO(OpticalObject *opto)
Definition: CocoaDBMgr.cc:151
const CLHEP::HepRotation & rmGlob() const
Definition: OpticalObject.h:83
static CocoaDBMgr * getInstance()
Definition: CocoaDBMgr.cc:38
const ALIstring & type() const
Definition: Entry.h:52
std::pair< std::unique_ptr< Alignments >, std::unique_ptr< AlignmentErrorsExtended > > BuildAlignments(bool bDT)
Definition: CocoaDBMgr.cc:284
std::map< ALIstring, ALIdouble, std::less< ALIstring > > & GlobalOptions()
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
const OpticalObject * parent() const
Definition: OpticalObject.h:60
bool isAvailable() const
Definition: Service.h:40
std::string dim_type_
def move(src, dest)
Definition: eostools.py:511
const ALIstring & type() const
Definition: OpticalObject.h:59
#define LogDebug(id)
const ALIuint getCmsswID() const
Definition: OpticalObject.h:97
CLHEP::HepRotation Rotation