#include "TpcAlignmentManager.h" #include "TpcAlignmentHelper.h" #include #include #include #include #include //#include #include "TLorentzVector.h" using namespace std; TpcAlignmentManager* TpcAlignmentManager::inst=NULL; TpcAlignmentManager::~TpcAlignmentManager(){ std::map::iterator itT=fTransformations.begin(); while(itT!=fTransformations.end()){ delete itT->second; ++itT; } fTransformations.clear(); } void TpcAlignmentManager::read(TString filename){ clear(); transformationFileName=filename; ifstream input(filename.Data()); if(!input){ cerr << "unable to open input file: " << filename << " --bailing out! \n"; throw; } string line; while(getline(input,line)){ istringstream lineStream(line); string word; lineStream>>word; // boost::algorithm::trim(word); if(word=="#"||word==""){ continue; }else if(word=="detName"){ string id; lineStream>>id; readDet(id,input); } } if(detNames.size()==0){ cerr << "empty alignment, terminating" < rot; while(getline(input,line)){ istringstream lineStream(line); string word; lineStream>>word; // boost::algorithm::trim(word); if(word=="#"||word==""){ continue; } if(word=="detName"){ setConv(detName, trans,rot,angleMode); string name; lineStream>>name; readDet(name,input); return; } if(word=="translation"){ double x,y,z; lineStream>>x>>y>>z; if(!(lineStream.good() or lineStream.eof())){ std::cerr<<"error reading translation for det:"<>phi>>theta>>psi; if(!(lineStream.good() or lineStream.eof())){ std::cerr<<"error reading rotation for det:"<::iterator itT=fTransformations.begin(); while(itT!=fTransformations.end()){ delete itT->second; ++itT; } fTransformations.clear(); fAngleMode.clear(); fAngles.clear(); detNames.clear(); } void TpcAlignmentManager::getPlaneDef(TString detName, TVector3 &o, TVector3 &u, TVector3 &v){ if(fTransformations.count(detName)){ o=TVector3(fTransformations[detName]->GetTranslation()); TVector3 u_l(1,0,0); TVector3 v_l(0,1,0); u=localToMasterVect(detName,u_l); v=localToMasterVect(detName,v_l); return; }else{ quit(detName); } } TGeoCombiTrans* TpcAlignmentManager::getTransformation(TString detName){ if(!initialized){ return new TGeoCombiTrans(*(new TGeoTranslation(0,0,0)),*(new TGeoRotation())); } if(fTransformations.count(detName)){ return fTransformations[detName]; }else{ quit(detName); } } void TpcAlignmentManager::getEulerAngles(TString detName, double &phi, double &theta, double &psi){ if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ fTransformations[detName]->GetRotation()->GetAngles(phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } } void TpcAlignmentManager::getEulerAnglesZYX(TString detName, double &phi, double &theta, double &psi){ TpcAlignmentHelper alHelper; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ alHelper.getAnglesZYX(fTransformations[detName]->GetRotation(),phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } } double TpcAlignmentManager::getPhi(TString detName) { double phi,theta,psi; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ fTransformations[detName]->GetRotation()->GetAngles(phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } return phi; } double TpcAlignmentManager::getPhiXYZ(TString detName) { double phi,theta,psi; TpcAlignmentHelper alHelper; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ alHelper.getAnglesZYX(fTransformations[detName]->GetRotation(),phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } return phi; } double TpcAlignmentManager::getTheta(TString detName) { double phi,theta,psi; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ fTransformations[detName]->GetRotation()->GetAngles(phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } return theta; } double TpcAlignmentManager::getThetaXYZ(TString detName) { double phi,theta,psi; TpcAlignmentHelper alHelper; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ alHelper.getAnglesZYX(fTransformations[detName]->GetRotation(),phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } return theta; } double TpcAlignmentManager::getPsi(TString detName) { double phi,theta,psi; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ fTransformations[detName]->GetRotation()->GetAngles(phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } return psi; } double TpcAlignmentManager::getPsiXYZ(TString detName) { double phi,theta,psi; TpcAlignmentHelper alHelper; if(!initialized){ phi=0;psi=0;theta=0; } if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ phi=0;psi=0;theta=0; }else{ alHelper.getAnglesZYX(fTransformations[detName]->GetRotation(),phi,theta,psi); } }else if(!initialized){ phi=0;psi=0;theta=0; }else { quit(detName); } return psi; } // TVector3 TpcAlignmentManager::getTranslation(TString detName) { if(fTransformations.count(detName)) { TGeoCombiTrans *tr=fTransformations[detName]; TVector3 trans(tr->GetTranslation()[0],tr->GetTranslation()[1],tr->GetTranslation()[2]); return trans; }else if(!initialized){ return TVector3(0,0,0); } else quit(detName); } void TpcAlignmentManager::setEulerAngles(TString detName, double phi, double theta, double psi, int mode) { ostringstream name; name<<"rotation_"< rotMatrix=alHelp.getRotationMatrixZYX(phi,theta,psi); grot->SetMatrix(rotMatrix.data()); fAngleMode[detName]=2; } fTransformations[detName]->SetRotation(grot); if(fAngles.count(detName)){ if(fAngles[detName].size()==3){ fAngles[detName][0]=phi; fAngles[detName][1]=theta; fAngles[detName][2]=psi; } } delete grot; } else{ std::cerr << "TpcAlignmentManager::setEulerAngles called for not existing detector:" <SetTranslation(newTrans.X(),newTrans.Y(),newTrans.Z()); }else { std::cerr << "TpcAlignmentManager::setSetTranslation called for not existing detector:" < rot, int angleMode){ if(fTransformations.count(detName)>0){ std::cerr<<"WARNING TpcAlignmentManager::setConv for detName "< rotMatrix=alHelp.getRotationMatrixZYX(rot[0],rot[1],rot[2]); grot->SetMatrix(rotMatrix.data()); } fAngleMode[detName]=angleMode; fTransformations[detName]=new TGeoCombiTrans(*trans,*grot); fAngles[detName]=rot; } TVector3 TpcAlignmentManager::localToMaster(TString detName, TVector3 uvw){ if(fTransformations.count(detName)){ vector uvw_=tVector3ToStlVector(uvw); double xyz[3]; fTransformations[detName]->LocalToMaster(&uvw_[0],xyz); return TVector3(xyz); }else if(!initialized){ return uvw; }else{ quit(detName); } } TMatrixD TpcAlignmentManager::localToMaster(TString detName, TMatrixD covUVW){ if(covUVW.GetNcols()!=3||covUVW.GetNrows()!=3){ std::cerr << "TpcAlignmentManager: trying to transform covariance of invalid dimension " < abort"<GetRotationMatrix()); // TMatrixD ret(3,3); TMatrixD rotT(3,3); rotT.Transpose(rot); TMatrixD ret=(rot*covUVW)*rotT; return ret; }else if(!initialized){ return covUVW; }else{ quit(detName); } } void TpcAlignmentManager::localToMaster(TString detName, TVector3 uvw,TVector3 &xyz){ xyz=localToMaster(detName, uvw); } void TpcAlignmentManager::localToMaster(TString detName, TMatrixD covUVW, TMatrixD &covXYZ){ covXYZ=localToMaster(detName, covUVW); } TVector3 TpcAlignmentManager::localToMasterVect(TString detName, TVector3 uvw){ if(fTransformations.count(detName)){ vector uvw_=tVector3ToStlVector(uvw); double xyz[3]; fTransformations[detName]->LocalToMasterVect(&uvw_[0],xyz); return TVector3(xyz); }else if(!initialized){ return uvw; }else{ quit(detName); } } void TpcAlignmentManager::localToMasterVect(TString detName, TVector3 uvw,TVector3 &xyz){ xyz=localToMasterVect(detName, uvw); } TVector3 TpcAlignmentManager::masterToLocal(TString detName, TVector3 xyz){ if(fTransformations.count(detName)){ vector xyz_=tVector3ToStlVector(xyz); double uvw[3]; fTransformations[detName]->MasterToLocal(&xyz_[0],uvw); return TVector3(uvw); }else if(!initialized){ return xyz; }else{ quit(detName); } } TMatrixD TpcAlignmentManager::masterToLocal(TString detName, TMatrixD covXYZ){ if(covXYZ.GetNcols()!=3||covXYZ.GetNrows()!=3){ std::cerr << "TpcAlignmentManager: trying to transform covariance of invalid dimension " < abort"<GetRotationMatrix()); TMatrixD ret(3,3); TMatrixD rotT(3,3); rotT.Transpose(rot); ret=rotT*covXYZ*rot; return ret; }else if(!initialized){ return covXYZ; }else{ quit(detName); } } void TpcAlignmentManager::masterToLocal(TString detName, TVector3 xyz,TVector3 &uvw){ uvw=localToMaster(detName, xyz); } void TpcAlignmentManager::masterToLocal(TString detName, TMatrixD covXYZ, TMatrixD &covUVW){ covUVW=localToMaster(detName, covXYZ); } TVector3 TpcAlignmentManager::masterToLocalVect(TString detName, TVector3 xyz){ if(fTransformations.count(detName)){ vector xyz_=tVector3ToStlVector(xyz); double uvw[3]; fTransformations[detName]->MasterToLocalVect(&xyz_[0],uvw); return TVector3(uvw); }else if(!initialized){ return xyz; }else{ quit(detName); } } void TpcAlignmentManager::masterToLocalVect(TString detName, TVector3 xyz,TVector3 &uvw){ uvw=localToMasterVect(detName, xyz); } void TpcAlignmentManager::write(TString outfilename){ if(initialized){ ofstream outfile(outfilename.Data()); time_t rawtime; struct tm * timeinfo; time(&rawtime); timeinfo=localtime(&rawtime); outfile<<"#alignment file written at "<GetTranslation()[0]<<"\t" <GetTranslation()[1]<<"\t" <GetTranslation()[2]<<" #"; outfile.unsetf ( std::ios::floatfield); outfile<GetTranslation()[0]<<"\t" <GetTranslation()[1]<<"\t" <GetTranslation()[2]<GetTranslation()[0]<<"\t " <GetTranslation()[1]<<"\t " <GetTranslation()[2]<matrix){ if(fTransformations.count(detName)){ if(fTransformations[detName]->GetRotation()==0){ TGeoRotation* rot = new TGeoRotation(); rot->SetMatrix(matrix.data()); fTransformations[detName]->SetRotation(rot); }else{ fTransformations[detName]->GetRotation()->SetMatrix(matrix.data()); } double a=0; double b=0; double c=0; if(fTransformations[detName]->GetRotation()==0){ return; } if(fTransformations[detName]->GetRotation()->IsRotation()){ fTransformations[detName]->GetRotation()->GetAngles(a,b,c); if(fAngleMode[detName]==2){ getEulerAnglesZYX(detName,a,b,c); } } fAngles[detName][0]=a; fAngles[detName][1]=b; fAngles[detName][2]=c; }else{ std::cerr << "TpcAlignmentManager::setRotationMatrixcalled for not existing detector:" <