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