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