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