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