protections against failures in deleting event content
[u/mrichter/AliRoot.git] / PWG / EMCAL / AliEmcalEsdTpcTrackTask.cxx
1 // $Id$
2 //
3 // Task to constrain TPC tracks to SPD vertex.
4 //
5 // Author: C.Loizides
6
7 #include <TClonesArray.h>
8 #include <TGeoGlobalMagField.h>
9 #include "AliAnalysisManager.h"
10 #include "AliESDEvent.h"
11 #include "AliESDtrackCuts.h"
12 #include "AliEmcalEsdTpcTrackTask.h"
13 #include "AliMagF.h"
14
15 ClassImp(AliEmcalEsdTpcTrackTask)
16
17 //________________________________________________________________________
18 AliEmcalEsdTpcTrackTask::AliEmcalEsdTpcTrackTask() : 
19   AliAnalysisTaskSE("TpcSpdVertexConstrainedTracks"),
20   fEsdTrackCuts(0),
21   fDoSpdVtxCon(0),
22   fHybridTrackCuts(0),
23   fTracksName(),
24   fEsdEv(0),
25   fTracks(0)
26 {
27   // Constructor.
28 }
29
30 //________________________________________________________________________
31 AliEmcalEsdTpcTrackTask::AliEmcalEsdTpcTrackTask(const char *name) : 
32   AliAnalysisTaskSE(name),
33   fEsdTrackCuts(0),
34   fDoSpdVtxCon(0),
35   fHybridTrackCuts(0),
36   fTracksName("TpcSpdVertexConstrainedTracks"),
37   fEsdEv(0),
38   fTracks(0)
39 {
40   // Constructor.
41
42   if (!name)
43     return;
44
45   SetName(name);
46
47   fBranchNames = "ESD:AliESDHeader.,AliESDRun.,SPDVertex.,Tracks";
48 }
49
50 //________________________________________________________________________
51 AliEmcalEsdTpcTrackTask::~AliEmcalEsdTpcTrackTask()
52 {
53   //Destructor
54
55   delete fEsdTrackCuts;
56 }
57
58 //________________________________________________________________________
59 void AliEmcalEsdTpcTrackTask::UserCreateOutputObjects()
60 {
61   // Create histograms.
62
63   fTracks = new TClonesArray("AliESDtrack");
64   fTracks->SetName(fTracksName);
65
66   if (!fEsdTrackCuts) {
67     AliInfo("No track cuts given, creating default (standard only TPC) cuts");
68     fEsdTrackCuts = AliESDtrackCuts::GetStandardTPCOnlyTrackCuts();
69     fEsdTrackCuts->SetPtRange(0.15,1e3);
70   }
71 }
72
73 //________________________________________________________________________
74 void AliEmcalEsdTpcTrackTask::UserExec(Option_t *) 
75 {
76   // Main loop, called for each event.
77
78   fEsdEv = dynamic_cast<AliESDEvent*>(InputEvent());
79   if (!fEsdEv) {
80     AliError("Task works only on ESD events, returning");
81     return;
82   }
83
84   AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
85   if (!am) {
86     AliError("Manager zero, returning");
87     return;
88   }
89
90   // add tracks to event if not yet there
91   fTracks->Delete();
92   if (!(InputEvent()->FindListObject(fTracksName)))
93     InputEvent()->AddObject(fTracks);
94
95   if (!fHybridTrackCuts) { // contrain TPC tracks to SPD vertex if fDoSpdVtxCon==kTRUE
96     am->LoadBranch("AliESDRun.");
97     am->LoadBranch("AliESDHeader.");
98     am->LoadBranch("Tracks");
99
100     if (fDoSpdVtxCon) {
101       if (!TGeoGlobalMagField::Instance()->GetField()) { // construct field map
102         fEsdEv->InitMagneticField();
103       }
104       am->LoadBranch("SPDVertex.");
105       const AliESDVertex *vtxSPD = fEsdEv->GetPrimaryVertexSPD();
106       if (!vtxSPD) {
107         AliError("No SPD vertex, returning");
108         return;
109       }
110       Int_t ntr = fEsdEv->GetNumberOfTracks();
111       for (Int_t i=0, ntrnew=0; i<ntr; ++i) {
112         AliESDtrack *etrack = fEsdEv->GetTrack(i);
113         if (!etrack)
114           continue;
115         if (!fEsdTrackCuts->AcceptTrack(etrack))
116           continue;
117         AliESDtrack *ntrack = AliESDtrackCuts::GetTPCOnlyTrack(fEsdEv,etrack->GetID());
118         if (!ntrack)
119           continue;
120         if (ntrack->Pt()<=0) {
121           delete ntrack;
122           continue;
123         }
124         Double_t bfield[3] = {0,0,0};
125         ntrack->GetBxByBz(bfield);
126         AliExternalTrackParam exParam;
127         Bool_t relate = ntrack->RelateToVertexBxByBz(vtxSPD,bfield,kVeryBig,&exParam);
128         if (!relate) {
129           delete ntrack;
130           continue;
131         }
132         // set the constraint parameters to the track
133         ntrack->Set(exParam.GetX(),exParam.GetAlpha(),exParam.GetParameter(),exParam.GetCovariance());
134         if (ntrack->Pt()<=0) {
135           delete ntrack;
136           continue;
137         }
138         new ((*fTracks)[ntrnew++]) AliESDtrack(*ntrack);
139         delete ntrack;
140       }
141     } else { /* no spd vtx constraint */
142       Int_t ntr = fEsdEv->GetNumberOfTracks();
143       for (Int_t i=0, ntrnew=0; i<ntr; ++i) {
144         AliESDtrack *etrack = fEsdEv->GetTrack(i);
145         if (!etrack)
146           continue;
147         if (!fEsdTrackCuts->AcceptTrack(etrack))
148           continue;
149         new ((*fTracks)[ntrnew++]) AliESDtrack(*etrack);
150       }
151     }
152
153   } else { // use hybrid track cuts
154
155     am->LoadBranch("Tracks");
156     Int_t ntr = fEsdEv->GetNumberOfTracks();
157     for (Int_t i=0, ntrnew=0; i<ntr; ++i) {
158       AliESDtrack *etrack = fEsdEv->GetTrack(i);
159       if (!etrack)
160         continue;
161       if (fEsdTrackCuts->AcceptTrack(etrack)) {
162         new ((*fTracks)[ntrnew]) AliESDtrack(*etrack);
163         AliESDtrack *newTrack = static_cast<AliESDtrack*>(fTracks->At(ntrnew));
164         newTrack->SetLabel(0);
165         ++ntrnew;
166       } else if (fHybridTrackCuts->AcceptTrack(etrack)) {
167         if (etrack->GetConstrainedParam()) {
168           new ((*fTracks)[ntrnew]) AliESDtrack(*etrack);
169           AliESDtrack *newTrack = static_cast<AliESDtrack*>(fTracks->At(ntrnew));
170           ++ntrnew;
171           const AliExternalTrackParam* constrainParam = etrack->GetConstrainedParam();
172           newTrack->Set(constrainParam->GetX(),
173                         constrainParam->GetAlpha(),
174                         constrainParam->GetParameter(),
175                         constrainParam->GetCovariance());
176           UInt_t status = etrack->GetStatus();
177           if ((status&AliESDtrack::kITSrefit)==0)
178             newTrack->SetLabel(2);
179           else
180             newTrack->SetLabel(1);
181         }
182       }
183     }
184   }
185 }