]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONAlignment.C
Minor script fixes
[u/mrichter/AliRoot.git] / MUON / MUONAlignment.C
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 /* $Id$ */
17
18 // ---
19 // Macro for MUON alignment using physics tracks. The macro uses AliMUONAlignment
20 // class to calculate the alignment parameters.
21 // An array for the alignment parameters is created and can be filled with
22 // initial values that will be used as starting values by the alignment
23 // algorithm.
24 // Ny default the macro run over galice.root in the working directory. If a file list
25 // of galice.root is provided as third argument the macro will run over all files.
26 // The macro loop over the files, events and tracks. For each track
27 // AliMUONAlignment::ProcessTrack(AliMUONTrack * track) and then
28 // AliMUONAlignment::LocalFit(Int_t iTrack, Double_t *lTrackParam, Int_t
29 // lSingleFit) are called. After all tracks have been procesed and fitted
30 // AliMUONAlignment::GlobalFit(Double_t *parameters,Double_t *errors,Double_t *pulls)
31 // is called. The array parameters contains the obatained misalignement parameters.
32 // A realigned geometry is generated in a local CDB.
33 //
34 // Authors: B. Becker and J. Castillo
35 // ---
36
37 #if !defined(__CINT__) || defined(__MAKECINT__)
38
39 #include "AliMUONAlignment.h"
40 #include "AliMUONTrack.h"
41 #include "AliMUONTrackExtrap.h"
42 #include "AliMUONTrackParam.h"
43 #include "AliMUONGeometryTransformer.h"
44 #include "AliMUONDataInterface.h"
45 #include "AliMUONVTrackStore.h"
46
47 #include "AliMagFMaps.h"
48 #include "AliTracker.h"
49 #include "AliCDBManager.h"
50 #include "AliCDBMetaData.h"
51 #include "AliCDBId.h"
52 #include "AliGeomManager.h"
53
54 #include <TString.h>
55 #include <TError.h>
56 #include <TH1.h>
57 #include <TGraphErrors.h>
58 #include <TFile.h>
59 #include <TClonesArray.h>
60 #include <Riostream.h>
61
62 #include <fstream>
63
64 #endif
65
66 void MUONAlignment(Int_t nEvents = 100000, char* geoFilename = "geometry.root", TString fileName = "galice.root", TString fileList = "")
67 {
68  
69   // Import TGeo geometry (needed by AliMUONTrackExtrap::ExtrapToVertex)
70   if ( ! AliGeomManager::GetGeometry() ) {
71     AliGeomManager::LoadGeometry(geoFilename);
72     if (! AliGeomManager::GetGeometry() ) {
73       Error("MUONAlignment", "getting geometry from file %s failed", geoFilename);
74       return;
75     }
76   }
77   
78   // set  mag field 
79   // waiting for mag field in CDB 
80   printf("Loading field map...\n");
81   AliMagFMaps* field = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
82   AliTracker::SetFieldMap(field, kFALSE);
83   // set the magnetic field for track extrapolations
84   AliMUONTrackExtrap::SetField(AliTracker::GetFieldMap());
85
86   Double_t parameters[3*156];
87   Double_t errors[3*156];
88   Double_t pulls[3*156];
89   for(Int_t k=0;k<3*156;k++) {
90     parameters[k]=0.;
91     errors[k]=0.;
92     pulls[k]=0.;
93   }
94
95   Double_t trackParams[8] = {0.,0.,0.,0.,0.,0.,0.,0.};
96
97   // Set initial values here, good guess may help convergence
98   // St 1 
99   //  Int_t iPar = 0;
100   //  parameters[iPar++] =  0.010300 ;  parameters[iPar++] =  0.010600 ;  parameters[iPar++] =  0.000396 ;  
101
102   bool bLoop = kFALSE;
103   ifstream sFileList;
104   if (fileList.Contains(".list")) {
105     cout << "Reading file list: " << fileList.Data() << endl;
106     bLoop = kTRUE;
107
108     TString fullListName(".");
109     fullListName +="/";
110     fullListName +=fileList;
111     sFileList.open(fileList.Data());
112   }
113   
114   TH1F *fInvBenMom = new TH1F("fInvBenMom","fInvBenMom",200,-0.1,0.1); 
115   TH1F *fBenMom = new TH1F("fBenMom","fBenMom",200,-40,40); 
116
117   AliMUONAlignment* alig = new AliMUONAlignment();
118   alig->InitGlobalParameters(parameters);
119
120   AliMUONGeometryTransformer *transform = new AliMUONGeometryTransformer();
121   transform->LoadGeometryData();
122   alig->SetGeometryTransformer(transform);
123
124   // Do alignment with magnetic field off
125   alig->SetBFieldOn(kFALSE);
126   
127   // Set tracking station to use
128   Bool_t bStOnOff[5] = {kTRUE,kTRUE,kTRUE,kTRUE,kTRUE};
129
130   // Fix parameters or add constraints here
131   for (Int_t iSt=0; iSt<5; iSt++)
132     if (!bStOnOff[iSt]) alig->FixStation(iSt+1);
133
134   // Left and right sides of the detector are independent, one can choose to align 
135   // only one side
136   Bool_t bSpecLROnOff[2] = {kTRUE,kTRUE};
137   alig->FixHalfSpectrometer(bStOnOff,bSpecLROnOff);
138
139   // Set predifined global constrains: X, Y, P, XvsZ, YvsZ, PvsZ, XvsY, YvsY, PvsY
140   Bool_t bVarXYT[9] = {kTRUE,kTRUE,kTRUE,kTRUE,kTRUE,kTRUE,kTRUE,kTRUE,kTRUE};
141   Bool_t bDetTLBR[4] = {kFALSE,kTRUE,kFALSE,kTRUE};
142   alig->AddConstraints(bStOnOff,bVarXYT,bDetTLBR,bSpecLROnOff);
143
144
145   char cFileName[100];  
146   AliMUONDataInterface *amdi =0x0;
147   AliMUONVTrackStore *trackStore =0x0;
148   AliMUONTrack* track =0x0;  
149     
150   Int_t lMaxFile = 1000;
151   Int_t iFile = 0;
152   Int_t iEvent = 0;
153   bool bKeepLoop = kTRUE;
154   Int_t iTrackTot=0;
155   Int_t iTrackOk=0;
156
157   while(bKeepLoop && iFile<lMaxFile){
158     iFile++;
159     if (bLoop) {
160       sFileList.getline(cFileName,100);
161       if (sFileList.eof()) bKeepLoop = kFALSE;
162     }
163     else {
164       sprintf(cFileName,fileName.Data());
165       bKeepLoop = kFALSE;
166     }
167     if (!strstr(cFileName,"galice.root")) continue;      
168     cout << "Using file: " << cFileName << endl;
169
170     if (!amdi) amdi = new AliMUONDataInterface(cFileName);
171     else amdi->Open(cFileName);
172
173     Int_t nevents = amdi->NumberOfEvents();
174     cout << "... with " << nevents << endl;
175     for(Int_t event = 0; event < nevents; event++) {
176       if (iEvent >= nEvents){
177         bKeepLoop = kFALSE;
178         break;
179       }
180       iEvent++;
181
182       trackStore = amdi->TrackStore(event);
183
184       Int_t ntracks = trackStore->GetSize();
185       if (!event%100)
186         cout << " there are " << ntracks << " tracks in event " << event << endl;
187       TIter next(trackStore->CreateIterator());
188       while ( ( track = static_cast<AliMUONTrack*>(next()) ) ) {
189         AliMUONTrackParam trackParam(*((AliMUONTrackParam*)(track->GetTrackParamAtHit()->First())));
190         AliMUONTrackExtrap::ExtrapToVertex(&trackParam,0.,0.,0.);
191         Double_t invBenMom = trackParam.GetInverseBendingMomentum();
192         fInvBenMom->Fill(invBenMom);
193         fBenMom->Fill(1./invBenMom);
194         if (TMath::Abs(invBenMom)<=1.04) {
195           alig->ProcessTrack(track);
196           alig->LocalFit(iTrackOk++,trackParams,0);
197         }
198         iTrackTot++;
199       }
200     }
201     cout << "Processed " << iTrackTot << " Tracks so far." << endl;
202   }
203   alig->GlobalFit(parameters,errors,pulls);
204
205   cout << "Done with GlobalFit " << endl;
206
207   // Store results
208   Double_t DEid[156] = {0};
209   Double_t MSDEx[156] = {0};
210   Double_t MSDEy[156] = {0};
211   Double_t MSDExt[156] = {0};
212   Double_t MSDEyt[156] = {0};
213   Double_t DEidErr[156] = {0};
214   Double_t MSDExErr[156] = {0};
215   Double_t MSDEyErr[156] = {0};
216   Double_t MSDExtErr[156] = {0};
217   Double_t MSDEytErr[156] = {0};
218   Int_t lNDetElem = 4*2+4*2+18*2+26*2+26*2;
219   Int_t lNDetElemCh[10] = {4,4,4,4,18,18,26,26,26,26};
220   // Int_t lSNDetElemCh[10] = {4,8,12,16,34,52,78,104,130,156};
221   Int_t idOffset = 0; // 400
222   Int_t lSDetElemCh = 0;
223   for(Int_t iDE=0; iDE<lNDetElem; iDE++){
224     DEidErr[iDE] = 0.;
225     DEid[iDE] = idOffset+100;
226     DEid[iDE] += iDE; 
227     lSDetElemCh = 0;
228     for(Int_t iCh=0; iCh<9; iCh++){
229       lSDetElemCh += lNDetElemCh[iCh];
230       if (iDE>=lSDetElemCh) {
231         DEid[iDE] += 100;
232         DEid[iDE] -= lNDetElemCh[iCh];
233       }
234     }
235     MSDEx[iDE]=parameters[3*iDE+0];
236     MSDEy[iDE]=parameters[3*iDE+1];
237     MSDExt[iDE]=parameters[3*iDE+2];
238     MSDEyt[iDE]=parameters[3*iDE+2];
239     MSDExErr[iDE]=(Double_t)alig->GetParError(3*iDE+0);
240     MSDEyErr[iDE]=(Double_t)alig->GetParError(3*iDE+1);
241     MSDExtErr[iDE]=(Double_t)alig->GetParError(3*iDE+2);
242     MSDEytErr[iDE]=(Double_t)alig->GetParError(3*iDE+2);
243   }
244
245   cout << "Let's create graphs ...  " << endl;
246
247   TGraphErrors *gMSDEx = new TGraphErrors(lNDetElem,DEid,MSDEx,DEidErr,MSDExErr); 
248   TGraphErrors *gMSDEy = new TGraphErrors(lNDetElem,DEid,MSDEy,DEidErr,MSDEyErr); 
249   TGraphErrors *gMSDExt = new TGraphErrors(lNDetElem,DEid,MSDExt,DEidErr,MSDExtErr); 
250   TGraphErrors *gMSDEyt = new TGraphErrors(lNDetElem,DEid,MSDEyt,DEidErr,MSDEytErr); 
251
252   cout << "... graphs created, open file ...  " << endl;
253
254   TFile *hFile = new TFile("measShifts.root","RECREATE");
255
256   cout << "... file opened ...  " << endl;
257
258   gMSDEx->Write("gMSDEx");
259   gMSDEy->Write("gMSDEy");
260   gMSDExt->Write("gMSDExt");
261   gMSDEyt->Write("gMSDEyt");
262   fInvBenMom->Write();
263   fBenMom->Write();
264   hFile->Close();
265   
266   cout << "... and closed!" << endl;
267   // Re Align
268   AliMUONGeometryTransformer *newTransform = alig->ReAlign(transform,parameters,true); 
269   newTransform->WriteTransformations("transform2ReAlign.dat");
270   
271   // Generate realigned data in local cdb
272   const TClonesArray* array = newTransform->GetMisAlignmentData();
273    
274   // CDB manager
275   AliCDBManager* cdbManager = AliCDBManager::Instance();
276   cdbManager->SetDefaultStorage("local://ReAlignCDB");
277   
278   AliCDBMetaData* cdbData = new AliCDBMetaData();
279   cdbData->SetResponsible("Dimuon Offline project");
280   cdbData->SetComment("MUON alignment objects with residual misalignment");
281   AliCDBId id("MUON/Align/Data", 0, 0); 
282   cdbManager->Put(const_cast<TClonesArray*>(array), id, cdbData);
283
284