]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ACORDE/AliACORDEAlign.cxx
8b6a64b2ff60c384181e484261fd76b0542ed305
[u/mrichter/AliRoot.git] / ACORDE / AliACORDEAlign.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 #include "AliACORDEAlign.h"
17 #include "TROOT.h"
18 #include "Riostream.h"
19 #include "TFile.h"
20 #include "TMath.h"
21 #include "TSystem.h"
22 #include "AliSurveyObj.h"
23 #include "AliAlignObjParams.h"
24 #include "AliCDBStorage.h"
25 #include <TClonesArray.h>
26 #include <TFile.h>
27 #include "AliLog.h"
28 #include "AliCDBManager.h"
29 #include "AliSurveyPoint.h" 
30 #include "AliACORDEConstants.h" 
31
32 // Class creating the ACORDE aligmnent objects 
33 // from the surveys done by surveyers at Point2.
34 // Survey results are fetched from 
35 // Survey Depot, based on survey results 
36 // position of ACORDE alignment objects is computed.
37
38
39 ClassImp(AliACORDEAlign)
40
41
42 //________________________________________________________________________
43 AliACORDEAlign::AliACORDEAlign() :
44   TObject(),
45   fFileGlob(0x0),
46   fRepLoc(0),
47   fRepGlob(0),
48   fUser(0x0),  
49   fX(),
50   fAlignACORDEObjArray(0x0),
51   fDebug(0)
52 {
53   //
54   //  default constructor
55   //
56 }
57
58 AliACORDEAlign::AliACORDEAlign(/*Int_t reportloc,*/Int_t reportglob):
59   TObject(),
60   fFileGlob(0x0),
61   fRepLoc(0),
62   fRepGlob(0),
63   fUser(0x0),
64   fX(120,4),
65   fAlignACORDEObjArray(0x0),
66   fDebug(0)
67 {
68   
69 //
70   // constructor
71   //fRepLoc = new reportloc[80];
72   //fRepGlob = new reportglob[80];
73   Char_t path[50];
74    fFileGlob = new Char_t[80];
75    fUser = new Char_t[10];
76   snprintf(path,50,"%s",gSystem->Getenv("ALICE_ROOT")); 
77   // 
78   snprintf(fFileGlob,80,"%s/ACORDE/Survey_%d_ACORDE.txt",path,reportglob);
79   //
80  snprintf(fUser,10,"%s",gSystem->Getenv("alien_API_USER"));
81
82 }
83
84
85
86
87 AliACORDEAlign::AliACORDEAlign(const AliACORDEAlign &align):
88   TObject(),
89   fFileGlob(0x0),
90   fRepLoc(0),
91   fRepGlob(0),
92   fUser(0x0),
93   fX(),
94   fAlignACORDEObjArray(0x0),
95   fDebug(0)
96 {
97   //
98   //  default copy constructor
99 //TObject = align.TObject;
100 fFileGlob = align.fFileGlob;
101 fRepLoc = align.fRepLoc;
102 fRepGlob = align.fRepGlob;
103 fUser = align.fUser;
104 fX = align.fX;
105 fAlignACORDEObjArray = align.fAlignACORDEObjArray;
106 fDebug = align.fDebug;
107 }
108
109 //__________________________________________________________________________
110 AliACORDEAlign & AliACORDEAlign::operator =(const AliACORDEAlign &align)
111 {
112   //
113   // assignment operator - dummy
114
115  //
116   if (&align==this) return *this;
117   new (this) AliACORDEAlign(align);
118   return (*this);
119 }
120
121 //__________________________________________________________________________
122 AliACORDEAlign::~AliACORDEAlign(){
123   //
124   // destructor
125   //
126 if(fAlignACORDEObjArray) delete fAlignACORDEObjArray;
127   if(fFileGlob) delete[] fFileGlob;
128   if(fUser) delete[] fUser;
129 }
130
131
132 void AliACORDEAlign::LoadSurveyData()
133 {
134
135 //
136 // Create a new survey object and fill it.
137  
138 AliSurveyObj * s1 = new AliSurveyObj(); 
139
140 if(fRepLoc != 0) 
141  { 
142  // Filling from DCDB (via GRID)
143  s1->SetGridUser(fUser);
144  s1->Fill("ACORDE",1014872,1,fUser); 
145  }
146  else
147  {
148    s1->FillFromLocalFile(fFileGlob);
149  }
150
151
152  //s1->GetEntries();
153  //s1->GetUnits();
154  //TObjArray* arr = s1->GetData();
155  //cout<< "number of entries " << arr->GetEntries() <<endl;
156  //arr->UncheckedAt(0)->ClassName();
157  //AliSurveyPoint *sp0 = (AliSurveyPoint*) arr->UncheckedAt(0);   
158  //cout << "point name " << sp0->GetPointName() << endl  ;
159   
160
161 //
162 TString ML= "M" ;
163 //TString PL= "P";
164 TString underscore =  "_";
165 TString  endInner =  "_I";
166 TString  endOuter =  "_O";
167 TString  endCenter = "_P";
168 //
169 TString surveyname;
170 TString surveynameInner;
171 TString surveynameOuter;
172 TString surveynameCenter;
173
174 //TString surveynameAngles;
175 // 
176 TString pointNamesInner[60];
177 TString pointNamesOuter[60];
178 TString pointNamesCenter[60];
179
180 //
181 Int_t  nid=0;
182 //
183  //for regular modules 
184  for (Int_t ncolum=0; ncolum<6; ncolum++)
185    {
186      for (Int_t nrow=0; nrow<10; nrow++)
187        {        
188          
189          surveyname=ML;
190          surveyname+=ncolum;
191          surveyname+=underscore;
192          surveyname+=nrow;
193   
194          surveynameInner=surveyname; 
195          surveynameInner+=endInner;
196
197          surveynameOuter=surveyname;
198          surveynameOuter+=endOuter; 
199                           
200          surveynameCenter=surveyname;
201          surveynameCenter+=endCenter;
202   
203         pointNamesInner[nid] =  surveynameInner;
204         pointNamesOuter[nid] = surveynameOuter; 
205         pointNamesCenter[nid] = surveynameCenter;
206          ++nid; 
207        }
208    }
209
210
211 //Read  two points 
212 AliSurveyPoint  *InnerPoint;
213 AliSurveyPoint *OuterPoint
214
215 AliSurveyPoint  *CenterPoint;
216
217
218  for(Int_t i=0;i<60;i++)
219  {
220
221    InnerPoint=0;
222    OuterPoint=0; 
223    CenterPoint=0;
224  
225    InnerPoint = (AliSurveyPoint *) s1->GetData()->FindObject(pointNamesInner[i]);
226    OuterPoint = (AliSurveyPoint *) s1->GetData()->FindObject(pointNamesOuter[i]);
227    CenterPoint = (AliSurveyPoint *) s1->GetData()->FindObject(pointNamesCenter[i]);
228
229
230
231   if(InnerPoint && OuterPoint)
232    {     
233      //Use center if it is available
234      if(CenterPoint)
235        { 
236          fX(i+60,0) =  CenterPoint->GetX()*100;  
237          fX(i+60,1) =  CenterPoint->GetY()*100; 
238          fX(i+60,2) =  CenterPoint->GetZ()*100;
239         }
240       else
241         {
242       //calculate center point 
243          fX(i+60,0) = 100*(InnerPoint->GetX() + OuterPoint->GetX())/2.0;  
244          fX(i+60,1) = 100*(InnerPoint->GetY() + OuterPoint->GetY())/2.0; 
245          fX(i+60,2) = 100*(InnerPoint->GetZ() + OuterPoint->GetZ())/2.0;
246         } 
247    
248       fX(i,0) =  OuterPoint->GetX()*100;  
249       fX(i,1) =  OuterPoint->GetY()*100;
250       fX(i,2) =  OuterPoint->GetZ()*100;
251    }
252    else 
253    {
254        if(InnerPoint && CenterPoint) 
255          {
256
257           fX(i+60,0) =  CenterPoint->GetX()*100;  
258           fX(i+60,1) =  CenterPoint->GetY()*100; 
259           fX(i+60,2) =  CenterPoint->GetZ()*100;
260
261           fX(i,0) =  InnerPoint->GetX()*100;  
262           fX(i,1) =  InnerPoint->GetY()*100;
263           fX(i,2) =  InnerPoint->GetZ()*100;   
264          } 
265         else
266         {
267           if(OuterPoint && CenterPoint)
268             { 
269         
270              fX(i+60,0) =  CenterPoint->GetX()*100;  
271              fX(i+60,1) =  CenterPoint->GetY()*100; 
272              fX(i+60,2) =  CenterPoint->GetZ()*100;
273
274              fX(i,0) =  OuterPoint->GetX()*100;  
275              fX(i,1) =  OuterPoint->GetY()*100;
276              fX(i,2) =  OuterPoint->GetZ()*100;   
277             }
278           else
279             { 
280
281              fX(i+60,0) = -99.0;  
282              fX(i+60,1) = -99.0; 
283              fX(i+60,2) = -99.0;
284
285              fX(i,0) =  -99.0;  
286              fX(i,1) =  -99.0;
287              fX(i,2) =  -99.0;   
288             
289             }
290         }  
291    } 
292
293
294  }//ends  for
295
296  delete s1;
297
298 }
299
300 void  AliACORDEAlign::ComputePosition()
301 {
302
303
304 //Residuals for rotations
305
306 Double_t theta;
307 Double_t resphi[60]; 
308 Double_t resiphi;
309
310 for (Int_t imod=0; imod<60; imod++)
311   {
312    if(TMath::Abs(fX(imod+60,0)-fX(imod,0))>=0.000001)
313    {
314    theta = (fX(imod+60,1)-fX(imod,1))/(fX(imod+60,0)-fX(imod,0));
315    resiphi = TMath::ATan(theta)*(180.0/TMath::Pi());
316    // calculate the residuals  special  modules 
317    if(imod==0 || imod==9 || imod==50 || imod==59 )
318     {    
319     resphi[imod] = 0.0-resiphi;
320     continue; 
321     }
322    // for module with no measurements 
323    if(imod == 42 )
324     {
325      resphi[imod]= 0.0;
326     continue;
327     } 
328    //face A
329    if(imod>0 && imod <20)
330     {
331     resphi[imod] = resiphi + 45.0;  
332     }
333    //face B
334    if(imod>=20 && imod <40)
335     {
336     resphi[imod] = -resiphi;  
337     }
338    //face C
339    if(imod>=40 && imod <60)
340     {
341     resphi[imod] = resiphi - 45.0;  
342     }
343  
344    }
345
346 }
347
348
349 //Get the  residuals for translations 
350
351 AliCDBManager* cdb = AliCDBManager::Instance();
352 if(!cdb->IsDefaultStorageSet()) cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
353 cdb->SetRun(0);
354
355 //AliCDBStorage* storage;
356 AliGeomManager::LoadGeometry(); 
357
358
359 TString symname;
360 TString basename = "ACORDE/Array";
361
362
363 // Get the ideal directly from the geometry 
364  Double_t *tr;
365  TGeoHMatrix *matrix;  
366   for (Int_t imod=0; imod<60; imod++)
367   {
368     symname = basename;
369     symname += imod; 
370     cout<< symname << endl;
371     matrix = AliGeomManager::GetMatrix(symname);
372     tr=matrix->GetTranslation();  
373
374     if(imod == 42)
375       {
376     fX(imod+60,0) = 0.0;  
377     fX(imod+60,1) = 0.0;  
378     fX(imod+60,2) = 0.0; 
379     fX(imod,0) = 0.0;  
380     fX(imod,1) = 0.0;  
381     fX(imod,2) = 0.0; 
382     continue;
383       }
384
385     fX(imod+60,0)=fX(imod+60,0)- tr[0];
386     fX(imod+60,1)=fX(imod+60,1)- tr[1]- 4.0;
387     fX(imod+60,2)=fX(imod+60,2)- tr[2];  
388   
389     fX(imod,0) = resphi[imod];  
390     fX(imod,1) = 0.0;  
391     fX(imod,2) = 0.0; 
392
393    }
394   
395
396
397
398 }
399
400 //______________________________________________________________________
401 void AliACORDEAlign::Run(){
402   //
403   // runs the full chain
404   //
405   
406   //if(!LoadSurveyFromAlienFile("ACORDE",999999,1))
407   //{
408    // cout<<"Missing points"<<endl;
409     //return;
410   //}
411   //else 
412   //{
413     //LoadSurveyfromLocalFile("ACORDE",99999,1);
414   //} 
415
416
417   LoadSurveyData();
418   ComputePosition();
419   //CreateACORDEAlignObjs();
420   StoreAlignObj();
421
422 }
423
424 //_________________________________________________________________________
425
426 void AliACORDEAlign::StoreAlignObj()
427 {
428   //
429   // Storing ACORDE alignment objects 
430   //
431
432   AliCDBManager* cdb = AliCDBManager::Instance();
433   if(!cdb->IsDefaultStorageSet()) cdb->SetDefaultStorage("local://$ALICE_ROOT");
434
435   TClonesArray *array = new TClonesArray("AliAlignObjParams",60);
436   //
437   // storing either in the OCDB or local file
438   //
439
440   TString symname;
441   TString basename = "ACORDE/Array";
442   Int_t iIndex=0; 
443
444   AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer;
445   UShort_t volid = AliGeomManager::LayerToVolUID(iLayer,iIndex);
446
447   Double_t dx=0., dy=0., dz=0., dpsi=0., dtheta=0., dphi=0.0;
448   
449    for (Int_t imod=0; imod<60; imod++)
450      {
451        
452        dphi = fX(imod,0);
453        dtheta = fX(imod,1);
454        dpsi = fX(imod,2);
455        dx = fX(imod+60,0);
456        dy = fX(imod+60,1);
457        dz = fX(imod+60,2);  
458        symname = basename;
459        symname +=  imod;         
460        new((*array)[imod]) AliAlignObjParams(symname,volid,dx,dy,dz,dpsi,dtheta,dphi,kFALSE);     
461      }
462
463
464   if( TString(gSystem->Getenv("TOCDB"))!= TString("kTRUE") )
465    {
466    
467     
468
469  // save on file
470     const char* filename = "ACORDESurveyMisalignment.root";
471     Char_t fullname[80];
472
473     
474
475     snprintf(fullname,80,"%s",filename);
476        
477    
478     TFile *f = new TFile(fullname,"RECREATE");
479
480     
481     if(!f)
482       {
483         AliError("cannot open file for output\n");
484         return;
485       }
486     AliInfo(Form("Saving alignment objects to the file %s", filename));
487     f->cd();
488     f->WriteObject(array,"ACORDEAlignObjs","kSingleKey");
489     f->Close();
490   }
491   else
492     {
493       // save in CDB storage
494       AliCDBStorage* storage;
495       //
496        TString Storage = gSystem->Getenv("STORAGE");
497        if(!Storage.BeginsWith("local://") && !Storage.BeginsWith("alien://"))
498          {
499            AliError(Form("STORAGE variable set to %s is not valid. Exiting\n",Storage.Data()));
500            return;
501          }
502        storage = cdb->GetStorage(Storage.Data());
503        if(!storage)
504          {
505            AliError(Form("Unable to open storage %s\n",Storage.Data()));
506            return;
507          }
508        //
509        AliCDBMetaData* md = new AliCDBMetaData();
510        md->SetResponsible("Pedro Podesta");
511        md->SetComment("Full misalignment of ACORDE from surveyors");
512        AliCDBId id("ACORDE/Align/Data",0,AliCDBRunRange::Infinity());
513        storage->Put(fAlignACORDEObjArray,id,md);
514     }
515
516 }