Just something left from v2...now clean
[u/mrichter/AliRoot.git] / TPC / AliTPCAlign.cxx
CommitLineData
2574314d 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/*
17$Log$
68e43693 18Revision 1.3 2007/10/22 14:20:25 kowal2
19Hardwired "ideal" coordinates replaced with those from the DCDB.
20Corrected bug in SetDebug method
21
3e3d6422 22Revision 1.2 2007/10/03 09:27:50 marian
23Extra semicolon removed (Marian)
24
dd89d4d2 25Revision 1.1 2007/10/01 14:12:45 kowal2
26Class creating the aligmnent object fro the surveyor measurements.
27
2574314d 28*/
29
30//
31// Creates the TPC align object
32//
33
34#include "AliTPCAlign.h"
68e43693 35#include "AliSurveyPoint.h"
36#include "AliSurveyPoint.h"
2574314d 37//
38#include "TROOT.h"
39#include "Riostream.h"
40#include "TFile.h"
41#include "TMath.h"
42#include "TSystem.h"
43#include "AliSurveyObj.h"
68e43693 44#include "AliSurveyPoint.h"
2574314d 45#include "AliAlignObjParams.h"
46#include "AliCDBStorage.h"
47#include <TClonesArray.h>
48#include <TFile.h>
49#include "AliLog.h"
50#include "AliCDBManager.h"
51
dd89d4d2 52ClassImp(AliTPCAlign)
2574314d 53
54AliTPCAlign::AliTPCAlign() :
55 TObject(),
56 fFileLoc(0x0),
57 fFileGlob(0x0),
58 fTPCAlignObj(0x0),
59 fX(),
60 fA(),
61 fY(),
62 fDebug(0)
63{
64 //
65 // default constructor
66 //
67}
68//________________________________________________________________________
69AliTPCAlign::AliTPCAlign(Int_t reportloc, Int_t reportglob) :
70 TObject(),
71 fFileLoc(0x0),
72 fFileGlob(0x0),
73 fTPCAlignObj(0x0),
74 fX(6,1),
75 fA(24,6),
76 fY(24,1),
77 fDebug(0)
78{
79 //
80 // constructor - defines data files
81 //
82 fFileLoc = new Char_t[80];
83 fFileGlob = new Char_t[80];
84 Char_t path[50];
85 sprintf(path,gSystem->Getenv("ALICE_ROOT"));
86 //
87 sprintf(fFileLoc,"%s/TPC/Survey_%d_TPC.txt",path,reportloc);
88 sprintf(fFileGlob,"%s/TPC/Survey_%d_TPC.txt",path,reportglob);
89 //
90
91}
92//_________________________________________________________________________
93AliTPCAlign::AliTPCAlign(const AliTPCAlign &align) :
94 TObject(),
95 fFileLoc(0x0),
96 fFileGlob(0x0),
97 fTPCAlignObj(0x0),
98 fX(),
99 fA(),
100 fY(),
101 fDebug(0)
102{
103 //
104 // copy constructor - dummy
105 //
106 fDebug = align.fDebug;
107}
108//__________________________________________________________________________
109AliTPCAlign & AliTPCAlign::operator =(const AliTPCAlign & align)
110{
111 //
112 // assignment operator - dummy
113 //
114 fDebug=align.fDebug;
115 return (*this);
116}
117
118//__________________________________________________________________________
119AliTPCAlign::~AliTPCAlign(){
120 //
121 // destructor
122 //
123 if(fTPCAlignObj) delete fTPCAlignObj;
124}
125//__________________________________________________________________________
126Bool_t AliTPCAlign::LoadSurveyData(){
127 //
128 // for a time being it loads from the local file the surveyed point
129 // and has the ideal points hardwired. I am waiting until Ricardo
130 // completes his job
131 //
132
133 AliSurveyObj * s1 = new AliSurveyObj();
134 s1->FillFromLocalFile(fFileGlob);
135 //
136 Int_t numberPoints = 8;
137 //
138 TString pointNames[8] = {"T1Final_R04241","T1Final_R05241","T1Final_R06241",
139 "T1Final_R07241","T1Final_R08241","T1Final_R10241",
140 "T1Final_R11241","T1Final_R12241"};
141 //
142 Float_t surveyedPoints[8][3];
143 AliSurveyPoint *currPoint;
144 //
145 for(Int_t i=0;i<numberPoints;i++){
146 currPoint=0;
147 currPoint = (AliSurveyPoint *) s1->GetData()->FindObject(pointNames[i]);
148 //
149 if(currPoint){
150 surveyedPoints[i][0]=currPoint->GetX();
151 surveyedPoints[i][1]=currPoint->GetY();
152 surveyedPoints[i][2]=currPoint->GetZ();
153 if(fDebug)
154 Printf(Form("INFO: Point \"%s\" coordinates read.", pointNames[i].Data()));
155 }
156 else {
157 if(fDebug){
158 Printf(Form("ERROR: Essential point missing: \"%s\"", pointNames[i].Data()));
159 return 1;
160 }
161 }
162 }
163 //
164 // Ideal points
165 //
166 Float_t idealPoints[8][3];
167 //
3e3d6422 168 AliSurveyObj * s2 = new AliSurveyObj();
169 s2->FillFromLocalFile(fFileLoc);
2574314d 170 //
3e3d6422 171 TString pointNamesL[8] ={"R04","R05","R06","R07","R08","R10","R11","R12"};
2574314d 172 //
3e3d6422 173 AliSurveyPoint *currPointL;
174 //
175 for(Int_t i=0;i<numberPoints;i++){
176 currPointL=0;
177 currPointL = (AliSurveyPoint *) s2->GetData()->FindObject(pointNamesL[i]);
178 if(currPointL){
179 idealPoints[i][0]=currPointL->GetY();
180 idealPoints[i][1]=currPointL->GetZ();
181 idealPoints[i][2]=currPointL->GetX();
182 if(fDebug)
183 Printf(Form("INFO: Point \"%s\" coordinates read.", pointNamesL[i].Data()));
184 }
185 else{
186 if(fDebug){
187 Printf(Form("ERROR: Essential point missing: \"%s\"", pointNamesL[i].Data()));
188 return 1; }
189 }
190 }
2574314d 191 //
192 // Create and fill matrices a & y
193 //
194 for(Int_t i = 0;i<numberPoints;i++){
195 for(Int_t j=0;j<3;j++){
196 fY(i*3+j,0)=surveyedPoints[i][j]-idealPoints[i][j];
197
198 }
199 }
200 fA.Zero();
201 //
202 //
203 // setting matrix a
204 //
205 for(Int_t i=0;i<numberPoints;i++){
206 fA(3*i,0)= -idealPoints[i][1];
207 fA(3*i,1)= idealPoints[i][2];
208 fA(3*i,3)=1.;
209 fA(3*i+1,0)= idealPoints[i][0];
210 fA(3*i+1,2)= -idealPoints[i][2];
211 fA(3*i+1,4)= 1.;
212 fA(3*i+2,1)= -idealPoints[i][0];
213 fA(3*i+2,2)= idealPoints[i][1];
214 fA(3*i+2,5)=1.;
215 }
216 //
217 delete s1;
218 //
219 return 0;
220}
221//_________________________________________________________________
222Double_t AliTPCAlign::ComputeTransform(){
223 //
224 // Here simple matrix operations for the linear least square
225 // The trigonometric function sin is approximated by the angle
226 // and the cos by "1", because angles are very small (Y-convention!)
227 // This secures the linearity of the problem
228 // This method returns a sum of squares of residuals
229 //
230 TMatrixD tt1(TMatrixD::kInverted,(TMatrixD(fA,TMatrixD::kTransposeMult,fA)));
231 fX=TMatrixD(tt1,TMatrixD::kMult,TMatrixD(fA,TMatrixD::kTransposeMult,fY));
232 //
233 TMatrixD Unit(24,24);
234 Unit.UnitMatrix();
235 TMatrixD xxt1(TMatrixD::kInverted,TMatrixD(fA,TMatrixD::kTransposeMult,fA));
236 TMatrixD t(fA,TMatrixD::kMult,TMatrixD(xxt1,TMatrixD::kMultTranspose,fA));
237 TMatrixD t2(Unit,TMatrixD::kMinus,t);
238 TMatrixD chi2(fY,TMatrixD::kTransposeMult,TMatrixD(t2,TMatrixD::kMult,fY));
239 //
240
241 return chi2(0,0);
242
243}
244//_______________________________________________________________________
245void AliTPCAlign::CreateAlignObj(){
246 //
247 // This method creates AliAlignObj and fills it with Euler angles
248 // and shifts. The units are degrees and cm.
249 //
250 fTPCAlignObj = new AliAlignObjParams();
251 fTPCAlignObj->SetSymName("ALIC_1/TPC_M_1");
252 fTPCAlignObj->SetVolUID(0);
253 Double_t raddeg = TMath::RadToDeg();
254 Double_t phi,theta,psi;
255 //
256 phi=fX(0,0)*raddeg;
257 theta=fX(1,0)*raddeg;
258 psi=fX(2,0)*raddeg;
259 //
260 Double_t dx,dy,dz;
261 dx=fX(3,0)*100.;
262 dy=fX(4,0)*100.;
263 dz=fX(5,0)*100.;
264 fTPCAlignObj->SetRotation(psi,theta,phi);
265 fTPCAlignObj->SetTranslation(dx,dy,dz);
266 fTPCAlignObj->Print("");
267}
268//______________________________________________________________________
269void AliTPCAlign::Run(){
270 //
271 // runs the full chain
272 //
273 SetDebug(0);
274 Bool_t flag = LoadSurveyData();
275 if(flag) {
276 cout<<"Missing points"<<endl;
277 return;
278 }
279 Double_t chi2 = ComputeTransform();
280 if(chi2>0.01) return;
281 CreateAlignObj();
282 StoreAlignObj();
283 //
284}
285//_________________________________________________________________________
286void AliTPCAlign::StoreAlignObj(){
287 //
288AliCDBManager* cdb = AliCDBManager::Instance();
289 if(!cdb->IsDefaultStorageSet()) cdb->SetDefaultStorage("local://$ALICE_ROOT");
290 //
291TClonesArray *array = new TClonesArray("AliAlignObjParams",1);
292//
293 Double_t shifts[3];
294 Double_t rots[3];
295 //
296 fTPCAlignObj->GetPars(shifts,rots);
297 new((*array)[0]) AliAlignObjParams(fTPCAlignObj->GetSymName(),0,shifts[0],
298 shifts[1],shifts[2],rots[0],rots[1],rots[2],kTRUE);
299
300//
301// storing either in the OCDB or local file
302//
303 if( TString(gSystem->Getenv("TOCDB")) != TString("kTRUE") ){
304 // save on file
305 const char* filename = "TPCSurveyMisalignment.root";
306 Char_t fullname[80];
307 sprintf(fullname,"%s/TPC/%s",gSystem->Getenv("ALICE_ROOT"),filename);
308 TFile *f = new TFile(fullname,"RECREATE");
309 if(!f){
310 AliError("cannot open file for output\n");
311 return;
312 }
313 AliInfo(Form("Saving alignment objects to the file %s", filename));
314 f->cd();
315 f->WriteObject(array,"TPCAlignObjs","kSingleKey");
316 f->Close();
317 }else{
318 // save in CDB storage
319 AliCDBStorage* storage;
320 //
321 TString Storage = gSystem->Getenv("STORAGE");
322 if(!Storage.BeginsWith("local://") && !Storage.BeginsWith("alien://")) {
323 AliError(Form(
324 "STORAGE variable set to %s is not valid. Exiting\n",Storage.Data()));
325 return;
326 }
327 storage = cdb->GetStorage(Storage.Data());
328 if(!storage){
329 AliError(Form("Unable to open storage %s\n",Storage.Data()));
330 return;
331 }
332 //
333 AliCDBMetaData* md = new AliCDBMetaData();
334 md->SetResponsible("Marek Kowalski");
335 md->SetComment("Full misalignment of entire TPC from surveyors");
336 AliCDBId id("TPC/Align/Data",0,AliCDBRunRange::Infinity());
337 storage->Put(array,id,md);
338 }
339
340}