]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONESDInterface.cxx
Adding analysis task
[u/mrichter/AliRoot.git] / MUON / AliMUONESDInterface.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 #include "AliMUONESDInterface.h"
19 #include "AliMUONTrack.h"
20 #include "AliMUONVTrackStore.h"
21 #include "AliMUONVCluster.h"
22 #include "AliMUONVClusterStore.h"
23 #include "AliMUONVDigit.h"
24 #include "AliMUONVDigitStore.h"
25 #include "AliMUONLocalTrigger.h"
26 #include "AliMUONTriggerTrack.h"
27 #include "AliMUONVTriggerStore.h"
28 #include "AliMUONVTriggerTrackStore.h"
29 #include "AliMUON2DMapIterator.h"
30 #include "AliMUONTrackParam.h"
31 #include "AliMUONTrackExtrap.h"
32 #include "AliMUONConstants.h"
33 #include "AliMUONTracker.h"
34 #include "AliMUONRecoParam.h"
35 #include "AliMUONVTrackReconstructor.h"
36 #include "AliMUONCDB.h"
37
38 #include "AliMpExMapIterator.h"
39 #include "AliMpVSegmentation.h"
40 #include "AliMpSegmentation.h"
41 #include "AliMpPad.h"
42
43 #include "AliESDEvent.h"
44 #include "AliESDMuonTrack.h"
45 #include "AliESDMuonCluster.h"
46 #include "AliESDMuonPad.h"
47 #include "AliLog.h"
48
49 #include <TROOT.h>
50 #include <TClass.h>
51 #include <TIterator.h>
52 #include <TMath.h>
53 #include <TMatrixD.h>
54 #include <Riostream.h>
55 #include <TGeoGlobalMagField.h>
56 #include <TVirtualMagField.h>
57
58 //-----------------------------------------------------------------------------
59 /// \class AliMUONESDInterface
60 ///
61 /// There are 2 way of using thid converter between MUON track/cluster/digit
62 /// and ESDMuon track/cluster/pad:
63 /// 
64 /// 1) using the static methods converting the objects one by one
65 ///
66 /// 2) loading a whole ESDEvent and using the finders and/or the iterators
67 ///    to access the corresponding MUON objects
68 ///
69 /// note: You can set the recoParam used to refit the MUON track with ResetTracker(...);
70 ///       By default we use Kalman filter + Smoother
71 ///
72 /// \author Philippe Pillot
73 //-----------------------------------------------------------------------------
74
75 using std::endl;
76 using std::cout;
77 /// \cond CLASSIMP
78 ClassImp(AliMUONESDInterface)
79 /// \endcond
80
81 AliMUONRecoParam* AliMUONESDInterface::fgRecoParam = 0x0;
82 AliMUONVTrackReconstructor* AliMUONESDInterface::fgTracker = 0x0;
83
84 TString AliMUONESDInterface::fgTrackStoreName = "AliMUONTrackStoreV1";
85 TString AliMUONESDInterface::fgClusterStoreName = "AliMUONClusterStoreV2";
86 TString AliMUONESDInterface::fgDigitStoreName = "AliMUONDigitStoreV2R";
87 TString AliMUONESDInterface::fgTriggerStoreName = "AliMUONTriggerStoreV1";
88 TString AliMUONESDInterface::fgTriggerTrackStoreName = "AliMUONTriggerTrackStoreV1";
89
90 //_____________________________________________________________________________
91 AliMUONESDInterface::AliMUONESDInterface()
92 : TObject(),
93 fTracks(0x0),
94 fDigits(0x0),
95 fTriggers(0x0),
96 fClusterMap(0x0),
97 fDigitMap(0x0)
98 {
99   /// Default constructor
100 }
101
102 //_____________________________________________________________________________
103 AliMUONESDInterface::~AliMUONESDInterface()
104 {
105   /// Destructor
106   delete fTracks;
107   delete fDigits;
108   delete fTriggers;
109   delete fClusterMap;
110   delete fDigitMap;
111 }
112
113 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
114 //                    methods to play with internal objects                    //
115 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
116
117 //_____________________________________________________________________________
118 void AliMUONESDInterface::Clear(Option_t*)
119 {
120   /// clear memory
121   delete fTracks; fTracks = 0x0;
122   delete fDigits; fDigits = 0x0;
123   delete fTriggers; fTriggers = 0x0;
124   delete fClusterMap; fClusterMap = 0x0;
125   delete fDigitMap; fDigitMap = 0x0;
126 }
127
128 //_____________________________________________________________________________
129 void AliMUONESDInterface::Reset()
130 {
131   /// reset stores and maps
132   
133   if (fTracks) fTracks->Clear("C");
134   else fTracks = NewTrackStore();
135   
136   if (fDigits) fDigits->Clear("C");
137   else fDigits = NewDigitStore();
138   
139   if (fTriggers) fTriggers->Clear("C");
140   else fTriggers = NewTriggerStore();
141   
142   if (fClusterMap) fClusterMap->Clear();
143   else fClusterMap = new AliMpExMap;
144   fClusterMap->SetOwner(kTRUE);
145   
146   if (fDigitMap) fDigitMap->Clear();
147   else fDigitMap = new AliMpExMap;
148   fDigitMap->SetOwner(kTRUE);
149 }
150
151 //_____________________________________________________________________________
152 void AliMUONESDInterface::LoadEvent(AliESDEvent& esdEvent, Bool_t refit)
153 {
154   /// Extract MUON data from the given ESD event
155   
156   // reset data members
157   Reset();
158   
159   // loop over ESD pads and fill the digit store
160   for (Int_t iPad = 0; iPad < esdEvent.GetNumberOfMuonPads(); iPad++)
161     Add(*(esdEvent.GetMuonPad(iPad)), *fDigits);
162   
163   // loop over ESD tracks and fill the stores
164   for (Int_t iTrack = 0; iTrack < esdEvent.GetNumberOfMuonTracks(); iTrack++) {
165     
166     // get ESD track
167     AliESDMuonTrack* esdTrack = esdEvent.GetMuonTrack(iTrack);
168     
169     // fill trigger store if related info are availables
170     if (esdTrack->ContainTriggerData()) Add(*esdTrack, *fTriggers);
171     
172     // fill tracker data if availables
173     if (!esdTrack->ContainTrackerData()) continue;
174     
175     // add it to track store
176     AliMUONTrack* track = Add(*esdTrack, *fTracks, refit);
177     
178     // prepare cluster map
179     AliMpExMap* cMap = new AliMpExMap;
180     cMap->SetOwner(kFALSE);
181     fClusterMap->Add(esdTrack->GetUniqueID(), cMap);
182     
183     // prepare digit maps
184     AliMpExMap* dMaps = new AliMpExMap;
185     dMaps->SetOwner(kTRUE);
186     fDigitMap->Add(esdTrack->GetUniqueID(), dMaps);
187     
188     // loop over clusters
189     for (Int_t iCluster = 0; iCluster < track->GetNClusters(); iCluster++) {
190       
191       // get cluster
192       AliMUONVCluster* cluster = ((AliMUONTrackParam*)track->GetTrackParamAtCluster()->UncheckedAt(iCluster))->GetClusterPtr();
193       
194       // fill cluster map
195       cMap->Add(cluster->GetUniqueID(), cluster);
196       
197       // prepare digit map
198       AliMpExMap* dMap =new AliMpExMap;
199       dMap->SetOwner(kFALSE);
200       dMaps->Add(cluster->GetUniqueID(), dMap);
201       
202       // loop over digits
203       for (Int_t iDigit = 0; iDigit < cluster->GetNDigits(); iDigit++) {
204         
205         // find the digit
206         AliMUONVDigit* digit = fDigits->FindObject(cluster->GetDigitId(iDigit));
207         
208         // fill digit map
209         if (digit) dMap->Add(digit->GetUniqueID(), digit);
210         else AliError("a digit is missing");
211         
212       } // end of loop over pads
213       
214     } // end of loop over clusters
215     
216   } // end of loop over tracks
217   
218 }
219
220 //___________________________________________________________________________
221 Int_t AliMUONESDInterface::GetNTracks() const
222 {
223   /// return the number of tracks
224   return fTracks ? fTracks->GetSize() : 0;
225 }
226
227 //___________________________________________________________________________
228 Int_t AliMUONESDInterface::GetNClusters() const
229 {
230   /// return the number of clusters
231   Int_t nClusters = 0;
232   AliMUONTrack *track;
233   TIter next(CreateTrackIterator());
234   while ((track = static_cast<AliMUONTrack*>(next()))) nClusters += track->GetNClusters();
235   return nClusters;
236 }
237
238 //___________________________________________________________________________
239 Int_t AliMUONESDInterface::GetNClusters(UInt_t trackId) const
240 {
241   /// return the number of clusters in track "trackId"
242   AliMUONTrack* track = FindTrack(trackId);
243   return track ? track->GetNClusters() : 0;
244 }
245
246 //___________________________________________________________________________
247 Int_t AliMUONESDInterface::GetNDigits() const
248 {
249   /// return the number of digits
250   return fDigits ? fDigits->GetSize() : 0;
251 }
252
253 //___________________________________________________________________________
254 Int_t AliMUONESDInterface::GetNDigits(UInt_t trackId) const
255 {
256   /// return the number of digits in all clusters of track "trackId"
257   Int_t nDigits = 0;
258   AliMUONVCluster *cluster;
259   TIter next(CreateClusterIterator(trackId));
260   while ((cluster = static_cast<AliMUONVCluster*>(next()))) nDigits += cluster->GetNDigits();
261   return nDigits;
262 }
263
264 //___________________________________________________________________________
265 Int_t AliMUONESDInterface::GetNDigits(UInt_t trackId, UInt_t clusterId) const
266 {
267   /// return the number of digits in cluster numbered "iCluster" of track "iTrack"
268   AliMUONVCluster* cluster = FindCluster(trackId, clusterId);
269   return cluster ? cluster->GetNDigits() : 0;
270 }
271
272 //___________________________________________________________________________
273 Int_t AliMUONESDInterface::GetNDigitsInCluster(UInt_t clusterId) const
274 {
275   /// return the number of digits in cluster "clusterId"
276   AliMUONVCluster* cluster = FindCluster(clusterId);
277   return cluster ? cluster->GetNDigits() : 0;
278 }
279
280 //___________________________________________________________________________
281 Int_t AliMUONESDInterface::GetNTriggers() const
282 {
283   /// return the number of triggers
284   return fTriggers ? fTriggers->GetSize() : 0;
285 }
286
287 //___________________________________________________________________________
288 Bool_t AliMUONESDInterface::DigitsStored(UInt_t trackId) const
289 {
290   /// return kTRUE if digits have been stored for all clusters of track "trackId"
291   AliMUONVCluster *cluster;
292   TIter next(CreateClusterIterator(trackId));
293   while ((cluster = static_cast<AliMUONVCluster*>(next())))
294     if (cluster->GetNDigits() == 0) return kFALSE;
295   return kTRUE;
296 }
297
298 //___________________________________________________________________________
299 AliMUONTrack* AliMUONESDInterface::FindTrack(UInt_t trackId) const
300 {
301   /// return track "trackId" (0x0 if not found)
302   AliMUONTrack *track = fTracks ? static_cast<AliMUONTrack*>(fTracks->FindObject(trackId)) : 0x0;
303   if (!track) AliWarning(Form("track %d does not exist",trackId));
304   return track;
305 }
306
307 //___________________________________________________________________________
308 AliMUONVCluster* AliMUONESDInterface::FindCluster(UInt_t clusterId) const
309 {
310   /// loop over tracks and return the first cluster "clusterId" found (0x0 if not found)
311   AliMpExMap *cMap;
312   AliMUONVCluster* cluster = 0x0;
313   
314   if (fClusterMap) {
315     
316     TIter next(fClusterMap->CreateIterator());
317     while ((cMap = static_cast<AliMpExMap*>(next()))) {
318       
319       cluster = static_cast<AliMUONVCluster*>(cMap->GetValue(clusterId));
320       if (cluster) return cluster;
321       
322     }
323     
324   }
325   
326   if (!cluster) AliWarning(Form("cluster %d does not exist",clusterId));
327   return 0x0;
328 }
329
330 //___________________________________________________________________________
331 AliMUONVCluster* AliMUONESDInterface::FindCluster(UInt_t trackId, UInt_t clusterId) const
332 {
333   /// return cluster "clusterId" in track "trackId" (0x0 if not found)
334   AliMpExMap *cMap = fClusterMap ? static_cast<AliMpExMap*>(fClusterMap->GetValue(trackId)) : 0x0;
335   AliMUONVCluster* cluster = cMap ? static_cast<AliMUONVCluster*>(cMap->GetValue(clusterId)) : 0x0;
336   if (!cluster) AliWarning(Form("cluster %d does not exist in track %d", clusterId, trackId));
337   return cluster;
338 }
339
340 //___________________________________________________________________________
341 AliMUONVDigit* AliMUONESDInterface::FindDigit(UInt_t digitId) const
342 {
343   /// return digit "digitId" (0x0 if not found)
344   AliMUONVDigit *digit = fDigits ? fDigits->FindObject(digitId) : 0x0;
345   if (!digit) AliWarning(Form("digit %d does not exist",digitId));
346   return digit;
347 }
348
349 //___________________________________________________________________________
350 AliMUONLocalTrigger* AliMUONESDInterface::FindLocalTrigger(Int_t boardNumber) const
351 {
352   /// return MUON local trigger "boardNumber"
353   return (fTriggers) ? fTriggers->FindLocal(boardNumber) : 0x0;
354 }
355
356 //___________________________________________________________________________
357 TIterator* AliMUONESDInterface::CreateTrackIterator() const
358 {
359   /// return iterator over all tracks
360   return fTracks ? fTracks->CreateIterator() : 0x0;
361 }
362
363 //___________________________________________________________________________
364 TIterator* AliMUONESDInterface::CreateClusterIterator() const
365 {
366   /// return iterator over all clusters
367   return fClusterMap ? new AliMUON2DMapIterator(*fClusterMap) : 0x0;
368 }
369
370 //___________________________________________________________________________
371 TIterator* AliMUONESDInterface::CreateClusterIterator(UInt_t trackId) const
372 {
373   /// return iterator over clusters of track "trackId"
374   AliMpExMap *cMap = fClusterMap ? static_cast<AliMpExMap*>(fClusterMap->GetValue(trackId)) : 0x0;
375   return cMap ? cMap->CreateIterator() : 0x0;
376 }
377
378 //___________________________________________________________________________
379 TIterator* AliMUONESDInterface::CreateDigitIterator() const
380 {
381   /// return iterator over all digits
382   return fDigits ? fDigits->CreateIterator() : 0x0;
383 }
384
385 //___________________________________________________________________________
386 TIterator* AliMUONESDInterface::CreateDigitIterator(UInt_t trackId) const
387 {
388   /// return iterator over all digits of track "trackId"
389   AliMpExMap* dMaps = fDigitMap ? static_cast<AliMpExMap*>(fDigitMap->GetValue(trackId)) : 0x0;
390   return dMaps ? new AliMUON2DMapIterator(*dMaps) : 0x0;
391 }
392
393 //___________________________________________________________________________
394 TIterator* AliMUONESDInterface::CreateDigitIterator(UInt_t trackId, UInt_t clusterId) const
395 {
396   /// return iterator over digits of cluster "clusterId" in track "trackId"
397   AliMpExMap* dMaps = fDigitMap ? static_cast<AliMpExMap*>(fDigitMap->GetValue(trackId)) : 0x0;
398   AliMpExMap* dMap = dMaps ? static_cast<AliMpExMap*>(dMaps->GetValue(clusterId)) : 0x0;
399   return dMap ? dMap->CreateIterator() : 0x0;
400 }
401
402 //___________________________________________________________________________
403 TIterator* AliMUONESDInterface::CreateDigitIteratorInCluster(UInt_t clusterId) const
404 {
405   /// return iterator over digits of the first cluster "clusterId" found by looping over all tracks
406   AliMpExMap *dMaps;
407   AliMpExMap* dMap = 0x0;
408   
409   if (fDigitMap) {
410     
411     TIter next(fDigitMap->CreateIterator());
412     while ((dMaps = static_cast<AliMpExMap*>(next()))) {
413       
414       dMap = static_cast<AliMpExMap*>(dMaps->GetValue(clusterId));
415       if (dMap) return dMap->CreateIterator();
416       
417     }
418     
419   }
420   
421   return 0x0;
422 }
423
424 //___________________________________________________________________________
425 TIterator* AliMUONESDInterface::CreateLocalTriggerIterator() const
426 {
427   /// return iterator over all local trigger
428   return fTriggers ? fTriggers->CreateLocalIterator() : 0x0;
429 }
430
431 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
432 //                                static methods                               //
433 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
434
435 //_____________________________________________________________________________
436 void AliMUONESDInterface::ResetTracker(const AliMUONRecoParam* recoParam, Bool_t info)
437 {
438   /// Reset the MUON tracker using "recoParam" if provided.
439   /// If not provided, will get them from OCDB.
440   /// Load the magnetic field from OCDB if not already set.
441   
442   delete fgTracker;
443   delete fgRecoParam;
444   
445   if (recoParam) {
446     
447     // use provided recoParam
448     fgRecoParam = new AliMUONRecoParam(*recoParam);
449     
450     if (info) AliInfoClass("will refit tracks with provided RecoParam:");
451     
452   } else {
453     
454     // or get them from OCDB
455     fgRecoParam = AliMUONCDB::LoadRecoParam();
456     
457     if (!fgRecoParam) AliFatalClass("RecoParam have not been set!");
458     
459   }
460   
461   // print useful parameters for refitting
462   if (info) {
463     cout<<"                                     --> Tracking mode = "<<fgRecoParam->GetTrackingMode()<<endl;
464     if (fgRecoParam->UseSmoother()) cout<<"                                     --> Use Smoother"<<endl;
465     else cout<<"                                     --> Do not use smoother"<<endl;
466     cout<<"                                     --> Vertex dispersion in bending direction = "
467     <<fgRecoParam->GetBendingVertexDispersion()<<" cm"<<endl;
468   }
469   
470   // load magnetic field for track extrapolation if not already set
471   if (!TGeoGlobalMagField::Instance()->GetField() && !AliMUONCDB::LoadField())
472     AliFatalClass("Magnetic field has not been set!");
473   
474   fgTracker = AliMUONTracker::CreateTrackReconstructor(fgRecoParam,0x0);
475   
476 }
477
478 //_____________________________________________________________________________
479 AliMUONVTrackStore* AliMUONESDInterface::NewTrackStore()
480 {
481   /// Create an empty track store of type fgTrackStoreName
482   TClass* classPtr = TClass::GetClass(fgTrackStoreName);
483   if (!classPtr || !classPtr->InheritsFrom("AliMUONVTrackStore")) {
484     AliErrorClass(Form("Unable to create store of type %s", fgTrackStoreName.Data()));
485     return 0x0;
486   }
487   return reinterpret_cast<AliMUONVTrackStore*>(gROOT->ProcessLineFast(Form("new %s()",fgTrackStoreName.Data())));
488 }
489
490 //_____________________________________________________________________________
491 AliMUONVClusterStore* AliMUONESDInterface::NewClusterStore()
492 {
493   /// Create an empty cluster store of type fgClusterStoreName
494   TClass* classPtr = TClass::GetClass(fgClusterStoreName);
495   if (!classPtr || !classPtr->InheritsFrom("AliMUONVClusterStore")) {
496     AliErrorClass(Form("Unable to create store of type %s", fgClusterStoreName.Data()));
497     return 0x0;
498   }
499   return reinterpret_cast<AliMUONVClusterStore*>(gROOT->ProcessLineFast(Form("new %s()",fgClusterStoreName.Data())));
500 }
501
502 //_____________________________________________________________________________
503 AliMUONVCluster* AliMUONESDInterface::NewCluster()
504 {
505   /// Create an empty cluster of type associated to the cluster store of type fgClusterStoreName
506   TClass* classPtr = TClass::GetClass(fgClusterStoreName);
507   if (!classPtr || !classPtr->InheritsFrom("AliMUONVClusterStore")) {
508     AliErrorClass(Form("Unable to create store of type %s", fgClusterStoreName.Data()));
509     return 0x0;
510   }
511   AliMUONVClusterStore* cStore = reinterpret_cast<AliMUONVClusterStore*>(classPtr->New());
512   AliMUONVCluster* cluster = cStore->CreateCluster(0,0,0);
513   delete cStore;
514   return cluster;
515 }
516
517 //_____________________________________________________________________________
518 AliMUONVDigitStore* AliMUONESDInterface::NewDigitStore()
519 {
520   /// Create an empty digit store of type fgDigitStoreName
521   TClass* classPtr = TClass::GetClass(fgDigitStoreName);
522   if (!classPtr || !classPtr->InheritsFrom("AliMUONVDigitStore")) {
523     AliErrorClass(Form("Unable to create store of type %s", fgDigitStoreName.Data()));
524     return 0x0;
525   }
526   return reinterpret_cast<AliMUONVDigitStore*>(gROOT->ProcessLineFast(Form("new %s()",fgDigitStoreName.Data())));
527 }
528
529 //_____________________________________________________________________________
530 AliMUONVDigit* AliMUONESDInterface::NewDigit()
531 {
532   /// Create an empty digit of type associated to the digit store of type fgDigitStoreName
533   TClass* classPtr = TClass::GetClass(fgDigitStoreName);
534   if (!classPtr || !classPtr->InheritsFrom("AliMUONVDigitStore")) {
535     AliErrorClass(Form("Unable to create store of type %s", fgDigitStoreName.Data()));
536     return 0x0;
537   }
538   AliMUONVDigitStore* dStore = reinterpret_cast<AliMUONVDigitStore*>(classPtr->New());
539   AliMUONVDigit* digit = dStore->CreateDigit(0,0,0,0);
540   delete dStore;
541   return digit;
542 }
543
544 //_____________________________________________________________________________
545 AliMUONVTriggerStore* AliMUONESDInterface::NewTriggerStore()
546 {
547   /// Create an empty trigger store of type fgTriggerStoreName
548   TClass* classPtr = TClass::GetClass(fgTriggerStoreName);
549   if (!classPtr || !classPtr->InheritsFrom("AliMUONVTriggerStore")) {
550     AliErrorClass(Form("Unable to create store of type %s", fgTriggerStoreName.Data()));
551     return 0x0;
552   }
553   return reinterpret_cast<AliMUONVTriggerStore*>(gROOT->ProcessLineFast(Form("new %s()",fgTriggerStoreName.Data())));
554 }
555
556 //_____________________________________________________________________________
557 AliMUONVTriggerTrackStore* AliMUONESDInterface::NewTriggerTrackStore()
558 {
559   /// Create an empty trigger track store of type fgTriggerTrackStoreName
560   TClass* classPtr = TClass::GetClass(fgTriggerTrackStoreName);
561   if (!classPtr || !classPtr->InheritsFrom("AliMUONVTriggerTrackStore")) {
562     AliErrorClass(Form("Unable to create store of type %s", fgTriggerTrackStoreName.Data()));
563     return 0x0;
564   }
565   return reinterpret_cast<AliMUONVTriggerTrackStore*>(gROOT->ProcessLineFast(Form("new %s()",fgTriggerTrackStoreName.Data())));
566 }
567
568 //_________________________________________________________________________
569 void AliMUONESDInterface::GetParamAtVertex(const AliESDMuonTrack& esdTrack, AliMUONTrackParam& trackParam)
570 {
571   /// Get parameters at vertex from ESDMuon track
572   trackParam.SetZ(esdTrack.GetZ());
573   trackParam.SetNonBendingCoor(esdTrack.GetNonBendingCoor());
574   trackParam.SetNonBendingSlope(TMath::Tan(esdTrack.GetThetaX()));
575   trackParam.SetBendingCoor(esdTrack.GetBendingCoor());
576   trackParam.SetBendingSlope(TMath::Tan(esdTrack.GetThetaY()));
577   trackParam.SetInverseBendingMomentum(esdTrack.GetInverseBendingMomentum());
578 }
579
580 //_________________________________________________________________________
581 void AliMUONESDInterface::GetParamAtDCA(const AliESDMuonTrack& esdTrack, AliMUONTrackParam& trackParam)
582 {
583   /// Get parameters at DCA from ESDMuon track
584   trackParam.SetZ(esdTrack.GetZ());
585   trackParam.SetNonBendingCoor(esdTrack.GetNonBendingCoorAtDCA());
586   trackParam.SetNonBendingSlope(TMath::Tan(esdTrack.GetThetaXAtDCA()));
587   trackParam.SetBendingCoor(esdTrack.GetBendingCoorAtDCA());
588   trackParam.SetBendingSlope(TMath::Tan(esdTrack.GetThetaYAtDCA()));
589   trackParam.SetInverseBendingMomentum(esdTrack.GetInverseBendingMomentumAtDCA());
590 }
591
592 //_________________________________________________________________________
593 void AliMUONESDInterface::GetParamAtFirstCluster(const AliESDMuonTrack& esdTrack, AliMUONTrackParam& trackParam)
594 {
595   /// Get parameters at first cluster from ESDMuon track
596   trackParam.SetZ(esdTrack.GetZUncorrected());
597   trackParam.SetNonBendingCoor(esdTrack.GetNonBendingCoorUncorrected());
598   trackParam.SetNonBendingSlope(TMath::Tan(esdTrack.GetThetaXUncorrected()));
599   trackParam.SetBendingCoor(esdTrack.GetBendingCoorUncorrected());
600   trackParam.SetBendingSlope(TMath::Tan(esdTrack.GetThetaYUncorrected()));
601   trackParam.SetInverseBendingMomentum(esdTrack.GetInverseBendingMomentumUncorrected());
602 }
603
604 //_________________________________________________________________________
605 void AliMUONESDInterface::GetParamCov(const AliESDMuonTrack& esdTrack, AliMUONTrackParam& trackParam)
606 {
607   /// Get parameters covariances from ESD track
608   
609   // get ESD covariance matrix
610   TMatrixD covariances(5,5);
611   esdTrack.GetCovariances(covariances);
612   
613   // compute Jacobian to change the coordinate system
614   // from (X,thetaX,Y,thetaY,c/pYZ) to (X,slopeX,Y,slopeY,c/pYZ)
615   Double_t cosThetaX = TMath::Cos(esdTrack.GetThetaXUncorrected());
616   Double_t cosThetaY = TMath::Cos(esdTrack.GetThetaYUncorrected());
617   TMatrixD jacob(5,5);
618   jacob.Zero();
619   jacob(0,0) = 1.;
620   jacob(1,1) = 1. / cosThetaX / cosThetaX;
621   jacob(2,2) = 1.;
622   jacob(3,3) = 1. / cosThetaY / cosThetaY;
623   jacob(4,4) = 1.;
624   
625   // compute covariance matrix in ESD coordinate system
626   TMatrixD tmp(covariances,TMatrixD::kMultTranspose,jacob);
627   trackParam.SetCovariances(TMatrixD(jacob,TMatrixD::kMult,tmp));
628   
629 }
630
631 //_________________________________________________________________________
632 void AliMUONESDInterface::SetParamAtVertex(const AliMUONTrackParam& trackParam, AliESDMuonTrack& esdTrack)
633 {
634   /// Set parameters in ESD track
635   esdTrack.SetZ(trackParam.GetZ());
636   esdTrack.SetNonBendingCoor(trackParam.GetNonBendingCoor());
637   esdTrack.SetThetaX(TMath::ATan(trackParam.GetNonBendingSlope()));
638   esdTrack.SetBendingCoor(trackParam.GetBendingCoor()); 
639   esdTrack.SetThetaY(TMath::ATan(trackParam.GetBendingSlope()));
640   esdTrack.SetInverseBendingMomentum(trackParam.GetInverseBendingMomentum());
641 }
642
643 //_________________________________________________________________________
644 void AliMUONESDInterface::SetParamAtDCA(const AliMUONTrackParam& trackParam, AliESDMuonTrack& esdTrack)
645 {
646   /// Set parameters in ESD track
647   esdTrack.SetNonBendingCoorAtDCA(trackParam.GetNonBendingCoor());
648   esdTrack.SetThetaXAtDCA(TMath::ATan(trackParam.GetNonBendingSlope()));
649   esdTrack.SetBendingCoorAtDCA(trackParam.GetBendingCoor()); 
650   esdTrack.SetThetaYAtDCA(TMath::ATan(trackParam.GetBendingSlope()));
651   esdTrack.SetInverseBendingMomentumAtDCA(trackParam.GetInverseBendingMomentum());
652 }
653
654 //_________________________________________________________________________
655 void AliMUONESDInterface::SetParamAtFirstCluster(const AliMUONTrackParam& trackParam, AliESDMuonTrack& esdTrack)
656 {
657   /// Set parameters in ESD track
658   esdTrack.SetZUncorrected(trackParam.GetZ());
659   esdTrack.SetNonBendingCoorUncorrected(trackParam.GetNonBendingCoor());
660   esdTrack.SetThetaXUncorrected(TMath::ATan(trackParam.GetNonBendingSlope()));
661   esdTrack.SetBendingCoorUncorrected(trackParam.GetBendingCoor()); 
662   esdTrack.SetThetaYUncorrected(TMath::ATan(trackParam.GetBendingSlope()));
663   esdTrack.SetInverseBendingMomentumUncorrected(trackParam.GetInverseBendingMomentum());
664 }
665
666 //_________________________________________________________________________
667 void AliMUONESDInterface::SetParamCov(const AliMUONTrackParam& trackParam, AliESDMuonTrack& esdTrack)
668 {
669   /// Set parameters covariances in ESD track
670   
671   // set null matrix if covariances does not exist
672   if (!trackParam.CovariancesExist()) {
673     TMatrixD tmp(5,5);
674     tmp.Zero();
675     esdTrack.SetCovariances(tmp);
676     return;
677   }
678   
679   // compute Jacobian to change the coordinate system
680   // from (X,slopeX,Y,slopeY,c/pYZ) to (X,thetaX,Y,thetaY,c/pYZ)
681   Double_t cosThetaX = TMath::Cos(TMath::ATan(trackParam.GetNonBendingSlope()));
682   Double_t cosThetaY = TMath::Cos(TMath::ATan(trackParam.GetBendingSlope()));
683   TMatrixD jacob(5,5);
684   jacob.Zero();
685   jacob(0,0) = 1.;
686   jacob(1,1) = cosThetaX * cosThetaX;
687   jacob(2,2) = 1.;
688   jacob(3,3) = cosThetaY * cosThetaY;
689   jacob(4,4) = 1.;
690   
691   // compute covariance matrix in ESD coordinate system
692   TMatrixD tmp(trackParam.GetCovariances(),TMatrixD::kMultTranspose,jacob);
693   esdTrack.SetCovariances(TMatrixD(jacob,TMatrixD::kMult,tmp));
694   
695 }
696
697 //_____________________________________________________________________________
698 void AliMUONESDInterface::ESDToMUON(const AliESDMuonTrack& esdTrack, AliMUONTrack& track, Bool_t refit)
699 {
700   /// Transfert data from ESDMuon track to MUON track.
701   /// ESDMuon track must hold the pointer to the ESD event otherwise we cannot recover the clusters.
702   /// If refit = kTRUE, the track parameters at each cluster are obtained by refitting the track
703   /// or by extrapolating the parameters at the first one if the refit failed.
704   /// If refit = kFALSE, only the track parameters at first cluster are valid.
705   /// note: You can set the recoParam used to refit the MUON track with ResetTracker(...);
706   ///       By default we use Kalman filter + Smoother
707   
708   // if the ESDMuon track is a ghost then return an empty MUON track
709   if (!esdTrack.ContainTrackerData()) {
710     track.Reset();
711     track.SetUniqueID(esdTrack.GetUniqueID());
712     return;
713   }
714   
715   track.Clear("C");
716   
717   // global info
718   track.SetUniqueID(esdTrack.GetUniqueID());
719   track.FitWithVertex(kFALSE);
720   track.FitWithMCS(kFALSE);
721   track.SetImproved(kFALSE);
722   track.SetVertexErrXY2(0.,0.);
723   track.SetGlobalChi2(esdTrack.GetChi2());
724   track.SetMatchTrigger(esdTrack.GetMatchTrigger());
725   track.SetChi2MatchTrigger(esdTrack.GetChi2MatchTrigger());
726   track.SetHitsPatternInTrigCh(esdTrack.GetHitsPatternInTrigCh());
727   track.SetHitsPatternInTrigChTrk(esdTrack.GetHitsPatternInTrigChTrk());
728   track.SetLocalTrigger(esdTrack.LoCircuit(), esdTrack.LoStripX(), esdTrack.LoStripY(),
729                         esdTrack.LoDev(), esdTrack.LoLpt(), esdTrack.LoHpt(),
730                         esdTrack.GetTriggerWithoutChamber());
731   track.Connected(esdTrack.IsConnected());
732   
733   // track parameters at vertex
734   AliMUONTrackParam paramAtVertex;
735   GetParamAtVertex(esdTrack, paramAtVertex);
736   track.SetTrackParamAtVertex(&paramAtVertex);
737     
738   // track parameters at first cluster
739   AliMUONTrackParam param;
740   GetParamAtFirstCluster(esdTrack, param);
741   GetParamCov(esdTrack, param);
742   
743   // create empty cluster
744   AliMUONVCluster* cluster = NewCluster();
745   
746   // fill TrackParamAtCluster with track parameters at each cluster if available
747   Bool_t clusterFound = kFALSE;
748   if(esdTrack.GetESDEvent()) {
749     
750     // loop over cluster Id
751     for (Int_t i = 0; i < esdTrack.GetNClusters(); i++) {
752       
753       // recover the ESDMuon cluster
754       AliESDMuonCluster *esdCluster = esdTrack.GetESDEvent()->FindMuonCluster(esdTrack.GetClusterId(i));
755       if (esdCluster) clusterFound = kTRUE;
756       else {
757         AliErrorClass("a cluster is missing in ESD");
758         continue;
759       }
760       
761       // copy cluster information
762       ESDToMUON(*esdCluster, *cluster);
763       
764       // only set the Z parameter to avoid error in the AddTrackParamAtCluster(...) method
765       param.SetZ(cluster->GetZ());
766       
767       // add common track parameters at current cluster
768       track.AddTrackParamAtCluster(param, *cluster, kTRUE);
769       
770     }
771     
772     // refit the track to get better parameters and covariances at each cluster (temporary disable track improvement)
773     if (clusterFound && refit) {
774       
775       AliMUONTrackParam *firstTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
776       AliMUONTrackParam paramSave(*firstTrackParam);
777       
778       if (!fgTracker) ResetTracker();
779       
780       if (!fgTracker->RefitTrack(track, kFALSE) && track.GetGlobalChi2() < AliMUONTrack::MaxChi2()) {
781         *firstTrackParam = paramSave;
782         track.UpdateCovTrackParamAtCluster();
783       }
784       
785     }
786     
787   }
788   
789   // fill TrackParamAtCluster with only track parameters at first (fake) cluster if no cluster found in ESD
790   if (!clusterFound) {
791     
792     // get number of the first hit chamber according to the MUONClusterMap
793     Int_t firstCh = 0;
794     while (firstCh < 10 && !esdTrack.IsInMuonClusterMap(firstCh)) firstCh++;
795     
796     // produce fake cluster at this chamber
797     cluster->SetUniqueID(AliMUONVCluster::BuildUniqueID(firstCh, 0, 0));
798     cluster->SetXYZ(param.GetNonBendingCoor(), param.GetBendingCoor(), param.GetZ());
799     cluster->SetErrXY(0., 0.);
800     
801     // add track parameters at first (fake) cluster
802     track.AddTrackParamAtCluster(param, *cluster, kTRUE);
803     
804   }
805   
806   // set the MC label from ESD track
807   track.SetMCLabel(esdTrack.GetLabel());
808   
809   delete cluster;
810   
811 }
812
813 //_____________________________________________________________________________
814 void AliMUONESDInterface::ESDToMUON(const AliESDMuonTrack& esdTrack, AliMUONLocalTrigger& locTrg)
815 {
816   /// Transfert trigger data from ESDMuon track to the MUONLocalTtrigger object
817   
818   // reset the local trigger content
819   AliMUONLocalTrigger emptyLocTrg;
820   locTrg = emptyLocTrg;
821   
822   // if the ESDMuon track is a ghost then return an empty local trigger
823   if (!esdTrack.ContainTriggerData()) return;
824   
825   locTrg.SetUniqueID(esdTrack.GetUniqueID());
826   locTrg.SetLoCircuit(esdTrack.LoCircuit());
827   locTrg.SetLoStripX(esdTrack.LoStripX());
828   locTrg.SetLoStripY(esdTrack.LoStripY());
829   locTrg.SetDeviation(esdTrack.LoDev());
830   locTrg.SetLoLpt(esdTrack.LoLpt());
831   locTrg.SetLoHpt(esdTrack.LoHpt());
832   for (Int_t ich = 0; ich < 4; ich++)
833     if (esdTrack.TriggerFiredWithoutChamber(ich))
834       locTrg.SetTriggerWithoutChamber(ich);
835   locTrg.SetLoTrigY(0);
836   locTrg.SetX1Pattern(esdTrack.GetTriggerX1Pattern());
837   locTrg.SetX2Pattern(esdTrack.GetTriggerX2Pattern());
838   locTrg.SetX3Pattern(esdTrack.GetTriggerX3Pattern());
839   locTrg.SetX4Pattern(esdTrack.GetTriggerX4Pattern());
840   locTrg.SetY1Pattern(esdTrack.GetTriggerY1Pattern());
841   locTrg.SetY2Pattern(esdTrack.GetTriggerY2Pattern());
842   locTrg.SetY3Pattern(esdTrack.GetTriggerY3Pattern());
843   locTrg.SetY4Pattern(esdTrack.GetTriggerY4Pattern());
844   
845 }
846
847 //_____________________________________________________________________________
848 void AliMUONESDInterface::ESDToMUON(const AliESDMuonCluster& esdCluster, AliMUONVCluster& cluster)
849 {
850   /// Transfert data from ESDMuon cluster to MUON cluster
851   
852   cluster.Clear("C");
853   
854   cluster.SetUniqueID(esdCluster.GetUniqueID());
855   cluster.SetXYZ(esdCluster.GetX(), esdCluster.GetY(), esdCluster.GetZ());
856   cluster.SetErrXY(esdCluster.GetErrX(),esdCluster.GetErrY());
857   cluster.SetCharge(esdCluster.GetCharge());
858   cluster.SetChi2(esdCluster.GetChi2());
859   cluster.SetMCLabel(esdCluster.GetLabel());
860   
861   cluster.SetDigitsId(esdCluster.GetNPads(), esdCluster.GetPadsId());
862   
863 }
864
865 //___________________________________________________________________________
866 void AliMUONESDInterface::ESDToMUON(const AliESDMuonPad& esdPad, AliMUONVDigit& digit)
867 {
868   /// Transfert data from ESDMuon pad to MUON digit
869   /// Load mapping from OCDB if not already set
870   
871   if (!AliMpSegmentation::Instance(kFALSE) && !AliMUONCDB::LoadMapping(kTRUE))
872     AliFatalClass("mapping segmentation has not been set!");
873   
874   const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(esdPad.GetDetElemId(), esdPad.GetManuId());  
875   AliMpPad pad = seg->PadByLocation(esdPad.GetManuId(), esdPad.GetManuChannel(), kFALSE);
876   
877   digit.Saturated(esdPad.IsSaturated());
878   digit.Used(kFALSE);
879   digit.Calibrated(esdPad.IsCalibrated());
880   digit.SetUniqueID(esdPad.GetUniqueID());
881   digit.SetCharge(esdPad.GetCharge());
882   digit.SetADC(esdPad.GetADC());
883   digit.SetPadXY(pad.GetIx(), pad.GetIy());
884   
885 }
886
887 //_____________________________________________________________________________
888 void AliMUONESDInterface::MUONToESD(const AliMUONTrack& track, AliESDEvent& esd, const Double_t vertex[3],
889                                     const AliMUONVDigitStore* digits, const AliMUONLocalTrigger* locTrg)
890 {
891   /// Transfert data from MUON track to ESD event (ESDTrack + ESDClusters)
892   /// Incorporate the ESDPads if the digits are provided
893   /// Add trigger info if the MUON track is matched with a trigger track
894   
895   // transfert track info
896   AliESDMuonTrack *esdTrack = esd.NewMuonTrack();
897   MUONToESD(track, *esdTrack, vertex, locTrg);
898   
899   // transfert cluster info if any
900   for (Int_t i = 0; i < track.GetNClusters(); i++) {
901     AliMUONTrackParam *param = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->UncheckedAt(i));
902     MUONToESD(*(param->GetClusterPtr()), esd, digits);
903   }
904   
905 }
906
907 //_____________________________________________________________________________
908 void AliMUONESDInterface::MUONToESD(const AliMUONTrack& track, AliESDMuonTrack& esdTrack,
909                                     const Double_t vertex[3], const AliMUONLocalTrigger* locTrg)
910 {
911   /// Transfert data from MUON track to ESDMuon track (ESDMuon track only contains clusters'Id, not cluster themselves)
912   /// Add trigger info if the MUON track is matched with a trigger track
913   
914   // empty MUON track -> produce a ghost ESDMuon track if trigger info are available otherwise produce an empty track
915   if (track.GetNClusters() == 0) {
916     if (locTrg) MUONToESD(*locTrg, esdTrack, track.GetUniqueID());
917     else {
918       cout<<"W-AliMUONESDInterface::MUONToESD: will produce an empty ESDMuon track"<<endl;
919       esdTrack.Reset();
920       esdTrack.SetUniqueID(0xFFFFFFFF);
921     }
922     return;
923   }
924   
925   esdTrack.Clear("C");
926   
927   // set global info
928   esdTrack.SetUniqueID(track.GetUniqueID());
929   esdTrack.SetChi2(track.GetGlobalChi2());
930   esdTrack.SetLabel(track.GetMCLabel());
931   
932   // set param at first cluster
933   AliMUONTrackParam* trackParam = static_cast<AliMUONTrackParam*>((track.GetTrackParamAtCluster())->First());
934   SetParamAtFirstCluster(*trackParam, esdTrack);
935   SetParamCov(*trackParam, esdTrack);
936   
937   // set transverse position at the end of the absorber
938   AliMUONTrackParam trackParamAtAbsEnd(*trackParam);
939   AliMUONTrackExtrap::ExtrapToZ(&trackParamAtAbsEnd, AliMUONConstants::AbsZEnd());
940   Double_t xAbs = trackParamAtAbsEnd.GetNonBendingCoor();
941   Double_t yAbs = trackParamAtAbsEnd.GetBendingCoor();
942   esdTrack.SetRAtAbsorberEnd(TMath::Sqrt(xAbs*xAbs + yAbs*yAbs));
943   
944   // set param at vertex
945   AliMUONTrackParam trackParamAtVtx(*trackParam);
946   AliMUONTrackExtrap::ExtrapToVertex(&trackParamAtVtx, vertex[0], vertex[1], vertex[2], 0., 0.);
947   SetParamAtVertex(trackParamAtVtx, esdTrack);
948   
949   // set param at Distance of Closest Approach
950   AliMUONTrackParam trackParamAtDCA(*trackParam);
951   AliMUONTrackExtrap::ExtrapToVertexWithoutBranson(&trackParamAtDCA, vertex[2]);
952   SetParamAtDCA(trackParamAtDCA, esdTrack);
953   
954   // set muon cluster info
955   esdTrack.SetMuonClusterMap(0);
956   while (trackParam) {
957     AliMUONVCluster *cluster = trackParam->GetClusterPtr();
958     esdTrack.AddClusterId(cluster->GetUniqueID());
959     esdTrack.AddInMuonClusterMap(cluster->GetChamberId());
960     trackParam = static_cast<AliMUONTrackParam*>(track.GetTrackParamAtCluster()->After(trackParam));
961   }
962   
963   // set connected flag
964   esdTrack.Connected(track.IsConnected());
965   
966   // set trigger info
967   esdTrack.SetLocalTrigger(track.GetLocalTrigger());
968   esdTrack.SetChi2MatchTrigger(track.GetChi2MatchTrigger());
969   esdTrack.SetHitsPatternInTrigCh(track.GetHitsPatternInTrigCh());
970   esdTrack.SetHitsPatternInTrigChTrk(track.GetHitsPatternInTrigChTrk());
971   if (locTrg) {
972     esdTrack.SetTriggerX1Pattern(locTrg->GetX1Pattern());
973     esdTrack.SetTriggerY1Pattern(locTrg->GetY1Pattern());
974     esdTrack.SetTriggerX2Pattern(locTrg->GetX2Pattern());
975     esdTrack.SetTriggerY2Pattern(locTrg->GetY2Pattern());
976     esdTrack.SetTriggerX3Pattern(locTrg->GetX3Pattern());
977     esdTrack.SetTriggerY3Pattern(locTrg->GetY3Pattern());
978     esdTrack.SetTriggerX4Pattern(locTrg->GetX4Pattern());
979     esdTrack.SetTriggerY4Pattern(locTrg->GetY4Pattern());
980   } else {
981     esdTrack.SetTriggerX1Pattern(0);
982     esdTrack.SetTriggerY1Pattern(0);
983     esdTrack.SetTriggerX2Pattern(0);
984     esdTrack.SetTriggerY2Pattern(0);
985     esdTrack.SetTriggerX3Pattern(0);
986     esdTrack.SetTriggerY3Pattern(0);
987     esdTrack.SetTriggerX4Pattern(0);
988     esdTrack.SetTriggerY4Pattern(0);
989   }
990   
991 }
992
993 //_____________________________________________________________________________
994 void AliMUONESDInterface::MUONToESD(const AliMUONLocalTrigger& locTrg, AliESDEvent& esd,
995                                     UInt_t trackId, const AliMUONTriggerTrack* triggerTrack)
996 {
997   /// Add in ESD event a ghost ESDMuon track containing only informations about trigger track
998   AliESDMuonTrack *esdTrack = esd.NewMuonTrack();
999   MUONToESD(locTrg, *esdTrack, trackId, triggerTrack);
1000 }
1001
1002 //_____________________________________________________________________________
1003 void AliMUONESDInterface::MUONToESD(const AliMUONLocalTrigger& locTrg, AliESDMuonTrack& esdTrack,
1004                                     UInt_t trackId, const AliMUONTriggerTrack* triggerTrack)
1005 {
1006   /// Build ghost ESDMuon track containing only informations about trigger track
1007   
1008   esdTrack.Reset();
1009   esdTrack.SetUniqueID(trackId);
1010   
1011   // set trigger info
1012   AliMUONTrack muonTrack;
1013   muonTrack.SetLocalTrigger(locTrg.LoCircuit(),
1014                             locTrg.LoStripX(),
1015                             locTrg.LoStripY(),
1016                             locTrg.GetDeviation(),
1017                             locTrg.LoLpt(),
1018                             locTrg.LoHpt(),
1019                             locTrg.GetTriggerWithoutChamber());
1020   esdTrack.SetLocalTrigger(muonTrack.GetLocalTrigger());
1021   esdTrack.SetChi2MatchTrigger(0.);
1022   esdTrack.SetTriggerX1Pattern(locTrg.GetX1Pattern());
1023   esdTrack.SetTriggerY1Pattern(locTrg.GetY1Pattern());
1024   esdTrack.SetTriggerX2Pattern(locTrg.GetX2Pattern());
1025   esdTrack.SetTriggerY2Pattern(locTrg.GetY2Pattern());
1026   esdTrack.SetTriggerX3Pattern(locTrg.GetX3Pattern());
1027   esdTrack.SetTriggerY3Pattern(locTrg.GetY3Pattern());
1028   esdTrack.SetTriggerX4Pattern(locTrg.GetX4Pattern());
1029   esdTrack.SetTriggerY4Pattern(locTrg.GetY4Pattern());
1030   UShort_t hitPattern = 0;
1031   esdTrack.SetHitsPatternInTrigChTrk(hitPattern);
1032   if(triggerTrack){
1033     hitPattern = triggerTrack->GetHitsPatternInTrigCh();
1034     esdTrack.SetHitsPatternInTrigCh(hitPattern);
1035     esdTrack.SetThetaXUncorrected(triggerTrack->GetThetax());
1036     esdTrack.SetThetaYUncorrected(triggerTrack->GetThetay());
1037     esdTrack.SetNonBendingCoorUncorrected(triggerTrack->GetX11());
1038     esdTrack.SetBendingCoorUncorrected(triggerTrack->GetY11());
1039     esdTrack.SetZUncorrected(triggerTrack->GetZ11());
1040   }
1041 }
1042
1043 //_____________________________________________________________________________
1044 void AliMUONESDInterface::MUONToESD(const AliMUONVCluster& cluster, AliESDEvent& esd, const AliMUONVDigitStore* digits)
1045 {
1046   /// Transfert data from MUON cluster to ESD event if it does not already exist
1047   /// Also transfert digits'Id to cluster and digits to ESD if they are provided
1048   
1049   AliESDMuonCluster *esdCluster = esd.FindMuonCluster(cluster.GetUniqueID());
1050   if (!esdCluster) esdCluster = esd.NewMuonCluster();
1051   else if (!digits || esdCluster->GetNPads() > 0) return;
1052   
1053   if (digits) {
1054     
1055     MUONToESD(cluster, *esdCluster, kTRUE);
1056     
1057     for (Int_t i=0; i<cluster.GetNDigits(); i++) {
1058       AliMUONVDigit* digit = digits->FindObject(cluster.GetDigitId(i));
1059       if (!digit) {
1060         AliErrorClass(Form("digit %u not found", cluster.GetDigitId(i)));
1061         continue;
1062       }
1063       MUONToESD(*digit, esd);
1064     }
1065     
1066   } else {
1067     
1068     MUONToESD(cluster, *esdCluster, kFALSE);
1069     
1070   }
1071   
1072 }
1073
1074 //_____________________________________________________________________________
1075 void AliMUONESDInterface::MUONToESD(const AliMUONVCluster& cluster, AliESDMuonCluster& esdCluster, Bool_t copyPadsId)
1076 {
1077   /// Transfert data from MUON cluster to ESDMuon cluster
1078   /// Also transfert digits'Is if required
1079   
1080   esdCluster.Clear("C");
1081   
1082   esdCluster.SetUniqueID(cluster.GetUniqueID());
1083   esdCluster.SetXYZ(cluster.GetX(), cluster.GetY(), cluster.GetZ());
1084   esdCluster.SetErrXY(cluster.GetErrX(), cluster.GetErrY());
1085   esdCluster.SetCharge(cluster.GetCharge());
1086   esdCluster.SetChi2(cluster.GetChi2());
1087   esdCluster.SetLabel(cluster.GetMCLabel());
1088   
1089   if (copyPadsId) esdCluster.SetPadsId(cluster.GetNDigits(), cluster.GetDigitsId());
1090   
1091 }
1092
1093 //_____________________________________________________________________________
1094 void AliMUONESDInterface::MUONToESD(const AliMUONVDigit& digit, AliESDEvent& esd)
1095 {
1096   /// Transfert data from MUON digit to ESD event if it does not already exist
1097   if (esd.FindMuonPad(digit.GetUniqueID())) return;
1098   AliESDMuonPad *esdPad = esd.NewMuonPad();
1099   MUONToESD(digit, *esdPad);
1100 }
1101
1102 //_____________________________________________________________________________
1103 void AliMUONESDInterface::MUONToESD(const AliMUONVDigit& digit, AliESDMuonPad& esdPad)
1104 {
1105   /// Transfert data from MUON digit to ESDMuon pad
1106   esdPad.SetUniqueID(digit.GetUniqueID());
1107   esdPad.SetADC(digit.ADC());
1108   esdPad.SetCharge(digit.Charge());
1109   esdPad.SetCalibrated(digit.IsCalibrated());
1110   esdPad.SetSaturated(digit.IsSaturated());
1111 }
1112
1113 //___________________________________________________________________________
1114 AliMUONTrack* AliMUONESDInterface::Add(const AliESDMuonTrack& esdTrack, AliMUONVTrackStore& trackStore, Bool_t refit)
1115 {
1116   /// Create MUON track from ESDMuon track and add it to the store
1117   /// Track parameters at each clusters are recomputed or not depending on the flag "refit"
1118   /// return a pointer to the track into the store (0x0 if the track already exist)
1119   if(trackStore.FindObject(esdTrack.GetUniqueID())) return 0x0;
1120   AliMUONTrack* track = trackStore.Add(AliMUONTrack());
1121   ESDToMUON(esdTrack, *track, refit);
1122   return track;
1123 }
1124
1125 //___________________________________________________________________________
1126 void AliMUONESDInterface::Add(const AliESDMuonTrack& esdTrack, AliMUONVTriggerStore& triggerStore)
1127 {
1128   /// Create MUON local trigger from ESDMuon track and add it to the store if not already there
1129   if (!triggerStore.FindLocal(esdTrack.LoCircuit())->IsNull()) return;
1130   AliMUONLocalTrigger locTrg;
1131   ESDToMUON(esdTrack, locTrg);
1132   triggerStore.Add(locTrg);
1133 }
1134
1135 //___________________________________________________________________________
1136 AliMUONVCluster* AliMUONESDInterface::Add(const AliESDMuonCluster& esdCluster, AliMUONVClusterStore& clusterStore)
1137 {
1138   /// Create MUON cluster from ESDMuon cluster and add it to the store
1139   /// return a pointer to the cluster into the store (0x0 if the cluster already exist)
1140   AliMUONVCluster* cluster = clusterStore.Add(esdCluster.GetChamberId(), esdCluster.GetDetElemId(), esdCluster.GetClusterIndex());
1141   if (cluster) ESDToMUON(esdCluster, *cluster);
1142   return cluster;
1143 }
1144
1145 //___________________________________________________________________________
1146 AliMUONVDigit* AliMUONESDInterface::Add(const AliESDMuonPad& esdPad, AliMUONVDigitStore& digitStore)
1147 {
1148   /// Create MUON digit from ESDMuon digit and add it to the store
1149   /// return a pointer to the digit into the store (0x0 if the digit already exist)
1150   AliMUONVDigit* digit = digitStore.Add(esdPad.GetDetElemId(), esdPad.GetManuId(), esdPad.GetManuChannel(), esdPad.GetCathode(), AliMUONVDigitStore::kDeny);
1151   if (digit) ESDToMUON(esdPad, *digit);
1152   return digit;
1153 }
1154