Coding conventions
[u/mrichter/AliRoot.git] / PWG3 / muondep / AliAnalysisTaskMuonRefit.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 /* $Id$ */
17
18 // ROOT includes
19 #include <TString.h>
20 #include <TGeoManager.h>
21
22 // STEER includes
23 #include "AliESDEvent.h"
24 #include "AliESDMuonTrack.h"
25 #include "AliCDBManager.h"
26 #include "AliCDBStorage.h"
27 #include "AliGeomManager.h"
28
29 // ANALYSIS includes
30 #include "AliAnalysisManager.h"
31 #include "AliInputEventHandler.h"
32 #include "AliAnalysisTaskMuonRefit.h"
33
34 // MUON includes
35 #include "AliMUONCDB.h"
36 #include "AliMUONRecoParam.h"
37 #include "AliMUONESDInterface.h"
38 #include "AliMUONRefitter.h"
39 #include "AliMUONTrack.h"
40 #include "AliMUONVCluster.h"
41 #include "AliMUONVTrackStore.h"
42 #include "AliMUONGeometryTransformer.h"
43
44 #ifndef SafeDelete
45 #define SafeDelete(x) if (x != NULL) { delete x; x = NULL; }
46 #endif
47
48 ClassImp(AliAnalysisTaskMuonRefit)
49
50 //________________________________________________________________________
51 AliAnalysisTaskMuonRefit::AliAnalysisTaskMuonRefit() :
52 AliAnalysisTaskSE(),
53 fDefaultStorage(""),
54 fImproveTracks(kFALSE),
55 fSigmaCut(-1.),
56 fSigmaCutForTrigger(-1.),
57 fReAlign(kFALSE),
58 fOldAlignStorage(""),
59 fNewAlignStorage(""),
60 fOldGeoTransformer(NULL),
61 fNewGeoTransformer(NULL),
62 fESDInterface(NULL),
63 fRefitter(NULL)
64 {
65   /// Default constructor
66 }
67
68 //________________________________________________________________________
69 AliAnalysisTaskMuonRefit::AliAnalysisTaskMuonRefit(const char *name) :
70 AliAnalysisTaskSE(name),
71 fDefaultStorage("raw://"),
72 fImproveTracks(kFALSE),
73 fSigmaCut(-1.),
74 fSigmaCutForTrigger(-1.),
75 fReAlign(kFALSE),
76 fOldAlignStorage(""),
77 fNewAlignStorage(""),
78 fOldGeoTransformer(NULL),
79 fNewGeoTransformer(NULL),
80 fESDInterface(NULL),
81 fRefitter(NULL)
82 {
83   /// Constructor
84   for (Int_t i = 0; i < AliMUONConstants::NTrackingCh(); i++) ResetClusterResolution(i, -1., -1.);
85 }
86
87 //________________________________________________________________________
88 AliAnalysisTaskMuonRefit::~AliAnalysisTaskMuonRefit()
89 {
90   /// Destructor
91   SafeDelete(fOldGeoTransformer);
92   SafeDelete(fNewGeoTransformer);
93   SafeDelete(fESDInterface);
94   SafeDelete(fRefitter);
95 }
96
97 //___________________________________________________________________________
98 void AliAnalysisTaskMuonRefit::UserCreateOutputObjects()
99 {
100 }
101
102 //________________________________________________________________________
103 void AliAnalysisTaskMuonRefit::UserExec(Option_t *)
104 {
105   /// Main event loop
106   
107   // check if refitter properly created
108   if (!fRefitter) return;
109   
110   AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
111   if (!esd) return;
112   
113   Int_t nTracks = (Int_t)esd->GetNumberOfMuonTracks();
114   if (nTracks < 1) return;
115   
116   // load the current event
117   fESDInterface->LoadEvent(*esd, kFALSE);
118   
119   // modify clusters
120   AliMUONVCluster* cluster = 0x0;
121   TIter nextCluster(fESDInterface->CreateClusterIterator());
122   while ((cluster = static_cast<AliMUONVCluster*>(nextCluster()))) ModifyCluster(*cluster);
123   
124   // refit the tracks from clusters
125   AliMUONVTrackStore* newTrackStore = fRefitter->ReconstructFromClusters();
126   
127   // loop over the list of ESD tracks
128   TClonesArray *esdTracks = (TClonesArray*) esd->FindListObject("MuonTracks");
129   for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
130     
131     // get the ESD track
132     AliESDMuonTrack* esdTrack = (AliESDMuonTrack*) esdTracks->UncheckedAt(iTrack);
133     
134     // skip ghost tracks (leave them unchanged)
135     if (!esdTrack->ContainTrackerData()) continue;
136     
137     // Find the corresponding re-fitted MUON track
138     AliMUONTrack* newTrack = (AliMUONTrack*) newTrackStore->FindObject(esdTrack->GetUniqueID());
139     
140     // replace the content of the current ESD track or remove it
141     if (newTrack && (!fImproveTracks || newTrack->IsImproved())) {
142       Double_t vertex[3] = {esdTrack->GetNonBendingCoor(), esdTrack->GetBendingCoor(), esdTrack->GetZ()};
143       AliMUONESDInterface::MUONToESD(*newTrack, *esdTrack, vertex, fESDInterface->GetDigits());
144       
145       // eventually remove the trigger part if matching chi2 do not pass the new cut
146       if (newTrack->GetChi2MatchTrigger() > fSigmaCutForTrigger*fSigmaCutForTrigger) {
147         esdTrack->SetLocalTrigger(0);
148         esdTrack->SetChi2MatchTrigger(0.);
149         esdTrack->SetHitsPatternInTrigCh(0);
150         esdTrack->SetTriggerX1Pattern(0);
151         esdTrack->SetTriggerY1Pattern(0);
152         esdTrack->SetTriggerX2Pattern(0);
153         esdTrack->SetTriggerY2Pattern(0);
154         esdTrack->SetTriggerX3Pattern(0);
155         esdTrack->SetTriggerY3Pattern(0);
156         esdTrack->SetTriggerX4Pattern(0);
157         esdTrack->SetTriggerY4Pattern(0);
158       }
159       
160     } else {
161       esdTracks->Remove(esdTrack);
162     }
163     
164   }
165   
166   // free memory
167   delete newTrackStore;
168   
169   // compress the array of ESD tracks
170   esdTracks->Compress();
171   
172 }
173
174 //________________________________________________________________________
175 void AliAnalysisTaskMuonRefit::NotifyRun()
176 {
177   /// load necessary data from OCDB and create the refitter
178   
179   AliCDBManager* cdbm = AliCDBManager::Instance();
180   cdbm->SetDefaultStorage(fDefaultStorage.Data());
181   cdbm->SetRun(fCurrentRunNumber);
182   
183   if (!AliMUONCDB::LoadField()) return;
184   
185   if (!AliMUONCDB::LoadMapping(kTRUE)) return;
186   
187   AliMUONRecoParam* recoParam = AliMUONCDB::LoadRecoParam();
188   if (!recoParam) return;
189   
190   if (fImproveTracks) {
191     if (fSigmaCut > 0.) recoParam->ImproveTracks(kTRUE, fSigmaCut);
192     else recoParam->ImproveTracks(kTRUE);
193   } else recoParam->ImproveTracks(kFALSE);
194   
195   if (fSigmaCutForTrigger > 0.) recoParam->SetSigmaCutForTrigger(fSigmaCutForTrigger);
196   else fSigmaCutForTrigger = recoParam->GetSigmaCutForTrigger();
197   
198   for (Int_t i = 0; i < AliMUONConstants::NTrackingCh(); i++) {
199     if (fClusterResNB[i] < 0.) fClusterResNB[i] = recoParam->GetDefaultNonBendingReso(i);
200     if (fClusterResB[i] < 0.) fClusterResB[i] = recoParam->GetDefaultBendingReso(i);
201   }
202   
203   if (fReAlign) {
204     
205     // recover default storage full name (raw:// cannot be used to set specific storage)
206     TString defaultStorage(cdbm->GetDefaultStorage()->GetType());
207     if (defaultStorage == "alien") defaultStorage += Form("://folder=%s", cdbm->GetDefaultStorage()->GetBaseFolder().Data());
208     else defaultStorage += Form("://%s", cdbm->GetDefaultStorage()->GetBaseFolder().Data());
209     
210     // reset existing geometry/alignment if any
211     if (cdbm->GetEntryCache()->Contains("GRP/Geometry/Data")) cdbm->UnloadFromCache("GRP/Geometry/Data");
212     if (cdbm->GetEntryCache()->Contains("MUON/Align/Data")) cdbm->UnloadFromCache("MUON/Align/Data");
213     if (AliGeomManager::GetGeometry()) AliGeomManager::GetGeometry()->UnlockGeometry();
214     
215     // get original geometry transformer
216     AliGeomManager::LoadGeometry();
217     if (!AliGeomManager::GetGeometry()) return;
218     if (fOldAlignStorage != "none") {
219       if (!fOldAlignStorage.IsNull()) cdbm->SetSpecificStorage("MUON/Align/Data",fOldAlignStorage.Data());
220       else cdbm->SetSpecificStorage("MUON/Align/Data",defaultStorage.Data());
221       AliGeomManager::ApplyAlignObjsFromCDB("MUON");
222     }
223     fOldGeoTransformer = new AliMUONGeometryTransformer();
224     fOldGeoTransformer->LoadGeometryData();
225     
226     // get new geometry transformer
227     cdbm->UnloadFromCache("GRP/Geometry/Data");
228     if (fOldAlignStorage != "none") cdbm->UnloadFromCache("MUON/Align/Data");
229     AliGeomManager::GetGeometry()->UnlockGeometry();
230     AliGeomManager::LoadGeometry();
231     if (!AliGeomManager::GetGeometry()) return;
232     if (!fNewAlignStorage.IsNull()) cdbm->SetSpecificStorage("MUON/Align/Data",fNewAlignStorage.Data());
233     else cdbm->SetSpecificStorage("MUON/Align/Data",defaultStorage.Data());
234     AliGeomManager::ApplyAlignObjsFromCDB("MUON");
235     fNewGeoTransformer = new AliMUONGeometryTransformer();
236     fNewGeoTransformer->LoadGeometryData();
237     
238   } else {
239     
240     // load geometry for track extrapolation to vertex
241     if (cdbm->GetEntryCache()->Contains("GRP/Geometry/Data")) cdbm->UnloadFromCache("GRP/Geometry/Data");
242     if (AliGeomManager::GetGeometry()) AliGeomManager::GetGeometry()->UnlockGeometry();
243     AliGeomManager::LoadGeometry();
244     if (!AliGeomManager::GetGeometry()) return;
245     
246   }
247   
248   fESDInterface = new AliMUONESDInterface();
249   fRefitter = new AliMUONRefitter(recoParam);
250   fRefitter->Connect(fESDInterface);
251 }
252
253 //________________________________________________________________________
254 void AliAnalysisTaskMuonRefit::Terminate(Option_t *)
255 {
256 }
257
258 //________________________________________________________________________
259 void AliAnalysisTaskMuonRefit::ModifyCluster(AliMUONVCluster& cl)
260 {
261   /// Reset the cluster resolution to the one given to the task and change
262   /// the cluster position according to the new alignment parameters if required
263   
264   Double_t gX,gY,gZ,lX,lY,lZ;
265   
266   // change their resolution
267   cl.SetErrXY(fClusterResNB[cl.GetChamberId()], fClusterResB[cl.GetChamberId()]);
268   
269   // change their position
270   if (fReAlign) {
271     gX = cl.GetX();
272     gY = cl.GetY();
273     gZ = cl.GetZ();
274     fOldGeoTransformer->Global2Local(cl.GetDetElemId(),gX,gY,gZ,lX,lY,lZ);
275     fNewGeoTransformer->Local2Global(cl.GetDetElemId(),lX,lY,lZ,gX,gY,gZ);
276     cl.SetXYZ(gX,gY,gZ);
277   }
278   
279 }
280