; removed
[u/mrichter/AliRoot.git] / VZERO / VZEROSurveyToAlignment.C
CommitLineData
d0c548dc 1void VZEROSurveyToAlignment(){
2
3 // Macro to convert survey data into alignment data.
4 // The position of four fiducial marks, sticked on the
5 // entrance face of the V0C box is converted into the
6 // global position of the box. Positions given by surveyers
7 // are extracted from Survey Data Base.
8
9 if(!gGeoManager) TGeoManager::Import("geometry.root");
10
11 TClonesArray *array = new TClonesArray("AliAlignObjMatrix",10);
12 TClonesArray &mobj = *array;
13
14 Double_t l_vect[3]={0.,0.,0.}; // a local vector (the origin)
15 Double_t g_vect[3]; // vector corresp. to it in global RS
16 Double_t m_vect[3]; // vector corresp. to it in mother RS
b2586b5f 17
d0c548dc 18 // ************* get global matrix *******************
b2586b5f 19 TGeoHMatrix *g3 = AliGeomManager::GetMatrix("VZERO/V0C");
20
d0c548dc 21 // this is used below as the ideal global matrix
22
23 // ************* get local matrix *******************
24 TGeoNode* n3 = gGeoManager->GetCurrentNode();
25 TGeoHMatrix* l3 = n3->GetMatrix();
26
27 // point coordinates in the global RS
28 g3->LocalToMaster(l_vect,g_vect);
29 cout<<endl<<"Point coordinates in the global RS: "<<
30 g_vect[0]<<" "<<g_vect[1]<<" "<<g_vect[2];
31
32 // point coordinates in the mother volume RS
33 l3->LocalToMaster(l_vect,m_vect);
34 cout<<endl<<"Point coordinates in the mother's volume RS: "<<
35 m_vect[0]<<" "<<m_vect[1]<<" "<<m_vect[2]<<" "<<endl;
36
37 // Hereafter are the four ideal fiducial marks on the V0C box,
38 // expressed in local coordinates and in cms - hard coded.
39
40 const Double_t xside = 22.627;
41 const Double_t yside = 22.627;
42 const Double_t zsize = 2.35;
43 const Double_t zoffset = 0.001;
44
45 const Double_t zdepth = zsize+zoffset;
46 Double_t A[3]={-xside,-yside,zdepth};
47 Double_t B[3]={xside,-yside,zdepth};
48 Double_t C[3]={xside,yside,zdepth};
49 Double_t D[3]={-xside,yside,zdepth};
50
51 TGeoTranslation* Atr = new TGeoTranslation("Atr",-xside,-yside,zdepth);
52 TGeoTranslation* Btr = new TGeoTranslation("Btr",xside,-yside,zdepth);
53 TGeoTranslation* Ctr = new TGeoTranslation("Ctr",xside,yside,zdepth);
54 TGeoTranslation* Dtr = new TGeoTranslation("Dtr",-xside,yside,zdepth);
55
56 // ^ local y
57 // |
58 // D-------------|-------------C
59 // | | |
60 // | | |
61 // | | |
62 // | | |
63 // | | |
64 // | | |
65 // ------------------|------------------> local x
66 // | | |
67 // | | |
68 // | | |
69 // | | |
70 // | | |
71 // | | |
72 // A-------------|-------------B
73 //
74 // local z exiting the plane of the screen
75
76 Double_t gA[3], gB[3], gC[3], gD[3];
77 g3->LocalToMaster(A,gA);
78 g3->LocalToMaster(B,gB);
79 g3->LocalToMaster(C,gC);
80 g3->LocalToMaster(D,gD);
81 cout<<endl<<"Ideal fiducial marks coordinates in the global RS:\n"<<
82 "A "<<gA[0]<<" "<<gA[1]<<" "<<gA[2]<<" "<<endl<<
83 "B "<<gB[0]<<" "<<gB[1]<<" "<<gB[2]<<" "<<endl<<
84 "C "<<gC[0]<<" "<<gC[1]<<" "<<gC[2]<<" "<<endl<<
85 "D "<<gD[0]<<" "<<gD[1]<<" "<<gD[2]<<" "<<endl;
b2586b5f 86 cout<<endl<<endl;
d0c548dc 87
b2586b5f 88// Retrieval of real survey data from ALICE Survey Data Depot :
d0c548dc 89
b2586b5f 90 AliSurveyObj *so = new AliSurveyObj();
d0c548dc 91
b2586b5f 92 so->FillFromLocalFile("Survey_835615_V0.txt");
93 size = so->GetEntries();
94
95 Printf("Title: \"%s\"", so->GetReportTitle().Data());
96 Printf("Date: \"%s\"", so->GetReportDate().Data());
97 Printf("Detector: \"%s\"", so->GetDetector().Data());
98 Printf("URL: \"%s\"", so->GetURL().Data());
99 Printf("Number: \"%d\"", so->GetReportNumber());
100 Printf("Version: \"%d\"", so->GetReportVersion());
101 Printf("Observations: \"%s\"", so->GetObservations().Data());
102 Printf("Coordinate System: \"%s\"", so->GetCoordSys().Data());
103 Printf("Measurement Units: \"%s\"", so->GetUnits().Data());
104 Printf("Nr Columns: \"%d\" \n", so->GetNrColumns());
105
106 TObjArray *colNames = so->GetColumnNames();
107
108 TObjArray *points = so->GetData();
109 const char namePoint[4] = "6001";
110 Double_t coordinates[4][3];
111// Printf(" ******* %c ******* \n\n ", namePoint[0]);
112 Printf("Relevant points to be used for alignment procedure (in mm):");
113 for (Int_t i = 0; i < points->GetEntries(); ++i) {
114 if(((AliSurveyPoint *) points->At(i))->GetPointName()[0] == namePoint[0]) {
115 Printf("Point %d --> \"%s\" %f %f %f ", i,
116 ((AliSurveyPoint *) points->At(i))->GetPointName().Data(),
117 ((AliSurveyPoint *) points->At(i))->GetX(),
118 ((AliSurveyPoint *) points->At(i))->GetY(),
119 ((AliSurveyPoint *) points->At(i))->GetZ() );
120 if(i > 10){
121 coordinates[i-11][0] = (AliSurveyPoint *) points->At(i))->GetX();
122 coordinates[i-11][1] = (AliSurveyPoint *) points->At(i))->GetY();
123 coordinates[i-11][2] = (AliSurveyPoint *) points->At(i))->GetZ(); }
124 }
125 }
126
d0c548dc 127 Double_t ngA[3], ngB[3], ngC[3], ngD[3];
128
129 for(Int_t i=0; i<3; i++)
b2586b5f 130 { ngA[i] = coordinates[0][i] / 10.0 ;
131 ngB[i] = coordinates[1][i] / 10.0 ;
132 ngC[i] = coordinates[2][i] / 10.0 ;
133 ngD[i] = coordinates[3][i] / 10.0 ; }
d0c548dc 134
b2586b5f 135 cout<<endl<<"Fiducial marks coordinates in the global RS given by surveyers:\n"<<
d0c548dc 136 "A "<<ngA[0]<<" "<<ngA[1]<<" "<<ngA[2]<<" "<<endl<<
137 "B "<<ngB[0]<<" "<<ngB[1]<<" "<<ngB[2]<<" "<<endl<<
138 "C "<<ngC[0]<<" "<<ngC[1]<<" "<<ngC[2]<<" "<<endl<<
139 "D "<<ngD[0]<<" "<<ngD[1]<<" "<<ngD[2]<<" "<<endl;
140
d0c548dc 141 // From the new fiducial marks coordinates derive back the new global position
142 // of the surveyed volume
143 //*** What follows is the actual survey-to-alignment procedure which assumes,
144 //*** as is the case of the present example, 4 fiducial marks
145 //*** at the corners of a square lying on a plane parallel to a surface
146 //*** of the surveyed box at a certain offset and with
147 //*** x and y sides parallel to the box's x and y axes.
148 //*** If the code below is placed in a separate class or method, it needs
149 //*** as input the four points and the offset from the origin (zdepth)
150 //*** The algorithm can be easily modified for different placement
151 //*** and/or cardinality of the fiducial marks.
152
153 Double_t ab[3], bc[3], n[3];
154 Double_t plane[4], s;
155
156 // first vector on the plane of the fiducial marks
157 for(i=0;i<3;i++){
158 ab[i] = ngB[i] - ngA[i];
159 }
160
161 // second vector on the plane of the fiducial marks
162 for(i=0;i<3;i++){
163 bc[i] = ngC[i] - ngB[i];
164 }
165
166 // vector normal to the plane of the fiducial marks obtained
167 // as cross product of the two vectors on the plane d0^d1
168 n[0] = ab[1] * bc[2] - ab[2] * bc[1];
169 n[1] = ab[2] * bc[0] - ab[0] * bc[2];
170 n[2] = ab[0] * bc[1] - ab[1] * bc[0];
171
172 Double_t sizen = TMath::Sqrt( n[0]*n[0] + n[1]*n[1] + n[2]*n[2] );
173 if(sizen>1.e-8){
174 s = Double_t(1.)/sizen ; //normalization factor
175 }else{
176 return 0;
177 }
178
179 // plane expressed in the hessian normal form, see:
180 // http://mathworld.wolfram.com/HessianNormalForm.html
181 // the first three are the coordinates of the orthonormal vector
182 // the fourth coordinate is equal to the distance from the origin
183 for(i=0;i<3;i++){
184 plane[i] = n[i] * s;
185 }
186 plane[3] = -( plane[0] * ngA[0] + plane[1] * ngA[1] + plane[2] * ngA[2] );
187 // cout<<plane[0]<<" "<<plane[1]<<" "<<plane[2]<<" "<<plane[3]<<" "<<endl;
188
189 // The center of the square with fiducial marks as corners
190 // as the middle point of one diagonal - md
191 // Used below to get the center - orig - of the surveyed box
192
193 Double_t orig[3], md[3];
194 for(i=0;i<3;i++){
195 md[i] = (ngA[i] + ngC[i]) * 0.5;
196 }
197
198 // center of the box
199 for(i=0;i<3;i++){
200 orig[i] = md[i] - plane[i]*zdepth;
201 }
202 orig[1] = md[1] - plane[1]*zdepth;
203 orig[2] = md[2] - plane[2]*zdepth;
204 cout<<endl<<"Center of the box: "<<orig[0]<<" "<<orig[1]<<" "<<orig[2]<<endl;
205
206 // get x,y local directions needed to write the global rotation matrix
207 // for the surveyed volume by normalising vectors ab and bc
208
209 Double_t sx = TMath::Sqrt(ab[0]*ab[0] + ab[1]*ab[1] + ab[2]*ab[2]);
210 if(sx>1.e-8){
211 for(i=0;i<3;i++){
212 ab[i] /= sx;
213 }
b2586b5f 214 cout<<"x direction "<<ab[0]<<" "<<ab[1]<<" "<<ab[2]<<endl;
d0c548dc 215 }
216 Double_t sy = TMath::Sqrt(bc[0]*bc[0] + bc[1]*bc[1] + bc[2]*bc[2]);
217 if(sy>1.e-8){
218 for(i=0;i<3;i++){
219 bc[i] /= sy;
220 }
b2586b5f 221 cout<<"y direction "<<bc[0]<<" "<<bc[1]<<" "<<bc[2]<<endl;
d0c548dc 222 }
223
224 // the global matrix for the surveyed volume - ng
225 Double_t rot[9] = {ab[0],bc[0],plane[0],ab[1],bc[1],plane[1],ab[2],bc[2],plane[2]};
226 TGeoHMatrix ng;
227 ng.SetTranslation(orig);
228 ng.SetRotation(rot);
229
230 cout<<"\n********* global matrix inferred from surveyed fiducial marks ***********\n";
231 ng.Print();
232
233 // To produce the alignment object for the given volume you would
234 // then do something like this:
235 // Calculate the global delta transformation as ng * g3^-1
236
237 TGeoHMatrix gdelta = g3->Inverse(); //now equal to the inverse of g3
238 gdelta.MultiplyLeft(&ng);
239 Int_t index = 0;
240
241 // if the volume is in the look-up table use something like this instead:
ae079791 242 // AliGeomManager::LayerToVolUID(AliGeomManager::kTOF,i);
d0c548dc 243
244 //AliAlignObjMatrix* mobj[0] = new AliAlignObjMatrix("VZERO/V0C",index,gdelta,kTRUE);
245
246 new(mobj[0]) AliAlignObjMatrix("VZERO/V0C",index,gdelta,kTRUE);
247
248 if(!gSystem->Getenv("$TOCDB")){
249 // save on file
250 TFile f("V0Survey.root","RECREATE");
251 if(!f) cerr<<"cannot open file for output\n";
252 f.cd();
253 f.WriteObject(array,"V0SurveyObjs ","kSingleKey");
254 f.Close();
255 }else{
256 // save in CDB storage
d0c548dc 257 AliCDBManager* cdb = AliCDBManager::Instance();
a3edd5a6 258 AliCDBStorage* storage = cdb->GetStorage("local://$ALICE_ROOT");
259 AliCDBMetaData* mda = new AliCDBMetaData();
260 mda->SetResponsible("Brigitte Cheynis");
261 mda->SetComment("Alignment objects for V0 survey");
262 mda->SetAliRootVersion(gSystem->Getenv("$ARVERSION"));
d0c548dc 263 AliCDBId id("VZERO/Align/Data",0,9999999);
a3edd5a6 264 storage->Put(array,id,mda);
d0c548dc 265 }
266
267 array->Delete();
268
269}