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