00001 #include "CondFormats/Alignment/interface/Alignments.h"
00002 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00003 #include "CondFormats/Alignment/interface/AlignTransform.h"
00004 #include "CondFormats/Alignment/interface/AlignTransformError.h"
00005 #include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007 #include "CondCore/DBCommon/interface/Exception.h"
00008 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011
00012 #include "Alignment/CocoaFit/interface/CocoaDBMgr.h"
00013 #include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurementInfo.h"
00014 #include "CondFormats/DataRecord/interface/OpticalAlignmentsRcd.h"
00015 #include "CondFormats/AlignmentRecord/interface/DTAlignmentRcd.h"
00016 #include "DetectorDescription/Core/interface/DDFilteredView.h"
00017 #include "DetectorDescription/Core/interface/DDCompactView.h"
00018 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00019 #include "FWCore/Framework/interface/Event.h"
00020 #include "FWCore/Framework/interface/EventSetup.h"
00021
00022 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
00023 #include "Alignment/CocoaModel/interface/Model.h"
00024 #include "Alignment/CocoaFit/interface/Fit.h"
00025 #include "Alignment/CocoaModel/interface/Entry.h"
00026 #include "Alignment/CocoaUtilities/interface/ALIFileOut.h"
00027 #include "Alignment/CocoaModel/interface/CocoaDaqReaderRoot.h"
00028 #include "Alignment/CocoaModel/interface/OpticalObject.h"
00029 #include "Alignment/CocoaUtilities/interface/GlobalOptionMgr.h"
00030
00031 #include "CondFormats/OptAlignObjects/interface/OpticalAlignments.h"
00032 #include "CondFormats/OptAlignObjects/interface/OpticalAlignInfo.h"
00033 #include "CondFormats/OptAlignObjects/interface/OpticalAlignMeasurements.h"
00034
00035 CocoaDBMgr* CocoaDBMgr::instance = 0;
00036
00037
00038 CocoaDBMgr* CocoaDBMgr::getInstance()
00039 {
00040 if(!instance) {
00041 instance = new CocoaDBMgr;
00042 }
00043 return instance;
00044 }
00045
00046
00047 CocoaDBMgr::CocoaDBMgr()
00048 {
00049 }
00050
00051
00052 bool CocoaDBMgr::DumpCocoaResults()
00053 {
00054 edm::Service<cond::service::PoolDBOutputService> myDbService;
00055
00056 GlobalOptionMgr* gomgr = GlobalOptionMgr::getInstance();
00057 int nrcd;
00058
00059 cond::Time_t appendTime = Fit::nEvent+1;
00060 if(gomgr->GlobalOptions()["writeDBOptAlign"] > 0 ) {
00061
00062
00063 OpticalAlignments* optalign = BuildOpticalAlignments();
00064
00065
00066 nrcd = optalign->opticalAlignments_.size();
00067 if( !myDbService.isAvailable() ){
00068 throw cms::Exception("CocoaDBMgr::DumpCocoaResults DB not available");
00069 }
00070
00071 if ( myDbService->isNewTagRequest( "OpticalAlignmentsRcd" ) ) {
00072 std::cout << " new OA to DB " << "begin " << myDbService->beginOfTime() << " current " << myDbService->currentTime() << " end " << myDbService->endOfTime() << std::endl;
00073 myDbService->createNewIOV<OpticalAlignments>(optalign,
00074 myDbService->beginOfTime(),
00075 myDbService->endOfTime(),
00076
00077 "OpticalAlignmentsRcd");
00078 } else {
00079 std::cout << " old OA to DB " << " current " << myDbService->currentTime() << " end " << myDbService->endOfTime() << std::endl;
00080 myDbService->appendSinceTime<OpticalAlignments>( optalign,
00081
00082 appendTime,
00083
00084 "OpticalAlignmentsRcd");
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 if(ALIUtils::debug >= 2) std::cout << "OpticalAlignmentsRcd WRITTEN TO DB : "<< nrcd << std::endl;
00097 }
00098
00099 if( gomgr->GlobalOptions()["writeDBAlign"] > 0) {
00100
00101
00102 std::pair< Alignments*,AlignmentErrors*> dtali = BuildAlignments(1);
00103 Alignments* dt_Alignments = dtali.first;
00104 AlignmentErrors* dt_AlignmentErrors = dtali.second;
00105
00106
00107 nrcd = dt_Alignments->m_align.size();
00108 if ( myDbService->isNewTagRequest( "DTAlignmentRcd" ) ) {
00109 myDbService->createNewIOV<Alignments>(&(*dt_Alignments),
00110 myDbService->beginOfTime(),
00111 myDbService->endOfTime(),
00112 "DTAlignmentRcd");
00113 } else {
00114 myDbService->appendSinceTime<Alignments>( &(*dt_Alignments),
00115 appendTime,
00116
00117 "DTAlignmentRcd");
00118 }
00119 if(ALIUtils::debug >= 2) std::cout << "DTAlignmentRcd WRITTEN TO DB : "<< nrcd << std::endl;
00120
00121 nrcd = dt_AlignmentErrors->m_alignError.size();
00122 if ( myDbService->isNewTagRequest( "DTAlignmentErrorRcd" ) ) {
00123 myDbService->createNewIOV<AlignmentErrors>(&(*dt_AlignmentErrors),
00124 myDbService->beginOfTime(),
00125 myDbService->endOfTime(),
00126 "DTAlignmentErrorRcd");
00127 } else {
00128 myDbService->appendSinceTime<AlignmentErrors>( &(*dt_AlignmentErrors),
00129 appendTime,
00130 "DTAlignmentErrorRcd");
00131 }
00132 if(ALIUtils::debug >= 2) std::cout << "DTAlignmentErrorRcd WRITTEN TO DB : "<< nrcd << std::endl;
00133
00134
00135 std::pair< Alignments*,AlignmentErrors*> cscali = BuildAlignments(0);
00136 Alignments* csc_Alignments = cscali.first;
00137 AlignmentErrors* csc_AlignmentErrors = cscali.second;
00138
00139
00140 nrcd = csc_Alignments->m_align.size();
00141 if ( myDbService->isNewTagRequest( "CSCAlignmentRcd" ) ) {
00142 myDbService->createNewIOV<Alignments>(&(*csc_Alignments),
00143 myDbService->beginOfTime(),
00144 myDbService->endOfTime(),
00145 "CSCAlignmentRcd");
00146 } else {
00147 myDbService->appendSinceTime<Alignments>( &(*csc_Alignments),
00148 appendTime,
00149 "CSCAlignmentRcd");
00150 }
00151 if(ALIUtils::debug >= 2) std::cout << "CSCAlignmentRcd WRITTEN TO DB : "<< nrcd << std::endl;
00152
00153 nrcd = csc_AlignmentErrors->m_alignError.size();
00154 if ( myDbService->isNewTagRequest( "CSCAlignmentErrorRcd" ) ) {
00155 myDbService->createNewIOV<AlignmentErrors>(&(*csc_AlignmentErrors),
00156 myDbService->beginOfTime(),
00157 myDbService->endOfTime(),
00158 "CSCAlignmentErrorRcd");
00159 } else {
00160 myDbService->appendSinceTime<AlignmentErrors>( &(*csc_AlignmentErrors),
00161 appendTime,
00162 "CSCAlignmentErrorRcd");
00163 }
00164 if(ALIUtils::debug >= 2) std::cout << "CSCAlignmentErrorRcd WRITTEN TO DB : "<< nrcd << std::endl;
00165
00166
00167
00168 return TRUE;
00169 }
00170
00171 return TRUE;
00172 }
00173
00174
00175
00176 OpticalAlignInfo CocoaDBMgr::GetOptAlignInfoFromOptO( OpticalObject* opto )
00177 {
00178 std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO " << opto->name() << std::endl;
00179 OpticalAlignInfo data;
00180 data.ID_=opto->getCmsswID();
00181 data.type_=opto->type();
00182 data.name_=opto->name();
00183
00184
00185 CLHEP::Hep3Vector centreLocal = opto->centreGlob() - opto->parent()->centreGlob();
00186 CLHEP::HepRotation parentRmGlobInv = inverseOf( opto->parent()->rmGlob() );
00187 centreLocal = parentRmGlobInv * centreLocal;
00188
00189 const std::vector< Entry* > theCoordinateEntryVector = opto->CoordinateEntryList();
00190 std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO starting coord " <<std::endl;
00191
00192 data.x_.value_= centreLocal.x() / 100.;
00193 std::cout << " matrix " << Fit::GetAtWAMatrix() << std::endl;
00194 std::cout << " matrix " << Fit::GetAtWAMatrix()->Mat() << " " << theCoordinateEntryVector[0]->fitPos() << std::endl;
00195 data.x_.error_= GetEntryError( theCoordinateEntryVector[0] ) / 100.;
00196
00197 data.y_.value_= centreLocal.y() / 100.;
00198 std::cout << " matrix " << Fit::GetAtWAMatrix()->Mat() << " " << theCoordinateEntryVector[1]->fitPos() << std::endl;
00199 data.y_.error_= GetEntryError( theCoordinateEntryVector[1] ) / 100.;
00200
00201 data.z_.value_= centreLocal.z() / 100.;
00202 std::cout << " matrix " << Fit::GetAtWAMatrix()->Mat() << " " << theCoordinateEntryVector[2]->fitPos() << std::endl;
00203 data.z_.error_= GetEntryError( theCoordinateEntryVector[2] ) / 100.;
00204
00205
00206 std::vector<double> anglocal = opto->getLocalRotationAngles( theCoordinateEntryVector );
00207
00208 data.angx_.value_= anglocal[0] *180./M_PI;
00209 std::cout << " matrix " << Fit::GetAtWAMatrix()->Mat() << theCoordinateEntryVector[3]->fitPos() << std::endl;
00210 data.angx_.error_= GetEntryError( theCoordinateEntryVector[3] ) * 180./M_PI;
00211
00212 data.angy_.value_= anglocal[1] * 180./M_PI;
00213 std::cout << " matrix " << Fit::GetAtWAMatrix()->Mat() << theCoordinateEntryVector[4]->fitPos() << std::endl;
00214 data.angy_.error_= GetEntryError( theCoordinateEntryVector[4] ) * 180./M_PI;
00215
00216 data.angz_.value_= anglocal[2] *180./M_PI;
00217 std::cout << " matrix " << Fit::GetAtWAMatrix()->Mat() << theCoordinateEntryVector[5]->fitPos() << std::endl;
00218 data.angz_.error_= GetEntryError( theCoordinateEntryVector[5] ) * 180./M_PI;
00219
00220
00221 const std::vector< Entry* > theExtraEntryVector = opto->ExtraEntryList(); std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO starting entry " << std::endl;
00222
00223 std::vector< Entry* >::const_iterator ite;
00224 for( ite = theExtraEntryVector.begin(); ite != theExtraEntryVector.end(); ite++ ) {
00225 OpticalAlignParam extraEntry;
00226 extraEntry.name_ = (*ite)->name();
00227 extraEntry.dim_type_ = (*ite)->type();
00228 extraEntry.value_ = (*ite)->value();
00229 extraEntry.error_ = (*ite)->sigma();
00230 extraEntry.quality_ = (*ite)->quality();
00231 data.extraEntries_.push_back( extraEntry );
00232 std::cout << " CocoaDBMgr::GetOptAlignInfoFromOptO done extra entry " << extraEntry.name_ << std::endl;
00233
00234 }
00235
00236 return data;
00237 }
00238
00239
00240
00241 double CocoaDBMgr::GetEntryError( const Entry* entry )
00242 {
00243 if( entry->quality() > 0 ) {
00244 return sqrt(Fit::GetAtWAMatrix()->Mat()->me[entry->fitPos()][entry->fitPos()]);
00245 } else {
00246 return entry->sigma();
00247 }
00248 }
00249
00250
00251
00252 double CocoaDBMgr::GetEntryError( const Entry* entry1, const Entry* entry2 )
00253 {
00254 if( entry1 == entry2 ) return GetEntryError( entry1 );
00255
00256 if( entry1->quality() > 0 && entry2->quality() > 0 ) {
00257 return sqrt(Fit::GetAtWAMatrix()->Mat()->me[entry1->fitPos()][entry2->fitPos()]);
00258 } else {
00259 return 0.;
00260 }
00261 }
00262
00263
00264
00265 OpticalAlignments* CocoaDBMgr::BuildOpticalAlignments()
00266 {
00267 OpticalAlignments* optalign= new OpticalAlignments;
00268
00269 static std::vector< OpticalObject* > optolist = Model::OptOList();
00270 static std::vector< OpticalObject* >::const_iterator ite;
00271 for(ite = optolist.begin(); ite != optolist.end(); ite++ ){
00272 if( (*ite)->type() == "system" ) continue;
00273 OpticalAlignInfo data = GetOptAlignInfoFromOptO( *ite );
00274 optalign->opticalAlignments_.push_back(data);
00275 if(ALIUtils::debug >= 5) {
00276 std::cout << "@@@@ OPTALIGNINFO TO BE WRITTEN TO DB "
00277 << data
00278 << std::endl;
00279 }
00280 }
00281 return optalign;
00282 }
00283
00284
00285
00286 std::pair< Alignments*,AlignmentErrors*> CocoaDBMgr::BuildAlignments(bool bDT)
00287 {
00288 Alignments* alignments = new Alignments;
00289 AlignmentErrors* alignmentErrors = new AlignmentErrors;
00290
00291
00292 static std::vector< OpticalObject* > optolist = Model::OptOList();
00293 static std::vector< OpticalObject* >::const_iterator ite;
00294 for(ite = optolist.begin(); ite != optolist.end(); ite++ ){
00295 if( (*ite)->type() == "system" ) continue;
00296 std::cout << "CocoaDBMgr::BuildAlignments getCmsswID " << (*ite) << std::endl;
00297 std::cout << "CocoaDBMgr::BuildAlignments getCmsswID " << (*ite)->getCmsswID() << std::endl;
00298
00299 if( (*ite)->getCmsswID() > 0 ) {
00300 std::cout << " cal fill alignments " << std::endl;
00301 alignments->m_align.push_back( *(GetAlignInfoFromOptO( *ite )));
00302 std::cout << " fill alignments " << std::endl;
00303
00304
00305 alignmentErrors->m_alignError.push_back(*(GetAlignInfoErrorFromOptO( *ite )));
00306 std::cout << "CocoaDBMgr::BuildAlignments add alignmentError " << alignmentErrors->m_alignError.size() << std::endl;
00307 }
00308 }
00309
00310 if(ALIUtils::debug >= 4) std::cout << "CocoaDBMgr::BuildAlignments end with n alignment " << alignments->m_align.size() << " n alignmentError " << alignmentErrors->m_alignError.size() << std::endl;
00311 return std::pair< Alignments*,AlignmentErrors*>(alignments,alignmentErrors);
00312 }
00313
00314
00315
00316 AlignTransform* CocoaDBMgr::GetAlignInfoFromOptO( OpticalObject* opto )
00317 {
00318 if(ALIUtils::debug >= 3) std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO " << opto->name() << std::endl;
00319
00320 AlignTransform::Translation trans = opto->centreGlob();
00321 AlignTransform::Rotation rot = opto->rmGlob();
00322 align::ID cmsswID = opto->getCmsswID();
00323
00324 std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO buildalign" << opto->name() << std::endl;
00325 AlignTransform* align = new AlignTransform( trans, rot, cmsswID );
00326
00327 std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO alig built " << opto->name() << std::endl;
00328
00329 return align;
00330
00331 }
00332
00333
00334 AlignTransformError* CocoaDBMgr::GetAlignInfoErrorFromOptO( OpticalObject* opto )
00335 {
00336 if(ALIUtils::debug >= 3) std::cout << "@@@ CocoaDBMgr::GetAlignInfoErrorFromOptO " << opto->name() << std::endl;
00337
00338 align::ID cmsswID = opto->getCmsswID();
00339
00340 GlobalError gerr(1.,
00341 0.,
00342 1.,
00343 0.,
00344 0.,
00345 1.);
00346
00347 CLHEP::HepSymMatrix errms = asHepMatrix(gerr.matrix());
00348 AlignTransformError* alignError = new AlignTransformError( errms, cmsswID );
00349 return alignError;
00350
00351 CLHEP::HepMatrix errm(3,3);
00352 const std::vector< Entry* > theCoordinateEntryVector = opto->CoordinateEntryList();
00353 std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptOfill errm " << opto->name() << std::endl;
00354 errm(0,0) = GetEntryError( theCoordinateEntryVector[0] ) / 100.;
00355 errm(1,1) = GetEntryError( theCoordinateEntryVector[1] ) / 100.;
00356 errm(2,2) = GetEntryError( theCoordinateEntryVector[2] ) / 100.;
00357 errm(0,1) = GetEntryError( theCoordinateEntryVector[0], theCoordinateEntryVector[1] ) / 100.;
00358 errm(0,2) = GetEntryError( theCoordinateEntryVector[0], theCoordinateEntryVector[2] ) / 100.;
00359 errm(1,2) = GetEntryError( theCoordinateEntryVector[1], theCoordinateEntryVector[2] ) / 100.;
00360
00361
00362
00363
00364 std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO errm filled" << opto->name() << std::endl;
00365
00366
00367
00368 std::cout << "@@@ CocoaDBMgr::GetAlignInfoFromOptO errms filled " << opto->name() << std::endl;
00369
00370
00371
00372 std::cout << alignError << "@@@ CocoaDBMgr::GetAlignInfoFromOptO error built " << opto->name() << std::endl;
00373
00374 return (AlignTransformError*)(0);
00375 }
00376
00377