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