CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Alignment/OfflineValidation/plugins/TrackerGeometryCompare.cc

Go to the documentation of this file.
00001 #include "CondFormats/Alignment/interface/Alignments.h"
00002 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00003 #include "CLHEP/Vector/RotationInterfaces.h" 
00004 #include "CondFormats/Alignment/interface/AlignmentSorter.h"
00005 #include "CondFormats/AlignmentRecord/interface/TrackerSurveyRcd.h"
00006 #include "CondFormats/AlignmentRecord/interface/TrackerSurveyErrorRcd.h"
00007 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentRcd.h"
00008 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentErrorRcd.h"
00009 #include "FWCore/Framework/interface/MakerMacros.h"
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011 #include "FWCore/Framework/interface/EventSetup.h"
00012 #include "FWCore/Framework/interface/ESTransientHandle.h"
00013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00014 
00015 #include "Alignment/CommonAlignment/interface/AlignableObjectId.h"
00016 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00017 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeomBuilderFromGeometricDet.h"
00018 #include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h" 
00019 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00020 #include "Geometry/TrackingGeometryAligner/interface/GeometryAligner.h"
00021 #include "Alignment/CommonAlignment/interface/Utilities.h"
00022 #include "Alignment/CommonAlignment/interface/SurveyDet.h"
00023 #include "Alignment/CommonAlignment/interface/Alignable.h"
00024 #include "DataFormats/DetId/interface/DetId.h"
00025 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
00026 #include "CondFormats/Alignment/interface/DetectorGlobalPosition.h"
00027 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00028 
00029 //#include "Alignment/OfflineValidation/interface/ComparisonUtilities.h"
00030 //#include "Alignment/CommonAlignment/interface/AlignTools.h"
00031 
00032 //#include "Alignment/OfflineValidation/plugins/TrackerGeometryCompare.h"
00033 #include "TrackerGeometryCompare.h"
00034 #include "TFile.h" 
00035 #include "CLHEP/Vector/ThreeVector.h"
00036 
00037 // Database
00038 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00039 #include "FWCore/ServiceRegistry/interface/Service.h"
00040 //#include "Geometry/Records/interface/PGeometricDetRcd.h"
00041 
00042 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00043 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00044 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00045 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00046 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00047 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00048 
00049 #include <iostream>
00050 #include <fstream>
00051 
00052 TrackerGeometryCompare::TrackerGeometryCompare(const edm::ParameterSet& cfg)
00053 {
00054         
00055         //input is ROOT
00056         _inputFilename1 = cfg.getUntrackedParameter< std::string > ("inputROOTFile1");
00057         _inputFilename2 = cfg.getUntrackedParameter< std::string > ("inputROOTFile2");
00058         _inputTreename = cfg.getUntrackedParameter< std::string > ("treeName");
00059         
00060         //output file
00061         _filename = cfg.getUntrackedParameter< std::string > ("outputFile");
00062         
00063         _writeToDB = cfg.getUntrackedParameter< bool > ("writeToDB" );
00064         
00065         const std::vector<std::string>& levels = cfg.getUntrackedParameter< std::vector<std::string> > ("levels");
00066         
00067         _weightBy = cfg.getUntrackedParameter< std::string > ("weightBy");
00068         _setCommonTrackerSystem = cfg.getUntrackedParameter< std::string > ("setCommonTrackerSystem");
00069         _detIdFlag = cfg.getUntrackedParameter< bool > ("detIdFlag");
00070         _detIdFlagFile = cfg.getUntrackedParameter< std::string > ("detIdFlagFile");
00071         _weightById  = cfg.getUntrackedParameter< bool > ("weightById");
00072         _weightByIdFile = cfg.getUntrackedParameter< std::string > ("weightByIdFile");
00073         
00074         //setting the levels being used in the geometry comparator
00075         AlignableObjectId dummy;
00076         edm::LogInfo("TrackerGeometryCompare") << "levels: " << levels.size();
00077         for (unsigned int l = 0; l < levels.size(); ++l){
00078                 theLevels.push_back( dummy.nameToType(levels[l]));
00079                 edm::LogInfo("TrackerGeometryCompare") << "level: " << levels[l];
00080         }
00081         
00082                 
00083         // if want to use, make id cut list
00084         if (_detIdFlag){
00085         ifstream fin;
00086         fin.open( _detIdFlagFile.c_str() );
00087         
00088         while (!fin.eof() && fin.good() ){
00089                         
00090                         uint32_t id;
00091                         fin >> id;
00092                         _detIdFlagVector.push_back(id);
00093         }
00094         fin.close();
00095         }               
00096         
00097         // turn weightByIdFile into weightByIdVector
00098         if (_weightById){
00099                 std::ifstream inFile;
00100                 inFile.open( _weightByIdFile.c_str() );
00101                 int ctr = 0;
00102                 while ( !inFile.eof() ){
00103                         ctr++;
00104                         unsigned int listId;
00105                         inFile >> listId;
00106                         inFile.ignore(256, '\n');
00107                         
00108                         _weightByIdVector.push_back( listId );
00109                 }
00110                 inFile.close();
00111         }
00112         
00113         
00114         //root configuration
00115         _theFile = new TFile(_filename.c_str(),"RECREATE");
00116         _alignTree = new TTree("alignTree","alignTree");//,"id:level:mid:mlevel:sublevel:x:y:z:r:phi:a:b:c:dx:dy:dz:dr:dphi:da:db:dc");
00117         _alignTree->Branch("id", &_id, "id/I");
00118         _alignTree->Branch("level", &_level, "level/I");
00119         _alignTree->Branch("mid", &_mid, "mid/I");
00120         _alignTree->Branch("mlevel", &_mlevel, "mlevel/I");
00121         _alignTree->Branch("sublevel", &_sublevel, "sublevel/I");
00122         _alignTree->Branch("x", &_xVal, "x/F");
00123         _alignTree->Branch("y", &_yVal, "y/F");
00124         _alignTree->Branch("z", &_zVal, "z/F");
00125         _alignTree->Branch("r", &_rVal, "r/F");
00126         _alignTree->Branch("phi", &_phiVal, "phi/F");
00127         _alignTree->Branch("eta", &_etaVal, "eta/F");
00128         _alignTree->Branch("alpha", &_alphaVal, "alpha/F");
00129         _alignTree->Branch("beta", &_betaVal, "beta/F");
00130         _alignTree->Branch("gamma", &_gammaVal, "gamma/F");
00131         _alignTree->Branch("dx", &_dxVal, "dx/F");
00132         _alignTree->Branch("dy", &_dyVal, "dy/F");
00133         _alignTree->Branch("dz", &_dzVal, "dz/F");
00134         _alignTree->Branch("dr", &_drVal, "dr/F");
00135         _alignTree->Branch("dphi", &_dphiVal, "dphi/F");
00136         _alignTree->Branch("dalpha", &_dalphaVal, "dalpha/F");
00137         _alignTree->Branch("dbeta", &_dbetaVal, "dbeta/F");
00138         _alignTree->Branch("dgamma", &_dgammaVal, "dgamma/F");
00139         _alignTree->Branch("du", &_duVal, "du/F");
00140         _alignTree->Branch("dv", &_dvVal, "dv/F");
00141         _alignTree->Branch("dw", &_dwVal, "dw/F");
00142         _alignTree->Branch("da", &_daVal, "da/F");
00143         _alignTree->Branch("db", &_dbVal, "db/F");
00144         _alignTree->Branch("dg", &_dgVal, "dg/F");
00145         _alignTree->Branch("useDetId", &_useDetId, "useDetId/I");
00146         _alignTree->Branch("detDim", &_detDim, "detDim/I");     
00147         _alignTree->Branch("surW", &_surWidth, "surW/F");
00148         _alignTree->Branch("surL", &_surLength, "surL/F");
00149         _alignTree->Branch("surRot", &_surRot, "surRot[9]/D");
00150         _alignTree->Branch("identifiers", &_identifiers, "identifiers[6]/I");
00151 
00152         
00153 }
00154 
00155 void TrackerGeometryCompare::beginJob(){
00156   firstEvent_ = true;
00157 }
00158 
00159 void TrackerGeometryCompare::analyze(const edm::Event&, const edm::EventSetup& iSetup){
00160 
00161   if (firstEvent_) {
00162 
00163         //upload the ROOT geometries
00164         createROOTGeometry(iSetup);
00165         
00166         //set common tracker system first
00167         // if setting the tracker common system
00168         if (_setCommonTrackerSystem != "NONE"){
00169                 setCommonTrackerSystem();
00170         }
00171         
00172         
00173         //compare the goemetries
00174         compareGeometries(referenceTracker,currentTracker);
00175         
00176         //write out ntuple
00177         //might be better to do within output module
00178         _theFile->cd();
00179         _alignTree->Write();
00180         _theFile->Close();
00181         
00182         
00183         if (_writeToDB){
00184                 Alignments* myAlignments = currentTracker->alignments();
00185                 AlignmentErrors* myAlignmentErrors = currentTracker->alignmentErrors();
00186                 
00187                 // 2. Store alignment[Error]s to DB
00188                 edm::Service<cond::service::PoolDBOutputService> poolDbService;
00189                 // Call service
00190                 if( !poolDbService.isAvailable() ) // Die if not available
00191                         throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
00192                 
00193                 poolDbService->writeOne<Alignments>(&(*myAlignments), poolDbService->beginOfTime(), "TrackerAlignmentRcd");
00194                 poolDbService->writeOne<AlignmentErrors>(&(*myAlignmentErrors), poolDbService->beginOfTime(), "TrackerAlignmentErrorRcd");
00195                 
00196         }               
00197 
00198         firstEvent_ = false;
00199   }
00200 }
00201 
00202 void TrackerGeometryCompare::createROOTGeometry(const edm::EventSetup& iSetup){
00203         
00204         int inputRawId1, inputRawId2;
00205         double inputX1, inputY1, inputZ1, inputX2, inputY2, inputZ2;
00206         double inputAlpha1, inputBeta1, inputGamma1, inputAlpha2, inputBeta2, inputGamma2;
00207                 
00208         //declare alignments
00209         Alignments* alignments1 = new Alignments();
00210         AlignmentErrors* alignmentErrors1 = new AlignmentErrors();      
00211         if (_inputFilename1 != "IDEAL"){
00212                 _inputRootFile1 = new TFile(_inputFilename1.c_str());
00213                 TTree* _inputTree1 = (TTree*) _inputRootFile1->Get(_inputTreename.c_str());
00214                 _inputTree1->SetBranchAddress("rawid", &inputRawId1);
00215                 _inputTree1->SetBranchAddress("x", &inputX1);
00216                 _inputTree1->SetBranchAddress("y", &inputY1);
00217                 _inputTree1->SetBranchAddress("z", &inputZ1);
00218                 _inputTree1->SetBranchAddress("alpha", &inputAlpha1);
00219                 _inputTree1->SetBranchAddress("beta", &inputBeta1);
00220                 _inputTree1->SetBranchAddress("gamma", &inputGamma1);
00221                 
00222                 int nEntries1 = _inputTree1->GetEntries();
00223                 //fill alignments
00224                 for (int i = 0; i < nEntries1; ++i){
00225                         
00226                         _inputTree1->GetEntry(i);
00227                         CLHEP::Hep3Vector translation1(inputX1, inputY1, inputZ1);
00228                         CLHEP::HepEulerAngles eulerangles1(inputAlpha1,inputBeta1,inputGamma1);
00229                         uint32_t detid1 = inputRawId1;
00230                         AlignTransform transform1(translation1, eulerangles1, detid1);
00231                         alignments1->m_align.push_back(transform1);
00232                         
00233                         //dummy errors
00234                         CLHEP::HepSymMatrix clhepSymMatrix(3,0);
00235                         AlignTransformError transformError(clhepSymMatrix, detid1);
00236                         alignmentErrors1->m_alignError.push_back(transformError);
00237                 }               
00238                 
00239                 // to get the right order
00240                 std::sort( alignments1->m_align.begin(), alignments1->m_align.end(), lessAlignmentDetId<AlignTransform>() );
00241                 std::sort( alignmentErrors1->m_alignError.begin(), alignmentErrors1->m_alignError.end(), lessAlignmentDetId<AlignTransformError>() );
00242         }
00243         //------------------
00244         Alignments* alignments2 = new Alignments();
00245         AlignmentErrors* alignmentErrors2 = new AlignmentErrors();
00246         if (_inputFilename2 != "IDEAL"){        
00247                 _inputRootFile2 = new TFile(_inputFilename2.c_str());
00248                 TTree* _inputTree2 = (TTree*) _inputRootFile2->Get(_inputTreename.c_str());
00249                 _inputTree2->SetBranchAddress("rawid", &inputRawId2);
00250                 _inputTree2->SetBranchAddress("x", &inputX2);
00251                 _inputTree2->SetBranchAddress("y", &inputY2);
00252                 _inputTree2->SetBranchAddress("z", &inputZ2);
00253                 _inputTree2->SetBranchAddress("alpha", &inputAlpha2);
00254                 _inputTree2->SetBranchAddress("beta", &inputBeta2);
00255                 _inputTree2->SetBranchAddress("gamma", &inputGamma2);
00256                 
00257                 int nEntries2 = _inputTree2->GetEntries();
00258                 //fill alignments
00259                 for (int i = 0; i < nEntries2; ++i){
00260                         
00261                         _inputTree2->GetEntry(i);
00262                         CLHEP::Hep3Vector translation2(inputX2, inputY2, inputZ2);
00263                         CLHEP::HepEulerAngles eulerangles2(inputAlpha2,inputBeta2,inputGamma2);
00264                         uint32_t detid2 = inputRawId2;
00265                         AlignTransform transform2(translation2, eulerangles2, detid2);
00266                         alignments2->m_align.push_back(transform2);
00267                         
00268                         //dummy errors
00269                         CLHEP::HepSymMatrix clhepSymMatrix(3,0);
00270                         AlignTransformError transformError(clhepSymMatrix, detid2);
00271                         alignmentErrors2->m_alignError.push_back(transformError);
00272                 }                       
00273                 
00274                 //to get the right order
00275                 std::sort( alignments2->m_align.begin(), alignments2->m_align.end(), lessAlignmentDetId<AlignTransform>() );
00276                 std::sort( alignmentErrors2->m_alignError.begin(), alignmentErrors2->m_alignError.end(), lessAlignmentDetId<AlignTransformError>() );
00277         }
00278         
00279         //accessing the initial geometry
00280         edm::ESTransientHandle<DDCompactView> cpv;
00281         iSetup.get<IdealGeometryRecord>().get(cpv);
00282         edm::ESHandle<GeometricDet> theGeometricDet;
00283         iSetup.get<IdealGeometryRecord>().get(theGeometricDet);
00284         TrackerGeomBuilderFromGeometricDet trackerBuilder;
00285         
00286         edm::ESHandle<Alignments> globalPositionRcd;
00287         iSetup.get<TrackerDigiGeometryRecord>().getRecord<GlobalPositionRcd>().get(globalPositionRcd);
00288         
00289         //reference tracker
00290         TrackerGeometry* theRefTracker = trackerBuilder.build(&*theGeometricDet); 
00291         if (_inputFilename1 != "IDEAL"){
00292                 GeometryAligner aligner1;
00293                 aligner1.applyAlignments<TrackerGeometry>( &(*theRefTracker), &(*alignments1), &(*alignmentErrors1),
00294                                                                                                   align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Tracker)));
00295         }
00296         referenceTracker = new AlignableTracker(&(*theRefTracker));
00297 
00298         //currernt tracker
00299         TrackerGeometry* theCurTracker = trackerBuilder.build(&*theGeometricDet); 
00300         if (_inputFilename2 != "IDEAL"){
00301                 GeometryAligner aligner2;
00302                 aligner2.applyAlignments<TrackerGeometry>( &(*theCurTracker), &(*alignments2), &(*alignmentErrors2),
00303                                                                                                   align::DetectorGlobalPosition(*globalPositionRcd, DetId(DetId::Tracker)));
00304         }
00305         currentTracker = new AlignableTracker(&(*theCurTracker));
00306         
00307         
00308 }
00309 
00310 void TrackerGeometryCompare::compareGeometries(Alignable* refAli, Alignable* curAli){
00311         
00312         const std::vector<Alignable*>& refComp = refAli->components();
00313         const std::vector<Alignable*>& curComp = curAli->components();
00314         
00315         unsigned int nComp = refComp.size();
00316         //only perform for designate levels
00317         bool useLevel = false;
00318         for (unsigned int i = 0; i < theLevels.size(); ++i){
00319                 if (refAli->alignableObjectId() == theLevels[i]) useLevel = true;
00320         }
00321         
00322         //another added level for difference between det and detunit
00323         //if ((refAli->alignableObjectId()==2)&&(nComp == 1)) useLevel = false;
00324         
00325         //coordinate matching, etc etc
00326         if (useLevel){
00327                 //std::cout << "ali identifiers: " << refAli->id() << ", " << refAli->alignableObjectId() << std::endl;
00328                 //std::cout << "diff pos" << (refAli->globalPosition() - curAli->globalPosition()) << std::endl;
00329                 //std::cout <<"z";
00330 
00331                 CLHEP::Hep3Vector Rtotal, Wtotal, lRtotal, lWtotal;
00332 
00333                 Rtotal.set(0.,0.,0.); Wtotal.set(0.,0.,0.);
00334                 lRtotal.set(0.,0.,0.); lWtotal.set(0.,0.,0.);
00335                 for (int i = 0; i < 100; i++){
00336                         AlgebraicVector diff = align::diffAlignables(refAli,curAli, _weightBy, _weightById, _weightByIdVector);
00337                         CLHEP::Hep3Vector dR(diff[0],diff[1],diff[2]);
00338                         Rtotal+=dR;
00339                         CLHEP::Hep3Vector dW(diff[3],diff[4],diff[5]);
00340                         CLHEP::HepRotation rot(Wtotal.unit(),Wtotal.mag());
00341                         CLHEP::HepRotation drot(dW.unit(),dW.mag());
00342                         rot*=drot;
00343                         Wtotal.set(rot.axis().x()*rot.delta(), rot.axis().y()*rot.delta(), rot.axis().z()*rot.delta());
00344                         // local coordinates
00345                         lRtotal.set(diff[6],diff[7],diff[8]);
00346                         lWtotal.set(diff[9],diff[10],diff[11]);
00347                         //std::cout << "a";
00348                         //if (refAli->alignableObjectId() == 1) std::cout << "DIFF: " << diff << std::endl;
00349                         align::moveAlignable(curAli, diff);
00350                         float tolerance = 1e-7;
00351                         AlgebraicVector check = align::diffAlignables(refAli,curAli, _weightBy, _weightById, _weightByIdVector);
00352                         align::GlobalVector checkR(check[0],check[1],check[2]);
00353                         align::GlobalVector checkW(check[3],check[4],check[5]);
00354                         DetId detid(refAli->id());
00355                         if ((checkR.mag() > tolerance)||(checkW.mag() > tolerance)){
00356                                 edm::LogInfo("TrackerGeometryCompare") << "Tolerance Exceeded!(alObjId: " << refAli->alignableObjectId()
00357                                 << ", rawId: " << refAli->geomDetId().rawId()
00358                                 << ", subdetId: "<< detid.subdetId() << "): " << diff;
00359                                 throw cms::Exception("Tolerance in TrackerGeometryCompare exceeded");
00360                         }
00361                         else{
00362                                 break;
00363                         }
00364                 }
00365                 
00366                 AlgebraicVector TRtot(12);
00367                 // global 
00368                 TRtot(1) = Rtotal.x(); TRtot(2) = Rtotal.y(); TRtot(3) = Rtotal.z();
00369                 TRtot(4) = Wtotal.x(); TRtot(5) = Wtotal.y(); TRtot(6) = Wtotal.z();
00370                 // local
00371                 TRtot(7) = lRtotal.x(); TRtot(8) = lRtotal.y(); TRtot(9) = lRtotal.z();
00372                 TRtot(10) = lWtotal.x(); TRtot(11) = lWtotal.y(); TRtot(12) = lWtotal.z();
00373                 fillTree(refAli, TRtot);
00374         }
00375 
00376         //another added level for difference between det and detunit
00377         for (unsigned int i = 0; i < nComp; ++i) 
00378           compareGeometries(refComp[i],curComp[i]);     
00379 }
00380 
00381 void TrackerGeometryCompare::setCommonTrackerSystem(){
00382 
00383         edm::LogInfo("TrackerGeometryCompare") << "Setting Common Tracker System....";
00384         
00385         AlignableObjectId dummy;
00386         _commonTrackerLevel = dummy.nameToType(_setCommonTrackerSystem);
00387                 
00388         diffCommonTrackerSystem(referenceTracker, currentTracker);
00389         
00390         align::EulerAngles dOmega(3); dOmega[0] = _TrackerCommonR.x() ; dOmega[1] = _TrackerCommonR.y(); dOmega[2] = _TrackerCommonR.z();
00391         align::RotationType rot = align::toMatrix( dOmega );
00392         align::GlobalVector theR = _TrackerCommonT;
00393         
00394         std::cout << "what we get from overlaying the pixels..." << theR << ", " << rot << std::endl;
00395         
00396         //transform to the Tracker System
00397         align::PositionType trackerCM = currentTracker->globalPosition();
00398         align::GlobalVector cmDiff( trackerCM.x()-_TrackerCommonCM.x(), trackerCM.y()-_TrackerCommonCM.y(), trackerCM.z()-_TrackerCommonCM.z() );
00399         
00400         std::cout << "Pixel CM: " << _TrackerCommonCM << ", tracker CM: " << trackerCM << std::endl;
00401         
00402         //adjust translational difference factoring in different rotational CM
00403         //needed because rotateInGlobalFrame is about CM of alignable, not Tracker
00404         align::GlobalVector::BasicVectorType lpvgf = cmDiff.basicVector();
00405         align::GlobalVector moveV( rot.multiplyInverse(lpvgf) - lpvgf);
00406         align::GlobalVector theRprime(theR + moveV);
00407         
00408         AlgebraicVector TrackerCommonTR(6);
00409         TrackerCommonTR(1) = theRprime.x(); TrackerCommonTR(2) = theRprime.y(); TrackerCommonTR(3) = theRprime.z();
00410         TrackerCommonTR(4) = _TrackerCommonR.x(); TrackerCommonTR(5) = _TrackerCommonR.y(); TrackerCommonTR(6) = _TrackerCommonR.z();
00411         
00412         std::cout << "and after the transformation: " << TrackerCommonTR << std::endl;
00413         
00414         align::moveAlignable(currentTracker, TrackerCommonTR );
00415         
00416 }
00417 
00418 void TrackerGeometryCompare::diffCommonTrackerSystem(Alignable *refAli, Alignable *curAli){
00419         
00420         const std::vector<Alignable*>& refComp = refAli->components();
00421         const std::vector<Alignable*>& curComp = curAli->components();
00422         
00423         unsigned int nComp = refComp.size();
00424         //only perform for designate levels
00425         bool useLevel = false;
00426         if (refAli->alignableObjectId() == _commonTrackerLevel) useLevel = true;
00427         
00428         //useLevel = false;
00429         if (useLevel){
00430                 CLHEP::Hep3Vector Rtotal, Wtotal;
00431                 Rtotal.set(0.,0.,0.); Wtotal.set(0.,0.,0.);
00432                 
00433                 AlgebraicVector diff = align::diffAlignables(refAli,curAli, _weightBy, _weightById, _weightByIdVector);
00434                 CLHEP::Hep3Vector dR(diff[0],diff[1],diff[2]);
00435                 Rtotal+=dR;
00436                 CLHEP::Hep3Vector dW(diff[3],diff[4],diff[5]);
00437                 CLHEP::HepRotation rot(Wtotal.unit(),Wtotal.mag());
00438                 CLHEP::HepRotation drot(dW.unit(),dW.mag());
00439                 rot*=drot;
00440                 Wtotal.set(rot.axis().x()*rot.delta(), rot.axis().y()*rot.delta(), rot.axis().z()*rot.delta());
00441                 /*
00442                  //std::cout << "a";
00443                  //if (refAli->alignableObjectId() == 1) std::cout << "DIFF: " << diff << std::endl;
00444                  align::moveAlignable(curAli, diff);
00445                  float tolerance = 1e-7;
00446                  AlgebraicVector check = align::diffAlignables(refAli,curAli, _weightBy, _weightById, _weightByIdVector);
00447                  align::GlobalVector checkR(check[0],check[1],check[2]);
00448                  align::GlobalVector checkW(check[3],check[4],check[5]);
00449                  DetId detid(refAli->id());
00450                  if ((checkR.mag() > tolerance)||(checkW.mag() > tolerance)){
00451                  edm::LogInfo("TrackerGeometryCompare") << "Tolerance Exceeded!(alObjId: " << refAli->alignableObjectId()
00452                  << ", rawId: " << refAli->geomDetId().rawId()
00453                  << ", subdetId: "<< detid.subdetId() << "): " << diff;
00454                  }
00455                  else{
00456                  break;
00457                  }
00458                  }
00459                  */
00460                 
00461                 //_TrackerCommonT.set(Rtotal.x(), Rtotal.y(), Rtotal.z());
00462                 _TrackerCommonT = align::GlobalVector(Rtotal.x(), Rtotal.y(), Rtotal.z());
00463                 _TrackerCommonR = align::GlobalVector(Wtotal.x(), Wtotal.y(), Wtotal.z());
00464                 _TrackerCommonCM = curAli->globalPosition();
00465                 //_TrackerCommonTR(1) = Rtotal.x(); _TrackerCommonTR(2) = Rtotal.y(); _TrackerCommonTR(3) = Rtotal.z();
00466                 //_TrackerCommonTR(4) = Wtotal.x(); _TrackerCommonTR(5) = Wtotal.y(); _TrackerCommonTR(6) = Wtotal.z();
00467                 
00468                 
00469         }
00470         else{
00471                 for (unsigned int i = 0; i < nComp; ++i) diffCommonTrackerSystem(refComp[i],curComp[i]);
00472         }
00473         
00474         
00475 }
00476 
00477 void TrackerGeometryCompare::fillTree(Alignable *refAli, AlgebraicVector diff){
00478         
00479         
00480         _id = refAli->id();
00481         _level = refAli->alignableObjectId();
00482         //need if ali has no mother
00483         if (refAli->mother()){
00484                 _mid = refAli->mother()->geomDetId().rawId();
00485                 _mlevel = refAli->mother()->alignableObjectId();
00486         }
00487         else{
00488                 _mid = -1;
00489                 _mlevel = -1;
00490         }
00491         DetId detid(_id);
00492         _sublevel = detid.subdetId();
00493         fillIdentifiers( _sublevel, _id );
00494         _xVal = refAli->globalPosition().x();
00495         _yVal = refAli->globalPosition().y();
00496         _zVal = refAli->globalPosition().z();
00497         align::GlobalVector vec(_xVal,_yVal,_zVal);
00498         _rVal = vec.perp();
00499         _phiVal = vec.phi();
00500         _etaVal = vec.eta();
00501         align::RotationType rot = refAli->globalRotation();
00502         align::EulerAngles eulerAngles = align::toAngles(rot);
00503         _alphaVal = eulerAngles[0];
00504         _betaVal = eulerAngles[1];
00505         _gammaVal = eulerAngles[2];
00506         // global
00507         _dxVal = diff[0];
00508         _dyVal = diff[1];
00509         _dzVal = diff[2];
00510         // local
00511         _duVal = diff[6];
00512         _dvVal = diff[7];
00513         _dwVal = diff[8];
00514         //...TODO...
00515         align::GlobalVector g(_dxVal, _dyVal, _dzVal);
00516         align::LocalVector l = refAli->surface().toLocal(g);
00517         //getting dR and dPhi
00518         align::GlobalVector vRef(_xVal,_yVal,_zVal);
00519         align::GlobalVector vCur(_xVal + _dxVal, _yVal + _dyVal, _zVal + _dzVal);
00520         _drVal = vCur.perp() - vRef.perp();
00521         _dphiVal = vCur.phi() - vRef.phi();
00522         // global
00523         _dalphaVal = diff[3];
00524         _dbetaVal = diff[4];
00525         _dgammaVal = diff[5];
00526         // local
00527         _daVal = diff[9];
00528         _dbVal = diff[10];
00529         _dgVal = diff[11];
00530         
00531         //detIdFlag
00532         if (refAli->alignableObjectId() == align::AlignableDetUnit){
00533                 if (_detIdFlag){
00534                         if ((passIdCut(refAli->id()))||(passIdCut(refAli->mother()->id()))){
00535                                 _useDetId = 1;
00536                         }
00537                         else{
00538                                 _useDetId = 0;
00539                         }
00540                 }
00541         }
00542         // det module dimension
00543         if (refAli->alignableObjectId() == align::AlignableDetUnit){
00544                 if (refAli->mother()->alignableObjectId() != align::AlignableDet) _detDim = 1;
00545                 else if (refAli->mother()->alignableObjectId() == align::AlignableDet) _detDim = 2;
00546         }
00547         else _detDim = 0;
00548         
00549         
00550         
00551         
00552         _surWidth = refAli->surface().width();
00553         _surLength = refAli->surface().length();
00554         align::RotationType rt = refAli->globalRotation();
00555         _surRot[0] = rt.xx(); _surRot[1] = rt.xy(); _surRot[2] = rt.xz();
00556         _surRot[3] = rt.yx(); _surRot[4] = rt.yy(); _surRot[5] = rt.yz();
00557         _surRot[6] = rt.zx(); _surRot[7] = rt.zy(); _surRot[8] = rt.zz();
00558         
00559         
00560         //Fill
00561         _alignTree->Fill();
00562         
00563 }
00564 
00565 void TrackerGeometryCompare::surveyToTracker(AlignableTracker* ali, Alignments* alignVals, AlignmentErrors* alignErrors){
00566         
00567         //getting the right alignables for the alignment record
00568         std::vector<Alignable*> detPB = ali->pixelHalfBarrelGeomDets();
00569         std::vector<Alignable*> detPEC = ali->pixelEndcapGeomDets();
00570         std::vector<Alignable*> detTIB = ali->innerBarrelGeomDets();
00571         std::vector<Alignable*> detTID = ali->TIDGeomDets();
00572         std::vector<Alignable*> detTOB = ali->outerBarrelGeomDets();
00573         std::vector<Alignable*> detTEC = ali->endcapGeomDets();
00574         
00575         std::vector<Alignable*> allGeomDets;
00576         std::copy(detPB.begin(), detPB.end(), std::back_inserter(allGeomDets));
00577         std::copy(detPEC.begin(), detPEC.end(), std::back_inserter(allGeomDets));
00578         std::copy(detTIB.begin(), detTIB.end(), std::back_inserter(allGeomDets));
00579         std::copy(detTID.begin(), detTID.end(), std::back_inserter(allGeomDets));
00580         std::copy(detTOB.begin(), detTOB.end(), std::back_inserter(allGeomDets));
00581         std::copy(detTEC.begin(), detTEC.end(), std::back_inserter(allGeomDets));
00582         
00583         std::vector<Alignable*> rcdAlis;
00584         for (std::vector<Alignable*>::iterator i = allGeomDets.begin(); i!= allGeomDets.end(); i++){
00585                 if ((*i)->components().size() == 1){
00586                         rcdAlis.push_back((*i));
00587                 }
00588                 else if ((*i)->components().size() > 1){
00589                         rcdAlis.push_back((*i));
00590                         std::vector<Alignable*> comp = (*i)->components();
00591                         for (std::vector<Alignable*>::iterator j = comp.begin(); j != comp.end(); j++){
00592                                 rcdAlis.push_back((*j));
00593                         }
00594                 }
00595         }
00596         
00597         //turning them into alignments
00598         for(std::vector<Alignable*>::iterator k = rcdAlis.begin(); k != rcdAlis.end(); k++){
00599                 
00600                 const SurveyDet* surveyInfo = (*k)->survey();
00601                 align::PositionType pos(surveyInfo->position());
00602                 align::RotationType rot(surveyInfo->rotation());
00603                 CLHEP::Hep3Vector clhepVector(pos.x(),pos.y(),pos.z());
00604                 CLHEP::HepRotation clhepRotation( CLHEP::HepRep3x3(rot.xx(),rot.xy(),rot.xz(),rot.yx(),rot.yy(),rot.yz(),rot.zx(),rot.zy(),rot.zz()));
00605                 AlignTransform transform(clhepVector, clhepRotation, (*k)->id());
00606                 AlignTransformError transformError(CLHEP::HepSymMatrix(3,1), (*k)->id());
00607                 alignVals->m_align.push_back(transform);
00608                 alignErrors->m_alignError.push_back(transformError);
00609         }
00610         
00611         //to get the right order
00612         std::sort( alignVals->m_align.begin(), alignVals->m_align.end(), lessAlignmentDetId<AlignTransform>() );
00613         std::sort( alignErrors->m_alignError.begin(), alignErrors->m_alignError.end(), lessAlignmentDetId<AlignTransformError>() );
00614         
00615 }
00616 
00617 void TrackerGeometryCompare::addSurveyInfo(Alignable* ali){
00618         
00619         const std::vector<Alignable*>& comp = ali->components();
00620         
00621         unsigned int nComp = comp.size();
00622         
00623         for (unsigned int i = 0; i < nComp; ++i) addSurveyInfo(comp[i]);
00624         
00625         const SurveyError& error = theSurveyErrors->m_surveyErrors[theSurveyIndex];
00626         
00627         if ( ali->geomDetId().rawId() != error.rawId() ||
00628                 ali->alignableObjectId() != error.structureType() )
00629         {
00630                 throw cms::Exception("DatabaseError")
00631                 << "Error reading survey info from DB. Mismatched id!";
00632         }
00633         
00634         const CLHEP::Hep3Vector&  pos = theSurveyValues->m_align[theSurveyIndex].translation();
00635         const CLHEP::HepRotation& rot = theSurveyValues->m_align[theSurveyIndex].rotation();
00636         
00637         AlignableSurface surf( align::PositionType( pos.x(), pos.y(), pos.z() ),
00638                                                   align::RotationType( rot.xx(), rot.xy(), rot.xz(),
00639                                                                                           rot.yx(), rot.yy(), rot.yz(),
00640                                                                                           rot.zx(), rot.zy(), rot.zz() ) );
00641         
00642         surf.setWidth( ali->surface().width() );
00643         surf.setLength( ali->surface().length() );
00644         
00645         ali->setSurvey( new SurveyDet( surf, error.matrix() ) );
00646         
00647         ++theSurveyIndex;
00648         
00649 }
00650 
00651 bool TrackerGeometryCompare::passIdCut( uint32_t id ){
00652         
00653         bool pass = false;
00654         int nEntries = _detIdFlagVector.size();
00655         
00656         for (int i = 0; i < nEntries; i++){
00657                 if (_detIdFlagVector[i] == id) pass = true;
00658         }
00659         
00660         return pass;
00661         
00662 }
00663 
00664 void TrackerGeometryCompare::fillIdentifiers( int subdetlevel, int rawid ){
00665         
00666         
00667         switch( subdetlevel ){
00668                         
00669                 case 1:
00670                 {
00671                         PXBDetId pxbid( rawid );
00672                         _identifiers[0] = pxbid.module();
00673                         _identifiers[1] = pxbid.ladder();
00674                         _identifiers[2] = pxbid.layer();
00675                         _identifiers[3] = 999;
00676                         _identifiers[4] = 999;
00677                         _identifiers[5] = 999;
00678                         break;
00679                 }
00680                 case 2:
00681                 {
00682                         PXFDetId pxfid( rawid );
00683                         _identifiers[0] = pxfid.module();
00684                         _identifiers[1] = pxfid.panel();
00685                         _identifiers[2] = pxfid.blade();
00686                         _identifiers[3] = pxfid.disk();
00687                         _identifiers[4] = pxfid.side();
00688                         _identifiers[5] = 999;
00689                         break;
00690                 }
00691                 case 3:
00692                 {
00693                         TIBDetId tibid( rawid );
00694                         _identifiers[0] = tibid.module();
00695                         _identifiers[1] = tibid.string()[0];
00696                         _identifiers[2] = tibid.string()[1];
00697                         _identifiers[3] = tibid.string()[2];
00698                         _identifiers[4] = tibid.layer();
00699                         _identifiers[5] = 999;
00700                         break;
00701                 }
00702                 case 4: 
00703                 {
00704                         TIDDetId tidid( rawid );
00705                         _identifiers[0] = tidid.module()[0];
00706                         _identifiers[1] = tidid.module()[1];
00707                         _identifiers[2] = tidid.ring();
00708                         _identifiers[3] = tidid.wheel();
00709                         _identifiers[4] = tidid.side();
00710                         _identifiers[5] = 999;
00711                         break;
00712                 }
00713                 case 5: 
00714                 {
00715                         TOBDetId tobid( rawid );
00716                         _identifiers[0] = tobid.module();
00717                         _identifiers[1] = tobid.rod()[0];
00718                         _identifiers[2] = tobid.rod()[1];
00719                         _identifiers[3] = tobid.layer();
00720                         _identifiers[4] = 999;
00721                         _identifiers[5] = 999;
00722                         break;
00723                 }
00724                 case 6: 
00725                 {
00726                         TECDetId tecid( rawid );
00727                         _identifiers[0] = tecid.module();
00728                         _identifiers[1] = tecid.ring();
00729                         _identifiers[2] = tecid.petal()[0];
00730                         _identifiers[3] = tecid.petal()[1];
00731                         _identifiers[4] = tecid.wheel();
00732                         _identifiers[5] = tecid.side();
00733                         break;
00734                 }
00735                 default:
00736                 {
00737                         std::cout << "Error: bad subdetid!!" << std::endl;
00738                         break;
00739                 }
00740                         
00741         }
00742 }
00743 
00744 
00745 DEFINE_FWK_MODULE(TrackerGeometryCompare);