protections against failures in deleting event content
[u/mrichter/AliRoot.git] / PWG / EMCAL / AliEmcalEsdTpcTrackTask.cxx
CommitLineData
cc2b5fb5 1// $Id$
b0a53615 2//
a93ef37c 3// Task to constrain TPC tracks to SPD vertex.
b0a53615 4//
cd231d42 5// Author: C.Loizides
cc2b5fb5 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
15ClassImp(AliEmcalEsdTpcTrackTask)
16
17//________________________________________________________________________
18AliEmcalEsdTpcTrackTask::AliEmcalEsdTpcTrackTask() :
27fde6c7 19 AliAnalysisTaskSE("TpcSpdVertexConstrainedTracks"),
cc2b5fb5 20 fEsdTrackCuts(0),
4d1e3842 21 fDoSpdVtxCon(0),
57184cc6 22 fHybridTrackCuts(0),
cc2b5fb5 23 fTracksName(),
24 fEsdEv(0),
25 fTracks(0)
26{
a93ef37c 27 // Constructor.
cc2b5fb5 28}
29
30//________________________________________________________________________
31AliEmcalEsdTpcTrackTask::AliEmcalEsdTpcTrackTask(const char *name) :
32 AliAnalysisTaskSE(name),
33 fEsdTrackCuts(0),
4d1e3842 34 fDoSpdVtxCon(0),
57184cc6 35 fHybridTrackCuts(0),
cc2b5fb5 36 fTracksName("TpcSpdVertexConstrainedTracks"),
37 fEsdEv(0),
38 fTracks(0)
39{
a93ef37c 40 // Constructor.
27fde6c7 41
42 if (!name)
43 return;
44
45 SetName(name);
46
a93ef37c 47 fBranchNames = "ESD:AliESDHeader.,AliESDRun.,SPDVertex.,Tracks";
cc2b5fb5 48}
49
50//________________________________________________________________________
51AliEmcalEsdTpcTrackTask::~AliEmcalEsdTpcTrackTask()
52{
53 //Destructor
54
55 delete fEsdTrackCuts;
56}
57
58//________________________________________________________________________
59void AliEmcalEsdTpcTrackTask::UserCreateOutputObjects()
60{
a93ef37c 61 // Create histograms.
cc2b5fb5 62
63 fTracks = new TClonesArray("AliESDtrack");
64 fTracks->SetName(fTracksName);
65
66 if (!fEsdTrackCuts) {
dd5a823e 67 AliInfo("No track cuts given, creating default (standard only TPC) cuts");
cc2b5fb5 68 fEsdTrackCuts = AliESDtrackCuts::GetStandardTPCOnlyTrackCuts();
69 fEsdTrackCuts->SetPtRange(0.15,1e3);
70 }
71}
72
73//________________________________________________________________________
74void 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
71c1dd76 91 fTracks->Delete();
cc2b5fb5 92 if (!(InputEvent()->FindListObject(fTracksName)))
93 InputEvent()->AddObject(fTracks);
94
dd5a823e 95 if (!fHybridTrackCuts) { // contrain TPC tracks to SPD vertex if fDoSpdVtxCon==kTRUE
57184cc6 96 am->LoadBranch("AliESDRun.");
97 am->LoadBranch("AliESDHeader.");
57184cc6 98 am->LoadBranch("Tracks");
4d1e3842 99
100 if (fDoSpdVtxCon) {
101 if (!TGeoGlobalMagField::Instance()->GetField()) { // construct field map
102 fEsdEv->InitMagneticField();
57184cc6 103 }
4d1e3842 104 am->LoadBranch("SPDVertex.");
105 const AliESDVertex *vtxSPD = fEsdEv->GetPrimaryVertexSPD();
106 if (!vtxSPD) {
107 AliError("No SPD vertex, returning");
108 return;
57184cc6 109 }
4d1e3842 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);
57184cc6 139 delete ntrack;
57184cc6 140 }
4d1e3842 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 }
cc2b5fb5 151 }
152
10d1f756 153 } else { // use hybrid track cuts
57184cc6 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 }
cc2b5fb5 184 }
185}