CMS 3D CMS Logo

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