Move HMPID QA code to PWG1.
[u/mrichter/AliRoot.git] / HMPID / SurveyToAlignHmpid.C
1 TVector3 fFM[28]; //array of global coordinates for 28 fiducial marks
2 Int_t sNch, oNch; // survey and offline chamber's number
3
4
5 TGeoHMatrix GetResSurvAlign(Int_t survNch, Int_t& offNch);
6
7 void SurveyToAlignHmpid(const char* filename="Survey_781282_HMPID.txt"){
8         // Open file with AliSurveyPoints for the 7 HMPID chambers
9         // Produce the corresponding alignment objects
10
11         AliSurveyObj *so = new AliSurveyObj();
12
13         Int_t size = so->GetEntries();
14         printf("-> %d\n", size);
15
16         so->FillFromLocalFile(filename);
17         size = so->GetEntries();
18         printf("--> %d\n", size);
19
20         TObjArray *points = so->GetData();
21         
22         // We retrieve and open the ideal geometry
23         AliCDBManager* cdbman = AliCDBManager::Instance();
24         if(!cdbman->IsDefaultStorageSet()){
25                 cdbman->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
26         }else{
27                 cdbman->SetSpecificStorage("GRP/Geometry/*","local://$ALICE_ROOT/OCDB");
28         }
29         cdbman->SetRun(0);
30         AliCDBEntry* cdbe = (AliCDBEntry*) cdbman->Get("GRP/Geometry/Data"); 
31
32
33         for (Int_t i = 0; i < points->GetEntries(); ++i)
34         {
35                 AliSurveyPoint *p=(AliSurveyPoint *) points->At(i);
36                 fFM[i].SetXYZ(p->GetX()*100.,p->GetY()*100.,p->GetZ()*100.);
37         }
38
39         TString chbasename("/HMPID/Chamber");
40         for(Int_t sNch=0; sNch<7; sNch++){
41                 TGeoHMatrix mtx = GetResSurvAlign(sNch,oNch); //get global matrix from survey points
42         
43                 TString chsymname = chbasename;
44                 chsymname += oNch;
45                 printf("getting global matrix for the alignable volume %s\n",chsymname.Data());
46                 TGeoHMatrix *gm = AliGeomManager::GetMatrix(chsymname.Data());
47
48                 if(!gm){
49                         printf("unable to get global matrix for the alignable volume %s\n",chsymname.Data());
50                         continue;
51                 }
52                 TGeoHMatrix gdelta = gm->Inverse();
53                 gdelta.MultiplyLeft(&mtx);
54
55                 //gdelta.Print();
56
57                 AliAlignObjMatrix* mobj = new
58                         AliAlignObjMatrix(AliGeomManager::SymName(AliGeomManager::LayerToVolUID(AliGeomManager::kHMPID,oNch)),
59                                         AliGeomManager::LayerToVolUID(AliGeomManager::kHMPID,oNch),gdelta,kTRUE);
60                 /*
61                    cout<<"\n************* obtained   AliAlignObjMatrix************\n";
62                    mobj->Print();
63                    cout<<""<<endl;
64
65                    TGeoHMatrix pa=gdelta*g0;
66
67                    pa.Print();
68                  */
69         }
70 }
71
72
73 TGeoHMatrix GetResSurvAlign(Int_t survNch, Int_t& offNch)
74 {
75         // For a given chamber identified by survey chamber number 'survNch',
76         // return the global matrix inferred from the survey points of its
77         // 4 fiducial marks and set the offline chamber number 'offNch'
78         //
79         Int_t ChSrv2Off[7] = {4,3,5,1,6,2,0};
80         //cout<<"  *********  Chamber Numbers  ******"<<endl;
81         //cout<<"  ****  Survey   ****  Offline *****"<<endl;
82         //for(Int_t ch=0; ch<7; ch++){
83         //      cout<<"           "<<ch<<"              "<<ChSrv2Off[ch]<<endl;
84         //}
85         
86         offNch=ChSrv2Off[survNch];
87
88         Double_t ab[3], bc[3], n[3];
89         Double_t plane[4], s;
90         Double_t ngA[3]={fFM[0+4*survNch].X(),fFM[0+4*survNch].Y(),fFM[0+4*survNch].Z()};
91         Double_t ngB[3]={fFM[1+4*survNch].X(),fFM[1+4*survNch].Y(),fFM[1+4*survNch].Z()};
92         Double_t ngC[3]={fFM[2+4*survNch].X(),fFM[2+4*survNch].Y(),fFM[2+4*survNch].Z()};
93         Double_t ngD[3]={fFM[3+4*survNch].X(),fFM[3+4*survNch].Y(),fFM[3+4*survNch].Z()};
94         if(survNch>4)
95         {
96                 // first vector on the plane of the fiducial marks
97                 for(Int_t i=0;i<3;i++){
98                         ab[i] = ngB[i] - ngA[i];
99                 }
100
101                 // second vector on the plane of the fiducial marks
102                 for(Int_t i=0;i<3;i++){
103                         bc[i] = ngC[i] - ngB[i];
104                 }
105         }
106         else{
107                 // first vector on the plane of the fiducial marks
108                 for(Int_t i=0;i<3;i++){
109                         ab[i] = ngB[i] - ngA[i];
110                 }
111
112                 // second vector on the plane of the fiducial marks
113                 for(Int_t i=0;i<3;i++){
114                         bc[i] = ngD[i] - ngB[i];
115                 }
116
117         }
118         // vector normal to the plane of the fiducial marks obtained
119         // as cross product of the two vectors on the plane d0^d1
120         n[0] = ab[1] * bc[2] - ab[2] * bc[1];
121         n[1] = ab[2] * bc[0] - ab[0] * bc[2];
122         n[2] = ab[0] * bc[1] - ab[1] * bc[0];
123
124         Double_t sizen = TMath::Sqrt( n[0]*n[0] + n[1]*n[1] + n[2]*n[2] );
125         if(sizen>1.e-8){
126                 s = Double_t(1.)/sizen ; //normalization factor
127         }else{
128                 return 0;
129         }
130
131         // plane expressed in the hessian normal form, see:
132         // http://mathworld.wolfram.com/HessianNormalForm.html
133         // the first three are the coordinates of the orthonormal vector
134         // the fourth coordinate is equal to the distance from the origin
135
136         for(i=0;i<3;i++){
137                 plane[i] = n[i] * s;
138         }
139         plane[3] = -( plane[0] * ngA[0] + plane[1] * ngA[1] + plane[2] * ngA[2] );
140         cout<<"normal to plane and distance from IP: "<<plane[0]<<"  "<<plane[1]<<"  "<<plane[2]<<"  "<<plane[3]<<"  "<<endl;
141
142         // The center of the square with fiducial marks as corners
143         // as the middle point of one diagonal - md
144         // Used below to get the center - orig - of the surveyed box
145         Double_t orig[3], md[3];
146
147         if(survNch>4){
148                 for(i=0;i<3;i++){
149                         md[i] = (ngA[i] + ngC[i]) * 0.5;//modified!!!!!!!!!
150                 }
151
152         }
153
154         else {
155                 for(i=0;i<3;i++){
156                         md[i] = (ngA[i] + ngD[i]) * 0.5;//modified!!!!!!!!!
157                 }
158         }
159         cout<<"The center of the box from Survey data: "<<md[0]<<"  "<<md[1]<<"  "<<md[2]<<endl;
160         const Double_t zdepth=-0.9-4.85; //the fiducial marks are down the radiator (behind the honeycomb structure). They
161         //lay on 4 cylinders whose height is 9 mm.
162
163         // The center of the box
164         for(i=0;i<1;i++){
165                 orig[i] = md[i] - (-plane[i])*(zdepth+plane[3]);
166         }
167         orig[1] = md[1] - (-plane[1])*(zdepth+plane[3]);
168         orig[2] = md[2] - (-plane[2])*(zdepth+plane[3]);
169
170         cout<<"The origin of the box: "<<orig[0]<<"  "<<orig[1]<<"  "<<orig[2]<<endl;
171
172         // get x,y local directions needed to write the global rotation matrix
173         // for the surveyed volume by normalising vectors ab and bc
174         Double_t sx = TMath::Sqrt(ab[0]*ab[0] + ab[1]*ab[1] + ab[2]*ab[2]);
175         if(sx>1.e-8){
176                 for(i=0;i<3;i++){
177                         ab[i] /= sx;
178                 }
179                 cout<<"x "<<ab[0]<<"  "<<ab[1]<<"  "<<ab[2]<<endl;
180         }
181         Double_t sy = TMath::Sqrt(bc[0]*bc[0] + bc[1]*bc[1] + bc[2]*bc[2]);
182         if(sy>1.e-8){
183                 for(i=0;i<3;i++){
184                         bc[i] /= sy;
185                 }
186                 cout<<"y "<<bc[0]<<"  "<<bc[1]<<"  "<<bc[2]<<endl;
187         }
188
189
190         // the global matrix for the surveyed volume - ng
191         TVector3 v1;
192         v1.SetXYZ(md[0],md[1],md[2]);
193
194         TVector3 w=v1.Unit();
195         Double_t chamberCenter[3];
196         chamberCenter[0]=-w.X()*(zdepth-v1.Mag());
197         chamberCenter[1]=-w.Y()*(zdepth-v1.Mag());
198         chamberCenter[2]=-w.Z()*(zdepth-v1.Mag());
199
200         Double_t rot[9] = {-ab[0],bc[0],-plane[0],-ab[1],bc[1],-plane[1],-ab[2],bc[2],-plane[2]};
201         TGeoHMatrix ng;
202         ng.SetTranslation(md);
203         ng.SetRotation(rot);
204
205         cout<<"\n********* global matrix inferred from surveyed fiducial marks for chamber"<<survNch<<"***********\n";
206         ng.Print();
207
208
209         return ng;
210
211 }
212
213
214
215