]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONRecoCheck.cxx
SVN keyword Id is set
[u/mrichter/AliRoot.git] / MUON / AliMUONRecoCheck.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 //-----------------------------------------------------------------------------
19 /// \class AliMUONRecoCheck
20 /// Utility class to check reconstruction
21 /// Reconstructed tracks are compared to reference tracks. 
22 /// The reference tracks are built from AliTrackReference for the
23 /// hit in chamber (0..9) and from kinematics for the vertex parameters.     
24 //-----------------------------------------------------------------------------
25
26 // 13 Nov 2007:
27 // Added a method to create a list of reconstructed AliMUONTrack objects from
28 // ESD data. This is necessary since the track objects that are actually created
29 // during offline reconstruction are no longer stored to disk.
30 //  - Artur Szostak <artursz@iafrica.com>
31 // 25 Jan 2008:
32 // Use the new ESDInterface to create MUON objects from ESD data
33 // - Philippe Pillot
34 // 
35
36 #include "AliMUONRecoCheck.h"
37 #include "AliMUONTrack.h"
38 #include "AliMUONVTrackStore.h"
39 #include "AliMUONVCluster.h"
40 #include "AliMUONVClusterStore.h"
41 #include "AliMUONConstants.h"
42 #include "AliMUONESDInterface.h"
43 #include "AliMUONTrackParam.h"
44 #include "AliMCEventHandler.h"
45 #include "AliMCEvent.h"
46 #include "AliStack.h"
47 #include "AliTrackReference.h"
48 #include "AliLog.h"
49 #include "AliESDEvent.h"
50 #include "AliESDMuonTrack.h"
51
52 #include <TFile.h>
53 #include <TTree.h>
54 #include <TParticle.h>
55 #include <TParticlePDG.h>
56 #include <Riostream.h>
57
58 /// \cond CLASSIMP
59 ClassImp(AliMUONRecoCheck)
60 /// \endcond
61
62 //_____________________________________________________________________________
63 AliMUONRecoCheck::AliMUONRecoCheck(const Char_t *esdFileName, const Char_t *pathSim)
64 : TObject(),
65 fMCEventHandler(new AliMCEventHandler()),
66 fESDEvent(new AliESDEvent()),
67 fESDTree (0x0),
68 fESDFile (0x0),
69 fCurrentEvent(0),
70 fTrackRefStore(0x0),
71 fRecoTrackRefStore(0x0),
72 fRecoTrackStore(0x0),
73 fESDEventOwner(kTRUE)
74 {
75   /// Normal ctor
76   
77   // TrackRefs and Particules
78   fMCEventHandler->SetInputPath(pathSim);
79   fMCEventHandler->InitIO("");
80   
81   // ESD MUON Tracks
82   fESDFile = TFile::Open(esdFileName); // open the file
83   if (!fESDFile || !fESDFile->IsOpen()) {
84     AliError(Form("opening ESD file %s failed", esdFileName));
85     fESDFile = 0x0;
86     return;
87   }
88   fESDTree = (TTree*) fESDFile->Get("esdTree"); // get the tree
89   if (!fESDTree) {
90     AliError("no ESD tree found");
91     fESDFile->Close();
92     fESDFile = 0x0;
93     return;
94   }
95   fESDEvent->ReadFromTree(fESDTree); // link fESDEvent to the tree
96 }
97
98 //_____________________________________________________________________________
99 AliMUONRecoCheck::AliMUONRecoCheck(AliESDEvent *esdEvent, AliMCEventHandler *mcEventHandler)
100 : TObject(),
101 fMCEventHandler(0),
102 fESDEvent(0),
103 fESDTree (0x0),
104 fESDFile (0x0),
105 fCurrentEvent(0),
106 fTrackRefStore(0x0),
107 fRecoTrackRefStore(0x0),
108 fRecoTrackStore(0x0),
109 fESDEventOwner(kFALSE)
110 {
111   /// Normal ctor
112   
113   // TrackRefs and Particules
114   fMCEventHandler = mcEventHandler;
115   
116   // ESD MUON Tracks
117   fESDEvent = esdEvent;
118   
119 }
120
121 //_____________________________________________________________________________
122 AliMUONRecoCheck::~AliMUONRecoCheck()
123 {
124   /// Destructor
125   if (fESDEventOwner) {
126     delete fMCEventHandler;
127     delete fESDEvent;
128     if (fESDFile) fESDFile->Close();
129   }
130   ResetStores();
131 }
132
133 //_____________________________________________________________________________
134 void AliMUONRecoCheck::ResetStores()
135 {
136   /// Deletes all the store objects that have been created and resets the pointers to 0x0
137   delete fTrackRefStore;      fTrackRefStore = 0x0;
138   delete fRecoTrackRefStore;  fRecoTrackRefStore = 0x0;
139   delete fRecoTrackStore;     fRecoTrackStore = 0x0;
140 }
141
142 //_____________________________________________________________________________
143 Int_t AliMUONRecoCheck::NumberOfEvents() const
144 {
145   /// Return the number of events
146   if (fESDEventOwner && fESDTree) return fESDTree->GetEntries();
147   return 0;
148 }
149
150 //_____________________________________________________________________________
151 AliMUONVTrackStore* AliMUONRecoCheck::ReconstructedTracks(Int_t event)
152 {
153   /// Return a track store containing the reconstructed tracks (converted into 
154   /// MUONTrack objects) for a given event
155   
156   if (!fESDEventOwner) {
157     MakeReconstructedTracks();
158     return fRecoTrackStore;
159   }
160
161   if (event != fCurrentEvent) {
162     ResetStores();
163     fCurrentEvent = event;
164   }
165   
166   if (fRecoTrackStore != 0x0) return fRecoTrackStore;
167   else {
168     if (!fESDTree) return 0x0;
169     if (fESDTree->GetEvent(event) <= 0) {
170       AliError(Form("fails to read ESD object for event %d", event));
171       return 0x0;
172     }
173     MakeReconstructedTracks();
174     return fRecoTrackStore;
175   }
176 }
177
178 //_____________________________________________________________________________
179 AliMUONVTrackStore* AliMUONRecoCheck::TrackRefs(Int_t event)
180 {
181   /// Return a track store containing the track references (converted into 
182   /// MUONTrack objects) for a given event
183   
184   if (!fESDEventOwner) {
185     MakeTrackRefs();
186     return fTrackRefStore;
187   }
188
189   if (event != fCurrentEvent) {
190     ResetStores();
191     fCurrentEvent = event;
192   }
193   
194   if (fTrackRefStore != 0x0) return fTrackRefStore;
195   else {
196     if (!fMCEventHandler->GetEvent(event)) {
197       AliError(Form("fails to read MC objects for event %d", event));
198       return 0x0;
199     }
200     MakeTrackRefs();
201     return fTrackRefStore;
202   }
203 }
204
205 //_____________________________________________________________________________
206 AliMUONVTrackStore* AliMUONRecoCheck::ReconstructibleTracks(Int_t event)
207 {
208   /// Return a track store containing the reconstructible tracks for a given event
209
210   if (!fESDEventOwner) {
211     if (TrackRefs(event) == 0x0) return 0x0;
212     MakeReconstructibleTracks();
213     return fRecoTrackRefStore;
214   }
215
216   if (event != fCurrentEvent) {
217     ResetStores();
218     fCurrentEvent = event;
219   }
220   
221   if (fRecoTrackRefStore != 0x0) return fRecoTrackRefStore;
222   else {
223     if (TrackRefs(event) == 0x0) return 0x0;
224     MakeReconstructibleTracks();
225     return fRecoTrackRefStore;
226   }
227 }
228
229 //_____________________________________________________________________________
230 void AliMUONRecoCheck::MakeReconstructedTracks()
231 {
232   /// Make reconstructed tracks
233   if (!(fRecoTrackStore = AliMUONESDInterface::NewTrackStore())) return;
234   
235   // loop over all reconstructed tracks and add them to the store (skip ghosts)
236   Int_t nTracks = (Int_t) fESDEvent->GetNumberOfMuonTracks();
237   for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
238     AliESDMuonTrack* esdTrack = fESDEvent->GetMuonTrack(iTrack);
239     if (esdTrack->ContainTrackerData()) AliMUONESDInterface::Add(*esdTrack, *fRecoTrackStore);
240   }
241   
242 }
243
244 //_____________________________________________________________________________
245 void AliMUONRecoCheck::MakeTrackRefs()
246 {
247   /// Make reconstructible tracks
248   AliMUONVTrackStore *tmpTrackRefStore = AliMUONESDInterface::NewTrackStore();
249   if (!tmpTrackRefStore) return;
250   
251   Double_t x, y, z, pX, pY, pZ, bendingSlope, nonBendingSlope, inverseBendingMomentum;
252   TParticle* particle;
253   TClonesArray* trackRefs;
254   Int_t nTrackRef = fMCEventHandler->MCEvent()->GetNumberOfTracks();
255   AliMUONVClusterStore* cStore = AliMUONESDInterface::NewClusterStore();
256   if (!cStore) return;
257   AliMUONVCluster* hit = cStore->CreateCluster(0,0,0);
258   
259   // loop over simulated tracks
260   for (Int_t iTrackRef  = 0; iTrackRef < nTrackRef; ++iTrackRef) {
261     Int_t nHits = fMCEventHandler->GetParticleAndTR(iTrackRef, particle, trackRefs);
262     
263     // skip empty trackRefs
264     if (nHits < 1) continue;
265     
266     // get the particle charge for further calculation
267     TParticlePDG* ppdg = particle->GetPDG();
268     Int_t charge = ppdg != NULL ? (Int_t)(ppdg->Charge()/3.0) : 0;
269     
270     AliMUONTrack track;
271     
272     // loop over simulated track hits
273     for (Int_t iHit = 0; iHit < nHits; ++iHit) {        
274       AliTrackReference* trackReference = static_cast<AliTrackReference*>(trackRefs->UncheckedAt(iHit));
275       
276       // skip trackRefs not in MUON
277       if (trackReference->DetectorId() != AliTrackReference::kMUON) continue;
278     
279       // Get track parameters of current hit
280       x = trackReference->X();
281       y = trackReference->Y();
282       z = trackReference->Z();
283       pX = trackReference->Px();
284       pY = trackReference->Py();
285       pZ = trackReference->Pz();
286       
287       // check chamberId of current trackReference
288       Int_t detElemId = trackReference->UserId();
289       Int_t chamberId = detElemId / 100 - 1;
290       if (chamberId < 0 || chamberId >= AliMUONConstants::NTrackingCh()) continue;
291       
292       // set hit parameters
293       hit->SetUniqueID(AliMUONVCluster::BuildUniqueID(chamberId, detElemId, iHit));
294       hit->SetXYZ(x,y,z);
295       hit->SetErrXY(0.,0.);
296       
297       // compute track parameters at hit
298       bendingSlope = 0;
299       nonBendingSlope = 0;
300       inverseBendingMomentum = 0;
301       if (TMath::Abs(pZ) > 0) {
302         bendingSlope = pY/pZ;
303         nonBendingSlope = pX/pZ;
304       }
305       Double_t pYZ = TMath::Sqrt(pY*pY+pZ*pZ);
306       if (pYZ >0) inverseBendingMomentum = 1/pYZ; 
307       inverseBendingMomentum *= charge;
308       
309       // set track parameters at hit
310       AliMUONTrackParam trackParam;
311       trackParam.SetNonBendingCoor(x);
312       trackParam.SetBendingCoor(y);
313       trackParam.SetZ(z);
314       trackParam.SetBendingSlope(bendingSlope);
315       trackParam.SetNonBendingSlope(nonBendingSlope);
316       trackParam.SetInverseBendingMomentum(inverseBendingMomentum);
317       
318       // add track parameters at current hit to the track
319       track.AddTrackParamAtCluster(trackParam, *hit, kTRUE);
320     }
321     
322     // if none of the track hits was in MUON, goto the next track
323     if (track.GetNClusters() < 1) continue;
324     
325     // get track parameters at particle's vertex
326     x = particle->Vx();
327     y = particle->Vy();
328     z = particle->Vz();
329     pX = particle->Px();
330     pY = particle->Py();
331     pZ = particle->Pz();
332     
333     // compute rest of track parameters at particle's vertex
334     bendingSlope = 0;
335     nonBendingSlope = 0;
336     inverseBendingMomentum = 0;
337     if (TMath::Abs(pZ) > 0) {
338       bendingSlope = pY/pZ;
339       nonBendingSlope = pX/pZ;
340     }
341     Double_t pYZ = TMath::Sqrt(pY*pY+pZ*pZ);
342     if (pYZ >0) inverseBendingMomentum = 1/pYZ;
343     inverseBendingMomentum *= charge;
344     
345     // set track parameters at particle's vertex
346     AliMUONTrackParam trackParamAtVertex;
347     trackParamAtVertex.SetNonBendingCoor(x);
348     trackParamAtVertex.SetBendingCoor(y);
349     trackParamAtVertex.SetZ(z);
350     trackParamAtVertex.SetBendingSlope(bendingSlope);
351     trackParamAtVertex.SetNonBendingSlope(nonBendingSlope);
352     trackParamAtVertex.SetInverseBendingMomentum(inverseBendingMomentum);
353     
354     // add track parameters at vertex
355     track.SetTrackParamAtVertex(&trackParamAtVertex);
356     
357     // store the track
358     track.SetUniqueID(iTrackRef);
359     tmpTrackRefStore->Add(track);
360   }
361   
362   CleanMuonTrackRef(tmpTrackRefStore);
363   
364   delete hit;
365   delete cStore;
366   delete tmpTrackRefStore;
367 }
368
369 //_____________________________________________________________________________
370 void AliMUONRecoCheck::CleanMuonTrackRef(const AliMUONVTrackStore *tmpTrackRefStore)
371 {
372   /// Re-calculate hits parameters because two AliTrackReferences are recorded for
373   /// each chamber (one when particle is entering + one when particle is leaving 
374   /// the sensitive volume) 
375   if (!(fTrackRefStore = AliMUONESDInterface::NewTrackStore())) return;
376   
377   Double_t maxGasGap = 1.; // cm 
378   Double_t x, y, z, pX, pY, pZ, x1, y1, z1, pX1, pY1, pZ1, z2;
379   Double_t bendingSlope,nonBendingSlope,inverseBendingMomentum;
380   AliMUONVClusterStore* cStore = AliMUONESDInterface::NewClusterStore();
381   if (!cStore) return;
382   AliMUONVCluster* hit = cStore->CreateCluster(0,0,0);
383   
384   // create iterator
385   TIter next(tmpTrackRefStore->CreateIterator());
386   
387   // loop over tmpTrackRef
388   AliMUONTrack* track;
389   while ( ( track = static_cast<AliMUONTrack*>(next()) ) ) {
390     
391     AliMUONTrack newTrack;
392     
393     // loop over tmpTrackRef's hits
394     Int_t iHit1 = 0;
395     Int_t nTrackHits = track->GetNClusters();
396     while (iHit1 < nTrackHits) {
397       AliMUONTrackParam *trackParam1 = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->UncheckedAt(iHit1);
398       
399       // get track parameters at hit1
400       x1  = trackParam1->GetNonBendingCoor();
401       y1  = trackParam1->GetBendingCoor();
402       z1  = trackParam1->GetZ();
403       pX1 = trackParam1->Px();
404       pY1 = trackParam1->Py();
405       pZ1 = trackParam1->Pz();
406       
407       // prepare new track parameters
408       x  = x1;
409       y  = y1;
410       z  = z1;
411       pX = pX1;
412       pY = pY1;
413       pZ = pZ1;
414       
415       // loop over next tmpTrackRef's hits
416       Int_t nCombinedHits = 1;
417       for (Int_t iHit2 = iHit1+1; iHit2 < nTrackHits; iHit2++) {
418         AliMUONTrackParam *trackParam2 = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->UncheckedAt(iHit2);
419         
420         // get z position of hit2
421         z2 = trackParam2->GetZ();
422         
423         // complete new track parameters if hit2 is on the same detection element
424         if ( TMath::Abs(z2-z1) < maxGasGap ) {
425           x  += trackParam2->GetNonBendingCoor();
426           y  += trackParam2->GetBendingCoor();
427           z  += z2;
428           pX += trackParam2->Px();
429           pY += trackParam2->Py();
430           pZ += trackParam2->Pz();
431           nCombinedHits++;
432           iHit1 = iHit2;
433         }
434         
435       }
436       
437       // finalize new track parameters
438       x  /= (Double_t)nCombinedHits;
439       y  /= (Double_t)nCombinedHits;
440       z  /= (Double_t)nCombinedHits;
441       pX /= (Double_t)nCombinedHits;
442       pY /= (Double_t)nCombinedHits;
443       pZ /= (Double_t)nCombinedHits;
444       bendingSlope = 0;
445       nonBendingSlope = 0;
446       inverseBendingMomentum = 0;
447       if (TMath::Abs(pZ) > 0) {
448         bendingSlope = pY/pZ;
449         nonBendingSlope = pX/pZ;
450       }
451       Double_t pYZ = TMath::Sqrt(pY*pY+pZ*pZ);
452       if (pYZ >0) inverseBendingMomentum = 1/pYZ; 
453       inverseBendingMomentum *= trackParam1->GetCharge();
454       
455       // set hit parameters
456       hit->SetUniqueID(trackParam1->GetClusterPtr()->GetUniqueID());
457       hit->SetXYZ(x,y,z);
458       hit->SetErrXY(0.,0.);
459       
460       // set new track parameters at new hit
461       AliMUONTrackParam trackParam;
462       trackParam.SetNonBendingCoor(x);
463       trackParam.SetBendingCoor(y);
464       trackParam.SetZ(z);
465       trackParam.SetBendingSlope(bendingSlope);
466       trackParam.SetNonBendingSlope(nonBendingSlope);
467       trackParam.SetInverseBendingMomentum(inverseBendingMomentum);
468       
469       // add track parameters at current hit to the track
470       newTrack.AddTrackParamAtCluster(trackParam, *hit, kTRUE);
471       
472       iHit1++;
473     }
474     
475     newTrack.SetUniqueID(track->GetUniqueID());
476     newTrack.SetTrackParamAtVertex(track->GetTrackParamAtVertex());
477     fTrackRefStore->Add(newTrack);
478     
479   }
480   
481   delete hit;
482   delete cStore;
483 }
484
485 //_____________________________________________________________________________
486 void AliMUONRecoCheck::MakeReconstructibleTracks()
487 {
488   /// Isolate the reconstructible tracks
489   if (!(fRecoTrackRefStore = AliMUONESDInterface::NewTrackStore())) return;
490   
491   // create iterator on trackRef
492   TIter next(fTrackRefStore->CreateIterator());
493   
494   // loop over trackRef
495   AliMUONTrack* track;
496   while ( ( track = static_cast<AliMUONTrack*>(next()) ) ) {
497     
498     Bool_t* chamberInTrack = new Bool_t(AliMUONConstants::NTrackingCh());
499     for (Int_t iCh = 0; iCh < AliMUONConstants::NTrackingCh(); iCh++) chamberInTrack[iCh] = kFALSE;
500     
501     // loop over trackRef's hits to get hit chambers
502     Int_t nTrackHits = track->GetNClusters();
503     for (Int_t iHit = 0; iHit < nTrackHits; iHit++) {
504       AliMUONVCluster* hit = ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->UncheckedAt(iHit))->GetClusterPtr(); 
505       chamberInTrack[hit->GetChamberId()] = kTRUE;
506     } 
507     
508     // track is reconstructible if the particle is depositing a hit
509     // in the following chamber combinations:
510     Bool_t trackOK = kTRUE;
511     if (!chamberInTrack[0] && !chamberInTrack[1]) trackOK = kFALSE;
512     if (!chamberInTrack[2] && !chamberInTrack[3]) trackOK = kFALSE;
513     if (!chamberInTrack[4] && !chamberInTrack[5]) trackOK = kFALSE;
514     Int_t nHitsInLastStations = 0;
515     for (Int_t iCh = 6; iCh < AliMUONConstants::NTrackingCh(); iCh++)
516       if (chamberInTrack[iCh]) nHitsInLastStations++; 
517     if(nHitsInLastStations < 3) trackOK = kFALSE;
518     
519     // Add reconstructible tracks to fRecoTrackRefStore
520     if (trackOK) fRecoTrackRefStore->Add(*track);
521     
522     delete [] chamberInTrack;
523   }
524
525 }
526