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