Bug fix: segfault in event disply due to incorrect
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskSE.cxx
CommitLineData
5232d0de 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 <TROOT.h>
19#include <TSystem.h>
20#include <TInterpreter.h>
21#include <TChain.h>
22#include <TFile.h>
23#include <TList.h>
24
25#include "AliAnalysisTaskSE.h"
26#include "AliAnalysisManager.h"
60a04972 27#include "AliAnalysisDataSlot.h"
5232d0de 28#include "AliESDEvent.h"
29#include "AliESD.h"
30#include "AliAODEvent.h"
3f2431c4 31#include "AliAODHeader.h"
c185e0db 32#include "AliAODTracklets.h"
5a922844 33#include "AliAODCaloCells.h"
113bc2bd 34#include "AliAODMCParticle.h"
5232d0de 35#include "AliVEvent.h"
36#include "AliAODHandler.h"
3f2431c4 37#include "AliAODInputHandler.h"
5232d0de 38#include "AliMCEventHandler.h"
39#include "AliInputEventHandler.h"
40#include "AliMCEvent.h"
41#include "AliStack.h"
4d0a82af 42#include "AliLog.h"
5232d0de 43
44
45ClassImp(AliAnalysisTaskSE)
46
47////////////////////////////////////////////////////////////////////////
c185e0db 48AliAODHeader* AliAnalysisTaskSE::fgAODHeader = NULL;
49TClonesArray* AliAnalysisTaskSE::fgAODTracks = NULL;
50TClonesArray* AliAnalysisTaskSE::fgAODVertices = NULL;
51TClonesArray* AliAnalysisTaskSE::fgAODV0s = NULL;
52TClonesArray* AliAnalysisTaskSE::fgAODPMDClusters = NULL;
53TClonesArray* AliAnalysisTaskSE::fgAODJets = NULL;
54TClonesArray* AliAnalysisTaskSE::fgAODFMDClusters = NULL;
55TClonesArray* AliAnalysisTaskSE::fgAODCaloClusters = NULL;
113bc2bd 56TClonesArray* AliAnalysisTaskSE::fgAODMCParticles = NULL;
c185e0db 57AliAODTracklets* AliAnalysisTaskSE::fgAODTracklets = NULL;
5a922844 58AliAODCaloCells* AliAnalysisTaskSE::fgAODEmcalCells = NULL;
59AliAODCaloCells* AliAnalysisTaskSE::fgAODPhosCells = NULL;
c185e0db 60
5232d0de 61
62AliAnalysisTaskSE::AliAnalysisTaskSE():
63 AliAnalysisTask(),
64 fDebug(0),
80d13558 65 fEntry(0),
5232d0de 66 fInputEvent(0x0),
80d13558 67 fInputHandler(0x0),
5232d0de 68 fOutputAOD(0x0),
69 fMCEvent(0x0),
70 fTreeA(0x0)
71{
72 // Default constructor
73}
74
75AliAnalysisTaskSE::AliAnalysisTaskSE(const char* name):
76 AliAnalysisTask(name, "AnalysisTaskSE"),
77 fDebug(0),
80d13558 78 fEntry(0),
5232d0de 79 fInputEvent(0x0),
80d13558 80 fInputHandler(0x0),
5232d0de 81 fOutputAOD(0x0),
82 fMCEvent(0x0),
83 fTreeA(0x0)
84{
85 // Default constructor
86 DefineInput (0, TChain::Class());
87 DefineOutput(0, TTree::Class());
88}
89
26f071d8 90AliAnalysisTaskSE::AliAnalysisTaskSE(const AliAnalysisTaskSE& obj):
91 AliAnalysisTask(obj),
92 fDebug(0),
80d13558 93 fEntry(0),
26f071d8 94 fInputEvent(0x0),
80d13558 95 fInputHandler(0x0),
26f071d8 96 fOutputAOD(0x0),
97 fMCEvent(0x0),
98 fTreeA(0x0)
99{
100// Copy constructor
80d13558 101 fDebug = obj.fDebug;
102 fEntry = obj.fEntry;
103 fInputEvent = obj.fInputEvent;
104 fInputHandler = obj.fInputHandler;
105 fOutputAOD = obj.fOutputAOD;
106 fMCEvent = obj.fMCEvent;
107 fTreeA = obj.fTreeA;
26f071d8 108}
109
110
111AliAnalysisTaskSE& AliAnalysisTaskSE::operator=(const AliAnalysisTaskSE& other)
112{
113// Assignment
114 AliAnalysisTask::operator=(other);
80d13558 115 fDebug = other.fDebug;
116 fEntry = other.fEntry;
117 fInputEvent = other.fInputEvent;
118 fInputHandler = other.fInputHandler;
119 fOutputAOD = other.fOutputAOD;
120 fMCEvent = other.fMCEvent;
121 fTreeA = other.fTreeA;
26f071d8 122 return *this;
123}
5232d0de 124
125
126void AliAnalysisTaskSE::ConnectInputData(Option_t* /*option*/)
127{
128// Connect the input data
129 if (fDebug > 1) printf("AnalysisTaskSE::ConnectInputData() \n");
130//
131// ESD
132//
f0b15803 133 fInputHandler = (AliInputEventHandler*)
259b7a8a 134 ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
5232d0de 135//
136// Monte Carlo
137//
138 AliMCEventHandler* mcH = 0;
139 mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler());
04a79fa0 140 if (mcH) {
141 fMCEvent = mcH->MCEvent();
142 }
259b7a8a 143
144 if (fInputHandler) {
145 fInputEvent = fInputHandler->GetEvent();
146 } else if( fMCEvent ) {
147 AliWarning("No Input Event Handler connected, only MC Truth Event Handler") ;
148 } else {
149 AliError("No Input Event Handler connected") ;
150 return ;
151 }
5232d0de 152}
153
154void AliAnalysisTaskSE::CreateOutputObjects()
155{
156// Create the output container
157//
158// Default AOD
159 if (fDebug > 1) printf("AnalysisTaskSE::CreateOutPutData() \n");
160
161 AliAODHandler* handler = (AliAODHandler*)
259b7a8a 162 ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
5232d0de 163
5a922844 164 Bool_t merging = kFALSE;
165 AliAODInputHandler* aodIH = static_cast<AliAODInputHandler*>((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
166 if (aodIH) {
167 if (aodIH->GetMergeEvents()) merging = kTRUE;
168 }
169
c185e0db 170 // Check if AOD replication has been required
171
3b9a675c 172 if (handler) {
173 fOutputAOD = handler->GetAOD();
174 fTreeA = handler->GetTree();
c185e0db 175 if (!(handler->IsStandard())) {
176 if ((handler->NeedsHeaderReplication()) && !(fgAODHeader))
177 {
87022830 178 if (fDebug > 1) AliInfo("Replicating header");
179 fgAODHeader = new AliAODHeader;
180 handler->AddBranch("AliAODHeader", &fgAODHeader);
c185e0db 181 }
5a922844 182 if ((handler->NeedsTracksBranchReplication() || merging) && !(fgAODTracks))
c185e0db 183 {
184 if (fDebug > 1) AliInfo("Replicating track branch\n");
185 fgAODTracks = new TClonesArray("AliAODTrack",500);
87022830 186 fgAODTracks->SetName("tracks");
c185e0db 187 handler->AddBranch("TClonesArray", &fgAODTracks);
188 }
4dd8a1c8 189 if ((handler->NeedsVerticesBranchReplication() || merging) && !(fgAODVertices))
c185e0db 190 {
191 if (fDebug > 1) AliInfo("Replicating vertices branch\n");
192 fgAODVertices = new TClonesArray("AliAODVertex",500);
87022830 193 fgAODVertices->SetName("vertices");
c185e0db 194 handler->AddBranch("TClonesArray", &fgAODVertices);
195 }
196 if ((handler->NeedsV0sBranchReplication()) && !(fgAODV0s))
197 {
198 if (fDebug > 1) AliInfo("Replicating V0s branch\n");
199 fgAODV0s = new TClonesArray("AliAODv0",500);
87022830 200 fgAODV0s->SetName("v0s");
c185e0db 201 handler->AddBranch("TClonesArray", &fgAODV0s);
202 }
203 if ((handler->NeedsTrackletsBranchReplication()) && !(fgAODTracklets))
204 {
205 if (fDebug > 1) AliInfo("Replicating Tracklets branch\n");
87022830 206 fgAODTracklets = new AliAODTracklets("tracklets","tracklets");
c185e0db 207 handler->AddBranch("AliAODTracklets", &fgAODTracklets);
208 }
209 if ((handler->NeedsPMDClustersBranchReplication()) && !(fgAODPMDClusters))
210 {
211 if (fDebug > 1) AliInfo("Replicating PMDClusters branch\n");
212 fgAODPMDClusters = new TClonesArray("AliAODPmdCluster",500);
87022830 213 fgAODPMDClusters->SetName("pmdClusters");
c185e0db 214 handler->AddBranch("TClonesArray", &fgAODPMDClusters);
215 }
5a922844 216 if ((handler->NeedsJetsBranchReplication() || merging) && !(fgAODJets))
c185e0db 217 {
218 if (fDebug > 1) AliInfo("Replicating Jets branch\n");
219 fgAODJets = new TClonesArray("AliAODJet",500);
87022830 220 fgAODJets->SetName("jets");
c185e0db 221 handler->AddBranch("TClonesArray", &fgAODJets);
222 }
223 if ((handler->NeedsFMDClustersBranchReplication()) && !(fgAODFMDClusters))
224 {
225 AliInfo("Replicating FMDClusters branch\n");
226 fgAODFMDClusters = new TClonesArray("AliAODFmdCluster",500);
87022830 227 fgAODFMDClusters->SetName("fmdClusters");
c185e0db 228 handler->AddBranch("TClonesArray", &fgAODFMDClusters);
229 }
5a922844 230 if ((handler->NeedsCaloClustersBranchReplication() || merging) && !(fgAODCaloClusters))
c185e0db 231 {
232 if (fDebug > 1) AliInfo("Replicating CaloClusters branch\n");
233 fgAODCaloClusters = new TClonesArray("AliAODCaloCluster",500);
87022830 234 fgAODCaloClusters->SetName("caloClusters");
c185e0db 235 handler->AddBranch("TClonesArray", &fgAODCaloClusters);
5a922844 236
237 fgAODEmcalCells = new AliAODCaloCells();
238 fgAODEmcalCells->SetName("emcalCells");
239 handler->AddBranch("AliAODCaloCells", &fgAODEmcalCells);
240
241 fgAODPhosCells = new AliAODCaloCells();
242 fgAODPhosCells->SetName("phosCells");
243 handler->AddBranch("AliAODCaloCells", &fgAODPhosCells);
c185e0db 244 }
113bc2bd 245 if ((handler->NeedsMCParticlesBranchReplication() || merging) && !(fgAODMCParticles))
246 {
247 if (fDebug > 1) AliInfo("Replicating MCParticles branch\n");
248 fgAODMCParticles = new TClonesArray("AliAODMCParticle",500);
249 fgAODMCParticles->SetName("mcparticles");
250 handler->AddBranch("TClonesArray", &fgAODMCParticles);
251 }
5a922844 252 // cache the pointerd in the AODEvent
253 fOutputAOD->GetStdContent();
3f2431c4 254 }
3b9a675c 255 } else {
256 AliWarning("No AOD Event Handler connected.") ;
257 }
5232d0de 258 UserCreateOutputObjects();
259}
260
261void AliAnalysisTaskSE::Exec(Option_t* option)
262{
263//
264// Exec analysis of one event
259b7a8a 265 if (fDebug > 1) AliInfo("AliAnalysisTaskSE::Exec() \n");
04a79fa0 266
267 if( fInputHandler ) {
259b7a8a 268 fEntry = fInputHandler->GetReadEntry();
04a79fa0 269 }
270
271
259b7a8a 272 else if( fMCEvent )
273 fEntry = fMCEvent->Header()->GetEvent();
4d0a82af 274 if ( !((Entry()-1)%100) && fDebug > 0)
259b7a8a 275 AliInfo(Form("%s ----> Processing event # %lld", CurrentFileName(), Entry()));
3f2431c4 276
277 AliAODHandler* handler = (AliAODHandler*)
278 ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
c185e0db 279 AliAODInputHandler* aodH = dynamic_cast<AliAODInputHandler*>(fInputHandler);
280
281 if (handler && aodH) {
04a79fa0 282 fMCEvent = aodH->MCEvent();
5a922844 283 Bool_t merging = aodH->GetMergeEvents();
04a79fa0 284
c185e0db 285 if (!(handler->IsStandard()) && !(handler->AODIsReplicated())) {
286 if ((handler->NeedsHeaderReplication()) && (fgAODHeader))
287 {
61d5496b 288 // copy the contents by assigment
289 *fgAODHeader = *(dynamic_cast<AliAODHeader*>(InputEvent()->GetHeader()));
daa81fe5 290 }
5a922844 291 if ((handler->NeedsTracksBranchReplication() || merging) && (fgAODTracks))
c185e0db 292 {
293 TClonesArray* tracks = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracks();
294 new (fgAODTracks) TClonesArray(*tracks);
295 }
296 if ((handler->NeedsVerticesBranchReplication()) && (fgAODVertices))
297 {
298 TClonesArray* vertices = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetVertices();
299 new (fgAODVertices) TClonesArray(*vertices);
300 }
301 if ((handler->NeedsV0sBranchReplication()) && (fgAODV0s))
302 {
113bc2bd 303 TClonesArray* v0s = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetV0s();
304 new (fgAODV0s) TClonesArray(*v0s);
c185e0db 305 }
306 if ((handler->NeedsTrackletsBranchReplication()) && (fgAODTracklets))
307 {
61d5496b 308 *fgAODTracklets = *(dynamic_cast<AliAODEvent*>(InputEvent()))->GetTracklets();
c185e0db 309 }
310 if ((handler->NeedsPMDClustersBranchReplication()) && (fgAODPMDClusters))
311 {
113bc2bd 312 TClonesArray* pmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetPmdClusters();
313 new (fgAODPMDClusters) TClonesArray(*pmdClusters);
c185e0db 314 }
5a922844 315 if ((handler->NeedsJetsBranchReplication() || merging) && (fgAODJets))
c185e0db 316 {
113bc2bd 317 TClonesArray* jets = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetJets();
318 new (fgAODJets) TClonesArray(*jets);
c185e0db 319 }
320 if ((handler->NeedsFMDClustersBranchReplication()) && (fgAODFMDClusters))
321 {
113bc2bd 322 TClonesArray* fmdClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetFmdClusters();
323 new (fgAODFMDClusters) TClonesArray(*fmdClusters);
c185e0db 324 }
5a922844 325 if ((handler->NeedsCaloClustersBranchReplication() || merging) && (fgAODCaloClusters))
c185e0db 326 {
113bc2bd 327 TClonesArray* caloClusters = (dynamic_cast<AliAODEvent*>(InputEvent()))->GetCaloClusters();
328 new (fgAODCaloClusters) TClonesArray(*caloClusters);
329 }
330
331 if ((handler->NeedsMCParticlesBranchReplication() || merging) && (fgAODMCParticles))
332 {
333 TClonesArray* mcParticles = (TClonesArray*) ((dynamic_cast<AliAODEvent*>(InputEvent()))->FindListObject("mcparticles"));
334 new (fgAODMCParticles) TClonesArray(*mcParticles);
c185e0db 335 }
5a922844 336
337 // Additional merging if needed
338 if (merging) {
113bc2bd 339 // mcParticles
340 TClonesArray* mcparticles = (TClonesArray*) ((aodH->GetEventToMerge())->FindListObject("mcparticles"));
341 Int_t npart = mcparticles->GetEntries();
342 Int_t nc = fgAODMCParticles->GetEntries();
343 Int_t nc0 = nc;
344
345 for (Int_t i = 0; i < npart; i++) {
346 AliAODMCParticle* particle = (AliAODMCParticle*) mcparticles->At(i);
347 AliAODMCParticle* newpart = new((*fgAODMCParticles)[nc++]) AliAODMCParticle(*particle);
348 }
349
5a922844 350 // tracks
351 TClonesArray* tracks = aodH->GetEventToMerge()->GetTracks();
352 Int_t ntr = tracks->GetEntries();
113bc2bd 353 nc = fgAODTracks->GetEntries();
5a922844 354 for (Int_t i = 0; i < ntr; i++) {
355 AliAODTrack* track = (AliAODTrack*) tracks->At(i);
356 AliAODTrack* newtrack = new((*fgAODTracks)[nc++]) AliAODTrack(*track);
357 newtrack->ResetBit(kIsReferenced);
358 newtrack->SetUniqueID(0);
113bc2bd 359 newtrack->SetLabel(newtrack->GetLabel() + nc0);
360
5a922844 361 }
362 // clusters
363 TClonesArray* clusters = aodH->GetEventToMerge()->GetCaloClusters();
364 Int_t ncl = clusters->GetEntries();
365 nc = fgAODCaloClusters->GetEntries();
366 for (Int_t i = 0; i < ncl; i++) {
367 AliAODCaloCluster* cluster = (AliAODCaloCluster*) clusters->At(i);
368 AliAODCaloCluster* newcluster = new((*fgAODCaloClusters)[nc++]) AliAODCaloCluster(*cluster);
369 }
370 // cells
371 AliAODCaloCells* cellsA = aodH->GetEventToMerge()->GetEMCALCells();
372 Int_t ncells = cellsA->GetNumberOfCells();
373 nc = fgAODEmcalCells->GetNumberOfCells();
374
375 for (Int_t i = 0; i < ncells; i++) {
376 Int_t cn = cellsA->GetCellNumber(i);
377 Int_t pos = fgAODEmcalCells->GetCellPosition(cn);
378 if (pos >= 0) {
379 Double_t amp = cellsA->GetAmplitude(i) + fgAODEmcalCells->GetAmplitude(pos);
380 fgAODEmcalCells->SetCell(pos, cn, amp);
381 } else {
382 Double_t amp = cellsA->GetAmplitude(i);
383 fgAODEmcalCells->SetCell(nc++, cn, amp);
384 fgAODEmcalCells->Sort();
385 }
386 }
113bc2bd 387
388
389 } // merging
c185e0db 390
5a922844 391 handler->SetAODIsReplicated();
3f2431c4 392 }
393 }
394
4d0a82af 395// Call the user analysis
5232d0de 396 UserExec(option);
60a04972 397 // Added protection in case the derived task is not an AOD producer.
398 AliAnalysisDataSlot *out0 = GetOutputSlot(0);
399 if (out0 && out0->IsConnected()) PostData(0, fTreeA);
5232d0de 400}
401
4d0a82af 402const char* AliAnalysisTaskSE::CurrentFileName()
403{
404// Returns the current file name
259b7a8a 405 if( fInputHandler )
406 return fInputHandler->GetTree()->GetCurrentFile()->GetName();
407 else if( fMCEvent )
408 return ((AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()))->TreeK()->GetCurrentFile()->GetName();
8803dcb1 409 else return "";
4d0a82af 410}
5232d0de 411
3cee83d7 412void AliAnalysisTaskSE::AddAODBranch(const char* cname, void* addobj, const char *fname)
164e94ff 413{
414 // Add a new branch to the aod tree
415 AliAODHandler* handler = (AliAODHandler*)
416 ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
417 if (handler) {
3cee83d7 418 handler->AddBranch(cname, addobj, fname);
164e94ff 419 }
420}
3cee83d7 421
422Bool_t AliAnalysisTaskSE::IsStandardAOD() const
423{
424// Check if the output AOD handler is configured for standard or delta AOD.
425// Users should first check that AODEvent() returns non-null.
426 AliAODHandler* handler = (AliAODHandler*)
427 ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
428 if (!handler) {
429 Error("IsStandardAOD", "No AOD handler. Please use AODEvent() to check this first");
430 return kTRUE;
431 }
432 return handler->IsStandard();
433}