2 // Class to take survey data and
3 // transform that to alignment objects.
7 #include "AliFMDSurveyToAlignObjs.h"
9 #include "AliSurveyPoint.h"
10 #include <TGraph2DErrors.h>
16 #include <TRotation.h>
17 #include <TGeoMatrix.h>
18 #include <TGeoManager.h>
19 #include <TGeoPhysicalNode.h>
20 #include "AliFMDGeometry.h"
22 //____________________________________________________________________
24 AliFMDSurveyToAlignObjs::GetUnitFactor() const
26 // Returns the conversion factor from the measured values to
28 if (!fSurveyObj) return 0;
29 TString units(fSurveyObj->GetUnits());
30 if (units.CompareTo("mm", TString::kIgnoreCase) == 0) return .1;
31 else if (units.CompareTo("cm", TString::kIgnoreCase) == 0) return 1.;
32 else if (units.CompareTo("m", TString::kIgnoreCase) == 0) return 100.;
36 //____________________________________________________________________
38 AliFMDSurveyToAlignObjs::GetPoint(const char* name,
40 TVector3& error) const
42 // Get named point. On return, point will contain the point
43 // coordinates in centimeters, and error will contain the
44 // meassurement errors in centimeters too. If no point is found,
45 // returns false, otherwise true.
46 if (!fSurveyPoints) return kFALSE;
48 Double_t unit = GetUnitFactor();
49 if (unit == 0) return kFALSE;
51 TObject* obj = fSurveyPoints->FindObject(name);
52 if (!obj) return kFALSE;
54 AliSurveyPoint* p = static_cast<AliSurveyPoint*>(obj);
55 point.SetXYZ(unit * p->GetX(),
58 error.SetXYZ(unit * p->GetPrecisionX(),
59 unit * p->GetPrecisionY(),
60 unit * p->GetPrecisionZ());
64 //____________________________________________________________________
66 AliFMDSurveyToAlignObjs::CalculatePlane(const TVector3& a,
74 // Calculate the plane translation and rotation from 3 survey points
80 // trans Translation vector
81 // rot Rotation matrix (direction cosines)
87 // Vector a->b, b->c, and normal to plane defined by these two
89 TVector3 ab(b-a), bc(c-a);
91 // Normal vector to the plane of the fiducial marks obtained
92 // as cross product of the two vectors on the plane d0^d1
93 TVector3 nn(ab.Cross(bc));
94 if (nn.Mag() < 1e-8) {
95 Info("CalculatePlane", "Normal vector is null vector");
99 // We express the plane in Hessian normal form.
103 // where n is the normalised normal vector given by
105 // n_x = a / l, n_y = b / l, n_z = c / l, p = d / l
107 // with l = sqrt(a^2+b^2+c^2) and a, b, c, and d are from the
108 // normal plane equation
110 // ax + by + cz + d = 0
113 TVector3 n(nn.Unit());
114 // Double_t p = - (n * a);
116 // The center of the square with the fiducial marks as the
117 // corners. The mid-point of one diagonal - md. Used to get the
118 // center of the surveyd box.
119 // TVector3 md(a + c);
124 // The center of the box.
125 TVector3 orig(md - depth * n);
130 // Normalize the spanning vectors
131 TVector3 uab(ab.Unit());
132 TVector3 ubc(bc.Unit());
134 for (size_t i = 0; i < 3; i++) {
135 // rot[i * 3 + 0] = ubc[i];
136 // rot[i * 3 + 1] = uab[i];
137 rot[i * 3 + 0] = uab[i];
138 rot[i * 3 + 1] = ubc[i];
139 rot[i * 3 + 2] = n[i];
144 //____________________________________________________________________
146 AliFMDSurveyToAlignObjs::FitPlane(const TObjArray& points,
147 const TObjArray& errors,
148 Double_t /* depth */,
153 // Calculate the plane rotation and translation by doing a fit of
154 // the plane equation to the surveyed points. At least 4 points
155 // must be passed in the @a points array with corresponding errors
156 // in the array @a errors. The arrays are assumed to contain
160 // points Array surveyed positions
161 // errors Array of errors corresponding to @a points
162 // depth Survey targets depth (perpendicular to the plane)
163 // trans On return, translation of the plane
164 // rot On return, rotation (direction cosines) of the plane
167 // @c true on success, @c false otherwise
170 Int_t nPoints = points.GetEntries();
172 AliError(Form("Cannot fit a plane equation to less than 4 survey points, "
173 "got only %d", nPoints));
178 // Loop and fill graph
179 for (int i = 0; i < nPoints; i++) {
180 TVector3* p = static_cast<TVector3*>(points.At(i));
181 TVector3* e = static_cast<TVector3*>(errors.At(i));
183 if (!p || !e) continue;
185 g.SetPoint(i, p->X(), p->Y(), p->Z());
186 g.SetPointError(i, e->X(), e->Y(), e->Z());
189 // Check that we have enough points
191 AliError(Form("Only got %d survey points - no good for plane fit",
196 // Next, declare fitting function and fit to graph.
197 // Fit to the plane equation:
199 // ax + by + cz + d = 0
203 // z = - ax/c - by/c - d/c
205 TF2 f("plane", "-[0]*x-[1]*y-[2]",
206 g.GetXmin(), g.GetXmax(), g.GetYmin(), g.GetYmax());
209 // Now, extract the normal and offset
210 TVector3 nv(f.GetParameter(0), f.GetParameter(1), 1);
211 TVector3 n(nv.Unit());
212 Double_t p = -f.GetParameter(2);
214 // Create two vectors spanning the plane
215 TVector3 a(1, 0, f.Eval(1, 0)-p);
216 TVector3 b(0, -1, f.Eval(0, -1)-p);
217 TVector3 ua(a.Unit());
218 TVector3 ub(b.Unit());
219 // Double_t angAb = ua.Angle(ub);
220 // PrintVector("ua: ", ua);
221 // PrintVector("ub: ", ub);
222 // std::cout << "Angle: " << angAb * 180 / TMath::Pi() << std::endl;
224 for (size_t i = 0; i < 3; i++) {
225 rot[i * 3 + 0] = ua[i];
226 rot[i * 3 + 1] = ub[i];
227 rot[i * 3 + 2] = n[i];
230 // The intersection of the plane is given by (0, 0, -d/c)
239 //____________________________________________________________________
241 AliFMDSurveyToAlignObjs::MakeDelta(const char* path,
243 const Double_t* trans,
244 TGeoHMatrix& delta) const
247 // Create a delta transform from a global rotation matrix and
251 // path Path of element to transform.
252 // rot Rotation matrix (direction cosines)
254 // delta On return, the delta transform
259 if (!gGeoManager) return kFALSE;
260 if (!gGeoManager->cd(path)) return kFALSE;
263 TGeoMatrix* global = gGeoManager->GetCurrentMatrix();
265 PrintRotation(Form("%s rot:", global->GetName()),global->GetRotationMatrix());
266 PrintVector(Form("%s trans:", global->GetName()),global->GetTranslation());
269 return MakeDelta(global, rot, trans, delta);
272 //____________________________________________________________________
274 AliFMDSurveyToAlignObjs::MakeDelta(const TGeoMatrix* global,
276 const Double_t* trans,
277 TGeoHMatrix& delta) const
280 // Create a delta transform from a global rotation matrix and
284 // global Global matrix of element to transform.
285 // rot Rotation matrix (direction cosines)
287 // delta On return, the delta transform
292 TGeoHMatrix* geoM = new TGeoHMatrix;
293 geoM->SetTranslation(trans);
294 geoM->SetRotation(rot);
296 delta = global->Inverse();
297 delta.MultiplyLeft(geoM);
302 //____________________________________________________________________
304 AliFMDSurveyToAlignObjs::GetFMD1Plane(Double_t* rot, Double_t* trans) const
307 // Get the FMD1 plane from the survey points
310 // rot Rotation matrix (direction cosines)
314 // @c true on success, @c false otherwise.
317 // The possile survey points
318 TVector3 icb, ict, ocb, oct, dummy;
320 if (!GetPoint("V0L_ICB", icb, dummy)) missing++;
321 if (!GetPoint("V0L_ICT", ict, dummy)) missing++;
322 if (!GetPoint("V0L_OCB", ocb, dummy)) missing++;
323 if (!GetPoint("V0L_OCT", oct, dummy)) missing++;
325 // Check that we have enough points
327 AliWarning(Form("Only got %d survey points - no good for FMD1 plane",
333 const char* lidN = "FMD1_lid_mat0";
334 TGeoMatrix* lidM = static_cast<TGeoMatrix*>(gGeoManager->GetListOfMatrices()
337 AliError(Form("Couldn't find FMD1 lid transformation %s", lidN));
341 const Double_t* lidT = lidM->GetTranslation();
342 Double_t lidZ = lidT[2];
343 Double_t off = lidZ-3.3;
348 // if (!CalculatePlane(ocb, icb, ict, off, trans, rot)) return kFALSE;
349 if (!CalculatePlane(ocb, icb, oct, off, trans, rot)) return kFALSE;
350 PrintRotation("FMD1 rotation:", rot);
351 PrintVector("FMD1 translation:", trans);
356 //____________________________________________________________________
358 AliFMDSurveyToAlignObjs::DoFMD1()
361 // Do the FMD1 analysis. We have 4 survey targets on V0-A on the
364 // - V0A_ICT In-side, C-side, top.
365 // - V0A_ICB In-side, C-side, bottom.
366 // - V0A_OCT Out-side, C-side, top.
367 // - V0A_OCB Out-side, C-side, bottom.
369 // These 4 survey targets sit 3.3mm over the V0-A C-side surface, or
370 // 3.3mm over the back surface of FMD1.
372 // Since these are really sitting on a plane, we can use the method
373 // proposed by the CORE offline.
376 // @c true on success, @c false otherwise.
380 Double_t rot[9], trans[3];
381 if (!GetFMD1Plane(rot, trans)) return kFALSE;
382 // const char* path = "/ALIC_1/F1MT_1/FMD1_lid_0";
385 // TGeoHMatrix delta;
386 Double_t gRot[9], gTrans[3];
387 TVector3 ocb(-127, -220, 324.67);
388 TVector3 oct(-127, +220, 324.67);
389 TVector3 icb(+127, -220, 324.67);
390 TVector3 ict(+127, +220, 324.67);
391 if (!CalculatePlane(ocb, icb, oct, 0, gTrans, gRot)) {
392 Warning("DoFMD1", "Failed to make reference plane");
395 PrintRotation("FMD1 ref rotation:", gRot);
396 PrintVector("FMD1 ref translation:", gTrans);
397 TGeoRotation ggRot; ggRot.SetMatrix(gRot);
398 TGeoCombiTrans global(gTrans[0], gTrans[1], gTrans[2], &ggRot);
401 TGeoTranslation global(0,0,324.670);
402 if (!MakeDelta(&global, rot, trans, fFMD1Delta))
405 // PrintRotation("FMD1 delta rotation:", fFMD1Delta.GetRotationMatrix());
406 // PrintVector("FMD1 delta translation:", fFMD1Delta.GetTranslation());
411 //____________________________________________________________________
413 AliFMDSurveyToAlignObjs::GetFMD2Plane(Double_t* rot, Double_t* trans) const
416 // Get the surveyed plane corresponding to the backside of FMD2.
417 // The plane is done as a best fit of the plane equation to at least
418 // 4 of the available survey points.
421 // rot Rotation matrix (direction cosines)
422 // trans Translation vector.
425 // @c true on success, @c false otherwise
428 // The possible survey points
429 const char* names[] = { "FMD2_ITOP", "FMD2_OTOP",
430 "FMD2_IBOTM", "FMD2_OBOTM",
431 "FMD2_IBOT", "FMD2_OBOT",
433 const char** name = names;
438 // Loop and fill graph
441 if (!GetPoint(*name++, p, e)) continue;
443 points.Add(new TVector3(p));
444 errors.Add(new TVector3(e));
446 if (points.GetEntries() < 4) {
447 AliWarning(Form("Only got %d survey points - no good for FMD2 plane",
448 points.GetEntries()));
452 return FitPlane(points, errors, 0, trans, rot);
455 #define M(I,J) rot[(J-1) * 3 + (I-1)]
456 //____________________________________________________________________
458 AliFMDSurveyToAlignObjs::DoFMD2()
461 // Do the FMD2 calculations. We have 6 survey points of which only
462 // 5 are normally surveyed. These are all sittings
464 // - FMD2_ITOP - In-side, top
465 // - FMD2_IBOTM - In-side, middle bottom
466 // - FMD2_IBOT - In-side, bottom
467 // - FMD2_OTOP - Out-side, top
468 // - FMD2_OBOTM - Out-side, middle bottom
469 // - FMD2_OBOT - Out-side, bottom
471 // The nominal coordinates of these retro-fitted survey stickers
472 // isn't known. Also, these stickers are put on a thin (0.3mm
473 // thick) carbon cover which flexes quite easily. This means, that
474 // to rotations and xy-translation obtained from the survey data
475 // cannot be used, and left is only the z-translation.
477 // Further more, since FMD2 to is attached to the ITS SPD thermal
478 // screen, it is questionable if the FMD2 survey will ever be used.
481 // @c true on success, @c false otherwise.
485 Double_t rot[9], trans[3];
486 if (!GetFMD2Plane(rot, trans)) return kFALSE;
487 PrintRotation("FMD2 rotation:", rot);
488 PrintVector("FMD2 translation:", trans);
491 for (int i = 0; i < 3; i++) {
492 for (int j = 0; j < 3; j++) {
493 rot[i*3+j] = (i == j ? 1 : 0);
497 trans[0] = trans[1] = 0;
499 // PrintRotation("FMD2 rotation:", rot);
500 // PrintVector("FMD2 translation:", trans);
502 // TGeoHMatrix delta;
503 if (!MakeDelta("/ALIC_1/F2MT_2/FMD2_support_0/FMD2_back_cover_2",
504 rot, trans, fFMD2Delta)) return kFALSE;
506 // PrintRotation("FMD2 delta rotation:", fFMD2Delta.GetRotationMatrix());
507 // PrintVector("FMD2 delta translation:", fFMD2Delta.GetTranslation());
512 //____________________________________________________________________
514 AliFMDSurveyToAlignObjs::Run()
521 AliFMDGeometry* geom = AliFMDGeometry::Instance();
523 geom->InitTransformations();
529 //____________________________________________________________________
531 AliFMDSurveyToAlignObjs::Run(const char** files)
538 AliFMDGeometry* geom = AliFMDGeometry::Instance();
540 geom->InitTransformations();
542 const char** file = files;
544 if ((*file)[0] == '\0') {
545 Warning("Run", "no file specified");
549 if (!LoadSurveyFromLocalFile(*file)) {
550 Warning("Run", "Failed to load %s", *file);
554 TString sDet(fSurveyObj->GetDetector());
555 Int_t d = Int_t(sDet[sDet.Length()-1] - '0');
556 Info("Run", "Making alignment for %s (%d)", sDet.Data(), d);
559 case 1: ret = DoFMD1(); break;
560 case 2: ret = DoFMD2(); break;
562 Warning("Run", "Do not know how to deal with %s", sDet.Data());
566 Warning("Run", "Calculation for %s failed", sDet.Data());
571 FillDefaultAlignObjs();
574 //____________________________________________________________________
576 AliFMDSurveyToAlignObjs::CreateDefaultAlignObj(const TString& path,
579 Int_t nAlign = fAlignObjArray->GetEntries();
580 AliAlignObjParams* obj =
581 new ((*fAlignObjArray)[nAlign]) AliAlignObjParams(path.Data(),
582 id,0,0,0,0,0,0,kTRUE);
584 AliError(Form("Failed to create alignment object for %s", path.Data()));
587 if (!obj->SetLocalPars(0, 0, 0, 0, 0, 0)) {
588 AliError(Form("Failed to set local transforms on %s", path.Data()));
594 //____________________________________________________________________
596 AliFMDSurveyToAlignObjs::FindAlignObj(const TString& path) const
598 AliAlignObjParams* p = 0;
599 for (int i = 0; i < fAlignObjArray->GetEntries(); i++) {
600 p = static_cast<AliAlignObjParams*>(fAlignObjArray->At(i));
601 if (path.EqualTo(p->GetSymName())) return p;
606 //____________________________________________________________________
608 AliFMDSurveyToAlignObjs::FillDefaultAlignObjs()
610 for (int d = 1; d <= 3; d++) {
611 const char sides[] = { 'T', 'B', 0 };
612 const char* side = sides;
614 TString path = TString::Format("FMD/FMD%d_%c", d, *side);
615 AliAlignObjParams* p = FindAlignObj(path);
617 p = CreateDefaultAlignObj(path, 0);
619 Info("FillDefaultAlignObjs", "Alignment object %s exists", path.Data());
620 const char halves[] = { 'I', d == 1 ? '\0' : 'O', 0 };
621 const char* half = halves;
623 int nsec = *half == 'I' ? 10 : 20;
624 int start = *side == 'T' ? 0 : nsec/2;
625 int end = *side == 'T' ? nsec/2 : nsec;
626 for (int s=start; s < end; s++) {
627 path = TString::Format("FMD/FMD%d_%c/FMD%c_%02d",
629 CreateDefaultAlignObj(path, 0);
640 //____________________________________________________________________
642 AliFMDSurveyToAlignObjs::CreateAlignObjs()
646 // Method to create the alignment objects
649 // @c true on success, @c false otherwise
651 TClonesArray& array = *fAlignObjArray;
652 Int_t n = array.GetEntriesFast();
654 if (!fFMD1Delta.IsIdentity()) {
655 new (array[n++]) AliAlignObjParams("FMD/FMD1_T", 0, fFMD1Delta, kTRUE);
656 new (array[n++]) AliAlignObjParams("FMD/FMD1_B", 0, fFMD1Delta, kTRUE);
658 if (!fFMD2Delta.IsIdentity()) {
659 new (array[n++]) AliAlignObjParams("FMD/FMD2_T", 0, fFMD2Delta, kTRUE);
660 new (array[n++]) AliAlignObjParams("FMD/FMD2_B", 0, fFMD2Delta, kTRUE);
667 //____________________________________________________________________
669 AliFMDSurveyToAlignObjs::PrintVector(const char* text, const TVector3& v)
672 // Service member function to print a vector
678 Double_t va[] = { v.X(), v.Y(), v.Z() };
679 PrintVector(text, va);
681 //____________________________________________________________________
683 AliFMDSurveyToAlignObjs::PrintVector(const char* text, const Double_t* v)
686 // Service member function to print a vector
690 // v Vector (array of 3 doubles)
693 << std::setw(15) << v[0]
694 << std::setw(15) << v[1]
695 << std::setw(15) << v[2]
700 //____________________________________________________________________
702 AliFMDSurveyToAlignObjs::PrintRotation(const char* text, const Double_t* rot)
705 // Service member function to print a rotation matrix
709 // v Matrix (array of 9 doubles)
712 std::cout << text << std::endl;
713 for (size_t i = 0; i < 3; i++) {
714 for (size_t j = 0; j < 3; j++)
715 std::cout << std::setw(15) << rot[i * 3 + j];
716 std::cout << std::endl;
720 //____________________________________________________________________