1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 ///////////////////////////////////////////////////////////////////////////////
19 // An AliTRDalignment object contains the alignment data (3 shifts and 3 //
20 // tilts) for all the alignable volumes of the TRD, i.e. for 18 supermodules //
21 // and 540 chambers. The class provides simple tools for reading and writing //
22 // these data in different formats, and for generating fake data that can be //
23 // used to simulate misalignment. //
24 // The six alignment variables have the following meaning: //
28 // tilt around rphi //
31 // The shifts are in cm and the tilts are in degrees. //
32 // The currently supported formats are: //
34 // - root file containing a TClonesArray of alignment objects //
35 // - offline conditions database //
36 // - OCDB-like root file //
37 // - geometry file (like misaligned_geometry.root) //
39 // Some examples of usage (in an aliroot session): //
40 // AliTRDalignment a,b,c,d,e; //
41 // double xsm[]={0,0,0,-70,0,0}; //
42 // double xch[]={0,0,-50,0,0,0}; //
44 // a.SetCh(120,xch); //
45 // a.WriteAscii("kuku.dat"); //
46 // TGeoManager::Import("geometry.root"); a.WriteRoot("kuku.root"); //
47 // TGeoManager::Import("geometry.root"); a.WriteDB("kukudb.root",0,0); //
48 // TGeoManager::Import("geometry.root"); //
49 // a.WriteDB("local://$ALICE_ROOT/OCDB", "TRD/Align/Data", 0,0); //
50 // TGeoManager::Import("geometry.root"); a.WriteGeo("kukugeometry.root"); //
52 // b.ReadAscii("kuku.dat"); //
53 // TGeoManager::Import("geometry.root"); c.ReadRoot("kuku.root"); //
54 // TGeoManager::Import("geometry.root"); d.ReadDB("kukudb.root"); //
55 // TGeoManager::Import("kukugeometry.root"); e.ReadCurrentGeo(); //
64 // D.Miskowiec, November 2006 //
66 ///////////////////////////////////////////////////////////////////////////////
73 #include "TGeoManager.h"
74 #include "TGeoPhysicalNode.h"
75 #include "TClonesArray.h"
81 #include "AliAlignObj.h"
82 #include "AliAlignObjParams.h"
83 #include "AliCDBManager.h"
84 #include "AliCDBStorage.h"
85 #include "AliCDBMetaData.h"
86 #include "AliCDBEntry.h"
87 #include "AliSurveyObj.h"
88 #include "AliSurveyPoint.h"
90 #include "AliTRDalignment.h"
92 void trdAlignmentFcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *x, Int_t iflag);
96 ClassImp(AliTRDalignment)
98 //_____________________________________________________________________________
99 AliTRDalignment::AliTRDalignment()
110 for (int i=0; i<18; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
111 fSurveyX[i][j][k][l] = 0.0;
112 fSurveyY[i][j][k][l] = 0.0;
113 fSurveyZ[i][j][k][l] = 0.0;
114 fSurveyEX[i][j][k][l] = 0.0;
115 fSurveyEY[i][j][k][l] = 0.0;
116 fSurveyEZ[i][j][k][l] = 0.0;
119 // Initialize the nominal positions of the survey points
120 // in the local frame of supermodule (where y is the long side,
121 // z corresponds to the radius in lab, and x to the phi in lab).
122 // Four survey marks are on each z-side of the supermodule.
124 // ----o-----------o---- x |
129 // ---o-----o--- -------------->
132 // For the purpose of this explanation lets define the origin such that
133 // the supermodule occupies 0 < x < 77.9 cm. Then the coordinates (x,y)
141 double x[2] = {22.5,30.25}; // lab phi, or tracking-y
142 double y[2] = {353.0, -353.0}; // lab z; inc. 2 cm survey target offset
143 double z[2] = {-(77.9/2.0-2.0),77.9/2.0-1.5}; // lab r, or better tracking-x
145 for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
146 fSurveyX0[j][k][l] = -TMath::Power(-1,l) * x[k];
147 fSurveyY0[j][k][l] = y[j];
148 fSurveyZ0[j][k][l] = z[k];
151 for (int i=0; i<1000; i++) {
158 //_____________________________________________________________________________
159 AliTRDalignment::AliTRDalignment(const AliTRDalignment& source)
161 ,fComment(source.fComment)
168 for (int i=0; i<18; i++) SetSm(i,source.fSm[i]);
169 for (int i=0; i<540; i++) SetCh(i,source.fCh[i]);
170 for (int i=0; i<18; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
171 fSurveyX[i][j][k][l] = source.fSurveyX[i][j][k][l];
172 fSurveyY[i][j][k][l] = source.fSurveyY[i][j][k][l];
173 fSurveyZ[i][j][k][l] = source.fSurveyZ[i][j][k][l];
174 fSurveyEX[i][j][k][l] = source.fSurveyEX[i][j][k][l];
175 fSurveyEY[i][j][k][l] = source.fSurveyEY[i][j][k][l];
176 fSurveyEZ[i][j][k][l] = source.fSurveyEZ[i][j][k][l];
178 for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
179 fSurveyX0[j][k][l] = source.fSurveyX0[j][k][l];
180 fSurveyY0[j][k][l] = source.fSurveyY0[j][k][l];
181 fSurveyZ0[j][k][l] = source.fSurveyZ0[j][k][l];
183 for (int i=0; i<1000; i++) {
190 //_____________________________________________________________________________
191 AliTRDalignment& AliTRDalignment::operator=(const AliTRDalignment &source)
194 // assignment operator
197 if (this != &source) {
198 for (int i = 0; i < 18; i++) SetSm(i,source.fSm[i]);
199 for (int i = 0; i < 540; i++) SetCh(i,source.fCh[i]);
200 for (int i=0; i<18; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
201 fSurveyX[i][j][k][l] = source.fSurveyX[i][j][k][l];
202 fSurveyY[i][j][k][l] = source.fSurveyY[i][j][k][l];
203 fSurveyZ[i][j][k][l] = source.fSurveyZ[i][j][k][l];
204 fSurveyEX[i][j][k][l] = source.fSurveyEX[i][j][k][l];
205 fSurveyEY[i][j][k][l] = source.fSurveyEY[i][j][k][l];
206 fSurveyEZ[i][j][k][l] = source.fSurveyEZ[i][j][k][l];
208 for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
209 fSurveyX0[j][k][l] = source.fSurveyX0[j][k][l];
210 fSurveyY0[j][k][l] = source.fSurveyY0[j][k][l];
211 fSurveyZ0[j][k][l] = source.fSurveyZ0[j][k][l];
213 fComment = source.fComment;
220 //_____________________________________________________________________________
221 AliTRDalignment& AliTRDalignment::operator*=(double fac)
224 // multiplication operator
227 for (int i = 0; i < 18; i++) for (int j = 0; j < 6; j++) this->fSm[i][j] *= fac;
228 for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) this->fCh[i][j] *= fac;
234 //_____________________________________________________________________________
235 AliTRDalignment& AliTRDalignment::operator+=(const AliTRDalignment &source)
241 for (int i = 0; i < 18; i++) for (int j = 0; j < 6; j++) this->fSm[i][j] += source.fSm[i][j];
242 for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) this->fCh[i][j] += source.fCh[i][j];
248 //_____________________________________________________________________________
249 AliTRDalignment& AliTRDalignment::operator-=(const AliTRDalignment &source)
252 // subtraction operator
255 for (int i = 0; i < 18; i++) for (int j = 0; j < 6; j++) fSm[i][j] -= source.fSm[i][j];
256 for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) fCh[i][j] -= source.fCh[i][j];
262 //_____________________________________________________________________________
263 Bool_t AliTRDalignment::operator==(const AliTRDalignment &source) const
266 // comparison operator
271 for (int i = 0; i < 18; i++) for (int j = 0; j < 6; j++) areEqual &= (fSm[i][j] == source.fSm[i][j]);
272 for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) areEqual &= (fCh[i][j] == source.fCh[i][j]);
278 //_____________________________________________________________________________
279 void AliTRDalignment::SetSmZero()
282 // reset to zero supermodule data
285 memset(&fSm[0][0],0,sizeof(fSm));
289 //_____________________________________________________________________________
290 void AliTRDalignment::SetChZero()
293 // reset to zero chamber data
296 memset(&fCh[0][0],0,sizeof(fCh));
300 //_____________________________________________________________________________
301 void AliTRDalignment::SetSmRandom(double a[6])
304 // generate random gaussian supermodule data with sigmas a
308 double xmax[6]={999, 0.6, 999, 999, 999, 999};
310 for (int i = 0; i < 18; i++) {
311 for (int j = 0; j < 6; j++) {
312 do {x[j] = fRan.Gaus(0,a[j]);} while (TMath::Abs(x[j]) > xmax[j]);
320 //_____________________________________________________________________________
321 void AliTRDalignment::SetChRandom(double a[6])
324 // generate random gaussian chamber data with sigmas a
329 for (int i = 0; i < 540; i++) {
330 fRan.Rannor(x[0],x[1]);
331 fRan.Rannor(x[2],x[3]);
332 fRan.Rannor(x[4],x[5]);
333 for (int j = 0; j < 6; j++) x[j] *= a[j];
340 //_____________________________________________________________________________
341 void AliTRDalignment::SetSmFull()
344 // generate random gaussian supermodule data similar to the misalignment
345 // expected from the mechanical precision
353 a[3] = 0.4/1000.0 / TMath::Pi()*180.0; // phi
354 a[4] = 2.0/1000.0 / TMath::Pi()*180.0; // z
355 a[5] = 0.4/1000.0 / TMath::Pi()*180.0; // r
361 //_____________________________________________________________________________
362 void AliTRDalignment::SetChFull()
365 // generate random gaussian chamber data similar to the misalignment
366 // expected from the mechanical precision
374 a[3] = 1.0/1000.0 / TMath::Pi()*180.0; // phi
375 a[4] = 1.0/1000.0 / TMath::Pi()*180.0; // z
376 a[5] = 0.7/1000.0 / TMath::Pi()*180.0; // r
382 //_____________________________________________________________________________
383 void AliTRDalignment::SetSmResidual()
386 // generate random gaussian supermodule data similar to the misalignment
387 // remaining after full calibration
388 // I assume that it will be negligible
395 //_____________________________________________________________________________
396 void AliTRDalignment::SetChResidual()
399 // generate random gaussian chamber data similar to the misalignment
400 // remaining after full calibration
408 a[3] = 0.3/1000.0 / TMath::Pi()*180.0; // phi
409 a[4] = 0.3/1000.0 / TMath::Pi()*180.0; // z
410 a[5] = 0.1/1000.0 / TMath::Pi()*180.0; // r
416 //_____________________________________________________________________________
417 void AliTRDalignment::PrintSm(int i, FILE * const fp) const
420 // print the supermodule data
423 fprintf(fp,"%4d %11.4f %11.4f %11.4f %11.5f %11.5f %11.5f %6d %s\n"
424 ,i,fSm[i][0],fSm[i][1],fSm[i][2],fSm[i][3],fSm[i][4],fSm[i][5]
429 //_____________________________________________________________________________
430 void AliTRDalignment::PrintCh(int i, FILE * const fp) const
433 // print the chamber data
436 fprintf(fp,"%4d %11.4f %11.4f %11.4f %11.5f %11.5f %11.5f %6d %s\n"
437 ,i,fCh[i][0],fCh[i][1],fCh[i][2],fCh[i][3],fCh[i][4],fCh[i][5]
438 ,GetVoi(i),GetChName(i));
442 //_____________________________________________________________________________
443 void AliTRDalignment::ReadAscii(const char * const filename)
446 // read the alignment data from ascii file
449 double x[6]; // alignment data
450 int volid; // volume id
451 std::string syna; // symbolic name
452 int j; // dummy index
454 fstream fi(filename,fstream::in);
456 AliError(Form("cannot open input file %s",filename));
462 for (int i = 0; i < 18; i++) {
463 fi>>j>>x[0]>>x[1]>>x[2]>>x[3]>>x[4]>>x[5]>>volid>>syna;
464 if (j != i) AliError(Form("sm %d expected, %d found",i,j));
465 if (volid != 0) AliError(Form("sm %d volume id %d expected, %d found",i,0,volid));
466 std::string symnam = GetSmName(i);
467 if (syna != symnam) AliError(Form("sm %d name %s expected, %s found",i,symnam.data(),syna.data()));
473 for (int i = 0; i < 540; i++) {
474 fi>>j>>x[0]>>x[1]>>x[2]>>x[3]>>x[4]>>x[5]>>volid>>syna;
475 if (j != i) AliError(Form("ch %d expected, %d found",i,j));
476 if (volid != GetVoi(i)) AliError(Form("ch %d volume id %d expected, %d found",i,GetVoi(i),volid));
477 std::string symnam = GetChName(i);
478 if (syna != symnam) AliError(Form("ch %d name %s expected, %s found",i,symnam.data(),syna.data()));
486 //_____________________________________________________________________________
487 void AliTRDalignment::ReadCurrentGeo()
490 // use currently loaded geometry to determine misalignment by comparing
491 // original and misaligned matrix of the last node
492 // Now, original, does not mean "ideal". It is the matrix before the alignment.
493 // So, if alignment was applied more than once, the numbers extracted will
494 // represent just the last alignment. -- check this!
498 TGeoHMatrix *ideSm[18]; // ideal
499 TGeoHMatrix *misSm[18]; // misaligned
500 for (int i = 0; i < 18; i++) if ((pne = gGeoManager->GetAlignableEntry(GetSmName(i)))) {
502 // read misaligned and original matrices
504 TGeoPhysicalNode *node = pne->GetPhysicalNode();
505 if (!node) AliError(Form("physical node entry %s has no physical node",GetSmName(i)));
507 misSm[i] = new TGeoHMatrix(*node->GetNode(node->GetLevel())->GetMatrix());
508 ideSm[i] = new TGeoHMatrix(*node->GetOriginalMatrix());
510 // calculate the local misalignment matrices as inverse misaligned times ideal
512 TGeoHMatrix mat(ideSm[i]->Inverse());
513 mat.Multiply(misSm[i]);
514 double *tra = mat.GetTranslation();
515 double *rot = mat.GetRotationMatrix();
520 if (TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) AliError("Failed to extract roll-pitch-yall angles!");
521 double raddeg = TMath::RadToDeg();
522 pars[3] = raddeg * TMath::ATan2(-rot[5],rot[8]);
523 pars[4] = raddeg * TMath::ASin(rot[2]);
524 pars[5] = raddeg * TMath::ATan2(-rot[1],rot[0]);
533 TGeoHMatrix *ideCh[540]; // ideal
534 TGeoHMatrix *misCh[540]; // misaligned
535 for (int i = 0; i < 540; i++) if ((pne = gGeoManager->GetAlignableEntry(GetChName(i)))) {
537 // read misaligned and original matrices
539 TGeoPhysicalNode *node = pne->GetPhysicalNode();
540 if (!node) AliError(Form("physical node entry %s has no physical node",GetChName(i)));
542 misCh[i] = new TGeoHMatrix(*node->GetNode(node->GetLevel())->GetMatrix());
543 ideCh[i] = new TGeoHMatrix(*node->GetOriginalMatrix());
545 // calculate the local misalignment matrices as inverse misaligned times ideal
547 TGeoHMatrix mat(ideCh[i]->Inverse());
548 mat.Multiply(misCh[i]);
549 double *tra = mat.GetTranslation();
550 double *rot = mat.GetRotationMatrix();
555 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
556 AliError("Failed to extract roll-pitch-yall angles!");
559 double raddeg = TMath::RadToDeg();
560 pars[3] = raddeg * TMath::ATan2(-rot[5],rot[8]);
561 pars[4] = raddeg * TMath::ASin(rot[2]);
562 pars[5] = raddeg * TMath::ATan2(-rot[1],rot[0]);
574 //_____________________________________________________________________________
575 void AliTRDalignment::ReadRoot(const char * const filename)
578 // read the alignment data from root file
581 TFile fi(filename,"READ");
584 TClonesArray *ar = (TClonesArray*) fi.Get("TRDAlignObjs");
588 else AliError(Form("cannot open input file %s",filename));
594 //_____________________________________________________________________________
595 void AliTRDalignment::ReadDB(const char * const filename)
598 // read the alignment data from database file
601 TFile fi(filename,"READ");
604 AliCDBEntry *e = (AliCDBEntry *) fi.Get("AliCDBEntry");
606 fComment.SetString(e->GetMetaData()->GetComment());
607 TClonesArray *ar = (TClonesArray *) e->GetObject();
611 else AliError(Form("cannot open input file %s",filename));
617 //_____________________________________________________________________________
618 void AliTRDalignment::ReadDB(const char * const db, const char * const path,
619 int run, int version, int subversion)
622 // read the alignment data from database
625 AliCDBManager *cdb = AliCDBManager::Instance();
626 AliCDBStorage *storLoc = cdb->GetStorage(db);
627 AliCDBEntry *e = storLoc->Get(path,run,version,subversion);
630 fComment.SetString(e->GetMetaData()->GetComment());
631 TClonesArray *ar = (TClonesArray *) e->GetObject();
636 //_____________________________________________________________________________
637 Bool_t AliTRDalignment::DecodeSurveyPointName(TString pna, Int_t &sm, Int_t &iz,
638 Int_t &ir, Int_t &iphi) {
639 // decode the survey point name and extract the sm, z, r and phi indices
641 if (pna(0,6)!="TRD_sm") {
642 AliError(Form("unexpected point name: %s",pna.Data()));
645 sm = atoi(pna(6,2).Data()); // supermodule number
647 if (pna(8) == 'a') iz=0; // anticlockwise, positive z
648 if (pna(8) == 'c') iz=1; // clockwise, negative z
650 if (pna(9) == 'l') ir=0; // low radius
651 if (pna(9) == 'h') ir=1; // high radius
653 if (pna(10) == '0') iphi = 0; // low phi within supermodule
654 if (pna(10) == '1') iphi = 1; // high phi within supermodule
655 if (sm>=0 && sm<18 && iz>=0 && iz<2 && ir>=0 && ir<2 && iphi>=0 && iphi<2) return kTRUE;
656 AliError(Form("cannot decode point name: %s",pna.Data()));
660 //_____________________________________________________________________________
661 void AliTRDalignment::ReadSurveyReport(const char * const filename)
664 // Read survey report and store the numbers in fSurveyX, fSurveyY, fSurveyZ,
665 // and fSurveyE. Store the survey info in the fComment.
666 // Each supermodule has 8 survey points. The point names look like
667 // TRD_sm08ah0 and have the following meaning.
669 // sm00..17 mean supermodule 0 through 17, following the phi.
670 // Supermodule 00 is between phi=0 and phi=20 degrees.
672 // a or c denotes the anticlockwise and clockwise end of the supermodule
673 // in z. Clockwise end is where z is negative and where the muon arm sits.
675 // l or h denote low radius and high radius holes
677 // 0 or 1 denote the hole at smaller and at larger phi, respectively.
680 // read the survey file
682 fstream in(filename,fstream::in);
684 AliError(Form("cannot open input file %s",filename));
688 // loop through the lines of the file until the beginning of data
690 TString title,date,subdetector,url,version,observations,system,units;
696 if (line.Contains("Title:")) title.ReadLine(in);
697 if (line.Contains("Date:")) date.ReadLine(in);
698 if (line.Contains("Subdetector:")) subdetector.ReadLine(in);
699 if (line.Contains("URL:")) url.ReadLine(in);
700 if (line.Contains("Version:")) version.ReadLine(in);
701 if (line.Contains("Observations:")) observations.ReadLine(in);
702 if (line.Contains("System:")) system.ReadLine(in);
703 if (line.Contains("Units:")) units.ReadLine(in);
704 if (line.Contains("Data:")) break;
707 // check what we found so far (watch out, they have \r at the end)
709 std::cout<<"title .........."<<title<<std::endl;
710 std::cout<<"date ..........."<<date<<std::endl;
711 std::cout<<"subdetector ...."<<subdetector<<std::endl;
712 std::cout<<"url ............"<<url<<std::endl;
713 std::cout<<"version ........"<<version<<std::endl;
714 std::cout<<"observations ..."<<observations<<std::endl;
715 std::cout<<"system ........."<<system<<std::endl;
716 std::cout<<"units .........."<<units<<std::endl;
718 if (!subdetector.Contains("TRD")) {
719 AliWarning(Form("Not a TRD survey file, subdetector = %s",subdetector.Data()));
722 double tocm = 0; // we want to have it in cm
723 if (units.Contains("mm")) tocm = 0.1;
724 else if (units.Contains("cm")) tocm = 1.0;
725 else if (units.Contains("m")) tocm = 100.0;
726 else if (units.Contains("pc")) tocm = 3.24078e-15;
728 AliError(Form("unexpected units: %s",units.Data()));
731 if (!system.Contains("ALICEPH")) {
732 AliError(Form("wrong system: %s, should be ALICEPH",system.Data()));
736 // scan the rest of the file which should contain list of surveyed points
737 // for every point, decode the point name and store the numbers in the right
738 // place in the arrays fSurveyX etc.
741 TString pna; // point name
743 double x,y,z,precision;
745 in >> pna >> x >> y >> z >> type >> target >> precision;
746 if (in.fail()) break;
748 if (DecodeSurveyPointName(pna,i,j,k,l)) {
749 fSurveyX[i][j][k][l] = tocm*x;
750 fSurveyY[i][j][k][l] = tocm*y;
751 fSurveyZ[i][j][k][l] = tocm*z;
752 fSurveyEX[i][j][k][l] = precision/10; // "precision" is supposed to be in mm
753 fSurveyEY[i][j][k][l] = precision/10; // "precision" is supposed to be in mm
754 fSurveyEZ[i][j][k][l] = precision/10; // "precision" is supposed to be in mm
755 // if, at some point, separate precision numbers for x,y,z show up in the
756 // survey reports the function will fail here
757 printf("decoded %s %02d %d %d %d %8.2f %8.2f %8.2f %6.2f %6.2f %6.2f\n",
758 pna.Data(), i, j, k, l,
759 fSurveyX[i][j][k][l], fSurveyY[i][j][k][l], fSurveyZ[i][j][k][l],
760 fSurveyEX[i][j][k][l], fSurveyEY[i][j][k][l], fSurveyEZ[i][j][k][l]);
761 } else AliError(Form("cannot decode point name: %s",pna.Data()));
764 TString info = "Survey "+title+" "+date+" "+url+" "+version+" "+observations;
765 info.ReplaceAll("\r","");
766 fComment.SetString(info.Data());
770 //_____________________________________________________________________________
771 void AliTRDalignment::ReadSurveyReport(const AliSurveyObj * const so)
774 // Read survey report and store the numbers in fSurveyX, fSurveyY, fSurveyZ,
775 // and fSurveyE. Store the survey info in the fComment.
776 // Each supermodule has 8 survey points. The point names look like
777 // TRD_sm08ah0 and have the following meaning.
779 // sm00..17 mean supermodule 0 through 17, following the phi.
780 // Supermodule 00 is between phi=0 and phi=20 degrees.
782 // a or c denotes the anticlockwise and clockwise end of the supermodule
783 // in z. Clockwise end is where z is negative and where the muon arm sits.
785 // l or h denote low radius and high radius holes
787 // 0 or 1 denote the hole at smaller and at larger phi, respectively.
790 // read and process the data from the survey object
792 Int_t size = so->GetEntries();
793 printf("-> %d\n", size);
795 TString title = so->GetReportTitle();
796 TString date = so->GetReportDate();
797 TString subdetector = so->GetDetector();
798 TString url = so->GetURL();
799 TString report = so->GetReportNumber();
800 TString version = so->GetReportVersion();
801 TString observations = so->GetObservations();
802 TString system = so->GetCoordSys();
803 TString units = so->GetUnits();
805 // check what we found so far (watch out, they have \r at the end)
807 std::cout<<"title .........."<<title<<std::endl;
808 std::cout<<"date ..........."<<date<<std::endl;
809 std::cout<<"subdetector ...."<<subdetector<<std::endl;
810 std::cout<<"url ............"<<url<<std::endl;
811 std::cout<<"version ........"<<version<<std::endl;
812 std::cout<<"observations ..."<<observations<<std::endl;
813 std::cout<<"system ........."<<system<<std::endl;
814 std::cout<<"units .........."<<units<<std::endl;
816 if (!subdetector.Contains("TRD")) {
817 AliWarning(Form("Not a TRD survey file, subdetector = %s",subdetector.Data()));
820 double tocm = 0; // we want to have it in cm
821 if (units.Contains("mm")) tocm = 0.1;
822 else if (units.Contains("cm")) tocm = 1.0;
823 else if (units.Contains("m")) tocm = 100.0;
824 else if (units.Contains("pc")) tocm = 3.24078e-15;
826 AliError(Form("unexpected units: %s",units.Data()));
829 if (!system.Contains("ALICEPH")) {
830 AliError(Form("wrong system: %s, should be ALICEPH",system.Data()));
834 // for every survey point, decode the point name and store the numbers in
835 // the right place in the arrays fSurveyX etc.
837 TObjArray *points = so->GetData();
838 for (int ip = 0; ip<points->GetEntries(); ++ip) {
839 AliSurveyPoint *po = (AliSurveyPoint *) points->At(ip);
840 TString pna = po->GetPointName();
842 if (DecodeSurveyPointName(pna,i,j,k,l)) {
843 fSurveyX[i][j][k][l] = tocm*po->GetX();
844 fSurveyY[i][j][k][l] = tocm*po->GetY();
845 fSurveyZ[i][j][k][l] = tocm*po->GetZ();
846 fSurveyEX[i][j][k][l] = po->GetPrecisionX()/10; // "precision" is supposed to be in mm
847 fSurveyEY[i][j][k][l] = po->GetPrecisionY()/10;
848 fSurveyEZ[i][j][k][l] = po->GetPrecisionZ()/10;
849 printf("decoded %s %02d %d %d %d %8.2f %8.2f %8.2f %6.2f %6.2f %6.2f\n",
850 pna.Data(), i, j, k, l,
851 fSurveyX[i][j][k][l], fSurveyY[i][j][k][l], fSurveyZ[i][j][k][l],
852 fSurveyEX[i][j][k][l], fSurveyEY[i][j][k][l], fSurveyEZ[i][j][k][l]);
853 } else AliError(Form("cannot decode point name: %s",pna.Data()));
856 TString info = "Survey "+title+" "+date+" "+url+" "+report+" "+version+" "+observations;
857 info.ReplaceAll("\r","");
858 fComment.SetString(info.Data());
861 //_____________________________________________________________________________
862 double AliTRDalignment::SurveyChi2(int i, const double * const a) {
865 // Compare the survey results to the ideal positions of the survey marks
866 // in the local frame of supermodule. When transforming, use the alignment
867 // parameters a[6]. Return chi-squared.
870 if (!IsGeoLoaded()) return 0;
871 printf("Survey of supermodule %d\n",i);
872 AliAlignObjParams al(GetSmName(i),0,a[0],a[1],a[2],a[3],a[4],a[5],0);
874 TGeoPNEntry *pne = gGeoManager->GetAlignableEntry(GetSmName(i));
875 if (!pne) AliError(Form("no such physical node entry: %s",GetSmName(i)));
876 TGeoPhysicalNode *node = pne->GetPhysicalNode();
878 AliWarning(Form("physical node entry %s has no physical node; making a new one",GetSmName(i)));
879 node = gGeoManager->MakeAlignablePN(pne);
882 // al.ApplyToGeometry();
883 // node = pne->GetPhysicalNode(); // changed in the meantime
884 // TGeoHMatrix *ma = node->GetMatrix();
886 // a less destructive method (it does not modify geometry), gives the same result:
888 TGeoHMatrix *ma = new TGeoHMatrix();
889 al.GetLocalMatrix(*ma);
890 ma->MultiplyLeft(node->GetMatrix()); // global trafo, modified by a[]
893 printf(" sm z r phi x (lab phi) y (lab z) z (lab r) all in cm\n");
894 for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
895 if (fSurveyEX[i][j][k][l] == 0.0
896 && fSurveyEY[i][j][k][l] == 0.0
897 && fSurveyEZ[i][j][k][l] == 0.0) continue; // no data for this survey point
898 double master[3] = {fSurveyX[i][j][k][l],fSurveyY[i][j][k][l],fSurveyZ[i][j][k][l]};
900 ma->MasterToLocal(master,local);
901 double dx = local[0]-fSurveyX0[j][k][l];
902 double dy = local[1]-fSurveyY0[j][k][l];
903 double dz = local[2]-fSurveyZ0[j][k][l];
904 chi2 += dx*dx/fSurveyEX[i][j][k][l]/fSurveyEX[i][j][k][l];
905 chi2 += dy*dy/fSurveyEY[i][j][k][l]/fSurveyEY[i][j][k][l];
906 chi2 += dz*dz/fSurveyEZ[i][j][k][l]/fSurveyEZ[i][j][k][l];
907 printf("local survey %3d %3d %3d %3d %12.3f %12.3f %12.3f\n",i,j,k,l,local[0],local[1],local[2]);
908 printf("local ideal %12.3f %12.3f %12.3f\n",fSurveyX0[j][k][l],
909 fSurveyY0[j][k][l],fSurveyZ0[j][k][l]);
910 printf("difference %12.3f %12.3f %12.3f\n",dx,dy,dz);
912 printf("chi2 = %.2f\n",chi2);
916 //_____________________________________________________________________________
917 void trdAlignmentFcn(int &npar, double *g, double &f, double *par, int iflag) {
920 // Standard function as needed by Minuit-like minimization procedures.
921 // For the set of parameters par calculates and returns chi-squared.
924 // smuggle a C++ object into a C function
925 AliTRDalignment *alignment = (AliTRDalignment*) gMinuit->GetObjectFit();
927 f = alignment->SurveyChi2(par);
930 if (g) {} // no warnings about unused stuff...
934 //_____________________________________________________________________________
935 void AliTRDalignment::SurveyToAlignment(int i, const char * const flag) {
938 // Find the supermodule alignment parameters needed to make the survey
939 // results coincide with the ideal positions of the survey marks.
940 // The string flag should look like "101000"; the six characters corresponds
941 // to the six alignment parameters and 0/1 mean that the parameter should
942 // be fixed/released in the fit.
944 if (strlen(flag)!=6) {
945 AliError(Form("unexpected flag: %s",flag));
949 printf("Finding alignment matrix for supermodule %d\n",i);
950 fIbuffer[0] = i; // store the sm number in the buffer so minuit can see it
953 gMinuit->SetObjectFit(this);
954 fitter.SetFCN(trdAlignmentFcn);
955 fitter.SetParameter(0,"dx",0,0.5,0,0);
956 fitter.SetParameter(1,"dy",0,0.5,0,0);
957 fitter.SetParameter(2,"dz",0,0.5,0,0);
958 fitter.SetParameter(3,"rx",0,0.1,0,0);
959 fitter.SetParameter(4,"ry",0,0.1,0,0);
960 fitter.SetParameter(5,"rz",0,0.1,0,0);
962 for (int j=0; j<6; j++) if (flag[j]=='0') fitter.FixParameter(j);
966 fitter.ExecuteCommand("SET PRINT", arglist, 1);
967 fitter.ExecuteCommand("SET ERR", arglist, 1);
969 //fitter.ExecuteCommand("SIMPLEX", arglist, 1);
970 fitter.ExecuteCommand("MINIMIZE", arglist, 1);
971 fitter.ExecuteCommand("CALL 3", arglist,0);
973 for (int j=0; j<6; j++) a[j] = fitter.GetParameter(j);
975 for (int j=0; j<6; j++) printf("%10.3f ",fitter.GetParameter(j));
977 for (int j=0; j<6; j++) printf("%10.3f ",fitter.GetParError(j));
982 //_____________________________________________________________________________
983 void AliTRDalignment::ReadAny(const char * const filename)
986 // read the alignment data from any kind of file
989 TString fist(filename);
990 if (fist.EndsWith(".txt")) ReadAscii(filename);
991 if (fist.EndsWith(".dat")) ReadAscii(filename);
992 if (fist.EndsWith(".root")) {
993 if (fist.Contains("Run")) ReadDB(filename);
994 else ReadRoot(filename);
999 //_____________________________________________________________________________
1000 void AliTRDalignment::WriteAscii(const char * const filename) const
1003 // store the alignment data on ascii file
1006 FILE *fp = fopen(filename, "w");
1008 AliError(Form("cannot open output file %s",filename));
1019 //_____________________________________________________________________________
1020 void AliTRDalignment::WriteRoot(const char * const filename)
1023 // store the alignment data on root file
1026 TClonesArray *ar = new TClonesArray("AliAlignObjParams",10000);
1028 TFile fo(filename,"RECREATE");
1031 fo.WriteObject(ar,"TRDAlignObjs","kSingleKey");
1034 else AliError(Form("cannot open output file %s",filename));
1040 //_____________________________________________________________________________
1041 void AliTRDalignment::WriteDB(const char * const filename, int run0, int run1, int ver, int subver)
1044 // dumping on a DB-like file
1047 TClonesArray *ar = new TClonesArray("AliAlignObjParams",10000);
1049 const Char_t *path = "TRD/Align/Data";
1050 AliCDBId id(path,run0,run1);
1051 AliCDBMetaData *md = new AliCDBMetaData();
1052 md->SetResponsible("Dariusz Miskowiec");
1053 md->SetComment(fComment.GetString().Data());
1054 AliCDBEntry *e = new AliCDBEntry(ar, id, md);
1056 e->SetSubVersion(subver);
1057 TFile fi(filename,"RECREATE");
1062 else AliError(Form("cannot open input file %s",filename));
1072 //_____________________________________________________________________________
1073 void AliTRDalignment::WriteDB(char * const db, const char * const path, int run0, int run1)
1076 // store the alignment data in database
1079 TClonesArray *ar = new TClonesArray("AliAlignObjParams",10000);
1081 AliCDBManager *cdb = AliCDBManager::Instance();
1082 AliCDBStorage *storLoc = cdb->GetStorage(db);
1083 AliCDBMetaData *md = new AliCDBMetaData();
1084 md->SetResponsible("Dariusz Miskowiec");
1085 md->SetComment(fComment.GetString().Data());
1086 AliCDBId id(path,run0,run1);
1087 storLoc->Put(ar,id,md);
1093 //_____________________________________________________________________________
1094 void AliTRDalignment::WriteGeo(char *filename)
1097 // apply misalignment to current geometry and store the
1098 // resulting geometry on a root file
1101 TClonesArray *ar = new TClonesArray("AliAlignObjParams",10000);
1104 gGeoManager->Export(filename);
1108 //_____________________________________________________________________________
1109 double AliTRDalignment::GetSmRMS(int xyz) const
1117 for (int i = 0; i < 18; i++) {
1119 s2 += fSm[i][xyz]*fSm[i][xyz];
1121 double rms2 = s2/18.0 - s1*s1/18.0/18.0;
1123 return rms2>0 ? sqrt(rms2) : 0.0;
1127 //_____________________________________________________________________________
1128 double AliTRDalignment::GetChRMS(int xyz) const
1136 for (int i = 0; i < 540; i++) {
1138 s2 += fCh[i][xyz]*fCh[i][xyz];
1140 double rms2 = s2/540.0 - s1*s1/540.0/540.0;
1142 return rms2>0 ? sqrt(rms2) : 0.0;
1146 //_____________________________________________________________________________
1147 void AliTRDalignment::PrintSmRMS() const
1153 printf(" %11.4f %11.4f %11.4f %11.5f %11.5f %11.5f supermodule rms\n"
1154 ,GetSmRMS(0),GetSmRMS(1),GetSmRMS(2),GetSmRMS(3),GetSmRMS(4),GetSmRMS(5));
1158 //_____________________________________________________________________________
1159 void AliTRDalignment::PrintChRMS() const
1165 printf(" %11.4f %11.4f %11.4f %11.5f %11.5f %11.5f chamber rms\n"
1166 ,GetChRMS(0),GetChRMS(1),GetChRMS(2),GetChRMS(3),GetChRMS(4),GetChRMS(5));
1170 //_____________________________________________________________________________
1171 void AliTRDalignment::ArToNumbers(TClonesArray * const ar)
1174 // for each of the alignment objects in array ar extract the six local
1175 // alignment parameters; recognize by name to which supermodule or chamber
1176 // the alignment object pertains; set the respective fSm or fCh
1180 if (!IsGeoLoaded()) return;
1181 for (int i = 0; i < ar->GetEntries(); i++) {
1182 AliAlignObj *aao = (AliAlignObj *) ar->At(i);
1183 aao->ApplyToGeometry();
1190 //_____________________________________________________________________________
1191 void AliTRDalignment::NumbersToAr(TClonesArray * const ar)
1194 // build array of AliAlignObj objects based on fSm and fCh data
1195 // at the same time, apply misalignment to the currently loaded geometry
1196 // it is important to apply misalignment of supermodules before creating
1197 // alignment objects for chambers
1200 if (!IsGeoLoaded()) return;
1201 TClonesArray &alobj = *ar;
1203 for (int i = 0; i < 18; i++) {
1204 new(alobj[nobj]) AliAlignObjParams(GetSmName(i)
1206 ,fSm[i][0],fSm[i][1],fSm[i][2]
1207 ,fSm[i][3],fSm[i][4],fSm[i][5]
1209 ((AliAlignObj *) alobj[nobj])->ApplyToGeometry();
1213 for (int i = 0; i < 540; i++) {
1214 if (gGeoManager->GetAlignableEntry(GetChName(i))) {
1215 new(alobj[nobj]) AliAlignObjParams(GetChName(i)
1217 ,fCh[i][0],fCh[i][1],fCh[i][2]
1218 ,fCh[i][3],fCh[i][4],fCh[i][5]
1220 ((AliAlignObj *) alobj[nobj])->ApplyToGeometry();
1224 AliInfo("current geometry modified");
1228 //_____________________________________________________________________________
1229 int AliTRDalignment::IsGeoLoaded()
1232 // check whether a geometry is loaded
1233 // issue a warning if geometry is not ideal
1237 if (gGeoManager->GetListOfPhysicalNodes()->GetEntries()) AliWarning("current geometry is not ideal");
1240 AliError("first load geometry by calling TGeoManager::Import(filename)");
1246 //_____________________________________________________________________________