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