Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
#include "CondFormats/Alignment/interface/Alignments.h"
#include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
#include "CondFormats/Alignment/interface/AlignTransform.h"
#include "CondFormats/Alignment/interface/AlignTransformErrorExtended.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"
#include <DD4hep/DD4hepUnits.h>
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ESHandle.h"

#include "Alignment/CocoaFit/interface/CocoaDBMgr.h"
#include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurementInfo.h"
#include "CondFormats/DataRecord/interface/OpticalAlignmentsRcd.h"
#include "CondFormats/AlignmentRecord/interface/DTAlignmentRcd.h"
#include "Geometry/Records/interface/IdealGeometryRecord.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"

#include "Alignment/CocoaUtilities/interface/ALIUtils.h"
#include "Alignment/CocoaModel/interface/Model.h"
#include "Alignment/CocoaFit/interface/Fit.h"
#include "Alignment/CocoaModel/interface/Entry.h"
#include "Alignment/CocoaUtilities/interface/ALIFileOut.h"
#include "Alignment/CocoaModel/interface/CocoaDaqReaderRoot.h"
#include "Alignment/CocoaModel/interface/OpticalObject.h"
#include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"

#include "CondFormats/OptAlignObjects/interface/OpticalAlignments.h"
#include "CondFormats/OptAlignObjects/interface/OpticalAlignInfo.h"
#include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurements.h"

#include "CondCore/CondDB/interface/Serialization.h"

CocoaDBMgr* CocoaDBMgr::instance = nullptr;

//----------------------------------------------------------------------
CocoaDBMgr* CocoaDBMgr::getInstance() {
  if (!instance) {
    instance = new CocoaDBMgr;
  }
  return instance;
}

//----------------------------------------------------------------------
CocoaDBMgr::CocoaDBMgr() {}

//-----------------------------------------------------------------------
bool CocoaDBMgr::DumpCocoaResults() {
  edm::Service<cond::service::PoolDBOutputService> myDbService;

  GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
  int nrcd;

  cond::Time_t appendTime = Fit::nEvent + 1;
  if (gomgr->GlobalOptions()["writeDBOptAlign"] > 0) {
    //----- Build OpticalAlignments
    std::unique_ptr<OpticalAlignments> optalign = BuildOpticalAlignments();

    //--- Dump OpticalAlignments
    nrcd = optalign->opticalAlignments_.size();
    if (!myDbService.isAvailable()) {
      throw cms::Exception("CocoaDBMgr::DumpCocoaResults DB not available");
    }
    //    try{
    if (myDbService->isNewTagRequest("OpticalAlignmentsRcd")) {
      std::cout << " new OA to DB "
                << "begin " << myDbService->beginOfTime() << " current " << myDbService->currentTime() << " end "
                << myDbService->endOfTime() << std::endl;
      myDbService->createOneIOV<OpticalAlignments>(*optalign, myDbService->beginOfTime(), "OpticalAlignmentsRcd");
    } else {
      std::cout << " old OA to DB "
                << " current " << myDbService->currentTime() << " end " << myDbService->endOfTime() << std::endl;
      myDbService->appendOneIOV<OpticalAlignments>(*optalign, appendTime, "OpticalAlignmentsRcd");
    }

    /*    }catch(const cond::Exception& er) {
	  std::cout<<er.what()<<std::endl;
	  }catch(const std::exception& er){
	  std::cout<<"caught std::exception "<<er.what()<<std::endl;
	  }catch(...){
	  std::cout<<"Funny error"<<std::endl;
	  } */

    if (ALIUtils::debug >= 2)
      std::cout << "OpticalAlignmentsRcd WRITTEN TO DB : " << nrcd << std::endl;
  }

  if (gomgr->GlobalOptions()["writeDBAlign"] > 0) {
    // Build DT alignments and errors
    const auto& dtali = BuildAlignments(true);
    auto& dt_Alignments = dtali.first;
    auto& dt_AlignmentErrors = dtali.second;

    // Dump DT alignments and errors
    nrcd = dt_Alignments->m_align.size();
    if (myDbService->isNewTagRequest("DTAlignmentRcd")) {
      myDbService->createOneIOV<Alignments>(*dt_Alignments, myDbService->beginOfTime(), "DTAlignmentRcd");
    } else {
      myDbService->appendOneIOV<Alignments>(*dt_Alignments, appendTime, "DTAlignmentRcd");
    }
    if (ALIUtils::debug >= 2)
      std::cout << "DTAlignmentRcd WRITTEN TO DB : " << nrcd << std::endl;

    nrcd = dt_AlignmentErrors->m_alignError.size();
    if (myDbService->isNewTagRequest("DTAlignmentErrorExtendedRcd")) {
      myDbService->createOneIOV<AlignmentErrorsExtended>(
          *dt_AlignmentErrors, myDbService->beginOfTime(), "DTAlignmentErrorExtendedRcd");
    } else {
      myDbService->appendOneIOV<AlignmentErrorsExtended>(
          *dt_AlignmentErrors, appendTime, "DTAlignmentErrorExtendedRcd");
    }
    if (ALIUtils::debug >= 2)
      std::cout << "DTAlignmentErrorExtendedRcd WRITTEN TO DB : " << nrcd << std::endl;

    // Build CSC alignments and errors
    const auto& cscali = BuildAlignments(false);
    auto& csc_Alignments = cscali.first;
    auto& csc_AlignmentErrors = cscali.second;

    // Dump CSC alignments and errors
    nrcd = csc_Alignments->m_align.size();
    if (myDbService->isNewTagRequest("CSCAlignmentRcd")) {
      myDbService->createOneIOV<Alignments>(*csc_Alignments, myDbService->beginOfTime(), "CSCAlignmentRcd");
    } else {
      myDbService->appendOneIOV<Alignments>(*csc_Alignments, appendTime, "CSCAlignmentRcd");
    }
    if (ALIUtils::debug >= 2)
      std::cout << "CSCAlignmentRcd WRITTEN TO DB : " << nrcd << std::endl;

    nrcd = csc_AlignmentErrors->m_alignError.size();
    if (myDbService->isNewTagRequest("CSCAlignmentErrorExtendedRcd")) {
      myDbService->createOneIOV<AlignmentErrorsExtended>(
          *csc_AlignmentErrors, myDbService->beginOfTime(), "CSCAlignmentErrorExtendedRcd");
    } else {
      myDbService->appendOneIOV<AlignmentErrorsExtended>(
          *csc_AlignmentErrors, appendTime, "CSCAlignmentErrorExtendedRcd");
    }
    if (ALIUtils::debug >= 2)
      std::cout << "CSCAlignmentErrorExtendedRcd WRITTEN TO DB : " << nrcd << std::endl;

    //? gives unreadable error???  std::cout << "@@@@ OPTICALALIGNMENTS WRITTEN TO DB " << *optalign << std::endl;

    return TRUE;
  }

  return TRUE;
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OpticalAlignInfo CocoaDBMgr::GetOptAlignInfoFromOptO(OpticalObject* opto) {
  LogDebug("Alignment") << " CocoaDBMgr::GetOptAlignInfoFromOptO " << opto->name();
  OpticalAlignInfo data;
  data.ID_ = opto->getCmsswID();
  data.type_ = opto->type();
  data.name_ = opto->name();

  //----- Centre in local coordinates
  CLHEP::Hep3Vector centreLocal = opto->centreGlob() - opto->parent()->centreGlob();
  CLHEP::HepRotation parentRmGlobInv = inverseOf(opto->parent()->rmGlob());
  centreLocal = parentRmGlobInv * centreLocal;

  const std::vector<Entry*>& theCoordinateEntryVector = opto->CoordinateEntryList();
  LogDebug("Alignment") << " CocoaDBMgr::GetOptAlignInfoFromOptO starting coord ";
  if (theCoordinateEntryVector.size() == 6) {
    const Entry* const translationX = theCoordinateEntryVector.at(0);
    OpticalAlignParam translationXDataForDB;
    translationXDataForDB.name_ = translationX->name();
    translationXDataForDB.dim_type_ = translationX->type();
    translationXDataForDB.value_ = centreLocal.x() * dd4hep::m;              // m in COCOA, dd4hep unit in DB
    translationXDataForDB.error_ = GetEntryError(translationX) * dd4hep::m;  // m in COCOA, dd4hep unit in DB
    translationXDataForDB.quality_ = translationX->quality();
    data.x_ = translationXDataForDB;

    const Entry* const translationY = theCoordinateEntryVector.at(1);
    OpticalAlignParam translationYDataForDB;
    translationYDataForDB.name_ = translationY->name();
    translationYDataForDB.dim_type_ = translationY->type();
    translationYDataForDB.value_ = centreLocal.y() * dd4hep::m;              // m in COCOA, dd4hep unit in DB
    translationYDataForDB.error_ = GetEntryError(translationY) * dd4hep::m;  // m in COCOA, dd4hep unit in DB
    translationYDataForDB.quality_ = translationY->quality();
    data.y_ = translationYDataForDB;

    const Entry* const translationZ = theCoordinateEntryVector.at(2);
    OpticalAlignParam translationZDataForDB;
    translationZDataForDB.name_ = translationZ->name();
    translationZDataForDB.dim_type_ = translationZ->type();
    translationZDataForDB.value_ = centreLocal.z() * dd4hep::m;              // m in COCOA, dd4hep unit in DB
    translationZDataForDB.error_ = GetEntryError(translationZ) * dd4hep::m;  // m in COCOA, dd4hep unit in DB
    translationZDataForDB.quality_ = translationZ->quality();
    data.z_ = translationZDataForDB;

    //----- angles in local coordinates
    std::vector<double> anglocal = opto->getLocalRotationAngles(theCoordinateEntryVector);
    if (anglocal.size() == 3) {
      const Entry* const rotationX = theCoordinateEntryVector.at(3);
      OpticalAlignParam rotationXDataForDB;
      rotationXDataForDB.name_ = rotationX->name();
      rotationXDataForDB.dim_type_ = rotationX->type();
      rotationXDataForDB.value_ = anglocal.at(0);
      rotationXDataForDB.error_ = GetEntryError(rotationX);
      rotationXDataForDB.quality_ = rotationX->quality();
      data.angx_ = rotationXDataForDB;

      const Entry* const rotationY = theCoordinateEntryVector.at(4);
      OpticalAlignParam rotationYDataForDB;
      rotationYDataForDB.name_ = rotationY->name();
      rotationYDataForDB.dim_type_ = rotationY->type();
      rotationYDataForDB.value_ = anglocal.at(1);
      rotationYDataForDB.error_ = GetEntryError(rotationY);
      rotationYDataForDB.quality_ = rotationY->quality();
      data.angy_ = rotationYDataForDB;

      const Entry* const rotationZ = theCoordinateEntryVector.at(5);
      OpticalAlignParam rotationZDataForDB;
      rotationZDataForDB.name_ = rotationZ->name();
      rotationZDataForDB.dim_type_ = rotationZ->type();
      rotationZDataForDB.value_ = anglocal.at(2);
      rotationZDataForDB.error_ = GetEntryError(rotationZ);
      rotationZDataForDB.quality_ = rotationZ->quality();
      data.angz_ = rotationZDataForDB;
    }
  }

  std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO starting entry " << std::endl;
  for (const auto& myDBExtraEntry : opto->ExtraEntryList()) {
    OpticalAlignParam extraEntry;
    extraEntry.name_ = myDBExtraEntry->name();
    extraEntry.dim_type_ = myDBExtraEntry->type();
    extraEntry.value_ = myDBExtraEntry->value();
    extraEntry.error_ = myDBExtraEntry->sigma();
    if (extraEntry.dim_type_ == "centre" || extraEntry.dim_type_ == "length") {
      extraEntry.value_ *= dd4hep::m;  // m in COCOA, dd4hep unit in DB
      extraEntry.error_ *= dd4hep::m;  // m in COCOA, dd4hep unit in DB
    }
    extraEntry.quality_ = myDBExtraEntry->quality();
    data.extraEntries_.emplace_back(extraEntry);
    std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO done extra entry " << extraEntry.name_ << std::endl;
  }

  return data;
}

//-----------------------------------------------------------------------
double CocoaDBMgr::GetEntryError(const Entry* entry) {
  if (entry->quality() > 0) {
    return sqrt(Fit::GetAtWAMatrix()->Mat()->me[entry->fitPos()][entry->fitPos()]);
  } else {  //entry not fitted, return original error
    return entry->sigma();
  }
}

//-----------------------------------------------------------------------
double CocoaDBMgr::GetEntryError(const Entry* entry1, const Entry* entry2) {
  if (entry1 == entry2)
    return GetEntryError(entry1);

  if (entry1->quality() > 0 && entry2->quality() > 0) {
    return sqrt(Fit::GetAtWAMatrix()->Mat()->me[entry1->fitPos()][entry2->fitPos()]);
  } else {  //entries not fitted, correlation is 0
    return 0.;
  }
}

//-----------------------------------------------------------------------
std::unique_ptr<OpticalAlignments> CocoaDBMgr::BuildOpticalAlignments() {
  std::unique_ptr<OpticalAlignments> optalign = std::make_unique<OpticalAlignments>();

  static std::vector<OpticalObject*> optolist = Model::OptOList();
  static std::vector<OpticalObject*>::const_iterator ite;
  for (ite = optolist.begin(); ite != optolist.end(); ++ite) {
    if ((*ite)->type() == "system")
      continue;
    OpticalAlignInfo data = GetOptAlignInfoFromOptO(*ite);
    optalign->opticalAlignments_.push_back(data);
    if (ALIUtils::debug >= 5) {
      std::cout << "@@@@ OPTALIGNINFO TO BE WRITTEN TO DB " << data << std::endl;
    }
  }
  return optalign;
}

//-----------------------------------------------------------------------
std::pair<std::unique_ptr<Alignments>, std::unique_ptr<AlignmentErrorsExtended> > CocoaDBMgr::BuildAlignments(bool bDT) {
  std::unique_ptr<Alignments> alignments = std::make_unique<Alignments>();
  std::unique_ptr<AlignmentErrorsExtended> alignmentErrors = std::make_unique<AlignmentErrorsExtended>();

  //read
  static std::vector<OpticalObject*> optolist = Model::OptOList();
  static std::vector<OpticalObject*>::const_iterator ite;
  for (ite = optolist.begin(); ite != optolist.end(); ++ite) {
    if ((*ite)->type() == "system")
      continue;
    std::cout << "CocoaDBMgr::BuildAlignments getCmsswID " << (*ite) << std::endl;
    std::cout << "CocoaDBMgr::BuildAlignments getCmsswID " << (*ite)->getCmsswID() << std::endl;
    //check CMSSW ID
    if ((*ite)->getCmsswID() > 0) {  //put the numbers of DT or CSC objects
      std::cout << " cal fill alignments " << std::endl;
      alignments->m_align.push_back(*(GetAlignInfoFromOptO(*ite)));
      std::cout << " fill alignments " << std::endl;
      //      AlignTransformErrorExtended* err =
      //GetAlignInfoErrorFromOptO( *ite );
      alignmentErrors->m_alignError.push_back(*(GetAlignInfoErrorFromOptO(*ite)));
      std::cout << "CocoaDBMgr::BuildAlignments add alignmentError " << alignmentErrors->m_alignError.size()
                << std::endl;
    }
  }

  if (ALIUtils::debug >= 4)
    std::cout << "CocoaDBMgr::BuildAlignments end with n alignment " << alignments->m_align.size()
              << " n alignmentError " << alignmentErrors->m_alignError.size() << std::endl;

  return std::make_pair(std::move(alignments), std::move(alignmentErrors));
}

//-----------------------------------------------------------------------
AlignTransform* CocoaDBMgr::GetAlignInfoFromOptO(OpticalObject* opto) {
  if (ALIUtils::debug >= 3)
    std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO " << opto->name() << std::endl;

  const AlignTransform::Translation& trans = opto->centreGlob();
  const AlignTransform::Rotation& rot = opto->rmGlob();
  align::ID cmsswID = opto->getCmsswID();

  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO buildalign" << opto->name() << std::endl;
  AlignTransform* align = new AlignTransform(trans, rot, cmsswID);

  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO alig built " << opto->name() << std::endl;

  return align;
  //  return dd;
}

//-----------------------------------------------------------------------
AlignTransformErrorExtended* CocoaDBMgr::GetAlignInfoErrorFromOptO(OpticalObject* opto) {
  if (ALIUtils::debug >= 3)
    std::cout << "@@@ CocoaDBMgr::GetAlignInfoErrorFromOptO " << opto->name() << std::endl;

  align::ID cmsswID = opto->getCmsswID();

  GlobalError gerr(1., 0., 1., 0., 0., 1.);
  //double(dx*dx),  0., double(dy*dy),     0., 0., double(dz*dz) ) ;
  CLHEP::HepSymMatrix errms = asHepMatrix(gerr.matrix());
  AlignTransformErrorExtended* alignError = new AlignTransformErrorExtended(errms, cmsswID);
  return alignError;

  CLHEP::HepMatrix errm(3, 3);
  const std::vector<Entry*>& theCoordinateEntryVector = opto->CoordinateEntryList();
  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptOfill errm " << opto->name() << std::endl;
  errm(0, 0) = GetEntryError(theCoordinateEntryVector[0]) * dd4hep::m;  // m in COCOA, dd4hep unit in DB
  errm(1, 1) = GetEntryError(theCoordinateEntryVector[1]) * dd4hep::m;  // m in COCOA, dd4hep unit in DB
  errm(2, 2) = GetEntryError(theCoordinateEntryVector[2]) * dd4hep::m;  // m in COCOA, dd4hep unit in DB
  errm(0, 1) = GetEntryError(theCoordinateEntryVector[0], theCoordinateEntryVector[1]) *
               dd4hep::m;  // m in COCOA, dd4hep unit in DB
  errm(0, 2) = GetEntryError(theCoordinateEntryVector[0], theCoordinateEntryVector[2]) *
               dd4hep::m;  // m in COCOA, dd4hep unit in DB
  errm(1, 2) = GetEntryError(theCoordinateEntryVector[1], theCoordinateEntryVector[2]) *
               dd4hep::m;  // m in COCOA, dd4hep unit in DB
  //   errm(1,0) = errm(0,1);
  // errm(2,0) = errm(0,2);
  // errm(2,1) = errm(1,2);

  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO errm filled" << opto->name() << std::endl;
  //  CLHEP::HepSymMatrix errms(3);
  //  errms.assign(errm);

  std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO errms filled " << opto->name() << std::endl;
  //  AlignTransformErrorExtended* alignError = new AlignTransformErrorExtended( errms, cmsswID );
  //  AlignTransformErrorExtended* alignError = 0;

  std::cout << alignError << "@@@ CocoaDBMgr::GetAlignInfoFromOptO error built " << opto->name() << std::endl;
  //t  return alignError;
  return (AlignTransformErrorExtended*)nullptr;
}