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